use std::{thread, time::Duration};
use rand_core::OsRng;
use hotstuff_rs::types::{
crypto_primitives::SigningKey, data_types::Power, update_sets::ValidatorSetUpdates,
};
use common::{logging::log_with_context, number_app::NumberApp};
mod common;
use crate::common::{network::mock_network, node::Node, number_app::NumberAppTransaction};
#[test]
fn progress_and_validator_set_update_test() {
let mut csprg = OsRng {};
let keypairs: Vec<SigningKey> = (0..3).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
};
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 an Increment transaction to the initial validator.",
);
nodes[0].submit_transaction(NumberAppTransaction::Increment);
log_with_context(
None,
"Polling the app state of every replica until the value is 1.",
);
while !nodes.iter().all(|node| node.number() == 1) {
thread::sleep(Duration::from_millis(500));
}
log_with_context(None, "Submitting 2 set validator transactions to the initial validator to register the rest (2) of the peers.");
let node_1 = nodes[1].verifying_key();
nodes[0].submit_transaction(NumberAppTransaction::SetValidator(node_1, Power::new(1)));
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 we have 3 validators.",
);
while !nodes
.iter()
.all(|node| node.committed_validator_set().len() == 3)
{
thread::sleep(Duration::from_millis(500));
}
log_with_context(
None,
"Submitting an increment transaction to each of the 3 validators we have now.",
);
nodes[0].submit_transaction(NumberAppTransaction::Increment);
nodes[1].submit_transaction(NumberAppTransaction::Increment);
nodes[2].submit_transaction(NumberAppTransaction::Increment);
log_with_context(
None,
"Polling the app state of every replica until the value is 4",
);
while !nodes.iter().all(|node| node.number() == 4) {
thread::sleep(Duration::from_millis(500));
}
}