Skip to content

feat(code/test): Dynamic validator set #987

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

Merged
merged 14 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 7 additions & 7 deletions code/crates/core-consensus/src/handle/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ where
}

DriverOutput::Vote(vote) => {
info!(
vote_type = ?vote.vote_type(),
value = %PrettyVal(vote.value().as_ref()),
round = %vote.round(),
"Voting",
);

if state.is_validator() {
info!(
vote_type = ?vote.vote_type(),
value = %PrettyVal(vote.value().as_ref()),
round = %vote.round(),
"Voting",
);

let vote_type = vote.vote_type();

let extended_vote = extend_vote(co, vote).await?;
Expand Down
5 changes: 4 additions & 1 deletion code/crates/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ malachitebft-core-types = { workspace = true }
malachitebft-config = { workspace = true }
malachitebft-core-consensus = { workspace = true }
malachitebft-proto = { workspace = true }
malachitebft-signing-ed25519 = { workspace = true, features = ["rand", "serde"] }
malachitebft-signing-ed25519 = { workspace = true, features = [
"rand",
"serde",
] }
malachitebft-sync = { workspace = true }

async-trait = { workspace = true }
Expand Down
44 changes: 39 additions & 5 deletions code/crates/test/app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ pub async fn run(
// We can simply respond by telling the engine to start consensus
// at the next height, and provide it with the genesis validator set
if reply
.send((start_height, genesis.validator_set.clone()))
.send((
start_height,
state
.ctx
.middleware()
.get_validator_set(&state.ctx, start_height, start_height, &genesis)
.expect("Validator set should be available"),
))
.is_err()
{
error!("Failed to send ConsensusReady reply");
Expand Down Expand Up @@ -164,8 +171,17 @@ pub async fn run(
//
// In our case, our validator set stays constant between heights so we can
// send back the validator set found in our genesis state.
AppMsg::GetValidatorSet { height: _, reply } => {
if reply.send(genesis.validator_set.clone()).is_err() {
AppMsg::GetValidatorSet { height, reply } => {
if reply
.send(
state
.ctx
.middleware()
.get_validator_set(&state.ctx, state.current_height, height, &genesis)
.expect("Validator set should be available"),
)
.is_err()
{
error!("Failed to send GetValidatorSet reply");
}
}
Expand Down Expand Up @@ -195,7 +211,16 @@ pub async fn run(
if reply
.send(ConsensusMsg::StartHeight(
state.current_height,
genesis.validator_set.clone(),
state
.ctx
.middleware()
.get_validator_set(
&state.ctx,
state.current_height,
state.current_height,
&genesis,
)
.expect("Validator set should be available"),
))
.is_err()
{
Expand All @@ -210,7 +235,16 @@ pub async fn run(
if reply
.send(ConsensusMsg::RestartHeight(
state.current_height,
genesis.validator_set.clone(),
state
.ctx
.middleware()
.get_validator_set(
&state.ctx,
state.current_height,
state.current_height,
&genesis,
)
.expect("Validator set should be available"),
))
.is_err()
{
Expand Down
112 changes: 111 additions & 1 deletion code/crates/test/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ use core::fmt;
use malachitebft_core_consensus::{LocallyProposedValue, ProposedValue};
use malachitebft_core_types::{CommitCertificate, NilOrVal, Round};

use crate::{Address, Height, Proposal, TestContext, Value, ValueId, Vote};
use crate::{Address, Genesis, Height, Proposal, TestContext, ValidatorSet, Value, ValueId, Vote};

pub trait Middleware: fmt::Debug + Send + Sync {
fn get_validator_set(
&self,
_ctx: &TestContext,
_current_height: Height,
_height: Height,
genesis: &Genesis,
) -> Option<ValidatorSet> {
Some(genesis.validator_set.clone())
}

fn new_proposal(
&self,
_ctx: &TestContext,
Expand Down Expand Up @@ -62,3 +72,103 @@ pub trait Middleware: fmt::Debug + Send + Sync {
pub struct DefaultMiddleware;

impl Middleware for DefaultMiddleware {}

#[derive(Copy, Clone, Debug)]
pub struct RotateValidators {
pub selection_size: usize,
}

impl Middleware for RotateValidators {
// Selects N validators from index height % num_validators (circularly).
// Example:
// - N = 3, num_validators = 5, height = 0 -> [0, 1, 2]
// - N = 3, num_validators = 5, height = 3 -> [3, 4, 0]
fn get_validator_set(
&self,
_ctx: &TestContext,
_current_height: Height,
height: Height,
genesis: &Genesis,
) -> Option<ValidatorSet> {
let num_validators = genesis.validator_set.len();

if num_validators <= self.selection_size {
return Some(genesis.validator_set.clone());
}

let start_index = height.as_u64() as usize % num_validators;
let end_index = start_index + self.selection_size;

let selected = genesis
.validator_set
.get_all()
.into_iter()
.cycle()
.skip(start_index)
.take(end_index - start_index)
.collect::<Vec<_>>();

Some(ValidatorSet::new(selected))
}
}

#[derive(Copy, Clone, Debug)]
pub struct EpochValidators {
pub epochs_limit: usize,
}

impl Middleware for EpochValidators {
fn get_validator_set(
&self,
_ctx: &TestContext,
current_height: Height,
height: Height,
genesis: &Genesis,
) -> Option<ValidatorSet> {
if height.as_u64() > current_height.as_u64() + self.epochs_limit as u64 {
return None;
}

Some(genesis.validator_set.clone())
}
}

#[derive(Copy, Clone, Debug)]
pub struct RotateEpochValidators {
pub selection_size: usize,
pub epochs_limit: usize,
}

impl Middleware for RotateEpochValidators {
fn get_validator_set(
&self,
_ctx: &TestContext,
current_height: Height,
height: Height,
genesis: &Genesis,
) -> Option<ValidatorSet> {
if height.as_u64() > current_height.as_u64() + self.epochs_limit as u64 {
return None;
}

let num_validators = genesis.validator_set.len();

if num_validators <= self.selection_size {
return Some(genesis.validator_set.clone());
}

let start_index = height.as_u64() as usize % num_validators;
let end_index = start_index + self.selection_size;

let selected = genesis
.validator_set
.get_all()
.into_iter()
.cycle()
.skip(start_index)
.take(end_index - start_index)
.collect::<Vec<_>>();

Some(ValidatorSet::new(selected))
}
}
10 changes: 10 additions & 0 deletions code/crates/test/src/validator_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ impl ValidatorSet {
}
}

/// Get the number of validators in the set
pub fn len(&self) -> usize {
self.validators.len()
}

/// Get the validators in the set
pub fn get_all(&self) -> Vec<Validator> {
self.validators.iter().cloned().collect()
}

/// The total voting power of the validator set
pub fn total_voting_power(&self) -> VotingPower {
self.validators.iter().map(|v| v.voting_power).sum()
Expand Down
1 change: 1 addition & 0 deletions code/crates/test/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod n3f0_consensus_mode;
mod n3f0_pubsub_protocol;
mod n3f1;
mod reset;
mod validator_set;
mod value_sync;
mod vote_sync;
mod vote_sync_bcast;
Expand Down
26 changes: 26 additions & 0 deletions code/crates/test/tests/it/validator_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::time::Duration;

use informalsystems_malachitebft_test::middleware::RotateValidators;

use crate::TestBuilder;

#[tokio::test]
async fn rotate_validator_set() {
const HEIGHT: u64 = 3;
const NUM_NODES: usize = 3;
const NUM_VALIDATORS_PER_HEIGHT: usize = 2;

let mut test = TestBuilder::<()>::new();

for _ in 0..NUM_NODES {
test.add_node()
.with_middleware(RotateValidators {
selection_size: NUM_VALIDATORS_PER_HEIGHT,
})
.start()
.wait_until(HEIGHT)
.success();
}

test.build().run(Duration::from_secs(50)).await
}
Loading