Skip to content

Commit

Permalink
feat(hardfork): use block timestamp of input cells as relative since …
Browse files Browse the repository at this point in the history
…start timestamp
  • Loading branch information
yangby-cryptape committed May 21, 2021
1 parent 92a7256 commit 4d75c5c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
7 changes: 6 additions & 1 deletion spec/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use serde::{Deserialize, Serialize};
pub struct HardForkConfig {
// TODO ckb2021 Update all rfc numbers and fix all links, after all proposals are merged.
/// Ref: [CKB RFC xxxx](https://github.com/nervosnetwork/rfcs/tree/master/rfcs/xxxx-rfc-title)
pub rfc_pr_0221: Option<EpochNumber>,
/// Ref: [CKB RFC xxxx](https://github.com/nervosnetwork/rfcs/tree/master/rfcs/xxxx-rfc-title)
pub rfc_pr_0223: Option<EpochNumber>,
}

Expand Down Expand Up @@ -56,7 +58,9 @@ impl HardForkConfig {
builder: HardForkSwitchBuilder,
ckb2021: EpochNumber,
) -> Result<HardForkSwitchBuilder, String> {
let builder = builder.rfc_pr_0223(check_default!(self, rfc_pr_0223, ckb2021));
let builder = builder
.rfc_pr_0221(check_default!(self, rfc_pr_0221, ckb2021))
.rfc_pr_0223(check_default!(self, rfc_pr_0223, ckb2021));
Ok(builder)
}

Expand All @@ -65,6 +69,7 @@ impl HardForkConfig {
/// Enable features which are set to `None` at the user provided epoch.
pub fn complete_with_default(&self, default: EpochNumber) -> Result<HardForkSwitch, String> {
HardForkSwitch::new_builder()
.rfc_pr_0221(self.rfc_pr_0221.unwrap_or(default))
.rfc_pr_0223(self.rfc_pr_0223.unwrap_or(default))
.build()
}
Expand Down
1 change: 1 addition & 0 deletions test/template/specs/integration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ epoch_duration_target = 14400
genesis_epoch_length = 1000

[params.hardfork]
rfc_pr_0221 = 9_223_372_036_854_775_807
rfc_pr_0223 = 9_223_372_036_854_775_807

[pow]
Expand Down
29 changes: 26 additions & 3 deletions util/types/src/core/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ macro_rules! define_methods {
/// [`HardForkSwitchBuilder`]: struct.HardForkSwitchBuilder.html
#[derive(Debug, Clone)]
pub struct HardForkSwitch {
rfc_pr_0221: EpochNumber,
rfc_pr_0223: EpochNumber,
}

Expand All @@ -103,6 +104,11 @@ pub struct HardForkSwitch {
#[derive(Debug, Clone, Default)]
pub struct HardForkSwitchBuilder {
// TODO ckb2021 Update all rfc numbers and fix all links, after all proposals are merged.
/// Use the input cell creation block timestamp as start time in the
/// "relative since timestamp".
///
/// Ref: [CKB RFC xxxx](https://github.com/nervosnetwork/rfcs/tree/master/rfcs/xxxx-rfc-title)
pub rfc_pr_0221: Option<EpochNumber>,
/// In the "since epoch", the index should be less than length and
/// the length should be greater than zero.
///
Expand All @@ -118,16 +124,29 @@ impl HardForkSwitch {

/// Creates a new builder based on the current instance.
pub fn as_builder(&self) -> HardForkSwitchBuilder {
Self::new_builder().rfc_pr_0223(self.rfc_pr_0223())
Self::new_builder()
.rfc_pr_0221(self.rfc_pr_0221())
.rfc_pr_0223(self.rfc_pr_0223())
}

/// Creates a new instance that all hard fork features are disabled forever.
pub fn new_without_any_enabled() -> Self {
// Use a builder to ensure all features are set manually.
Self::new_builder().disable_rfc_pr_0223().build().unwrap()
Self::new_builder()
.disable_rfc_pr_0221()
.disable_rfc_pr_0223()
.build()
.unwrap()
}
}

define_methods!(
rfc_pr_0221,
block_ts_as_relative_since_start,
is_block_ts_as_relative_since_start_enabled,
disable_rfc_pr_0221,
"RFC PR 0221"
);
define_methods!(
rfc_pr_0223,
check_length_in_epoch_since,
Expand All @@ -151,7 +170,11 @@ impl HardForkSwitchBuilder {
})?;
};
}
let rfc_pr_0221 = try_find!(rfc_pr_0221);
let rfc_pr_0223 = try_find!(rfc_pr_0223);
Ok(HardForkSwitch { rfc_pr_0223 })
Ok(HardForkSwitch {
rfc_pr_0221,
rfc_pr_0223,
})
}
}
18 changes: 16 additions & 2 deletions verification/src/transaction_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ impl<'a, DL: HeaderProvider> SinceVerifier<'a, DL> {
self.block_median_time(&parent_hash)
}

fn parent_block_time(&self, block_hash: &Byte32) -> u64 {
let (timestamp, _, _) = self.data_loader.timestamp_and_parent(block_hash);
timestamp
}

fn block_median_time(&self, block_hash: &Byte32) -> u64 {
if let Some(median_time) = self.median_timestamps_cache.borrow().peek(block_hash) {
return *median_time;
Expand Down Expand Up @@ -666,10 +671,19 @@ impl<'a, DL: HeaderProvider> SinceVerifier<'a, DL> {
// parent of current block.
// pass_median_time(input_cell's block) starts with cell_block_number - 1,
// which is the parent of input_cell's block
let proposal_window = self.consensus.tx_proposal_window();
let parent_hash = self.tx_env.parent_hash();
let cell_median_timestamp = self.parent_median_time(&info.block_hash);
let epoch_number = self.tx_env.epoch_number(proposal_window);
let hardfork_switch = self.consensus.hardfork_switch();
let base_timestamp = if hardfork_switch
.is_block_ts_as_relative_since_start_enabled(epoch_number)
{
self.parent_median_time(&info.block_hash)
} else {
self.parent_block_time(&info.block_hash)
};
let current_median_time = self.block_median_time(&parent_hash);
if current_median_time < cell_median_timestamp + timestamp {
if current_median_time < base_timestamp + timestamp {
return Err((TransactionError::Immature { index }).into());
}
}
Expand Down

0 comments on commit 4d75c5c

Please sign in to comment.