From 351fde1090171e9ab517183799fdfdfd44427225 Mon Sep 17 00:00:00 2001 From: gterzian <2792687+gterzian@users.noreply.github.com> Date: Mon, 21 Mar 2022 17:25:00 +0800 Subject: [PATCH] storage: move store to after protocol check --- .../src/tests/mock_protocol_controller.rs | 4 +- massa-network-exports/src/commands.rs | 5 +- massa-network-worker/src/network_event.rs | 12 ++-- massa-network-worker/src/network_worker.rs | 4 +- massa-network-worker/src/node_worker.rs | 10 +-- .../src/tests/mock_network_controller.rs | 18 ++---- massa-protocol-exports/src/tests/tools.rs | 8 +-- massa-protocol-worker/src/protocol_worker.rs | 29 ++++----- .../src/tests/ask_block_scenarios.rs | 16 +++-- .../src/tests/ban_nodes_scenarios.rs | 17 ++--- .../src/tests/cache_scenarios.rs | 18 ++---- .../src/tests/endorsements_scenarios.rs | 17 ++--- .../tests/in_block_operations_scenarios.rs | 13 +--- .../src/tests/operations_scenarios.rs | 44 ++++--------- massa-protocol-worker/src/tests/scenarios.rs | 21 +++---- massa-protocol-worker/src/tests/tools.rs | 62 ------------------- 16 files changed, 82 insertions(+), 216 deletions(-) diff --git a/massa-consensus-worker/src/tests/mock_protocol_controller.rs b/massa-consensus-worker/src/tests/mock_protocol_controller.rs index 4e03fd2292d..ddfbdb58aa1 100644 --- a/massa-consensus-worker/src/tests/mock_protocol_controller.rs +++ b/massa-consensus-worker/src/tests/mock_protocol_controller.rs @@ -1,8 +1,8 @@ // Copyright (c) 2022 MASSA LABS use massa_models::{ - constants::CHANNEL_SIZE, signed::Signable, storage::Storage, Block, BlockHeader, BlockId, - SerializeCompact, SignedHeader, + constants::CHANNEL_SIZE, signed::Signable, storage::Storage, Block, BlockId, SerializeCompact, + SignedHeader, }; use massa_protocol_exports::{ ProtocolCommand, ProtocolCommandSender, ProtocolEvent, ProtocolEventReceiver, diff --git a/massa-network-exports/src/commands.rs b/massa-network-exports/src/commands.rs index bd97393067e..e8a2ae90e79 100644 --- a/massa-network-exports/src/commands.rs +++ b/massa-network-exports/src/commands.rs @@ -4,7 +4,7 @@ use crate::{BootstrapPeers, Peers}; use massa_models::SignedEndorsement; use massa_models::SignedHeader; use massa_models::SignedOperation; -use massa_models::{composite::PubkeySig, node::NodeId, stats::NetworkStats, BlockId}; +use massa_models::{composite::PubkeySig, node::NodeId, stats::NetworkStats, Block, BlockId}; use tokio::sync::oneshot; /// Commands that the worker can execute @@ -58,7 +58,8 @@ pub enum NetworkEvent { /// A block was received ReceivedBlock { node: NodeId, - block_id: BlockId, + block: Block, + serialized: Vec, }, /// A block header was received ReceivedBlockHeader { diff --git a/massa-network-worker/src/network_event.rs b/massa-network-worker/src/network_event.rs index 9d237b244d4..5fd345804ce 100644 --- a/massa-network-worker/src/network_event.rs +++ b/massa-network-worker/src/network_event.rs @@ -80,7 +80,9 @@ impl EventSender { pub mod event_impl { use crate::{network_worker::NetworkWorker, node_worker::NodeCommand}; use massa_logging::massa_trace; - use massa_models::{node::NodeId, BlockId, SignedEndorsement, SignedHeader, SignedOperation}; + use massa_models::{ + node::NodeId, Block, BlockId, SignedEndorsement, SignedHeader, SignedOperation, + }; use massa_network_exports::{NetworkError, NetworkEvent}; use std::net::IpAddr; use tracing::{debug, info}; @@ -108,17 +110,19 @@ pub mod event_impl { pub async fn on_received_block( worker: &mut NetworkWorker, from: NodeId, - block_id: BlockId, + block: Block, + serialized: Vec, ) -> Result<(), NetworkError> { massa_trace!( "network_worker.on_node_event receive NetworkEvent::ReceivedBlock", - {"block_id": block_id, "node": from} + {"block": block, "node": from} ); if let Err(err) = worker .event .send(NetworkEvent::ReceivedBlock { node: from, - block_id, + block, + serialized, }) .await { diff --git a/massa-network-worker/src/network_worker.rs b/massa-network-worker/src/network_worker.rs index f872a8e4b4c..14c0a07ceb1 100644 --- a/massa-network-worker/src/network_worker.rs +++ b/massa-network-worker/src/network_worker.rs @@ -748,8 +748,8 @@ impl NetworkWorker { NodeEvent(from_node_id, NodeEventType::ReceivedPeerList(lst)) => { event_impl::on_received_peer_list(self, from_node_id, &lst)? } - NodeEvent(from_node_id, NodeEventType::ReceivedBlock(data)) => { - event_impl::on_received_block(self, from_node_id, data).await? + NodeEvent(from_node_id, NodeEventType::ReceivedBlock(block, serialized)) => { + event_impl::on_received_block(self, from_node_id, block, serialized).await? } NodeEvent(from_node_id, NodeEventType::ReceivedAskForBlocks(list)) => { event_impl::on_received_ask_for_blocks(self, from_node_id, list).await diff --git a/massa-network-worker/src/node_worker.rs b/massa-network-worker/src/node_worker.rs index cb4bdd41d5a..944454883dd 100644 --- a/massa-network-worker/src/node_worker.rs +++ b/massa-network-worker/src/node_worker.rs @@ -14,7 +14,7 @@ use massa_models::{ }, node::NodeId, signed::Signable, - BlockId, SignedEndorsement, SignedHeader, SignedOperation, + Block, BlockId, SignedEndorsement, SignedHeader, SignedOperation, }; use massa_models::{SerializeCompact, SerializeVarInt}; use massa_network_exports::{ConnectionClosureReason, NetworkError, NetworkSettings}; @@ -66,7 +66,7 @@ pub enum NodeEventType { /// Node we are connected to sent peer list ReceivedPeerList(Vec), /// Node we are connected to sent block - ReceivedBlock(BlockId), + ReceivedBlock(Block, Vec), /// Node we are connected to sent block header ReceivedBlockHeader(SignedHeader), /// Node we are connected to asks for a block. @@ -309,11 +309,7 @@ impl NodeWorker { "node_worker.run_loop. receive Message::Block", {"block_id": block.header.content.compute_id()?, "block": block, "node": self.node_id} ); - - // TODO: avoid computing id. - let block_id = block.header.content.compute_id()?; - self.storage.store_block(block_id, block.clone(), serialized.unwrap()); - self.send_node_event(NodeEvent(self.node_id, NodeEventType::ReceivedBlock(block_id))).await; + self.send_node_event(NodeEvent(self.node_id, NodeEventType::ReceivedBlock(block, serialized.expect("Block should come wiht its serialized form.")))).await; }, Message::BlockHeader(header) => { massa_trace!( diff --git a/massa-protocol-exports/src/tests/mock_network_controller.rs b/massa-protocol-exports/src/tests/mock_network_controller.rs index afb6c803d01..5160b0b90e1 100644 --- a/massa-protocol-exports/src/tests/mock_network_controller.rs +++ b/massa-protocol-exports/src/tests/mock_network_controller.rs @@ -1,10 +1,7 @@ // Copyright (c) 2022 MASSA LABS -use massa_models::SerializeCompact; use massa_models::{constants::CHANNEL_SIZE, node::NodeId}; -use massa_models::{ - storage::Storage, Block, BlockId, SignedEndorsement, SignedHeader, SignedOperation, -}; +use massa_models::{Block, BlockId, SignedEndorsement, SignedHeader, SignedOperation}; use massa_network_exports::{ NetworkCommand, NetworkCommandSender, NetworkEvent, NetworkEventReceiver, }; @@ -72,19 +69,12 @@ impl MockNetworkController { .expect("Couldn't send header to protocol."); } - pub async fn send_block( - &mut self, - source_node_id: NodeId, - block_id: BlockId, - block_and_storage: Option<(Block, Storage)>, - ) { - if let Some((block, storage)) = block_and_storage { - storage.store_block(block_id, block.clone(), block.to_bytes_compact().unwrap()); - } + pub async fn send_block(&mut self, source_node_id: NodeId, block: Block, serialized: Vec) { self.network_event_tx .send(NetworkEvent::ReceivedBlock { node: source_node_id, - block_id, + block, + serialized, }) .await .expect("Couldn't send block to protocol."); diff --git a/massa-protocol-exports/src/tests/tools.rs b/massa-protocol-exports/src/tests/tools.rs index 16bf9b7c8cd..9f782333b95 100644 --- a/massa-protocol-exports/src/tests/tools.rs +++ b/massa-protocol-exports/src/tests/tools.rs @@ -8,9 +8,9 @@ use crate::{ use massa_hash::hash::Hash; use massa_models::node::NodeId; use massa_models::signed::{Signable, Signed}; +use massa_models::SerializeCompact; use massa_models::{ - storage::Storage, Address, Amount, Block, BlockHeader, BlockId, SignedEndorsement, - SignedOperation, Slot, + Address, Amount, Block, BlockHeader, BlockId, SignedEndorsement, SignedOperation, Slot, }; use massa_models::{Endorsement, Operation, OperationType}; use massa_network_exports::NetworkCommand; @@ -132,13 +132,13 @@ pub async fn send_and_propagate_block( valid: bool, source_node_id: NodeId, protocol_event_receiver: &mut ProtocolEventReceiver, - storage: Option, ) { let expected_hash = block.header.content.compute_id().unwrap(); + let serialized = block.to_bytes_compact().unwrap(); // Send block to protocol. network_controller - .send_block(source_node_id, expected_hash, storage.map(|s| (block, s))) + .send_block(source_node_id, block, serialized) .await; // Check protocol sends block to consensus. diff --git a/massa-protocol-worker/src/protocol_worker.rs b/massa-protocol-worker/src/protocol_worker.rs index bc56dcbd429..11fe798ab14 100644 --- a/massa-protocol-worker/src/protocol_worker.rs +++ b/massa-protocol-worker/src/protocol_worker.rs @@ -9,8 +9,8 @@ use massa_models::{ node::NodeId, prehash::{BuildMap, Map, Set}, signed::Signable, - Address, BlockId, EndorsementId, OperationId, OperationType, SignedEndorsement, SignedHeader, - SignedOperation, + Address, Block, BlockId, EndorsementId, OperationId, OperationType, SignedEndorsement, + SignedHeader, SignedOperation, }; use massa_network_exports::{NetworkCommandSender, NetworkEvent, NetworkEventReceiver}; use massa_protocol_exports::{ @@ -1125,7 +1125,7 @@ impl ProtocolWorker { /// - Check root hash. async fn note_block_from_node( &mut self, - block: &BlockId, + block: &Block, source_node_id: &NodeId, ) -> Result< Option<( @@ -1138,13 +1138,11 @@ impl ProtocolWorker { massa_trace!("protocol.protocol_worker.note_block_from_node", { "node": source_node_id, "block": block }); let (header, operations, operation_merkle_root, slot) = { - let stored_block = self.storage.retrieve_block(block).unwrap(); - let stored_block = stored_block.read(); ( - stored_block.block.header.clone(), - stored_block.block.operations.clone(), - stored_block.block.header.content.operation_merkle_root, - stored_block.block.header.content.slot, + block.header.clone(), + block.operations.clone(), + block.header.content.operation_merkle_root, + block.header.content.slot, ) }; @@ -1390,14 +1388,17 @@ impl ProtocolWorker { } NetworkEvent::ReceivedBlock { node: from_node_id, - block_id, + block, + serialized, } => { - massa_trace!("protocol.protocol_worker.on_network_event.received_block", { "node": from_node_id, "block_id": block_id}); - - // TODO: remove clone of block. + massa_trace!("protocol.protocol_worker.on_network_event.received_block", { "node": from_node_id, "block": block}); if let Some((block_id, operation_set, endorsement_ids)) = - self.note_block_from_node(&block_id, &from_node_id).await? + self.note_block_from_node(&block, &from_node_id).await? { + // Store block in shared storage. + self.storage + .store_block(block_id, block.clone(), serialized); + let mut set = Set::::with_capacity_and_hasher(1, BuildMap::default()); set.insert(block_id); self.stop_asking_blocks(set)?; diff --git a/massa-protocol-worker/src/tests/ask_block_scenarios.rs b/massa-protocol-worker/src/tests/ask_block_scenarios.rs index 289377ecd56..ea77c086d32 100644 --- a/massa-protocol-worker/src/tests/ask_block_scenarios.rs +++ b/massa-protocol-worker/src/tests/ask_block_scenarios.rs @@ -1,6 +1,6 @@ // Copyright (c) 2022 MASSA LABS -use super::tools::{protocol_test, protocol_test_with_storage}; +use super::tools::protocol_test; use massa_models::prehash::Set; use massa_models::signed::Signable; use massa_models::BlockId; @@ -16,14 +16,13 @@ async fn test_without_a_priori() { // start let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, protocol_event_receiver, mut protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { let node_a = tools::create_and_connect_nodes(1, &mut network_controller) .await .pop() @@ -57,7 +56,7 @@ async fn test_without_a_priori() { // node B replied with the block network_controller - .send_block(node_b.id, hash_1, Some((block, storage))) + .send_block(node_b.id, block, Default::default()) .await; // 7. Make sure protocol did not send additional ask for block commands. @@ -91,14 +90,13 @@ async fn test_without_a_priori() { async fn test_someone_knows_it() { // start let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, mut protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { let node_a = tools::create_and_connect_nodes(1, &mut network_controller) .await .pop() @@ -140,7 +138,7 @@ async fn test_someone_knows_it() { // node C replied with the block network_controller - .send_block(node_c.id, hash_1, Some((block, storage))) + .send_block(node_c.id, block, Default::default()) .await; // 7. Make sure protocol did not send additional ask for block commands. diff --git a/massa-protocol-worker/src/tests/ban_nodes_scenarios.rs b/massa-protocol-worker/src/tests/ban_nodes_scenarios.rs index 751e6894267..d145274b95c 100644 --- a/massa-protocol-worker/src/tests/ban_nodes_scenarios.rs +++ b/massa-protocol-worker/src/tests/ban_nodes_scenarios.rs @@ -1,6 +1,6 @@ // Copyright (c) 2022 MASSA LABS -use super::tools::{protocol_test, protocol_test_with_storage}; +use super::tools::protocol_test; use massa_models::prehash::{Map, Set}; use massa_models::signed::Signable; use massa_models::{BlockId, Slot}; @@ -15,14 +15,13 @@ use std::time::Duration; #[serial] async fn test_protocol_bans_node_sending_block_with_invalid_signature() { let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { // Create 1 node. let mut nodes = tools::create_and_connect_nodes(1, &mut network_controller).await; @@ -36,15 +35,7 @@ async fn test_protocol_bans_node_sending_block_with_invalid_signature() { // 3. Send block to protocol. network_controller - .send_block( - creator_node.id, - block - .header - .content - .compute_id() - .expect("Fail to compute block id"), - Some((block, storage)), - ) + .send_block(creator_node.id, block, Default::default()) .await; // The node is banned. diff --git a/massa-protocol-worker/src/tests/cache_scenarios.rs b/massa-protocol-worker/src/tests/cache_scenarios.rs index 88c1e657567..d97d6810a4b 100644 --- a/massa-protocol-worker/src/tests/cache_scenarios.rs +++ b/massa-protocol-worker/src/tests/cache_scenarios.rs @@ -2,8 +2,7 @@ // RUST_BACKTRACE=1 cargo test test_one_handshake -- --nocapture --test-threads=1 -use super::tools::protocol_test_with_storage; -use massa_models::signed::Signable; +use super::tools::protocol_test; use massa_models::{self, Address, Slot}; use massa_protocol_exports::tests::tools; use massa_protocol_exports::ProtocolEvent; @@ -26,14 +25,13 @@ lazy_static::lazy_static! { async fn test_noting_block_does_not_panic_with_zero_max_node_known_blocks_size() { let protocol_settings = &CUSTOM_PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { // Create 1 node. let nodes = tools::create_and_connect_nodes(1, &mut network_controller).await; @@ -54,15 +52,7 @@ async fn test_noting_block_does_not_panic_with_zero_max_node_known_blocks_size() // and of its header, // does not panic. network_controller - .send_block( - nodes[0].id, - block - .header - .content - .compute_id() - .expect("Fail to compute block id"), - Some((block, storage)), - ) + .send_block(nodes[0].id, block, Default::default()) .await; // Wait for the event, should not panic. diff --git a/massa-protocol-worker/src/tests/endorsements_scenarios.rs b/massa-protocol-worker/src/tests/endorsements_scenarios.rs index 77a8a9a5c69..b9a240cedd3 100644 --- a/massa-protocol-worker/src/tests/endorsements_scenarios.rs +++ b/massa-protocol-worker/src/tests/endorsements_scenarios.rs @@ -2,7 +2,7 @@ // RUST_BACKTRACE=1 cargo test test_one_handshake -- --nocapture --test-threads=1 -use super::tools::{protocol_test, protocol_test_with_storage}; +use super::tools::protocol_test; use massa_models::prehash::Map; use massa_models::signed::Signable; use massa_models::{Address, Slot}; @@ -494,14 +494,13 @@ async fn test_protocol_propagates_endorsements_only_to_nodes_that_dont_know_abou async fn test_protocol_propagates_endorsements_only_to_nodes_that_dont_know_about_it_indirect_knowledge_via_header( ) { let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, mut protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { // Create 2 nodes. let nodes = tools::create_and_connect_nodes(2, &mut network_controller).await; @@ -521,15 +520,7 @@ async fn test_protocol_propagates_endorsements_only_to_nodes_that_dont_know_abou // Node 2 sends block, resulting in endorsements noted in block info. network_controller - .send_block( - nodes[1].id, - block - .header - .content - .compute_id() - .expect("Fail to get block id"), - Some((block.clone(), storage)), - ) + .send_block(nodes[1].id, block.clone(), Default::default()) .await; // Node 1 sends header, resulting in protocol using the block info to determine diff --git a/massa-protocol-worker/src/tests/in_block_operations_scenarios.rs b/massa-protocol-worker/src/tests/in_block_operations_scenarios.rs index 0322b1a1982..ca8360de87e 100644 --- a/massa-protocol-worker/src/tests/in_block_operations_scenarios.rs +++ b/massa-protocol-worker/src/tests/in_block_operations_scenarios.rs @@ -1,6 +1,6 @@ // Copyright (c) 2022 MASSA LABS -use super::tools::{protocol_test, protocol_test_with_storage}; +use super::tools::protocol_test; use massa_hash::hash::Hash; use massa_models::signed::Signed; use massa_models::{get_serialization_context, Address, Amount, Block, BlockHeader, Slot}; @@ -23,14 +23,13 @@ async fn test_protocol_sends_blocks_with_operations_to_consensus() { // .init() // .unwrap(); let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { let serialization_context = get_serialization_context(); // Create 1 node. @@ -69,7 +68,6 @@ async fn test_protocol_sends_blocks_with_operations_to_consensus() { true, creator_node.id, &mut protocol_event_receiver, - Some(storage.clone()), ) .await; } @@ -91,7 +89,6 @@ async fn test_protocol_sends_blocks_with_operations_to_consensus() { false, creator_node.id, &mut protocol_event_receiver, - Some(storage.clone()), ) .await; } @@ -112,7 +109,6 @@ async fn test_protocol_sends_blocks_with_operations_to_consensus() { false, creator_node.id, &mut protocol_event_receiver, - Some(storage.clone()), ) .await; } @@ -147,7 +143,6 @@ async fn test_protocol_sends_blocks_with_operations_to_consensus() { false, creator_node.id, &mut protocol_event_receiver, - Some(storage.clone()), ) .await; } @@ -169,7 +164,6 @@ async fn test_protocol_sends_blocks_with_operations_to_consensus() { false, creator_node.id, &mut protocol_event_receiver, - Some(storage.clone()), ) .await; } @@ -191,7 +185,6 @@ async fn test_protocol_sends_blocks_with_operations_to_consensus() { false, creator_node.id, &mut protocol_event_receiver, - Some(storage.clone()), ) .await; } diff --git a/massa-protocol-worker/src/tests/operations_scenarios.rs b/massa-protocol-worker/src/tests/operations_scenarios.rs index 5e04fef0154..000f37a2b47 100644 --- a/massa-protocol-worker/src/tests/operations_scenarios.rs +++ b/massa-protocol-worker/src/tests/operations_scenarios.rs @@ -2,7 +2,7 @@ // RUST_BACKTRACE=1 cargo test test_one_handshake -- --nocapture --test-threads=1 -use super::tools::{protocol_test, protocol_test_with_storage}; +use super::tools::protocol_test; use massa_models::prehash::{Map, Set}; use massa_models::signed::Signable; use massa_models::{self, Address, Amount, OperationId, Slot}; @@ -509,14 +509,13 @@ async fn test_protocol_propagates_operations_only_to_nodes_that_dont_know_about_ async fn test_protocol_propagates_operations_only_to_nodes_that_dont_know_about_it_indirect_knowledge_via_header( ) { let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, mut protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { // Create 2 nodes. let nodes = tools::create_and_connect_nodes(2, &mut network_controller).await; @@ -534,15 +533,9 @@ async fn test_protocol_propagates_operations_only_to_nodes_that_dont_know_about_ vec![operation.clone()], ); - let block_id = block - .header - .content - .compute_id() - .expect("Fail to compute block id"); - // Node 2 sends block, resulting in operations and endorsements noted in block info. network_controller - .send_block(nodes[1].id, block_id, Some((block.clone(), storage))) + .send_block(nodes[1].id, block.clone(), Default::default()) .await; // Node 1 sends header, resulting in protocol using the block info to determine @@ -605,14 +598,13 @@ async fn test_protocol_propagates_operations_only_to_nodes_that_dont_know_about_ async fn test_protocol_propagates_operations_only_to_nodes_that_dont_know_about_it_indirect_knowledge_via_header_wrong_root_hash( ) { let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, mut protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { // Create 3 nodes. let nodes = tools::create_and_connect_nodes(3, &mut network_controller).await; @@ -635,20 +627,15 @@ async fn test_protocol_propagates_operations_only_to_nodes_that_dont_know_about_ // Change the root operation hash block.operations = vec![operation_2.clone()]; - let block_id = block - .header - .content - .compute_id() - .expect("Fail to compute block id"); // Node 2 sends block, not resulting in operations and endorsements noted in block info, // because of the invalid root hash. network_controller - .send_block(nodes[1].id, block_id, Some((block.clone(), storage))) + .send_block(nodes[1].id, block.clone(), Default::default()) .await; // Node 3 sends block, resulting in operations and endorsements noted in block info. network_controller - .send_block(nodes[2].id, block_id, None) + .send_block(nodes[2].id, block.clone(), Default::default()) .await; // Node 1 sends header, but the block is empty. @@ -706,14 +693,13 @@ async fn test_protocol_propagates_operations_only_to_nodes_that_dont_know_about_ #[serial] async fn test_protocol_does_not_propagates_operations_when_receiving_those_inside_a_block() { let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, protocol_event_receiver, protocol_command_sender, protocol_manager, - mut protocol_pool_event_receiver, - storage| { + mut protocol_pool_event_receiver| { // Create 2 nodes. let mut nodes = tools::create_and_connect_nodes(2, &mut network_controller).await; @@ -737,15 +723,7 @@ async fn test_protocol_does_not_propagates_operations_when_receiving_those_insid // 4. Send block to protocol. network_controller - .send_block( - creator_node.id, - block - .header - .content - .compute_id() - .expect("Fail to compute block id"), - Some((block, storage)), - ) + .send_block(creator_node.id, block, Default::default()) .await; // 5. Check that the operation included in the block is not propagated. diff --git a/massa-protocol-worker/src/tests/scenarios.rs b/massa-protocol-worker/src/tests/scenarios.rs index 34efa358736..bd1702b0d39 100644 --- a/massa-protocol-worker/src/tests/scenarios.rs +++ b/massa-protocol-worker/src/tests/scenarios.rs @@ -2,10 +2,10 @@ // RUST_BACKTRACE=1 cargo test test_one_handshake -- --nocapture --test-threads=1 -use super::tools::{protocol_test, protocol_test_with_storage}; +use super::tools::protocol_test; use massa_models::prehash::{Map, Set}; use massa_models::signed::Signable; -use massa_models::{BlockId, SerializeCompact}; +use massa_models::BlockId; use massa_network_exports::NetworkCommand; use massa_protocol_exports::tests::tools; use massa_protocol_exports::{ @@ -351,14 +351,13 @@ async fn test_protocol_propagates_block_to_node_who_asked_for_it_and_only_header async fn test_protocol_sends_full_blocks_it_receives_to_consensus() { let protocol_settings = &tools::PROTOCOL_SETTINGS; - protocol_test_with_storage( + protocol_test( protocol_settings, async move |mut network_controller, mut protocol_event_receiver, protocol_command_sender, protocol_manager, - protocol_pool_event_receiver, - storage| { + protocol_pool_event_receiver| { // Create 1 node. let mut nodes = create_and_connect_nodes(1, &mut network_controller).await; @@ -371,15 +370,11 @@ async fn test_protocol_sends_full_blocks_it_receives_to_consensus() { // 3. Send block to protocol. network_controller - .send_block( - creator_node.id, - expected_hash, - Some((block.clone(), storage)), - ) + .send_block(creator_node.id, block.clone(), Default::default()) .await; // Check protocol sends block to consensus. - let block = match wait_protocol_event( + let block_id = match wait_protocol_event( &mut protocol_event_receiver, 1000.into(), |evt| match evt { @@ -389,10 +384,10 @@ async fn test_protocol_sends_full_blocks_it_receives_to_consensus() { ) .await { - Some(ProtocolEvent::ReceivedBlock { block_id, .. }) => block, + Some(ProtocolEvent::ReceivedBlock { block_id, .. }) => block_id, _ => panic!("Unexpected or no protocol event."), }; - assert_eq!(expected_hash, block.header.content.compute_id().unwrap()); + assert_eq!(expected_hash, block_id); ( network_controller, diff --git a/massa-protocol-worker/src/tests/tools.rs b/massa-protocol-worker/src/tests/tools.rs index 95776a4dc0e..0b39b8e5b8e 100644 --- a/massa-protocol-worker/src/tests/tools.rs +++ b/massa-protocol-worker/src/tests/tools.rs @@ -65,65 +65,3 @@ where .await .expect("Failed to shutdown protocol."); } - -pub async fn protocol_test_with_storage(protocol_settings: &'static ProtocolSettings, test: F) -where - F: FnOnce( - MockNetworkController, - ProtocolEventReceiver, - ProtocolCommandSender, - ProtocolManager, - ProtocolPoolEventReceiver, - Storage, - ) -> V, - V: Future< - Output = ( - MockNetworkController, - ProtocolEventReceiver, - ProtocolCommandSender, - ProtocolManager, - ProtocolPoolEventReceiver, - ), - >, -{ - let (network_controller, network_command_sender, network_event_receiver) = - MockNetworkController::new(); - let storage: Storage = Default::default(); - // start protocol controller - let ( - protocol_command_sender, - protocol_event_receiver, - protocol_pool_event_receiver, - protocol_manager, - ) = start_protocol_controller( - protocol_settings, - 5u64, - 10, - network_command_sender, - network_event_receiver, - storage.clone(), - ) - .await - .expect("could not start protocol controller"); - - let ( - _network_controller, - protocol_event_receiver, - _protocol_command_sender, - protocol_manager, - protocol_pool_event_receiver, - ) = test( - network_controller, - protocol_event_receiver, - protocol_command_sender, - protocol_manager, - protocol_pool_event_receiver, - storage, - ) - .await; - - protocol_manager - .stop(protocol_event_receiver, protocol_pool_event_receiver) - .await - .expect("Failed to shutdown protocol."); -}