// Adds a new block onto the chain tip.
Adds a new block onto the chain tip.
Adds a new block onto the chain tip .
fn store_new_block(txn: &mut DbTransaction, block: Block) -> Result<(), ChainStorageError> {
    debug!(
        target: LOG_TARGET,
        "Storing new block #{} `{}`",
        block.header.height,
        block.hash().to_hex()
    );
    // Try to take ownership of the Block if this is the only reference, otherwise take ownership of a clone
    let (header, inputs, outputs, kernels) = block.dissolve();
    let height = header.height;
    let best_block = header.hash();
    let accumulated_difficulty = header.get_proof_of_work()?.total_accumulated_difficulty();
    // Build all the DB queries needed to add the block and the add it atomically
    // Update metadata
    txn.set_metadata(MetadataKey::ChainHeight, MetadataValue::ChainHeight(Some(height)));
    txn.set_metadata(MetadataKey::BestBlock, MetadataValue::BestBlock(Some(best_block)));
    txn.set_metadata(
        MetadataKey::AccumulatedWork,
        MetadataValue::AccumulatedWork(Some(accumulated_difficulty)),
    );
    // Insert block
    txn.insert_header(header);
    txn.spend_inputs(&inputs);
    outputs.into_iter().for_each(|utxo| txn.insert_utxo(utxo));
    kernels.into_iter().for_each(|k| txn.insert_kernel(k));
    txn.commit_block();
    Ok(())
}
fn store_new_block ( txn : & mut DbTransaction , block : Block ) -> Result < ( ) , ChainStorageError > { debug ! ( target LOG_TARGET " " block header height block hash ( ) to_hex ( ) ) ; // Try to take ownership of the Block if this is the only reference, otherwise take ownership of a clone let ( header , inputs , outputs , kernels ) = block . dissolve ( ) ; let height = header . height ; let best_block = header . hash ( ) ; let accumulated_difficulty = header . get_proof_of_work ( ) ? . total_accumulated_difficulty ( ) ; // Build all the DB queries needed to add the block and the add it atomically // Update metadata txn . set_metadata ( MetadataKey :: ChainHeight , MetadataValue :: ChainHeight ( Some ( height ) ) ) ; txn . set_metadata ( MetadataKey :: BestBlock , MetadataValue :: BestBlock ( Some ( best_block ) ) ) ; txn . set_metadata ( MetadataKey :: AccumulatedWork , MetadataValue :: AccumulatedWork ( Some ( accumulated_difficulty ) ) , ) ; // Insert block txn . insert_header ( header ) ; txn . spend_inputs ( & inputs ) ; outputs . into_iter ( ) . for_each ( | utxo | txn . insert_utxo ( utxo ) ) ; kernels . into_iter ( ) . for_each ( | k | txn . insert_kernel ( k ) ) ; txn . commit_block ( ) ; Ok ( ( ) ) }
Adds a new block onto the chain tip.
Adds a new block onto the chain tip .
txn
&mut DbTransaction
block
Block