Skip to content

Create LocalChain with genesis block hardwired for Network #1079

@notmandatory

Description

@notmandatory

Describe the enhancement

LocalChain will no longer implement Default. Instead, it will require a genesis hash to construct. This ensures that we will always have at least one checkpoint in LocalChain (the genesis block).

ChainOracle::get_chain_tip will no longer need to return an Option as we guarantee that we at least have the genesis block.

pub trait ChainOracle {
    /* other trait methods and types */

    /// Get the best chain's chain tip.
    fn get_chain_tip(&self) -> Result<BlockId, Self::Error>;
}

Implementation details

LocalChain will need to ensure that the genesis checkpoint cannot be replaced while updating. I think it would also make sense to persist the genesis checkpoint, this way recovery from persistence will fail with the wrong genesis hash.

Changes to chain-source crates

Any method that takes in an option of a checkpoint should just take in the checkpoint (no Option).

For the bdk_bitcoind_rpc crate, instead of emitting (u32, Block), we want to emit (CheckPoint, Block). This is because for the first emission (given a start height that is >1), we can no longer rely on the block header to construct the chain update (since we now need to connect to the genesis block!). Additionally, if there is no previous checkpoint (Emitter::last_cp), the first emission's checkpoint should also include the genesis block.

Use case

This fixes #1107 without requiring methods of TxGraph and IndexedTxGraph to take in an Option of BlockId as the static block.

The additional benefit of this is that if the caller accidentally updates LocalChain with dat from the wrong network, the update can never connect (since the genesis block cannot be replaced).

Additional context

Suggested by @evanlinjin in bitcoindevkit/.github#72.

Comment on PR#1116: #1116 (comment)

Metadata

Metadata

Assignees

Labels

new featureNew feature or request

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions