Kagome
Polkadot Runtime Engine in C++17
|
#include <grandpa_impl.hpp>
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< MovableRoundState > | getLastCompletedRound () const |
std::shared_ptr< VotingRound > | makeInitialRound (const MovableRoundState &round_state, std::shared_ptr< VoterSet > voters) |
std::shared_ptr< VotingRound > | makeNextRound (const std::shared_ptr< VotingRound > &previous_round) |
void | loadMissingBlocks () |
Main entry point to grandpa consensus implementation in KAGOME:
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.
|
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.
|
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
block_info | block being finalized by justification |
justification | justification containing precommit votes and signatures for block info |
Implements kagome::consensus::grandpa::JustificationObserver.
Definition at line 1117 of file grandpa_impl.cpp.
|
private |
Definition at line 291 of file grandpa_impl.cpp.
|
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.
|
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
round_state | information required to execute the round |
voters | is the set of voters for this round |
Definition at line 175 of file grandpa_impl.cpp.
|
private |
Takes given round and creates next one for it
previous_round | VotingRound from which the new one is created |
Definition at line 208 of file grandpa_impl.cpp.
|
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
peer_id | id of the peer that sent catch up request |
msg | network message containing catch up request |
Implements kagome::consensus::grandpa::CatchUpObserver.
Definition at line 485 of file grandpa_impl.cpp.
|
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
peer_id | id of remote peer that sent catch up response |
msg | message containing catch up response |
Implements kagome::consensus::grandpa::CatchUpObserver.
Definition at line 602 of file grandpa_impl.cpp.
|
overridevirtual |
Processing of commit message
We check commit message for politeness and send it to GrandpaImpl::applyJustification()
peer_id | id of remote peer |
msg | message containing commit message with justification |
Implements kagome::consensus::grandpa::RoundObserver.
Definition at line 1003 of file grandpa_impl.cpp.
|
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)
peer_id | id of the peer that sent the message |
msg | received grandpa neighbour message |
Implements kagome::consensus::grandpa::NeighborObserver.
Definition at line 363 of file grandpa_impl.cpp.
|
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
peer_id | id of remote peer |
msg | vote 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.
Definition at line 93 of file grandpa_impl.cpp.
|
private |
Selects round by provided number and voter set id
round_number | number of round to be selected |
voter_set_id | id of voter set for corresponding round |
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
Definition at line 171 of file grandpa_impl.cpp.
|
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
round | previous round from which new one is created and executed |
Implements kagome::consensus::grandpa::Grandpa.
Definition at line 323 of file grandpa_impl.cpp.
|
overridevirtual |
Selects round next to provided one and updates it by checking if prevote_ghost, estimate and finalized block were updated.
round_number | the round after which we select the round for update |
Implements kagome::consensus::grandpa::Grandpa.
Definition at line 353 of file grandpa_impl.cpp.
|
private |
Definition at line 270 of file grandpa_impl.hpp.
|
private |
Definition at line 273 of file grandpa_impl.hpp.
|
private |
Definition at line 280 of file grandpa_impl.hpp.
|
private |
Definition at line 268 of file grandpa_impl.hpp.
|
private |
Definition at line 265 of file grandpa_impl.hpp.
|
private |
Definition at line 276 of file grandpa_impl.hpp.
|
private |
Definition at line 264 of file grandpa_impl.hpp.
|
private |
Definition at line 281 of file grandpa_impl.hpp.
|
private |
Definition at line 266 of file grandpa_impl.hpp.
|
static |
Timeout of waiting catchup response for request.
Definition at line 69 of file grandpa_impl.hpp.
|
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.
|
private |
Definition at line 267 of file grandpa_impl.hpp.
|
static |
Maximum number of rounds we are keep to communication.
Definition at line 66 of file grandpa_impl.hpp.
|
private |
Definition at line 287 of file grandpa_impl.hpp.
|
private |
Definition at line 285 of file grandpa_impl.hpp.
|
private |
Definition at line 284 of file grandpa_impl.hpp.
|
private |
Definition at line 272 of file grandpa_impl.hpp.
|
private |
Definition at line 279 of file grandpa_impl.hpp.
|
private |
Definition at line 274 of file grandpa_impl.hpp.
|
private |
Definition at line 262 of file grandpa_impl.hpp.
|
private |
Definition at line 269 of file grandpa_impl.hpp.
|
private |
Definition at line 271 of file grandpa_impl.hpp.