Skip to content
Bitcoin Roots

v30.3-roots.1 · Commit 3: [PATCH 03/68] feat(policy): preserve Roots relay and network controls on Core 30.3

src/net.h

Before: src/net.h · After: src/net.h

File metadata
index afbcc52bd0fd953bcb19364a4d1619a8b7e32c16..541ca3d6097bbd50ae5df67fa3667acb2c2edbb3 100644

Download original patch · Release notes

Diff rows 1–214 of 214. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/net.h CHANGED
@@ -32,6 +32,7 @@
32
  #include <util/check.h>
33
  #include <util/sock.h>
34
  #include <util/threadinterrupt.h>
 
35
 
36
  #include <atomic>
37
  #include <condition_variable>
@@ -97,6 +98,9 @@ static constexpr bool DEFAULT_V2_TRANSPORT{true};
97
 
98
  typedef int64_t NodeId;
99
 
 
 
 
100
  struct AddedNodeParams {
101
  std::string m_added_node;
102
  bool m_use_v2transport;
@@ -149,7 +153,7 @@ enum
149
  LOCAL_NONE, // unknown
150
  LOCAL_IF, // address a local interface listens on
151
  LOCAL_BIND, // address explicit bound to
152
- LOCAL_MAPPED, // address reported by PCP
153
  LOCAL_MANUAL, // address explicitly specified (-externalip=)
154
 
155
  LOCAL_MAX
@@ -222,6 +226,10 @@ public:
222
  TransportProtocolType m_transport_type;
223
  /** BIP324 session id string in hex, if any. */
224
  std::string m_session_id;
 
 
 
 
225
  };
226
 
227
 
@@ -666,6 +674,8 @@ struct CNodeOptions
666
  NetPermissionFlags permission_flags = NetPermissionFlags::None;
667
  std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
668
  bool prefer_evict = false;
 
 
669
  size_t recv_flood_size{DEFAULT_MAXRECEIVEBUFFER * 1000};
670
  bool use_v2transport = false;
671
  };
@@ -723,6 +733,7 @@ public:
723
  */
724
  std::string cleanSubVer GUARDED_BY(m_subver_mutex){};
725
  const bool m_prefer_evict{false}; // This peer is preferred for eviction.
 
726
  bool HasPermission(NetPermissionFlags permission) const {
727
  return NetPermissions::HasFlag(m_permission_flags, permission);
728
  }
@@ -738,6 +749,10 @@ public:
738
  std::atomic_bool fPauseRecv{false};
739
  std::atomic_bool fPauseSend{false};
740
 
 
 
 
 
741
  const ConnectionType m_conn_type;
742
 
743
  /** Move all messages from the received queue to the processing queue. */
@@ -889,6 +904,7 @@ public:
889
  const std::string& addrNameIn,
890
  ConnectionType conn_type_in,
891
  bool inbound_onion,
 
892
  CNodeOptions&& node_opts = {});
893
  CNode(const CNode&) = delete;
894
  CNode& operator=(const CNode&) = delete;
@@ -947,6 +963,25 @@ public:
947
 
948
  void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950
  std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
951
 
952
  /**
@@ -971,6 +1006,9 @@ public:
971
  m_min_ping_time = std::min(m_min_ping_time.load(), ping_time);
972
  }
973
 
 
 
 
974
  private:
975
  const NodeId id;
976
  const uint64_t nLocalHostNonce;
@@ -1071,6 +1109,7 @@ public:
1071
  std::vector<NetWhitebindPermissions> vWhiteBinds;
1072
  std::vector<CService> vBinds;
1073
  std::vector<CService> onion_binds;
 
1074
  /// True if the user did not specify -bind= or -whitebind= and thus
1075
  /// we should bind on `0.0.0.0` (IPv4) and `::` (IPv6).
1076
  bool bind_on_any;
@@ -1080,6 +1119,8 @@ public:
1080
  bool m_i2p_accept_incoming;
1081
  bool whitelist_forcerelay = DEFAULT_WHITELISTFORCERELAY;
1082
  bool whitelist_relay = DEFAULT_WHITELISTRELAY;
 
 
1083
  };
1084
 
1085
  void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
@@ -1114,11 +1155,18 @@ public:
1114
  m_added_node_params.push_back({added_node, use_v2transport});
1115
  }
1116
  }
 
1117
  m_onion_binds = connOptions.onion_binds;
 
1118
  whitelist_forcerelay = connOptions.whitelist_forcerelay;
1119
  whitelist_relay = connOptions.whitelist_relay;
 
 
1120
  }
1121
 
 
 
 
1122
  CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, const NetGroupManager& netgroupman,
1123
  const CChainParams& params, bool network_active = true);
1124
 
@@ -1127,9 +1175,10 @@ public:
1127
  bool Start(CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex, !m_added_nodes_mutex, !m_addr_fetches_mutex, !mutexMsgProc);
1128
 
1129
  void StopThreads();
1130
- void StopNodes();
1131
- void Stop()
1132
  {
 
1133
  StopThreads();
1134
  StopNodes();
1135
  };
@@ -1281,10 +1330,13 @@ public:
1281
  void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
1282
 
1283
  /** Return true if we should disconnect the peer for failing an inactivity check. */
1284
- bool ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const;
1285
 
1286
  bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
1287
 
 
 
 
1288
  private:
1289
  struct ListenSocket {
1290
  public:
@@ -1331,7 +1383,7 @@ private:
1331
  void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
1332
  void NotifyNumConnectionsChanged();
1333
  /** Return true if the peer is inactive and should be disconnected. */
1334
- bool InactivityCheck(const CNode& node) const;
1335
 
1336
  /**
1337
  * Generate a collection of sockets to check for IO readiness.
@@ -1375,7 +1427,14 @@ private:
1375
  */
1376
  bool AlreadyConnectedToAddress(const CAddress& addr);
1377
 
1378
- bool AttemptToEvictConnection();
 
 
 
 
 
 
 
1379
  CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1380
  void AddWhitelistPermissionFlags(NetPermissionFlags& flags, std::optional<CNetAddr> addr, const std::vector<NetWhitelistPermissions>& ranges) const;
1381
 
@@ -1586,11 +1645,14 @@ private:
1586
  */
1587
  std::atomic_bool m_start_extra_block_relay_peers{false};
1588
 
 
 
1589
  /**
1590
  * A vector of -bind=<address>:<port>=onion arguments each of which is
1591
  * an address and port that are designated for incoming Tor connections.
1592
  */
1593
  std::vector<CService> m_onion_binds;
 
1594
 
1595
  /**
1596
  * flag for adding 'forcerelay' permission to whitelisted inbound
@@ -1604,6 +1666,18 @@ private:
1604
  */
1605
  bool whitelist_relay;
1606
 
 
 
 
 
 
 
 
 
 
 
 
 
1607
  /**
1608
  * Mutex protecting m_i2p_sam_sessions.
1609
  */
 
32
  #include <util/check.h>
33
  #include <util/sock.h>
34
  #include <util/threadinterrupt.h>
35
+ #include <util/time.h>
36
 
37
  #include <atomic>
38
  #include <condition_variable>
 
98
 
99
  typedef int64_t NodeId;
100
 
101
+ /** Get the score of a local address. */
102
+ int GetnScore(const CService& addr);
103
+
104
  struct AddedNodeParams {
105
  std::string m_added_node;
106
  bool m_use_v2transport;
 
153
  LOCAL_NONE, // unknown
154
  LOCAL_IF, // address a local interface listens on
155
  LOCAL_BIND, // address explicit bound to
156
+ LOCAL_MAPPED, // address reported by UPnP or PCP
157
  LOCAL_MANUAL, // address explicitly specified (-externalip=)
158
 
159
  LOCAL_MAX
 
226
  TransportProtocolType m_transport_type;
227
  /** BIP324 session id string in hex, if any. */
228
  std::string m_session_id;
229
+ /** whether this peer forced its connection by evicting another */
230
+ bool m_forced_inbound;
231
+ /** CPU time spent processing messages to/from the peer. */
232
+ std::chrono::nanoseconds m_cpu_time;
233
  };
234
 
235
 
 
674
  NetPermissionFlags permission_flags = NetPermissionFlags::None;
675
  std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
676
  bool prefer_evict = false;
677
+ // True if ForceInbound connection required evicting a peer
678
+ bool forced_inbound{false};
679
  size_t recv_flood_size{DEFAULT_MAXRECEIVEBUFFER * 1000};
680
  bool use_v2transport = false;
681
  };
 
733
  */
734
  std::string cleanSubVer GUARDED_BY(m_subver_mutex){};
735
  const bool m_prefer_evict{false}; // This peer is preferred for eviction.
736
+ const bool m_forced_inbound{false}; // This peer forced an inbound connection
737
  bool HasPermission(NetPermissionFlags permission) const {
738
  return NetPermissions::HasFlag(m_permission_flags, permission);
739
  }
 
749
  std::atomic_bool fPauseRecv{false};
750
  std::atomic_bool fPauseSend{false};
751
 
752
+ /** Network key used to prevent fingerprinting our node across networks.
753
+ * Influenced by the network and the bind address (+ bind port for inbounds) */
754
+ const uint64_t m_network_key;
755
+
756
  const ConnectionType m_conn_type;
757
 
758
  /** Move all messages from the received queue to the processing queue. */
 
904
  const std::string& addrNameIn,
905
  ConnectionType conn_type_in,
906
  bool inbound_onion,
907
+ uint64_t network_key,
908
  CNodeOptions&& node_opts = {});
909
  CNode(const CNode&) = delete;
910
  CNode& operator=(const CNode&) = delete;
 
963
 
964
  void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
965
 
966
+ bool PunishInvalidBlocks() const
967
+ {
968
+ if (HasPermission(NetPermissionFlags::NoBan)) {
969
+ return false;
970
+ }
971
+ switch (m_conn_type) {
972
+ case ConnectionType::INBOUND:
973
+ case ConnectionType::MANUAL:
974
+ case ConnectionType::FEELER:
975
+ return false;
976
+ case ConnectionType::OUTBOUND_FULL_RELAY:
977
+ case ConnectionType::BLOCK_RELAY:
978
+ case ConnectionType::ADDR_FETCH:
979
+ return true;
980
+ } // no default case, so the compiler can warn about missing cases
981
+
982
+ assert(false);
983
+ }
984
+
985
  std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
986
 
987
  /**
 
1006
  m_min_ping_time = std::min(m_min_ping_time.load(), ping_time);
1007
  }
1008
 
1009
+ /** CPU time spent processing messages to/from the peer. */
1010
+ std::atomic<std::chrono::nanoseconds> m_cpu_time;
1011
+
1012
  private:
1013
  const NodeId id;
1014
  const uint64_t nLocalHostNonce;
 
1109
  std::vector<NetWhitebindPermissions> vWhiteBinds;
1110
  std::vector<CService> vBinds;
1111
  std::vector<CService> onion_binds;
1112
+ bool listenonion{false};
1113
  /// True if the user did not specify -bind= or -whitebind= and thus
1114
  /// we should bind on `0.0.0.0` (IPv4) and `::` (IPv6).
1115
  bool bind_on_any;
 
1119
  bool m_i2p_accept_incoming;
1120
  bool whitelist_forcerelay = DEFAULT_WHITELISTFORCERELAY;
1121
  bool whitelist_relay = DEFAULT_WHITELISTRELAY;
1122
+ bool m_capture_messages = false;
1123
+ bool disable_v1conn_clearnet = false;
1124
  };
1125
 
1126
  void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
 
1155
  m_added_node_params.push_back({added_node, use_v2transport});
1156
  }
1157
  }
1158
+ m_normal_binds = connOptions.vBinds;
1159
  m_onion_binds = connOptions.onion_binds;
1160
+ m_listenonion = connOptions.listenonion;
1161
  whitelist_forcerelay = connOptions.whitelist_forcerelay;
1162
  whitelist_relay = connOptions.whitelist_relay;
1163
+ m_capture_messages = connOptions.m_capture_messages;
1164
+ disable_v1conn_clearnet = connOptions.disable_v1conn_clearnet;
1165
  }
1166
 
1167
+ // test only
1168
+ void SetCaptureMessages(bool cap) { m_capture_messages = cap; }
1169
+
1170
  CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, const NetGroupManager& netgroupman,
1171
  const CChainParams& params, bool network_active = true);
1172
 
 
1175
  bool Start(CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex, !m_added_nodes_mutex, !m_addr_fetches_mutex, !mutexMsgProc);
1176
 
1177
  void StopThreads();
1178
+ void StopNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex);
1179
+ void Stop() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex)
1180
  {
1181
+ AssertLockNotHeld(m_reconnections_mutex);
1182
  StopThreads();
1183
  StopNodes();
1184
  };
 
1330
  void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
1331
 
1332
  /** Return true if we should disconnect the peer for failing an inactivity check. */
1333
+ bool ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const;
1334
 
1335
  bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
1336
 
1337
+ /* Returns true if outbound v1 connections need to be disabled on IPV4/IPV6 network. */
1338
+ bool DisableV1OnClearnet(Network net) const;
1339
+
1340
  private:
1341
  struct ListenSocket {
1342
  public:
 
1383
  void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
1384
  void NotifyNumConnectionsChanged();
1385
  /** Return true if the peer is inactive and should be disconnected. */
1386
+ bool InactivityCheck(const CNode& node, std::chrono::microseconds now) const;
1387
 
1388
  /**
1389
  * Generate a collection of sockets to check for IO readiness.
 
1427
  */
1428
  bool AlreadyConnectedToAddress(const CAddress& addr);
1429
 
1430
+ /**
1431
+ * Attempt to disconnect a connected peer.
1432
+ * Used to make room for new inbound connections, returns true if successful.
1433
+ * @param[in] force Try to evict a random inbound ban-able peer if
1434
+ * all connections are otherwise protected.
1435
+ */
1436
+ bool AttemptToEvictConnection(bool force);
1437
+
1438
  CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1439
  void AddWhitelistPermissionFlags(NetPermissionFlags& flags, std::optional<CNetAddr> addr, const std::vector<NetWhitelistPermissions>& ranges) const;
1440
 
 
1645
  */
1646
  std::atomic_bool m_start_extra_block_relay_peers{false};
1647
 
1648
+ std::vector<CService> m_normal_binds;
1649
+
1650
  /**
1651
  * A vector of -bind=<address>:<port>=onion arguments each of which is
1652
  * an address and port that are designated for incoming Tor connections.
1653
  */
1654
  std::vector<CService> m_onion_binds;
1655
+ bool m_listenonion;
1656
 
1657
  /**
1658
  * flag for adding 'forcerelay' permission to whitelisted inbound
 
1666
  */
1667
  bool whitelist_relay;
1668
 
1669
+ /**
1670
+ * flag for whether messages are captured
1671
+ */
1672
+ bool m_capture_messages{false};
1673
+
1674
+ /**
1675
+ * option for disabling outbound v1 connections on IPV4 and IPV6.
1676
+ * outbound connections on IPV4/IPV6 need to be v2 connections.
1677
+ * outbound connections on Tor/I2P/CJDNS can be v1 or v2 connections.
1678
+ */
1679
+ bool disable_v1conn_clearnet;
1680
+
1681
  /**
1682
  * Mutex protecting m_i2p_sam_sessions.
1683
  */