From 7e83dfac378cc9914ae824a058ef5d7430a6da99 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Mon, 5 Aug 2024 00:17:47 +0800 Subject: [PATCH] Integration Test: SyncInvalid should get new block_template before generate block --- test/src/node.rs | 19 ++++++++++++++++++- test/src/specs/sync/sync_invalid.rs | 29 ++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/test/src/node.rs b/test/src/node.rs index 2856e32b15..b1f3902b1c 100644 --- a/test/src/node.rs +++ b/test/src/node.rs @@ -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}; @@ -754,6 +755,22 @@ impl Node { Ok(config) } + pub fn access_log(&self, line_checker: F) -> io::Result + 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(&self, f: F) where F: Fn(&ChainDB), diff --git a/test/src/specs/sync/sync_invalid.rs b/test/src/specs/sync/sync_invalid.rs index 41b13e559e..e8b8250dae 100644 --- a/test/src/specs/sync/sync_invalid.rs +++ b/test/src/specs/sync/sync_invalid.rs @@ -3,6 +3,7 @@ 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 std::thread::sleep; @@ -15,6 +16,21 @@ impl Spec for SyncInvalid { fn run(&self, nodes: &mut Vec) { 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]); @@ -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);