Commit graph

67 commits

Author SHA1 Message Date
Olaoluwa Osuntokun
76ba88422f integration: stabilize pre-verack disconnect cycles
In this commit, we make the pre-verack lifecycle test count only peers that
btcd has admitted and processed through version exchange.

The source handshake limit can close a rapid follow-up socket while the
previous disconnect is still unwinding. This made the client-side version
write fail with EPIPE before the intended lifecycle path ran. We now wait for
a version response from btcd, retry rejected attempts under a deadline, and
still disconnect without sending verack.

This preserves all 50 peerDone without peerAdd cycles while removing the
scheduler-dependent admission race from the test.
2026-07-21 16:59:33 -07:00
Olaoluwa Osuntokun
5a2c5063b6 integration: synchronize p2p lifecycle stress batches
In this commit, we make each stress wave establish its full set of peers
before disconnecting them together. A ping/pong barrier proves the server
processed each verack, while RPC connection-count barriers prove
registration and removal completed before the next wave starts.

We bound concurrent handshakes from the shared source, drain every worker on
failure, and wait for server-side cleanup. This keeps the test compatible
with the admission limits it exercises and makes a passing run evidence of
the intended NewPeer/DonePeer ordering.
2026-07-21 16:45:25 -07:00
Boris Nagaev
6c056ec471
server: centralize inbound handshake admission
In this commit, we introduce a server-wide admission policy for
incomplete inbound handshakes and the CPU-bound portion of v2 responder
setup. Source accounting uses normalized IPv4 and IPv6 prefixes, while
global, per-source, and concurrent v2 budgets remain independent and
bounded.

Peer construction passes the policy through a small interface only for
inbound v2 responders. Handshake slots release on verack or disconnect,
and the connection manager reserves MaxPeers capacity for automatic
outbound peers.
2026-07-18 00:17:55 -05:00
Calvin Kim
f8ce7a7da8 rpctest: scope shared state to the current process
Two pieces of rpctest's global state silently aliased across concurrent
test processes (which is what `go test ./...` does by default, so any
`make unit` that exercises -tags=rpctest hit this):

- btcdExecutablePath compiled to a fixed path /tmp/btcd/rpctest/btcd.
  Two `go build` invocations would race on the same file, occasionally
  yielding a truncated or stale binary and downstream "tls: certificate
  signed by unknown authority" failures when the harness tried to talk
  to the resulting node.

- lastPort started at the same defaultNodePort in every process. The
  bind-test in NextAvailablePort closes the listener before returning,
  so two processes climbing from the same base would frequently hand
  out the same port and one harness would die with "connection refused"
  when btcd failed to bind.

Suffix the executable with a random uint32 and seed lastPort with a
random offset into a 50k-port window so each process climbs through
its own range.
2026-05-30 17:49:55 +09:00
Calvin Kim
221178501c integration: fix p2a_test build under -tags=rpctest
p2a_test.go calls btcutil.NewAddressPayToAnchor, but the
NewAddressPayToAnchor constructor lives in the address/v2 module's
address package. Import that package and call it through there so the
integration package builds when -tags=rpctest is set.
2026-05-30 16:47:18 +09:00
Oliver Gugger
dccea8feba multi: use new v2 modules everywhere 2026-05-14 18:05:33 -07:00
Olaoluwa Osuntokun
56613bb876
Merge pull request #2480 from Aharonee/bugfix/peer_race_condition
server: fix peer add/done race between peerHandler and syncManager
2026-05-14 15:59:07 -07:00
Olaoluwa Osuntokun
de921aee28 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-12 10:56:34 -07: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
Or Aharonee
08be37f3b6
server, integration: add unit regression tests for peer lifecycle fix
Address review feedback on the peer add/done race fix:

Add three direct unit tests in server_test.go that exercise the fix
without the full server or rpctest harness:

- TestOnVerAckDoubleCall: call OnVerAck twice on the same serverPeer,
  assert no panic and verAckCh remains closed.
- TestPeerLifecycleOrdering: verack before disconnect emits peerAdd
  then peerDone in order.
- TestPeerLifecycleSimultaneousReady: both verAckCh and Peer.Done()
  ready before the handler runs; assert peerDone always arrives and
  peerAdd, if emitted, precedes it (100 iterations).

Harden integration tests in sync_race_test.go:

- Check fakePeerConn errors via require.NoError instead of discarding.
- Extract dialAndSendVersion helper for TestPreVerackDisconnect;
  check all errors instead of silently continuing.
- Fix comment wording ("produces" -> "is expected to produce").
2026-04-09 18:19:15 +03: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
Or Aharonee
ce91359808
server: serialize peer lifecycle via single goroutine
Address review feedback on the peer add/done race fix:

- Make peerLifecycleHandler (renamed from peerDoneHandler) the sole
  sender of both peerAdd and peerDone events for each peer. OnVerAck
  now closes a signal channel (verAckCh) instead of sending directly,
  and peerLifecycleHandler selects on verAckCh vs peer.Done() to
  decide whether to send peerAdd before peerDone. This guarantees
  ordering by construction: a single goroutine sends both events
  sequentially, eliminating the negotiateTimeout race window.

- Add Done() method to peer.Peer exposing the quit channel read-only,
  enabling select-based disconnect detection from server code.

- Remove the now-unused AddPeer method.

- Address style feedback: 80-char line limit, empty lines between
  switch cases, break long function calls, use require.GreaterOrEqualf
  instead of if+Fatalf, bump syncRaceConcurrency to 300 for
  backpressure testing, add TestPreVerackDisconnect for disconnect
  prior to verack.
2026-02-16 16:58:12 +02:00
Or Aharonee
091b790163
server: fix peer add/done race between peerHandler and syncManager
peerDoneHandler ran as a separate goroutine per peer and independently
notified both peerHandler (via donePeers channel) and the sync manager
(via syncManager.DonePeer) about a peer disconnect. Because these two
sends were unsynchronized, the sync manager could observe DonePeer
before NewPeer when a peer connected and disconnected quickly. This
caused the sync manager to log "unknown peer", then later register the
already-dead peer as a sync candidate that was never cleaned up,
potentially leaving it stuck with a dead sync peer.

Two structural changes eliminate the race:

1. Merge the newPeers and donePeers channels into a single
   peerLifecycle channel. Since OnVerAck (add) always fires before
   WaitForDisconnect returns (done), a single FIFO channel guarantees
   peerHandler always processes add before done for a given peer,
   removing the select-ambiguity where Go could pick done first.

2. Move the syncManager.DonePeer call and orphan eviction from
   peerDoneHandler into handleDonePeerMsg, which runs inside
   peerHandler. All sync manager peer lifecycle notifications now
   originate from the single peerHandler goroutine and flow into
   sm.msgChan in guaranteed add-before-done order.
2026-02-16 11:46:12 +02:00
Boris Nagaev
cd4e5426b2
regtest: align activations with Bitcoin Core
Set regtest buried heights to match Core: BIP34/65/66 at height 1 and make
CSV/SegWit/Taproot always active. Added regtest coverage for header version
floors, coinbase height enforcement, and deployment state to ensure we now
reject the blocks Core rejects and accept the ones Core accepts.

Updated package fullblocktests to generate BIP34-compliant blocks.

Test helpers now set prev block height to 0 (not the default -1) so generated
blocks start at height 1 and satisfy coinbase height rules.
2025-12-24 23:09:34 -03:00
xinhangzhou
06cf379174 refactor: use maps.Copy for cleaner map handling
Signed-off-by: xinhangzhou <shuangcui@aliyun.com>
2025-03-25 01:53:15 +08:00
Olaoluwa Osuntokun
a4f82f2540
blockchain: implement AlwaysActiveHeight for forced deployment activation
This commit introduces the concept of `AlwaysActiveHeight` to the
deployment mechanism, allowing a deployment to be forced into the active
state if the next block's height meets or exceeds this threshold.

This is intended primarily to be used alongside the new Testnet4
deployment, as the past major soft forks are meant to be active from the
very first block height.
2025-03-18 11:18:39 -05:00
yyforyongyu
814a1fc198
rpctest: make sure to WaitForShutdown 2024-12-03 23:44:16 +08:00
Calvin Kim
1cb4d3a503 rpcclient, integration: Add invalidateblock and reconsiderblock
invalidateblock and reconsiderblock are added to the rpcclient package
and an integration test is added to test the added functions.
2024-06-19 19:58:29 -04:00
yyforyongyu
a033b0d6e7
rpcclient+integration: add new method GetTxSpendingPrevOut 2024-02-27 23:04:46 +08:00
yyforyongyu
8817ebdd39
integration: print logs to console 2024-01-15 17:22:42 +08:00
yyforyongyu
c104e72151
rpctest: add itest for testmempoolaccept 2024-01-15 17:22:41 +08:00
Oliver Gugger
dd5f3bd513
rpctest: make test TCP ports unique per process
This commit adds a new NextAvailablePortForProcess function that takes a
process ID and then assures unique (non-occupied) port numbers are
returned per process.
This uses a temporary file that contains the latest used port and a
secondary temporary lock file to assure only a single goroutine can
request a new port at a time.

The GenerateProcessUniqueListenerAddresses is intened to be used as a
package-level override for the ListenAddressGenerator variable. We don't
use it by default to make sure we don't break any existing assumptions.
2023-12-21 09:42:09 +01:00
Oliver Gugger
d33ac28be1
rpctest: add more context to errors 2023-12-21 08:20:28 +01:00
Oliver Gugger
323cf1e0b1
rpctest: fix test node directory 2023-12-21 08:20:28 +01:00
Oliver Gugger
6e7680ad15
rpctest: fix formatting 2023-12-21 08:05:57 +01:00
Olaoluwa Osuntokun
f7e9fba086
Merge pull request #1918 from kcalvinalvin/2022-11-06-implement-getchaintips
blockchain, btcjson: Implement getchaintips rpc call
2023-11-14 17:16:15 -08:00
Calvin Kim
520d45e3b1 fixup! integration: add test to check prune status 2023-10-16 16:34:49 +09:00
Calvin Kim
d3d0682dd3 integration: add test to check prune status
Adds a check to make sure that prune status on getblockchaininfo
returns true for pruned nodes.
2023-10-12 15:24:42 +09:00
Olaoluwa Osuntokun
0aaa7c5e7b
Merge pull request #1979 from kcalvinalvin/merkle-calc-fast
blockchain, integration, mining, main: Rolling merkle root calculation
2023-08-10 15:05:40 -07:00
Conner Fromknecht
ecfbb7e5d8 blockchain, rpctest, mining, main: replace usage of BuildMerkleTreeStore with CalcMerkleRoot 2023-08-04 00:02:26 +09:00
Calvin Kim
fc99e96b59 rpcclient, integration: Add test for getchaintips call
rpcclient now support calling the getchaintips rpc call.

getchaintips_test.go adds test for the getchaintips rpc call. The test
includes hard-coded blocks which the test will feed the node via rpc and
it'll check that the returned chain tips from the getchaintips call
are chaintips that we expect to be returned.
2023-07-16 16:03:50 +09:00
Calvin Kim
ba5407615d multi: Run gofmt on the entire repository
The doc formatting changes introduced in the recent go version is
increasing the diff for all of the new commits.  Formatting it all in
this commit will help the readability of future PRs by reducing the
diff.
2023-06-21 22:31:09 +09:00
Harsha Goli
e0149d63a1 rpctest: ensure rpclisten is set to an available port 2022-04-09 07:25:55 -04:00
Olaoluwa Osuntokun
eee3c3b337
multi: switch project over to using btcec/v2 2022-01-26 16:10:17 -08:00
Olaoluwa Osuntokun
c6b66ee79c
blockchain+integration: add support for min activation height and custom thresholds
In this commit, we extend the existing version bits state machine to add
support for the new minimum activation height and custom block threshold
for activation. We then extend the existing BIP 9 tests (tho this isn't
really BIP 9 anymore...) to exercise the new min activation height
logic.
2022-01-25 15:27:46 -08:00
Olaoluwa Osuntokun
caac0f821a
multi: update btcutil imports to point to new sub-module
In this commit, we update all the btcutil imports to point to the new
sub-module.

In the same commit, we also modify the recently added `btcutil/go.mod`
file as we need to continue pointing to the _old_ version of btcd, until
we merge this PR and push a new tag.
2022-01-10 18:44:58 -08:00
Matthew Bajorek
a1f43e4d84 integration: Add unit tests for all three GetNetworkHashPS client methods 2021-12-14 10:05:52 -05:00
Johan T. Halseth
37a6e8485b
rpctest: add witness commitment when calling CreateBlock
If we tried to include transactions having witnesses, the block would be
invalid since the witness commitment was not added.
2021-04-26 13:53:53 +02:00
Wilmer Paulino
f133593b93
build: update btcutil dependency 2021-03-29 16:59:44 -07:00
Jake Sylvestre
d08785547a docs: update shields 2021-03-05 07:45:19 -05:00
Jake Sylvestre
2a1aa5129e Add Batch JSON-RPC support (rpc client & server) 2021-02-09 09:47:46 -05:00
Olaoluwa Osuntokun
e9c7a5ac64
Merge pull request #1659 from guggero/itest-fixes
integration: optimize harness for better itest control, restore bitcoind compatibility
2020-11-13 16:05:16 -08:00
Liran Sharir
9fd26cf795 integration/rpctest: randomizes port in rpctest.New to reduce collisions 2020-11-11 11:37:34 -05:00
Oliver Gugger
65d2b7a18c
integration: allow specifying connection behavior 2020-11-11 14:29:17 +01:00
Oliver Gugger
93cc7f36cf
integration: allow overwriting address generator 2020-11-11 14:24:14 +01:00
Oliver Gugger
9250064837
integration: allow setting custom btcd exe path
To allow using a custom btcd executable, we allow specifying a path to a
file. If the path is empty, the harness will fall back to compiling one
from scratch.
2020-11-11 14:16:08 +01:00
Wilmer Paulino
266851e329
btcjson+rpcclient: support new unified softfork bitcoind format 2019-11-05 16:28:33 -08:00
Johan T. Halseth
f5b60801a5
integration/rpctest: make exec path compatible with modules
With the use of go modules, the btcdPkgPath will no longer be `github.com/btcsuite/btcd`, but end with a version string (e.g. btcd@v0.0.0-20181129140220-beb77e89572a).

This trips up build.ImportDir, which will return a ImportPath of ".", since this package cannot be found in the go path.

There are probably several ways of fixing this, by clever string magic, but seems easiest to just hardcode the btcd package name.
2018-11-29 15:13:37 +01:00
Wilmer Paulino
dfd7f6caf8
integration/rpctest: add ability to create txs without change outputs 2018-08-16 16:24:02 -07:00
Wilmer Paulino
9aa83ad423
integration/rpctest: disable the txindex by default
In this commit, we disable the txindex by default as the flag cannot be
overwritten by btcd once set.
2018-08-16 16:24:01 -07:00