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 all 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
53 changes: 42 additions & 11 deletions code/crates/test/app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ pub async fn run(
sleep(Duration::from_millis(200)).await;

// 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()))
.is_err()
{
// at the next height, and provide it with the appropriate validator set
let validator_set = state
.ctx
.middleware()
.get_validator_set(&state.ctx, start_height, start_height, &genesis)
.expect("Validator set should be available");

if reply.send((start_height, validator_set)).is_err() {
error!("Failed to send ConsensusReady reply");
}
}
Expand Down Expand Up @@ -162,10 +165,16 @@ pub async fn run(
// than the one we are at (e.g. because we are lagging behind a little bit),
// the engine may ask us for the validator set at that height.
//
// 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(Some(genesis.validator_set.clone())).is_err() {
// We send back the appropriate validator set for that height.
AppMsg::GetValidatorSet { height, reply } => {
let validator_set = state.ctx.middleware().get_validator_set(
&state.ctx,
state.current_height,
height,
&genesis,
);

if reply.send(validator_set).is_err() {
error!("Failed to send GetValidatorSet reply");
}
}
Expand All @@ -192,10 +201,21 @@ pub async fn run(
match state.commit(certificate).await {
Ok(_) => {
// And then we instruct consensus to start the next height
let validator_set = state
.ctx
.middleware()
.get_validator_set(
&state.ctx,
state.current_height,
state.current_height,
&genesis,
)
.expect("Validator set should be available");

if reply
.send(ConsensusMsg::StartHeight(
state.current_height,
genesis.validator_set.clone(),
validator_set,
))
.is_err()
{
Expand All @@ -207,10 +227,21 @@ pub async fn run(
error!("Commit failed: {e}");
error!("Restarting height {}", state.current_height);

let validator_set = state
.ctx
.middleware()
.get_validator_set(
&state.ctx,
state.current_height,
state.current_height,
&genesis,
)
.expect("Validator set should be available");

if reply
.send(ConsensusMsg::RestartHeight(
state.current_height,
genesis.validator_set.clone(),
validator_set,
))
.is_err()
{
Expand Down
107 changes: 106 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,98 @@ pub trait Middleware: fmt::Debug + Send + Sync {
pub struct DefaultMiddleware;

impl Middleware for DefaultMiddleware {}

fn select_validators(genesis: &Genesis, height: Height, selection_size: usize) -> ValidatorSet {
let num_validators = genesis.validator_set.len();

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

ValidatorSet::new(
genesis
.validator_set
.iter()
.cycle()
.skip(height.as_u64() as usize % num_validators)
.take(selection_size)
.cloned()
.collect::<Vec<_>>(),
)
}

#[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());
}

Some(select_validators(genesis, height, self.selection_size))
}
}

#[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());
}

Some(select_validators(genesis, height, self.selection_size))
}
}
16 changes: 16 additions & 0 deletions code/crates/test/src/validator_set.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::slice;
use std::sync::Arc;

use malachitebft_core_types::VotingPower;
Expand Down Expand Up @@ -69,6 +70,21 @@ impl ValidatorSet {
}
}

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

/// Check if the set is empty
pub fn is_empty(&self) -> bool {
self.validators.is_empty()
}

/// Iterate over the validators in the set
pub fn iter(&self) -> slice::Iter<Validator> {
self.validators.iter()
}

/// 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 = 20;
const NUM_NODES: usize = 5;
const NUM_VALIDATORS_PER_HEIGHT: usize = 3;

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
}
68 changes: 68 additions & 0 deletions code/crates/test/tests/it/value_sync.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use informalsystems_malachitebft_test::middleware::RotateEpochValidators;
use malachitebft_config::ValuePayload;

use crate::{TestBuilder, TestParams};
Expand Down Expand Up @@ -197,3 +198,70 @@ pub async fn start_late() {
)
.await
}

#[tokio::test]
pub async fn start_late_rotate_epoch_validator_set() {
const HEIGHT: u64 = 20;

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

test.add_node()
.with_voting_power(10)
.with_middleware(RotateEpochValidators {
selection_size: 2,
epochs_limit: 5,
})
.start()
.wait_until(HEIGHT)
.success();

test.add_node()
.with_voting_power(10)
.with_middleware(RotateEpochValidators {
selection_size: 2,
epochs_limit: 5,
})
.start()
.wait_until(HEIGHT)
.success();

test.add_node()
.with_voting_power(10)
.with_middleware(RotateEpochValidators {
selection_size: 2,
epochs_limit: 5,
})
.start()
.wait_until(HEIGHT)
.success();

// Add 2 full nodes with one starting late
test.add_node()
.full_node()
.with_middleware(RotateEpochValidators {
selection_size: 2,
epochs_limit: 5,
})
.start()
.wait_until(HEIGHT)
.success();
test.add_node()
.full_node()
.with_middleware(RotateEpochValidators {
selection_size: 2,
epochs_limit: 5,
})
.start_after(1, Duration::from_secs(5))
.wait_until(HEIGHT)
.success();

test.build()
.run_with_params(
Duration::from_secs(30),
TestParams {
enable_value_sync: true,
..Default::default()
},
)
.await
}
Loading
Loading