-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git: Merge branch 'master' into feature/grandpa_rating
- Loading branch information
Showing
29 changed files
with
438 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORY | ||
#define KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORY | ||
|
||
#include "primitives/babe_configuration.hpp" | ||
|
||
namespace kagome::consensus::babe { | ||
|
||
/// Keeps actual babe configuration | ||
class BabeConfigRepository { | ||
public: | ||
virtual ~BabeConfigRepository() = default; | ||
|
||
/// Returns actual babe configuration, obtaining it from runtime if needed | ||
/// @return actual babe configuration | ||
virtual const primitives::BabeConfiguration &config() = 0; | ||
}; | ||
|
||
} // namespace kagome::consensus::babe | ||
|
||
#endif // KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "babe_config_repository_impl.hpp" | ||
|
||
#include "application/app_state_manager.hpp" | ||
#include "crypto/hasher.hpp" | ||
#include "primitives/block_header.hpp" | ||
#include "runtime/runtime_api/babe_api.hpp" | ||
#include "scale.hpp" | ||
|
||
namespace kagome::consensus::babe { | ||
|
||
BabeConfigRepositoryImpl::BabeConfigRepositoryImpl( | ||
const std::shared_ptr<application::AppStateManager> &app_state_manager, | ||
std::shared_ptr<runtime::BabeApi> babe_api, | ||
std::shared_ptr<crypto::Hasher> hasher, | ||
primitives::events::ChainSubscriptionEnginePtr chain_events_engine, | ||
const primitives::GenesisBlockHeader &genesis_block_header) | ||
: babe_api_(std::move(babe_api)), | ||
hasher_(std::move(hasher)), | ||
chain_sub_([&] { | ||
BOOST_ASSERT(chain_events_engine != nullptr); | ||
return std::make_shared<primitives::events::ChainEventSubscriber>( | ||
chain_events_engine); | ||
}()), | ||
block_hash_(genesis_block_header.hash), | ||
valid_(false) { | ||
BOOST_ASSERT(babe_api_ != nullptr); | ||
BOOST_ASSERT(hasher_ != nullptr); | ||
|
||
BOOST_ASSERT(app_state_manager != nullptr); | ||
app_state_manager->atPrepare([this] { return prepare(); }); | ||
} | ||
|
||
bool BabeConfigRepositoryImpl::prepare() { | ||
chain_sub_->subscribe(chain_sub_->generateSubscriptionSetId(), | ||
primitives::events::ChainEventType::kFinalizedHeads); | ||
chain_sub_->setCallback([wp = weak_from_this()]( | ||
subscription::SubscriptionSetId, | ||
auto &&, | ||
primitives::events::ChainEventType type, | ||
const primitives::events::ChainEventParams | ||
&event) { | ||
if (type == primitives::events::ChainEventType::kFinalizedHeads) { | ||
if (auto self = wp.lock()) { | ||
auto hash = self->hasher_->blake2b_256( | ||
scale::encode( | ||
boost::get<primitives::events::HeadsEventParams>(event).get()) | ||
.value()); | ||
self->block_hash_ = hash; | ||
self->valid_ = false; | ||
} | ||
} | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
const primitives::BabeConfiguration &BabeConfigRepositoryImpl::config() { | ||
if (not valid_) { | ||
auto babe_config_res = babe_api_->configuration(block_hash_); | ||
if (babe_config_res.has_value()) { | ||
babe_configuration_ = std::move(babe_config_res.value()); | ||
valid_ = true; | ||
} | ||
} | ||
return babe_configuration_; | ||
} | ||
|
||
} // namespace kagome::consensus::babe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORYIMPL | ||
#define KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORYIMPL | ||
|
||
#include "consensus/babe/babe_config_repository.hpp" | ||
#include "primitives/block_header.hpp" | ||
#include "primitives/event_types.hpp" | ||
|
||
namespace kagome::application { | ||
class AppStateManager; | ||
} | ||
namespace kagome::crypto { | ||
class Hasher; | ||
} | ||
namespace kagome::runtime { | ||
class BabeApi; | ||
} | ||
|
||
namespace kagome::consensus::babe { | ||
|
||
class BabeConfigRepositoryImpl final | ||
: public BabeConfigRepository, | ||
public std::enable_shared_from_this<BabeConfigRepositoryImpl> { | ||
public: | ||
BabeConfigRepositoryImpl( | ||
const std::shared_ptr<application::AppStateManager> &app_state_manager, | ||
std::shared_ptr<runtime::BabeApi> babe_api, | ||
std::shared_ptr<crypto::Hasher> hasher, | ||
primitives::events::ChainSubscriptionEnginePtr, | ||
const primitives::GenesisBlockHeader &genesis_block_header); | ||
|
||
const primitives::BabeConfiguration &config() override; | ||
|
||
bool prepare(); | ||
|
||
private: | ||
std::shared_ptr<runtime::BabeApi> babe_api_; | ||
std::shared_ptr<crypto::Hasher> hasher_; | ||
std::shared_ptr<primitives::events::ChainEventSubscriber> chain_sub_; | ||
primitives::BlockHash block_hash_; | ||
|
||
mutable primitives::BabeConfiguration babe_configuration_{}; | ||
bool valid_; | ||
}; | ||
|
||
} // namespace kagome::consensus::babe | ||
|
||
#endif // KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORYIMPL |
Oops, something went wrong.