Skip to content

Commit

Permalink
Check MTP when CSV is activated
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Jul 17, 2024
1 parent c8fe7fd commit 571a0b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 4 additions & 4 deletions crates/sc-consensus-nakamoto/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ where
) -> Result<(), Error> {
let transactions = &block.txdata;

let parent_number = block_number - 1;
let parent_hash =
self.client
.hash((block_number - 1).into())?
.hash(parent_number.into())?
.ok_or(sp_blockchain::Error::Backend(format!(
"Parent block #{} not found",
block_number - 1
"Parent block #{parent_number} not found"
)))?;

// Verifies non-coinbase transaction.
Expand Down Expand Up @@ -301,7 +301,7 @@ where
// TODO: verify transactions in parallel.
for (index, tx) in transactions.iter().enumerate() {
if index == 0 {
// TODO: verify coinbase script
// Coinbase script was already checked in check_block_sanity().
continue;
}

Expand Down
14 changes: 10 additions & 4 deletions crates/sc-consensus-nakamoto/src/verification/header_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
/// - Check proof of work.
/// - Check the timestamp of the block is in the range:
/// - Time is not greater than 2 hours from now.
/// - Time is not the median time of last 12 blocks or before.
/// - Time is not the median time of last 11 blocks or before.
pub fn verify_header(&self, header: &BitcoinHeader) -> Result<(), Error> {
let last_block_header = self.client.block_header(header.prev_blockhash).ok_or(
sp_blockchain::Error::MissingHeader(header.prev_blockhash.to_string()),
Expand Down Expand Up @@ -100,9 +100,15 @@ where
return Err(Error::TooFarInFuture);
}

let median_time = self.calculate_median_time_past(header);
if header.time <= median_time {
return Err(Error::TimeTooOld);
let block_number = last_block_height + 1;

// TODO: check deployment state properly.
const MAINNET_CSV_HEIGHT: u32 = 419328;
if block_number >= MAINNET_CSV_HEIGHT {
let median_time = self.calculate_median_time_past(header);
if header.time <= median_time {
return Err(Error::TimeTooOld);
}
}

Ok(())
Expand Down

0 comments on commit 571a0b7

Please sign in to comment.