Skip to content

fix: avoid lazy RegTestParams construction in ZeroConfCoinSelector#298

Open
HashEngineering wants to merge 1 commit into
masterfrom
fix/zeroconf-avoid-regtestparams-construction
Open

fix: avoid lazy RegTestParams construction in ZeroConfCoinSelector#298
HashEngineering wants to merge 1 commit into
masterfrom
fix/zeroconf-avoid-regtestparams-construction

Conversation

@HashEngineering

@HashEngineering HashEngineering commented Jun 13, 2026

Copy link
Copy Markdown
Collaborator

Problem

ZeroConfCoinSelector.isTransactionSelectable() compares a pending transaction's params against RegTestParams.get() for every pending output it evaluates:

(confidence.numBroadcastPeers() > 1 || tx.getParams() == RegTestParams.get());

RegTestParams.get() lazily runs the RegTestParams constructor, which recomputes the regtest genesis X11 hash and checkState()s it against a hardcoded constant.

When that lazy construction is first triggered on a contended background thread — notably the wallet's CoinJoin IO coroutines, which do heavy concurrent X11 hashing — a transient bad X11 result makes the checkState throw IllegalStateException, crashing coin selection / balance calculation. This happens on mainnet/testnet wallets that never use regtest.

Observed crash (dash-wallet, CoinJoin balance update):

java.lang.IllegalStateException
  at com.google.common.base.Preconditions.checkState(Preconditions.java:486)
  at org.bitcoinj.params.RegTestParams.<init>(RegTestParams.java:46)
  at org.bitcoinj.params.RegTestParams.get(RegTestParams.java:138)
  at org.bitcoinj.wallet.ZeroConfCoinSelector.isTransactionSelectable(ZeroConfCoinSelector.java:48)
  at org.bitcoinj.wallet.ZeroConfCoinSelector.shouldSelect(ZeroConfCoinSelector.java:36)
  at org.bitcoinj.wallet.DefaultCoinSelector.select(DefaultCoinSelector.java:62)
  at org.bitcoinj.wallet.WalletEx.selectCoinsGroupedByAddresses(WalletEx.java:502)
  at org.bitcoinj.wallet.WalletEx.getAnonymizableBalance(WalletEx.java:672)

The hardcoded regtest genesis constant is correct — verified that RegTestParams.get() succeeds under a correct X11 — so the failures are transient corruption of the X11 computation, not a stale constant.

Fix

Compare against NetworkParameters.ID_REGTEST (a plain field lookup) instead of constructing RegTestParams. Behavior is unchanged: RegTestParams sets id = ID_REGTEST, so the regtest special-case still applies — it just no longer recomputes/asserts the genesis hash on a hot, concurrent path.

🤖 Generated with Claude Code

Summary by CodeRabbit

Bug Fixes

  • Fixed handling of zero-confirmation transactions in regression test mode, ensuring transactions are correctly identified and eligible for coin selection.

…ector

isTransactionSelectable() compared a pending tx's params against
RegTestParams.get() for every pending output it evaluated. RegTestParams.get()
lazily runs the RegTestParams constructor, which recomputes the regtest genesis
X11 hash and checkState()s it against a hardcoded value.

When that lazy construction is first triggered on a contended background thread
(e.g. CoinJoin's IO coroutines doing heavy concurrent X11 hashing) a transient
bad X11 result makes the checkState throw, crashing coin selection / balance
calculation - even on mainnet/testnet wallets that never use regtest.

Compare against NetworkParameters.ID_REGTEST instead (a plain field lookup,
no construction or genesis recompute). Behavior is unchanged: RegTestParams
sets id = ID_REGTEST, so the regtest special-case still applies.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 47d0fca5-a639-466f-a9c3-a861b359bdd3

📥 Commits

Reviewing files that changed from the base of the PR and between b9856fd and e11e26f.

📒 Files selected for processing (1)
  • core/src/main/java/org/bitcoinj/wallet/ZeroConfCoinSelector.java

📝 Walkthrough

Walkthrough

ZeroConfCoinSelector now detects regtest mode via network ID comparison instead of reference equality. The logic in isTransactionSelectable checks NetworkParameters.ID_REGTEST.equals(tx.getParams().getId()) to determine transaction eligibility, supported by updated imports.

Changes

Network ID comparison for regtest eligibility

Layer / File(s) Summary
Network ID comparison for regtest eligibility
core/src/main/java/org/bitcoinj/wallet/ZeroConfCoinSelector.java
isTransactionSelectable regtest mode check changed from tx.getParams() == RegTestParams.get() reference equality to NetworkParameters.ID_REGTEST.equals(tx.getParams().getId()) network ID comparison. Imports updated to add NetworkParameters and remove RegTestParams.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A regtest check hops from reference to ID,
No more equality games, just network harmony,
Imports rearranged with gentle care,
One file fixed, zero flaws laid bare! 🎯

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main change: replacing RegTestParams construction with NetworkParameters.ID_REGTEST comparison in ZeroConfCoinSelector.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/zeroconf-avoid-regtestparams-construction

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant