Skip to content

Commit a800e96

Browse files
acatangiuark0f
authored andcommitted
client/beefy: fix voter initialization (paritytech#12274)
Fix corner case where voter gets a single burst of finality notifications just when it starts. The notification stream was consumed by "wait_for_pallet" logic, then main loop would subscribe to finality notifications, but by that time some notifications might've been lost. Fix this by subscribing the main loop to notifications before waiting for pallet to become available. Share the same stream with the main loop so that notifications for blocks before pallet available are ignored, while _all_ notifications after pallet available are processed. Add regression test for this. Signed-off-by: acatangiu <[email protected]>
1 parent aad46ec commit a800e96

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

client/beefy/src/tests.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,37 @@ fn beefy_importing_blocks() {
811811
}));
812812
}
813813
}
814+
815+
#[test]
816+
fn voter_initialization() {
817+
sp_tracing::try_init_simple();
818+
// Regression test for voter initialization where finality notifications were dropped
819+
// after waiting for BEEFY pallet availability.
820+
821+
let mut runtime = Runtime::new().unwrap();
822+
let peers = &[BeefyKeyring::Alice, BeefyKeyring::Bob];
823+
let validator_set = ValidatorSet::new(make_beefy_ids(peers), 0).unwrap();
824+
let session_len = 5;
825+
// Should vote on all mandatory blocks no matter the `min_block_delta`.
826+
let min_block_delta = 10;
827+
828+
let mut net = BeefyTestNet::new(2, 0);
829+
let api = Arc::new(two_validators::TestApi {});
830+
let beefy_peers = peers.iter().enumerate().map(|(id, key)| (id, key, api.clone())).collect();
831+
runtime.spawn(initialize_beefy(&mut net, beefy_peers, min_block_delta));
832+
833+
// push 30 blocks
834+
net.generate_blocks_and_sync(26, session_len, &validator_set, false);
835+
let net = Arc::new(Mutex::new(net));
836+
837+
// Finalize multiple blocks at once to get a burst of finality notifications right from start.
838+
// Need to finalize at least one block in each session, choose randomly.
839+
// Expect voters to pick up all of them and BEEFY-finalize the mandatory blocks of each session.
840+
finalize_block_and_wait_for_beefy(
841+
&net,
842+
peers,
843+
&mut runtime,
844+
&[1, 6, 10, 17, 24, 26],
845+
&[1, 5, 10, 15, 20, 25],
846+
);
847+
}

client/beefy/src/worker.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use std::{
2424
};
2525

2626
use codec::{Codec, Decode, Encode};
27-
use futures::StreamExt;
27+
use futures::{stream::Fuse, StreamExt};
2828
use log::{debug, error, info, log_enabled, trace, warn};
2929

30-
use sc_client_api::{Backend, FinalityNotification, HeaderBackend};
30+
use sc_client_api::{Backend, FinalityNotification, FinalityNotifications, HeaderBackend};
3131
use sc_network_gossip::GossipEngine;
3232

3333
use sp_api::{BlockId, ProvideRuntimeApi};
@@ -723,12 +723,11 @@ where
723723

724724
/// Wait for BEEFY runtime pallet to be available.
725725
/// Should be called only once during worker initialization.
726-
async fn wait_for_runtime_pallet(&mut self) {
726+
async fn wait_for_runtime_pallet(&mut self, finality: &mut Fuse<FinalityNotifications<B>>) {
727727
let mut gossip_engine = &mut self.gossip_engine;
728-
let mut finality_stream = self.client.finality_notification_stream().fuse();
729728
loop {
730729
futures::select! {
731-
notif = finality_stream.next() => {
730+
notif = finality.next() => {
732731
let notif = match notif {
733732
Some(notif) => notif,
734733
None => break
@@ -762,11 +761,13 @@ where
762761
pub(crate) async fn run(mut self) {
763762
info!(target: "beefy", "🥩 run BEEFY worker, best grandpa: #{:?}.", self.best_grandpa_block_header.number());
764763
let mut block_import_justif = self.links.from_block_import_justif_stream.subscribe().fuse();
764+
// Subscribe to finality notifications before waiting for runtime pallet and reuse stream,
765+
// so we process notifications for all finalized blocks after pallet is available.
766+
let mut finality_notifications = self.client.finality_notification_stream().fuse();
765767

766-
self.wait_for_runtime_pallet().await;
768+
self.wait_for_runtime_pallet(&mut finality_notifications).await;
767769
trace!(target: "beefy", "🥩 BEEFY pallet available, starting voter.");
768770

769-
let mut finality_notifications = self.client.finality_notification_stream().fuse();
770771
let mut votes = Box::pin(
771772
self.gossip_engine
772773
.messages_for(topic::<B>())

0 commit comments

Comments
 (0)