Skip to content

Commit

Permalink
test: add cases that mining during sync
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 authored and driftluo committed Aug 7, 2021
1 parent ad4d48d commit 3537b8e
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ fn all_specs() -> Vec<Box<dyn Spec>> {
Box::new(CheckVmVersion),
Box::new(CheckVmBExtension),
Box::new(ImmatureHeaderDeps),
Box::new(InboundSync),
Box::new(OutboundSync),
Box::new(InboundMinedDuringSync),
Box::new(OutboundMinedDuringSync),
];
specs.shuffle(&mut thread_rng());
specs
Expand Down
2 changes: 2 additions & 0 deletions test/src/specs/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod ibd_process;
mod invalid_block;
mod invalid_locator_size;
mod last_common_header;
mod sync_and_mine;
mod sync_timeout;

pub use block_sync::*;
Expand All @@ -14,4 +15,5 @@ pub use ibd_process::*;
pub use invalid_block::*;
pub use invalid_locator_size::*;
pub use last_common_header::*;
pub use sync_and_mine::*;
pub use sync_timeout::*;
231 changes: 231 additions & 0 deletions test/src/specs/sync/sync_and_mine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
use crate::util::mining::mine;
use crate::{Node, Spec};
use ckb_types::packed;
use std::thread::sleep;
use std::time::{Duration, Instant};

pub struct InboundMinedDuringSync;

impl Spec for InboundMinedDuringSync {
crate::setup!(num_nodes: 2);

fn run(&self, nodes: &mut Vec<Node>) {
let node0 = &nodes[0];
let node1 = &nodes[1];

mine(node0, 1);
mine(node1, 1);

// node0 is outbound
// node1 is inbound
node0.connect(node1);

mine(node0, 2000);
let template = node1.rpc_client().get_block_template(None, None, None);
let block = packed::Block::from(template).into_view();
node1.submit_block(&block);
ckb_logger::debug!("node1 submit block-{}", block.number());

let last_check_time = Instant::now();
let mut last_logging_time = Instant::now();
let mut tip_number1 = node1.get_tip_block_number();
loop {
let tip_block0 = node0.get_tip_block();
let tip_block1 = node1.get_tip_block();
if tip_block0.number() == tip_block1.number() {
break;
}

if last_logging_time.elapsed() > Duration::from_secs(10) {
last_logging_time = Instant::now();
ckb_logger::debug!(
"node0.tip_block.number = {}, node1.tip_block.number = {}",
tip_block0.number(),
tip_block1.number()
);
}

if last_check_time.elapsed() > Duration::from_secs(30) {
if tip_number1 == tip_block1.number() {
assert_eq!(
tip_block0.number(),
tip_block1.number(),
"node1 did not grow up in at past {} seconds",
last_check_time.elapsed().as_secs()
);
}

tip_number1 = tip_block1.number();
}

sleep(Duration::from_secs(1));
}
}
}
pub struct OutboundMinedDuringSync;

impl Spec for OutboundMinedDuringSync {
crate::setup!(num_nodes: 2);

fn run(&self, nodes: &mut Vec<Node>) {
let node0 = &nodes[0];
let node1 = &nodes[1];

mine(node0, 1);
mine(node1, 1);

// node0 is outbound
// node1 is inbound
node1.connect(node0);

mine(node0, 2000);
let template = node1.rpc_client().get_block_template(None, None, None);
let block = packed::Block::from(template).into_view();
node1.submit_block(&block);
ckb_logger::debug!("node1 submit block-{}", block.number());

let last_check_time = Instant::now();
let mut last_logging_time = Instant::now();
let mut tip_number1 = node1.get_tip_block_number();
loop {
let tip_block0 = node0.get_tip_block();
let tip_block1 = node1.get_tip_block();
if tip_block0.number() == tip_block1.number() {
break;
}

if last_logging_time.elapsed() > Duration::from_secs(10) {
last_logging_time = Instant::now();
ckb_logger::debug!(
"node0.tip_block.number = {}, node1.tip_block.number = {}",
tip_block0.number(),
tip_block1.number()
);
}

if last_check_time.elapsed() > Duration::from_secs(30) {
if tip_number1 == tip_block1.number() {
assert_eq!(
tip_block0.number(),
tip_block1.number(),
"node1 did not grow up in at past {} seconds",
last_check_time.elapsed().as_secs()
);
}

tip_number1 = tip_block1.number();
}

sleep(Duration::from_secs(1));
}
}
}

pub struct InboundSync;

impl Spec for InboundSync {
crate::setup!(num_nodes: 2);

fn run(&self, nodes: &mut Vec<Node>) {
let node0 = &nodes[0];
let node1 = &nodes[1];

mine(node0, 1);
mine(node1, 1);

// node0 is outbound
// node1 is inbound
node0.connect(node1);

mine(node0, 2000);

let last_check_time = Instant::now();
let mut last_logging_time = Instant::now();
let mut tip_number1 = node1.get_tip_block_number();
loop {
let tip_block0 = node0.get_tip_block();
let tip_block1 = node1.get_tip_block();
if tip_block0.number() == tip_block1.number() {
break;
}

if last_logging_time.elapsed() > Duration::from_secs(10) {
last_logging_time = Instant::now();
ckb_logger::debug!(
"node0.tip_block.number = {}, node1.tip_block.number = {}",
tip_block0.number(),
tip_block1.number()
);
}

if last_check_time.elapsed() > Duration::from_secs(30) {
if tip_number1 == tip_block1.number() {
assert_eq!(
tip_block0.number(),
tip_block1.number(),
"node1 did not grow up in at past {} seconds",
last_check_time.elapsed().as_secs()
);
}

tip_number1 = tip_block1.number();
}

sleep(Duration::from_secs(1));
}
}
}
pub struct OutboundSync;

impl Spec for OutboundSync {
crate::setup!(num_nodes: 2);

fn run(&self, nodes: &mut Vec<Node>) {
let node0 = &nodes[0];
let node1 = &nodes[1];

mine(node0, 1);
mine(node1, 1);

// node0 is outbound
// node1 is inbound
node1.connect(node0);

mine(node0, 2000);

let last_check_time = Instant::now();
let mut last_logging_time = Instant::now();
let mut tip_number1 = node1.get_tip_block_number();
loop {
let tip_block0 = node0.get_tip_block();
let tip_block1 = node1.get_tip_block();
if tip_block0.number() == tip_block1.number() {
break;
}

if last_logging_time.elapsed() > Duration::from_secs(10) {
last_logging_time = Instant::now();
ckb_logger::debug!(
"node0.tip_block.number = {}, node1.tip_block.number = {}",
tip_block0.number(),
tip_block1.number()
);
}

if last_check_time.elapsed() > Duration::from_secs(30) {
if tip_number1 == tip_block1.number() {
assert_eq!(
tip_block0.number(),
tip_block1.number(),
"node1 did not grow up in at past {} seconds",
last_check_time.elapsed().as_secs()
);
}

tip_number1 = tip_block1.number();
}

sleep(Duration::from_secs(1));
}
}
}

0 comments on commit 3537b8e

Please sign in to comment.