Commit graph

843 commits

Author SHA1 Message Date
dergoegge
cd0c8eeb09 [net] Pass nRecvFloodSize to CNode 2023-03-27 16:00:02 +02:00
dergoegge
860402ef2e [net] Remove trivial GetConnectionType() getter 2023-03-27 16:00:02 +02:00
dergoegge
b5a85b365a [net] Delete CNetMessage copy constructor/assignment op 2023-03-27 16:00:01 +02:00
dergoegge
3566aa7d49 [net] Remove CNode friends
Both `CConnman` and `ConnmanTestMsg` no longer access private members of
`CNode`, we can therefore remove the friend relationship.
2023-03-22 13:18:57 +01:00
dergoegge
3eac5e7cd1 [net] Add CNode helper for send byte accounting 2023-03-22 13:18:57 +01:00
dergoegge
60441a3432 scripted-diff: [net] Rename CNode process queue members
-BEGIN VERIFY SCRIPT-
ren() { sed -i "s:\<$1\>:$2:g" $(git grep -l "\<$1\>" ./src ./test); }

ren cs_vProcessMsg    m_msg_process_queue_mutex
ren vProcessMsg       m_msg_process_queue
ren nProcessQueueSize m_msg_process_queue_size

-END VERIFY SCRIPT-
2023-03-22 13:18:56 +01:00
dergoegge
6693c499f7 [net] Make cs_vProcessMsg a non-recursive mutex 2023-03-22 13:18:32 +01:00
dergoegge
23d9352654 [net] Make CNode msg process queue members private
Now that all access to the process queue members is handled by methods
of `CNode` we can make these members private.
2023-03-22 13:18:32 +01:00
dergoegge
897e342d6e [net] Encapsulate CNode message polling 2023-03-22 13:18:30 +01:00
dergoegge
cc5cdf8776 [net] Deduplicate marking received message for processing 2023-03-19 14:34:37 +01:00
dergoegge
ad44aa5c64 [net] Add connection type getter to CNode 2023-03-19 14:34:36 +01:00
fanquake
30874a7cc9
Merge bitcoin/bitcoin#26837: I2P network optimizations
3c1de032de i2p: use consistent number of tunnels with i2pd and Java I2P (Vasil Dimov)
801b405f85 i2p: lower the number of tunnels for transient sessions (Vasil Dimov)
b906b64eb7 i2p: reuse created I2P sessions if not used (Vasil Dimov)

Pull request description:

  * Reuse an I2P transient session instead of discarding it if we failed to connect to the desired peer. This means we never used the generated address (destination), whose creation is not cheap. This does not mean that we will use the same address for more than one peer.
  * Lower the number of tunnels for transient sessions.
  * Explicitly specify the number of tunnels for persistent sessions instead of relying on the defaults which differ between I2P routers. This way we get consistent behavior with all routers.

  Alleviates: https://github.com/bitcoin/bitcoin/issues/26754

  (I have not tested this with i2pd, yet)

ACKs for top commit:
  jonatack:
    ACK 3c1de032de
  mzumsande:
    Light ACK 3c1de032de

Tree-SHA512: 477b4b9a5755e6a9a46bc0f7b268fa419dff4414e25445c750ae913f7552d9e2313f2aca4e3b70067b8390c2d0c2d68ec459f331765e939fc84139e454031cd4
2023-02-22 17:58:41 +00:00
Martin Zumsande
c77c877a8e net: Load fixed seeds from reachable networks for which we don't have addresses
Previously, we'd only load fixed seeds if we'd not
know any addresses at all. This change makes it possible
to change -onlynet abruptly, e.g. from -onlynet=onion to
-onlynet=i2p and still find peers.
2023-01-26 18:11:13 -05:00
Vasil Dimov
b906b64eb7
i2p: reuse created I2P sessions if not used
In the case of `i2pacceptincoming=0` we use transient addresses
(destinations) for ourselves for each outbound connection. It may
happen that we
* create the session (and thus our address/destination too)
* fail to connect to the particular peer (e.g. if they are offline)
* dispose the unused session.

This puts unnecessary load on the I2P network because session creation
is not cheap. Is exaggerated if `onlynet=i2p` is used in which case we
will be trying to connect to I2P peers more often.

To help with this, save the created but unused sessions and pick them
later instead of creating new ones.

Alleviates: https://github.com/bitcoin/bitcoin/issues/26754
2023-01-11 13:56:12 +01:00
Hennadii Stepanov
306ccd4927
scripted-diff: Bump copyright headers
-BEGIN VERIFY SCRIPT-
./contrib/devtools/copyright_header.py update ./
-END VERIFY SCRIPT-

Commits of previous years:
- 2021: f47dda2c58
- 2020: fa0074e2d8
- 2019: aaaaad6ac9
2022-12-24 23:49:50 +00:00
fanquake
1b680948d4
Merge bitcoin/bitcoin#26292: util: move threadinterrupt into util/
b89530483d util: move threadinterrupt into util (fanquake)

Pull request description:

  Alongside thread and threadnames. It's part of libbitcoin_util.

ACKs for top commit:
  ryanofsky:
    Code review ACK b89530483d. No changes since last review other than rebase
  theuni:
    ACK b89530483d.

Tree-SHA512: 0421f4d1881ec295272446804b27d16bf63e6b62b272f8bb52bfecde9ae6605e8109ed16294690d3e3ce4b15cc5e7c4046f99442df73adb10bdf069d3fb165aa
2022-11-22 09:52:53 +00:00
fanquake
b89530483d
util: move threadinterrupt into util 2022-11-01 10:14:49 +00:00
MacroFake
fa24239a1c
net: Avoid SetTxRelay for feeler connections 2022-10-27 16:09:33 +02:00
fanquake
97f865bb76
Merge bitcoin/bitcoin#25989: init: abort if i2p/cjdns are chosen via -onlynet but are unreachable
68209a7b5c rpc: make addpeeraddress work with cjdns addresses (Martin Zumsande)
a8a9ed67cc init: Abort if i2p/cjdns are chosen via -onlynet but unreachable (Martin Zumsande)

Pull request description:

  If the networks i2p / cjdns are chosen via `-onlynet` but the user forgot to provide `-i2psam` / `-cjdnsreachable`, no outbound connections will be made - it would be nice to inform the user about that.
  The solution proposed here mimics existing behavior for `-onlynet=onion` and non-specified `-onion`/`-proxy` where we already abort with an InitError - if reviewers would prefer to just print a warning, please say so.

  The second commit adds CJDNS support to the debug-only `addpeeraddress` RPC allowing to add CJDNS addresses to addrman for testing and debug purposes. (if `-cjdnsreachable=1`)

  This is the result of an [IRC discussion](https://bitcoin-irc.chaincode.com/bitcoin-core-dev/2022-09-01#848066;) with vasild.

ACKs for top commit:
  vasild:
    ACK 68209a7b5c
  dergoegge:
    ACK 68209a7b5c

Tree-SHA512: 6db9787f01820190f14f90a0b39e4206603421eb7521f792879094d8bbf4d4d0bfd70665eadcc40994ac7941a15ab5a8d65c4779fba5634c0e6fa66eb0972b8d
2022-09-21 11:00:47 +01:00
Martin Zumsande
68209a7b5c rpc: make addpeeraddress work with cjdns addresses
This allows us to add cjdns addresses to addrman for
testing and debug purposes (if -cjdnsreachable is true)
2022-09-19 11:06:43 -04:00
Anthony Towns
bf12abe454 net: drop cs_sendProcessing
SendMessages() is now protected g_msgproc_mutex; so this additional
per-node mutex is redundant.
2022-09-15 14:44:42 +10:00
Anthony Towns
1e78f566d5 net: add NetEventsInterface::g_msgproc_mutex
There are many cases where we assume message processing is
single-threaded in order for how we access node-related memory to be
safe. Add an explicit mutex that we can use to document this, which allows
the compiler to catch any cases where we try to access that memory from
other threads and break that assumption.
2022-09-15 14:44:38 +10:00
Anthony Towns
377e9ccda4 scripted-diff: net: rename permissionFlags to permission_flags
-BEGIN VERIFY SCRIPT-
sed -i 's/permissionFlags/permission_flags/g' $(git grep -l permissionFlags)
-END VERIFY SCRIPT-
2022-09-01 20:55:22 +10:00
Anthony Towns
0a7fc42897 net: make CNode::m_prefer_evict const 2022-09-01 20:54:35 +10:00
Anthony Towns
d394156b99 net: make CNode::m_permissionFlags const 2022-09-01 20:53:57 +10:00
Anthony Towns
9dccc3328e net: add CNodeOptions for optional CNode constructor params 2022-09-01 20:52:20 +10:00
Anthony Towns
9816dc96b7 net: note CNode members that are treated as const
m_permissionFlags and m_prefer_evict are treated as const -- they're
only set immediately after construction before any other thread has
access to the object, and not changed again afterwards. As such they
don't need to be marked atomic or guarded by a mutex; though it would
probably be better to actually mark them as const...
2022-08-29 22:50:54 +10:00
Anthony Towns
ef26f2f421 net: mark CNode unique_ptr members as const
Dereferencing a unique_ptr is not necessarily thread safe. The reason
these are safe is because their values are set at construction and do
not change later; so mark them as const and set them via the initializer
list to guarantee that.
2022-08-29 22:50:54 +10:00
Anthony Towns
bbec32c9ad net: mark TransportSerializer/m_serializer as const
The (V1)TransportSerializer instance CNode::m_serializer is used from
multiple threads via PushMessage without protection by a mutex. This
is only thread safe because the class does not have any mutable state,
so document that by marking the methods and the object as "const".
2022-08-29 22:50:54 +10:00
Anthony Towns
06ebdc886f net/net_processing: add missing thread safety annotations 2022-08-29 22:50:51 +10:00
Vasil Dimov
ae1e97ce86
net: use transient I2P session for outbound if -i2pacceptincoming=0
If not accepting I2P connections, then do not create
`CConnman::m_i2p_sam_session`.

When opening a new outbound I2P connection either use
`CConnman::m_i2p_sam_session` like before or create a temporary one and
store it in `CNode` for destruction later.
2022-08-16 13:02:18 +02:00
Vasil Dimov
a1580a04f5
net: store an optional I2P session in CNode
and destroy it when `CNode::m_sock` is closed.

I2P transient sessions are created per connection (i.e. per `CNode`) and
should be destroyed when the connection is closed. Storing the session
in `CNode` is a convenient way to destroy it together with the connection
socket (`CNode::m_sock`).

An alternative approach would be to store a list of all I2P sessions in
`CConnman` and from `CNode::CloseSocketDisconnect()` to somehow ask the
`CConnman` to destroy the relevant session.
2022-08-16 13:02:17 +02:00
fanquake
cc7b2fdd70
refactor: move compat.h into compat/ 2022-07-20 10:34:46 +01:00
John Newbery
8d8eeb422e [net processing] Remove CNode::nLocalServices 2022-07-14 15:25:15 +02:00
dergoegge
5961f8eea1 [net] Return CService from GetLocalAddrForPeer and GetLocalAddress 2022-07-14 15:24:00 +02:00
John Newbery
d9079fe18d [net processing] Remove CNode::nServices
Use Peer::m_their_services instead
2022-07-14 14:50:44 +02:00
John Newbery
f65e83d51b [net processing] Remove fClient and m_limited_node
fClient is replaced by CanServeBlocks(), and m_limited_node is replaced
by IsLimitedPeer().
2022-07-14 14:48:41 +02:00
John Newbery
1f52c47d5c [net processing] Add m_our_services and m_their_services to Peer
Track services offered by us and the peer in the Peer object.
2022-07-14 14:44:44 +02:00
dergoegge
0101d2bc3c [net] Move eviction logic to its own file 2022-07-06 18:13:54 +02:00
Cory Fields
c741d748d4 [net] Move ConnectionType to its own file 2022-07-06 18:13:53 +02:00
dergoegge
a3c2707039 [net] Add connection type to NodeEvictionCandidate 2022-07-04 14:58:43 +02:00
dergoegge
42aa5d5b62 [net] Add NoBan status to NodeEvictionCandidate 2022-07-04 14:57:49 +02:00
laanwj
0ea92cad52
Merge bitcoin/bitcoin#24356: refactor: replace CConnman::SocketEvents() with mockable Sock::WaitMany()
6e68ccbefe net: use Sock::WaitMany() instead of CConnman::SocketEvents() (Vasil Dimov)
ae263460ba net: introduce Sock::WaitMany() (Vasil Dimov)
cc74459768 net: also wait for exceptional events in Sock::Wait() (Vasil Dimov)

Pull request description:

  _This is a piece of #21878, chopped off to ease review._

  `Sock::Wait()` waits for IO events on one socket. Introduce a similar `virtual` method `WaitMany()` that waits simultaneously for IO events on more than one socket.

  Use `WaitMany()` instead of `CConnman::SocketEvents()` (and ditch the latter). Given that the former is a `virtual` method, it can be mocked by unit and fuzz tests. This will help to make bigger parts of `CConnman` testable (unit and fuzz).

ACKs for top commit:
  laanwj:
    Code review ACK 6e68ccbefe
  jonatack:
    re-ACK 6e68ccbefe per `git range-diff e18fd47 6747729 6e68ccb`, and verified rebase to master and debug build

Tree-SHA512: 917fb6ad880d64d3af1ebb301c06fbd01afd8ff043f49e4055a088ebed6affb7ffe1dcf59292d822f10de5f323b6d52d557cb081dd7434634995f9148efcf08f
2022-06-16 20:05:03 +02:00
Vasil Dimov
6e68ccbefe
net: use Sock::WaitMany() instead of CConnman::SocketEvents()
Rename `GenerateSelectSet()` to `GenerateWaitSockets()` and adapt it to
generate a wait data suitable for `Sock::WaitMany()`. Then call it from
`CConnman::SocketHandler()` and feed the generated data to
`Sock::WaitMany()`.

This way `CConnman::SocketHandler()` can be unit tested because
`Sock::WaitMany()` is mockable, so the usage of real sockets can be
avoided.

Resolves https://github.com/bitcoin/bitcoin/issues/21744
2022-06-09 16:20:24 +02:00
Anthony Towns
bba87c0553 scripted-diff: Convert global Mutexes to GlobalMutexes
-BEGIN VERIFY SCRIPT-
sed -i -E -e '/^([a-z]+ )?Mutex [a-z]/ s/Mutex/GlobalMutex/' $(git grep -lE '^([a-z]+ )?Mutex [a-z]')
-END VERIFY SCRIPT-
2022-05-21 01:23:23 +10:00
MacroFake
aa3200d896
Merge bitcoin/bitcoin#25109: Strengthen AssertLockNotHeld assertions
436ce0233c sync.h: strengthen AssertLockNotHeld assertion (Anthony Towns)
7d73f58e9c Increase threadsafety annotation coverage (Anthony Towns)

Pull request description:

  This changes `AssertLockNotHeld` so that it is annotated with the negative capability for the mutex it refers to. clang applies negative capabilities recursively, so this helps avoid forgetting to annotate functions.

  Note that this can't reasonably be used for globals, because clang would require every function to be annotated with `EXCLUSIVE_LOCKS_REQUIRED(!g_mutex)` for each global mutex. At present, the only global mutexes that use `AssertLockNotHeld` are `RecursiveMutex` so we treat that as an exception in order to avoid having to add an excessive number of negative annotations.

ACKs for top commit:
  vasild:
    ACK 436ce0233c
  MarcoFalke:
    review ACK 436ce0233c 🌺

Tree-SHA512: 5f16d098790a36b5277324d5ee89cdc87033c19b11c7943c2f630a41c2e3998eb39d356a763e857f4d8fefb6c0c02291f720bb6769bcbdf5e2cd765bf266ab8c
2022-05-16 14:18:08 +02:00
Jon Atack
51ec96b904 refactor: move StartExtraBlockRelayPeers from header to implementation
where all the other logging actions in src/net.{h,cpp} are located.

StartExtraBlockRelayPeers() does not appear to be a hotspot that needs to be
inlined for performance, as it is called from CheckForStaleTipAndEvictPeers(),
called in turn from StartScheduledTasks() with a scheduleEvery delta of 45
seconds, called at the end of AppInitMain() on bitcoind startup.

This allows dropping `#include <logging.h>` from net.h, which can improve
compile time/speed. Currently, none of the other includes in net.h use
logging.h, except src/sync.h if DEBUG_LOCKCONTENTION is defined.
2022-05-12 17:41:32 +02:00
Anthony Towns
7d73f58e9c Increase threadsafety annotation coverage 2022-05-12 02:25:55 +10:00
MacroFake
0d080a183b
Merge bitcoin/bitcoin#24141: Rename message_command variables in src/net* and src/rpc/net.cpp
e71c51b27d refactor: rename command -> message type in comments in the src/net* files (Shashwat)
2b09593bdd scripted-diff: Rename message command to message type (Shashwat)

Pull request description:

  This PR is a follow-up to #24078.

  > a message is not a command, but simply a message of some type

  The first commit covers the message_command variable name and comments not addressed in the original PR in `src/net*` files.
  The second commit goes beyond the original `src/net*` limit of #24078 and does similar changes in the `src/rpc/net.cpp` file.

ACKs for top commit:
  MarcoFalke:
    review ACK e71c51b27d 💥

Tree-SHA512: 24015d132c00f15239e5d3dc7aedae904ae3103a90920bb09e984ff57723402763f697d886322f78e42a0cb46808cb6bc9d4905561dc6ddee9961168f8324b05
2022-05-05 08:37:35 +02:00
laanwj
a19f641a80
Merge bitcoin/bitcoin#24157: p2p: Replace RecursiveMutex cs_totalBytesSent with Mutex and rename it
709af67add p2p: replace RecursiveMutex `m_total_bytes_sent_mutex` with Mutex (w0xlt)
8be75fd0f0 p2p: add assertions and negative TS annotations for `m_total_bytes_sent_mutex` (w0xlt)
a237a065cc scripted-diff: rename cs_totalBytesSent -> m_total_bytes_sent_mutex (w0xlt)

Pull request description:

  Related to #19303, this PR gets rid of the RecursiveMutex `cs_totalBytesSent` and also adds `AssertLockNotHeld` macros combined with `LOCKS_EXCLUDED` thread safety annotations to avoid recursive locking.

ACKs for top commit:
  jonatack:
    ACK 709af67add per `git range-diff 7a4ac71 eff7918 709af67`, rebase to master, clang 15 debug build, and build with -Wthread-safety-negative
  vasild:
    ACK 709af67add
  hebasto:
    ACK 709af67add, tested on Ubuntu 22.04.

Tree-SHA512: 560b4e6c92b1511911d69185207df6ee809db09b96d97f96430d8d2595dc05c98cc691aaec8a58ef87cf2ab0a98675c210b8ce0be3dedb81e31114bbbfdfd8be
2022-04-26 10:21:52 +02:00