Skip to content

Commit ebe6de8

Browse files
committed
feat: generate real workers to proptest, test module chores
1 parent 24d0593 commit ebe6de8

File tree

4 files changed

+76
-12
lines changed

4 files changed

+76
-12
lines changed

node/narwhal/src/gateway.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -789,11 +789,19 @@ impl<N: Network> Gateway<N> {
789789
}
790790
}
791791

792-
mod tests {
792+
#[cfg(test)]
793+
pub mod gateway_tests {
793794
use crate::{
794-
helpers::tests::{CommitteeInput, Validator},
795+
helpers::{
796+
committee_tests::{CommitteeInput, Validator},
797+
init_worker_channels,
798+
storage_tests::StorageInput,
799+
WorkerSender,
800+
},
795801
Gateway,
802+
Worker,
796803
MAX_COMMITTEE_SIZE,
804+
MAX_WORKERS,
797805
MEMORY_POOL_PORT,
798806
};
799807
use indexmap::IndexMap;
@@ -808,12 +816,15 @@ mod tests {
808816

809817
type N = Testnet3;
810818

811-
#[derive(Arbitrary, Debug)]
812-
struct GatewayInput {
819+
#[derive(Arbitrary, Debug, Clone)]
820+
pub struct GatewayInput {
813821
#[filter(CommitteeInput::is_valid)]
814-
committee_input: CommitteeInput,
815-
node_validator: Validator,
816-
dev: Option<u8>,
822+
pub committee_input: CommitteeInput,
823+
pub node_validator: Validator,
824+
pub dev: Option<u8>,
825+
#[strategy(0..MAX_WORKERS)]
826+
pub workers_count: u8,
827+
pub worker_storage: StorageInput,
817828
}
818829

819830
impl GatewayInput {
@@ -826,6 +837,30 @@ mod tests {
826837
};
827838
Gateway::new(Arc::new(RwLock::new(committee)), account, dev).unwrap()
828839
}
840+
841+
pub async fn generate_workers(
842+
&self,
843+
gateway: &Gateway<N>,
844+
) -> (IndexMap<u8, Worker<N>>, IndexMap<u8, WorkerSender<N>>) {
845+
// Construct a map of the worker senders.
846+
let mut tx_workers = IndexMap::new();
847+
let mut workers = IndexMap::new();
848+
849+
// Initialize the workers.
850+
for id in 0..self.workers_count {
851+
// Construct the worker channels.
852+
let (tx_worker, rx_worker) = init_worker_channels();
853+
// Construct the worker instance.
854+
let mut worker = Worker::new(id, gateway.clone(), self.worker_storage.to_storage()).unwrap();
855+
// Run the worker instance.
856+
worker.run(rx_worker).await.unwrap();
857+
858+
// Add the worker and the worker sender to maps
859+
workers.insert(id, worker);
860+
tx_workers.insert(id, tx_worker);
861+
}
862+
(workers, tx_workers)
863+
}
829864
}
830865

831866
#[proptest]
@@ -857,18 +892,20 @@ mod tests {
857892
#[proptest(async = "tokio")]
858893
async fn gateway_start(#[filter(|x| x.dev.is_some())] input: GatewayInput) {
859894
let Some(dev) = input.dev else { unreachable!() };
895+
println!("{input:?}");
860896
let mut gateway = input.to_gateway();
861897
let tcp_config = gateway.tcp().config();
862898
assert_eq!(tcp_config.listener_ip, Some(IpAddr::V4(Ipv4Addr::LOCALHOST)));
863899
assert_eq!(tcp_config.desired_listening_port, Some(MEMORY_POOL_PORT + (dev as u16)));
864900

865-
match gateway.run(IndexMap::new()).await {
901+
let (workers, worker_senders) = input.generate_workers(&gateway).await;
902+
match gateway.run(worker_senders).await {
866903
Ok(_) => {
867904
assert_eq!(
868905
gateway.local_ip(),
869906
SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), MEMORY_POOL_PORT + (dev as u16))
870907
);
871-
assert_eq!(gateway.num_workers(), 0);
908+
assert_eq!(gateway.num_workers(), workers.len() as u8);
872909
}
873910
Err(err) => {
874911
panic!("Shouldn't fail because of {err}");

node/narwhal/src/helpers/committee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<N: Network> Committee<N> {
103103
}
104104

105105
#[cfg(test)]
106-
pub mod tests {
106+
pub mod committee_tests {
107107
use crate::helpers::Committee;
108108
use anyhow::Result;
109109
use indexmap::IndexMap;
@@ -115,7 +115,7 @@ pub mod tests {
115115

116116
type N = Testnet3;
117117

118-
#[derive(Arbitrary, Debug)]
118+
#[derive(Arbitrary, Debug, Clone)]
119119
pub struct CommitteeInput {
120120
#[strategy(0u64..)]
121121
pub round: u64,

node/narwhal/src/helpers/storage.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,12 @@ impl<N: Network> Storage<N> {
329329
}
330330

331331
#[cfg(test)]
332-
mod tests {
332+
pub mod storage_tests {
333333
use super::*;
334334
use snarkvm::prelude::TestRng;
335335

336336
use indexmap::indexset;
337+
use test_strategy::Arbitrary;
337338

338339
type CurrentNetwork = snarkvm::prelude::Testnet3;
339340

@@ -418,4 +419,15 @@ mod tests {
418419
// Ensure the certificate is no longer stored in the round.
419420
assert!(storage.get_certificates_for_round(round).is_empty());
420421
}
422+
423+
#[derive(Arbitrary, Debug, Clone)]
424+
pub struct StorageInput {
425+
pub gc_rounds: u64,
426+
}
427+
428+
impl StorageInput {
429+
pub fn to_storage(&self) -> Storage<CurrentNetwork> {
430+
Storage::new(self.gc_rounds)
431+
}
432+
}
421433
}

node/narwhal/src/worker.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,18 @@ impl<N: Network> Worker<N> {
289289
self.handles.lock().iter().for_each(|handle| handle.abort());
290290
}
291291
}
292+
293+
#[cfg(test)]
294+
mod worker_tests {
295+
use crate::{gateway_tests::GatewayInput, helpers::storage_tests::StorageInput, Gateway, Worker, MAX_WORKERS};
296+
use snarkvm::prelude::Testnet3;
297+
use test_strategy::Arbitrary;
298+
299+
type N = Testnet3;
300+
301+
#[derive(Arbitrary, Debug, Clone)]
302+
pub struct WorkerInput {
303+
pub id: u8,
304+
pub storage: StorageInput,
305+
}
306+
}

0 commit comments

Comments
 (0)