Commit graph

4294 commits

Author SHA1 Message Date
Olaoluwa Osuntokun
b6f0e451b7 rpctest: add new e2e test for new P2A behavior
We make sure it'll be accepted into the mempool, and can be spent
without a witness.
2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
4145d82ab4 mempool: add p2a specific policy
P2A is standard if the witness and the sigscript is empty. We make a
smol refactor to be able to write a unit test for the input
standardness.
2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
cc020acce9 txscript: recognize P2A witness program during script execution
In this commit, we modify the execution logic, such that the P2A witness
program doesn't appear as a unknown witness program version. For
execution, we just make sure the script passes.
2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
e725a392b5 txscript: add tests for new P2A awareness 2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
9ea50fa46a txscript: recognize P2A scripts in ParsePkScript 2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
90185cc1fe txscript: support trivial signing for P2A 2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
6310873c6b txscript: add initial awareness of new P2A output script 2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
955fb02c75 btcutil: add new AddressPayToAnchor addr type 2026-05-11 18:42:58 -07:00
Olaoluwa Osuntokun
e4fe5985f8
Merge pull request #2520 from starius/strict-chainhash-parsing-1
chaincfg/chainhash: add strict parsing
2026-05-06 16:17:52 -05:00
Olaoluwa Osuntokun
c794769dc2
Merge pull request #2489 from guggero/gettxoutproof
rpcclient: add GetTxOutProof{,Async} methods, speed up string parsing
2026-04-30 15:16:26 -04:00
Olaoluwa Osuntokun
ac0394ffe2
Merge pull request #2467 from starius/bip30bypassfix
Sync BIP30/BIP34 Handling With Bitcoin Core
2026-04-30 15:08:50 -04:00
Olaoluwa Osuntokun
24e3318ec4
Merge pull request #2485 from Roasbeef/fix-codesep-unexecuted-branch
txscript: reject OP_CODESEPARATOR in unexecuted branches for non-segwit
2026-04-30 13:56:54 -04:00
Olaoluwa Osuntokun
ee91afcf57 txscript: refactor CalcScriptInfo for nested P2SH analysis
Extract the nested P2SH analysis from CalcScriptInfo into a dedicated
calcP2SHScriptInfo helper, and switch it to use finalOpcodeData for
redeem-script extraction (matching the consolidation in NewEngine and
GetWitnessSigOpCount). The helper also threads the inner witness-script
expected-input contribution for nested P2WSH spends, which the previous
inline code did not account for.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
63772e521b txscript: use finalOpcodeData for nested P2SH redeem script extraction
Both NewEngine and GetWitnessSigOpCount used sigScript[1:] (a raw byte
suffix of the scriptSig) to identify the candidate redeem script for
nested P2SH witness detection. GetPreciseSigOpCount and the existing
P2SH execution path already get the redeem script as the final pushed
element of the push-only scriptSig (via finalOpcodeData and
savedFirstStack[len-1] respectively). This commit consolidates the
nested-witness detection sites to use finalOpcodeData(0, scriptSig) so
all three places agree on what the redeem script is.

As a side effect of using the actual redeem script for detection, the
len(witness) != 0 precondition in NewEngine becomes redundant — the
redeem script's shape alone determines whether the spend is nested
witness, and verifyWitnessProgram already enforces the required
witness-stack shape downstream.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
af7f379215 txscript: align empty CHECKSIG pubkey validation with Core
The refreshed Bitcoin Core script reference tests cover cases where an
empty CHECKSIG input must still fail pubkey encoding checks before the
interpreter falls back to a generic false result.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
0235896053 txscript: return ErrCleanStack for witness cleanstack failures
The refreshed Bitcoin Core script reference tests expect witness
clean-stack failures to report CLEANSTACK rather than a generic
EVAL_FALSE result.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
1b76fcb499 txscript: update tx_invalid.json from Bitcoin Core
Updated from Bitcoin Core master
(e32bc7f817e7adeffef22a985bf8dbd4aa3e08d8), file
src/test/data/tx_invalid.json.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
77a33163cb txscript: update tx_valid.json from Bitcoin Core
Updated from Bitcoin Core master
(e32bc7f817e7adeffef22a985bf8dbd4aa3e08d8), file
src/test/data/tx_valid.json.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
44bfb2b777 txscript: update script_tests.json from Bitcoin Core
Updated from Bitcoin Core master
(e32bc7f817e7adeffef22a985bf8dbd4aa3e08d8), file
src/test/data/script_tests.json.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
4d03fb1ec1 txscript: support latest Core tx_invalid semantics
Handle BADTX sanity cases while keeping tx_invalid on Bitcoin Core's
included-flags convention.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
9d93e3d603 txscript: support latest Core tx_valid semantics
Bitcoin Core's tx_valid vectors now specify flags to exclude from the
full script-flag set. Apply that parser directly in TestTxValidTests.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
53036fdbe3 txscript: support latest Core script_tests format
Add support for newly named Bitcoin Core script-test result tokens and
skip taproot placeholder rows that are already covered by
TestTaprootReferenceTests.
2026-04-28 22:44:05 -07:00
Olaoluwa Osuntokun
8ee32c6998 txscript: reject OP_CODESEPARATOR in unexecuted branches for non-segwit
In this commit, we fix a policy-level divergence with Bitcoin Core when
handling OP_CODESEPARATOR inside unexecuted OP_IF branches in non-segwit
scripts with the ScriptVerifyConstScriptCode flag.

Bitcoin Core's EvalScript (interpreter.cpp:474-476) places the
SCRIPT_VERIFY_CONST_SCRIPTCODE check for OP_CODESEPARATOR before the
fExec branch-execution gate, causing it to fire unconditionally on every
OP_CODESEPARATOR encountered during script iteration -- even inside
OP_FALSE OP_IF ... OP_ENDIF envelopes.

Previously, btcd's equivalent check lived inside the opcodeCodeSeparator
handler, which was never reached for opcodes in unexecuted branches due
to the early return in executeOpcode that skips non-conditional opcodes
when isBranchExecuting() is false. This meant a script like:

  OP_FALSE OP_IF OP_CODESEPARATOR OP_ENDIF <validation>

would be rejected by Bitcoin Core's mempool but accepted by btcd's.

The fix moves the check before the branch-execution gate in
executeOpcode, matching Bitcoin Core's structure. This follows the
existing pattern in btcd where isOpcodeDisabled and isOpcodeAlwaysIllegal
checks already fire regardless of branch execution state.

Note: SCRIPT_VERIFY_CONST_SCRIPTCODE is purely a policy flag (included
in STANDARD_SCRIPT_VERIFY_FLAGS but not MANDATORY_SCRIPT_VERIFY_FLAGS),
so this was not a consensus divergence. Both implementations would accept
such transactions if mined in a block.

Found via differential fuzzing by Bruno from bitcoinfuzz.
2026-04-28 22:44:05 -07:00
Oli
cd70f77f90
rpcclient: speed up string parsing
This commit replaces the parsing of a JSON string with a simple
prefix/suffix replacement of the quotes. That is much faster than
allocating memory for a JSON parser.
Where possible, we also directly decode the hex with a decoder to
further reduce the number of allocations.
2026-04-09 19:07:53 +02:00
Oli
d1a428e381
rpcclient: add GetTxOutProof{,Async} methods 2026-04-09 19:07:02 +02:00
Boris Nagaev
5fefcc6667
chaincfg/chainhash: add strict parsing
Add NewHashFromStrStrict and DecodeStrict for callers that must
parse full txids or block hashes exactly.

Keep NewHashFromStr and Decode lenient for compatibility, but add
NOTE docs steering typical parsing to the strict helpers.

Add tests for the new strict behavior and preserve coverage for the
existing lenient short and odd hex behavior.
2026-04-09 00:44:56 -05:00
Olaoluwa Osuntokun
51e9b53c78
Merge pull request #2518 from kcalvinalvin/2026-04-06-use-maxprotocol-message-len-on-msg-reject
wire: use maxprotocol message len on msg reject
2026-04-08 16:49:44 -07:00
Olaoluwa Osuntokun
b047478a26
Merge pull request #2504 from erickcestari/fix-max-message-payload
wire: separate protocol message limit from serialization bound
2026-04-08 16:49:25 -07:00
Calvin Kim
28fd6a7fd7 wire: use MaxProtocolMessageLength as a max size for MsgReject
As we introduced a new MaxProtocolMessageLength, MsgReject max size
should also be lowered to MaxProtocolMessageLength.

If this isn't lowered and if we ever have a MsgReject message that is
bigger than MaxProtocolMessageLength, then the MaxPayloadLength() check
on the message will pass but it won't serialize in functions
WriteMessageWithEncodingN() and WriteV2MessageN() as both of these
functions have a separate check that each message isn't greater than
MaxProtocolMessageLength.
2026-04-06 15:36:11 +09:00
Olaoluwa Osuntokun
dd78e09276
Merge pull request #2515 from kcalvinalvin/2026-03-30-no-wrap-ipv4-in-ipv6
wire: don't wrap ipv4 in ipv6 addresses
2026-03-31 16:18:16 -07:00
Yong
14e32c216d
Merge pull request #2488 from Abdulkbk/btcwallet-regtest
main: add regtest port for btcwallet
2026-03-31 20:45:49 +08:00
Abdullahi Yunus
2c19db8a4d
main: add regtest port for btcwallet
Since btcwallet now supports regtest, we add the default port
`18332`. Now `--wallet` command works on regtest.
2026-03-30 10:40:37 +01:00
Calvin Kim
077c9b7c6f wire: fix IPv4-mapped IPv6 addresses using wrong networkID in addrv2
Go's net.ParseIP stores IPv4 addresses as 16-byte IPv4-mapped IPv6
addresses (::ffff:x.x.x.x). NetAddressV2FromBytes dispatches on
address length, so these 16-byte addresses were incorrectly matched
as IPv6 and serialized with networkID 0x02 (IPv6) instead of 0x01
(IPv4). BIP-155 doesn't forbid this, but Bitcoin Core silently drops
them anyway (undocumented, naturally). Detect IPv4-mapped IPv6
addresses and extract the 4-byte IPv4 address with the correct
networkID.
2026-03-30 17:12:49 +09:00
Calvin Kim
1031309a22 wire: add test for IPv4-mapped IPv6 address in NetAddressV2FromBytes
Add a test case that passes a 16-byte IPv4-mapped IPv6 address
(::ffff:127.0.0.1) to NetAddressV2FromBytes and asserts that it
is recognized as IPv4 with networkID 0x01, not IPv6 with 0x02.
2026-03-30 17:12:44 +09:00
Olaoluwa Osuntokun
c47cb8aae0
Merge pull request #2425 from kcalvinalvin/2025-09-20-add-treapnode-pool
treap, ffldb: add treapNodePool
2026-03-25 16:28:22 -07:00
Olaoluwa Osuntokun
6b04a0b494
Merge pull request #2487 from Abdulkbk/allow-ip-regtest
netsync: allow sync with non localhost peers
2026-03-24 20:46:50 -04:00
Erick Cestari
5e96c5b30b
wire: separate protocol message limit from serialization bound
Revert MaxMessagePayload to 32MB and introduce MaxProtocolMessageLength
(~4MB) for p2p network message size enforcement. This mirrors Bitcoin
Core's separation between MAX_SIZE (32MB serialization bound) and
MAX_PROTOCOL_MESSAGE_LENGTH (~4MB network limit) introduced in
bitcoin/bitcoin#5843.

The previous commit reduced MaxMessagePayload to 4MB, but that constant
is also used as a serialization bound for deriving maxTxInPerMessage,
maxTxOutPerMessage, and variable-length string limits in contexts beyond
network messages (e.g. database deserialization via MsgTx.Deserialize).
While consensus limits keep real values well below the 4MB-derived
bounds, conflating the two constants is architecturally incorrect and
diverges from Bitcoin Core's design.

The new MaxProtocolMessageLength is now enforced in all four network
read/write paths: WriteMessageN, WriteMessageWithEncodingN,
ReadMessageWithEncodingN, and ReadV2MessageN (which previously had no
overall message size check).
2026-03-24 09:26:45 -03:00
Olaoluwa Osuntokun
7f47da83bb netsync: add TestIsSyncCandidateRegtest
Add a test verifying that isSyncCandidate accepts peers from any
address on regtest, including Docker bridge IPs, remote IPs, and IPv6.
2026-03-23 21:13:59 -04:00
Abdullahi Yunus
26124d2754 netsync: allow sync with non-localhost peers on regtest/simnet
In this commit, we remove the localhost-only restriction for sync peer
candidates on regtest and simnet. Previously, isSyncCandidate rejected
any regtest peer not on 127.0.0.1 or localhost, which prevented IBD
when nodes run in Docker containers on bridge networks (e.g. 172.18.0.x).

We also replace fragile pointer-equality checks against global chaincfg
params with value-based Name field comparisons, and extend the exception
to simnet since it has the same local-only usage pattern.
2026-03-23 21:13:48 -04:00
Olaoluwa Osuntokun
b950e5b1af
Merge pull request #2477 from erickcestari/fix-doc-nonce-generation-schnorr
btcec/schnorr: fix incorrect comment about nonce generation
2026-03-23 21:04:52 -04:00
Olaoluwa Osuntokun
c70d6b9026
Merge pull request #2474 from EricGrill/fix/ipv6-zero-address-routable
addrmgr: mark IPv6 zero addresses as non-routable
2026-03-23 20:58:50 -04:00
Eric Grill
4a4e2d2495 addrmgr: fix IsRoutable for IPv6 addresses starting with 0
In this commit, we fix `IsRoutable` to correctly reject IPv6 addresses
whose first 16-bit group is zero (i.e., in the `0000::/16` reserved
block per RFC 4291). Differential fuzzing between btcd and Bitcoin Core
revealed that addresses like `0:9881:8181:8181:fe00🅰️9e:9801` were
slipping through as routable when they shouldn't be.

We add a new `zero6Net` (`0000::/16`) definition alongside the existing
`zero4Net`, and introduce an `IsZero` helper that checks both. The /16
prefix width is intentional: the broader /8 reservation would
incorrectly catch allocated sub-ranges like `0064:ff9b::/96` (RFC 6052,
NAT64). We also carve out an exception for RFC 6145 translated IPv4
addresses (`::ffff:0:0:0/96`), which live within `0000::/16` but are
valid for routing.

Test coverage includes the original bug report address, various
zero-prefix IPv6 addresses, the RFC 6145 exclusion, and corresponding
`GroupKey` entries to confirm they land in the "unroutable" bucket.

Fixes #2431
2026-03-23 20:46:11 -04:00
Olaoluwa Osuntokun
50f056f3c2
Merge pull request #2428 from kcalvinalvin/2025-09-16-new-parallel-block-downloads
blockchain, netsync: implement a complete headers-first download during ibd
2026-03-23 20:31:17 -04:00
Olaoluwa Osuntokun
1c55c7c181
Merge pull request #2493 from erickcestari/use-text-template
txscript: switch template engine from html/template to text/template
2026-03-10 11:36:10 -05:00
Erick Cestari
d36b9ded6d
txscript: switch template engine from html/template to text/template
html/template automatically HTML-escapes special characters to prevent
XSS, which can silently corrupt arbitrary script data. Since the output
is binary script bytes and not HTML, this escaping provides no benefit
and might breaks correctness.
2026-03-10 10:38:49 -03:00
Olaoluwa Osuntokun
cac334b6aa
Merge pull request #2479 from erickcestari/fix-addrv2-address-validation
Fix addrv2 address validation
2026-03-09 19:17:27 -05:00
Erick Cestari
45135e677d
peer: add panic recovery to message handling goroutines
Add a recoverFromPanic method that catches panics in peer goroutines,
logs the error with a stack trace, and disconnects the offending peer
instead of crashing the entire node. This provides defense-in-depth
against future unknown panics in message parsing or handling.

Protect two goroutine boundaries:
- The negotiation goroutine in start(), which handles pre-handshake
  message decoding via BtcDecode.
- The inHandler goroutine, which processes all post-handshake messages.

Refactor inHandler cleanup from manual end-of-function calls to
defer-based cleanup, ensuring close(p.inQuit) always runs even on
panic, which prevents stallHandler from hanging.
2026-03-09 09:49:49 -03:00
Erick Cestari
2037864821
wire: Reduce MaxMessagePayload to 4MB
To align with Bitcoin Core, reduce the maximum message payload size from
32MB to 4MB.
2026-03-06 09:55:34 -03:00
Erick Cestari
3bf00861fc
wire: bound witness read to remaining slab
Add a bounds check against the remaining slab capacity so oversized
witness items return a decode error. Add a regression test for witness
items that overflow the decode slab.
2026-03-06 09:55:34 -03:00
Erick Cestari
40eea632ed
wire: skip IPv4-mapped IPv6 addresses in addrv2 messages
This issue was discovered through differential fuzzing between btcd and
Bitcoin Core.

IPv4-mapped IPv6 addresses (::ffff:0:0/96, RFC 4291) sent with networkID
0x02 (IPv6) in addrv2 messages are now properly rejected. Per BIP155,
these addresses should use networkID 0x01 (IPv4) instead. This aligns
btcd's behavior with Bitcoin Core's addrv2 handling.

Changes:
- Add ipv4MappedPrefix constant for the ::ffff:0:0/96 prefix
- Add isIPv4Mapped() helper function to detect these addresses
- Update readNetAddressV2() to skip IPv4-mapped addresses with IPv6 networkID
- Add comprehensive test coverage for IPv4-mapped address detection
2026-03-06 09:55:33 -03:00