From 6570a20a9d7b61fa8a57f450a2e34094a7594d4a Mon Sep 17 00:00:00 2001 From: Adrian Catangiu Date: Thu, 14 Dec 2023 11:47:04 +0200 Subject: [PATCH 1/2] fix beefy genesis during first session of the chain --- substrate/client/consensus/beefy/src/lib.rs | 35 ++++++++++----------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/substrate/client/consensus/beefy/src/lib.rs b/substrate/client/consensus/beefy/src/lib.rs index b3ff11add27e..e041721a4c72 100644 --- a/substrate/client/consensus/beefy/src/lib.rs +++ b/substrate/client/consensus/beefy/src/lib.rs @@ -544,24 +544,21 @@ where R::Api: BeefyApi, { debug!(target: LOG_TARGET, "🥩 Try to find validator set active at header: {:?}", at_header); - runtime - .runtime_api() - .validator_set(at_header.hash()) - .ok() - .flatten() - .or_else(|| { - // if state unavailable, fallback to walking up the chain looking for the header - // Digest emitted when validator set active 'at_header' was enacted. - let blockchain = backend.blockchain(); - let mut header = at_header.clone(); - loop { - debug!(target: LOG_TARGET, "🥩 look for auth set change digest in header number: {:?}", *header.number()); - match worker::find_authorities_change::(&header) { - Some(active) => return Some(active), - // Move up the chain. - None => header = blockchain.expect_header(*header.parent_hash()).ok()?, - } + // walk up the chain looking for the validator set active 'at_header' - try both state and + // header digests + let blockchain = backend.blockchain(); + let mut header = at_header.clone(); + loop { + debug!(target: LOG_TARGET, "🥩 look for auth set change in block number: {:?}", *header.number()); + if let Some(active) = runtime.runtime_api().validator_set(at_header.hash()).ok().flatten() { + return Ok(active) + } else { + match worker::find_authorities_change::(&header) { + Some(active) => return Ok(active), + // Move up the chain. Ultimately we'll get it from chain genesis state, or error out + // here. + None => header = blockchain.expect_header(*header.parent_hash())?, } - }) - .ok_or_else(|| ClientError::Backend("Could not find initial validator set".into())) + } + } } From 67aca8dd4a755b06fa7054b3677a77747da5338c Mon Sep 17 00:00:00 2001 From: Serban Iorga Date: Thu, 14 Dec 2023 15:06:10 +0100 Subject: [PATCH 2/2] Nits --- substrate/client/consensus/beefy/src/lib.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/substrate/client/consensus/beefy/src/lib.rs b/substrate/client/consensus/beefy/src/lib.rs index e041721a4c72..c3da2b886f49 100644 --- a/substrate/client/consensus/beefy/src/lib.rs +++ b/substrate/client/consensus/beefy/src/lib.rs @@ -543,16 +543,17 @@ where R: ProvideRuntimeApi, R::Api: BeefyApi, { - debug!(target: LOG_TARGET, "🥩 Try to find validator set active at header: {:?}", at_header); - // walk up the chain looking for the validator set active 'at_header' - try both state and - // header digests let blockchain = backend.blockchain(); + + // Walk up the chain looking for the validator set active at 'at_header'. Process both state and + // header digests. + debug!(target: LOG_TARGET, "🥩 Trying to find validator set active at header: {:?}", at_header); let mut header = at_header.clone(); loop { - debug!(target: LOG_TARGET, "🥩 look for auth set change in block number: {:?}", *header.number()); - if let Some(active) = runtime.runtime_api().validator_set(at_header.hash()).ok().flatten() { + if let Ok(Some(active)) = runtime.runtime_api().validator_set(at_header.hash()) { return Ok(active) } else { + debug!(target: LOG_TARGET, "🥩 Looking for auth set change at block number: {:?}", *header.number()); match worker::find_authorities_change::(&header) { Some(active) => return Ok(active), // Move up the chain. Ultimately we'll get it from chain genesis state, or error out