Skip to content

Commit 326a77e

Browse files
bkchrltfschoen
authored andcommitted
Fix flaky BABE test (paritytech#13199)
The `authoring_blocks` test of BABE was calculating the slot based on the timestamp it sometimes failed in CI. The problem is that we combine all the notifications and authoring futures in one big future. This one big future may first polls one authoring future to build a block. Then it polls all notification futures again to import the block. Then some other authoring future is polled and builds on the imported block using the same slot and making the import fail. The solution is that we just artificially increase the slot to make the test work.
1 parent 4fa574d commit 326a77e

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

client/consensus/babe/src/tests.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,27 @@ async fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + '
432432
.for_each(|_| future::ready(())),
433433
);
434434

435+
let client_clone = client.clone();
435436
babe_futures.push(
436437
start_babe(BabeParams {
437438
block_import: data.block_import.lock().take().expect("import set up during init"),
438439
select_chain,
439440
client,
440441
env: environ,
441442
sync_oracle: DummyOracle,
442-
create_inherent_data_providers: Box::new(|_, _| async {
443-
let slot = InherentDataProvider::from_timestamp_and_slot_duration(
444-
Timestamp::current(),
445-
SlotDuration::from_millis(SLOT_DURATION_MS),
443+
create_inherent_data_providers: Box::new(move |parent, _| {
444+
// Get the slot of the parent header and just increase this slot.
445+
//
446+
// Below we will running everything in one big future. If we would use
447+
// time based slot, it can happen that on babe instance imports a block from
448+
// another babe instance and then tries to build a block in the same slot making
449+
// this test fail.
450+
let parent_header = client_clone.header(parent).ok().flatten().unwrap();
451+
let slot = Slot::from(
452+
find_pre_digest::<TestBlock>(&parent_header).unwrap().slot() + 1,
446453
);
447-
Ok((slot,))
454+
455+
async move { Ok((InherentDataProvider::new(slot),)) }
448456
}),
449457
force_authoring: false,
450458
backoff_authoring_blocks: Some(BackoffAuthoringOnFinalizedHeadLagging::default()),

0 commit comments

Comments
 (0)