Skip to content

Commit

Permalink
fix grandpa time (#1346)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
  • Loading branch information
turuslan authored Sep 22, 2022
1 parent c48739c commit 60e9e2d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
20 changes: 20 additions & 0 deletions core/application/chain_spec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ namespace kagome::application {
*/
virtual const ChildrenDefaultRawData &getGenesisChildrenDefaultSection()
const = 0;

bool idStartsWith(std::string_view prefix) const {
return id().rfind(prefix, 0) == 0;
}

bool isKusama() const {
return idStartsWith("kusama") || idStartsWith("ksm");
}

bool isRococo() const {
return idStartsWith("rococo") || idStartsWith("rco");
}

bool isWococo() const {
return idStartsWith("wococo") || idStartsWith("wco");
}

bool isVersi() const {
return idStartsWith("versi") || idStartsWith("vrs");
}
};

} // namespace kagome::application
Expand Down
3 changes: 2 additions & 1 deletion core/consensus/babe/impl/babe_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ namespace kagome::consensus::babe {
if (current_state_ == Babe::State::HEADERS_LOADING) {
current_state_ = Babe::State::HEADERS_LOADED;
startStateSyncing(peer_id);
} else if (current_state_ == Babe::State::CATCHING_UP) {
} else if (current_state_ == Babe::State::CATCHING_UP
or current_state_ == Babe::State::WAIT_REMOTE_STATUS) {
onSynchronized();
}
return;
Expand Down
14 changes: 13 additions & 1 deletion core/consensus/grandpa/impl/grandpa_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,31 @@ namespace kagome::consensus::grandpa {

using authority::IsBlockFinalized;

namespace {
Clock::Duration getGossipDuration(const application::ChainSpec &chain) {
// https://github.com/paritytech/polkadot/pull/5448
auto slow = chain.isVersi() || chain.isWococo() || chain.isRococo()
|| chain.isKusama();
return std::chrono::duration_cast<Clock::Duration>(
std::chrono::milliseconds{slow ? 2000 : 1000});
}
} // namespace

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)
: environment_{std::move(environment)},
: round_time_factor_{getGossipDuration(chain_spec)},
environment_{std::move(environment)},
crypto_provider_{std::move(crypto_provider)},
grandpa_api_{std::move(grandpa_api)},
keypair_{keypair},
Expand Down
7 changes: 3 additions & 4 deletions core/consensus/grandpa/impl/grandpa_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/operators.hpp>

#include "application/app_state_manager.hpp"
#include "application/chain_spec.hpp"
#include "blockchain/block_tree.hpp"
#include "consensus/authority/authority_manager.hpp"
#include "consensus/grandpa/environment.hpp"
Expand Down Expand Up @@ -70,6 +71,7 @@ namespace kagome::consensus::grandpa {
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,
Expand Down Expand Up @@ -251,10 +253,7 @@ namespace kagome::consensus::grandpa {
*/
void loadMissingBlocks();

// Note: Duration value was gotten from substrate
// https://github.com/paritytech/substrate/blob/efbac7be80c6e8988a25339061078d3e300f132d/bin/node-template/node/src/service.rs#L166
// Perhaps, 333ms is not enough for normal communication during the round
const Clock::Duration round_time_factor_ = std::chrono::milliseconds(333);
const Clock::Duration round_time_factor_;

std::shared_ptr<VotingRound> current_round_;

Expand Down
4 changes: 2 additions & 2 deletions core/consensus/grandpa/impl/voting_round_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ namespace kagome::consensus::grandpa {
endPrevoteStage();
}
},
toMilliseconds(duration_ * 2 - (start_time_ - scheduler_->now())));
toMilliseconds(duration_ * 2 - (scheduler_->now() - start_time_)));

on_complete_handler_ = [this] {
if (stage_ == Stage::PREVOTE_RUNS) {
Expand Down Expand Up @@ -345,7 +345,7 @@ namespace kagome::consensus::grandpa {
endPrecommitStage();
}
},
toMilliseconds(duration_ * 4 - (start_time_ - scheduler_->now())));
toMilliseconds(duration_ * 4 - (scheduler_->now() - start_time_)));

on_complete_handler_ = [this] {
if (stage_ == Stage::PRECOMMIT_RUNS) {
Expand Down
1 change: 1 addition & 0 deletions core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,7 @@ namespace {
injector.template create<sptr<crypto::Ed25519Provider>>(),
injector.template create<sptr<runtime::GrandpaApi>>(),
session_keys->getGranKeyPair(),
injector.template create<const application::ChainSpec &>(),
injector.template create<sptr<clock::SteadyClock>>(),
injector.template create<sptr<libp2p::basic::Scheduler>>(),
injector.template create<sptr<authority::AuthorityManager>>(),
Expand Down

0 comments on commit 60e9e2d

Please sign in to comment.