Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
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
Prev Previous commit
Next Next commit
client/beefy: persist voter state as soon as initialized
  • Loading branch information
acatangiu committed Nov 18, 2022
commit e46a6e244f99a5b26ef914f815ca5f1626c95249
102 changes: 48 additions & 54 deletions client/beefy/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,71 +112,65 @@ where

debug!(target: "beefy", "🥩 pallet available: header {:?} validator set {:?}", best_grandpa, active);

if active.id() == GENESIS_AUTHORITY_SET_ID {
let state = if active.id() == GENESIS_AUTHORITY_SET_ID {
let start = *best_grandpa.number();
info!(
target: "beefy",
"🥩 Loading BEEFY voter state from genesis on what appears to be first startup. \
Starting voting rounds at block {:?}.",
start
);
return Ok(PersistedState::initialize(
best_grandpa,
Zero::zero(),
start,
active,
min_block_delta,
))
}
PersistedState::initialize(best_grandpa, Zero::zero(), start, active, min_block_delta)
} else {
// Walk back the imported blocks and initialize voter either, at the last block with
// a BEEFY justification, or at current session's boundary; voter will resume from there.
let blockchain = backend.blockchain();
let mut header = best_grandpa.clone();
loop {
if let Some(true) = blockchain
.justifications(header.hash())
.ok()
.flatten()
.map(|justifs| justifs.get(BEEFY_ENGINE_ID).is_some())
{
info!(
target: "beefy",
"🥩 Initialize BEEFY voter at last BEEFY finalized block: {:?}.",
*header.number()
);
let mut state = PersistedState::initialize(
best_grandpa,
*header.number(),
*header.number(),
active,
min_block_delta,
);
// Mark the round as already finalized.
if let Some(round) = state.active_round_mut() {
round.conclude(*header.number());
}
break state
}

// Walk back the imported blocks and initialize voter either, at the last block with
// a BEEFY justification, or at current session's boundary; voter will resume from there.
let blockchain = backend.blockchain();
let mut header = best_grandpa.clone();
let state = loop {
if let Some(true) = blockchain
.justifications(header.hash())
.ok()
.flatten()
.map(|justifs| justifs.get(BEEFY_ENGINE_ID).is_some())
{
info!(
target: "beefy",
"🥩 Initialize BEEFY voter at last BEEFY finalized block: {:?}.",
*header.number()
);
let mut state = PersistedState::initialize(
best_grandpa,
*header.number(),
*header.number(),
active,
min_block_delta,
);
// Mark the round as already finalized.
if let Some(round) = state.active_round_mut() {
round.conclude(*header.number());
if let Some(active) = crate::worker::find_authorities_change::<B>(&header) {
info!(
target: "beefy",
"🥩 Initialize BEEFY voter at current session boundary: {:?}.",
*header.number()
);
let state = PersistedState::initialize(
best_grandpa,
Zero::zero(),
*header.number(),
active,
min_block_delta,
);
break state
}
break state
}

if let Some(active) = crate::worker::find_authorities_change::<B>(&header) {
info!(
target: "beefy",
"🥩 Initialize BEEFY voter at current session boundary: {:?}.",
*header.number()
);
let state = PersistedState::initialize(
best_grandpa,
Zero::zero(),
*header.number(),
active,
min_block_delta,
);
break state
// Move up the chain.
header = blockchain.expect_header(BlockId::Hash(*header.parent_hash()))?;
}

// Move up the chain.
header = blockchain.expect_header(BlockId::Hash(*header.parent_hash()))?;
};

write_voter_state(backend, &state)?;
Expand Down