Skip to content

Commit

Permalink
setup more bug resistent
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Feb 16, 2024
1 parent ff968fe commit ca38160
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 55 deletions.
16 changes: 9 additions & 7 deletions node/actors/sync_blocks/src/peers/tests/fakes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use zksync_consensus_storage::testonly::new_store;
async fn processing_invalid_sync_states() {
let ctx = &ctx::test_root(&ctx::RealClock);
let rng = &mut ctx.rng();
let setup = Setup::builder(rng, 4)
.push_blocks(rng, 3)
.build();
let mut setup = Setup::new(rng, 4);
setup.push_blocks(rng,3);
let (storage, _runner) = new_store(ctx, &setup.genesis).await;

let (message_sender, _) = channel::unbounded();
Expand All @@ -23,9 +22,8 @@ async fn processing_invalid_sync_states() {
let invalid_sync_state = sync_state(&setup, Some(&invalid_block));
assert!(peer_states.update(peer, invalid_sync_state).is_err());

let other_network = Setup::builder(rng, 4)
.push_blocks(rng, 2)
.build();
let mut other_network = Setup::new(rng, 4);
other_network.push_blocks(rng, 2);
let invalid_sync_state = sync_state(&other_network, other_network.blocks.get(1));
assert!(peer_states.update(peer, invalid_sync_state).is_err());
}
Expand Down Expand Up @@ -93,7 +91,11 @@ impl Test for PeerWithFakeBlock {
// other block than requested
setup.blocks[1].clone(),
// block with wrong validator set
Setup::builder(rng, 4).push_blocks(rng, 1).build().blocks[0].clone(),
{
let mut s = Setup::new(rng, 4);
s.push_blocks(rng, 1);
s.blocks[0].clone()
},
// block with mismatching payload,
{
let mut block = setup.blocks[0].clone();
Expand Down
7 changes: 3 additions & 4 deletions node/actors/sync_blocks/src/peers/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ async fn test_peer_states<T: Test>(test: T) {
let clock = ctx::ManualClock::new();
let ctx = &ctx::test_root(&clock);
let rng = &mut ctx.rng();
let mut b = validator::testonly::Setup::builder(rng, 4);
let mut setup = validator::testonly::Setup::new(rng, 4);
for _ in 0..T::BLOCK_COUNT {
if rng.gen_range(0..3) == 0 { b = b.fork(); }
b.push_block(rng.gen());
if rng.gen_range(0..3) == 0 { setup.fork(); }
setup.push_block(rng.gen());
}
let setup = b.build();
let (store, store_run) = new_store(ctx, &setup.genesis).await;
test.initialize_storage(ctx, store.as_ref(), &setup).await;

Expand Down
13 changes: 6 additions & 7 deletions node/actors/sync_blocks/src/tests/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ impl Node {
gossip_peers: usize,
) -> (Vec<Node>, Vec<NodeRunner>) {
let rng = &mut ctx.rng();
let setup = validator::testonly::Setup::builder(rng, node_count)
.push_blocks(rng,3)
.fork()
.push_blocks(rng,7)
.fork()
.push_blocks(rng,10)
.build();
let mut setup = validator::testonly::Setup::new(rng, node_count);
setup.push_blocks(rng,1);
setup.fork();
setup.push_blocks(rng,9);
setup.fork();
setup.push_blocks(rng,10);
let setup = Arc::new(setup);
let mut nodes = vec![];
let mut runners = vec![];
Expand Down
66 changes: 29 additions & 37 deletions node/libs/roles/src/validator/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,27 @@ use std::sync::Arc;
use zksync_concurrency::time;
use zksync_consensus_utils::enum_util::Variant;

/// Builder of Setup.
pub struct SetupBuilder(Setup);
/// Test setup.
#[derive(Debug,Clone)]
pub struct Setup(SetupInner);

impl SetupBuilder {
/// Build the setup.
pub fn build(self) -> Setup { self.0 }
impl Setup {
/// New Setup builder.
pub fn new(rng: &mut impl Rng, validators: usize) -> Self {
let keys: Vec<SecretKey> = (0..validators).map(|_| rng.gen()).collect();
let genesis = Genesis {
validators: ValidatorSet::new(keys.iter().map(|k| k.public())).unwrap(),
forks: ForkSet::new(vec![Fork {
number: ForkNumber(rng.gen_range(0..100)),
first_block: BlockNumber(rng.gen_range(0..100)),
first_parent: None,
}]).unwrap(),
};
Self(SetupInner { keys, genesis, blocks: vec![] })
}

/// Produce a fork at the current head.
pub fn fork(mut self) -> Self {
pub fn fork(&mut self) {
let number = self.0.genesis.forks.current().number.next();
self.0.genesis.forks.push(match self.0.blocks.last() {
Some(b) => Fork {
Expand All @@ -40,7 +52,6 @@ impl SetupBuilder {
first_block: self.0.genesis.forks.root().first_block.next(),
},
}).unwrap();
self
}

fn next(&self) -> BlockNumber {
Expand Down Expand Up @@ -79,17 +90,22 @@ impl SetupBuilder {
}

/// Pushes `count` blocks with a random payload.
pub fn push_blocks(mut self, rng: &mut impl Rng, count: usize) -> Self {
pub fn push_blocks(&mut self, rng: &mut impl Rng, count: usize) {
for _ in 0..count {
self.push_block(rng.gen());
}
self
}

/// Finds the block by the number.
pub fn block(&self, n: BlockNumber) -> Option<&FinalBlock> {
let first = self.0.blocks.first()?.number();
self.0.blocks.get(n.0.checked_sub(first.0)? as usize)
}
}

/// Setup.
#[derive(Debug, Clone)]
pub struct Setup {
pub struct SetupInner {
/// Validators' secret keys.
pub keys: Vec<SecretKey>,
/// Past blocks.
Expand All @@ -98,33 +114,9 @@ pub struct Setup {
pub genesis: Genesis,
}

impl Setup {
/// New Setup builder.
pub fn builder(rng: &mut impl Rng, validators: usize) -> SetupBuilder {
let keys: Vec<SecretKey> = (0..validators).map(|_| rng.gen()).collect();
let genesis = Genesis {
validators: ValidatorSet::new(keys.iter().map(|k| k.public())).unwrap(),
forks: ForkSet::new(vec![Fork {
number: ForkNumber(rng.gen_range(0..100)),
first_block: BlockNumber(rng.gen_range(0..100)),
first_parent: None,
}]).unwrap(),
};
SetupBuilder(Self { keys, genesis, blocks: vec![] })
}

/// Constructs GenesisSetup.
pub fn new(rng: &mut impl Rng, validators: usize) -> Self {
Self::builder(rng,validators).build()
}

/// Finds the block by the number.
/// `Setup` is assumed to be constructed via `SetupBuilder`,
/// and therefore the blocks have consecutive numbers.
pub fn block(&self, n: BlockNumber) -> Option<&FinalBlock> {
let first = self.blocks.first()?.number();
self.blocks.get(n.0.checked_sub(first.0)? as usize)
}
impl std::ops::Deref for Setup {
type Target = SetupInner;
fn deref(&self) -> &Self::Target { &self.0 }
}

impl AggregateSignature {
Expand Down

0 comments on commit ca38160

Please sign in to comment.