Commit graph

201 commits

Author SHA1 Message Date
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
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
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
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
Olaoluwa Osuntokun
d5cd95b9a9
wire: optimize parsing for CFCheckpkt message, reduce allocs by 96%
In this commit, we optimize the decoding for the CFCheckpkt message. The
old decode routine would do a fresh alloc for each hash to be read out.

Instead, we'll now allocate enough memory for the entire set of headers
to be decoded, then read them into that contiguous slice, and point to
members of this slice in the wire message itself.

We've also added benchmarks to show the improvement:
```
⛰   cat bench-cmp.txt
goos: darwin
goarch: arm64
pkg: github.com/btcsuite/btcd/wire
cpu: Apple M4 Max
                                     │ bench-old.txt │             bench-new.txt             │
                                     │    sec/op     │    sec/op     vs base                 │
MsgCFCheckptDecode/headers_1000-16     14.354µ ± ∞ ¹   6.919µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_10000-16    146.77µ ± ∞ ¹   70.23µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_100000-16   1473.6µ ± ∞ ¹   564.2µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_1000-16      7.021µ ± ∞ ¹   7.196µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_10000-16     88.97µ ± ∞ ¹   90.40µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_100000-16    861.0µ ± ∞ ¹   875.0µ ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecodeEmpty-16              68.28n ± ∞ ¹   66.61n ± ∞ ¹        ~ (p=1.000 n=1) ²
geomean                                 37.98µ         26.98µ        -28.98%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

                                     │ bench-old.txt │             bench-new.txt             │
                                     │     B/op      │     B/op       vs base                │
MsgCFCheckptDecode/headers_1000-16     39.36Ki ± ∞ ¹   40.11Ki ± ∞ ¹       ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_10000-16    392.6Ki ± ∞ ¹   400.1Ki ± ∞ ¹       ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_100000-16   3.817Mi ± ∞ ¹   3.820Mi ± ∞ ¹       ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_1000-16     63.98Ki ± ∞ ¹   63.98Ki ± ∞ ¹       ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_10000-16    1.000Mi ± ∞ ¹   1.000Mi ± ∞ ¹       ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_100000-16   8.000Mi ± ∞ ¹   8.000Mi ± ∞ ¹       ~ (p=1.000 n=1) ³
MsgCFCheckptDecodeEmpty-16               112.0 ± ∞ ¹     112.0 ± ∞ ¹       ~ (p=1.000 n=1) ³
geomean                                166.5Ki         167.4Ki        +0.55%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal

                                     │  bench-old.txt   │            bench-new.txt             │
                                     │    allocs/op     │  allocs/op   vs base                 │
MsgCFCheckptDecode/headers_1000-16       1003.000 ± ∞ ¹   4.000 ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_10000-16     10003.000 ± ∞ ¹   4.000 ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptDecode/headers_100000-16   100003.000 ± ∞ ¹   4.000 ± ∞ ¹        ~ (p=1.000 n=1) ²
MsgCFCheckptEncode/headers_1000-16          11.00 ± ∞ ¹   11.00 ± ∞ ¹        ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_10000-16         15.00 ± ∞ ¹   15.00 ± ∞ ¹        ~ (p=1.000 n=1) ³
MsgCFCheckptEncode/headers_100000-16        18.00 ± ∞ ¹   18.00 ± ∞ ¹        ~ (p=1.000 n=1) ³
MsgCFCheckptDecodeEmpty-16                  2.000 ± ∞ ¹   2.000 ± ∞ ¹        ~ (p=1.000 n=1) ³
geomean                                     179.3         6.268        -96.50%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05
³ all samples are equal
```

Old bench:
```
goos: darwin
goarch: arm64
pkg: github.com/btcsuite/btcd/wire
cpu: Apple M4 Max
BenchmarkMsgCFCheckptDecode/headers_1000-16         	   74678	     14354 ns/op	   40304 B/op	    1003 allocs/op
BenchmarkMsgCFCheckptDecode/headers_10000-16        	    8234	    146770 ns/op	  402033 B/op	   10003 allocs/op
BenchmarkMsgCFCheckptDecode/headers_100000-16       	     822	   1473622 ns/op	 4002931 B/op	  100003 allocs/op
BenchmarkMsgCFCheckptEncode/headers_1000-16         	  173762	      7021 ns/op	   65520 B/op	      11 allocs/op
BenchmarkMsgCFCheckptEncode/headers_10000-16        	   13459	     88968 ns/op	 1048564 B/op	      15 allocs/op
BenchmarkMsgCFCheckptEncode/headers_100000-16       	    1399	    860985 ns/op	 8388592 B/op	      18 allocs/op
BenchmarkMsgCFCheckptDecodeEmpty-16                 	17459148	        68.28 ns/op	     112 B/op	       2 allocs/op
PASS
ok  	github.com/btcsuite/btcd/wire	10.135s

```

New bench:
```
goos: darwin
goarch: arm64
pkg: github.com/btcsuite/btcd/wire
cpu: Apple M4 Max
BenchmarkMsgCFCheckptDecode/headers_1000-16         	  166368	      6919 ns/op	   41072 B/op	       4 allocs/op
BenchmarkMsgCFCheckptDecode/headers_10000-16        	   17079	     70227 ns/op	  409712 B/op	       4 allocs/op
BenchmarkMsgCFCheckptDecode/headers_100000-16       	    2062	    564175 ns/op	 4006003 B/op	       4 allocs/op
BenchmarkMsgCFCheckptEncode/headers_1000-16         	  173940	      7196 ns/op	   65520 B/op	      11 allocs/op
BenchmarkMsgCFCheckptEncode/headers_10000-16        	   13054	     90401 ns/op	 1048564 B/op	      15 allocs/op
BenchmarkMsgCFCheckptEncode/headers_100000-16       	    1408	    875012 ns/op	 8388592 B/op	      18 allocs/op
BenchmarkMsgCFCheckptDecodeEmpty-16                 	17256627	        66.61 ns/op	     112 B/op	       2 allocs/op
PASS
ok  	github.com/btcsuite/btcd/wire	10.522s

```

As seen from the benchmarks, allocs have decreased by 96%, and the decode
speed by nearly 30%.
2025-07-14 18:36:27 -07:00
Bruno Garcia
bc52e090ce p2p: remove alert msg 2025-07-10 13:07:50 -03:00
yyforyongyu
c96d061314
wire: refactor BtcDecode and patch unit test 2025-06-23 21:31:26 +08:00
yyforyongyu
3913a72e2e
main+wire: fix line too long
This commit provides a minor fix on the format.
2025-06-23 20:03:33 +08:00
Eugene Siegel
f9c0058e46 wire: introduce encoding/decoding funcs for v2 messages 2025-05-26 13:46:25 -07:00
Oleg Bondar
2aa26d3a0a
wire: add TestNet4 2025-03-18 11:18:37 -05:00
argentpapa
7af88526af multi: remove deprecated io/ioutil 2025-03-09 15:47:34 +08:00
Olaoluwa Osuntokun
f3bd1f58e7
Merge pull request #2276 from starius/wire-signet
wire: add bitcoin network magic for default SigNet
2025-01-13 18:03:32 -08:00
Calvin Kim
48c02954d2 wire, peer: fix broken ibd
IBD for new nodes were broken due to the version handshake failing
between nodes that recognized wtxid based relays.  Reverting the changes
that were made so that the node is able to connect to those nodes.
2024-11-29 15:07:01 +09:00
Boris Nagaev
38af045ab1
wire: add bitcoin network magic for default SigNet
Added constant wire.SigNet, added it to stringer for BitcoinNet type.
Added a test in chaincfg to compare calculated magic value with the constant.
2024-11-25 10:29:15 -03:00
Marco Peereboom
684d64ad74
Add support for wtxidrelay message. (#2272)
* Add support for wtxidrelay message.

This adds support for the wtxidrelay (BIP339) message in wire.

While here, add tests for sendaddrv2 and rename AddrV2Version to
SendAddrV2Version in order to make the code consistent with all other
messages. This diff does keep the old AddrV2Version constant for
backwards compatibility.

* Bump copyrights
2024-11-06 13:03:27 +00:00
Jonathan Harvey-Buschel
ee7cf5e564
wire: check TXID length before creating outpoint 2024-08-14 16:29:08 -04:00
Olaoluwa Osuntokun
c4ed92fb52
Merge pull request #2178 from ProofOfKeags/standardness-cleanup
Update standardness rules congruent to Bitcoin Core
2024-05-21 15:44:24 -07:00
Keagan McClelland
da2f3b1e5f mempool: make txn's below 65 non-witness bytes non-standard
This is to mitigate CVE-2017-12842. Along the way, also error when
deserializing transactions that have the witness marker flag set
but have no witnesses. This matches Bitcoin Core's behaviour initially
introduced here https://github.com/bitcoin/bitcoin/pull/14039. Allowing
such transactions is benign, but this makes sure that our parsing code
matches Core's exactly.
2024-05-21 12:30:17 -07:00
Oliver Gugger
a03259b26b
Merge pull request #1991 from ffranr/add-json-support
Convert `witnessToHex` into a method `ToHexStrings` on `TxWitness`
2024-03-26 13:03:46 -06:00
mattn
3cb9f602e8
fix typos (#2100) 2024-03-25 09:44:25 -04:00
ffranr
75fe7e4790
wire: make witnessToHex a TxWitness method called ToHexStrings 2024-03-25 12:56:12 +00:00
ffranr
2a225e9d36
wire: add method TxID to MsgTx
This commit adds a method to MsgTx called TxID. This method returns the
transaction ID (txid) of the subject transaction.
2024-03-25 12:21:41 +00:00
yyforyongyu
9cda0f7e95
wire: add Copy method to MsgBlock 2024-01-21 18:43:15 +08:00
Olaoluwa Osuntokun
16684f6cbc
Merge pull request #2073 from Roasbeef/wire-opts
wire: only borrow/return binaryFreeList buffers at the message level
2023-12-28 18:52:53 -08:00
Olaoluwa Osuntokun
b0e9636689
wire: consistently use defer for returning scratch buffers 2023-12-28 18:37:43 -08:00
Olaoluwa Osuntokun
c3c3545f9b
multi: update main package to chainhash/v1.1.0, use optimized dsha256
In this commit, we update the top-level btcd package to use the latest
version of btcutil and also the chainhash package. With this version
bump, we can now use the new optimized dsha256 routine where applicable.

With this commit, I've covered most of the areas we'll hash an entire
transaction/block/header, but we may want to optimize some other areas
further, in particular, the witness sighash calc.
2023-12-19 15:01:55 -08:00
Conner Fromknecht
8c4da83890
wire/msgblock+msgtx: user block-level script slab 2023-12-15 16:38:02 -08:00
Conner Fromknecht
d7396dc13d
wire/msgtx: use tx-level script slab 2023-12-15 16:37:57 -08:00
Conner Fromknecht
2e6eefcab1
wire/msgtx: remove unused read/writeOutPoint 2023-12-15 16:37:54 -08:00
Conner Fromknecht
4cc4f76c55
wire/msgtx: remove unused readScript 2023-12-15 16:37:51 -08:00
Conner Fromknecht
e0fa866890
wire/msgtx: remove unused readTxIn 2023-12-15 16:37:49 -08:00
Conner Fromknecht
f0184e58ba
wire/msgtx: remove unused writeTxIn 2023-12-15 16:37:46 -08:00
Conner Fromknecht
da89ed68ff
wire/msgtx: remove unused writeTxWitness 2023-12-15 16:37:44 -08:00
Conner Fromknecht
80ae5d342c
wire/msgpong: remove usage of read/writeElement 2023-12-15 16:37:41 -08:00
Conner Fromknecht
3698f2deab
wire/msgping: remove usage for read/writeElement 2023-12-15 16:37:39 -08:00
Conner Fromknecht
7207967135
wire/msgmerkleblock: optimize serialization by reusing small buffers 2023-12-15 16:37:36 -08:00
Conner Fromknecht
8bf07cc0bf
wire/netaddress: add optimiezed read/writeNetAddressBuf 2023-12-15 16:37:34 -08:00
Conner Fromknecht
dc4fbb04b3
wire/msgreject: optimize serialization by reusing small buffers 2023-12-15 16:37:31 -08:00
Conner Fromknecht
57daac345e
wire/common: add optimized writeVarStrBuf an readVarStrBuf 2023-12-15 16:37:29 -08:00
Conner Fromknecht
1cd5e02838
wire/invvect: remove unused readInvVect and writeInvVect 2023-12-15 16:37:26 -08:00
Conner Fromknecht
efcf964be2
wire/msgnotfound: optimize serialization by reusing small buffers 2023-12-15 16:37:24 -08:00
Conner Fromknecht
834febbb86
wire/msgcfilter: optimize serialization by reusing small buffers 2023-12-15 16:37:21 -08:00
Conner Fromknecht
ddeba60026
wiree/msggetcfilters: optimize serialization by reusing small buffers 2023-12-15 16:37:19 -08:00
Conner Fromknecht
d6594daa49
wire/msggetdata: optimize serialization by reusing small buffers 2023-12-15 16:37:16 -08:00
Conner Fromknecht
2383a04977
wire/msginv: optimize by reusing small buffers 2023-12-15 16:37:13 -08:00