From 09ba21eb491ac2e7ba473e2b7756c875dc01d15e Mon Sep 17 00:00:00 2001 From: Grzegorz Prusak Date: Fri, 16 Feb 2024 12:27:54 +0100 Subject: [PATCH] all tests pass --- node/actors/bft/src/replica/timer.rs | 2 +- node/actors/bft/src/testonly/ut_harness.rs | 2 +- node/actors/bft/src/tests.rs | 5 ++-- node/actors/executor/src/tests.rs | 29 ++++++++++++---------- node/actors/network/src/gossip/tests.rs | 15 +++++------ node/libs/concurrency/src/testonly.rs | 1 + node/libs/roles/src/validator/testonly.rs | 3 ++- node/libs/storage/src/tests.rs | 12 ++++----- node/tools/src/tests.rs | 5 ++-- 9 files changed, 38 insertions(+), 36 deletions(-) diff --git a/node/actors/bft/src/replica/timer.rs b/node/actors/bft/src/replica/timer.rs index 445b259b..0cbae88c 100644 --- a/node/actors/bft/src/replica/timer.rs +++ b/node/actors/bft/src/replica/timer.rs @@ -6,7 +6,7 @@ use zksync_consensus_roles::validator; impl StateMachine { /// The base duration of the timeout. - pub(crate) const BASE_DURATION: time::Duration = time::Duration::milliseconds(1000); + pub(crate) const BASE_DURATION: time::Duration = time::Duration::milliseconds(2000); /// Resets the timer. On every timeout we double the duration, starting from a given base duration. /// This is a simple exponential backoff. diff --git a/node/actors/bft/src/testonly/ut_harness.rs b/node/actors/bft/src/testonly/ut_harness.rs index cfcfd82e..e54fbafd 100644 --- a/node/actors/bft/src/testonly/ut_harness.rs +++ b/node/actors/bft/src/testonly/ut_harness.rs @@ -75,7 +75,7 @@ impl UTHarness { leader, replica, pipe: recv, - keys: setup.keys, + keys: setup.keys.clone(), }; let _: Signed = this.try_recv().unwrap(); (this, runner) diff --git a/node/actors/bft/src/tests.rs b/node/actors/bft/src/tests.rs index 303ac620..e6bb83b5 100644 --- a/node/actors/bft/src/tests.rs +++ b/node/actors/bft/src/tests.rs @@ -1,10 +1,11 @@ use crate::testonly::{ut_harness::UTHarness, Behavior, Network, Test}; -use zksync_concurrency::{ctx, scope}; +use zksync_concurrency::{ctx, scope, time}; use zksync_consensus_roles::validator; async fn run_test(behavior: Behavior, network: Network) { + let _guard = zksync_concurrency::testonly::set_timeout(time::Duration::seconds(20)); zksync_concurrency::testonly::abort_on_panic(); - let ctx = &ctx::test_root(&ctx::AffineClock::new(1.)); + let ctx = &ctx::test_root(&ctx::RealClock); const NODES: usize = 11; let mut nodes = vec![behavior; NODES]; diff --git a/node/actors/executor/src/tests.rs b/node/actors/executor/src/tests.rs index f9bcd422..b2924500 100644 --- a/node/actors/executor/src/tests.rs +++ b/node/actors/executor/src/tests.rs @@ -6,6 +6,7 @@ use zksync_concurrency::{ testonly::{abort_on_panic, set_timeout}, time, }; +use tracing::Instrument as _; use zksync_consensus_bft as bft; use zksync_consensus_roles::{validator::testonly::Setup, validator::BlockNumber}; use zksync_consensus_storage::{ @@ -91,10 +92,11 @@ async fn test_block_revert() { abort_on_panic(); let _guard = set_timeout(time::Duration::seconds(10)); - let ctx = &ctx::test_root(&ctx::AffineClock::new(20.0)); + let ctx = &ctx::test_root(&ctx::AffineClock::new(10.)); let rng = &mut ctx.rng(); - let mut setup = Setup::new(rng, 2); + let setup = Setup::new(rng, 2); + let first = setup.next(); let mut cfgs = new_configs(rng, &setup, 1); // Persistent stores for the validators. let mut ps : Vec<_> = cfgs.iter().map(|_|in_memory::BlockStore::new(setup.genesis.clone())).collect(); @@ -109,42 +111,43 @@ async fn test_block_revert() { stores.push(store); } for s in stores { - s.wait_until_persisted(ctx, BlockNumber(6)).await?; + s.wait_until_persisted(ctx, BlockNumber(first.0 + 6)).await?; } Ok(()) }).await.unwrap(); tracing::info!("Revert blocks"); - let first = BlockNumber(3); + let first = BlockNumber(first.0 + 3); let fork = validator::Fork { number: setup.genesis.forks.current().number.next(), first_block: first, first_parent: ps[0].block(ctx,first).await.unwrap().header().parent, }; - setup.genesis.forks.push(fork.clone()).unwrap(); + let mut genesis = setup.genesis.clone(); + genesis.forks.push(fork.clone()).unwrap(); // Update configs and persistent storage. for i in 0..cfgs.len() { - cfgs[i].genesis = setup.genesis.clone(); + cfgs[i].genesis = genesis.clone(); ps[i] = ps[i].fork(fork.clone()).unwrap(); } - let last_block = BlockNumber(8); + let last_block = BlockNumber(first.0+8); scope::run!(ctx, |ctx,s| async { tracing::info!("Make validators produce blocks on the new fork."); let mut stores = vec![]; for i in 0..cfgs.len() { let (store, runner) = BlockStore::new(ctx, Box::new(ps[i].clone())).await.unwrap(); - s.spawn_bg(runner.run(ctx)); - s.spawn_bg(make_executor(&cfgs[i],store.clone()).run(ctx)); + s.spawn_bg(runner.run(ctx).instrument(tracing::info_span!("node",i))); + s.spawn_bg(make_executor(&cfgs[i],store.clone()).run(ctx).instrument(tracing::info_span!("node",i))); stores.push(store); } tracing::info!("Spawn a new node with should fetch blocks from both new and old fork"); - let (store, runner) = new_store(ctx, &setup.genesis).await; - s.spawn_bg(runner.run(ctx)); - s.spawn_bg(make_executor(&new_fullnode(rng,&cfgs[0]),store.clone()).run(ctx)); + let (store, runner) = new_store(ctx, &genesis).await; + s.spawn_bg(runner.run(ctx).instrument(tracing::info_span!("fullnode"))); + s.spawn_bg(make_executor(&new_fullnode(rng,&cfgs[0]),store.clone()).run(ctx).instrument(tracing::info_span!("fullnode"))); store.wait_until_persisted(ctx, last_block).await?; - storage::testonly::verify(ctx, &*store, &setup.genesis).await.context("verify(storage)")?; + storage::testonly::verify(ctx, &*store, &genesis).await.context("verify(storage)")?; Ok(()) }).await.unwrap(); } diff --git a/node/actors/network/src/gossip/tests.rs b/node/actors/network/src/gossip/tests.rs index 77a0a998..51e70599 100644 --- a/node/actors/network/src/gossip/tests.rs +++ b/node/actors/network/src/gossip/tests.rs @@ -311,9 +311,8 @@ async fn syncing_blocks(node_count: usize, gossip_peers: usize) { let ctx = &ctx::test_root(&ctx::AffineClock::new(20.0)); let rng = &mut ctx.rng(); - let setup = validator::testonly::Setup::builder(rng, node_count) - .push_blocks(rng, EXCHANGED_STATE_COUNT) - .build(); + let mut setup = validator::testonly::Setup::new(rng, node_count); + setup.push_blocks(rng, EXCHANGED_STATE_COUNT); let cfgs = testonly::new_configs(rng, &setup, gossip_peers); scope::run!(ctx, |ctx, s| async { let mut nodes = vec![]; @@ -385,9 +384,8 @@ async fn uncoordinated_block_syncing( let ctx = &ctx::test_root(&ctx::AffineClock::new(20.0)); let rng = &mut ctx.rng(); - let setup = validator::testonly::Setup::builder(rng, node_count) - .push_blocks(rng, EXCHANGED_STATE_COUNT) - .build(); + let mut setup = validator::testonly::Setup::new(rng, node_count); + setup.push_blocks(rng, EXCHANGED_STATE_COUNT); scope::run!(ctx, |ctx, s| async { for (i, cfg) in testonly::new_configs(rng, &setup, gossip_peers) .into_iter() @@ -424,9 +422,8 @@ async fn getting_blocks_from_peers(node_count: usize, gossip_peers: usize) { let ctx = &ctx::test_root(&ctx::RealClock); let rng = &mut ctx.rng(); - let setup = validator::testonly::Setup::builder(rng, node_count) - .push_blocks(rng,1) - .build(); + let mut setup = validator::testonly::Setup::new(rng, node_count); + setup.push_blocks(rng,1); let cfgs = testonly::new_configs(rng, &setup, gossip_peers); // All inbound and outbound peers should answer the request. diff --git a/node/libs/concurrency/src/testonly.rs b/node/libs/concurrency/src/testonly.rs index c726eef9..67870101 100644 --- a/node/libs/concurrency/src/testonly.rs +++ b/node/libs/concurrency/src/testonly.rs @@ -38,6 +38,7 @@ pub fn abort_on_panic() { /// Guard which has to be dropped before timeout is reached. /// Otherwise the test will panic. #[allow(unused_tuple_struct_fields)] +#[must_use] pub struct TimeoutGuard(std::sync::mpsc::Sender<()>); /// Panics if (real time) timeout is reached before ctx is canceled. diff --git a/node/libs/roles/src/validator/testonly.rs b/node/libs/roles/src/validator/testonly.rs index 57a225aa..a069cd18 100644 --- a/node/libs/roles/src/validator/testonly.rs +++ b/node/libs/roles/src/validator/testonly.rs @@ -54,7 +54,8 @@ impl Setup { }).unwrap(); } - fn next(&self) -> BlockNumber { + /// Next block to finalize. + pub fn next(&self) -> BlockNumber { match self.0.blocks.last() { Some(b) => b.header().number.next(), None => self.0.genesis.forks.root().first_block, diff --git a/node/libs/storage/src/tests.rs b/node/libs/storage/src/tests.rs index 6155310b..b1a4f8dd 100644 --- a/node/libs/storage/src/tests.rs +++ b/node/libs/storage/src/tests.rs @@ -7,11 +7,10 @@ use zksync_concurrency::{ctx, scope, sync, testonly::abort_on_panic}; async fn test_inmemory_block_store() { let ctx = &ctx::test_root(&ctx::RealClock); let rng = &mut ctx.rng(); - let setup = Setup::builder(rng, 3) - .push_blocks(rng, 3) - .fork() - .push_blocks(rng, 3) - .build(); + let mut setup = Setup::new(rng, 3); + setup.push_blocks(rng, 3); + setup.fork(); + setup.push_blocks(rng, 3); let store = &testonly::in_memory::BlockStore::new(setup.genesis.clone()); let mut want = vec![]; @@ -34,7 +33,8 @@ async fn test_state_updates() { abort_on_panic(); let ctx = &ctx::test_root(&ctx::RealClock); let rng = &mut ctx.rng(); - let setup = Setup::builder(rng, 1).push_blocks(rng, 1).build(); + let mut setup = Setup::new(rng, 1); + setup.push_blocks(rng, 1); let (store, runner) = new_store(ctx, &setup.genesis).await; scope::run!(ctx, |ctx, s| async { s.spawn_bg(runner.run(ctx)); diff --git a/node/tools/src/tests.rs b/node/tools/src/tests.rs index 278762cc..d4fdc43d 100644 --- a/node/tools/src/tests.rs +++ b/node/tools/src/tests.rs @@ -46,9 +46,8 @@ async fn test_reopen_rocksdb() { let ctx = &ctx::test_root(&ctx::RealClock); let rng = &mut ctx.rng(); let dir = TempDir::new().unwrap(); - let setup = Setup::builder(rng, 3) - .push_blocks(rng, 5) - .build(); + let mut setup = Setup::new(rng, 3); + setup.push_blocks(rng, 5); let mut want = vec![]; for b in &setup.blocks { let store = store::RocksDB::open(setup.genesis.clone(),dir.path()).await.unwrap();