Skip to content

Commit

Permalink
added a test
Browse files Browse the repository at this point in the history
  • Loading branch information
pompon0 committed Dec 13, 2023
1 parent 72590fe commit 8768646
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
15 changes: 15 additions & 0 deletions node/actors/bft/src/testonly/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ impl PayloadSource for RandomPayloadSource {
}
}

/// Never provides a payload.
pub struct InavailablePayloadSource;

#[async_trait::async_trait]
impl PayloadSource for InavailablePayloadSource {
async fn propose(
&self,
ctx: &ctx::Ctx,
_block_number: validator::BlockNumber,
) -> ctx::Result<validator::Payload> {
ctx.canceled().await;
Err(ctx::Canceled.into())
}
}

/// Creates a genesis block with the given payload
/// and a validator set for the chain.
pub fn make_genesis(
Expand Down
15 changes: 13 additions & 2 deletions node/actors/bft/src/testonly/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Fuzz;
use crate::io;
use crate::{io, testonly};
use rand::Rng;
use std::sync::Arc;
use zksync_concurrency::{ctx, scope};
Expand All @@ -13,6 +13,8 @@ use zksync_consensus_utils::pipe::DispatcherPipe;
pub(crate) enum Behavior {
/// A replica that is always online and behaves honestly.
Honest,
/// Same as honest, except that it never proposes a block (which is a legit behavior)
HonestNotProposing,
/// A replica that is always offline and does not produce any messages.
Offline,
/// A replica that is always online and behaves randomly. It will produce
Expand All @@ -23,6 +25,15 @@ pub(crate) enum Behavior {
Byzantine,
}

impl Behavior {
pub(crate) fn payload_source(&self) -> Box<dyn crate::PayloadSource> {
match self {
Self::HonestNotProposing => Box::new(testonly::InavailablePayloadSource),
_ => Box::new(testonly::RandomPayloadSource),
}
}
}

/// Struct representing a node.
pub(super) struct Node {
pub(crate) net: network::testonly::Instance,
Expand Down Expand Up @@ -64,7 +75,7 @@ impl Node {
io::OutputMessage::Network(mut message) => {
let message_to_send = match self.behavior {
Behavior::Offline => continue,
Behavior::Honest => message,
Behavior::Honest | Behavior::HonestNotProposing => message,
// Create a random consensus message and broadcast.
Behavior::Random => ConsensusInputMessage {
message: rng.gen(),
Expand Down
4 changes: 2 additions & 2 deletions node/actors/bft/src/testonly/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Behavior, Node, RandomPayloadSource};
use super::{Behavior, Node};
use crate::testonly;
use anyhow::Context;
use std::{collections::HashMap, sync::Arc};
Expand Down Expand Up @@ -108,7 +108,7 @@ async fn run_nodes(ctx: &ctx::Ctx, network: Network, nodes: &[Node]) -> anyhow::
node.net.consensus_config().key.clone(),
validator_set,
storage,
&RandomPayloadSource,
&*node.behavior.payload_source(),
)
.await
.context("consensus.run()")
Expand Down
15 changes: 15 additions & 0 deletions node/actors/bft/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,18 @@ async fn timeout_leader_in_consecutive_prepare() {
util.new_leader_commit(ctx).await;
util.produce_block_after_timeout(ctx).await;
}

/// Not being able to propose a block shouldn't cause a deadlock.
#[tokio::test]
async fn non_proposing_leader() {
zksync_concurrency::testonly::abort_on_panic();
let ctx = &ctx::test_root(&ctx::AffineClock::new(5.));
Test {
network: Network::Real,
nodes: vec![Behavior::Honest, Behavior::HonestNotProposing],
blocks_to_finalize: 10,
}
.run(ctx)
.await
.unwrap()
}

0 comments on commit 8768646

Please sign in to comment.