Kagome
Polkadot Runtime Engine in C++17
kagome::consensus::grandpa::GrandpaImpl Class Reference

#include <grandpa_impl.hpp>

Inheritance diagram for kagome::consensus::grandpa::GrandpaImpl:
Collaboration diagram for kagome::consensus::grandpa::GrandpaImpl:

Public Member Functions

 ~GrandpaImpl () override=default
 
 GrandpaImpl (std::shared_ptr< application::AppStateManager > app_state_manager, std::shared_ptr< Environment > environment, std::shared_ptr< crypto::Ed25519Provider > crypto_provider, std::shared_ptr< runtime::GrandpaApi > grandpa_api, const std::shared_ptr< crypto::Ed25519Keypair > &keypair, const application::ChainSpec &chain_spec, std::shared_ptr< Clock > clock, std::shared_ptr< libp2p::basic::Scheduler > scheduler, std::shared_ptr< authority::AuthorityManager > authority_manager, std::shared_ptr< network::Synchronizer > synchronizer, std::shared_ptr< network::PeerManager > peer_manager, std::shared_ptr< blockchain::BlockTree > block_tree, std::shared_ptr< network::ReputationRepository > reputation_repository)
 
bool prepare ()
 
bool start ()
 
void stop ()
 
void onNeighborMessage (const libp2p::peer::PeerId &peer_id, const network::GrandpaNeighborMessage &msg) override
 
void onCatchUpRequest (const libp2p::peer::PeerId &peer_id, const network::CatchUpRequest &msg) override
 
void onCatchUpResponse (const libp2p::peer::PeerId &peer_id, const network::CatchUpResponse &msg) override
 
void onVoteMessage (const libp2p::peer::PeerId &peer_id, const network::VoteMessage &msg) override
 
void onCommitMessage (const libp2p::peer::PeerId &peer_id, const network::FullCommitMessage &msg) override
 
outcome::result< void > applyJustification (const BlockInfo &block_info, const GrandpaJustification &justification) override
 
void tryExecuteNextRound (const std::shared_ptr< VotingRound > &round) override
 
void updateNextRound (RoundNumber round_number) override
 
- Public Member Functions inherited from kagome::consensus::grandpa::Grandpa
virtual ~Grandpa ()=default
 
- Public Member Functions inherited from kagome::consensus::grandpa::GrandpaObserver
 ~GrandpaObserver () override=default
 
- Public Member Functions inherited from kagome::consensus::grandpa::RoundObserver
virtual ~RoundObserver ()=default
 
virtual void onVoteMessage (const libp2p::peer::PeerId &peer_id, const VoteMessage &msg)=0
 
- Public Member Functions inherited from kagome::consensus::grandpa::JustificationObserver
virtual ~JustificationObserver ()=default
 
- Public Member Functions inherited from kagome::consensus::grandpa::CatchUpObserver
virtual ~CatchUpObserver ()=default
 
- Public Member Functions inherited from kagome::consensus::grandpa::NeighborObserver
virtual ~NeighborObserver ()=default
 

Static Public Attributes

static const size_t kCatchUpThreshold = 2
 
static const size_t kKeepRecentRounds = 3
 Maximum number of rounds we are keep to communication. More...
 
static constexpr Clock::Duration kCatchupRequestTimeout
 Timeout of waiting catchup response for request. More...
 

Private Member Functions

std::optional< std::shared_ptr< VotingRound > > selectRound (RoundNumber round_number, std::optional< VoterSetId > voter_set_id)
 
outcome::result< MovableRoundStategetLastCompletedRound () const
 
std::shared_ptr< VotingRoundmakeInitialRound (const MovableRoundState &round_state, std::shared_ptr< VoterSet > voters)
 
std::shared_ptr< VotingRoundmakeNextRound (const std::shared_ptr< VotingRound > &previous_round)
 
void loadMissingBlocks ()
 

Private Attributes

const Clock::Duration round_time_factor_
 
std::shared_ptr< Environmentenvironment_
 
std::shared_ptr< crypto::Ed25519Providercrypto_provider_
 
std::shared_ptr< runtime::GrandpaApigrandpa_api_
 
const std::shared_ptr< crypto::Ed25519Keypair > & keypair_
 
std::shared_ptr< Clockclock_
 
std::shared_ptr< libp2p::basic::Scheduler > scheduler_
 
std::shared_ptr< authority::AuthorityManagerauthority_manager_
 
std::shared_ptr< network::Synchronizersynchronizer_
 
std::shared_ptr< network::PeerManagerpeer_manager_
 
std::shared_ptr< blockchain::BlockTreeblock_tree_
 
std::shared_ptr< network::ReputationRepositoryreputation_repository_
 
std::shared_ptr< VotingRoundcurrent_round_
 
std::optional< const std::tuple< libp2p::peer::PeerId, network::CatchUpRequest > > pending_catchup_request_
 
libp2p::basic::Scheduler::Handle catchup_request_timer_handle_
 
libp2p::basic::Scheduler::Handle fallback_timer_handle_
 
metrics::RegistryPtr metrics_registry_ = metrics::createRegistry()
 
metrics::Gaugemetric_highest_round_
 
log::Logger logger_ = log::createLogger("Grandpa", "grandpa")
 

Detailed Description

Main entry point to grandpa consensus implementation in KAGOME:

  • starts grandpa consensus execution
  • keeps track of current rounds
    • we should be able to receive votes for current and previous rounds
  • processes incoming grandpa messages

processes rounds catch ups

GrandpaImpl entity is registered in kagome::application::AppStateManager, which executes prepare(), start() and stop() functions to manage execution of consensus algorithm

When start is invoked we execute new grandpa round and gossip neighbour messages.

Also we start receiving other grandpa messages over paritytech/grandpa/1 protocol (see kagome::network::GrandpaProtocol::start()). If we notice that we are lagging behind the most recent grandpa round in the network, then catch up procedure is executed (see GrandpaImpl::onNeighborMessage() and EnvironmentImpl::onCatchUpRequested()). Catch up process is also described in the spec

Otherwise if no catch up is needed, we proceed executing grandpa round until it is completable and previous round has finalized block

Definition at line 57 of file grandpa_impl.hpp.

Constructor & Destructor Documentation

kagome::consensus::grandpa::GrandpaImpl::~GrandpaImpl ( )
overridedefault
kagome::consensus::grandpa::GrandpaImpl::GrandpaImpl ( std::shared_ptr< application::AppStateManager app_state_manager,
std::shared_ptr< Environment environment,
std::shared_ptr< crypto::Ed25519Provider crypto_provider,
std::shared_ptr< runtime::GrandpaApi grandpa_api,
const std::shared_ptr< crypto::Ed25519Keypair > &  keypair,
const application::ChainSpec chain_spec,
std::shared_ptr< Clock clock,
std::shared_ptr< libp2p::basic::Scheduler >  scheduler,
std::shared_ptr< authority::AuthorityManager authority_manager,
std::shared_ptr< network::Synchronizer synchronizer,
std::shared_ptr< network::PeerManager peer_manager,
std::shared_ptr< blockchain::BlockTree block_tree,
std::shared_ptr< network::ReputationRepository reputation_repository 
)

Definition at line 42 of file grandpa_impl.cpp.

Member Function Documentation

outcome::result< void > kagome::consensus::grandpa::GrandpaImpl::applyJustification ( const BlockInfo block_info,
const GrandpaJustification justification 
)
overridevirtual

Selects round that corresponds for justification, checks justification, finalizes corresponding block and stores justification in storage

If there is no corresponding round, it will be created

Parameters
block_infoblock being finalized by justification
justificationjustification containing precommit votes and signatures for block info
Returns
nothing or an error

Implements kagome::consensus::grandpa::JustificationObserver.

Definition at line 1117 of file grandpa_impl.cpp.

outcome::result< MovableRoundState > kagome::consensus::grandpa::GrandpaImpl::getLastCompletedRound ( ) const
private
Returns
Get grandpa::MovableRoundState for the last completed round

Definition at line 291 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::loadMissingBlocks ( )
private

Request blocks that are missing to run consensus (for example when we cannot accept precommit when there is no corresponding block)

Definition at line 1223 of file grandpa_impl.cpp.

std::shared_ptr< VotingRound > kagome::consensus::grandpa::GrandpaImpl::makeInitialRound ( const MovableRoundState round_state,
std::shared_ptr< VoterSet voters 
)
private

Initializes new round by provided round state and voter. Note that round created that way is instantly ended. Because it is needed only to become basis of the next round

Parameters
round_stateinformation required to execute the round
votersis the set of voters for this round
Returns
VotingRound that we can execute

Definition at line 175 of file grandpa_impl.cpp.

std::shared_ptr< VotingRound > kagome::consensus::grandpa::GrandpaImpl::makeNextRound ( const std::shared_ptr< VotingRound > &  previous_round)
private

Takes given round and creates next one for it

Parameters
previous_roundVotingRound from which the new one is created
Returns
new VotingRound

Definition at line 208 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::onCatchUpRequest ( const libp2p::peer::PeerId &  peer_id,
const network::CatchUpRequest msg 
)
overridevirtual

Catch up request processing according to spec

We check voter set ids between ours and remote peer match. Then we check politeness of request and send response containing state for requested round

Parameters
peer_idid of the peer that sent catch up request
msgnetwork message containing catch up request

Implements kagome::consensus::grandpa::CatchUpObserver.

Definition at line 485 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::onCatchUpResponse ( const libp2p::peer::PeerId &  peer_id,
const network::CatchUpResponse msg 
)
overridevirtual

Catch up response processing according to spec

Response is ignored if remote peer's voter set id does not match ours peer id Response is ignored if message contains info about round in the past (earlier than our current round) If message is received from the future round we create round from the round state information in response, check round for completeness and execute the round following the round from response

Parameters
peer_idid of remote peer that sent catch up response
msgmessage containing catch up response

Implements kagome::consensus::grandpa::CatchUpObserver.

Definition at line 602 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::onCommitMessage ( const libp2p::peer::PeerId &  peer_id,
const network::FullCommitMessage msg 
)
overridevirtual

Processing of commit message

We check commit message for politeness and send it to GrandpaImpl::applyJustification()

Parameters
peer_idid of remote peer
msgmessage containing commit message with justification

Implements kagome::consensus::grandpa::RoundObserver.

Definition at line 1003 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::onNeighborMessage ( const libp2p::peer::PeerId &  peer_id,
const network::GrandpaNeighborMessage msg 
)
overridevirtual

Processes grandpa neighbour message Neighbour message is ignored if voter set ids between our and receiving peer do not match. Otherwsie if our peer is behind by grandpa rounds by more than GrandpaImpl::kCatchUpThreshold, then catch request is sent to the peer that sent us a message (see GrandpaImpl::onCatchUpRequest()).

Otherwise our peer will send back the response containing known votes for the round in msg (if any)

Parameters
peer_idid of the peer that sent the message
msgreceived grandpa neighbour message

Implements kagome::consensus::grandpa::NeighborObserver.

Definition at line 363 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::onVoteMessage ( const libp2p::peer::PeerId &  peer_id,
const network::VoteMessage &  msg 
)
override

Processing of vote messages

Vote messages are ignored if they are not sent politely Otherwise, we check if we have the round that corresponds to the received message and process it by this round

Parameters
peer_idid of remote peer
msgvote message that could be either primary propose, prevote, or precommit message

Definition at line 806 of file grandpa_impl.cpp.

bool kagome::consensus::grandpa::GrandpaImpl::prepare ( )

Prepares for grandpa round execution: e.g. sets justification observer handler.

Returns
true preparation was done with no issues
See also
kagome::application::AppStateManager::takeControl()

Definition at line 93 of file grandpa_impl.cpp.

std::optional< std::shared_ptr< VotingRound > > kagome::consensus::grandpa::GrandpaImpl::selectRound ( RoundNumber  round_number,
std::optional< VoterSetId voter_set_id 
)
private

Selects round by provided number and voter set id

Parameters
round_numbernumber of round to be selected
voter_set_idid of voter set for corresponding round
Returns
optional<shared_ptr> containing the round if we have one and nullopt otherwise

Definition at line 266 of file grandpa_impl.cpp.

bool kagome::consensus::grandpa::GrandpaImpl::start ( )

Initiates grandpa voting process e.g.:

Definition at line 99 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::stop ( )

Does nothing. Needed only for AppStateManager

See also
kagome::application::AppStateManager::takeControl()

Definition at line 171 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::tryExecuteNextRound ( const std::shared_ptr< VotingRound > &  round)
overridevirtual

Creates and executes round that follows the round with provided round_number

Also this method removes old round, so that only 3 rounds in total are stored at any moment

Parameters
roundprevious round from which new one is created and executed

Implements kagome::consensus::grandpa::Grandpa.

Definition at line 323 of file grandpa_impl.cpp.

void kagome::consensus::grandpa::GrandpaImpl::updateNextRound ( RoundNumber  round_number)
overridevirtual

Selects round next to provided one and updates it by checking if prevote_ghost, estimate and finalized block were updated.

See also
VotingRound::update()
Parameters
round_numberthe round after which we select the round for update

Implements kagome::consensus::grandpa::Grandpa.

Definition at line 353 of file grandpa_impl.cpp.

Member Data Documentation

std::shared_ptr<authority::AuthorityManager> kagome::consensus::grandpa::GrandpaImpl::authority_manager_
private

Definition at line 270 of file grandpa_impl.hpp.

std::shared_ptr<blockchain::BlockTree> kagome::consensus::grandpa::GrandpaImpl::block_tree_
private

Definition at line 273 of file grandpa_impl.hpp.

libp2p::basic::Scheduler::Handle kagome::consensus::grandpa::GrandpaImpl::catchup_request_timer_handle_
private

Definition at line 280 of file grandpa_impl.hpp.

std::shared_ptr<Clock> kagome::consensus::grandpa::GrandpaImpl::clock_
private

Definition at line 268 of file grandpa_impl.hpp.

std::shared_ptr<crypto::Ed25519Provider> kagome::consensus::grandpa::GrandpaImpl::crypto_provider_
private

Definition at line 265 of file grandpa_impl.hpp.

std::shared_ptr<VotingRound> kagome::consensus::grandpa::GrandpaImpl::current_round_
private

Definition at line 276 of file grandpa_impl.hpp.

std::shared_ptr<Environment> kagome::consensus::grandpa::GrandpaImpl::environment_
private

Definition at line 264 of file grandpa_impl.hpp.

libp2p::basic::Scheduler::Handle kagome::consensus::grandpa::GrandpaImpl::fallback_timer_handle_
private

Definition at line 281 of file grandpa_impl.hpp.

std::shared_ptr<runtime::GrandpaApi> kagome::consensus::grandpa::GrandpaImpl::grandpa_api_
private

Definition at line 266 of file grandpa_impl.hpp.

constexpr Clock::Duration kagome::consensus::grandpa::GrandpaImpl::kCatchupRequestTimeout
static
Initial value:
=
std::chrono::milliseconds(45'000)

Timeout of waiting catchup response for request.

Definition at line 69 of file grandpa_impl.hpp.

const size_t kagome::consensus::grandpa::GrandpaImpl::kCatchUpThreshold = 2
static

Maximum number of rounds we are behind a peer before issuing a catch up request.

Definition at line 63 of file grandpa_impl.hpp.

const std::shared_ptr<crypto::Ed25519Keypair>& kagome::consensus::grandpa::GrandpaImpl::keypair_
private

Definition at line 267 of file grandpa_impl.hpp.

const size_t kagome::consensus::grandpa::GrandpaImpl::kKeepRecentRounds = 3
static

Maximum number of rounds we are keep to communication.

Definition at line 66 of file grandpa_impl.hpp.

log::Logger kagome::consensus::grandpa::GrandpaImpl::logger_ = log::createLogger("Grandpa", "grandpa")
private

Definition at line 287 of file grandpa_impl.hpp.

metrics::Gauge* kagome::consensus::grandpa::GrandpaImpl::metric_highest_round_
private

Definition at line 285 of file grandpa_impl.hpp.

metrics::RegistryPtr kagome::consensus::grandpa::GrandpaImpl::metrics_registry_ = metrics::createRegistry()
private

Definition at line 284 of file grandpa_impl.hpp.

std::shared_ptr<network::PeerManager> kagome::consensus::grandpa::GrandpaImpl::peer_manager_
private

Definition at line 272 of file grandpa_impl.hpp.

std::optional< const std::tuple<libp2p::peer::PeerId, network::CatchUpRequest> > kagome::consensus::grandpa::GrandpaImpl::pending_catchup_request_
private

Definition at line 279 of file grandpa_impl.hpp.

std::shared_ptr<network::ReputationRepository> kagome::consensus::grandpa::GrandpaImpl::reputation_repository_
private

Definition at line 274 of file grandpa_impl.hpp.

const Clock::Duration kagome::consensus::grandpa::GrandpaImpl::round_time_factor_
private

Definition at line 262 of file grandpa_impl.hpp.

std::shared_ptr<libp2p::basic::Scheduler> kagome::consensus::grandpa::GrandpaImpl::scheduler_
private

Definition at line 269 of file grandpa_impl.hpp.

std::shared_ptr<network::Synchronizer> kagome::consensus::grandpa::GrandpaImpl::synchronizer_
private

Definition at line 271 of file grandpa_impl.hpp.


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