pub fn validate_header_chain(
    headers: &HeaderArray,
    constant_difficulty: bool,
) -> Result<U256, SPVError> {
    let mut total_difficulty = U256::from(0u8);
    // declared outside the loop for proper check ordering
    let mut digest = Hash256Digest::default();
    let mut target = U256::default();

    for i in 0..headers.len() {
        let header = headers.index(i);

        if i == 0 {
            target = header.target();
        }
        if constant_difficulty && header.target() != target {
            return Err(SPVError::UnexpectedDifficultyChange);
        }

        if i != 0 && !validate_header_prev_hash(header, digest) {
            return Err(SPVError::InvalidChain);
        }

        digest = btcspv::hash256(&[header.as_ref()]);
        if !validate_header_work(digest, &target) {
            return Err(SPVError::InsufficientWork);
        }
        total_difficulty += btcspv::calculate_difficulty(&target);
    }
    Ok(total_difficulty)
}
/// Checks validity of header chain.
/// Compares the hash of each header to the prevHash in the next header.
///
/// # Arguments
///
/// * `headers` - Raw byte array of header chain
///
/// # Errors
///
/// * Errors if header chain is the wrong length, chain is invalid or insufficient work
Checks validity of header chain.
Compares the hash of each header to the prevHash in the next header.
Checks validity of header chain . Compares the hash of each header to the prevHash in the next header .
pub fn validate_header_chain(
    headers: &HeaderArray,
    constant_difficulty: bool,
) -> Result<U256, SPVError> {
    let mut total_difficulty = U256::from(0u8);
    // declared outside the loop for proper check ordering
    let mut digest = Hash256Digest::default();
    let mut target = U256::default();
    for i in 0..headers.len() {
        let header = headers.index(i);
        if i == 0 {
            target = header.target();
        }
        if constant_difficulty && header.target() != target {
            return Err(SPVError::UnexpectedDifficultyChange);
        }
        if i != 0 && !validate_header_prev_hash(header, digest) {
            return Err(SPVError::InvalidChain);
        }
        digest = btcspv::hash256(&[header.as_ref()]);
        if !validate_header_work(digest, &target) {
            return Err(SPVError::InsufficientWork);
        }
        total_difficulty += btcspv::calculate_difficulty(&target);
    }
    Ok(total_difficulty)
}
pub fn validate_header_chain ( headers : & HeaderArray , constant_difficulty : bool , ) -> Result < U256 , SPVError > { let mut total_difficulty = U256 :: from ( 0u8 ) ; // declared outside the loop for proper check ordering let mut digest = Hash256Digest :: default ( ) ; let mut target = U256 :: default ( ) ; for i in 0 .. headers . len ( ) { let header = headers . index ( i ) ; if i == 0 { target = header . target ( ) ; } if constant_difficulty && header . target ( ) != target { return Err ( SPVError :: UnexpectedDifficultyChange ) ; } if i != 0 && ! validate_header_prev_hash ( header , digest ) { return Err ( SPVError :: InvalidChain ) ; } digest = btcspv :: hash256 ( & [ header . as_ref ( ) ] ) ; if ! validate_header_work ( digest , & target ) { return Err ( SPVError :: InsufficientWork ) ; } total_difficulty += btcspv :: calculate_difficulty ( & target ) ; } Ok ( total_difficulty ) }
Checks validity of header chain.
Checks validity of header chain .
headers
&HeaderArray
constant_difficulty
bool
headers
&HeaderArray
constant_difficulty
bool
errors
Errors if header chain is the wrong length, chain is invalid or insufficient work
Errors if header chain is the wrong length chain is invalid or insufficient work