Skip to content

Commit

Permalink
fix: overwrite mem block timestamp if CKB median time is less than ti…
Browse files Browse the repository at this point in the history
…p block
  • Loading branch information
jjyr committed Mar 23, 2022
1 parent 15e3b2a commit 5ba9f9b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
12 changes: 12 additions & 0 deletions crates/block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
28 changes: 25 additions & 3 deletions crates/mem-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::{
iter::FromIterator,
ops::Shr,
sync::Arc,
time::Instant,
time::{Duration, Instant},
};
use tracing::instrument;

Expand Down Expand Up @@ -583,16 +583,38 @@ 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));

// check memblock timestamp
if new_number > 0 {
assert!(
mem_timestamp >= tip_timestamp,
"mem block timestamp must greater than tip block timestamp"
);
}

// set tip
self.current_tip = (new_tip, new_tip_block.raw().number().unpack());

Expand Down
5 changes: 4 additions & 1 deletion crates/tools/src/deploy_genesis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::ops::{Deref, Sub};
use std::path::Path;
use std::str::FromStr;

Expand Down Expand Up @@ -360,9 +360,12 @@ pub fn deploy_rollup_cell(args: DeployRollupCellArgs) -> Result<RollupDeployment

// millisecond
let timestamp = timestamp.unwrap_or_else(|| {
// New created CKB dev chain's may out of sync with real world time,
// So we using an earlier time to get around this issue.
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("timestamp")
.sub(core::time::Duration::from_secs(3600))
.as_millis() as u64
});

Expand Down

0 comments on commit 5ba9f9b

Please sign in to comment.