Skip to content

Commit

Permalink
Merge branch 'master' into simplify/grandpa-api
Browse files Browse the repository at this point in the history
  • Loading branch information
turuslan authored Sep 7, 2023
2 parents 8d4aee4 + a8ed484 commit 1447066
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 42 deletions.
12 changes: 6 additions & 6 deletions core/consensus/grandpa/impl/grandpa_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace kagome::consensus::grandpa {
auto vote_crypto_provider = std::make_shared<VoteCryptoProviderImpl>(
keypair, crypto_provider_, round_state.round_number, config.voters);

auto new_round = std::make_shared<VotingRoundImpl>(
auto new_round = VotingRoundImpl::create(
shared_from_this(),
std::move(config),
hasher_,
Expand Down Expand Up @@ -310,7 +310,7 @@ namespace kagome::consensus::grandpa {
auto vote_crypto_provider = std::make_shared<VoteCryptoProviderImpl>(
keypair, crypto_provider_, new_round_number, config.voters);

auto new_round = std::make_shared<VotingRoundImpl>(
auto new_round = VotingRoundImpl::create(
shared_from_this(),
std::move(config),
hasher_,
Expand Down Expand Up @@ -1354,7 +1354,7 @@ namespace kagome::consensus::grandpa {
auto voters = VoterSet::make(authorities).value();
MovableRoundState state;
state.round_number = justification.round_number;
VotingRoundImpl round{
auto round = VotingRoundImpl::create(
shared_from_this(),
GrandpaConfig{voters, justification.round_number, {}, {}},
hasher_,
Expand All @@ -1366,9 +1366,9 @@ namespace kagome::consensus::grandpa {
std::make_shared<VoteGraphImpl>(
primitives::BlockInfo{}, voters, environment_),
scheduler_,
state,
};
promise_res->set_value(round.validatePrecommitJustification(justification));
state);
promise_res->set_value(
round->validatePrecommitJustification(justification));
}

void GrandpaImpl::applyJustification(
Expand Down
54 changes: 35 additions & 19 deletions core/consensus/grandpa/impl/voting_round_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,13 @@ namespace kagome::consensus::grandpa {

SL_DEBUG(logger_, "Round #{}: Start round", round_number_);

pending_timer_handle_ =
scheduler_->scheduleWithHandle([&] { pending(); }, pending_interval_);
pending_timer_handle_ = scheduler_->scheduleWithHandle(
[wself{weak_from_this()}] {
if (auto self = wself.lock()) {
self->pending();
}
},
pending_interval_);

sendNeighborMessage();

Expand Down Expand Up @@ -281,12 +286,14 @@ namespace kagome::consensus::grandpa {
}

stage_timer_handle_ = scheduler_->scheduleWithHandle(
[&] {
if (stage_ == Stage::PREVOTE_RUNS) {
SL_DEBUG(logger_,
"Round #{}: Time of prevote stage is out",
round_number_);
endPrevoteStage();
[wself{weak_from_this()}] {
if (auto self = wself.lock()) {
if (self->stage_ == Stage::PREVOTE_RUNS) {
SL_DEBUG(self->logger_,
"Round #{}: Time of prevote stage is out",
self->round_number_);
self->endPrevoteStage();
}
}
},
toMilliseconds(duration_ * 2 - (scheduler_->now() - start_time_)));
Expand Down Expand Up @@ -346,12 +353,14 @@ namespace kagome::consensus::grandpa {
}

stage_timer_handle_ = scheduler_->scheduleWithHandle(
[&] {
if (stage_ == Stage::PRECOMMIT_RUNS) {
SL_DEBUG(logger_,
"Round #{}: Time of precommit stage is out",
round_number_);
endPrecommitStage();
[wself{weak_from_this()}] {
if (auto self = wself.lock()) {
if (self->stage_ == Stage::PRECOMMIT_RUNS) {
SL_DEBUG(self->logger_,
"Round #{}: Time of precommit stage is out",
self->round_number_);
self->endPrecommitStage();
}
}
},
toMilliseconds(duration_ * 4 - (scheduler_->now() - start_time_)));
Expand Down Expand Up @@ -1086,9 +1095,11 @@ namespace kagome::consensus::grandpa {
need_to_update_estimate = true;
}
if (prevote_ghost_) {
scheduler_->schedule([&] {
if (stage_ == Stage::PRECOMMIT_WAITS_FOR_PREVOTES) {
endPrecommitStage();
scheduler_->schedule([wself{weak_from_this()}] {
if (auto self = wself.lock()) {
if (self->stage_ == Stage::PRECOMMIT_WAITS_FOR_PREVOTES) {
self->endPrecommitStage();
}
}
});
}
Expand Down Expand Up @@ -1609,7 +1620,12 @@ namespace kagome::consensus::grandpa {
SL_DEBUG(logger_, "Resend votes of recent rounds");
resend(shared_from_this());

pending_timer_handle_ =
scheduler_->scheduleWithHandle([&] { pending(); }, pending_interval_);
pending_timer_handle_ = scheduler_->scheduleWithHandle(
[wself{weak_from_this()}] {
if (auto self = wself.lock()) {
self->pending();
}
},
pending_interval_);
}
} // namespace kagome::consensus::grandpa
19 changes: 13 additions & 6 deletions core/consensus/grandpa/impl/voting_round_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ namespace kagome::consensus::grandpa {

namespace kagome::consensus::grandpa {

class VotingRoundImpl : public VotingRound {
class VotingRoundImpl : public VotingRound,
public std::enable_shared_from_this<VotingRoundImpl> {
protected:
// This ctor is needed only for tests purposes
VotingRoundImpl() : round_number_{}, duration_{} {}

private:
VotingRoundImpl(const std::shared_ptr<Grandpa> &grandpa,
const GrandpaConfig &config,
Expand All @@ -37,11 +42,6 @@ namespace kagome::consensus::grandpa {
std::shared_ptr<VoteGraph> vote_graph,
std::shared_ptr<libp2p::basic::Scheduler> scheduler);

protected:
// This ctor is needed only for tests purposes
VotingRoundImpl() : round_number_{}, duration_{} {}

public:
VotingRoundImpl(
const std::shared_ptr<Grandpa> &grandpa,
const GrandpaConfig &config,
Expand All @@ -66,6 +66,13 @@ namespace kagome::consensus::grandpa {
const std::shared_ptr<libp2p::basic::Scheduler> &scheduler,
const std::shared_ptr<VotingRound> &previous_round);

public:
template <typename... Args>
static std::shared_ptr<VotingRoundImpl> create(Args &&...args) {
return std::shared_ptr<VotingRoundImpl>(
new VotingRoundImpl(std::forward<Args>(args)...));
}

enum class Stage {
// Initial stage, round is just created
INIT,
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/grandpa/voting_round.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace kagome::consensus::grandpa {
/**
* Handles execution of one grandpa round. For details @see VotingRoundImpl
*/
class VotingRound : public std::enable_shared_from_this<VotingRound> {
class VotingRound {
public:
virtual ~VotingRound() = default;

Expand Down
20 changes: 10 additions & 10 deletions test/core/consensus/grandpa/voting_round/voting_round_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,16 @@ class VotingRoundTest : public testing::Test,
.WillRepeatedly(ReturnRef(finalized_in_prev_round_));
EXPECT_CALL(*previous_round_, doCommit()).Times(AnyNumber());

round_ = std::make_shared<VotingRoundImpl>(grandpa_,
config,
hasher_,
env_,
vote_crypto_provider_,
prevotes_,
precommits_,
vote_graph_,
scheduler_,
previous_round_);
round_ = VotingRoundImpl::create(grandpa_,
config,
hasher_,
env_,
vote_crypto_provider_,
prevotes_,
precommits_,
vote_graph_,
scheduler_,
previous_round_);
}

SignedMessage preparePrimaryPropose(const Id &id,
Expand Down

0 comments on commit 1447066

Please sign in to comment.