Skip to content

Commit

Permalink
Integration Test: SyncInvalid should get new block_template before ge…
Browse files Browse the repository at this point in the history
…nerate block

Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Aug 4, 2024
1 parent d895209 commit 3c3302f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
19 changes: 18 additions & 1 deletion test/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use ckb_types::{
};
use std::borrow::{Borrow, BorrowMut};
use std::collections::{HashMap, HashSet};
use std::fs;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -754,6 +755,22 @@ impl Node {
Ok(config)
}

pub fn access_log<F>(&self, line_checker: F) -> io::Result<bool>
where
F: Fn(&str) -> bool,
{
let file = File::open(self.log_path())?;
let reader = BufReader::new(file);

for line in reader.lines() {
let line = line?;
if line_checker(&line) {
return Ok(true);
}
}
Ok(false)
}

pub fn access_db<F>(&self, f: F)
where
F: Fn(&ChainDB),
Expand Down
31 changes: 23 additions & 8 deletions test/src/specs/sync/sync_invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use ckb_app_config::CKBAppConfig;
use ckb_logger::info;
use ckb_store::{ChainDB, ChainStore};
use ckb_types::core;
use ckb_types::core::BlockNumber;
use ckb_types::packed;
use ckb_types::prelude::{AsBlockBuilder, Builder, Entity, IntoUncleBlockView};
use ckb_types::prelude::{Builder, Entity, IntoUncleBlockView};
use std::thread::sleep;
use std::time::Duration;

Expand All @@ -15,6 +16,21 @@ impl Spec for SyncInvalid {

fn run(&self, nodes: &mut Vec<Node>) {
nodes[0].mine(20);

{
// wait for node[0] to find unverified blocks finished

let now = std::time::Instant::now();
while !nodes[0]
.access_log(|line: &str| line.contains("find unverified blocks finished"))
.expect("node[0] must have log")
{
if now.elapsed() > Duration::from_secs(60) {
panic!("node[0] should find unverified blocks finished in 60s");
}
info!("waiting for node[0] to find unverified blocks finished");
}
}
nodes[1].mine(1);

nodes[0].connect(&nodes[1]);
Expand All @@ -29,21 +45,20 @@ impl Spec for SyncInvalid {
);
};

let insert_invalid_block = || {
let template = nodes[0].rpc_client().get_block_template(None, None, None);

let block = packed::Block::from(template)
.as_advanced_builder()
let insert_invalid_block = |number: BlockNumber| {
let block = nodes[0]
.new_block_builder_with_blocking(|template| template.number < number.into())
.uncle(packed::UncleBlock::new_builder().build().into_view())
.build();
nodes[0]
.rpc_client()
.process_block_without_verify(block.data().into(), false);
info!("inserted invalid block {}", number);
};

info_nodes_tip();
insert_invalid_block();
insert_invalid_block();
insert_invalid_block(21);
insert_invalid_block(22);
info_nodes_tip();
assert_eq!(nodes[0].get_tip_block_number(), 22);

Expand Down

0 comments on commit 3c3302f

Please sign in to comment.