Commit graph

4255 commits

Author SHA1 Message Date
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
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
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
Erick Cestari
52e7b26a11
addrmgr: treat RFC7343 (ORCHIDv2) addresses as unroutable
This issue was discovered through differential fuzzing between btcd and
Bitcoin Core.

Add detection and filtering of IPv6 ORCHIDv2 addresses as defined by
RFC7343 (2001:20::/28). These addresses are overlay routable
cryptographic hash identifiers and should not be treated as routable
peer addresses, consistent with Bitcoin Core's behavior.

Changes:
- Add rfc7343Net variable for the 2001:20::/28 address block
- Add IsRFC7343() function to detect ORCHIDv2 addresses
- Update IsRoutable() to exclude RFC7343 addresses
- Add comprehensive test coverage for RFC7343 detection
- Fix incorrect error message in TestIPTypes (IsRFC1918 -> IsRFC6145)
2026-03-06 09:55:31 -03:00
Calvin Kim
2aae8a6d68 netsync: add TestStartSyncChainCurrent for chain-current noop path
Verify that startSync does not set syncPeer or ibdMode when the
chain tip is recent and no peer advertises a height above ours.
2026-02-26 23:08:01 +09:00
Calvin Kim
570d3c8da3 netsync: add TestStallNoDisconnectAtSameHeight for stall handler 2026-02-26 22:22:51 +09:00
Calvin Kim
ce094262ff netsync: add TestStartSyncBlockFallback for block-only sync path
Verify that startSync skips header download and directly requests
blocks when the header chain is already caught up to the peer's
height but the block chain lags behind.
2026-02-26 22:05:57 +09:00
Calvin Kim
dc6e096cd3 netsync: add TestSyncStateMachine for end-to-end IBD sync flow
Tests the full IBD state machine: startSync → fetchHeaders →
handleHeadersMsg → fetchHeaderBlocks → handleBlockMsg → IBD complete.

Three sub-cases exercise different paths:
- fresh IBD from genesis
- node restart with partially synced blocks
- stalled sync peer recovery via handleStallSample
2026-02-26 21:57:54 +09:00
Calvin Kim
0b1c7b7acd netsync: rename headersFirstMode to ibdMode
ibdMode is a more fitting name than headersFirstMode since all blocks
are downloaded headers-first during the initial block download.
2026-02-26 18:10:29 +09:00
Calvin Kim
5af7680b2b netsync: remove unused headerList and checkpoint code
Since we no longer utilize the headerList for doing headers-first
download and checkpoint tracking, we remove related code.
2026-02-26 18:10:29 +09:00
Calvin Kim
9530eda3ee netsync: update handleBlockMsg
handleBlockMsg is updated to not be based off of the headerList and
sm.nextCheckpoint when doing operations related to checkpoints and
headers.
2026-02-26 18:10:29 +09:00
Calvin Kim
46574cc746 netsync: change startSync to be based off of processed headers
ince we now utilize ProcessBlockHeaders, we change the startSync
function to utilize the block index for downloading blocks/headers
instead of using the headerList in SyncManager.
2026-02-26 18:10:29 +09:00
Calvin Kim
717926887c integration: add reorg regression test for fetchHeaderBlocks fork point
This tests that after two nodes diverge and reconnect, the shorter
node downloads blocks starting from the fork point rather than its
current height. It verifies the reorg completes, both nodes
converge, and the orphaned fork appears as a side chain via
getchaintips.
2026-02-26 16:40:01 +09:00
Calvin Kim
8588a25129 netsync: require peer argument in fetchHeaderBlocks
Refactor fetchHeaderBlocks and buildBlockRequest to take an explicit
peer parameter instead of implicitly using sm.syncPeer. This makes the
caller responsible for choosing which peer to fetch from and adds a nil
guard to prevent a panic if the sync peer has been cleared.
2026-02-26 16:40:01 +09:00
Calvin Kim
dc90f8ca2a netsync: change fetchHeaderBlocks to be based on the processed block
headers

We introduce buildBlockRequest that'll create a getdata message based
off of the block index instead of the headerList in SyncManager.

The new fetchHeaderBlocks utilizes buildBlockRequest and now will create
fetch requests based on the processed block headers.
2026-02-26 16:39:55 +09:00
Calvin Kim
28b6690ca9 blockchain: add BestChainHeaderForkHeight to return the fork point
between the best chain and the best header chain
2026-02-26 16:34:46 +09:00
Calvin Kim
26c24592d2 netsync: use ProcessBlockHeader in handleBlockHeader
Instead of the old headerList based header processing, we make use of
the new ProcessBlockHeader function.
2026-02-26 16:34:46 +09:00
Calvin Kim
2f2364db66 netsync: add fetchHeaders
fetchHeaders picks a random peer at a higher advertised block and
requests headers from them.
2026-02-26 16:34:46 +09:00
Calvin Kim
51576a44cc netsync: add isInIBDMode
isInIBDMode returns if the SyncManager needs to download blocks and sync
to the latest chain tip.

It determines if it's in ibd mode by checking if the blockchain thinks
we're current and if we don't have peers that are at higher advertised
blocks.
2026-02-26 16:34:46 +09:00
Calvin Kim
ddf80b87ef netsync: add fetchHigherPeers
fetchHigherPeers provides a convenient function to get peers that are
sync candidates and are at a higher advertised height than the passed in
height.
2026-02-26 16:34:44 +09:00
Calvin Kim
b1aef3ad21 netsync: add checkHeadersList
checkHeadersList takes in a blockhash and returns if it's a checkpointed
block and the correct behavior flags for the verification of the block.
2026-02-26 16:33:47 +09:00
Calvin Kim
eeb2d43a79 blockchain: add exports methods based on block header tip
The added exported methods on BlockChain provide access to the block
header tip like fetching block hashes and heights from the headers
chain.
2026-02-25 18:49:51 +09:00
Calvin Kim
e4b38e0cbf blockchain: change HaveBlock to also check for block data availability
In block processing and block downloading, HaveBlock is used to check if
the block data already exists.  It was ok to just check for the
existence of the blockNode but we now we also need to check if the data
exists as the blockNode may be present for just the block header.
2026-02-25 18:49:51 +09:00
Calvin Kim
9e45f60e9e blockchain: don't flush blockNodes that we don't have the data for
On flushes to the database, we check that the blockNodes we have for
the downloaded block headers are not flushed to the disk unless the
block data is stored as well for backwards compatibility.

With older btcd clients, they rely on the fact that the blockNode is
present to check if the block data is also present. Since we now
store blockNodes for just the block headers, this is no longer true.

Because of this, we don't flush the blockNodes if there's no
accompanying block data for it. This results in downloading and
verifying the headers again if the node were to restart but since the
header data is small and the verification is quick, it's not a big
downside.

As an optimization, flushToDB now skips opening a write transaction
entirely when every dirty node is header-only. This avoids a no-op
write transaction on every ProcessBlockHeader call during header sync.
2026-02-25 18:49:51 +09:00
Calvin Kim
f9645f07b5 blockchain: reuse existing header node in maybeAcceptBlock
maybeAcceptBlock unconditionally created a new blockNode, overwriting the
index entry.  If maybeAcceptBlockHeader had already processed the header,
the pointer held by bestHeader's chainView became orphaned, breaking
bestHeader.Contains and downstream checks like IsValidHeader.

Check for an existing node first and upgrade its status to
statusDataStored rather than replacing it.
2026-02-25 18:49:51 +09:00
Calvin Kim
c1a46122ca blockchain: add ProcessBlockHeader
ProcessBlockHeader performs chain selection and context-free &
contextual validation for the given block header.  The function allows
a header-first downloading of blocks even without checkpoints.
2026-02-25 18:49:51 +09:00
Calvin Kim
fd16786b1e blockchain: add maybeAcceptHeader
maybeAcceptHeader performs checks to accept block headers into the
header chain.  This function allows for a true headers-first download
where we only accept block headers for downloading new blocks.
2026-02-25 15:42:46 +09:00
Calvin Kim
743d0a2c40 blockchain: add ErrKnownInvalidBlock error code
Distinguish between a block that has itself failed validation
versus one that is only invalid due to an ancestor.
2026-02-25 15:42:46 +09:00
Calvin Kim
b60c2c3016 blockchain: add statusHeaderStored for blockNode status
Since we may now have blockNodes with just the block header stored
without the data, we add a new status to account for this.
2026-02-25 15:42:46 +09:00
Calvin Kim
5f25cf0d37 blockchain: add bestHeader to BlockChain
We add a chainview of bestHeaders so that we'll be able to keep track of
headers separately from the bestChain. This is needed as we're getting
headers for new block annoucements instead of invs.
2026-02-25 15:42:46 +09:00
Erick Cestari
3a0df88558
btcec/schnorr: fix nonce generation comments
The comments in schnorrSign and Sign incorrectly stated that CustomNonce
triggers RFC6979 nonce generation. The actual behavior is the opposite:

- With CustomNonce: BIP-340 compliant nonce derivation (steps 6-8)
- Without CustomNonce (default): RFC6979 deterministic nonce generation

Also fixes typo "set 14" -> "step 14".
2026-01-15 09:35:47 -03:00
Olaoluwa Osuntokun
3eacced04e
Merge pull request #2471 from Roasbeef/container-version-bump
build: bump version of Go container for release builds
2026-01-12 18:34:13 -08:00
Olaoluwa Osuntokun
7d4da22923
build: bump version of Go container for release builds 2026-01-12 18:22:47 -08:00
Yong
84315272d6
Merge pull request #2468 from jlopp/spellcheck
fix spelling error
2026-01-12 19:49:16 +08:00
Jameson Lopp
d3615dcc28
fix spelling error 2025-12-25 07:33:49 -05:00
Calvin Kim
e009124748 treap, ffldb: change Put() to take in multiple key-value pairs
Previously, the Put() function took in a single key-value pair,
resulting in memory allocation overhead as the intermediary treapNodes
got allocated and immediately garbage collected, resulting in
performance loss due to the garbage collection overhead.

We're able to recycle the intermediary treapNodes allocated by taking in
multiple key-value pairs.  This results in better performance of the
node especially during UTXO cache flushes.
2025-11-14 14:58:35 +01:00
Olaoluwa Osuntokun
9ff0780da6
Merge pull request #2450 from FiveBellsSettlement/btc-client-correct-dialcontext
rpcclient: ensure http dial respects timeout
2025-11-05 17:07:55 -08:00