Skip to content

Commit

Permalink
Test passes
Browse files Browse the repository at this point in the history
  • Loading branch information
AgeManning committed Oct 17, 2024
1 parent 4947c61 commit 55587b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion anchor/qbft/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Default for ConfigBuilder<DefaultLeaderFunction> {
config: Config {
operator_id: OperatorId::default(),
instance_height: InstanceHeight::default(),
committee_size: 5,
committee_size: 0,
committee_members: HashSet::new(),
quorum_size: 4,
round: Round::default(),
Expand Down
4 changes: 2 additions & 2 deletions anchor/qbft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ where
warn!(
from = *operator_id,
is_leader,
?consensus_data.data,
?consensus_data,
current_round = *self.current_round,
?self.state,
"Invalid propose message"
Expand Down Expand Up @@ -354,7 +354,7 @@ where
// Ensure the data is for the current round
&& self.current_round == consensus_data.round)
{
warn!(?consensus_data, "Received and invalid prepare message.");
warn!(?consensus_data, "Received an invalid prepare message.");
return;
}

Expand Down
39 changes: 19 additions & 20 deletions anchor/qbft/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const ENABLE_TEST_LOGGING: bool = true;

/// A struct to help build and initialise a test of running instances
struct TestQBFTCommitteeBuilder {
/// The size of the test committee. (Default is 5).
committee_size: usize,
/// The configuration to use for all the instances.
config: Config<DefaultLeaderFunction>,
/// Whether we should send back dummy validation input to each instance when it requests it.
Expand All @@ -33,9 +31,14 @@ struct TestQBFTCommitteeBuilder {

impl Default for TestQBFTCommitteeBuilder {
fn default() -> Self {
let mut config = Config::default();
// Set a default committee size of 5.
config.committee_size = 5;
// Populate the committee members
config.committee_members = (0..5).map(OperatorId::from).collect::<HashSet<_>>();

TestQBFTCommitteeBuilder {
committee_size: 5,
config: Config::default(),
config,
emulate_client_processor: true,
emulate_broadcast_network: true,
}
Expand All @@ -46,7 +49,7 @@ impl Default for TestQBFTCommitteeBuilder {
impl TestQBFTCommitteeBuilder {
/// Sets the size of the testing committee.
pub fn committee_size(mut self, committee_size: usize) -> Self {
self.committee_size = committee_size;
self.config.committee_size = committee_size;
self
}

Expand Down Expand Up @@ -81,8 +84,7 @@ impl TestQBFTCommitteeBuilder {
.init();
}

let (senders, mut receivers) =
construct_and_run_committee(self.config, self.committee_size);
let (senders, mut receivers) = construct_and_run_committee(self.config);

if self.emulate_client_processor {
receivers = emulate_client_processor(receivers, senders.clone(), data);
Expand Down Expand Up @@ -169,29 +171,26 @@ where
#[allow(clippy::type_complexity)]
fn construct_and_run_committee<D: Debug + Default + Clone + Send + Sync + 'static + Eq + Hash>(
mut config: Config<DefaultLeaderFunction>,
committee_size: usize,
) -> (
HashMap<OperatorId, UnboundedSender<InMessage<D>>>,
HashMap<OperatorId, UnboundedReceiver<OutMessage<D>>>,
) {
// The ID of a committee is just an integer in [0,committee_size)

// A collection of channels to send messages to each instance.
let mut senders = HashMap::with_capacity(committee_size);
let mut senders = HashMap::with_capacity(config.committee_size);
// A collection of channels to receive messages from each instances.
// We will redirect messages to each instance, simulating a broadcast network.
let mut receivers = HashMap::with_capacity(committee_size);
let mut receivers = HashMap::with_capacity(config.committee_size);

for id in 0..committee_size {
for id in 0..config.committee_size {
// Creates a new instance
// 0 config.id = 0
config.operator_id = OperatorId::from(id);
let (sender, receiver, instance) = Qbft::new(config.clone());
senders.insert(config.operator_id, sender);
receivers.insert(config.operator_id, receiver);

// spawn the instance
// TODO: Make the round time adjustable, to get deterministic results for testing.
debug!(id, "Starting instance");
tokio::spawn(instance.start_instance());
}
Expand Down Expand Up @@ -278,7 +277,7 @@ fn emulate_broadcast_network<D: Default + Debug + Clone + Send + Sync + 'static
.for_each(|(current_operator_id, sender)| {
if current_operator_id != operator_id {
let _ = sender.send(InMessage::Propose(
current_operator_id.clone(),
operator_id.clone(),
consensus_data.clone(),
));
}
Expand All @@ -290,7 +289,7 @@ fn emulate_broadcast_network<D: Default + Debug + Clone + Send + Sync + 'static
.for_each(|(current_operator_id, sender)| {
if current_operator_id != operator_id {
let _ = sender.send(InMessage::Prepare(
current_operator_id.clone(),
operator_id.clone(),
prepare_message.clone(),
));
}
Expand All @@ -303,7 +302,7 @@ fn emulate_broadcast_network<D: Default + Debug + Clone + Send + Sync + 'static
.for_each(|(current_operator_id, sender)| {
if current_operator_id != operator_id {
let _ = sender.send(InMessage::Commit(
current_operator_id.clone(),
operator_id.clone(),
commit_message.clone(),
));
}
Expand All @@ -315,7 +314,7 @@ fn emulate_broadcast_network<D: Default + Debug + Clone + Send + Sync + 'static
.for_each(|(current_operator_id, sender)| {
if current_operator_id != operator_id {
let _ = sender.send(InMessage::RoundChange(
current_operator_id.clone(),
operator_id.clone(),
round.clone(),
optional_data.clone(),
));
Expand Down Expand Up @@ -381,11 +380,11 @@ where
));

while let Some((operator_id, out_message)) = grouped_receivers.next().await {
/*debug!(
debug!(
?out_message,
"Instance" = index,
operator = *operator_id,
"Handling message from instance"
);*/
);
// Custom handling of the out message
message_handling(out_message, &operator_id, &mut senders, &mut new_senders);
// Add back a new future to await for the next message
Expand Down

0 comments on commit 55587b3

Please sign in to comment.