Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Feb 16, 2024
1 parent ca38160 commit 09ba21e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion node/actors/bft/src/replica/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/testonly/ut_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl UTHarness {
leader,
replica,
pipe: recv,
keys: setup.keys,
keys: setup.keys.clone(),
};
let _: Signed<ReplicaPrepare> = this.try_recv().unwrap();
(this, runner)
Expand Down
5 changes: 3 additions & 2 deletions node/actors/bft/src/tests.rs
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
29 changes: 16 additions & 13 deletions node/actors/executor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
15 changes: 6 additions & 9 deletions node/actors/network/src/gossip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions node/libs/concurrency/src/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion node/libs/roles/src/validator/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions node/libs/storage/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![];
Expand All @@ -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));
Expand Down
5 changes: 2 additions & 3 deletions node/tools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 09ba21e

Please sign in to comment.