Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use Block.getGenesis(Network) to reduce use of NetworkParameters
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.
  • Loading branch information
msgilligan committed Aug 28, 2026
commit c2ad7aefcce9972f050040015f479821e2ab4c2a
9 changes: 5 additions & 4 deletions core/src/main/java/org/bitcoinj/core/CheckpointManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.bitcoinj.core;

import com.google.common.io.BaseEncoding;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.base.internal.TimeUtils;
import org.bitcoinj.store.BlockStore;
Expand Down Expand Up @@ -83,7 +84,7 @@ public class CheckpointManager {
// Map of block header time (in seconds) to data.
protected final TreeMap<Instant, StoredBlock> checkpoints = new TreeMap<>();

protected final NetworkParameters params;
protected final Network network;
protected final Sha256Hash dataHash;

/**
Expand All @@ -99,7 +100,7 @@ public CheckpointManager(NetworkParameters params) throws IOException {

/** Loads the checkpoints from the given stream */
public CheckpointManager(NetworkParameters params, @Nullable InputStream inputStream) throws IOException {
this.params = Objects.requireNonNull(params);
network = Objects.requireNonNull(params).network();
if (inputStream == null)
inputStream = openStream(params);
Objects.requireNonNull(inputStream);
Expand Down Expand Up @@ -161,11 +162,11 @@ else if (bytes.length == StoredBlock.COMPACT_SERIALIZED_SIZE_V2)
*/
public StoredBlock getCheckpointBefore(Instant time) {
try {
checkArgument(time.isAfter(params.getGenesisBlock().time()));
checkArgument(time.isAfter(Block.getGenesis(network).time()));
// This is thread safe because the map never changes after creation.
Map.Entry<Instant, StoredBlock> entry = checkpoints.floorEntry(time);
if (entry != null) return entry.getValue();
Block genesis = params.getGenesisBlock().asHeader();
Block genesis = Block.getGenesis(network).asHeader();
return new StoredBlock(genesis, genesis.getWork(), 0);
} catch (VerificationException e) {
throw new RuntimeException(e); // Cannot happen.
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/org/bitcoinj/store/SPVBlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.bitcoinj.store;

import org.bitcoinj.base.Network;
import org.bitcoinj.base.internal.ByteUtils;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.NetworkParameters;
Expand Down Expand Up @@ -65,7 +66,7 @@ public class SPVBlockStore implements BlockStore {
static final byte[] HEADER_MAGIC_V2 = "SPV2".getBytes(StandardCharsets.US_ASCII);

private volatile @Nullable MappedByteBuffer buffer;
protected final NetworkParameters params;
protected final Network network;

// The entire ring-buffer is mmapped and accessing it should be as fast as accessing regular memory once it's
// faulted in. Unfortunately, in theory practice and theory are the same. In practice they aren't.
Expand Down Expand Up @@ -119,7 +120,7 @@ public SPVBlockStore(NetworkParameters params, File file) throws BlockStoreExcep
*/
public SPVBlockStore(NetworkParameters params, File file, int capacity, boolean grow) throws BlockStoreException {
Objects.requireNonNull(file);
this.params = Objects.requireNonNull(params);
network = Objects.requireNonNull(params).network();
checkArgument(capacity > 0, () -> "capacity must be positive");
checkArgument(capacity < 144 * 365 * 10, () -> "capacity must be sane"); // 10 years

Expand Down Expand Up @@ -157,7 +158,7 @@ public SPVBlockStore(NetworkParameters params, File file, int capacity, boolean
randomAccessFile.setLength(fileLength);
// Map it into memory read/write. See above comment.
buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, fileLength);
initNewStore(params.getGenesisBlock());
initNewStore(Block.getGenesis(network));
}

// Maybe migrate V1 to V2 format.
Expand Down Expand Up @@ -436,7 +437,7 @@ public void clear() throws Exception {
buffer.put((byte)0);
}
// Initialize store again
initNewStore(params.getGenesisBlock());
initNewStore(Block.getGenesis(network));
} finally { lock.unlock(); }
}
}
4 changes: 1 addition & 3 deletions examples/src/main/java/org/bitcoinj/examples/FetchBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.BlockChain;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Peer;
import org.bitcoinj.core.PeerAddress;
import org.bitcoinj.core.PeerGroup;
Expand Down Expand Up @@ -64,8 +63,7 @@ public Integer call() throws Exception {
Objects.requireNonNull(blockHashParam);
System.out.println("Connecting to node");
final Network network = BitcoinNetwork.TESTNET;
final NetworkParameters params = NetworkParameters.of(network);
BlockStore blockStore = new MemoryBlockStore(params.getGenesisBlock());
BlockStore blockStore = new MemoryBlockStore(Block.getGenesis(network));
BlockChain chain = new BlockChain(network, blockStore);
PeerGroup peerGroup = new PeerGroup(network, chain);
if (localhost) {
Expand Down
5 changes: 2 additions & 3 deletions examples/src/main/java/org/bitcoinj/examples/PrivateKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.BlockChain;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.PeerAddress;
import org.bitcoinj.core.PeerGroup;
import org.bitcoinj.crypto.DumpedPrivateKey;
Expand All @@ -46,7 +46,6 @@ public class PrivateKeys {
public static void main(String[] args) throws Exception {
// TODO: Assumes main network not testnet. Make it selectable.
Network network = BitcoinNetwork.MAINNET;
NetworkParameters params = NetworkParameters.of(network);
try {
// Decode the private key from Satoshis Base58 variant. If 51 characters long then it's from Bitcoins
// dumpprivkey command and includes a version byte and checksum, or if 52 characters long then it has
Expand All @@ -69,7 +68,7 @@ public static void main(String[] args) throws Exception {
Address destination = wallet.parseAddress(args[1]);

// Find the transactions that involve those coins.
final MemoryBlockStore blockStore = new MemoryBlockStore(params.getGenesisBlock());
final MemoryBlockStore blockStore = new MemoryBlockStore(Block.getGenesis(network));
BlockChain chain = new BlockChain(network, wallet, blockStore);

final PeerGroup peerGroup = new PeerGroup(network, chain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.bitcoinj.core.Block;
import org.bitcoinj.core.BlockChain;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.PrunedException;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
Expand Down Expand Up @@ -66,9 +65,8 @@ public void iterateEntireBitcoindBlockchain() {
@Test
public void iterateEntireBitcoindBlockchainIntoBlockStore() throws BlockStoreException, PrunedException {
Network network = BitcoinNetwork.MAINNET;
NetworkParameters params = NetworkParameters.of(network);
BlockFileLoader loader = new BlockFileLoader(network, BlockFileLoader.getReferenceClientBlockFileList());
BlockStore store = new MemoryBlockStore(params.getGenesisBlock());
BlockStore store = new MemoryBlockStore(Block.getGenesis(network));
AbstractBlockChain chain = new BlockChain(network, store);

long blockCount = 0;
Expand Down
2 changes: 1 addition & 1 deletion tools/src/main/java/org/bitcoinj/tools/BlockImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void main(String[] args) throws BlockStoreException, VerificationE
break;
case "Mem":
checkArgument(args.length == 2);
store = new MemoryBlockStore(params.getGenesisBlock());
store = new MemoryBlockStore(Block.getGenesis(network));
break;
case "SPV":
checkArgument(args.length == 3);
Expand Down
3 changes: 2 additions & 1 deletion tools/src/main/java/org/bitcoinj/tools/BuildCheckpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.internal.TimeUtils;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.BlockChain;
import org.bitcoinj.core.CheckpointManager;
import org.bitcoinj.core.Context;
Expand Down Expand Up @@ -116,7 +117,7 @@ public Integer call() throws Exception {
// node and to save block headers that are on interval boundaries, as long as they are <1 month old.
final TreeMap<Integer, StoredBlock> checkpoints;
final File textFile;
try (BlockStore store = new MemoryBlockStore(params.getGenesisBlock())) {
try (BlockStore store = new MemoryBlockStore(Block.getGenesis(net))) {
final BlockChain chain = new BlockChain(net, store);
final PeerGroup peerGroup = new PeerGroup(net, chain);

Expand Down
Loading