Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix grandpa time #1346

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 isVersi() const {
return idStartsWith("kusama") || idStartsWith("ksm");
}

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

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

bool isKusama() const {
return idStartsWith("versi") || idStartsWith("vrs");
}
turuslan marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace kagome::application
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