Skip to content

Commit

Permalink
Fixes for fast sync in kusama
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrm committed Oct 3, 2022
1 parent 6e840f4 commit 7894fc8
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 164 deletions.
662 changes: 516 additions & 146 deletions core/consensus/authority/impl/authority_manager_impl.cpp

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions core/consensus/authority/impl/authority_manager_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ namespace kagome::storage::trie {
}

namespace kagome::authority {

struct ScheduledChangeEntry;
template<typename> struct ForkTree;
using ScheduleTree = ForkTree<ScheduledChangeEntry>;

class AuthorityManagerImpl : public AuthorityManager,
public AuthorityUpdateObserver {
public:
Expand All @@ -63,7 +68,7 @@ namespace kagome::authority {
std::shared_ptr<storage::BufferStorage> persistent_storage,
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo);

~AuthorityManagerImpl() override = default;
~AuthorityManagerImpl() override;

outcome::result<void> recalculateStoredState(
primitives::BlockNumber last_finalized_number) override;
Expand Down Expand Up @@ -132,8 +137,21 @@ namespace kagome::authority {
void reorganize(std::shared_ptr<ScheduleNode> node,
std::shared_ptr<ScheduleNode> new_node);


ScheduleTree *findClosestAncestor(
ScheduleTree &current,
primitives::BlockInfo const &block) const;

ScheduleTree const *findClosestAncestor(
ScheduleTree const &current,
primitives::BlockInfo const &block) const;

std::unique_ptr<ScheduleTree> scheduled_changes_;
primitives::AuthoritySet current_set_;
primitives::BlockInfo current_block_;

Config config_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<const blockchain::BlockTree> block_tree_;
std::shared_ptr<storage::trie::TrieStorage> trie_storage_;
std::shared_ptr<runtime::GrandpaApi> grandpa_api_;
std::shared_ptr<crypto::Hasher> hasher_;
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/babe/impl/consistency_keeper_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace kagome::consensus::babe {
auto &block = block_res.value();

SL_WARN(logger_,
"Found partial applied block {}. Trying to rollback him",
"Found partial applied block {}. Trying to rollback it",
block);

rollback(block);
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/babe/types/babe_block_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace kagome::consensus {
return slot_assignment_type == SlotType::Primary;
}

bool needAuthorCheck() const {
bool isProducedInSecondarySlot() const {
return slot_assignment_type == SlotType::SecondaryPlain
or slot_assignment_type == SlotType::SecondaryVRF;
}
Expand Down
9 changes: 6 additions & 3 deletions core/consensus/grandpa/impl/grandpa_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,13 +853,16 @@ namespace kagome::consensus::grandpa {
return VotingRoundError::JUSTIFICATION_FOR_AUTHORITY_SET_IN_PAST;
}
if (authority_set->id == current_round_->voterSetId()
&& justification.round_number
< current_round_->roundNumber()) {
&& justification.round_number < current_round_->roundNumber()) {
return VotingRoundError::JUSTIFICATION_FOR_ROUND_IN_PAST;
}

if (authority_set->id > current_round_->voterSetId() + 1) {
return VotingRoundError::WRONG_ORDER_OF_VOTER_SET_ID;
SL_WARN(logger_,
"Authority set on block {} with justification has id {}, "
"while the current round set id is {} (difference must be 1)",
block_info, authority_set->id, current_round_->voterSetId());
//return VotingRoundError::WRONG_ORDER_OF_VOTER_SET_ID;
}

auto voters = std::make_shared<VoterSet>(authority_set->id);
Expand Down
8 changes: 7 additions & 1 deletion core/consensus/grandpa/impl/voting_round_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,13 @@ namespace kagome::consensus::grandpa {
signed_precommit.id, signed_precommit.getBlockHash());
success) {
// New vote
auto weight_opt = voter_set_->voterWeight(signed_precommit.id);
if (!weight_opt) {
SL_DEBUG(logger_, "Voter {} is not in the current voter set", signed_precommit.id.toHex());
continue;
}
if (env_->hasAncestry(vote.hash, signed_precommit.getBlockHash())) {
total_weight += voter_set_->voterWeight(signed_precommit.id).value();
total_weight += weight_opt.value();
} else {
SL_DEBUG(logger_,
"Vote does not have ancestry with target block: "
Expand Down Expand Up @@ -1093,6 +1098,7 @@ namespace kagome::consensus::grandpa {
// Check if voter is contained in current voter set
auto index_and_weight_opt = voter_set_->indexAndWeight(vote.id);
if (!index_and_weight_opt) {
SL_DEBUG(logger_, "Voter {} is not in the current voter set", vote.id.toHex());
return VotingRoundError::UNKNOWN_VOTER;
}
const auto &[index, weight] = index_and_weight_opt.value();
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/grandpa/voting_round_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ OUTCOME_CPP_DEFINE_CATEGORY(kagome::consensus::grandpa, VotingRoundError, e) {
return "New round has an abnormal voter set id, expected "
"equal or greater by one than the previous voter set id";
case E::UNKNOWN_VOTER:
return "Provided vote is the vote of unknown voter";
return "Provided vote's voter is unknown";
case E::ZERO_WEIGHT_VOTER:
return "Provided vote of disabled (zero weight) voter";
case E::DUPLICATED_VOTE:
Expand Down
18 changes: 10 additions & 8 deletions core/consensus/validation/babe_block_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ namespace kagome::consensus {

// @see
// https://github.com/paritytech/substrate/blob/polkadot-v0.9.8/client/consensus/babe/src/verification.rs#L111
if (babe_header.needAuthorCheck()) {
if ((not babe_header.needVRFCheck()
and configuration_->allowed_slots
!= primitives::AllowedSlots::PrimaryAndSecondaryPlainSlots)
or (babe_header.needVRFCheck()
and configuration_->allowed_slots
!= primitives::AllowedSlots::
PrimaryAndSecondaryVRFSlots)) {
if (babe_header.isProducedInSecondarySlot()) {
bool plainAndAllowed =
babe_header.slotType() == SlotType::SecondaryPlain
&& configuration_->allowed_slots
== primitives::AllowedSlots::PrimaryAndSecondaryPlainSlots;
bool vrfAndAllowed =
babe_header.slotType() == SlotType::SecondaryVRF
&& configuration_->allowed_slots
== primitives::AllowedSlots::PrimaryAndSecondaryVRFSlots;
if (!plainAndAllowed and !vrfAndAllowed) {
SL_WARN(log_, "Secondary slots assignments disabled");
return ValidationError::SECONDARY_SLOT_ASSIGNMENTS_DISABLED;
}
Expand Down
2 changes: 1 addition & 1 deletion core/network/impl/stream_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ namespace kagome::network {
}

if (!stream_res) {
self->logger_->error(
self->logger_->debug(
"Could not send message to new {} stream with {}: {}",
protocol->protocol(),
peer_id,
Expand Down

0 comments on commit 7894fc8

Please sign in to comment.