Skip to content

Commit

Permalink
Avoid missing BEEFY voting sessions during node restart
Browse files Browse the repository at this point in the history
  • Loading branch information
serban300 committed Jan 26, 2024
1 parent ba24c52 commit a05699a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion substrate/client/consensus/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub async fn start_beefy_gadget<B, BE, C, N, P, R, S>(
},
};

let worker_base = worker::BeefyWorkerBase {
let mut worker_base = worker::BeefyWorkerBase {
backend: backend.clone(),
runtime: runtime.clone(),
key_store: key_store.clone().into(),
Expand Down
6 changes: 3 additions & 3 deletions substrate/client/consensus/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ async fn voter_init_setup(
);
let (beefy_genesis, best_grandpa) =
wait_for_runtime_pallet(api, &mut gossip_engine, finality).await.unwrap();
let worker_base = BeefyWorkerBase {
let mut worker_base = BeefyWorkerBase {
backend,
runtime: Arc::new(api.clone()),
key_store: None.into(),
Expand Down Expand Up @@ -1081,7 +1081,7 @@ async fn should_initialize_voter_at_custom_genesis() {
);
let (beefy_genesis, best_grandpa) =
wait_for_runtime_pallet(&api, &mut gossip_engine, &mut finality).await.unwrap();
let worker_base = BeefyWorkerBase {
let mut worker_base = BeefyWorkerBase {
backend: backend.clone(),
runtime: Arc::new(api),
key_store: None.into(),
Expand Down Expand Up @@ -1122,7 +1122,7 @@ async fn should_initialize_voter_at_custom_genesis() {
// the network state persists and uses the old `GossipEngine` initialized for `peer(0)`
let (beefy_genesis, best_grandpa) =
wait_for_runtime_pallet(&api, &mut gossip_engine, &mut finality).await.unwrap();
let worker_base = BeefyWorkerBase {
let mut worker_base = BeefyWorkerBase {
backend: backend.clone(),
runtime: Arc::new(api),
key_store: None.into(),
Expand Down
16 changes: 15 additions & 1 deletion substrate/client/consensus/beefy/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ where
}

pub async fn load_or_init_state(
&self,
&mut self,
beefy_genesis: NumberFor<B>,
best_grandpa: <B as Block>::Header,
min_block_delta: u32,
Expand All @@ -470,12 +470,26 @@ where
info!(target: LOG_TARGET, "🥩 Loading BEEFY voter state from db: {:?}.", state);

// Make sure that all the headers that we need have been synced.
let mut new_sessions = vec![];
let mut header = best_grandpa.clone();
while *header.number() > state.best_beefy() {
if let Some(active) = find_authorities_change::<B>(&header) {
new_sessions.push((active, *header.number()));
}
header =
wait_for_parent_header(self.backend.blockchain(), header, HEADER_SYNC_DELAY)
.await?;
}

// Make sure we didn't miss any sessions during node restart.
for (validator_set, new_session_start) in new_sessions.drain(..).rev() {
info!(
target: LOG_TARGET,
"🥩 Handling missed BEEFY session after node restart: {:?}.",
new_session_start
);
self.init_session_at(&mut state, validator_set, new_session_start);
}
return Ok(state)
}

Expand Down

0 comments on commit a05699a

Please sign in to comment.