Commit graph

121 commits

Author SHA1 Message Date
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
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
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
Bruno Garcia
bc52e090ce p2p: remove alert msg 2025-07-10 13:07:50 -03: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
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
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
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
Calvin Kim
48c02954d2 wire, peer: fix broken ibd
IBD for new nodes were broken due to the version handshake failing
between nodes that recognized wtxid based relays.  Reverting the changes
that were made so that the node is able to connect to those nodes.
2024-11-29 15:07:01 +09:00
Marco Peereboom
684d64ad74
Add support for wtxidrelay message. (#2272)
* Add support for wtxidrelay message.

This adds support for the wtxidrelay (BIP339) message in wire.

While here, add tests for sendaddrv2 and rename AddrV2Version to
SendAddrV2Version in order to make the code consistent with all other
messages. This diff does keep the old AddrV2Version constant for
backwards compatibility.

* Bump copyrights
2024-11-06 13:03:27 +00:00
mattn
3cb9f602e8
fix typos (#2100) 2024-03-25 09:44:25 -04: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
eugene
cb6f21b598
peer+wire: add addrv2 message, protocol negotiation 2022-02-24 13:11:15 -05: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
Wilmer Paulino
fdb479f121
peer: allow external testing of peer.Peer
The previous use of allowSelfConns prevented this, as users aren't able
to invoke peer.TstAllowSelfConns themselves due to being part of a test
file, which aren't exported at the library level, leading to a
"disconnecting peer connected to self" error upon establishing a mock
connection between two peers. By including the option at the config
level instead (false by default, prevents connections to self) we enable
users of the peer library to properly test the behavior of the peer.Peer
struct externally.
2021-03-11 18:24:13 -08:00
Jake Sylvestre
d08785547a docs: update shields 2021-03-05 07:45:19 -05:00
Olaoluwa Osuntokun
7d69fb9ba6 peer: prevent last block height going backwards
This modifies the UpdateLastBlockHeight function to ensure the new
height is after the existing height before updating it in order to
prevent it from going backwards so it properly matches the intent of the
function which is to report the latest known block height for the peer.

Without this change, the value will properly start out at the latest
known block height reported by the peer during version negotiation,
however, it will be set to lower values when syncing from the peer due
to requesting old blocks and blindly updating the height.

It also adds a test to ensure proper functionality.

This is a backport of https://github.com/decred/dcrd/pull/1747
2020-08-31 09:47:41 -04:00
Javed Khan
875b51c9fb peer: knownInventory, sentNonces - use generic lru
While here, also rename and generalize limitMap and apply to
other maps which need to be bounded.
2020-07-08 16:44:04 -04:00
George Tankersley
8bbbe98be9 peer: fix small typo 2020-03-04 08:38:26 -05:00
Wilmer Paulino
5de9e5b6c2
peer: include verack as part of version handshake
It was possible for connections to bitcoind nodes to be closed from
their side if the OnVersion callback queued a message to send. This is
because bitcoind expects a verack message before attempting to process
any other messages. To address this, we ensure the verack is sent as
part of the handshake process, such that any queued messages happen
after the fact.
2019-08-20 16:25:21 -07:00
Olaoluwa Osuntokun
1c5c32a5e8
Merge pull request #1315 from halseth/msgcfilter-string
peer log: add MsgCFHeaders and MsgGetCFHeaders message summary
2018-10-12 17:43:50 -07:00
Johan T. Halseth
5e578f546f
peer log: add MsgCFHeaders and MsgGetCFHeaders string representation 2018-10-01 13:40:45 +02:00
Dave Collins
4d1e1db9ea
peer: Improve net address service adverts.
This modifies the peer code which deals with advertising service flags
via the net address fields of the version message as follows:

- For outgoing connections:
  - Set the local netaddress services to what the local peer supports
  - Set the remote netaddress services to 0 to indicate no services as
    they are still unknown
- For incoming connections:
  - Set the local netaddress services to what the local peer supports
  - Set the remote netaddress services to the what was advertised by the
    remote peer in its version message
2018-09-20 22:31:23 -05:00
Dave Collins
7b103e2434
peer: Allow OnVersion callback to reject peer.
This modifies the OnVersion callback to allow a reject message to be
returned in which case the message will be sent to the peer and the peer
will be disconnected.

Backported from Decred.
2018-09-20 22:29:27 -05:00
Dave Collins
118f55233b
peer: Rework version negotiation.
This modifies the negotiation logic to ensure the callback has the
opportunity to see the message before the peer is disconnected and
improves the error handling when reading the remote version message.

It also has the side effect of ensuring the protocol version is
negotiated before sending reject messages with the exception of the
first message not being a version message since negotiation is not
possible in that case.

This is being changed because it is useful for the server to see the
message regardless in order to have the opportunity to things such as
update the address manager and reject peers that don't have desired
services.

Backported from Decred.
2018-09-20 22:26:36 -05:00
Dave Collins
25dfda9bd3
peer: Add duplicate version message test.
This adds a test to ensure duplicate version messages are rejected.

Backported from Decred.
2018-09-20 22:18:36 -05:00
Conner Fromknecht
475eafdc70
peer/example_test: switch TrickleTimeout -> TrickleInterval 2018-08-23 22:55:21 -07:00
Conner Fromknecht
7b402cd63b
peer/peer_test: switch TrickleTimeout -> TrickleInterval 2018-08-23 22:55:21 -07:00
Conner Fromknecht
602bced5f5
peer/peer: switch TrickleTimeout -> TrickleInterval 2018-08-23 22:55:21 -07:00
Olaoluwa Osuntokun
9f436585d7
Merge pull request #1247 from davecgh/peer_minor_cleanup
peer: Minor function definition order cleanup.
2018-08-22 19:41:16 -07:00
Dave Collins
fd78330fd3
peer: Minor function definition order cleanup.
This rearranges some of the function definitions that pertain to initial
peer version negotiation and bringup so they are more consistent with
the preferred order used throughout the codebase.  In particular, the
functions are defined before they're first used and generally as close
as possible to the first use when they're defined in the same file.

There are no functional changes.

Backported from Decred.
2018-08-10 20:20:11 -05:00
Johan T. Halseth
ee0740a289
peer test: set TrickleTimeout for test configs 2018-07-24 13:11:02 +02:00
Johan T. Halseth
08619220b4
config/peer: make trickleTimeout configurable 2018-07-24 12:59:44 +02:00
Olaoluwa Osuntokun
088eec67ec
peer: ensure the version negotiation goroutine will always exit
In this commit, we patch a goroutine leak within the peer struct. This
goroutine leak can happen, if the remote side fails to actually finish
negotiation the protocol before our timeout ticker ticks. In this case,
the goroutine will be blocked on a send, as the channel is unfired. An
example trace from a btcd node I had on testnet showed:
```
3183 @ 0x42e78a 0x42e83e 0x40540b 0x4051a5 0x872f76 0x45bfc1
```

So, all instances of the goroutine failing to exit due to the remote
peer not finishing the p2p version negotiation handshake.

Our fix is simple: make the `negotiateErr' channel unbuffered. With this
simple change, we ensure that the goroutine will always exit even in the
case that the parent goroutine exists due to a timeout.  # Please enter
the commit message for your changes. Lines starting
2018-07-21 17:41:23 -07:00
Olaoluwa Osuntokun
5596b63846 multi: fix linter warnings 2018-05-23 16:46:15 -07:00
Jim Posen
0581e18840 wire: Define CFCheckpt message. 2018-05-23 16:46:15 -07:00
Jim Posen
336b18c584 wire: Define GetCFCheckpt message. 2018-05-23 16:46:15 -07:00
Jim Posen
7a53a05878 multi: Redefine GetCFHeaders to have StartHeight and StopHash. 2018-05-23 16:46:15 -07:00
Jim Posen
daac60675e multi: Redefine GetCFilters to have StartHeight and StopHash. 2018-05-23 16:46:15 -07:00
Jim Posen
621f347929 wire: Remove cftypes and getcftypes commands. 2018-05-23 16:46:15 -07:00
Alex
c7e7acc7fd multi: use hidden varint for cftypes count; make filter type enum, not uint8 2018-05-23 16:46:15 -07:00
Alex
2b7ebfd698 peer: add OnCFTypes and OnGetCFTypes callbacks 2018-05-23 16:46:15 -07:00
Alex
621c73dad1 multi: change cfilter Extended bool to FilterType uint8
The cfilter BIP specifies that the filter type is a uint8. The
current code encodes it correctly on the wire, but everywhere else,
it's treated as a boolean (false for basic filter, true for
extended). This commit corrects that to account for possible
additional filter types in the future. All package changes are
done in one commit as they're all interdependent. The following
packages are updated:

* blockchain/indexers
* btcjson
* peer
* wire
* main (server.go and rpcserver.go)
2018-05-23 16:46:15 -07:00
Olaoluwa Osuntokun
8aeb8c3ef2 peer: temporarily disable stall detection 2018-05-23 16:46:15 -07:00
Olaoluwa Osuntokun
9a30e95d83 peer: don't inv trickle new blocks 2018-05-23 16:46:15 -07:00