8 #include <boost/optional/optional_io.hpp> 16 #include "scale/scale.hpp" 23 using primitives::Justification;
26 std::shared_ptr<blockchain::BlockTree> block_tree,
27 std::shared_ptr<blockchain::BlockHeaderRepository> header_repository,
28 std::shared_ptr<authority::AuthorityManager> authority_manager,
29 std::shared_ptr<network::GrandpaTransmitter> transmitter)
30 : block_tree_{std::move(block_tree)},
50 return std::vector<BlockHash>{base};
53 OUTCOME_TRY(chain,
block_tree_->getChainByBlocks(base, block));
54 std::reverse(chain.begin(), chain.end());
55 return std::move(chain);
60 return base == block ||
block_tree_->hasDirectChain(base, block);
65 std::optional<VoterSetId> voter_set_id)
const {
66 SL_DEBUG(
logger_,
"Finding best chain containing block {}", base);
67 OUTCOME_TRY(best_block,
block_tree_->getBestContaining(base, std::nullopt));
70 if (voter_set_id.has_value()) {
79 if (voter_set.has_value()
80 && voter_set.value()->id == voter_set_id.value()) {
85 best_block = parent_block;
89 SL_DEBUG(
logger_,
"Found best chain: {}", best_block);
90 return std::move(best_block);
98 .voter_set_id = set_id};
99 transmitter_->sendCatchUpRequest(peer_id, std::move(message));
100 return outcome::success();
107 std::vector<SignedPrevote> prevote_justification,
108 std::vector<SignedPrecommit> precommit_justification,
110 SL_DEBUG(
logger_,
"Send Catch-Up-Response upto round {}", round_number);
113 .round_number = round_number,
114 .prevote_justification = std::move(prevote_justification),
115 .precommit_justification = std::move(precommit_justification),
116 .best_final_candidate = best_final_candidate};
117 transmitter_->sendCatchUpResponse(peer_id, std::move(message));
118 return outcome::success();
125 "Round #{}: Send {} signed by {} for block {}",
129 [&](
const Prevote &) { return
"prevote"; },
130 [&](
const Precommit &) { return
"precommit"; },
136 {.round_number = round, .counter = set_id, .vote = vote}};
138 return outcome::success();
146 "Round #{}: Send {} signed by {} for block {} (as send state)",
150 [&](
const Prevote &) { return
"prevote"; },
151 [&](
const Precommit &) { return
"precommit"; },
154 vote.getBlockInfo());
157 .counter = voter_set_id,
159 transmitter_->sendVoteMessage(peer_id, std::move(message));
162 for (
const auto &vv : state.
votes) {
167 send(pair_vote.first);
168 send(pair_vote.second);
179 return outcome::success();
182 SL_DEBUG(
logger_,
"Round #{}: Send commit of block {}", round, vote);
186 .set_id = voter_ser_id,
187 .message = {.target_hash = vote.
hash, .target_number = vote.
number}};
188 for (
const auto &item : justification.
items) {
190 const auto &precommit = boost::relaxed_get<Precommit>(item.message);
191 message.message.precommits.push_back(precommit);
192 message.message.auth_data.emplace_back(item.signature, item.id);
196 return outcome::success();
201 SL_DEBUG(
logger_,
"Round #{}: Send neighbor message", round);
204 .voter_set_id = set_id,
205 .last_finalized = last_finalized};
208 return outcome::success();
215 BOOST_ASSERT(justification_observer);
219 scale::decode<grandpa::GrandpaJustification>(raw_justification.
data));
222 "Trying to apply justification on round #{} for block {}",
223 justification.round_number,
224 justification.block_info);
227 justification_observer->applyJustification(block_info, justification));
229 return outcome::success();
235 OUTCOME_TRY(enc, scale::encode(grandpa_justification));
240 return outcome::success();
245 OUTCOME_TRY(encoded_justification,
249 grandpa_justification,
250 scale::decode<GrandpaJustification>(encoded_justification.data));
252 return outcome::success(std::move(grandpa_justification));
outcome::result< std::vector< primitives::BlockHash > > getAncestry(const primitives::BlockHash &base, const primitives::BlockHash &block) const override
Get the ancestry of a {.
Tagged< bool, struct IsBlockFinalizedTag > IsBlockFinalized
outcome::result< void > finalize(VoterSetId id, const GrandpaJustification &justification) override
outcome::result< void > onCatchUpRespond(const libp2p::peer::PeerId &peer_id, VoterSetId set_id, RoundNumber round_number, std::vector< SignedPrevote > prevote_justification, std::vector< SignedPrecommit > precommit_justification, BlockInfo best_final_candidate) override
std::vector< SignedPrecommit > items
outcome::result< void > onVoted(RoundNumber round, VoterSetId set_id, const SignedMessage &vote) override
std::vector< VoteVariant > votes
bool hasAncestry(const primitives::BlockHash &base, const primitives::BlockHash &block) const override
Check if block is ancestor for second one.
EnvironmentImpl(std::shared_ptr< blockchain::BlockTree > block_tree, std::shared_ptr< blockchain::BlockHeaderRepository > header_repository, std::shared_ptr< authority::AuthorityManager > authority_manager, std::shared_ptr< network::GrandpaTransmitter > transmitter)
outcome::result< void > applyJustification(const BlockInfo &block_info, const primitives::Justification &justification) override
outcome::result< void > onNeighborMessageSent(RoundNumber round, VoterSetId set_id, BlockNumber last_finalized) override
libp2p::peer::PeerId PeerId
SLBuffer & put(std::string_view view)
Put a string into byte buffer.
primitives::BlockNumber BlockNumber
std::shared_ptr< blockchain::BlockHeaderRepository > header_repository_
std::shared_ptr< authority::AuthorityManager > authority_manager_
common::Hash256 BlockHash
std::weak_ptr< JustificationObserver > justification_observer_
std::pair< SignedMessage, SignedMessage > EquivocatorySignedMessage
BlockInfo getBlockInfo() const
outcome::result< void > onCatchUpRequested(const libp2p::peer::PeerId &peer_id, VoterSetId set_id, RoundNumber round_number) override
Stores the current state of the round.
outcome::result< BlockInfo > bestChainContaining(const primitives::BlockHash &base, std::optional< VoterSetId > voter_set_id) const override
outcome::result< GrandpaJustification > getJustification(const BlockHash &block_hash) override
Logger createLogger(const std::string &tag)
void sendState(const libp2p::peer::PeerId &peer_id, const MovableRoundState &state, VoterSetId voter_set_id) override
std::shared_ptr< network::GrandpaTransmitter > transmitter_
std::shared_ptr< blockchain::BlockTree > block_tree_
outcome::result< bool > hasBlock(const primitives::BlockHash &block) const override
Checks if {.
outcome::result< void > onCommitted(RoundNumber round, VoterSetId voter_ser_id, const BlockInfo &vote, const GrandpaJustification &justification) override
Tagged< bool, struct IsBlockFinalizedTag > IsBlockFinalized