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 1 commit
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
13 changes: 8 additions & 5 deletions code/examples/channel/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub async fn run(state: &mut State, channels: &mut Channels<TestContext>) -> eyr
sleep(Duration::from_millis(200)).await;

if reply
.send((start_height, state.get_validator_set().clone()))
.send((start_height, state.get_validator_set(start_height).clone()))
.is_err()
{
error!("Failed to send ConsensusReady reply");
Expand Down Expand Up @@ -171,8 +171,11 @@ pub async fn run(state: &mut State, channels: &mut Channels<TestContext>) -> eyr
//
// 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(state.get_validator_set().clone())).is_err() {
AppMsg::GetValidatorSet { height, reply } => {
if reply
.send(Some(state.get_validator_set(height).clone()))
.is_err()
{
error!("Failed to send GetValidatorSet reply");
}
}
Expand Down Expand Up @@ -201,7 +204,7 @@ pub async fn run(state: &mut State, channels: &mut Channels<TestContext>) -> eyr
if reply
.send(ConsensusMsg::StartHeight(
state.current_height,
state.get_validator_set().clone(),
state.get_validator_set(state.current_height).clone(),
))
.is_err()
{
Expand All @@ -214,7 +217,7 @@ pub async fn run(state: &mut State, channels: &mut Channels<TestContext>) -> eyr
if reply
.send(ConsensusMsg::RestartHeight(
state.current_height,
state.get_validator_set().clone(),
state.get_validator_set(state.current_height).clone(),
))
.is_err()
{
Expand Down
28 changes: 23 additions & 5 deletions code/examples/channel/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,27 @@ impl State {
parts
}

/// Returns the set of validators.
pub fn get_validator_set(&self) -> &ValidatorSet {
&self.genesis.validator_set
/// Returns the validator set for the given height.
/// The validator set is rotated based on the height, selecting floor((n+1)/2)
/// validators from the genesis validator set.
pub fn get_validator_set(&self, height: Height) -> ValidatorSet {
let num_validators = self.genesis.validator_set.len();
let selection_size = (num_validators + 1) / 2;

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

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

/// Verifies the signature of the proposal.
Expand Down Expand Up @@ -436,8 +454,8 @@ impl State {
};

// Retrieve the the proposer
let proposer = self
.get_validator_set()
let validator_set = self.get_validator_set(self.current_height);
let proposer = validator_set
.get_by_address(&parts.proposer)
.ok_or(SignatureVerificationError::ProposerNotFound)?;

Expand Down
40 changes: 29 additions & 11 deletions docs/tutorials/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ fn factor_value(value: Value) -> Vec<u64> {

```

Then, let's introduce _getter_ methods that are quite self-explanatory:
Then, let's introduce _getter_ methods that are quite self-explanatory. For the validator set, we select a rotating subset (floor((n+1)/2)) of the validators from the genesis validator set based on the height. This is done to showcase the ability to change the validator set over time (or height, in this case).

```rust
impl State {
Expand Down Expand Up @@ -549,9 +549,27 @@ impl State {
)))
}

// Returns the set of validators.
pub fn get_validator_set(&self) -> &ValidatorSet {
&self.genesis.validator_set
/// Returns the validator set for the given height.
/// The validator set is rotated based on the height,selecting floor((n+1)/2)
/// validators from the genesis validator set.
pub fn get_validator_set(&self, height: Height) -> ValidatorSet {
let num_validators = self.genesis.validator_set.len();
let selection_size = (num_validators + 1) / 2;

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

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

// ...
Expand Down Expand Up @@ -785,8 +803,8 @@ impl State {
};

// Retrieve the the proposer
let proposer = self
.get_validator_set()
let validator_set = self.get_validator_set(self.current_height);
let proposer = validator_set
.get_by_address(&parts.proposer)
.ok_or(SignatureVerificationError::ProposerNotFound)?;

Expand Down Expand Up @@ -1097,7 +1115,7 @@ which is either 1 or the next height after the last decided value in the store (
sleep(Duration::from_millis(200)).await;

if reply
.send((start_height, state.get_validator_set().clone()))
.send((start_height, state.get_validator_set(start_height).clone()))
.is_err()
{
error!("Failed to send ConsensusReady reply");
Expand Down Expand Up @@ -1310,8 +1328,8 @@ In our case, our validator set stays constant between heights so we can
send back the validator set found in our genesis state.

```rust
AppMsg::GetValidatorSet { height: _, reply } => {
if reply.send(state.get_validator_set().clone()).is_err() {
AppMsg::GetValidatorSet { height, reply } => {
if reply.send(state.get_validator_set(height).clone()).is_err() {
error!("Failed to send GetValidatorSet reply");
}
}
Expand Down Expand Up @@ -1391,7 +1409,7 @@ If `commit` fails we can re-run consensus for the same height.
if reply
.send(ConsensusMsg::StartHeight(
state.current_height,
state.get_validator_set().clone(),
state.get_validator_set(state.current_height).clone(),
))
.is_err()
{
Expand All @@ -1404,7 +1422,7 @@ If `commit` fails we can re-run consensus for the same height.
if reply
.send(ConsensusMsg::RestartHeight(
state.current_height,
state.get_validator_set().clone(),
state.get_validator_set(state.current_height).clone(),
))
.is_err()
{
Expand Down
Loading