/// Validates a header. If a failure occurs, a
/// [ValidateHeaderError](ValidateHeaderError) will be returned.
Validates a header. If a failure occurs, a
[ValidateHeaderError](ValidateHeaderError) will be returned.
Validates a header . If a failure occurs a [ ValidateHeaderError ] ( ValidateHeaderError ) will be returned .
pub fn validate_header(
    network: &Network,
    store: &impl HeaderStore,
    header: &BlockHeader,
) -> Result<(), ValidateHeaderError> {
    let prev_stored_header = match store.get_header(&header.prev_blockhash) {
        Some(header) => header,
        None => {
            return Err(ValidateHeaderError::PrevHeaderNotFound);
        }
    };
    if !is_timestamp_valid(store, header) {
        return Err(ValidateHeaderError::HeaderIsOld);
    }
    if !is_checkpoint_valid(network, prev_stored_header, header) {
        return Err(ValidateHeaderError::DoesNotMatchCheckpoint);
    }
    let header_target = header.target();
    if header_target > max_target(network) {
        return Err(ValidateHeaderError::TargetDifficultyAboveMax);
    }
    if header.validate_pow(&header_target).is_err() {
        return Err(ValidateHeaderError::InvalidPoWForHeaderTarget);
    }
    let target = get_next_target(network, store, prev_stored_header, header);
    if let Err(err) = header.validate_pow(&target) {
        match err {
            bitcoin::Error::BlockBadProofOfWork => println!("bad proof of work"),
            bitcoin::Error::BlockBadTarget => println!("bad target"),
            _ => {}
        };
        return Err(ValidateHeaderError::InvalidPoWForComputedTarget);
    }
    Ok(())
}
pub fn validate_header ( network : & Network , store : & impl HeaderStore , header : & BlockHeader , ) -> Result < ( ) , ValidateHeaderError > { let prev_stored_header = match store . get_header ( & header . prev_blockhash ) { Some ( header ) => header , None => { return Err ( ValidateHeaderError :: PrevHeaderNotFound ) ; } } ; if ! is_timestamp_valid ( store , header ) { return Err ( ValidateHeaderError :: HeaderIsOld ) ; } if ! is_checkpoint_valid ( network , prev_stored_header , header ) { return Err ( ValidateHeaderError :: DoesNotMatchCheckpoint ) ; } let header_target = header . target ( ) ; if header_target > max_target ( network ) { return Err ( ValidateHeaderError :: TargetDifficultyAboveMax ) ; } if header . validate_pow ( & header_target ) . is_err ( ) { return Err ( ValidateHeaderError :: InvalidPoWForHeaderTarget ) ; } let target = get_next_target ( network , store , prev_stored_header , header ) ; if let Err ( err ) = header . validate_pow ( & target ) { match err { bitcoin :: Error :: BlockBadProofOfWork => println ! ( " " ) , bitcoin :: Error :: BlockBadTarget => println ! ( " " ) , _ => { } } ; return Err ( ValidateHeaderError :: InvalidPoWForComputedTarget ) ; } Ok ( ( ) ) }
Validates a header.
Validates a header .
network
&Network
store
&impl HeaderStore
header
&BlockHeader