From 5116f2f4a3b1f48a30725a7fe40d2848506dacd1 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Wed, 18 Aug 2021 13:11:14 -0400 Subject: [PATCH 1/3] hackity hack hack --- runtime/common/src/apis.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs index 7faee2b7ba..f6e204f2a0 100644 --- a/runtime/common/src/apis.rs +++ b/runtime/common/src/apis.rs @@ -351,20 +351,29 @@ macro_rules! impl_runtime_apis_plus_common { slot: u32, parent_header: &::Header ) -> bool { + let block_number = parent_header.number + 1; + // The Moonbeam runtimes use an entropy source that needs to do some accounting // work during block initialization. Therefore we initialize it here to match // the state it will be in when the next block is being executed. use frame_support::traits::OnInitialize; System::initialize( - &(parent_header.number + 1), + &block_number, &parent_header.hash(), &parent_header.digest, frame_system::InitKind::Inspection ); - RandomnessCollectiveFlip::on_initialize(System::block_number()); - - // And now the actual prediction call - AuthorInherent::can_author(&author, &slot) + RandomnessCollectiveFlip::on_initialize(block_number); + + // Because the staking solution calculates the next staking set at the beginning + // of the first block in the next round, the only way to accurately predict the + // authors would be to run the staking election while predicting. However this + // election is heavy and will take too long during prediction. So instead we + // work around it by always authoring the first slot in a new round. A longer + // term solution will be to calculate the staking election result in the last + // block of the previous round. + parachain_staking::Pallet::::round().should_update(block_number) + || AuthorInherent::can_author(&author, &slot) } } From 3d851995d882294427c502794053192b60bad424 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Wed, 18 Aug 2021 13:13:52 -0400 Subject: [PATCH 2/3] clearer comments --- runtime/common/src/apis.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs index f6e204f2a0..e535939234 100644 --- a/runtime/common/src/apis.rs +++ b/runtime/common/src/apis.rs @@ -366,12 +366,12 @@ macro_rules! impl_runtime_apis_plus_common { RandomnessCollectiveFlip::on_initialize(block_number); // Because the staking solution calculates the next staking set at the beginning - // of the first block in the next round, the only way to accurately predict the + // of the first block in the new round, the only way to accurately predict the // authors would be to run the staking election while predicting. However this // election is heavy and will take too long during prediction. So instead we - // work around it by always authoring the first slot in a new round. A longer + // work around it by always authoring the first slot in a new round. A longer- // term solution will be to calculate the staking election result in the last - // block of the previous round. + // block of the ending round. parachain_staking::Pallet::::round().should_update(block_number) || AuthorInherent::can_author(&author, &slot) } From c17d8e8f08238c1a7b9bdae247875763a9d3faec Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Wed, 18 Aug 2021 15:29:30 -0400 Subject: [PATCH 3/3] helpful message explaining the CannotBeAuthor error. --- Cargo.lock | 3 +++ runtime/common/Cargo.toml | 3 +++ runtime/common/src/apis.rs | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ead342f771..09c104c92a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9121,6 +9121,9 @@ dependencies = [ [[package]] name = "runtime-common" version = "0.8.0-dev" +dependencies = [ + "log", +] [[package]] name = "rustc-demangle" diff --git a/runtime/common/Cargo.toml b/runtime/common/Cargo.toml index 7333a3bf37..1465c370c2 100644 --- a/runtime/common/Cargo.toml +++ b/runtime/common/Cargo.toml @@ -7,5 +7,8 @@ version = '0.8.0-dev' authors = ["PureStake"] edition = '2018' +[dependencies] +log = "0.4" + [features] std = [] \ No newline at end of file diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs index e535939234..4a608b7649 100644 --- a/runtime/common/src/apis.rs +++ b/runtime/common/src/apis.rs @@ -372,8 +372,18 @@ macro_rules! impl_runtime_apis_plus_common { // work around it by always authoring the first slot in a new round. A longer- // term solution will be to calculate the staking election result in the last // block of the ending round. - parachain_staking::Pallet::::round().should_update(block_number) - || AuthorInherent::can_author(&author, &slot) + if parachain_staking::Pallet::::round().should_update(block_number) { + log::info!(target: "nimbus-staking-workaround", "A new round is starting.\ + Moonbeam will author during this slot without predicting eligibility first.\ + You may see a `CannotBeAuthor` error soon. This is expected and harmless.\ + It will be resolved soon."); + + true + } + else { + AuthorInherent::can_author(&author, &slot) + + } } }