use std::{thread, time::Duration};
use rand_core::OsRng;
use hotstuff_rs::types::{
crypto_primitives::SigningKey, data_types::Power, update_sets::ValidatorSetUpdates,
};
mod common;
use common::{
logging::log_with_context,
network::mock_network,
node::Node,
number_app::{NumberApp, NumberAppTransaction},
};
#[test]
fn multiple_validator_set_updates_test() {
let mut csprg = OsRng {};
let keypairs: Vec<SigningKey> = (0..6).map(|_| SigningKey::generate(&mut csprg)).collect();
let network_stubs = mock_network(keypairs.iter().map(|kp| kp.verifying_key()));
let init_as = NumberApp::initial_app_state();
let init_vs_updates = {
let mut vs_updates = ValidatorSetUpdates::new();
vs_updates.insert(keypairs[0].verifying_key(), Power::new(1));
vs_updates.insert(keypairs[1].verifying_key(), Power::new(1));
vs_updates
};
let mut nodes: Vec<Node> = keypairs
.into_iter()
.zip(network_stubs)
.map(|(keypair, network)| {
Node::new(keypair, network, init_as.clone(), init_vs_updates.clone())
})
.collect();
log_with_context(None, "Submitting a set validator transaction to the initial validator to register 1 more validator (small power).");
let node_2 = nodes[2].verifying_key();
nodes[0].submit_transaction(NumberAppTransaction::SetValidator(node_2, Power::new(1)));
log_with_context(
None,
"Polling the validator set of every replica until all see 3 validators.",
);
while !nodes
.iter()
.all(|node| node.committed_validator_set().len() == 3)
{
thread::sleep(Duration::from_millis(500));
}
log_with_context(None, "Submitting a set validator transaction to one of the initial validators to register 1 more validator (big power).");
let node_3 = nodes[3].verifying_key();
nodes[1].submit_transaction(NumberAppTransaction::SetValidator(node_3, Power::new(3)));
log_with_context(
None,
"Polling the validator set of every replica until all see 4 validators.",
);
while !nodes
.iter()
.all(|node| node.committed_validator_set().len() == 4)
{
thread::sleep(Duration::from_millis(500));
}
log_with_context(None, "Submitting a set validator transaction to one of the initial validators to delete the current validators and add two new validators.");
let node_4 = nodes[4].verifying_key();
let node_5 = nodes[5].verifying_key();
let node_0 = nodes[0].verifying_key();
let node_1 = nodes[1].verifying_key();
nodes[1].submit_transaction(NumberAppTransaction::SetValidator(node_4, Power::new(2)));
nodes[1].submit_transaction(NumberAppTransaction::SetValidator(node_5, Power::new(4)));
nodes[1].submit_transaction(NumberAppTransaction::DeleteValidator(node_0));
nodes[1].submit_transaction(NumberAppTransaction::DeleteValidator(node_1));
nodes[1].submit_transaction(NumberAppTransaction::DeleteValidator(node_2));
nodes[1].submit_transaction(NumberAppTransaction::DeleteValidator(node_3));
log_with_context(
None,
"Polling the validator set of every replica until all see 2 validators.",
);
while !nodes
.iter()
.all(|node| node.committed_validator_set().len() == 2)
{
thread::sleep(Duration::from_millis(500));
}
}