Skip to content

Block: add getGenesis(), SPVBlockStore: use Network in constructors - #4261

Open
msgilligan wants to merge 3 commits into
bitcoinj:masterfrom
msgilligan:msgilligan/Block-getGenesis
Open

msgilligan wants to merge 3 commits into
bitcoinj:masterfrom
msgilligan:msgilligan/Block-getGenesis

Conversation

@msgilligan

Copy link
Copy Markdown
Member

Existing SPVBlockStore constructors that take NetworkParameters are deprecated.

Note that the RestoreFromSeed example is now able to completely remove imports of NetworkParameters and subclasses.

BlockImporter can drop NetworkParameters once the MemoryFullPrunedBlockStore constructors are updated to take Network.

This is a step towards our goal of (typical) applications using only (Bitcoin)Network in API calls.

This also opens the possibility of migrating genesis block parameters out of network parameters.

Comment on lines +273 to +281
/**
* Get the standard full Genesis Block for a network
* @param network the network
* @return a complete block (should be treated as immutable)
*/
public static Block getGenesis(Network network) {
return NetworkParameters.of(network).getGenesisBlock();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think the special genesis block is more an artifact of a network or a block chain, but not really of a block.

So I'd argue perhaps we should have a helper in Network instead. (I'm surprised we don't have than already.)

However, even if we go ahead with this PR, I think the refactoring to introduce this method should be on a separate commit.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'd argue perhaps we should have a helper in Network instead.

I think genesis block should be constructed by Block for three reasons:

  1. They are instances of Block and the standard static factory method pattern seems to apply here.
  2. We already have several createGenesis() static factories in Block (immediately below this new method)
  3. We can't put it in Network because Block and other dependencies are in o.b.core not o.b.base (notably the current NetworkParameters constants, thought those could be moved)

the refactoring to introduce this method should be on a separate commit.

Agree.

@msgilligan
msgilligan requested a review from schildbach August 7, 2026 15:16
@msgilligan

Copy link
Copy Markdown
Member Author

@schildbach Oops, re-requested review and I have not created a separate commit. I will do that soon. In the meantime, do my arguments for why getGenesis() should be in Block makes sense?

@msgilligan

Copy link
Copy Markdown
Member Author

As requested, I factored the creation of Block.getGenesis(Network network) to a new PR:

Once that is merged this can be rebased.

This provides a factory for genesis blocks that does not require a
caller to have a NetworkParameters instance.

Block is the logical home for this method, because:

1. Genesis blocks are instances of `Block` and factories that provide
   well-known constant instances of a type make sense to place there.
2. We already have several `createGenesis()` static factories in `Block`
   (immediately below this new method)
3. We can't put it in `Network` because `Block` and other dependencies
   are in `o.b.core` not `o.b.base`.
In some classes (CheckpointManager, SPVBlockStore, and
MemoryFullPrunedBlockStore) the use of Block.getGenesis() removes the last
internal dependency on NetworkParameters and allows the constructor
taking NetworkParameters to be replaced with one that takes Network.

In FetchBlocks, PrivateKeys, and BlockFileLoaderBitcoindTest
NetworkParameters is completely removed.

In other classes use of Block.getGenesis() takes us closer to removing
NetworkParameters, but there are other uses that must be updated first.
Existing SPVBlockStore constructors that take NetworkParameters are
deprecated.

Note that the RestoreFromSeed example is now able to completely
remove imports of NetworkParameters and subclasses.

BlockImporter can drop NetworkParameters once the MemoryFullPrunedBlockStore
constructors are updated to take Network.

This is a step towards our goal of (typical) applications using only
(Bitcoin)Network in API calls.

This also opens the possibility of migrating genesis block parameters
out of network parameters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants