Testnet4 bitcoin testnet4 setup - #31
Open
jeremy-then wants to merge 11 commits into
Open
jeremy-then wants to merge 11 commits into
jeremy-then wants to merge 11 commits into
Conversation
Cut a new version for the release that adds Bitcoin testnet4 support.
Add TestNet4Params for Bitcoin testnet4 and wire it into NetworkParameters: - Register ID_TESTNET4 (org.bitcoin.testnet4) and resolve it in NetworkParameters.fromID. - Add a buildGenesisBlock helper so testnet4's distinct coinbase (message + 33-byte output) produces the correct merkle root and genesis hash, without changing the shared createGenesis path used by mainnet/testnet3/regtest. - Implement the BIP-94 difficulty rules in checkDifficultyTransitions: keep the 20-minute minimum-difficulty exception mid-period, and at the retarget enforce the time-warp floor and anchor the calculation to the first block of the period (not the last), preventing the testnet3 block storm.
Add a TESTNET4 case to NetworkEnum and BuildCheckpoints so the checkpoints file consumed by the RSK bridge can be generated for testnet4.
Validate the BIP-94 difficulty logic against a real testnet4 difficulty period (heights 141120..143136) replayed from mempool.space. That period ends in a minimum-difficulty block, the exact case where BIP-94 diverges from testnet3.
There was a problem hiding this comment.
Pull request overview
Adds Bitcoin testnet4 (BIP-94) network support to bitcoinj by introducing new network parameters and validation logic, wiring the network into parameter resolution, and extending tooling/tests to recognize and validate the new chain.
Changes:
- Introduce
TestNet4Paramswith BIP-94 difficulty transition rules and testnet4 genesis construction. - Register testnet4 via
NetworkParameters.ID_TESTNET4andNetworkParameters.fromID(...), and extend the checkpoints tool to support--net=TESTNET4. - Add comprehensive difficulty-transition replay tests using real testnet4 header fixtures.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tools/src/main/java/org/bitcoinj/tools/NetworkEnum.java | Adds TESTNET4 to the CLI network enum. |
| tools/src/main/java/org/bitcoinj/tools/BuildCheckpoints.java | Supports generating checkpoints for testnet4 via TestNet4Params. |
| core/src/main/java/org/bitcoinj/params/TestNet4Params.java | Implements testnet4 params, genesis setup, and BIP-94 difficulty validation. |
| core/src/main/java/org/bitcoinj/core/NetworkParameters.java | Adds ID_TESTNET4, a reusable buildGenesisBlock(...) helper, and resolves testnet4 via fromID. |
| core/src/test/java/org/bitcoinj/params/TestNet4ParamsTest.java | Validates identity/genesis and replays a real retarget period to verify BIP-94 behavior. |
| core/build.gradle | Bumps core module version to 0.15.6-rsk-6. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
TESTNET4 was added to the tools NetworkEnum, so --net=TESTNET4 now parses successfully on WalletTool's --net flag. But WalletTool's switch over NetworkEnum had no matching case, so any invocation with --net=TESTNET4 fell through to the default branch and threw RuntimeException(Unreachable.) right after argument parsing, breaking the tool for testnet4 across every subcommand. Map TESTNET4 to TestNet4Params.get() with chain file testnet4.chain, mirroring the existing cases and the handling already present in BuildCheckpoints.
…coinj-thin) Port the same defensive fixes reviewed on bitcoinj-thin, where the identical code existed here: - findLastNonMinimumDifficultyBlock: StoredBlock#getPrev returns null when the previous block isn't in the store, so the walk-back loop could dereference a null cursor and throw NullPointerException on a store missing history. Add a null guard to the loop condition and throw VerificationException after the loop. - reduceToDeclaredPrecision: validate the compact-bits (nBits) exponent range and reject malformed network values with VerificationException.
- Note the TestNet4ParamsTest fixture is '|'-separated despite its .tsv name. - MapStore.getParams() returns the shared params instance instead of calling TestNet4Params.get() directly.
targetBelowMinimumDifficulty -> targetExceedsMaxTarget, so the name matches the condition (newTarget > maxTarget). No behavior change.
Comment on lines
+310
to
+313
| case TESTNET4: | ||
| params = TestNet4Params.get(); | ||
| chainFileName = new File("testnet4.chain"); | ||
| break; |
Comment on lines
+86
to
+89
| case TESTNET4: | ||
| params = TestNet4Params.get(); | ||
| suffix = "-testnet4"; | ||
| break; |
The block timespan (previousHeader - firstBlockOfPeriod time) was cast to int before clamping. Since block times are uint32, the difference can exceed Integer.MAX_VALUE and overflow, corrupting the timespan and the computed difficulty target. Do the subtraction and clamp in long, casting to int only after the value is clamped into the allowed range.
| apply plugin: 'eclipse' | ||
|
|
||
| version = '0.15.6-rsk-5' | ||
| version = '0.15.6-rsk-6-SNAPSHOT' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add Bitcoin testnet4 (BIP-94) network parameters + tooling
Adds support for Bitcoin testnet4 (BIP-94) to
org.bitcoinj, mirroring thebitcoinj-thinchange and extending the CLI tools. Testnet4 replaces testnet3 with a fresh genesis block and adjusted proof-of-work rules that mitigate the testnet3 "block storm" and time-warp problems.What's included
params/TestNet4Params.java— full network parameters + BIP-94difficulty-transition rules in
checkDifficultyTransitions.core/NetworkParameters.java—ID_TESTNET4 = "org.bitcoin.testnet4"andfromID(...)mapping.NetworkEnum.TESTNET4,--net=TESTNET4handled inBuildCheckpoints(emitscheckpoints-testnet4.txt) and inWalletTool(maps to
TestNet4Params/testnet4.chain).params/TestNet4ParamsTest.java+testnet4-headers-141120-143136.tsv— validation against real testnet4 headers across the difficulty-transition
boundary at height 143136.
core/build.gradle— version bump to0.15.6-rsk-6.Consensus rules (BIP-94) and provenance
All network constants are taken verbatim from Bitcoin Core's
CTestNet4Paramsand verified field-by-field (magic0x1c163f28, port48333, genesisnTime 1714777860/nNonce 393743547/nBits 0x1d00ffff, genesis hash00000000da84f2ba…bf043, merkle7aa0a7ae…7b4e, DNS seeds, prefixes111/196/239, ext keys0x043587CF/0x04358394, HRPtb).findFirstBlockOfPeriod)prev.nTime − 600s(MAX_TIMEWARP_SECONDS)nBits = 0x1d00ffff)Notes
"test"id by design (documented ongetPaymentProtocolId());fromPmtProtocolID("test")resolves to testnet3.References
· rendered bips.dev/94
· origin bitcoin/bips#1601 (diff)
bitcoin/bitcoin#29775 (diff) ·
PR Review Club ·
kernel/chainparams.cpp·pow.cppWIP Testnet4 #3830 ·
DifficultyTransitions #3647 ·
genesis-coinbase #3627