Commit graph

15 commits

Author SHA1 Message Date
Boris Nagaev
0860e42dcf
netsync: avoid clock tick race in sync state test
TestSyncStateMachine checked that handleHeadersMsg advanced
lastProgressTime by comparing it against the timestamp written by
startSync. Those two writes can happen within the same clock tick, so
the handler can update the field while time.After still reports false.

Reset lastProgressTime to the zero value before delivering headers and
assert that the handler writes a non-zero value. This keeps the test
focused on the behavior under test without depending on adjacent
time.Now calls producing distinct timestamps.
2026-07-02 01:18:38 -05:00
Oli
a7e980dd34
netsync: require block-serving services on regtest/simnet sync peers
Commit 26124d275 made every peer a sync candidate on regtest and
simnet so that nodes on non-localhost networks (e.g. Docker bridge
networks) can be synced from. Dropping the address requirement was
the intent, but the change also dropped the service-flag requirement,
so light clients became eligible sync peers.

A light client (e.g. neutrino) advertises a recent best height but
can serve neither headers nor blocks. Electing one as the sync peer
stalls the sync until the stall handler disconnects it, and with
other light client connections present the next one is elected and
stalls again, livelocking the sync indefinitely. This surfaced in
neutrino's sync tests, where a btcd simnet node connected to both a
neutrino instance and other btcd nodes never synced.

Keep accepting any peer address on regtest/simnet, but require the
peer to signal SFNodeNetwork or SFNodeNetworkLimited like on any
other network.
2026-06-17 13:45:43 +02:00
Oliver Gugger
dccea8feba multi: use new v2 modules everywhere 2026-05-14 18:05:33 -07:00
Boris Nagaev
74f48480e0
tests: fix stale regtest assumptions after #2467
PR #2467 changed regtest to match Core's BIP34/65/66 activation rules, and
the merged stack carries that (commit cd4e5426 "regtest: align activations
with Bitcoin Core"). Height-1 regtest blocks now need a BIP34-compliant
coinbase height and a post-BIP66 block version.

The failing tests came from commits added after PR #2467 was opened on
December 25, 2025 but before it merged on April 30, 2026:

- c1a46122 ("blockchain: add ProcessBlockHeader")
- f9645f07 ("blockchain: reuse existing header node in maybeAcceptBlock")
- dc6e096c ("netsync: add TestSyncStateMachine for end-to-end IBD sync flow")
- ce094262 ("netsync: add TestStartSyncBlockFallback for block-only sync path")
- 2aae8a6d ("netsync: add TestStartSyncChainCurrent for chain-current noop path")

Because those tests landed later, they kept the old regtest assumptions even
though #2467 had already been authored and tested against the older tree.
Once #2467 finally merged, these newer tests started building invalid regtest
blocks and headers.

Fix them by setting the genesis tip height to 0 before generating descendants,
using Version 4 in the regtest block/header helpers, and encoding the test
coinbase height with a minimal BIP34 push plus padding for the generic coinbase
script-length rule.
2026-05-06 17:16:25 -05: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
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
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
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