Commit graph

339 commits

Author SHA1 Message Date
Oliver Gugger
9dfa926fff server: fix linter issue 2026-05-14 18:05:33 -07:00
Oliver Gugger
dccea8feba multi: use new v2 modules everywhere 2026-05-14 18:05:33 -07:00
Calvin Kim
589d7c0ae0 main: use btcutil.InterfaceAddr
We use this as the net.InterfaceAddr is broken from android 11.
2026-05-14 16:16:14 -07:00
Olaoluwa Osuntokun
f2a24d41d1 server: document peerLifecycle channel buffer sizing
The peerLifecycle channel is sized cfg.MaxPeers*2 because each
peer can emit two lifecycle events (peerAdd followed by peerDone).
Without an inline comment a future reader could "simplify" the
factor to cfg.MaxPeers and tighten back-pressure under normal
connect/disconnect churn, causing peerLifecycleHandler goroutines
to block.
2026-05-12 16:20:56 -07:00
Olaoluwa Osuntokun
e6f589e515 server: gate handleDonePeerMsg on peerAdded, not VerAckReceived
peer.processRemoteVerAckMsg sets verAckReceived=true *before* it
invokes the OnVerAck listener that closes verAckCh. If the peer
disconnects in that window, the peerLifecycleHandler select may
pick Peer.Done() over verAckCh and skip the peerAdd send -- yet
VerAckReceived() still returns true. handleDonePeerMsg would then
call syncManager.DonePeer for a peer that was never NewPeer'd
(sync manager logs a warning and discards).

Add an explicit peerAdded atomic.Bool on serverPeer. Set it in
peerLifecycleHandler after peerAdd has been enqueued. Gate the
syncManager.DonePeer + orphan eviction in handleDonePeerMsg on
peerAdded.Load() so those side effects only fire for peers that
were actually registered.

This tightens the "peerAdd precedes peerDone, or peerAdd is
skipped entirely" invariant from this PR into something explicit
rather than something derived from a flag that races OnVerAck.
2026-05-12 16:20:43 -07:00
Olaoluwa Osuntokun
f1b95f8fd9 server: guard OnVerAck with sync.Once
The prior select/default+close() guard on verAckCh is correct only
under the invariant that OnVerAck is invoked from a single goroutine
(peer.processRemoteVerAckMsg on the input handler). The two steps
are not atomic: any future change that invokes listeners off the
input goroutine would let two concurrent callers both observe
default and panic on double-close.

Replace the guard with sync.Once. This makes the close-once
contract obviously correct rather than correct-by-distant-invariant
and drops the dead "called more than once" log path.
2026-05-12 16:20:15 -07:00
Or Aharonee
f6d69f7a15
server: address review feedback on peer lifecycle handling
Prioritize verAckCh in peerLifecycleHandler select to avoid
nondeterministic peerAdd skipping when both channels are ready.

Guard OnVerAck against double-close by checking the channel before
closing, logging an error instead of panicking.

Adjust peerLifecycleEvent comment to reflect that peerAdd may be
skipped when the peer disconnects before or concurrently with verack.

Fix verAckCh field comment formatting.
2026-02-18 14:43:40 +02: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
Bruno Garcia
bc52e090ce p2p: remove alert msg 2025-07-10 13:07:50 -03: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
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
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
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
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
cec489
d881c686e6
Fix the btcctl uptime command
commit 0b2998b7f279d3aef4d83415dae26948f5a6bdf4
Author: cec489 <173723251+cec489@users.noreply.github.com>
Date:   Mon Jun 24 20:01:13 2024 +0000

    A cleaner fix is to set the startTime in the server Start() function
    which is where the server is actually started.

commit ae6c1256981befb43972e83a086ea663df629873
Author: cec489 <173723251+cec489@users.noreply.github.com>
Date:   Mon Jun 24 19:15:23 2024 +0000

    Fix the btcctl uptime command by moving the setting of startupTime
2024-06-27 13:36:29 -04:00
Calvin Kim
d65999e4a1 main: add logging if the node is pruned
For debug purposes down the road, log that the node is pruned if it's
set to pruned.
2024-05-01 18:02:05 +09:00
Thabokani
a4df044cfd
Fix some typos (#2085)
* Dockerfile: fix typo

* blockchain: fix typos

* sample-btcd.conf: fix typos

* server: fix typos

* txscript: fix typos
2024-01-03 16:36:49 -05:00
Calvin Kim
16cd44f0e6 blockchain, netsync, main, cmd/addblock: Use utxocache
This change is part of the effort to add utxocache support to btcd.

utxo cache is now used by the BlockChain struct.  By default it's used
and the minimum cache is set to 250MiB.  The change made helps speed up
block/tx validation as the cache allows for much faster lookup of utxos.
The initial block download in particular is improved as the db i/o
bottleneck is remedied by the cache.
2023-12-16 16:53:17 +09:00
Calvin Kim
02469e16a6 main: Add prune flag
This change is part of the effort to add pruning support to btcd.

Pruning is now available to the end user via --prune flag.  There are
checks in place so that the user doesn't go below the minimum prune
target of 1536 MiB.  The minimum is set so that we keep at least 288
blocks per the requirement for NODE_NETWORK_LIMITED nodes specified by
BIP0159.  The default value of 0 will disable pruning.
2023-08-23 00:46:26 +09:00
Calvin Kim
0212c334ce wire, main: Add SFNodeNetworkLimited flag
This change is part of the effort to add pruning support to btcd.

Wire now supports the ability to signal NODE_NETWORK_LIMITED which
signals to peers that the node is able to serve the last 288 blocks.

Since archival nodes have all blocks, they can also signal for
NODE_NETWORK_LIMITED.  SFNodeNetworkLimited flag is added to the default
services.
2023-08-22 15:48:55 +09:00
David Vennik
e563459b72 Fixed ban bug that doesn't print numTxns
(cherry picked from commit 11c8d11a26d14a64fe6e412313624b6bfc5b4e2c)
2022-10-10 10:34:58 -04:00
eugene
cb6f21b598
peer+wire: add addrv2 message, protocol negotiation 2022-02-24 13:11:15 -05:00
eugene
201c0836ec
multi: update addrmgr, server to use NetAddressV2 instead of legacy 2022-02-24 13:11:15 -05: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
Olaoluwa Osuntokun
e98a1a1b4c
peer+server: add new config option to optionally disable stall detection
In this commit, we add a new config options that allows one to start
`btcd` in an operating mode that disables the stall detection. This can
be useful in simnet/regtest integration tests settings where it's
important that `btcd` holds on to its possibly sole connection to the
only other node in the test harness.

A new config flag has been added to gate this behavior, which is off by
default.
2021-10-01 14:55:50 -07:00
yyforyongyu
61634447e7
btcd+netsync: support witness tx and block in notfound msg 2020-09-03 18:53:16 +08:00
Javed Khan
24db7d7c0c netsync: handle notfound messages from peers
backport from https://github.com/decred/dcrd/pull/2253

When a peer sends a notfound message, remove the hash from requested
map.  Also increase notfound ban score and return early if it
disconnects the peer.
2020-07-28 09:23:35 -04:00
David Hill
f7399e6157 build: clean linter warnings 2020-05-13 08:58:39 -04:00
Steven Roose
9e94ccbd0e server: Fix incorrect log message format 2020-03-05 17:00:45 -05:00
Olaoluwa Osuntokun
037489ef50
Merge pull request #1485 from wpaulino/syncmanager-done-peer-verack
server: remove peer from SyncManager on VerAckReceived
2019-11-08 18:21:37 -08:00
nsa
c2ca0a408e
server: add addressesMtx to fix race condition 2019-11-08 21:11:24 -05:00
Wilmer Paulino
bc21593480
server: remove peer from SyncManager on VerAckReceived
Peers are now added to the SyncManager if we receive their verack, but
we'd still attempt to remove them from the SyncManager if we didn't
receive it.
2019-10-29 19:38:38 -07:00
Wilmer Paulino
baeb789a7d
server: prevent adding peers if already disconnected
This addresses an issue where the server ends up tracking a peer that
has been disconnected due to it processing a peer's `done` message
before its `add` message.
2019-10-14 13:26:44 +02:00
Wilmer Paulino
769c4e152f
server: request addresses from new peers once they can process messages
This was previously done within the OnVersion listener, which should not
be a blocking operation. It turns out that requesting these messages
there can lead to blocking due to peers not being able to process
messages since their message queues have yet to start. Therefore, we'll
now request within handleAddPeerMsg, which should allow it to go
through.
2019-10-14 13:26:43 +02:00
Wilmer Paulino
a1a5bfa819
server: add new peers within OnVerAck instead of within OnVersion
This change is needed as part of requiring peers to also send a verack
message following their version message during protocol negotiation.
Peers were previously added to the SyncManager before their message
queues were started, causing the server to stall if a peer didn't
provide a timely verack response following their version. We now do this
within OnVerAck, which happens shortly before peer message queues are
started.
2019-10-14 13:26:42 +02:00
Wilmer Paulino
11b84f5cb5
server: signal SyncManager with new peer within AddPeer
This makes the logic a bit more unified as previously it was possible we
for us to report the new peer to the SyncManager, but for whatever
reason failed to track the peer in the server internally within AddPeer.
This change ensures this can no longer happen.
2019-10-14 13:26:16 +02:00
Wilmer Paulino
0d00cdf82c
server: request new peer after disconnection of non-persistent peers
Doing so ensures we reach our target number of outbound peers as soon as
possible. This is only necessary after calls to connmgr.Remove, as these
won't request a new peer connection.
2019-10-10 19:05:41 -04:00
Wilmer Paulino
45d66d46f9
server: standardize use of connmanager's Disconnect and Remove methods
The Disconnect method would still attempt to reconnect to the same
peer, which could cause us to reconnect to bad/unstable peers if we came
across them. Instead, we'll now use Remove whenever we intend to remove
a peer that is not persistent.
2019-10-10 19:05:37 -04:00
Wilmer Paulino
ab6f3089f6
server: mark address as connected within handleAddPeerMsg
We do this to ensure the address manager contains live addresses.
Previously, addresses with which we established connections with would
not be marked as connected because it would be done once we disconnect
peers. Given that we don't process all of the disconnect logic when
we're shutting down, addresses of stable and good peers would never be
marked as connected unless the connection was lost during operation.
2019-10-10 12:10:52 -04:00
Wilmer Paulino
06baabe5da
server: mark address attempted on attempt rather than upon connection
We should mark addresses as attempted when we attempt to connect to
them, not once we establish a connection with said address.
2019-10-10 12:03:47 -04:00
Wilmer Paulino
95d0a371d9
mempool: implement RBF signaling policy 2019-06-13 16:35:53 -07:00
Conner Fromknecht
39592eba78
btcd: pass user agent black and white lists to server 2019-04-16 21:31:08 -07:00
Conner Fromknecht
d3ecdc91a9
server: adds hybrid black/whitelist filtering to user agents 2019-04-15 17:46:08 -07:00
Dave Collins
8c981e4ef2
server: Use local addr var in version handler.
This modifies the OnVersion handler for server peers to use a local
variable for the remote address of the peer in order to avoid grabbing
the mutex multiple times.

There are no functional changes.

Backported from Decred.
2018-09-20 22:32:59 -05:00