/// This function tests that the block timestamp is greater than the median timestamp at the specified height.
This function tests that the block timestamp is greater than the median timestamp at the specified height.
This function tests that the block timestamp is greater than the median timestamp at the specified height .
pub fn check_median_timestamp<B: BlockchainBackend>(
    db: &B,
    block_header: &BlockHeader,
    height: u64,
    rules: ConsensusManager,
) -> Result<(), ValidationError>
{
    if block_header.height == 0 || rules.get_genesis_block_hash() == block_header.hash() {
        return Ok(()); // Its the genesis block, so we dont have to check median
    }
    let min_height = height.saturating_sub(
        rules
            .consensus_constants(block_header.height)
            .get_median_timestamp_count() as u64,
    );
    let timestamps = fetch_headers(db, min_height, height)?
        .iter()
        .map(|h| h.timestamp)
        .collect::<Vec<_>>();
    let median_timestamp = get_median_timestamp(timestamps).ok_or_else(|| {
        error!(target: LOG_TARGET, "Validation could not get median timestamp");
        ValidationError::BlockHeaderError(BlockHeaderValidationError::InvalidTimestamp)
    })?;
    if block_header.timestamp < median_timestamp {
        warn!(
            target: LOG_TARGET,
            "Block header timestamp {} is less than median timestamp: {} for block:{}",
            block_header.timestamp,
            median_timestamp,
            block_header.hash().to_hex()
        );
        return Err(ValidationError::BlockHeaderError(
            BlockHeaderValidationError::InvalidTimestamp,
        ));
    }
    Ok(())
}
pub fn check_median_timestamp < B : BlockchainBackend > ( db : & B , block_header : & BlockHeader , height : u64 , rules : ConsensusManager , ) -> Result < ( ) , ValidationError > { if block_header . height == 0 || rules . get_genesis_block_hash ( ) == block_header . hash ( ) { return Ok ( ( ) ) ; // Its the genesis block, so we dont have to check median } let min_height = height . saturating_sub ( rules . consensus_constants ( block_header . height ) . get_median_timestamp_count ( ) as u64 , ) ; let timestamps = fetch_headers ( db , min_height , height ) ? . iter ( ) . map ( | h | h . timestamp ) . collect :: < Vec < _ > > ( ) ; let median_timestamp = get_median_timestamp ( timestamps ) . ok_or_else ( | | { error ! ( target LOG_TARGET " " ) ; ValidationError :: BlockHeaderError ( BlockHeaderValidationError :: InvalidTimestamp ) } ) ? ; if block_header . timestamp < median_timestamp { warn ! ( target LOG_TARGET " " block_header timestamp median_timestamp block_header hash ( ) to_hex ( ) ) ; return Err ( ValidationError :: BlockHeaderError ( BlockHeaderValidationError :: InvalidTimestamp , ) ) ; } Ok ( ( ) ) }
This function tests that the block timestamp is greater than the median timestamp at the specified height.
This function tests that the block timestamp is greater than the median timestamp at the specified height .
db
&B
block_header
&BlockHeader
height
u64
rules
ConsensusManager