Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overwrite mem block timestamp if CKB median time is less than tip block #625

Merged
merged 1 commit into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 17 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,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
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