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. The two line-number columns refer to before and after.

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