Skip to content
Bitcoin Roots

v29.4-roots.2 · Commit 3: [PATCH 03/21] policy: restore conservative relay and network controls

src/net.h

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

File metadata
index e025b20bcdefb31cd92272825370d90d8a6f4ba8..6146a4f94739af109081a194ad585d619a566f62 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
@@ -31,6 +31,7 @@
31
31
  #include <util/check.h>
32
32
  #include <util/sock.h>
33
33
  #include <util/threadinterrupt.h>
34
+ #include <util/time.h>
34
35
 
35
36
  #include <atomic>
36
37
  #include <condition_variable>
@@ -96,6 +97,9 @@ static constexpr bool DEFAULT_V2_TRANSPORT{true};
96
97
 
97
98
  typedef int64_t NodeId;
98
99
 
100
+ /** Get the score of a local address. */
101
+ int GetnScore(const CService& addr);
102
+
99
103
  struct AddedNodeParams {
100
104
  std::string m_added_node;
101
105
  bool m_use_v2transport;
@@ -148,7 +152,7 @@ enum
148
152
  LOCAL_NONE, // unknown
149
153
  LOCAL_IF, // address a local interface listens on
150
154
  LOCAL_BIND, // address explicit bound to
151
- LOCAL_MAPPED, // address reported by PCP
155
+ LOCAL_MAPPED, // address reported by UPnP or PCP
152
156
  LOCAL_MANUAL, // address explicitly specified (-externalip=)
153
157
 
154
158
  LOCAL_MAX
@@ -220,6 +224,10 @@ public:
220
224
  TransportProtocolType m_transport_type;
221
225
  /** BIP324 session id string in hex, if any. */
222
226
  std::string m_session_id;
227
+ /** whether this peer forced its connection by evicting another */
228
+ bool m_forced_inbound;
229
+ /** CPU time spent processing messages to/from the peer. */
230
+ std::chrono::nanoseconds m_cpu_time;
223
231
  };
224
232
 
225
233
 
@@ -664,6 +672,8 @@ struct CNodeOptions
664
672
  NetPermissionFlags permission_flags = NetPermissionFlags::None;
665
673
  std::unique_ptr<i2p::sam::Session> i2p_sam_session = nullptr;
666
674
  bool prefer_evict = false;
675
+ // True if ForceInbound connection required evicting a peer
676
+ bool forced_inbound{false};
667
677
  size_t recv_flood_size{DEFAULT_MAXRECEIVEBUFFER * 1000};
668
678
  bool use_v2transport = false;
669
679
  };
@@ -721,6 +731,7 @@ public:
721
731
  */
722
732
  std::string cleanSubVer GUARDED_BY(m_subver_mutex){};
723
733
  const bool m_prefer_evict{false}; // This peer is preferred for eviction.
734
+ const bool m_forced_inbound{false}; // This peer forced an inbound connection
724
735
  bool HasPermission(NetPermissionFlags permission) const {
725
736
  return NetPermissions::HasFlag(m_permission_flags, permission);
726
737
  }
@@ -736,6 +747,10 @@ public:
736
747
  std::atomic_bool fPauseRecv{false};
737
748
  std::atomic_bool fPauseSend{false};
738
749
 
750
+ /** Network key used to prevent fingerprinting our node across networks.
751
+ * Influenced by the network and the bind address (+ bind port for inbounds) */
752
+ const uint64_t m_network_key;
753
+
739
754
  const ConnectionType m_conn_type;
740
755
 
741
756
  /** Move all messages from the received queue to the processing queue. */
@@ -887,6 +902,7 @@ public:
887
902
  const std::string& addrNameIn,
888
903
  ConnectionType conn_type_in,
889
904
  bool inbound_onion,
905
+ uint64_t network_key,
890
906
  CNodeOptions&& node_opts = {});
891
907
  CNode(const CNode&) = delete;
892
908
  CNode& operator=(const CNode&) = delete;
@@ -945,6 +961,25 @@ public:
945
961
 
946
962
  void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
947
963
 
964
+ bool PunishInvalidBlocks() const
965
+ {
966
+ if (HasPermission(NetPermissionFlags::NoBan)) {
967
+ return false;
968
+ }
969
+ switch (m_conn_type) {
970
+ case ConnectionType::INBOUND:
971
+ case ConnectionType::MANUAL:
972
+ case ConnectionType::FEELER:
973
+ return false;
974
+ case ConnectionType::OUTBOUND_FULL_RELAY:
975
+ case ConnectionType::BLOCK_RELAY:
976
+ case ConnectionType::ADDR_FETCH:
977
+ return true;
978
+ } // no default case, so the compiler can warn about missing cases
979
+
980
+ assert(false);
981
+ }
982
+
948
983
  std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
949
984
 
950
985
  /**
@@ -969,6 +1004,9 @@ public:
969
1004
  m_min_ping_time = std::min(m_min_ping_time.load(), ping_time);
970
1005
  }
971
1006
 
1007
+ /** CPU time spent processing messages to/from the peer. */
1008
+ std::atomic<std::chrono::nanoseconds> m_cpu_time;
1009
+
972
1010
  private:
973
1011
  const NodeId id;
974
1012
  const uint64_t nLocalHostNonce;
@@ -1069,6 +1107,7 @@ public:
1069
1107
  std::vector<NetWhitebindPermissions> vWhiteBinds;
1070
1108
  std::vector<CService> vBinds;
1071
1109
  std::vector<CService> onion_binds;
1110
+ bool listenonion{false};
1072
1111
  /// True if the user did not specify -bind= or -whitebind= and thus
1073
1112
  /// we should bind on `0.0.0.0` (IPv4) and `::` (IPv6).
1074
1113
  bool bind_on_any;
@@ -1078,6 +1117,8 @@ public:
1078
1117
  bool m_i2p_accept_incoming;
1079
1118
  bool whitelist_forcerelay = DEFAULT_WHITELISTFORCERELAY;
1080
1119
  bool whitelist_relay = DEFAULT_WHITELISTRELAY;
1120
+ bool m_capture_messages = false;
1121
+ bool disable_v1conn_clearnet = false;
1081
1122
  };
1082
1123
 
1083
1124
  void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
@@ -1112,11 +1153,18 @@ public:
1112
1153
  m_added_node_params.push_back({added_node, use_v2transport});
1113
1154
  }
1114
1155
  }
1156
+ m_normal_binds = connOptions.vBinds;
1115
1157
  m_onion_binds = connOptions.onion_binds;
1158
+ m_listenonion = connOptions.listenonion;
1116
1159
  whitelist_forcerelay = connOptions.whitelist_forcerelay;
1117
1160
  whitelist_relay = connOptions.whitelist_relay;
1161
+ m_capture_messages = connOptions.m_capture_messages;
1162
+ disable_v1conn_clearnet = connOptions.disable_v1conn_clearnet;
1118
1163
  }
1119
1164
 
1165
+ // test only
1166
+ void SetCaptureMessages(bool cap) { m_capture_messages = cap; }
1167
+
1120
1168
  CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, const NetGroupManager& netgroupman,
1121
1169
  const CChainParams& params, bool network_active = true);
1122
1170
 
@@ -1125,9 +1173,10 @@ public:
1125
1173
  bool Start(CScheduler& scheduler, const Options& options) EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex, !m_added_nodes_mutex, !m_addr_fetches_mutex, !mutexMsgProc);
1126
1174
 
1127
1175
  void StopThreads();
1128
- void StopNodes();
1129
- void Stop()
1176
+ void StopNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex);
1177
+ void Stop() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex)
1130
1178
  {
1179
+ AssertLockNotHeld(m_reconnections_mutex);
1131
1180
  StopThreads();
1132
1181
  StopNodes();
1133
1182
  };
@@ -1268,10 +1317,13 @@ public:
1268
1317
  void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
1269
1318
 
1270
1319
  /** Return true if we should disconnect the peer for failing an inactivity check. */
1271
- bool ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const;
1320
+ bool ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const;
1272
1321
 
1273
1322
  bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
1274
1323
 
1324
+ /* Returns true if outbound v1 connections need to be disabled on IPV4/IPV6 network. */
1325
+ bool DisableV1OnClearnet(Network net) const;
1326
+
1275
1327
  private:
1276
1328
  struct ListenSocket {
1277
1329
  public:
@@ -1318,7 +1370,7 @@ private:
1318
1370
  void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
1319
1371
  void NotifyNumConnectionsChanged();
1320
1372
  /** Return true if the peer is inactive and should be disconnected. */
1321
- bool InactivityCheck(const CNode& node) const;
1373
+ bool InactivityCheck(const CNode& node, std::chrono::microseconds now) const;
1322
1374
 
1323
1375
  /**
1324
1376
  * Generate a collection of sockets to check for IO readiness.
@@ -1362,7 +1414,14 @@ private:
1362
1414
  */
1363
1415
  bool AlreadyConnectedToAddress(const CAddress& addr);
1364
1416
 
1365
- bool AttemptToEvictConnection();
1417
+ /**
1418
+ * Attempt to disconnect a connected peer.
1419
+ * Used to make room for new inbound connections, returns true if successful.
1420
+ * @param[in] force Try to evict a random inbound ban-able peer if
1421
+ * all connections are otherwise protected.
1422
+ */
1423
+ bool AttemptToEvictConnection(bool force);
1424
+
1366
1425
  CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1367
1426
  void AddWhitelistPermissionFlags(NetPermissionFlags& flags, std::optional<CNetAddr> addr, const std::vector<NetWhitelistPermissions>& ranges) const;
1368
1427
 
@@ -1573,11 +1632,14 @@ private:
1573
1632
  */
1574
1633
  std::atomic_bool m_start_extra_block_relay_peers{false};
1575
1634
 
1635
+ std::vector<CService> m_normal_binds;
1636
+
1576
1637
  /**
1577
1638
  * A vector of -bind=<address>:<port>=onion arguments each of which is
1578
1639
  * an address and port that are designated for incoming Tor connections.
1579
1640
  */
1580
1641
  std::vector<CService> m_onion_binds;
1642
+ bool m_listenonion;
1581
1643
 
1582
1644
  /**
1583
1645
  * flag for adding 'forcerelay' permission to whitelisted inbound
@@ -1591,6 +1653,18 @@ private:
1591
1653
  */
1592
1654
  bool whitelist_relay;
1593
1655
 
1656
+ /**
1657
+ * flag for whether messages are captured
1658
+ */
1659
+ bool m_capture_messages{false};
1660
+
1661
+ /**
1662
+ * option for disabling outbound v1 connections on IPV4 and IPV6.
1663
+ * outbound connections on IPV4/IPV6 need to be v2 connections.
1664
+ * outbound connections on Tor/I2P/CJDNS can be v1 or v2 connections.
1665
+ */
1666
+ bool disable_v1conn_clearnet;
1667
+
1594
1668
  /**
1595
1669
  * Mutex protecting m_i2p_sam_sessions.
1596
1670
  */