fn insert_header(
        &self,
        txn: &WriteTransaction<'_>,
        header: &BlockHeader,
        accum_data: &BlockHeaderAccumulatedData,
    ) -> Result<(), ChainStorageError> {
        if let Some(current_header_at_height) = lmdb_get::<_, BlockHeader>(txn, &self.headers_db, &header.height)? {
            let hash = current_header_at_height.hash();
            if hash != accum_data.hash {
                return Err(ChainStorageError::InvalidOperation(format!(
                    "There is a different header stored at height {} already. New header ({}), current header: ({})",
                    header.height,
                    accum_data.hash.to_hex(),
                    current_header_at_height.hash().to_hex(),
                )));
            }
            return Err(ChainStorageError::InvalidOperation(format!(
                "The header at height {} already exists. Existing header hash: {}",
                header.height,
                hash.to_hex()
            )));
        }

        // Check that the current height is still header.height - 1 and that no other threads have inserted
        if let Some(ref last_header) = self.fetch_last_header_in_txn(&txn)? {
            if last_header.height != header.height.saturating_sub(1) {
                return Err(ChainStorageError::InvalidOperation(format!(
                    "Attempted to insert a header out of order. Was expecting chain height to be {} but current last \
                     header height is {}",
                    header.height - 1,
                    last_header.height
                )));
            }

            // Possibly remove this check later
            let hash = last_header.hash();
            if hash != header.prev_hash {
                return Err(ChainStorageError::InvalidOperation(format!(
                    "Attempted to insert a block header at height {} that didn't form a chain. Previous block \
                     hash:{}, new block's previous hash:{}",
                    header.height,
                    hash.to_hex(),
                    header.prev_hash.to_hex()
                )));
            }
        } else if header.height != 0 {
            return Err(ChainStorageError::InvalidOperation(format!(
                "The first header inserted must have height 0. Height provided: {}",
                header.height
            )));
        }

        lmdb_insert(
            &txn,
            &self.header_accumulated_data_db,
            &header.height,
            &accum_data,
            "header_accumulated_data_db",
        )?;
        lmdb_insert(
            txn,
            &self.block_hashes_db,
            header.hash().as_slice(),
            &header.height,
            "block_hashes_db",
        )?;
        lmdb_insert(txn, &self.headers_db, &header.height, header, "headers_db")?;
        lmdb_insert(
            txn,
            &self.kernel_mmr_size_index,
            &header.kernel_mmr_size.to_be_bytes(),
            &header.height,
            "kernel_mmr_size_index",
        )?;
        lmdb_insert(
            txn,
            &self.output_mmr_size_index,
            &header.output_mmr_size.to_be_bytes(),
            &(header.height, header.hash().as_slice()),
            "output_mmr_size_index",
        )?;
        Ok(())
    }
/// Inserts the header and header accumulated data.
Inserts the header and header accumulated data.
Inserts the header and header accumulated data .
fn insert_header(
        &self,
        txn: &WriteTransaction<'_>,
        header: &BlockHeader,
        accum_data: &BlockHeaderAccumulatedData,
    ) -> Result<(), ChainStorageError> {
        if let Some(current_header_at_height) = lmdb_get::<_, BlockHeader>(txn, &self.headers_db, &header.height)? {
            let hash = current_header_at_height.hash();
            if hash != accum_data.hash {
                return Err(ChainStorageError::InvalidOperation(format!(
                    "There is a different header stored at height {} already. New header ({}), current header: ({})",
                    header.height,
                    accum_data.hash.to_hex(),
                    current_header_at_height.hash().to_hex(),
                )));
            }
            return Err(ChainStorageError::InvalidOperation(format!(
                "The header at height {} already exists. Existing header hash: {}",
                header.height,
                hash.to_hex()
            )));
        }
        // Check that the current height is still header.height - 1 and that no other threads have inserted
        if let Some(ref last_header) = self.fetch_last_header_in_txn(&txn)? {
            if last_header.height != header.height.saturating_sub(1) {
                return Err(ChainStorageError::InvalidOperation(format!(
                    "Attempted to insert a header out of order. Was expecting chain height to be {} but current last \
                     header height is {}",
                    header.height - 1,
                    last_header.height
                )));
            }
            // Possibly remove this check later
            let hash = last_header.hash();
            if hash != header.prev_hash {
                return Err(ChainStorageError::InvalidOperation(format!(
                    "Attempted to insert a block header at height {} that didn't form a chain. Previous block \
                     hash:{}, new block's previous hash:{}",
                    header.height,
                    hash.to_hex(),
                    header.prev_hash.to_hex()
                )));
            }
        } else if header.height != 0 {
            return Err(ChainStorageError::InvalidOperation(format!(
                "The first header inserted must have height 0. Height provided: {}",
                header.height
            )));
        }
        lmdb_insert(
            &txn,
            &self.header_accumulated_data_db,
            &header.height,
            &accum_data,
            "header_accumulated_data_db",
        )?;
        lmdb_insert(
            txn,
            &self.block_hashes_db,
            header.hash().as_slice(),
            &header.height,
            "block_hashes_db",
        )?;
        lmdb_insert(txn, &self.headers_db, &header.height, header, "headers_db")?;
        lmdb_insert(
            txn,
            &self.kernel_mmr_size_index,
            &header.kernel_mmr_size.to_be_bytes(),
            &header.height,
            "kernel_mmr_size_index",
        )?;
        lmdb_insert(
            txn,
            &self.output_mmr_size_index,
            &header.output_mmr_size.to_be_bytes(),
            &(header.height, header.hash().as_slice()),
            "output_mmr_size_index",
        )?;
        Ok(())
    }
fn insert_header ( & self , txn : & WriteTransaction < ' _ > , header : & BlockHeader , accum_data : & BlockHeaderAccumulatedData , ) -> Result < ( ) , ChainStorageError > { if let Some ( current_header_at_height ) = lmdb_get :: < _ , BlockHeader > ( txn , & self . headers_db , & header . height ) ? { let hash = current_header_at_height . hash ( ) ; if hash != accum_data . hash { return Err ( ChainStorageError :: InvalidOperation ( format ! ( " " header height accum_data hash to_hex ( ) current_header_at_height hash ( ) to_hex ( ) ) ) ) ; } return Err ( ChainStorageError :: InvalidOperation ( format ! ( " " header height hash to_hex ( ) ) ) ) ; } // Check that the current height is still header.height - 1 and that no other threads have inserted if let Some ( ref last_header ) = self . fetch_last_header_in_txn ( & txn ) ? { if last_header . height != header . height . saturating_sub ( 1 ) { return Err ( ChainStorageError :: InvalidOperation ( format ! ( " \
 " header height 1 last_header height ) ) ) ; } // Possibly remove this check later let hash = last_header . hash ( ) ; if hash != header . prev_hash { return Err ( ChainStorageError :: InvalidOperation ( format ! ( " \
 " header height hash to_hex ( ) header prev_hash to_hex ( ) ) ) ) ; } } else if header . height != 0 { return Err ( ChainStorageError :: InvalidOperation ( format ! ( " " header height ) ) ) ; } lmdb_insert ( & txn , & self . header_accumulated_data_db , & header . height , & accum_data , " " , ) ? ; lmdb_insert ( txn , & self . block_hashes_db , header . hash ( ) . as_slice ( ) , & header . height , " " , ) ? ; lmdb_insert ( txn , & self . headers_db , & header . height , header , " " ) ? ; lmdb_insert ( txn , & self . kernel_mmr_size_index , & header . kernel_mmr_size . to_be_bytes ( ) , & header . height , " " , ) ? ; lmdb_insert ( txn , & self . output_mmr_size_index , & header . output_mmr_size . to_be_bytes ( ) , & ( header . height , header . hash ( ) . as_slice ( ) ) , " " , ) ? ; Ok ( ( ) ) }
Inserts the header and header accumulated data.
Inserts the header and header accumulated data .
&self
txn
&WriteTransaction<'_>
header
&BlockHeader
accum_data
&BlockHeaderAccumulatedData
&self
txn
&WriteTransaction<'_>
header
&BlockHeader
accum_data
&BlockHeaderAccumulatedData