From 80b78edeb33d629de89c152ceb7a23ac9e500935 Mon Sep 17 00:00:00 2001 From: Niklas Date: Mon, 17 Jul 2023 17:50:47 -0700 Subject: [PATCH] tests: adapt tooling to work with the BFT --- node/narwhal/tests/bft_e2e.rs | 36 +++++++++++++++++ node/narwhal/tests/common/primary.rs | 40 +++++++++++++------ node/narwhal/tests/{e2e.rs => narwhal_e2e.rs} | 9 +++-- 3 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 node/narwhal/tests/bft_e2e.rs rename node/narwhal/tests/{e2e.rs => narwhal_e2e.rs} (97%) diff --git a/node/narwhal/tests/bft_e2e.rs b/node/narwhal/tests/bft_e2e.rs new file mode 100644 index 0000000000..83a4979cbd --- /dev/null +++ b/node/narwhal/tests/bft_e2e.rs @@ -0,0 +1,36 @@ +// Copyright (C) 2019-2023 Aleo Systems Inc. +// This file is part of the snarkOS library. + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +mod common; + +use crate::common::primary::{TestNetwork, TestNetworkConfig}; + +#[tokio::test(flavor = "multi_thread")] +#[ignore = "long-running e2e test"] +async fn test_state_coherence() { + const N: u16 = 4; + let mut network = TestNetwork::new(TestNetworkConfig { + num_nodes: N, + bft: true, + connect_all: true, + fire_cannons: true, + // Set this to Some(0..=4) to see the logs. + log_level: Some(0), + log_connections: true, + }); + + network.start().await; + + std::future::pending::<()>().await; +} diff --git a/node/narwhal/tests/common/primary.rs b/node/narwhal/tests/common/primary.rs index d54130182c..a0d42d8cb8 100644 --- a/node/narwhal/tests/common/primary.rs +++ b/node/narwhal/tests/common/primary.rs @@ -21,6 +21,7 @@ use snarkos_account::Account; use snarkos_node_narwhal::{ helpers::{init_primary_channels, PrimarySender, Storage}, Primary, + BFT, MAX_BATCH_DELAY, MAX_GC_ROUNDS, }; @@ -38,6 +39,7 @@ use tracing::*; #[derive(Clone, Copy, Debug)] pub struct TestNetworkConfig { pub num_nodes: u16, + pub bft: bool, pub connect_all: bool, pub fire_cannons: bool, pub log_level: Option, @@ -54,14 +56,15 @@ pub struct TestNetwork { pub struct TestValidator { pub id: u16, pub primary: Primary, - pub sender: Option>, + pub primary_sender: Option>, + pub bft: Option>, pub handles: Arc>>>, } impl TestValidator { pub fn fire_cannons(&mut self) { - let solution_handle = fire_unconfirmed_solutions(self.sender.as_mut().unwrap(), self.id); - let transaction_handle = fire_unconfirmed_transactions(self.sender.as_mut().unwrap(), self.id); + let solution_handle = fire_unconfirmed_solutions(self.primary_sender.as_mut().unwrap(), self.id); + let transaction_handle = fire_unconfirmed_transactions(self.primary_sender.as_mut().unwrap(), self.id); self.handles.lock().push(solution_handle); self.handles.lock().push(transaction_handle); @@ -76,7 +79,7 @@ impl TestValidator { for connection in connections { debug!(" {}", connection); } - sleep(Duration::from_secs(10)).await; + sleep(Duration::from_secs(5)).await; } })); } @@ -95,9 +98,17 @@ impl TestNetwork { for (id, account) in accounts.into_iter().enumerate() { let storage = Storage::new(committee.clone(), MAX_GC_ROUNDS); let ledger = Arc::new(MockLedgerService::new()); - let primary = Primary::::new(account, storage, ledger, None, Some(id as u16)).unwrap(); - let test_validator = TestValidator { id: id as u16, primary, sender: None, handles: Default::default() }; + let (primary, bft) = if config.bft { + let bft = BFT::::new(account, storage, ledger, None, Some(id as u16)).unwrap(); + (bft.primary().clone(), Some(bft)) + } else { + let primary = Primary::::new(account, storage, ledger, None, Some(id as u16)).unwrap(); + (primary, None) + }; + + let test_validator = + TestValidator { id: id as u16, primary, primary_sender: None, bft, handles: Default::default() }; validators.insert(id as u16, test_validator); } @@ -107,10 +118,16 @@ impl TestNetwork { // Starts each node in the network. pub async fn start(&mut self) { for validator in self.validators.values_mut() { - // Setup the channels and start the primary. - let (sender, receiver) = init_primary_channels(); - validator.sender = Some(sender.clone()); - validator.primary.run(sender.clone(), receiver, None).await.unwrap(); + let (primary_sender, primary_receiver) = init_primary_channels(); + if let Some(bft) = &mut validator.bft { + // Setup the channels and start the bft. + validator.primary_sender = Some(primary_sender.clone()); + bft.run(primary_sender, primary_receiver, None).await.unwrap(); + } else { + // Setup the channels and start the primary. + validator.primary_sender = Some(primary_sender.clone()); + validator.primary.run(primary_sender, primary_receiver, None).await.unwrap(); + } if self.config.fire_cannons { validator.fire_cannons(); @@ -147,7 +164,7 @@ impl TestNetwork { let ip = other_validator.primary.gateway().local_ip(); validator.primary.gateway().connect(ip); // Give the connection time to be established. - tokio::time::sleep(std::time::Duration::from_millis(100)).await; + tokio::time::sleep(std::time::Duration::from_millis(10)).await; } } @@ -187,7 +204,6 @@ fn new_test_committee(n: u16) -> (Vec>, Committee