Commit graph

4184 commits

Author SHA1 Message Date
Oliver Gugger
ffcda0f629
rpcclient: add bitcoind version dependent error matching
Fixes #2404.
If different versions of bitcoind return different error strings, we
need a way to match those as well.
2025-08-19 09:00:02 +02:00
Olaoluwa Osuntokun
baebb836c2
Merge pull request #2366 from bhandras/submitpackage
btcjson: add stubs for SubmitPackage
2025-07-28 18:00:03 -05:00
Andras Banki-Horvath
5e4519770f
btcjson: add json parsing unit tests for SubmitPackage stubs 2025-07-23 10:38:59 +02:00
Andras Banki-Horvath
6b2a9a4a48
btcjson: add stubs for SubmitPackage 2025-07-23 10:38:32 +02:00
Olaoluwa Osuntokun
7b7644ca81
Merge pull request #2397 from gijswijs/bloom-update
Align hash function count with bloom filter capacity
2025-07-22 16:49:28 -05:00
Yong
ab068f48b6
Merge pull request #2399 from mohamedawnallah/add-gemini-style-guide
.gemini: add `styleguide.md`
2025-07-16 20:34:44 +08:00
yyforyongyu
e8ea247cf0 .gemini: add styleguide.md 2025-07-16 11:46:18 +00:00
Yong
9181aa1af6
Merge pull request #2398 from Roasbeef/neutrino-opt
wire: optimize parsing for CFCheckpkt message, reduce allocs by 96%
2025-07-16 17:32:11 +08: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
Gijs van Dam
5e69e56cb8
btcutil/bloom: align hash function count with filter capacity
In this commit, we ensure hash function parameters are consistent with
filter size to avoid unnecessary operations. A filter with no capacity
doesn't actually require any hash functions, so we can cut that loop
short.
2025-07-14 11:03:36 +02:00
Yong
9adca7479d
Merge pull request #2375 from Aharonee/chore/update_psbt_deps
psbt: upgrade dependencies and fix unit test
2025-07-14 05:17:05 +03:00
Or Aharonee
4727f323ed
Update Go module and dependencies
Upgrade several package versions and fix a test case for transaction serialization.
2025-07-11 23:30:28 +03:00
Olaoluwa Osuntokun
4530538f8c
Merge pull request #2216 from Roasbeef/script-template
txscript: add new ScriptTemplate DSL for writing Scripts
2025-07-11 12:38:56 -07:00
Olaoluwa Osuntokun
c6b5b83f37
Merge pull request #2396 from brunoerg/2025-07-remove-alert
p2p: remove alert msg
2025-07-11 12:38:45 -07:00
Olaoluwa Osuntokun
c9bd7b4c0f
txscript: add new ScriptTemplate DSL for writing Scripts
In this commit, we add a new function, `ScriptTemplate` to make the
process of making custom Bitcoin scripts a bit less verbose.

ScriptTemplate processes a script template with parameters and returns the
corresponding script bytes. This functions allows Bitcoin scripts to be
created using a DSL-like syntax, based on Go's templating system.

An example of a simple p2pkh template would be:

   `OP_DUP OP_HASH160 0x14e8948c7afa71b6e6fad621256474b5959e0305 OP_EQUALVERIFY OP_CHECKSIG`

Strings that have the `0x` prefix are assumed to byte strings to be pushed
ontop of the stack. Integers can be passed as normal. If a value can't be
parsed as an integer, then it's assume that it's a byte slice without the 0x
prefix.

Normal go template operations can be used as well. The params argument
houses paramters to pass into the script, for example a local variable
storing a computed public key.
2025-07-11 12:20:05 -07:00
Bruno Garcia
bc52e090ce p2p: remove alert msg 2025-07-10 13:07:50 -03:00
Oliver Gugger
fa8d919dd0
Merge pull request #2395 from mohamedawnallah/addGeminiCodeAssist
CI: add gemini assist for code reviews
2025-07-10 08:41:28 +02:00
Mohamed Awnallah
73344dbe49 CI: add gemini assist for code reviews 2025-07-09 20:23:35 +00:00
Oliver Gugger
2c9907c387
Merge pull request #2393 from bshramin/btcctl-config-error
Print a readable error before os.Exit(1) in btcctl
2025-07-03 08:37:03 +02:00
Olaoluwa Osuntokun
1d9a130076
Merge pull request #2322 from starius/block-count
rpcclient: clarify GetBlockCount description
2025-07-02 15:58:28 -07:00
Olaoluwa Osuntokun
91daea9bd4
Merge pull request #2389 from yyforyongyu/refactor
Minor refactor to improve code health
2025-07-02 15:54:33 -07:00
Amin Bashiri
e4c44fe796 Print a readable error before os.Exit(1) in btcctl 2025-07-01 15:17:21 -04:00
yyforyongyu
1827fcf2f5
main: fix typo 2025-06-25 16:55:33 +08:00
yyforyongyu
9dc2503295
main: refactor OnGetData to simplify the logic
The preivous usage of two channels - by piping them together to create a
semaphore effect, is difficult to follow and prone to bugs. This commit
now refactors the method to explicitly implement a semaphore.

Prior to this change, we would allow at max 3 concurrent goroutines -
this is now bumped to 5.
2025-06-25 16:55:33 +08:00
yyforyongyu
796d0efc0d
main: add method pushInventory to be used in OnGetData
A minor refactor on method `OnGetData`.
2025-06-23 21:31:26 +08: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
Olaoluwa Osuntokun
3afc25bed2
Merge pull request #2284 from Roasbeef/ctrl-block-error
txscript: add more detail to invalid tapscript merkle proof error
2025-06-18 18:23:01 -07:00
Olaoluwa Osuntokun
e5d2ae489b
txscript: add more detail to invalid tapscript merkle proof error
In this commit, we add more detail to the invalid tapscript merkle proof
error. Before this commit, the error was blank, making such a case hard
to debug. We'll now log the expected witness program, what we derived,
and also the passed in tapscript root.
2025-06-18 18:11:24 -07:00
Olaoluwa Osuntokun
ac58f6984c
Merge pull request #2384 from Roasbeef/bip324-modules
build: remove replace directives left over after BIP 324 PRs
2025-06-06 16:02:06 -07:00
Olaoluwa Osuntokun
5f415da375
build: remove replace directives left over after BIP 324 PRs 2025-06-06 15:50:48 -07:00
Olaoluwa Osuntokun
7716758a83
Merge pull request #2383 from Roasbeef/v2-module-fix
v2transport: fix module path
2025-06-06 15:44:07 -07:00
Olaoluwa Osuntokun
56cb0bb2fe
v2transport: fix module path
In this commit, we fix the module path for the new `v2transport`
package. It needs the repo prefix (btcsuite/btcd).
2025-06-06 15:33:49 -07:00
Olaoluwa Osuntokun
089f012e6d
Merge pull request #2381 from Roasbeef/v2-transport-module
v2transport: update to btcec v2.3.5, reduce module deps
2025-06-06 15:25:51 -07:00
Yong
0cba26df01
Merge pull request #2374 from ffranr/2372-fix-bip32-derivation-overflow-bug
psbt: overflow checks when computing Taproot BIP32 derivation min size
2025-06-06 15:29:43 +08:00
Olaoluwa Osuntokun
48058b0d8d
v2transport+btcd: update to btcec v2.3.5 2025-06-03 17:03:19 -07:00
Olaoluwa Osuntokun
f44d32bd2d
v2transport: reduce size of module by shedding dep on wire package
In this commit, we reduce the size of the v2transport module by shedding
the dep on the wire package. This allows us to only depend on btcec/v2,
which itself is a sub module, instead of the entire btcd module.
2025-06-03 17:03:19 -07:00
Olaoluwa Osuntokun
00db18bc82
v2transport: minor style fixes 2025-06-03 17:03:19 -07:00
Olaoluwa Osuntokun
253c36dd27
Merge pull request #2260 from Crypt-iQ/v2_transport
multi: introduce v2transport and implement BIP324
2025-06-03 16:44:36 -07:00
Olaoluwa Osuntokun
5ab25f3f2b config: add new --v2transport option to gate BIP 324 2025-05-26 13:46:25 -07:00
Olaoluwa Osuntokun
ef0e43ba06 multi: refactor v2 -> v1 downgrade logic
In this commit, we refactor the v2 -> v1 downgrade logic to: simplify
the code in the server for reconnection, use a functional option to
avoid breaking the connmgr API, and we also encapsulate the downgrade
state in a new struct so it can be re-used elsewhere.
2025-05-26 13:46:25 -07:00
Olaoluwa Osuntokun
be67c07001 peer: log when we downgrade outbound peers from v2 to v1 2025-05-26 13:46:25 -07:00
Olaoluwa Osuntokun
53afae48ff peer: use random garbage length for v2 peers 2025-05-26 13:46:25 -07:00
Olaoluwa Osuntokun
1ffd7b4b18 v2transport: export maxGarbageLen 2025-05-26 13:46:25 -07:00
Olaoluwa Osuntokun
c6b2553ab8 rpcserver: expose v2 connection status in getpeerinfo
This commit adds a new boolean field, `V2Connection`, to the
`getpeerinfo` RPC result. This field indicates whether the connection
to the peer is using the v2 encrypted transport protocol.

The `peer.StatsSnap` struct is updated to include this `V2Connection`
field, which is populated based on the `UsingV2Conn` field in the peer's
configuration. The `btcjson.GetPeerInfoResult` struct is also updated
to include the corresponding JSON field `v2_connection`. Finally, the
`handleGetPeerInfo` RPC handler is modified to copy this value from the
peer's stats snapshot into the RPC response.
2025-05-26 13:46:25 -07:00
Eugene Siegel
2d2bf57a69 server.go+connmgr: changes to trigger a reconnect in connmgr
This changes the Disconnect function to accept a `reconnect` argument.
Also change handleDisconnected to have reconnection logic.
2025-05-26 13:46:25 -07:00
Eugene Siegel
675d3007cf peer+server.go: pendingReconnects map to reconnect to downgraded v2->v1 peers
When ShouldDowngradeToV1() returns true, we'll:
- mark in pendingReconnects that we should attempt v1 bitcoin p2p transport
  when the connmgr successfully re-establishes the outbound TCP connection.
- set a bit on the associated *serverPeer that will cause us to tell the
  connmgr to reconnect. This is needed because not all outbound peers are
  permanent and we might otherwise not reconnect to them if the initial v2
  transport negotiation failed.
2025-05-26 13:46:25 -07:00
Eugene Siegel
b5f0ebb132 v2transport+server+peer: add v2->v1 downgrade error if EOF during handshake 2025-05-26 13:46:25 -07:00
Olaoluwa Osuntokun (aider)
6588e2e829 v2transport: Add detailed logging to transport implementation
This commit introduces detailed logging throughout the v2 transport
implementation using the `btclog` package. Logging has been added to
key functions to provide better visibility into the handshake process,
packet encryption/decryption, and underlying network operations.

Log levels are used as follows:
- Debug: High-level steps within functions (e.g., starting handshake,
  creating ciphers, sending/receiving major components).
- Trace: Detailed internal state, cryptographic operations, byte-level
  data (e.g., derived keys, intermediate values, raw bytes sent/received,
  loop iterations).
- Info: Significant state changes or events (e.g., reverting to v1).
- Warn/Error/Critical: Issues encountered during operation.

Logging includes variable states using the format "key=%v" for easier
parsing and debugging. This enhances observability during connection
establishment and data transfer using the v2 transport protocol.
2025-05-26 13:46:25 -07:00
Eugene Siegel
824e163e1e server.go+peer: enable v2 functionality in peer code
This adds the v2 software flag to the list of services btcd supports
and adds functionality to optionally use v2 transport if the peer
supports it.
2025-05-26 13:46:25 -07:00