fn initialize_block_body_v1_db(
    env: &Environment,
    block_header_db: &Database,
    block_body_v1_db: &Database,
    deleted_block_body_hashes_raw: &HashSet<&[u8]>,
    should_check_integrity: bool,
) -> Result<(), Error> {
    info!("initializing v1 block body database");
    let mut txn = env.begin_rw_txn()?;

    let block_body_hash_to_header_map =
        construct_block_body_to_block_header_reverse_lookup(&txn, block_header_db)?;

    let mut cursor = txn.open_rw_cursor(*block_body_v1_db)?;

    for (raw_key, _raw_val) in cursor.iter() {
        let block_body_hash =
            Digest::try_from(raw_key).map_err(|err| LmdbExtError::DataCorrupted(Box::new(err)))?;
        if !block_body_hash_to_header_map.contains_key(&block_body_hash) {
            if !deleted_block_body_hashes_raw.contains(raw_key) {
                // This means that the block body isn't referenced by any header, but no header
                // referencing it was just deleted, either
                warn!(?raw_key, "orphaned block body detected");
            }
            info!(?raw_key, "deleting v1 block body");
            cursor.del(WriteFlags::empty())?;
        }
    }

    drop(cursor);

    if should_check_integrity {
        let expected_hashing_algorithm_version = HashingAlgorithmVersion::V1;
        for (raw_key, raw_val) in txn.open_ro_cursor(*block_body_v1_db)?.iter() {
            let block_body_hash = Digest::try_from(raw_key)
                .map_err(|err| LmdbExtError::DataCorrupted(Box::new(err)))?;
            let block_body: BlockBody = lmdb_ext::deserialize(raw_val)?;
            if let Some(block_header) = block_body_hash_to_header_map.get(&block_body_hash) {
                let actual_hashing_algorithm_version = block_header.hashing_algorithm_version();
                if expected_hashing_algorithm_version != actual_hashing_algorithm_version {
                    return Err(Error::UnexpectedHashingAlgorithmVersion {
                        expected_hashing_algorithm_version,
                        actual_hashing_algorithm_version,
                    });
                }
                // Use smart constructor for block and propagate validation error accordingly
                Block::new_from_header_and_body(block_header.to_owned(), block_body)?;
            } else {
                // Should be unreachable because we just deleted all block bodies that aren't
                // referenced by any header
                return Err(Error::NoBlockHeaderForBlockBody {
                    block_body_hash,
                    hashing_algorithm_version: expected_hashing_algorithm_version,
                    block_body: Box::new(block_body),
                });
            }
        }
    }

    txn.commit()?;
    info!("v1 block body database initialized");
    Ok(())
}
/// Purges stale entries from the block body database, and checks the integrity of the remainder if
/// `should_check_integrity` is true.
Purges stale entries from the block body database, and checks the integrity of the remainder if
`should_check_integrity` is true.
Purges stale entries from the block body database and checks the integrity of the remainder if ` should_check_integrity ` is true .
fn initialize_block_body_v1_db(
    env: &Environment,
    block_header_db: &Database,
    block_body_v1_db: &Database,
    deleted_block_body_hashes_raw: &HashSet<&[u8]>,
    should_check_integrity: bool,
) -> Result<(), Error> {
    info!("initializing v1 block body database");
    let mut txn = env.begin_rw_txn()?;
    let block_body_hash_to_header_map =
        construct_block_body_to_block_header_reverse_lookup(&txn, block_header_db)?;
    let mut cursor = txn.open_rw_cursor(*block_body_v1_db)?;
    for (raw_key, _raw_val) in cursor.iter() {
        let block_body_hash =
            Digest::try_from(raw_key).map_err(|err| LmdbExtError::DataCorrupted(Box::new(err)))?;
        if !block_body_hash_to_header_map.contains_key(&block_body_hash) {
            if !deleted_block_body_hashes_raw.contains(raw_key) {
                // This means that the block body isn't referenced by any header, but no header
                // referencing it was just deleted, either
                warn!(?raw_key, "orphaned block body detected");
            }
            info!(?raw_key, "deleting v1 block body");
            cursor.del(WriteFlags::empty())?;
        }
    }
    drop(cursor);
    if should_check_integrity {
        let expected_hashing_algorithm_version = HashingAlgorithmVersion::V1;
        for (raw_key, raw_val) in txn.open_ro_cursor(*block_body_v1_db)?.iter() {
            let block_body_hash = Digest::try_from(raw_key)
                .map_err(|err| LmdbExtError::DataCorrupted(Box::new(err)))?;
            let block_body: BlockBody = lmdb_ext::deserialize(raw_val)?;
            if let Some(block_header) = block_body_hash_to_header_map.get(&block_body_hash) {
                let actual_hashing_algorithm_version = block_header.hashing_algorithm_version();
                if expected_hashing_algorithm_version != actual_hashing_algorithm_version {
                    return Err(Error::UnexpectedHashingAlgorithmVersion {
                        expected_hashing_algorithm_version,
                        actual_hashing_algorithm_version,
                    });
                }
                // Use smart constructor for block and propagate validation error accordingly
                Block::new_from_header_and_body(block_header.to_owned(), block_body)?;
            } else {
                // Should be unreachable because we just deleted all block bodies that aren't
                // referenced by any header
                return Err(Error::NoBlockHeaderForBlockBody {
                    block_body_hash,
                    hashing_algorithm_version: expected_hashing_algorithm_version,
                    block_body: Box::new(block_body),
                });
            }
        }
    }
    txn.commit()?;
    info!("v1 block body database initialized");
    Ok(())
}
fn initialize_block_body_v1_db ( env : & Environment , block_header_db : & Database , block_body_v1_db : & Database , deleted_block_body_hashes_raw : & HashSet < & [ u8 ] > , should_check_integrity : bool , ) -> Result < ( ) , Error > { info ! ( " " ) ; let mut txn = env . begin_rw_txn ( ) ? ; let block_body_hash_to_header_map = construct_block_body_to_block_header_reverse_lookup ( & txn , block_header_db ) ? ; let mut cursor = txn . open_rw_cursor ( * block_body_v1_db ) ? ; for ( raw_key , _raw_val ) in cursor . iter ( ) { let block_body_hash = Digest :: try_from ( raw_key ) . map_err ( | err | LmdbExtError :: DataCorrupted ( Box :: new ( err ) ) ) ? ; if ! block_body_hash_to_header_map . contains_key ( & block_body_hash ) { if ! deleted_block_body_hashes_raw . contains ( raw_key ) { // This means that the block body isn't referenced by any header, but no header // referencing it was just deleted, either warn ! ( raw_key " " ) ; } info ! ( raw_key " " ) ; cursor . del ( WriteFlags :: empty ( ) ) ? ; } } drop ( cursor ) ; if should_check_integrity { let expected_hashing_algorithm_version = HashingAlgorithmVersion :: V1 ; for ( raw_key , raw_val ) in txn . open_ro_cursor ( * block_body_v1_db ) ? . iter ( ) { let block_body_hash = Digest :: try_from ( raw_key ) . map_err ( | err | LmdbExtError :: DataCorrupted ( Box :: new ( err ) ) ) ? ; let block_body : BlockBody = lmdb_ext :: deserialize ( raw_val ) ? ; if let Some ( block_header ) = block_body_hash_to_header_map . get ( & block_body_hash ) { let actual_hashing_algorithm_version = block_header . hashing_algorithm_version ( ) ; if expected_hashing_algorithm_version != actual_hashing_algorithm_version { return Err ( Error :: UnexpectedHashingAlgorithmVersion { expected_hashing_algorithm_version , actual_hashing_algorithm_version , } ) ; } // Use smart constructor for block and propagate validation error accordingly Block :: new_from_header_and_body ( block_header . to_owned ( ) , block_body ) ? ; } else { // Should be unreachable because we just deleted all block bodies that aren't // referenced by any header return Err ( Error :: NoBlockHeaderForBlockBody { block_body_hash , hashing_algorithm_version : expected_hashing_algorithm_version , block_body : Box :: new ( block_body ) , } ) ; } } } txn . commit ( ) ? ; info ! ( " " ) ; Ok ( ( ) ) }
Purges stale entries from the block body database, and checks the integrity of the remainder if
`should_check_integrity` is true.
Purges stale entries from the block body database and checks the integrity of the remainder if ` should_check_integrity ` is true .
env
&Environment
block_header_db
&Database
block_body_v1_db
&Database
deleted_block_body_hashes_raw
&HashSet<&[u8]>
should_check_integrity
bool
env
&Environment
block_header_db
&Database
block_body_v1_db
&Database
deleted_block_body_hashes_raw
&HashSet<&[u8]>
should_check_integrity
bool