From fb08a9698e64ef28b5720fa8d522c4fce97bc416 Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 23 Mar 2022 17:27:45 +0800 Subject: [PATCH] fix: overwrite mem block timestamp if CKB median time is less than tip block --- crates/block-producer/src/block_producer.rs | 12 ++++++++++++ crates/mem-pool/src/pool.rs | 20 +++++++++++++++++--- crates/tools/src/deploy_genesis.rs | 5 ++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/block-producer/src/block_producer.rs b/crates/block-producer/src/block_producer.rs index 6dce45328..327575b99 100644 --- a/crates/block-producer/src/block_producer.rs +++ b/crates/block-producer/src/block_producer.rs @@ -312,6 +312,18 @@ impl BlockProducer { Some(time) => time, None => return Ok(()), }; + let tip_block_timestamp = Duration::from_millis( + self.store + .get_last_valid_tip_block()? + .raw() + .timestamp() + .unpack(), + ); + if median_time < tip_block_timestamp { + log::warn!("[block producer] median time is less than tip block timestamp, skip produce new block"); + return Ok(()); + } + let poa_cell_input = InputCellInfo { input: CellInput::new_builder() .previous_output(rollup_cell.out_point.clone()) diff --git a/crates/mem-pool/src/pool.rs b/crates/mem-pool/src/pool.rs index b48264969..6b5546459 100644 --- a/crates/mem-pool/src/pool.rs +++ b/crates/mem-pool/src/pool.rs @@ -42,7 +42,7 @@ use std::{ iter::FromIterator, ops::Shr, sync::Arc, - time::Instant, + time::{Duration, Instant}, }; use tracing::instrument; @@ -583,14 +583,28 @@ impl MemPool { } // estimate next l2block timestamp - let estimated_timestamp = self.provider.estimate_next_blocktime().await?; + let estimated_timestamp = { + let estimated = self.provider.estimate_next_blocktime().await?; + let tip_timestamp = Duration::from_millis(new_tip_block.raw().timestamp().unpack()); + if estimated <= tip_timestamp { + let overwriten_timestamp = tip_timestamp.saturating_add(Duration::from_secs(1)); + log::warn!( + "[mem-pool] reset mem-pool with insatisfied estimated time, overwrite estimated time {:?} -> {:?}", + estimated, + overwriten_timestamp + ); + overwriten_timestamp + } else { + estimated + } + }; // reset mem block state // Fix execute_raw_l2transaction panic by updating mem_store first and storing it to mem_pool_state after. let snapshot = self.store.get_snapshot(); assert_eq!(snapshot.get_tip_block_hash()?, new_tip, "set new snapshot"); let mem_store = MemStore::new(snapshot); - mem_store.update_mem_pool_block_info(self.mem_block.block_info())?; let mem_block_content = self.mem_block.reset(&new_tip_block, estimated_timestamp); + mem_store.update_mem_pool_block_info(self.mem_block.block_info())?; self.mem_pool_state.store(Arc::new(mem_store)); // set tip diff --git a/crates/tools/src/deploy_genesis.rs b/crates/tools/src/deploy_genesis.rs index 97da71762..c0e405484 100644 --- a/crates/tools/src/deploy_genesis.rs +++ b/crates/tools/src/deploy_genesis.rs @@ -1,4 +1,4 @@ -use std::ops::Deref; +use std::ops::{Deref, Sub}; use std::path::Path; use std::str::FromStr; @@ -360,9 +360,12 @@ pub fn deploy_rollup_cell(args: DeployRollupCellArgs) -> Result