diff --git a/testing/jormungandr-integration-tests/src/jormungandr/bft/block.rs b/testing/jormungandr-integration-tests/src/jormungandr/bft/block.rs new file mode 100644 index 0000000000..e615fae70f --- /dev/null +++ b/testing/jormungandr-integration-tests/src/jormungandr/bft/block.rs @@ -0,0 +1,53 @@ +use assert_fs::TempDir; +use chain_impl_mockchain::chaintypes::ConsensusType; +use jormungandr_lib::interfaces::SlotDuration; +use jormungandr_testing_utils::testing::{ + adversary::process::AdversaryNodeBuilder, + jormungandr::{ConfigurationBuilder, Starter, StartupVerificationMode}, + network::LeadershipMode, + node::grpc::server::ProtocolVersion, + Block0ConfigurationBuilder, FragmentNode, +}; +use std::time::Duration; + +#[test] +fn block0_with_incorrect_hash() { + let block0 = Block0ConfigurationBuilder::new() + .with_slot_duration(SlotDuration::new(10).unwrap()) + .with_block0_consensus(ConsensusType::Bft) + .build() + .to_block(); + + let adversary = AdversaryNodeBuilder::new(block0.clone()) + .with_protocol_version(ProtocolVersion::Bft) + .with_server_enabled() + .build(); + + assert!(adversary + .node_data + .read() + .unwrap() + .get_block(block0.header().id()) + .is_ok()); + + println!("ADVERSARY ADDR:{}", adversary.address()); + println!("BLOCK0 HASH: {}", adversary.genesis_block_hash()); + + let passive_temp_dir = TempDir::new().unwrap(); + + let passive_params = ConfigurationBuilder::default() + .with_block0_consensus(ConsensusType::Bft) + .with_trusted_peers(vec![adversary.to_trusted_peer()]) + .with_block_hash(format!("{}", adversary.genesis_block_hash())) + .build(&passive_temp_dir); + + let passive = Starter::default() + .config(passive_params) + .leadership_mode(LeadershipMode::Passive) + .start() + .unwrap(); + + for log in passive.log_content() { + println!("{}", log); + } +} diff --git a/testing/jormungandr-integration-tests/src/jormungandr/bft/mod.rs b/testing/jormungandr-integration-tests/src/jormungandr/bft/mod.rs index 00d8d257e8..45047b4071 100644 --- a/testing/jormungandr-integration-tests/src/jormungandr/bft/mod.rs +++ b/testing/jormungandr-integration-tests/src/jormungandr/bft/mod.rs @@ -1,2 +1,3 @@ -pub mod mempool; -pub mod start_node; +mod block; +mod mempool; +mod start_node; diff --git a/testing/jormungandr-testing-utils/src/testing/adversary/process.rs b/testing/jormungandr-testing-utils/src/testing/adversary/process.rs index 898dcc3da3..fa68b6cb42 100644 --- a/testing/jormungandr-testing-utils/src/testing/adversary/process.rs +++ b/testing/jormungandr-testing-utils/src/testing/adversary/process.rs @@ -35,7 +35,7 @@ use std::sync::{Arc, RwLock}; pub struct AdversaryNode { temp_dir: Option, alias: String, - node_data: Arc>, + pub node_data: Arc>, server: Option, open_client_connections: HashMap, } diff --git a/testing/jormungandr-testing-utils/src/testing/node/grpc/server/mod.rs b/testing/jormungandr-testing-utils/src/testing/node/grpc/server/mod.rs index 25bef1374f..ad85f0f5c6 100644 --- a/testing/jormungandr-testing-utils/src/testing/node/grpc/server/mod.rs +++ b/testing/jormungandr-testing-utils/src/testing/node/grpc/server/mod.rs @@ -8,6 +8,11 @@ pub use super::proto::{ }, }; +use chain_core::{ + mempack::{ReadBuf, Readable}, + property::Serialize, +}; +use chain_impl_mockchain::key::Hash; use std::sync::RwLock; use tokio::sync::mpsc; use tokio_stream::wrappers::ReceiverStream; @@ -150,13 +155,41 @@ impl Node for JormungandrServerImpl { } async fn get_blocks( &self, - _request: tonic::Request, + request: tonic::Request, ) -> Result, tonic::Status> { info!( method = %MethodType::GetBlocks, "Get blocks request received" ); - let (_tx, rx) = mpsc::channel(1); + + let block_ids = request.into_inner(); + + let mut blocks = vec![]; + + for block_id in block_ids.ids.iter() { + let block_hash = Hash::read(&mut ReadBuf::from(block_id.as_ref())).unwrap(); + + blocks.push( + self.data + .read() + .unwrap() + .get_block(block_hash) + .map_err(|_| tonic::Status::not_found(format!("{} not available", block_hash))), + ); + } + + let (tx, rx) = mpsc::channel(blocks.len()); + + for block in blocks { + tx.send(block.map(|b| { + let mut bytes = vec![]; + b.serialize(&mut bytes).unwrap(); + Block { content: bytes } + })) + .await + .unwrap(); + } + Ok(Response::new(ReceiverStream::new(rx))) } async fn get_headers(