Kagome
Polkadot Runtime Engine in C++17
kagome::network::SynchronizerImpl Class Reference

#include <synchronizer_impl.hpp>

Inheritance diagram for kagome::network::SynchronizerImpl:
Collaboration diagram for kagome::network::SynchronizerImpl:

Classes

struct  KnownBlock
 

Public Types

enum  Error {
  Error::SHUTTING_DOWN = 1, Error::EMPTY_RESPONSE, Error::RESPONSE_WITHOUT_BLOCK_HEADER, Error::RESPONSE_WITHOUT_BLOCK_BODY,
  Error::DISCARDED_BLOCK, Error::WRONG_ORDER, Error::INVALID_HASH, Error::ALREADY_IN_QUEUE,
  Error::PEER_BUSY, Error::ARRIVED_TOO_EARLY, Error::DUPLICATE_REQUEST
}
 
- Public Types inherited from kagome::network::Synchronizer
using SyncResultHandler = std::function< void(outcome::result< primitives::BlockInfo >)>
 

Public Member Functions

 SynchronizerImpl (const application::AppConfiguration &app_config, std::shared_ptr< application::AppStateManager > app_state_manager, std::shared_ptr< blockchain::BlockTree > block_tree, std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker, std::shared_ptr< consensus::BlockAppender > block_appender, std::shared_ptr< consensus::BlockExecutor > block_executor, std::shared_ptr< storage::trie::TrieSerializer > serializer, std::shared_ptr< storage::trie::TrieStorage > storage, std::shared_ptr< network::Router > router, std::shared_ptr< libp2p::basic::Scheduler > scheduler, std::shared_ptr< crypto::Hasher > hasher, std::shared_ptr< storage::BufferStorage > buffer_storage)
 
bool prepare ()
 
bool start ()
 
void stop ()
 
bool syncByBlockInfo (const primitives::BlockInfo &block_info, const libp2p::peer::PeerId &peer_id, SyncResultHandler &&handler, bool subscribe_to_block) override
 
bool syncByBlockHeader (const primitives::BlockHeader &header, const libp2p::peer::PeerId &peer_id, SyncResultHandler &&handler) override
 
void syncMissingJustifications (const PeerId &peer_id, primitives::BlockInfo target_block, std::optional< uint32_t > limit, SyncResultHandler &&handler) override
 
void syncState (const libp2p::peer::PeerId &peer_id, const primitives::BlockInfo &block, SyncResultHandler &&handler) override
 
void findCommonBlock (const libp2p::peer::PeerId &peer_id, primitives::BlockNumber lower, primitives::BlockNumber upper, primitives::BlockNumber hint, SyncResultHandler &&handler, std::map< primitives::BlockNumber, primitives::BlockHash > &&observed={})
 
void loadBlocks (const libp2p::peer::PeerId &peer_id, primitives::BlockInfo from, SyncResultHandler &&handler)
 
void loadJustifications (const libp2p::peer::PeerId &peer_id, primitives::BlockInfo target_block, std::optional< uint32_t > limit, SyncResultHandler &&handler)
 
bool hasIncompleteRequestOfStateSync () const override
 Check if incomplete requests of state sync exists. More...
 
- Public Member Functions inherited from kagome::network::Synchronizer
virtual ~Synchronizer ()=default
 

Static Public Attributes

static constexpr size_t kMinPreloadedBlockAmount = 256
 
static constexpr size_t kMinPreloadedBlockAmountForFastSyncing
 
static constexpr size_t kMaxDistanceToBlockForSubscription
 
static constexpr std::chrono::milliseconds kRecentnessDuration
 

Private Types

using JustificationPair = std::pair< primitives::BlockInfo, primitives::Justification >
 

Private Member Functions

bool subscribeToBlock (const primitives::BlockInfo &block_info, SyncResultHandler &&handler)
 
void notifySubscribers (const primitives::BlockInfo &block_info, outcome::result< void > res)
 Notifies subscribers about arrived block. More...
 
void askNextPortionOfBlocks ()
 Tries to request another portion of block. More...
 
void applyNextBlock ()
 Pops next block from queue and tries to apply that. More...
 
void applyNextJustification ()
 Pops next justification from queue and tries to apply it. More...
 
size_t discardBlock (const primitives::BlockHash &block)
 
void prune (const primitives::BlockInfo &finalized_block)
 
void scheduleRecentRequestRemoval (const libp2p::peer::PeerId &peer_id, const BlocksRequest::Fingerprint &fingerprint)
 

Private Attributes

std::shared_ptr< application::AppStateManagerapp_state_manager_
 
std::shared_ptr< blockchain::BlockTreeblock_tree_
 
std::shared_ptr< storage::changes_trie::ChangesTrackertrie_changes_tracker_
 
std::shared_ptr< consensus::BlockAppenderblock_appender_
 
std::shared_ptr< consensus::BlockExecutorblock_executor_
 
std::shared_ptr< storage::trie::TrieSerializerserializer_
 
std::shared_ptr< storage::trie::TrieStoragestorage_
 
std::shared_ptr< network::Routerrouter_
 
std::shared_ptr< libp2p::basic::Scheduler > scheduler_
 
std::shared_ptr< crypto::Hasherhasher_
 
std::shared_ptr< storage::BufferStoragebuffer_storage_
 
application::AppConfiguration::SyncMethod sync_method_
 
metrics::RegistryPtr metrics_registry_ = metrics::createRegistry()
 
metrics::Gaugemetric_import_queue_length_
 
log::Logger log_ = log::createLogger("Synchronizer", "synchronizer")
 
telemetry::Telemetry telemetry_ = telemetry::createTelemetryService()
 
std::atomic_bool state_sync_request_in_progress_ = false
 
std::optional< network::StateRequeststate_sync_request_
 
std::optional< primitives::BlockInfostate_sync_on_block_
 
bool node_is_shutting_down_ = false
 
std::unordered_map< primitives::BlockHash, KnownBlockknown_blocks_
 
std::optional< primitives::BlockInfosync_block_
 
std::multimap< primitives::BlockNumber, primitives::BlockHashgenerations_
 
std::unordered_multimap< primitives::BlockHash, primitives::BlockHashancestry_
 
std::queue< JustificationPairjustifications_
 
std::mutex justifications_mutex_
 
primitives::BlockNumber watched_blocks_number_ {}
 
std::unordered_multimap< primitives::BlockHash, SyncResultHandlerwatched_blocks_
 
std::multimap< primitives::BlockInfo, SyncResultHandlersubscriptions_
 
std::atomic_bool applying_in_progress_ = false
 
std::atomic_bool asking_blocks_portion_in_progress_ = false
 
std::set< libp2p::peer::PeerId > busy_peers_
 
std::set< std::tuple< libp2p::peer::PeerId, BlocksRequest::Fingerprint > > recent_requests_
 
std::unordered_map< storage::trie::RootHash, std::tuple< common::Buffer, unsigned, std::shared_ptr< storage::trie::PersistentTrieBatch > > > batches_store_
 
size_t entries_ {0}
 

Detailed Description

Definition at line 41 of file synchronizer_impl.hpp.


Class Documentation

struct kagome::network::SynchronizerImpl::KnownBlock

Definition at line 229 of file synchronizer_impl.hpp.

Collaboration diagram for kagome::network::SynchronizerImpl::KnownBlock:
Class Members
BlockData data Data of block.
set< PeerId > peers Peers who know this block.

Member Typedef Documentation

Member Enumeration Documentation

Enumerator
SHUTTING_DOWN 
EMPTY_RESPONSE 
RESPONSE_WITHOUT_BLOCK_HEADER 
RESPONSE_WITHOUT_BLOCK_BODY 
DISCARDED_BLOCK 
WRONG_ORDER 
INVALID_HASH 
ALREADY_IN_QUEUE 
PEER_BUSY 
ARRIVED_TOO_EARLY 
DUPLICATE_REQUEST 

Definition at line 64 of file synchronizer_impl.hpp.

Constructor & Destructor Documentation

kagome::network::SynchronizerImpl::SynchronizerImpl ( const application::AppConfiguration app_config,
std::shared_ptr< application::AppStateManager app_state_manager,
std::shared_ptr< blockchain::BlockTree block_tree,
std::shared_ptr< storage::changes_trie::ChangesTracker changes_tracker,
std::shared_ptr< consensus::BlockAppender block_appender,
std::shared_ptr< consensus::BlockExecutor block_executor,
std::shared_ptr< storage::trie::TrieSerializer serializer,
std::shared_ptr< storage::trie::TrieStorage storage,
std::shared_ptr< network::Router router,
std::shared_ptr< libp2p::basic::Scheduler >  scheduler,
std::shared_ptr< crypto::Hasher hasher,
std::shared_ptr< storage::BufferStorage buffer_storage 
)

Definition at line 70 of file synchronizer_impl.cpp.

Member Function Documentation

void kagome::network::SynchronizerImpl::applyNextBlock ( )
private

Pops next block from queue and tries to apply that.

Definition at line 1112 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::applyNextJustification ( )
private

Pops next justification from queue and tries to apply it.

Definition at line 1248 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::askNextPortionOfBlocks ( )
private

Tries to request another portion of block.

Definition at line 1356 of file synchronizer_impl.cpp.

size_t kagome::network::SynchronizerImpl::discardBlock ( const primitives::BlockHash block)
private

Removes block {

Parameters
block}and all all dependent on it from the queue
Returns
number of affected blocks

Definition at line 1283 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::findCommonBlock ( const libp2p::peer::PeerId &  peer_id,
primitives::BlockNumber  lower,
primitives::BlockNumber  upper,
primitives::BlockNumber  hint,
SyncResultHandler &&  handler,
std::map< primitives::BlockNumber, primitives::BlockHash > &&  observed = {} 
)

Finds best common block with peer {

Parameters
peer_id}in provided interval. It is using tail-recursive algorithm, till {
hint}is the needed block
loweris number of definitely known common block (i.e. last finalized).
upperis number of definitely unknown block.
hintis examining block for current call
handlercallback what is called at the end of process

Definition at line 406 of file synchronizer_impl.cpp.

bool kagome::network::SynchronizerImpl::hasIncompleteRequestOfStateSync ( ) const
inlineoverridevirtual

Check if incomplete requests of state sync exists.

Implements kagome::network::Synchronizer.

Definition at line 163 of file synchronizer_impl.hpp.

void kagome::network::SynchronizerImpl::loadBlocks ( const libp2p::peer::PeerId &  peer_id,
primitives::BlockInfo  from,
SyncResultHandler &&  handler 
)

Loads blocks from peer {

Parameters
peer_id}since block {
from}till its best. Calls {
handler}when process is finished or failed

Definition at line 583 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::loadJustifications ( const libp2p::peer::PeerId &  peer_id,
primitives::BlockInfo  target_block,
std::optional< uint32_t >  limit,
SyncResultHandler &&  handler 
)

Loads block justification from {

Parameters
peer_id}for {
target_block}or a range of blocks up to {
upper_bound_block}.Calls {
handler}when operation finishes

Definition at line 806 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::notifySubscribers ( const primitives::BlockInfo block_info,
outcome::result< void >  res 
)
private

Notifies subscribers about arrived block.

Definition at line 191 of file synchronizer_impl.cpp.

bool kagome::network::SynchronizerImpl::prepare ( )
See also
AppStateManager::takeControl

Definition at line 118 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::prune ( const primitives::BlockInfo finalized_block)
private

Removes blocks what will never be applied because they are contained in side-branch for provided finalized block {

Parameters
finalized_block}

Definition at line 1313 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::scheduleRecentRequestRemoval ( const libp2p::peer::PeerId &  peer_id,
const BlocksRequest::Fingerprint fingerprint 
)
private

Purges internal cache of recent requests for specified {

Parameters
peer_id}and {
fingerprint}after kRecentnessDuration timeout

Definition at line 1344 of file synchronizer_impl.cpp.

bool kagome::network::SynchronizerImpl::start ( )
See also
AppStateManager::takeControl

Definition at line 148 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::stop ( )
See also
AppStateManager::takeControl

Definition at line 153 of file synchronizer_impl.cpp.

bool kagome::network::SynchronizerImpl::subscribeToBlock ( const primitives::BlockInfo block_info,
SyncResultHandler &&  handler 
)
private

Subscribes handler for block with provided {

Parameters
block_info}{
handler}will be called When block is received or discarded
Returns
true if subscription is successful

Definition at line 157 of file synchronizer_impl.cpp.

bool kagome::network::SynchronizerImpl::syncByBlockHeader ( const primitives::BlockHeader header,
const libp2p::peer::PeerId &  peer_id,
Synchronizer::SyncResultHandler &&  handler 
)
overridevirtual

Enqueues loading and applying block {

Parameters
block_info}from peer {
peer_id}.
Returns
true if sync is ran (peer is not busy) If provided block is the best after applying, {
Parameters
handler}be called

Implements kagome::network::Synchronizer.

Definition at line 318 of file synchronizer_impl.cpp.

bool kagome::network::SynchronizerImpl::syncByBlockInfo ( const primitives::BlockInfo block_info,
const libp2p::peer::PeerId &  peer_id,
Synchronizer::SyncResultHandler &&  handler,
bool  subscribe_to_block 
)
overridevirtual

Enqueues loading (and applying) blocks from peer {

Parameters
peer_id}since best common block up to provided {
block_info}.{
handler}will be called when this process is finished or failed
Returns
true if sync is ran (peer is not busy)
Note
Is used for start/continue catching up.

Implements kagome::network::Synchronizer.

Definition at line 209 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::syncMissingJustifications ( const PeerId peer_id,
primitives::BlockInfo  target_block,
std::optional< uint32_t >  limit,
SyncResultHandler &&  handler 
)
overridevirtual

Tries to request block justifications from {

Parameters
peer_id}for {
target_block}or a range of blocks up to {
limit}count. Calls {
handler}when operation finishes

Implements kagome::network::Synchronizer.

Definition at line 376 of file synchronizer_impl.cpp.

void kagome::network::SynchronizerImpl::syncState ( const libp2p::peer::PeerId &  peer_id,
const primitives::BlockInfo block,
SyncResultHandler &&  handler 
)
overridevirtual

Enqueues loading and applying state on block {

Parameters
block}from peer {
peer_id}.If finished, {
handler}be called

Implements kagome::network::Synchronizer.

Definition at line 921 of file synchronizer_impl.cpp.

Member Data Documentation

std::unordered_multimap<primitives::BlockHash, primitives::BlockHash> kagome::network::SynchronizerImpl::ancestry_
private

Definition at line 246 of file synchronizer_impl.hpp.

std::shared_ptr<application::AppStateManager> kagome::network::SynchronizerImpl::app_state_manager_
private

Definition at line 201 of file synchronizer_impl.hpp.

std::atomic_bool kagome::network::SynchronizerImpl::applying_in_progress_ = false
private

Definition at line 263 of file synchronizer_impl.hpp.

std::atomic_bool kagome::network::SynchronizerImpl::asking_blocks_portion_in_progress_ = false
private

Definition at line 264 of file synchronizer_impl.hpp.

std::unordered_map< storage::trie::RootHash, std::tuple<common::Buffer, unsigned, std::shared_ptr<storage::trie::PersistentTrieBatch> > > kagome::network::SynchronizerImpl::batches_store_
private

Definition at line 275 of file synchronizer_impl.hpp.

std::shared_ptr<consensus::BlockAppender> kagome::network::SynchronizerImpl::block_appender_
private

Definition at line 205 of file synchronizer_impl.hpp.

std::shared_ptr<consensus::BlockExecutor> kagome::network::SynchronizerImpl::block_executor_
private

Definition at line 206 of file synchronizer_impl.hpp.

std::shared_ptr<blockchain::BlockTree> kagome::network::SynchronizerImpl::block_tree_
private

Definition at line 202 of file synchronizer_impl.hpp.

std::shared_ptr<storage::BufferStorage> kagome::network::SynchronizerImpl::buffer_storage_
private

Definition at line 212 of file synchronizer_impl.hpp.

std::set<libp2p::peer::PeerId> kagome::network::SynchronizerImpl::busy_peers_
private

Definition at line 265 of file synchronizer_impl.hpp.

size_t kagome::network::SynchronizerImpl::entries_ {0}
private

Definition at line 276 of file synchronizer_impl.hpp.

std::multimap<primitives::BlockNumber, primitives::BlockHash> kagome::network::SynchronizerImpl::generations_
private

Definition at line 242 of file synchronizer_impl.hpp.

std::shared_ptr<crypto::Hasher> kagome::network::SynchronizerImpl::hasher_
private

Definition at line 211 of file synchronizer_impl.hpp.

std::queue<JustificationPair> kagome::network::SynchronizerImpl::justifications_
private

Definition at line 251 of file synchronizer_impl.hpp.

std::mutex kagome::network::SynchronizerImpl::justifications_mutex_
private

Definition at line 252 of file synchronizer_impl.hpp.

constexpr size_t kagome::network::SynchronizerImpl::kMaxDistanceToBlockForSubscription
static
Initial value:

Indicating how far the block can be subscribed to. In general we don't needed wait very far blocks. This limit to avoid extra memory consumption.

Definition at line 58 of file synchronizer_impl.hpp.

constexpr size_t kagome::network::SynchronizerImpl::kMinPreloadedBlockAmount = 256
static

Block amount enough for applying and preloading other ones simultaneously. 256 is doubled max amount block in BlocksResponse.

Definition at line 48 of file synchronizer_impl.hpp.

constexpr size_t kagome::network::SynchronizerImpl::kMinPreloadedBlockAmountForFastSyncing
static
Initial value:

Block amount enough for applying and preloading other ones simultaneously during fast syncing

Definition at line 52 of file synchronizer_impl.hpp.

std::unordered_map<primitives::BlockHash, KnownBlock> kagome::network::SynchronizerImpl::known_blocks_
private

Definition at line 237 of file synchronizer_impl.hpp.

constexpr std::chrono::milliseconds kagome::network::SynchronizerImpl::kRecentnessDuration
static
Initial value:
=
std::chrono::seconds(60)

Definition at line 61 of file synchronizer_impl.hpp.

log::Logger kagome::network::SynchronizerImpl::log_ = log::createLogger("Synchronizer", "synchronizer")
private

Definition at line 220 of file synchronizer_impl.hpp.

metrics::Gauge* kagome::network::SynchronizerImpl::metric_import_queue_length_
private

Definition at line 218 of file synchronizer_impl.hpp.

metrics::RegistryPtr kagome::network::SynchronizerImpl::metrics_registry_ = metrics::createRegistry()
private

Definition at line 217 of file synchronizer_impl.hpp.

bool kagome::network::SynchronizerImpl::node_is_shutting_down_ = false
private

Definition at line 227 of file synchronizer_impl.hpp.

std::set<std::tuple<libp2p::peer::PeerId, BlocksRequest::Fingerprint> > kagome::network::SynchronizerImpl::recent_requests_
private

Definition at line 268 of file synchronizer_impl.hpp.

std::shared_ptr<network::Router> kagome::network::SynchronizerImpl::router_
private

Definition at line 209 of file synchronizer_impl.hpp.

std::shared_ptr<libp2p::basic::Scheduler> kagome::network::SynchronizerImpl::scheduler_
private

Definition at line 210 of file synchronizer_impl.hpp.

std::shared_ptr<storage::trie::TrieSerializer> kagome::network::SynchronizerImpl::serializer_
private

Definition at line 207 of file synchronizer_impl.hpp.

std::optional<primitives::BlockInfo> kagome::network::SynchronizerImpl::state_sync_on_block_
private

Definition at line 225 of file synchronizer_impl.hpp.

std::optional<network::StateRequest> kagome::network::SynchronizerImpl::state_sync_request_
private

Definition at line 224 of file synchronizer_impl.hpp.

std::atomic_bool kagome::network::SynchronizerImpl::state_sync_request_in_progress_ = false
private

Definition at line 223 of file synchronizer_impl.hpp.

std::shared_ptr<storage::trie::TrieStorage> kagome::network::SynchronizerImpl::storage_
private

Definition at line 208 of file synchronizer_impl.hpp.

std::multimap<primitives::BlockInfo, SyncResultHandler> kagome::network::SynchronizerImpl::subscriptions_
private

Definition at line 261 of file synchronizer_impl.hpp.

std::optional<primitives::BlockInfo> kagome::network::SynchronizerImpl::sync_block_
private

Definition at line 239 of file synchronizer_impl.hpp.

application::AppConfiguration::SyncMethod kagome::network::SynchronizerImpl::sync_method_
private

Definition at line 214 of file synchronizer_impl.hpp.

telemetry::Telemetry kagome::network::SynchronizerImpl::telemetry_ = telemetry::createTelemetryService()
private

Definition at line 221 of file synchronizer_impl.hpp.

std::shared_ptr<storage::changes_trie::ChangesTracker> kagome::network::SynchronizerImpl::trie_changes_tracker_
private

Definition at line 204 of file synchronizer_impl.hpp.

std::unordered_multimap<primitives::BlockHash, SyncResultHandler> kagome::network::SynchronizerImpl::watched_blocks_
private

Definition at line 259 of file synchronizer_impl.hpp.

primitives::BlockNumber kagome::network::SynchronizerImpl::watched_blocks_number_ {}
private

Definition at line 255 of file synchronizer_impl.hpp.


The documentation for this class was generated from the following files: