Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add liveness tests (BFT-387) #46

Merged
merged 9 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions node/actors/bft/src/leader/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use zksync_consensus_roles::validator::{
async fn replica_prepare_sanity() {
let mut util = UTHarness::new_many().await;

util.set_view(util.owner_as_view_leader());

let replica_prepare = util.new_current_replica_prepare(|_| {}).cast().unwrap().msg;
util.dispatch_replica_prepare_many(
vec![replica_prepare; util.consensus_threshold()],
Expand Down Expand Up @@ -60,8 +58,6 @@ async fn replica_prepare_sanity_yield_leader_prepare() {
async fn replica_prepare_sanity_yield_leader_prepare_reproposal() {
let mut util = UTHarness::new_many().await;

util.set_view(util.owner_as_view_leader());

let replica_prepare: ReplicaPrepare =
util.new_unfinalized_replica_prepare().cast().unwrap().msg;
util.dispatch_replica_prepare_many(
Expand Down Expand Up @@ -141,7 +137,7 @@ async fn replica_prepare_during_commit() {
async fn replica_prepare_not_leader_in_view() {
let mut util = UTHarness::new_with(2).await;

let current_view_leader = util.view_leader(util.current_replica_view());
let current_view_leader = util.view_leader(util.replica_view());
assert_ne!(current_view_leader, util.owner_key().public());

let replica_prepare = util.new_current_replica_prepare(|_| {});
Expand Down Expand Up @@ -277,8 +273,6 @@ async fn replica_prepare_non_validator_signer() {
async fn replica_commit_sanity() {
let mut util = UTHarness::new_many().await;

util.set_view(util.owner_as_view_leader());

let replica_commit = util
.new_procedural_replica_commit_many()
.await
Expand Down Expand Up @@ -330,7 +324,7 @@ async fn replica_commit_old() {
.cast::<ReplicaCommit>()
.unwrap()
.msg;
replica_commit.view = util.current_replica_view().prev();
replica_commit.view = util.replica_view().prev();
let replica_commit = util
.owner_key()
.sign_msg(ConsensusMsg::ReplicaCommit(replica_commit));
Expand All @@ -339,8 +333,8 @@ async fn replica_commit_old() {
assert_matches!(
res,
Err(ReplicaCommitError::Old { current_view, current_phase }) => {
assert_eq!(current_view, util.current_replica_view());
assert_eq!(current_phase, util.current_replica_phase());
assert_eq!(current_view, util.replica_view());
assert_eq!(current_phase, util.replica_phase());
}
);
}
Expand All @@ -349,7 +343,7 @@ async fn replica_commit_old() {
async fn replica_commit_not_leader_in_view() {
let mut util = UTHarness::new_with(2).await;

let current_view_leader = util.view_leader(util.current_replica_view());
let current_view_leader = util.view_leader(util.replica_view());
assert_ne!(current_view_leader, util.owner_key().public());

let replica_commit = util.new_current_replica_commit(|_| {});
Expand Down
16 changes: 4 additions & 12 deletions node/actors/bft/src/replica/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use zksync_consensus_roles::validator::{
async fn leader_prepare_sanity() {
let mut util = UTHarness::new_many().await;

util.set_view(util.owner_as_view_leader());

let leader_prepare = util.new_procedural_leader_prepare_many().await;
util.dispatch_leader_prepare(leader_prepare).await.unwrap();
}
Expand All @@ -24,8 +22,6 @@ async fn leader_prepare_sanity() {
async fn leader_prepare_reproposal_sanity() {
let mut util = UTHarness::new_many().await;

util.set_view(util.owner_as_view_leader());

let replica_prepare: ReplicaPrepare =
util.new_unfinalized_replica_prepare().cast().unwrap().msg;
util.dispatch_replica_prepare_many(
Expand Down Expand Up @@ -136,7 +132,7 @@ async fn leader_prepare_old_view() {
.cast::<LeaderPrepare>()
.unwrap()
.msg;
leader_prepare.view = util.current_replica_view().prev();
leader_prepare.view = util.replica_view().prev();
let leader_prepare = util
.owner_key()
.sign_msg(ConsensusMsg::LeaderPrepare(leader_prepare));
Expand All @@ -145,8 +141,8 @@ async fn leader_prepare_old_view() {
assert_matches!(
res,
Err(LeaderPrepareError::Old { current_view, current_phase }) => {
assert_eq!(current_view, util.current_replica_view());
assert_eq!(current_phase, util.current_replica_phase());
assert_eq!(current_view, util.replica_view());
assert_eq!(current_phase, util.replica_phase());
}
);
}
Expand Down Expand Up @@ -375,8 +371,6 @@ async fn leader_prepare_proposal_non_sequential_number() {
async fn leader_prepare_reproposal_without_quorum() {
let mut util = UTHarness::new_many().await;

util.set_view(util.owner_as_view_leader());

let mut leader_prepare = util
.new_procedural_leader_prepare_many()
.await
Expand Down Expand Up @@ -446,8 +440,6 @@ async fn leader_prepare_reproposal_invalid_block() {
async fn leader_commit_sanity() {
let mut util = UTHarness::new_many().await;

util.set_view(util.owner_as_view_leader());

let leader_commit = util.new_procedural_leader_commit_many().await;
util.dispatch_leader_commit(leader_commit).await.unwrap();
}
Expand Down Expand Up @@ -489,7 +481,7 @@ async fn leader_commit_sanity_yield_replica_prepare() {
async fn leader_commit_invalid_leader() {
let mut util = UTHarness::new_with(2).await;

let current_view_leader = util.view_leader(util.current_replica_view());
let current_view_leader = util.view_leader(util.replica_view());
assert_ne!(current_view_leader, util.owner_key().public());

let leader_commit = util.new_rnd_leader_commit(|_| {});
Expand Down
59 changes: 53 additions & 6 deletions node/actors/bft/src/testonly/ut_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ impl UTHarness {
pub(crate) async fn new_one() -> UTHarness {
UTHarness::new_with(1).await
}

/// Creates a new `UTHarness` with minimally-significant validator set size.
pub(crate) async fn new_many() -> UTHarness {
let num_validators = 6;
assert_matches!(crate::misc::faulty_replicas(num_validators), res if res > 0);
UTHarness::new_with(num_validators).await
let mut util = UTHarness::new_with(num_validators).await;
util.set_replica_view(util.owner_as_view_leader_next());
util
}

/// Creates a new `UTHarness` with the specified validator set size.
Expand Down Expand Up @@ -70,6 +71,28 @@ impl UTHarness {
}
}

pub(crate) fn check_recovery_after_timeout(&mut self) {
self.set_replica_view(self.owner_as_view_leader_next());

let base_replica_view = self.replica_view();
let base_leader_view = self.leader_view();
assert!(base_leader_view < base_replica_view);

assert_eq!(self.replica_phase(), Phase::Prepare);

let replica_prepare = self.new_current_replica_prepare(|_| {}).cast().unwrap().msg;
self.dispatch_replica_prepare_many(
vec![replica_prepare; self.consensus_threshold()],
self.keys(),
)
.unwrap();

assert_eq!(self.replica_view(), base_replica_view);
assert_eq!(self.leader_view(), base_replica_view);
assert_eq!(self.replica_phase(), Phase::Prepare);
assert_eq!(self.leader_phase(), Phase::Commit);
}

pub(crate) fn consensus_threshold(&self) -> usize {
crate::misc::consensus_threshold(self.keys.len())
}
Expand All @@ -78,8 +101,8 @@ impl UTHarness {
&self.consensus.inner.secret_key
}

pub(crate) fn owner_as_view_leader(&self) -> ViewNumber {
let mut view = self.current_replica_view();
pub(crate) fn owner_as_view_leader_next(&self) -> ViewNumber {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: IMO the "next" suffix seems to suggest at the very least that the returned view will be AFTER the current view.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to imply that it's not beyond the next one.
But perhaps there will be a use case where the current is to be skipped, so I made it explicit with a longer name: owner_as_view_leader_current_or_next.

let mut view = self.replica_view();
while self.view_leader(view) != self.owner_key().public() {
view = view.next();
}
Expand Down Expand Up @@ -382,6 +405,22 @@ impl UTHarness {
.unwrap()
}

pub(crate) async fn sim_timeout(&mut self) {
scope::run!(&self.ctx, |ctx, s| {
moshababo marked this conversation as resolved.
Show resolved Hide resolved
s.spawn(async {
let _ = self
.consensus
.replica
.process_input(ctx, &self.consensus.inner, None)
.await;
Ok(())
})
.join(ctx)
})
.await
.unwrap()
}

pub(crate) async fn dispatch_leader_commit(
&mut self,
msg: Signed<ConsensusMsg>,
Expand Down Expand Up @@ -412,14 +451,22 @@ impl UTHarness {
})
}

pub(crate) fn current_replica_view(&self) -> ViewNumber {
pub(crate) fn replica_view(&self) -> ViewNumber {
self.consensus.replica.view
}

pub(crate) fn current_replica_phase(&self) -> Phase {
pub(crate) fn leader_view(&self) -> ViewNumber {
self.consensus.leader.view
}

pub(crate) fn replica_phase(&self) -> Phase {
self.consensus.replica.phase
}

pub(crate) fn leader_phase(&self) -> Phase {
self.consensus.leader.phase
}

pub(crate) fn view_leader(&self, view: ViewNumber) -> validator::PublicKey {
self.consensus.inner.view_leader(view)
}
Expand Down
Loading
Loading