Skip to content

Commit

Permalink
test: missing uncle request
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Oct 10, 2019
1 parent 77a2769 commit ecc0d7d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ fn all_specs() -> SpecMap {
Box::new(ProposeButNotCommit),
Box::new(ProposeDuplicated),
Box::new(ForkedTransaction),
Box::new(MissingUncleRequest),
];
specs.into_iter().map(|spec| (spec.name(), spec)).collect()
}
Expand Down
77 changes: 77 additions & 0 deletions test/src/specs/relay/get_block_transactions_process.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::{Net, Spec, TestProtocol};
use ckb_sync::NetworkProtocol;
use ckb_types::{
core::UncleBlockView,
packed::{self, RelayMessage},
prelude::*,
};

pub struct MissingUncleRequest;

impl Spec for MissingUncleRequest {
crate::name!("missing_uncle_request");

crate::setup!(protocols: vec![TestProtocol::sync(), TestProtocol::relay()]);

// Case: Send to node GetBlockTransactions with missing uncle index, node should response BlockTransactions with uncles
fn run(&self, net: &mut Net) {
net.exit_ibd_mode();
let node = &net.nodes[0];
net.connect(node);
let (peer_id, _, _) = net.receive();

node.generate_block();
let _ = net.receive();

let builder = node.new_block_builder(None, None, None);
let block1 = builder.clone().nonce(0.pack()).build();
let block2 = builder.clone().nonce(1.pack()).build();
node.submit_block(&block1.data());
node.submit_block(&block2.data());

let builder = node.new_block_builder(None, None, None);
let block = builder
.clone()
.set_uncles(vec![block2.as_uncle()])
.nonce(0.pack())
.build();
node.submit_block(&block.data());

let content = packed::GetBlockTransactions::new_builder()
.block_hash(block.hash())
.uncle_indexes(vec![0u32].pack())
.build();
let message = packed::RelayMessage::new_builder().set(content).build();

(0..3).for_each(|_| {
net.receive(); // ignore three new block announce
});

net.send(
NetworkProtocol::RELAY.into(),
peer_id,
message.as_slice().into(),
);

let (_, _, data) = net.receive();
let message = RelayMessage::from_slice(&data).unwrap();

assert_eq!(
message.to_enum().item_name(),
packed::BlockTransactions::NAME,
"Node should reponse BlockTransactions message",
);

if let packed::RelayMessageUnionReader::BlockTransactions(reader) =
message.to_enum().as_reader()
{
let block_transactions = reader.to_entity();
let received_uncles: Vec<UncleBlockView> = block_transactions
.uncles()
.into_iter()
.map(|uncle| uncle.into_view())
.collect();
assert_eq!(received_uncles[0], block2.as_uncle());
}
}
}
2 changes: 2 additions & 0 deletions test/src/specs/relay/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod block_relay;
mod compact_block;
mod get_block_transactions_process;
mod transaction_relay;

pub use block_relay::*;
pub use compact_block::*;
pub use get_block_transactions_process::*;
pub use transaction_relay::*;

0 comments on commit ecc0d7d

Please sign in to comment.