Skip to content

Commit

Permalink
Fix/re-enable memory network tests (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-maron authored Sep 29, 2023
1 parent 7c48e20 commit 67be089
Show file tree
Hide file tree
Showing 2 changed files with 368 additions and 356 deletions.
356 changes: 0 additions & 356 deletions crates/hotshot/src/traits/networking/memory_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,359 +574,3 @@ impl<TYPES: NodeType, I: NodeImplementation<TYPES>, MEMBERSHIP: Membership<TYPES
Box::new(move |network| MemoryCommChannel::new(network))
}
}

#[cfg(test)]
// panic in tests
#[allow(clippy::panic)]
mod tests {
// use super::*;
// use crate::{
// demos::vdemo::{Addition, Subtraction, VDemoBlock, VDemoState, VDemoTransaction},
// traits::election::static_committee::{
// GeneralStaticCommittee, StaticElectionConfig, StaticVoteToken,
// },
// };
//
// use crate::traits::implementations::MemoryStorage;
// use async_compatibility_layer::logging::setup_logging;
// use hotshot_types::traits::election::QuorumExchange;
// use hotshot_types::traits::node_implementation::{ChannelMaps, ValidatingExchanges};
// use hotshot_types::{
// data::ViewNumber,
// message::{DataMessage, MessageKind, ValidatingMessage},
// traits::{
// signature_key::ed25519::{Ed25519Priv, Ed25519Pub},
// state::ConsensusTime,
// },
// vote::QuorumVote,
// };
// use hotshot_types::{
// data::{ValidatingLeaf, ValidatingProposal},
// traits::consensus_type::validating_consensus::ValidatingConsensus,
// };
// use serde::{Deserialize, Serialize};
//
// #[derive(
// Copy,
// Clone,
// Debug,
// Default,
// Hash,
// PartialEq,
// Eq,
// PartialOrd,
// Ord,
// serde::Serialize,
// serde::Deserialize,
// )]
// struct Test {}
// #[derive(Clone, Debug, Deserialize, Serialize, Hash, Eq, PartialEq)]
// struct TestImpl {}
//
// // impl NetworkMsg for Test {}
//
// impl NodeType for Test {
// // TODO (da) can this be SequencingConsensus?
// type ConsensusType = ValidatingConsensus;
// type Time = ViewNumber;
// type BlockType = VDemoBlock;
// type SignatureKey = Ed25519Pub;
// type VoteTokenType = StaticVoteToken<Ed25519Pub>;
// type Transaction = VDemoTransaction;
// type ElectionConfigType = StaticElectionConfig;
// type StateType = VDemoState;
// }
//
// type TestMembership = GeneralStaticCommittee<Test, TestLeaf, Ed25519Pub>;
// type TestNetwork = MemoryCommChannel<Test, TestImpl, TestProposal, TestVote, TestMembership>;
//
// impl NodeImplementation<Test> for TestImpl {
// type ConsensusMessage = ValidatingMessage<Test, Self>;
// type Exchanges = ValidatingExchanges<
// Test,
// Message<Test, Self>,
// QuorumExchange<
// Test,
// TestLeaf,
// TestProposal,
// TestMembership,
// TestNetwork,
// Message<Test, Self>,
// >,
// ViewSyncExchange<Test, TestProposal, TestMembership, TestNetwork, Message<Test, Self>>,
// >;
// type Leaf = TestLeaf;
// type Storage = MemoryStorage<Test, TestLeaf>;
//
// fn new_channel_maps(
// start_view: ViewNumber,
// ) -> (ChannelMaps<Test, Self>, Option<ChannelMaps<Test, Self>>) {
// (ChannelMaps::new(start_view), None)
// }
// }
//
// type TestLeaf = ValidatingLeaf<Test>;
// type TestVote = QuorumVote<Test, TestLeaf>;
// type TestProposal = ValidatingProposal<Test, TestLeaf>;
//
// /// fake Eq
// /// we can't compare the votetokentype for equality, so we can't
// /// derive EQ on `VoteType<TYPES>` and thereby message
// /// we are only sending data messages, though so we compare key and
// /// data message
// fn fake_message_eq(message_1: Message<Test, TestImpl>, message_2: Message<Test, TestImpl>) {
// assert_eq!(message_1.sender, message_2.sender);
// if let MessageKind::Data(DataMessage::SubmitTransaction(d_1, _)) = message_1.kind {
// if let MessageKind::Data(DataMessage::SubmitTransaction(d_2, _)) = message_2.kind {
// assert_eq!(d_1, d_2);
// }
// } else {
// panic!("Got unexpected message type in memory test!");
// }
// }
//
// #[instrument]
// fn get_pubkey() -> Ed25519Pub {
// let priv_key = Ed25519Priv::generate();
// Ed25519Pub::from_private(&priv_key)
// }
//
// /// create a message
// fn gen_messages(num_messages: u64, seed: u64, pk: Ed25519Pub) -> Vec<Message<Test, TestImpl>> {
// let mut messages = Vec::new();
// for i in 0..num_messages {
// let message = Message {
// sender: pk,
// kind: MessageKind::Data(DataMessage::SubmitTransaction(
// VDemoTransaction {
// add: Addition {
// account: "A".to_string(),
// amount: 50 + i + seed,
// },
// sub: Subtraction {
// account: "B".to_string(),
// amount: 50 + i + seed,
// },
// nonce: seed + i,
// padding: vec![50; 0],
// },
// <ViewNumber as ConsensusTime>::new(0),
// )),
// _phantom: PhantomData,
// };
// messages.push(message);
// }
// messages
// }
//
// // Spawning a single MemoryNetwork should produce no errors
// #[cfg_attr(
// feature = "tokio-executor",
// tokio::test(flavor = "multi_thread", worker_threads = 2)
// )]
// #[cfg_attr(feature = "async-std-executor", async_std::test)]
// #[instrument]
// async fn spawn_single() {
// setup_logging();
// let group: Arc<MasterMap<Message<Test, TestImpl>, <Test as NodeType>::SignatureKey>> =
// MasterMap::new();
// trace!(?group);
// let pub_key = get_pubkey();
// let _network = MemoryNetwork::new(pub_key, NoMetrics::boxed(), group, Option::None);
// }
//
// // // Spawning a two MemoryNetworks and connecting them should produce no errors
// #[cfg_attr(
// feature = "tokio-executor",
// tokio::test(flavor = "multi_thread", worker_threads = 2)
// )]
// #[cfg_attr(feature = "async-std-executor", async_std::test)]
// #[instrument]
// async fn spawn_double() {
// setup_logging();
// let group: Arc<MasterMap<Message<Test, TestImpl>, <Test as NodeType>::SignatureKey>> =
// MasterMap::new();
// trace!(?group);
// let pub_key_1 = get_pubkey();
// let _network_1 =
// MemoryNetwork::new(pub_key_1, NoMetrics::boxed(), group.clone(), Option::None);
// let pub_key_2 = get_pubkey();
// let _network_2 = MemoryNetwork::new(pub_key_2, NoMetrics::boxed(), group, Option::None);
// }
//
// // Check to make sure direct queue works
// #[cfg_attr(
// feature = "tokio-executor",
// tokio::test(flavor = "multi_thread", worker_threads = 2)
// )]
// #[cfg_attr(feature = "async-std-executor", async_std::test)]
// #[allow(deprecated)]
// #[instrument]
// async fn direct_queue() {
// setup_logging();
// // Create some dummy messages
//
// // Make and connect the networking instances
// let group: Arc<MasterMap<Message<Test, TestImpl>, <Test as NodeType>::SignatureKey>> =
// MasterMap::new();
// trace!(?group);
// let pub_key_1 = get_pubkey();
// let network1 =
// MemoryNetwork::new(pub_key_1, NoMetrics::boxed(), group.clone(), Option::None);
// let pub_key_2 = get_pubkey();
// let network2 = MemoryNetwork::new(pub_key_2, NoMetrics::boxed(), group, Option::None);
//
// let first_messages: Vec<Message<Test, TestImpl>> = gen_messages(5, 100, pub_key_1);
//
// // Test 1 -> 2
// // Send messages
// for sent_message in first_messages {
// network1
// .direct_message(sent_message.clone(), pub_key_2)
// .await
// .expect("Failed to message node");
// let mut recv_messages = network2
// .recv_msgs(TransmitType::Direct)
// .await
// .expect("Failed to receive message");
// let recv_message = recv_messages.pop().unwrap();
// assert!(recv_messages.is_empty());
// fake_message_eq(sent_message, recv_message);
// }
//
// let second_messages: Vec<Message<Test, TestImpl>> = gen_messages(5, 200, pub_key_2);
//
// // Test 2 -> 1
// // Send messages
// for sent_message in second_messages {
// network2
// .direct_message(sent_message.clone(), pub_key_1)
// .await
// .expect("Failed to message node");
// let mut recv_messages = network1
// .recv_msgs(TransmitType::Direct)
// .await
// .expect("Failed to receive message");
// let recv_message = recv_messages.pop().unwrap();
// assert!(recv_messages.is_empty());
// fake_message_eq(sent_message, recv_message);
// }
// }
//
// // Check to make sure direct queue works
// #[cfg_attr(
// feature = "tokio-executor",
// tokio::test(flavor = "multi_thread", worker_threads = 2)
// )]
// #[cfg_attr(feature = "async-std-executor", async_std::test)]
// #[allow(deprecated)]
// #[instrument]
// async fn broadcast_queue() {
// setup_logging();
// // Make and connect the networking instances
// let group: Arc<MasterMap<Message<Test, TestImpl>, <Test as NodeType>::SignatureKey>> =
// MasterMap::new();
// trace!(?group);
// let pub_key_1 = get_pubkey();
// let network1 =
// MemoryNetwork::new(pub_key_1, NoMetrics::boxed(), group.clone(), Option::None);
// let pub_key_2 = get_pubkey();
// let network2 = MemoryNetwork::new(pub_key_2, NoMetrics::boxed(), group, Option::None);
//
// let first_messages: Vec<Message<Test, TestImpl>> = gen_messages(5, 100, pub_key_1);
//
// // Test 1 -> 2
// // Send messages
// for sent_message in first_messages {
// network1
// .broadcast_message(
// sent_message.clone(),
// vec![pub_key_2].into_iter().collect::<BTreeSet<_>>(),
// )
// .await
// .expect("Failed to message node");
// let mut recv_messages = network2
// .recv_msgs(TransmitType::Broadcast)
// .await
// .expect("Failed to receive message");
// let recv_message = recv_messages.pop().unwrap();
// assert!(recv_messages.is_empty());
// fake_message_eq(sent_message, recv_message);
// }
//
// let second_messages: Vec<Message<Test, TestImpl>> = gen_messages(5, 200, pub_key_2);
//
// // Test 2 -> 1
// // Send messages
// for sent_message in second_messages {
// network2
// .broadcast_message(
// sent_message.clone(),
// vec![pub_key_1].into_iter().collect::<BTreeSet<_>>(),
// )
// .await
// .expect("Failed to message node");
// let mut recv_messages = network1
// .recv_msgs(TransmitType::Broadcast)
// .await
// .expect("Failed to receive message");
// let recv_message = recv_messages.pop().unwrap();
// assert!(recv_messages.is_empty());
// fake_message_eq(sent_message, recv_message);
// }
// }
//
// #[cfg_attr(
// feature = "tokio-executor",
// tokio::test(flavor = "multi_thread", worker_threads = 2)
// )]
// #[cfg_attr(feature = "async-std-executor", async_std::test)]
// #[instrument]
// #[allow(deprecated)]
// async fn test_in_flight_message_count() {
// // setup_logging();
//
// // let group: Arc<MasterMap<Message<Test, TestImpl>, <Test as NodeType>::SignatureKey>> =
// // MasterMap::new();
// // trace!(?group);
// // let pub_key_1 = get_pubkey();
// // let network1 =
// // MemoryNetwork::new(pub_key_1, NoMetrics::boxed(), group.clone(), Option::None);
// // let pub_key_2 = get_pubkey();
// // let network2 = MemoryNetwork::new(pub_key_2, NoMetrics::boxed(), group, Option::None);
//
// // // Create some dummy messages
// // let messages: Vec<Message<Test, TestImpl>> = gen_messages(5, 100, pub_key_1);
//
// // // assert_eq!(network1.in_flight_message_count(), Some(0));
// // // assert_eq!(network2.in_flight_message_count(), Some(0));
//
// // for (_count, message) in messages.iter().enumerate() {
// // network1
// // .direct_message(message.clone(), pub_key_2)
// // .await
// // .unwrap();
// // // network 2 has received `count` broadcast messages and `count + 1` direct messages
// // // assert_eq!(network2.in_flight_message_count(), Some(count + count + 1));
//
// // // network2.broadcast_message(message.clone()).await.unwrap();
// // // network 1 has received `count` broadcast messages
// // // assert_eq!(network1.in_flight_message_count(), Some(count + 1));
//
// // // network 2 has received `count + 1` broadcast messages and `count + 1` direct messages
// // // assert_eq!(network2.in_flight_message_count(), Some((count + 1) * 2));
// // }
//
// // for _count in (0..messages.len()).rev() {
// // network1.recv_msgs(TransmitType::Broadcast).await.unwrap();
// // // assert_eq!(network1.in_flight_message_count(), Some(count));
//
// // network2.recv_msgs(TransmitType::Broadcast).await.unwrap();
// // network2.recv_msgs(TransmitType::Direct).await.unwrap();
// // // assert_eq!(network2.in_flight_message_count(), Some(count * 2));
// // }
//
// // // assert_eq!(network1.in_flight_message_count(), Some(0));
// // // assert_eq!(network2.in_flight_message_count(), Some(0));
// }
}
Loading

0 comments on commit 67be089

Please sign in to comment.