mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 23:07:59 +01:00
Merge bitcoin/bitcoin#23758: net: Use type-safe mockable time for peer connection time
fad943821e
scripted-diff: Rename touched member variables (MarcoFalke)fa663a4c0d
Use mockable time for peer connection time (MarcoFalke)fad7ead146
refactor: Use type-safe std::chrono in net (MarcoFalke) Pull request description: Benefits: * Type-safe * Mockable * Allows to revert a temporary test workaround ACKs for top commit: naumenkogs: ACKfad943821e
shaavan: ACKfad943821e
Tree-SHA512: af9bdfc695ab727b100c6810a7289d29b02b0ea9fa4fee9cc1f3eeefb52c8c465ea2734bae0c1c63b3b0d6264ba2c493268bc970ef6916570eb166de77829d82
This commit is contained in:
commit
60b5795133
11 changed files with 102 additions and 95 deletions
|
@ -42,7 +42,7 @@ static void EvictionProtection0Networks250Candidates(benchmark::Bench& bench)
|
||||||
bench,
|
bench,
|
||||||
250 /* num_candidates */,
|
250 /* num_candidates */,
|
||||||
[](NodeEvictionCandidate& c) {
|
[](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_network = NET_IPV4;
|
c.m_network = NET_IPV4;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ static void EvictionProtection1Networks250Candidates(benchmark::Bench& bench)
|
||||||
bench,
|
bench,
|
||||||
250 /* num_candidates */,
|
250 /* num_candidates */,
|
||||||
[](NodeEvictionCandidate& c) {
|
[](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = false;
|
c.m_is_local = false;
|
||||||
if (c.id >= 130 && c.id < 240) { // 110 Tor
|
if (c.id >= 130 && c.id < 240) { // 110 Tor
|
||||||
c.m_network = NET_ONION;
|
c.m_network = NET_ONION;
|
||||||
|
@ -69,7 +69,7 @@ static void EvictionProtection2Networks250Candidates(benchmark::Bench& bench)
|
||||||
bench,
|
bench,
|
||||||
250 /* num_candidates */,
|
250 /* num_candidates */,
|
||||||
[](NodeEvictionCandidate& c) {
|
[](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = false;
|
c.m_is_local = false;
|
||||||
if (c.id >= 90 && c.id < 160) { // 70 Tor
|
if (c.id >= 90 && c.id < 160) { // 70 Tor
|
||||||
c.m_network = NET_ONION;
|
c.m_network = NET_ONION;
|
||||||
|
@ -87,7 +87,7 @@ static void EvictionProtection3Networks050Candidates(benchmark::Bench& bench)
|
||||||
bench,
|
bench,
|
||||||
50 /* num_candidates */,
|
50 /* num_candidates */,
|
||||||
[](NodeEvictionCandidate& c) {
|
[](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 28 || c.id == 47); // 2 localhost
|
c.m_is_local = (c.id == 28 || c.id == 47); // 2 localhost
|
||||||
if (c.id >= 30 && c.id < 47) { // 17 I2P
|
if (c.id >= 30 && c.id < 47) { // 17 I2P
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -105,7 +105,7 @@ static void EvictionProtection3Networks100Candidates(benchmark::Bench& bench)
|
||||||
bench,
|
bench,
|
||||||
100 /* num_candidates */,
|
100 /* num_candidates */,
|
||||||
[](NodeEvictionCandidate& c) {
|
[](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id >= 55 && c.id < 60); // 5 localhost
|
c.m_is_local = (c.id >= 55 && c.id < 60); // 5 localhost
|
||||||
if (c.id >= 70 && c.id < 80) { // 10 I2P
|
if (c.id >= 70 && c.id < 80) { // 10 I2P
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -123,7 +123,7 @@ static void EvictionProtection3Networks250Candidates(benchmark::Bench& bench)
|
||||||
bench,
|
bench,
|
||||||
250 /* num_candidates */,
|
250 /* num_candidates */,
|
||||||
[](NodeEvictionCandidate& c) {
|
[](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id >= 140 && c.id < 160); // 20 localhost
|
c.m_is_local = (c.id >= 140 && c.id < 160); // 20 localhost
|
||||||
if (c.id >= 170 && c.id < 180) { // 10 I2P
|
if (c.id >= 170 && c.id < 180) { // 10 I2P
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
|
34
src/net.cpp
34
src/net.cpp
|
@ -588,9 +588,9 @@ void CNode::CopyStats(CNodeStats& stats)
|
||||||
}
|
}
|
||||||
X(m_last_send);
|
X(m_last_send);
|
||||||
X(m_last_recv);
|
X(m_last_recv);
|
||||||
X(nLastTXTime);
|
X(m_last_tx_time);
|
||||||
X(nLastBlockTime);
|
X(m_last_block_time);
|
||||||
X(nTimeConnected);
|
X(m_connected);
|
||||||
X(nTimeOffset);
|
X(nTimeOffset);
|
||||||
X(m_addr_name);
|
X(m_addr_name);
|
||||||
X(nVersion);
|
X(nVersion);
|
||||||
|
@ -847,7 +847,7 @@ static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate &a, const
|
||||||
|
|
||||||
static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
||||||
{
|
{
|
||||||
return a.nTimeConnected > b.nTimeConnected;
|
return a.m_connected > b.m_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
|
static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b) {
|
||||||
|
@ -857,27 +857,27 @@ static bool CompareNetGroupKeyed(const NodeEvictionCandidate &a, const NodeEvict
|
||||||
static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
static bool CompareNodeBlockTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
||||||
{
|
{
|
||||||
// There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
|
// There is a fall-through here because it is common for a node to have many peers which have not yet relayed a block.
|
||||||
if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
|
if (a.m_last_block_time != b.m_last_block_time) return a.m_last_block_time < b.m_last_block_time;
|
||||||
if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
|
if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
|
||||||
return a.nTimeConnected > b.nTimeConnected;
|
return a.m_connected > b.m_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
||||||
{
|
{
|
||||||
// There is a fall-through here because it is common for a node to have more than a few peers that have not yet relayed txn.
|
// There is a fall-through here because it is common for a node to have more than a few peers that have not yet relayed txn.
|
||||||
if (a.nLastTXTime != b.nLastTXTime) return a.nLastTXTime < b.nLastTXTime;
|
if (a.m_last_tx_time != b.m_last_tx_time) return a.m_last_tx_time < b.m_last_tx_time;
|
||||||
if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
|
if (a.fRelayTxes != b.fRelayTxes) return b.fRelayTxes;
|
||||||
if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
|
if (a.fBloomFilter != b.fBloomFilter) return a.fBloomFilter;
|
||||||
return a.nTimeConnected > b.nTimeConnected;
|
return a.m_connected > b.m_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pick out the potential block-relay only peers, and sort them by last block time.
|
// Pick out the potential block-relay only peers, and sort them by last block time.
|
||||||
static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
static bool CompareNodeBlockRelayOnlyTime(const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
|
||||||
{
|
{
|
||||||
if (a.fRelayTxes != b.fRelayTxes) return a.fRelayTxes;
|
if (a.fRelayTxes != b.fRelayTxes) return a.fRelayTxes;
|
||||||
if (a.nLastBlockTime != b.nLastBlockTime) return a.nLastBlockTime < b.nLastBlockTime;
|
if (a.m_last_block_time != b.m_last_block_time) return a.m_last_block_time < b.m_last_block_time;
|
||||||
if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
|
if (a.fRelevantServices != b.fRelevantServices) return b.fRelevantServices;
|
||||||
return a.nTimeConnected > b.nTimeConnected;
|
return a.m_connected > b.m_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -896,7 +896,7 @@ struct CompareNodeNetworkTime {
|
||||||
{
|
{
|
||||||
if (m_is_local && a.m_is_local != b.m_is_local) return b.m_is_local;
|
if (m_is_local && a.m_is_local != b.m_is_local) return b.m_is_local;
|
||||||
if ((a.m_network == m_network) != (b.m_network == m_network)) return b.m_network == m_network;
|
if ((a.m_network == m_network) != (b.m_network == m_network)) return b.m_network == m_network;
|
||||||
return a.nTimeConnected > b.nTimeConnected;
|
return a.m_connected > b.m_connected;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1023,12 +1023,12 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
|
||||||
// (vEvictionCandidates is already sorted by reverse connect time)
|
// (vEvictionCandidates is already sorted by reverse connect time)
|
||||||
uint64_t naMostConnections;
|
uint64_t naMostConnections;
|
||||||
unsigned int nMostConnections = 0;
|
unsigned int nMostConnections = 0;
|
||||||
int64_t nMostConnectionsTime = 0;
|
std::chrono::seconds nMostConnectionsTime{0};
|
||||||
std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
|
std::map<uint64_t, std::vector<NodeEvictionCandidate> > mapNetGroupNodes;
|
||||||
for (const NodeEvictionCandidate &node : vEvictionCandidates) {
|
for (const NodeEvictionCandidate &node : vEvictionCandidates) {
|
||||||
std::vector<NodeEvictionCandidate> &group = mapNetGroupNodes[node.nKeyedNetGroup];
|
std::vector<NodeEvictionCandidate> &group = mapNetGroupNodes[node.nKeyedNetGroup];
|
||||||
group.push_back(node);
|
group.push_back(node);
|
||||||
const int64_t grouptime = group[0].nTimeConnected;
|
const auto grouptime{group[0].m_connected};
|
||||||
|
|
||||||
if (group.size() > nMostConnections || (group.size() == nMostConnections && grouptime > nMostConnectionsTime)) {
|
if (group.size() > nMostConnections || (group.size() == nMostConnections && grouptime > nMostConnectionsTime)) {
|
||||||
nMostConnections = group.size();
|
nMostConnections = group.size();
|
||||||
|
@ -1072,8 +1072,8 @@ bool CConnman::AttemptToEvictConnection()
|
||||||
peer_relay_txes = node->m_tx_relay->fRelayTxes;
|
peer_relay_txes = node->m_tx_relay->fRelayTxes;
|
||||||
peer_filter_not_null = node->m_tx_relay->pfilter != nullptr;
|
peer_filter_not_null = node->m_tx_relay->pfilter != nullptr;
|
||||||
}
|
}
|
||||||
NodeEvictionCandidate candidate = {node->GetId(), node->nTimeConnected, node->m_min_ping_time,
|
NodeEvictionCandidate candidate = {node->GetId(), node->m_connected, node->m_min_ping_time,
|
||||||
node->nLastBlockTime, node->nLastTXTime,
|
node->m_last_block_time, node->m_last_tx_time,
|
||||||
HasAllDesirableServiceFlags(node->nServices),
|
HasAllDesirableServiceFlags(node->nServices),
|
||||||
peer_relay_txes, peer_filter_not_null, node->nKeyedNetGroup,
|
peer_relay_txes, peer_filter_not_null, node->nKeyedNetGroup,
|
||||||
node->m_prefer_evict, node->addr.IsLocal(),
|
node->m_prefer_evict, node->addr.IsLocal(),
|
||||||
|
@ -1322,7 +1322,7 @@ void CConnman::NotifyNumConnectionsChanged()
|
||||||
|
|
||||||
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const
|
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const
|
||||||
{
|
{
|
||||||
return std::chrono::seconds{node.nTimeConnected} + m_peer_connect_timeout < now;
|
return node.m_connected + m_peer_connect_timeout < now;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CConnman::InactivityCheck(const CNode& node) const
|
bool CConnman::InactivityCheck(const CNode& node) const
|
||||||
|
@ -2977,7 +2977,7 @@ ServiceFlags CConnman::GetLocalServices() const
|
||||||
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
|
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
|
||||||
|
|
||||||
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
|
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
|
||||||
: nTimeConnected(GetTimeSeconds()),
|
: m_connected{GetTime<std::chrono::seconds>()},
|
||||||
addr(addrIn),
|
addr(addrIn),
|
||||||
addrBind(addrBindIn),
|
addrBind(addrBindIn),
|
||||||
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
|
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
|
||||||
|
|
20
src/net.h
20
src/net.h
|
@ -243,9 +243,9 @@ public:
|
||||||
bool fRelayTxes;
|
bool fRelayTxes;
|
||||||
std::chrono::seconds m_last_send;
|
std::chrono::seconds m_last_send;
|
||||||
std::chrono::seconds m_last_recv;
|
std::chrono::seconds m_last_recv;
|
||||||
int64_t nLastTXTime;
|
std::chrono::seconds m_last_tx_time;
|
||||||
int64_t nLastBlockTime;
|
std::chrono::seconds m_last_block_time;
|
||||||
int64_t nTimeConnected;
|
std::chrono::seconds m_connected;
|
||||||
int64_t nTimeOffset;
|
int64_t nTimeOffset;
|
||||||
std::string m_addr_name;
|
std::string m_addr_name;
|
||||||
int nVersion;
|
int nVersion;
|
||||||
|
@ -422,8 +422,8 @@ public:
|
||||||
|
|
||||||
std::atomic<std::chrono::seconds> m_last_send{0s};
|
std::atomic<std::chrono::seconds> m_last_send{0s};
|
||||||
std::atomic<std::chrono::seconds> m_last_recv{0s};
|
std::atomic<std::chrono::seconds> m_last_recv{0s};
|
||||||
//! Unix epoch time at peer connection, in seconds.
|
//! Unix epoch time at peer connection
|
||||||
const int64_t nTimeConnected;
|
const std::chrono::seconds m_connected;
|
||||||
std::atomic<int64_t> nTimeOffset{0};
|
std::atomic<int64_t> nTimeOffset{0};
|
||||||
// Address of this peer
|
// Address of this peer
|
||||||
const CAddress addr;
|
const CAddress addr;
|
||||||
|
@ -562,13 +562,13 @@ public:
|
||||||
* preliminary validity checks and was saved to disk, even if we don't
|
* preliminary validity checks and was saved to disk, even if we don't
|
||||||
* connect the block or it eventually fails connection. Used as an inbound
|
* connect the block or it eventually fails connection. Used as an inbound
|
||||||
* peer eviction criterium in CConnman::AttemptToEvictConnection. */
|
* peer eviction criterium in CConnman::AttemptToEvictConnection. */
|
||||||
std::atomic<int64_t> nLastBlockTime{0};
|
std::atomic<std::chrono::seconds> m_last_block_time{0s};
|
||||||
|
|
||||||
/** UNIX epoch time of the last transaction received from this peer that we
|
/** UNIX epoch time of the last transaction received from this peer that we
|
||||||
* had not yet seen (e.g. not already received from another peer) and that
|
* had not yet seen (e.g. not already received from another peer) and that
|
||||||
* was accepted into our mempool. Used as an inbound peer eviction criterium
|
* was accepted into our mempool. Used as an inbound peer eviction criterium
|
||||||
* in CConnman::AttemptToEvictConnection. */
|
* in CConnman::AttemptToEvictConnection. */
|
||||||
std::atomic<int64_t> nLastTXTime{0};
|
std::atomic<std::chrono::seconds> m_last_tx_time{0s};
|
||||||
|
|
||||||
/** Last measured round-trip time. Used only for RPC/GUI stats/debugging.*/
|
/** Last measured round-trip time. Used only for RPC/GUI stats/debugging.*/
|
||||||
std::atomic<std::chrono::microseconds> m_last_ping_time{0us};
|
std::atomic<std::chrono::microseconds> m_last_ping_time{0us};
|
||||||
|
@ -1274,10 +1274,10 @@ void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Spa
|
||||||
struct NodeEvictionCandidate
|
struct NodeEvictionCandidate
|
||||||
{
|
{
|
||||||
NodeId id;
|
NodeId id;
|
||||||
int64_t nTimeConnected;
|
std::chrono::seconds m_connected;
|
||||||
std::chrono::microseconds m_min_ping_time;
|
std::chrono::microseconds m_min_ping_time;
|
||||||
int64_t nLastBlockTime;
|
std::chrono::seconds m_last_block_time;
|
||||||
int64_t nLastTXTime;
|
std::chrono::seconds m_last_tx_time;
|
||||||
bool fRelevantServices;
|
bool fRelevantServices;
|
||||||
bool fRelayTxes;
|
bool fRelayTxes;
|
||||||
bool fBloomFilter;
|
bool fBloomFilter;
|
||||||
|
|
|
@ -61,8 +61,8 @@ static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60; // 20 minutes
|
||||||
static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60; // 10 minutes
|
static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60; // 10 minutes
|
||||||
/** How frequently to check for extra outbound peers and disconnect, in seconds */
|
/** How frequently to check for extra outbound peers and disconnect, in seconds */
|
||||||
static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45;
|
static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45;
|
||||||
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds */
|
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
|
||||||
static constexpr int64_t MINIMUM_CONNECT_TIME = 30;
|
static constexpr std::chrono::seconds MINIMUM_CONNECT_TIME{30};
|
||||||
/** SHA256("main address relay")[0:8] */
|
/** SHA256("main address relay")[0:8] */
|
||||||
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
|
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
|
||||||
/// Age after which a stale block will no longer be served if requested as
|
/// Age after which a stale block will no longer be served if requested as
|
||||||
|
@ -330,7 +330,7 @@ private:
|
||||||
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
|
||||||
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
|
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
|
||||||
void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
void EvictExtraOutboundPeers(std::chrono::seconds now) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
|
||||||
/** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
|
/** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
|
||||||
void ReattemptInitialBroadcast(CScheduler& scheduler);
|
void ReattemptInitialBroadcast(CScheduler& scheduler);
|
||||||
|
@ -2511,7 +2511,7 @@ void PeerManagerImpl::ProcessBlock(CNode& node, const std::shared_ptr<const CBlo
|
||||||
bool new_block{false};
|
bool new_block{false};
|
||||||
m_chainman.ProcessNewBlock(m_chainparams, block, force_processing, &new_block);
|
m_chainman.ProcessNewBlock(m_chainparams, block, force_processing, &new_block);
|
||||||
if (new_block) {
|
if (new_block) {
|
||||||
node.nLastBlockTime = GetTime();
|
node.m_last_block_time = GetTime<std::chrono::seconds>();
|
||||||
} else {
|
} else {
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
mapBlockSource.erase(block->GetHash());
|
mapBlockSource.erase(block->GetHash());
|
||||||
|
@ -3314,7 +3314,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||||
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
|
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
|
||||||
m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
|
m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
|
||||||
|
|
||||||
pfrom.nLastTXTime = GetTime();
|
pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();
|
||||||
|
|
||||||
LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
|
LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
|
||||||
pfrom.GetId(),
|
pfrom.GetId(),
|
||||||
|
@ -4228,7 +4228,7 @@ void PeerManagerImpl::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PeerManagerImpl::EvictExtraOutboundPeers(int64_t time_in_seconds)
|
void PeerManagerImpl::EvictExtraOutboundPeers(std::chrono::seconds now)
|
||||||
{
|
{
|
||||||
// If we have any extra block-relay-only peers, disconnect the youngest unless
|
// If we have any extra block-relay-only peers, disconnect the youngest unless
|
||||||
// it's given us a block -- in which case, compare with the second-youngest, and
|
// it's given us a block -- in which case, compare with the second-youngest, and
|
||||||
|
@ -4237,14 +4237,14 @@ void PeerManagerImpl::EvictExtraOutboundPeers(int64_t time_in_seconds)
|
||||||
// to temporarily in order to sync our tip; see net.cpp.
|
// to temporarily in order to sync our tip; see net.cpp.
|
||||||
// Note that we use higher nodeid as a measure for most recent connection.
|
// Note that we use higher nodeid as a measure for most recent connection.
|
||||||
if (m_connman.GetExtraBlockRelayCount() > 0) {
|
if (m_connman.GetExtraBlockRelayCount() > 0) {
|
||||||
std::pair<NodeId, int64_t> youngest_peer{-1, 0}, next_youngest_peer{-1, 0};
|
std::pair<NodeId, std::chrono::seconds> youngest_peer{-1, 0}, next_youngest_peer{-1, 0};
|
||||||
|
|
||||||
m_connman.ForEachNode([&](CNode* pnode) {
|
m_connman.ForEachNode([&](CNode* pnode) {
|
||||||
if (!pnode->IsBlockOnlyConn() || pnode->fDisconnect) return;
|
if (!pnode->IsBlockOnlyConn() || pnode->fDisconnect) return;
|
||||||
if (pnode->GetId() > youngest_peer.first) {
|
if (pnode->GetId() > youngest_peer.first) {
|
||||||
next_youngest_peer = youngest_peer;
|
next_youngest_peer = youngest_peer;
|
||||||
youngest_peer.first = pnode->GetId();
|
youngest_peer.first = pnode->GetId();
|
||||||
youngest_peer.second = pnode->nLastBlockTime;
|
youngest_peer.second = pnode->m_last_block_time;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
NodeId to_disconnect = youngest_peer.first;
|
NodeId to_disconnect = youngest_peer.first;
|
||||||
|
@ -4262,13 +4262,14 @@ void PeerManagerImpl::EvictExtraOutboundPeers(int64_t time_in_seconds)
|
||||||
// valid headers chain with at least as much work as our tip.
|
// valid headers chain with at least as much work as our tip.
|
||||||
CNodeState *node_state = State(pnode->GetId());
|
CNodeState *node_state = State(pnode->GetId());
|
||||||
if (node_state == nullptr ||
|
if (node_state == nullptr ||
|
||||||
(time_in_seconds - pnode->nTimeConnected >= MINIMUM_CONNECT_TIME && node_state->nBlocksInFlight == 0)) {
|
(now - pnode->m_connected >= MINIMUM_CONNECT_TIME && node_state->nBlocksInFlight == 0)) {
|
||||||
pnode->fDisconnect = true;
|
pnode->fDisconnect = true;
|
||||||
LogPrint(BCLog::NET, "disconnecting extra block-relay-only peer=%d (last block received at time %d)\n", pnode->GetId(), pnode->nLastBlockTime);
|
LogPrint(BCLog::NET, "disconnecting extra block-relay-only peer=%d (last block received at time %d)\n",
|
||||||
|
pnode->GetId(), count_seconds(pnode->m_last_block_time));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
LogPrint(BCLog::NET, "keeping block-relay-only peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
|
LogPrint(BCLog::NET, "keeping block-relay-only peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
|
||||||
pnode->GetId(), pnode->nTimeConnected, node_state->nBlocksInFlight);
|
pnode->GetId(), count_seconds(pnode->m_connected), node_state->nBlocksInFlight);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
@ -4308,12 +4309,13 @@ void PeerManagerImpl::EvictExtraOutboundPeers(int64_t time_in_seconds)
|
||||||
// Also don't disconnect any peer we're trying to download a
|
// Also don't disconnect any peer we're trying to download a
|
||||||
// block from.
|
// block from.
|
||||||
CNodeState &state = *State(pnode->GetId());
|
CNodeState &state = *State(pnode->GetId());
|
||||||
if (time_in_seconds - pnode->nTimeConnected > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0) {
|
if (now - pnode->m_connected > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0) {
|
||||||
LogPrint(BCLog::NET, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode->GetId(), oldest_block_announcement);
|
LogPrint(BCLog::NET, "disconnecting extra outbound peer=%d (last block announcement received at time %d)\n", pnode->GetId(), oldest_block_announcement);
|
||||||
pnode->fDisconnect = true;
|
pnode->fDisconnect = true;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
LogPrint(BCLog::NET, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n", pnode->GetId(), pnode->nTimeConnected, state.nBlocksInFlight);
|
LogPrint(BCLog::NET, "keeping outbound peer=%d chosen for eviction (connect time: %d, blocks_in_flight: %d)\n",
|
||||||
|
pnode->GetId(), count_seconds(pnode->m_connected), state.nBlocksInFlight);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -4335,7 +4337,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
|
||||||
|
|
||||||
int64_t time_in_seconds = GetTime();
|
int64_t time_in_seconds = GetTime();
|
||||||
|
|
||||||
EvictExtraOutboundPeers(time_in_seconds);
|
EvictExtraOutboundPeers(std::chrono::seconds{time_in_seconds});
|
||||||
|
|
||||||
if (time_in_seconds > m_stale_tip_check_time) {
|
if (time_in_seconds > m_stale_tip_check_time) {
|
||||||
// Check whether our tip is stale, and if so, allow using an extra
|
// Check whether our tip is stale, and if so, allow using an extra
|
||||||
|
@ -4565,7 +4567,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
||||||
|
|
||||||
const auto current_time = GetTime<std::chrono::microseconds>();
|
const auto current_time = GetTime<std::chrono::microseconds>();
|
||||||
|
|
||||||
if (pto->IsAddrFetchConn() && current_time - std::chrono::seconds(pto->nTimeConnected) > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
|
if (pto->IsAddrFetchConn() && current_time - pto->m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
|
||||||
LogPrint(BCLog::NET, "addrfetch connection timeout; disconnecting peer=%d\n", pto->GetId());
|
LogPrint(BCLog::NET, "addrfetch connection timeout; disconnecting peer=%d\n", pto->GetId());
|
||||||
pto->fDisconnect = true;
|
pto->fDisconnect = true;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1173,9 +1173,9 @@ void RPCConsole::updateDetailWidget()
|
||||||
if (bip152_hb_settings.isEmpty()) bip152_hb_settings = ts.no;
|
if (bip152_hb_settings.isEmpty()) bip152_hb_settings = ts.no;
|
||||||
ui->peerHighBandwidth->setText(bip152_hb_settings);
|
ui->peerHighBandwidth->setText(bip152_hb_settings);
|
||||||
const auto time_now{GetTime<std::chrono::seconds>()};
|
const auto time_now{GetTime<std::chrono::seconds>()};
|
||||||
ui->peerConnTime->setText(GUIUtil::formatDurationStr(time_now - std::chrono::seconds{stats->nodeStats.nTimeConnected}));
|
ui->peerConnTime->setText(GUIUtil::formatDurationStr(time_now - stats->nodeStats.m_connected));
|
||||||
ui->peerLastBlock->setText(TimeDurationField(time_now, std::chrono::seconds{stats->nodeStats.nLastBlockTime}));
|
ui->peerLastBlock->setText(TimeDurationField(time_now, stats->nodeStats.m_last_block_time));
|
||||||
ui->peerLastTx->setText(TimeDurationField(time_now, std::chrono::seconds{stats->nodeStats.nLastTXTime}));
|
ui->peerLastTx->setText(TimeDurationField(time_now, stats->nodeStats.m_last_tx_time));
|
||||||
ui->peerLastSend->setText(TimeDurationField(time_now, stats->nodeStats.m_last_send));
|
ui->peerLastSend->setText(TimeDurationField(time_now, stats->nodeStats.m_last_send));
|
||||||
ui->peerLastRecv->setText(TimeDurationField(time_now, stats->nodeStats.m_last_recv));
|
ui->peerLastRecv->setText(TimeDurationField(time_now, stats->nodeStats.m_last_recv));
|
||||||
ui->peerBytesSent->setText(GUIUtil::formatBytes(stats->nodeStats.nSendBytes));
|
ui->peerBytesSent->setText(GUIUtil::formatBytes(stats->nodeStats.nSendBytes));
|
||||||
|
|
|
@ -198,11 +198,11 @@ static RPCHelpMan getpeerinfo()
|
||||||
obj.pushKV("relaytxes", stats.fRelayTxes);
|
obj.pushKV("relaytxes", stats.fRelayTxes);
|
||||||
obj.pushKV("lastsend", count_seconds(stats.m_last_send));
|
obj.pushKV("lastsend", count_seconds(stats.m_last_send));
|
||||||
obj.pushKV("lastrecv", count_seconds(stats.m_last_recv));
|
obj.pushKV("lastrecv", count_seconds(stats.m_last_recv));
|
||||||
obj.pushKV("last_transaction", stats.nLastTXTime);
|
obj.pushKV("last_transaction", count_seconds(stats.m_last_tx_time));
|
||||||
obj.pushKV("last_block", stats.nLastBlockTime);
|
obj.pushKV("last_block", count_seconds(stats.m_last_block_time));
|
||||||
obj.pushKV("bytessent", stats.nSendBytes);
|
obj.pushKV("bytessent", stats.nSendBytes);
|
||||||
obj.pushKV("bytesrecv", stats.nRecvBytes);
|
obj.pushKV("bytesrecv", stats.nRecvBytes);
|
||||||
obj.pushKV("conntime", stats.nTimeConnected);
|
obj.pushKV("conntime", count_seconds(stats.m_connected));
|
||||||
obj.pushKV("timeoffset", stats.nTimeOffset);
|
obj.pushKV("timeoffset", stats.nTimeOffset);
|
||||||
if (stats.m_last_ping_time > 0us) {
|
if (stats.m_last_ping_time > 0us) {
|
||||||
obj.pushKV("pingtime", CountSecondsDouble(stats.m_last_ping_time));
|
obj.pushKV("pingtime", CountSecondsDouble(stats.m_last_ping_time));
|
||||||
|
|
|
@ -131,6 +131,9 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
|
||||||
options.m_max_outbound_full_relay = max_outbound_full_relay;
|
options.m_max_outbound_full_relay = max_outbound_full_relay;
|
||||||
options.nMaxFeeler = MAX_FEELER_CONNECTIONS;
|
options.nMaxFeeler = MAX_FEELER_CONNECTIONS;
|
||||||
|
|
||||||
|
const auto time_init{GetTime<std::chrono::seconds>()};
|
||||||
|
SetMockTime(time_init);
|
||||||
|
const auto time_later{time_init + 3 * std::chrono::seconds{chainparams.GetConsensus().nPowTargetSpacing} + 1s};
|
||||||
connman->Init(options);
|
connman->Init(options);
|
||||||
std::vector<CNode *> vNodes;
|
std::vector<CNode *> vNodes;
|
||||||
|
|
||||||
|
@ -146,7 +149,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
|
||||||
BOOST_CHECK(node->fDisconnect == false);
|
BOOST_CHECK(node->fDisconnect == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
SetMockTime(GetTime() + 3 * chainparams.GetConsensus().nPowTargetSpacing + 1);
|
SetMockTime(time_later);
|
||||||
|
|
||||||
// Now tip should definitely be stale, and we should look for an extra
|
// Now tip should definitely be stale, and we should look for an extra
|
||||||
// outbound peer
|
// outbound peer
|
||||||
|
@ -161,7 +164,9 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
|
||||||
// If we add one more peer, something should get marked for eviction
|
// If we add one more peer, something should get marked for eviction
|
||||||
// on the next check (since we're mocking the time to be in the future, the
|
// on the next check (since we're mocking the time to be in the future, the
|
||||||
// required time connected check should be satisfied).
|
// required time connected check should be satisfied).
|
||||||
|
SetMockTime(time_init);
|
||||||
AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
|
AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
|
||||||
|
SetMockTime(time_later);
|
||||||
|
|
||||||
peerLogic->CheckForStaleTipAndEvictPeers();
|
peerLogic->CheckForStaleTipAndEvictPeers();
|
||||||
for (int i = 0; i < max_outbound_full_relay; ++i) {
|
for (int i = 0; i < max_outbound_full_relay; ++i) {
|
||||||
|
@ -237,7 +242,7 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
|
||||||
// Update the last block time for the extra peer,
|
// Update the last block time for the extra peer,
|
||||||
// and check that the next youngest peer gets evicted.
|
// and check that the next youngest peer gets evicted.
|
||||||
vNodes.back()->fDisconnect = false;
|
vNodes.back()->fDisconnect = false;
|
||||||
vNodes.back()->nLastBlockTime = GetTime();
|
vNodes.back()->m_last_block_time = GetTime<std::chrono::seconds>();
|
||||||
|
|
||||||
peerLogic->CheckForStaleTipAndEvictPeers();
|
peerLogic->CheckForStaleTipAndEvictPeers();
|
||||||
for (int i = 0; i < max_outbound_block_relay - 1; ++i) {
|
for (int i = 0; i < max_outbound_block_relay - 1; ++i) {
|
||||||
|
|
|
@ -21,10 +21,10 @@ FUZZ_TARGET(node_eviction)
|
||||||
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
|
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
|
||||||
eviction_candidates.push_back({
|
eviction_candidates.push_back({
|
||||||
/*id=*/fuzzed_data_provider.ConsumeIntegral<NodeId>(),
|
/*id=*/fuzzed_data_provider.ConsumeIntegral<NodeId>(),
|
||||||
/*nTimeConnected=*/fuzzed_data_provider.ConsumeIntegral<int64_t>(),
|
/*m_connected=*/std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
|
||||||
/*m_min_ping_time=*/std::chrono::microseconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
|
/*m_min_ping_time=*/std::chrono::microseconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
|
||||||
/*nLastBlockTime=*/fuzzed_data_provider.ConsumeIntegral<int64_t>(),
|
/*m_last_block_time=*/std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
|
||||||
/*nLastTXTime=*/fuzzed_data_provider.ConsumeIntegral<int64_t>(),
|
/*m_last_tx_time=*/std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
|
||||||
/*fRelevantServices=*/fuzzed_data_provider.ConsumeBool(),
|
/*fRelevantServices=*/fuzzed_data_provider.ConsumeBool(),
|
||||||
/*fRelayTxes=*/fuzzed_data_provider.ConsumeBool(),
|
/*fRelayTxes=*/fuzzed_data_provider.ConsumeBool(),
|
||||||
/*fBloomFilter=*/fuzzed_data_provider.ConsumeBool(),
|
/*fBloomFilter=*/fuzzed_data_provider.ConsumeBool(),
|
||||||
|
|
|
@ -64,11 +64,11 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
FastRandomContext random_context{true};
|
FastRandomContext random_context{true};
|
||||||
int num_peers{12};
|
int num_peers{12};
|
||||||
|
|
||||||
// Expect half of the peers with greatest uptime (the lowest nTimeConnected)
|
// Expect half of the peers with greatest uptime (the lowest m_connected)
|
||||||
// to be protected from eviction.
|
// to be protected from eviction.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [](NodeEvictionCandidate& c) {
|
num_peers, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = false;
|
c.m_is_local = false;
|
||||||
c.m_network = NET_IPV4;
|
c.m_network = NET_IPV4;
|
||||||
},
|
},
|
||||||
|
@ -79,7 +79,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// Verify in the opposite direction.
|
// Verify in the opposite direction.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [num_peers](NodeEvictionCandidate& c) {
|
num_peers, [num_peers](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = num_peers - c.id;
|
c.m_connected = std::chrono::seconds{num_peers - c.id};
|
||||||
c.m_is_local = false;
|
c.m_is_local = false;
|
||||||
c.m_network = NET_IPV6;
|
c.m_network = NET_IPV6;
|
||||||
},
|
},
|
||||||
|
@ -101,10 +101,10 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
random_context));
|
random_context));
|
||||||
|
|
||||||
// Expect 1/4 onion peers and 1/4 of the other peers to be protected,
|
// Expect 1/4 onion peers and 1/4 of the other peers to be protected,
|
||||||
// sorted by longest uptime (lowest nTimeConnected), if no localhost or I2P peers.
|
// sorted by longest uptime (lowest m_connected), if no localhost or I2P peers.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [](NodeEvictionCandidate& c) {
|
num_peers, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = false;
|
c.m_is_local = false;
|
||||||
c.m_network = (c.id == 3 || c.id > 7) ? NET_ONION : NET_IPV6;
|
c.m_network = (c.id == 3 || c.id > 7) ? NET_ONION : NET_IPV6;
|
||||||
},
|
},
|
||||||
|
@ -124,10 +124,10 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
random_context));
|
random_context));
|
||||||
|
|
||||||
// Expect 1/4 localhost peers and 1/4 of the other peers to be protected,
|
// Expect 1/4 localhost peers and 1/4 of the other peers to be protected,
|
||||||
// sorted by longest uptime (lowest nTimeConnected), if no onion or I2P peers.
|
// sorted by longest uptime (lowest m_connected), if no onion or I2P peers.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [](NodeEvictionCandidate& c) {
|
num_peers, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id > 6);
|
c.m_is_local = (c.id > 6);
|
||||||
c.m_network = NET_IPV6;
|
c.m_network = NET_IPV6;
|
||||||
},
|
},
|
||||||
|
@ -147,10 +147,10 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
random_context));
|
random_context));
|
||||||
|
|
||||||
// Expect 1/4 I2P peers and 1/4 of the other peers to be protected,
|
// Expect 1/4 I2P peers and 1/4 of the other peers to be protected,
|
||||||
// sorted by longest uptime (lowest nTimeConnected), if no onion or localhost peers.
|
// sorted by longest uptime (lowest m_connected), if no onion or localhost peers.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [](NodeEvictionCandidate& c) {
|
num_peers, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = false;
|
c.m_is_local = false;
|
||||||
c.m_network = (c.id == 4 || c.id > 8) ? NET_I2P : NET_IPV6;
|
c.m_network = (c.id == 4 || c.id > 8) ? NET_I2P : NET_IPV6;
|
||||||
},
|
},
|
||||||
|
@ -165,7 +165,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// stable sort breaks tie with array order of localhost first.
|
// stable sort breaks tie with array order of localhost first.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
4, [](NodeEvictionCandidate& c) {
|
4, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 4);
|
c.m_is_local = (c.id == 4);
|
||||||
c.m_network = (c.id == 3) ? NET_ONION : NET_IPV4;
|
c.m_network = (c.id == 3) ? NET_ONION : NET_IPV4;
|
||||||
},
|
},
|
||||||
|
@ -178,7 +178,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// uptime; stable sort breaks tie with array order of localhost first.
|
// uptime; stable sort breaks tie with array order of localhost first.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
7, [](NodeEvictionCandidate& c) {
|
7, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 6);
|
c.m_is_local = (c.id == 6);
|
||||||
c.m_network = (c.id == 5) ? NET_ONION : NET_IPV4;
|
c.m_network = (c.id == 5) ? NET_ONION : NET_IPV4;
|
||||||
},
|
},
|
||||||
|
@ -191,7 +191,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// by uptime; stable sort breaks tie with array order of localhost first.
|
// by uptime; stable sort breaks tie with array order of localhost first.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
8, [](NodeEvictionCandidate& c) {
|
8, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 6);
|
c.m_is_local = (c.id == 6);
|
||||||
c.m_network = (c.id == 5) ? NET_ONION : NET_IPV4;
|
c.m_network = (c.id == 5) ? NET_ONION : NET_IPV4;
|
||||||
},
|
},
|
||||||
|
@ -204,7 +204,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// uptime; stable sort breaks ties with the array order of localhost first.
|
// uptime; stable sort breaks ties with the array order of localhost first.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [](NodeEvictionCandidate& c) {
|
num_peers, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 6 || c.id == 9 || c.id == 11);
|
c.m_is_local = (c.id == 6 || c.id == 9 || c.id == 11);
|
||||||
c.m_network = (c.id == 7 || c.id == 8 || c.id == 10) ? NET_ONION : NET_IPV6;
|
c.m_network = (c.id == 7 || c.id == 8 || c.id == 10) ? NET_ONION : NET_IPV6;
|
||||||
},
|
},
|
||||||
|
@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// protect 2 localhost and 1 onion, plus 3 other peers, sorted by longest uptime.
|
// protect 2 localhost and 1 onion, plus 3 other peers, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [](NodeEvictionCandidate& c) {
|
num_peers, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id > 4 && c.id < 9);
|
c.m_is_local = (c.id > 4 && c.id < 9);
|
||||||
c.m_network = (c.id == 10) ? NET_ONION : NET_IPV4;
|
c.m_network = (c.id == 10) ? NET_ONION : NET_IPV4;
|
||||||
},
|
},
|
||||||
|
@ -228,7 +228,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// protect 2 localhost and 2 onions, plus 4 other peers, sorted by longest uptime.
|
// protect 2 localhost and 2 onions, plus 4 other peers, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
16, [](NodeEvictionCandidate& c) {
|
16, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 6 || c.id == 9 || c.id == 11 || c.id == 12);
|
c.m_is_local = (c.id == 6 || c.id == 9 || c.id == 11 || c.id == 12);
|
||||||
c.m_network = (c.id == 8 || c.id == 10) ? NET_ONION : NET_IPV6;
|
c.m_network = (c.id == 8 || c.id == 10) ? NET_ONION : NET_IPV6;
|
||||||
},
|
},
|
||||||
|
@ -241,7 +241,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// others, sorted by longest uptime.
|
// others, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
16, [](NodeEvictionCandidate& c) {
|
16, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id > 10);
|
c.m_is_local = (c.id > 10);
|
||||||
c.m_network = (c.id == 10) ? NET_ONION : NET_IPV4;
|
c.m_network = (c.id == 10) ? NET_ONION : NET_IPV4;
|
||||||
},
|
},
|
||||||
|
@ -254,7 +254,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// plus 4 others, sorted by longest uptime.
|
// plus 4 others, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
16, [](NodeEvictionCandidate& c) {
|
16, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 15);
|
c.m_is_local = (c.id == 15);
|
||||||
c.m_network = (c.id > 6 && c.id < 11) ? NET_ONION : NET_IPV6;
|
c.m_network = (c.id > 6 && c.id < 11) ? NET_ONION : NET_IPV6;
|
||||||
},
|
},
|
||||||
|
@ -267,7 +267,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// others, sorted by longest uptime.
|
// others, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
num_peers, [](NodeEvictionCandidate& c) {
|
num_peers, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = false;
|
c.m_is_local = false;
|
||||||
if (c.id == 8 || c.id == 10) {
|
if (c.id == 8 || c.id == 10) {
|
||||||
c.m_network = NET_ONION;
|
c.m_network = NET_ONION;
|
||||||
|
@ -288,7 +288,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// by longest uptime; stable sort breaks tie with array order of I2P first.
|
// by longest uptime; stable sort breaks tie with array order of I2P first.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
4, [](NodeEvictionCandidate& c) {
|
4, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 3);
|
c.m_is_local = (c.id == 3);
|
||||||
if (c.id == 4) {
|
if (c.id == 4) {
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -307,7 +307,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// by longest uptime; stable sort breaks tie with array order of I2P first.
|
// by longest uptime; stable sort breaks tie with array order of I2P first.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
7, [](NodeEvictionCandidate& c) {
|
7, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 4);
|
c.m_is_local = (c.id == 4);
|
||||||
if (c.id == 6) {
|
if (c.id == 6) {
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -326,7 +326,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// by uptime; stable sort breaks tie with array order of I2P then localhost.
|
// by uptime; stable sort breaks tie with array order of I2P then localhost.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
8, [](NodeEvictionCandidate& c) {
|
8, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 6);
|
c.m_is_local = (c.id == 6);
|
||||||
if (c.id == 5) {
|
if (c.id == 5) {
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -345,7 +345,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// for 8 total, sorted by longest uptime.
|
// for 8 total, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
16, [](NodeEvictionCandidate& c) {
|
16, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 6 || c.id > 11);
|
c.m_is_local = (c.id == 6 || c.id > 11);
|
||||||
if (c.id == 7 || c.id == 11) {
|
if (c.id == 7 || c.id == 11) {
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -364,7 +364,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// sorted by longest uptime.
|
// sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
24, [](NodeEvictionCandidate& c) {
|
24, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 12);
|
c.m_is_local = (c.id == 12);
|
||||||
if (c.id > 14 && c.id < 23) { // 4 protected instead of usual 2
|
if (c.id > 14 && c.id < 23) { // 4 protected instead of usual 2
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -383,7 +383,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// unused localhost slot), plus 6 others for 12/24 total, sorted by longest uptime.
|
// unused localhost slot), plus 6 others for 12/24 total, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
24, [](NodeEvictionCandidate& c) {
|
24, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 15);
|
c.m_is_local = (c.id == 15);
|
||||||
if (c.id == 12 || c.id == 14 || c.id == 17) {
|
if (c.id == 12 || c.id == 14 || c.id == 17) {
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -402,7 +402,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// for 12/24 total, sorted by longest uptime.
|
// for 12/24 total, sorted by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
24, [](NodeEvictionCandidate& c) {
|
24, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id == 13);
|
c.m_is_local = (c.id == 13);
|
||||||
if (c.id > 16) {
|
if (c.id > 16) {
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -421,7 +421,7 @@ BOOST_AUTO_TEST_CASE(peer_protection_test)
|
||||||
// by longest uptime.
|
// by longest uptime.
|
||||||
BOOST_CHECK(IsProtected(
|
BOOST_CHECK(IsProtected(
|
||||||
24, [](NodeEvictionCandidate& c) {
|
24, [](NodeEvictionCandidate& c) {
|
||||||
c.nTimeConnected = c.id;
|
c.m_connected = std::chrono::seconds{c.id};
|
||||||
c.m_is_local = (c.id > 15);
|
c.m_is_local = (c.id > 15);
|
||||||
if (c.id > 10 && c.id < 15) {
|
if (c.id > 10 && c.id < 15) {
|
||||||
c.m_network = NET_I2P;
|
c.m_network = NET_I2P;
|
||||||
|
@ -484,7 +484,7 @@ BOOST_AUTO_TEST_CASE(peer_eviction_test)
|
||||||
// into our mempool should be protected from eviction.
|
// into our mempool should be protected from eviction.
|
||||||
BOOST_CHECK(!IsEvicted(
|
BOOST_CHECK(!IsEvicted(
|
||||||
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
||||||
candidate.nLastTXTime = number_of_nodes - candidate.id;
|
candidate.m_last_tx_time = std::chrono::seconds{number_of_nodes - candidate.id};
|
||||||
},
|
},
|
||||||
{0, 1, 2, 3}, random_context));
|
{0, 1, 2, 3}, random_context));
|
||||||
|
|
||||||
|
@ -492,7 +492,7 @@ BOOST_AUTO_TEST_CASE(peer_eviction_test)
|
||||||
// blocks should be protected from eviction.
|
// blocks should be protected from eviction.
|
||||||
BOOST_CHECK(!IsEvicted(
|
BOOST_CHECK(!IsEvicted(
|
||||||
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
||||||
candidate.nLastBlockTime = number_of_nodes - candidate.id;
|
candidate.m_last_block_time = std::chrono::seconds{number_of_nodes - candidate.id};
|
||||||
if (candidate.id <= 7) {
|
if (candidate.id <= 7) {
|
||||||
candidate.fRelayTxes = false;
|
candidate.fRelayTxes = false;
|
||||||
candidate.fRelevantServices = true;
|
candidate.fRelevantServices = true;
|
||||||
|
@ -504,14 +504,14 @@ BOOST_AUTO_TEST_CASE(peer_eviction_test)
|
||||||
// protected from eviction.
|
// protected from eviction.
|
||||||
BOOST_CHECK(!IsEvicted(
|
BOOST_CHECK(!IsEvicted(
|
||||||
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
||||||
candidate.nLastBlockTime = number_of_nodes - candidate.id;
|
candidate.m_last_block_time = std::chrono::seconds{number_of_nodes - candidate.id};
|
||||||
},
|
},
|
||||||
{0, 1, 2, 3}, random_context));
|
{0, 1, 2, 3}, random_context));
|
||||||
|
|
||||||
// Combination of the previous two tests.
|
// Combination of the previous two tests.
|
||||||
BOOST_CHECK(!IsEvicted(
|
BOOST_CHECK(!IsEvicted(
|
||||||
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
||||||
candidate.nLastBlockTime = number_of_nodes - candidate.id;
|
candidate.m_last_block_time = std::chrono::seconds{number_of_nodes - candidate.id};
|
||||||
if (candidate.id <= 7) {
|
if (candidate.id <= 7) {
|
||||||
candidate.fRelayTxes = false;
|
candidate.fRelayTxes = false;
|
||||||
candidate.fRelevantServices = true;
|
candidate.fRelevantServices = true;
|
||||||
|
@ -524,8 +524,8 @@ BOOST_AUTO_TEST_CASE(peer_eviction_test)
|
||||||
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
|
||||||
candidate.nKeyedNetGroup = number_of_nodes - candidate.id; // 4 protected
|
candidate.nKeyedNetGroup = number_of_nodes - candidate.id; // 4 protected
|
||||||
candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; // 8 protected
|
candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; // 8 protected
|
||||||
candidate.nLastTXTime = number_of_nodes - candidate.id; // 4 protected
|
candidate.m_last_tx_time = std::chrono::seconds{number_of_nodes - candidate.id}; // 4 protected
|
||||||
candidate.nLastBlockTime = number_of_nodes - candidate.id; // 4 protected
|
candidate.m_last_block_time = std::chrono::seconds{number_of_nodes - candidate.id}; // 4 protected
|
||||||
},
|
},
|
||||||
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context));
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context));
|
||||||
|
|
||||||
|
|
|
@ -47,10 +47,10 @@ std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candida
|
||||||
for (int id = 0; id < n_candidates; ++id) {
|
for (int id = 0; id < n_candidates; ++id) {
|
||||||
candidates.push_back({
|
candidates.push_back({
|
||||||
/*id=*/id,
|
/*id=*/id,
|
||||||
/*nTimeConnected=*/static_cast<int64_t>(random_context.randrange(100)),
|
/*m_connected=*/std::chrono::seconds{random_context.randrange(100)},
|
||||||
/*m_min_ping_time=*/std::chrono::microseconds{random_context.randrange(100)},
|
/*m_min_ping_time=*/std::chrono::microseconds{random_context.randrange(100)},
|
||||||
/*nLastBlockTime=*/static_cast<int64_t>(random_context.randrange(100)),
|
/*m_last_block_time=*/std::chrono::seconds{random_context.randrange(100)},
|
||||||
/*nLastTXTime=*/static_cast<int64_t>(random_context.randrange(100)),
|
/*m_last_tx_time=*/std::chrono::seconds{random_context.randrange(100)},
|
||||||
/*fRelevantServices=*/random_context.randbool(),
|
/*fRelevantServices=*/random_context.randbool(),
|
||||||
/*fRelayTxes=*/random_context.randbool(),
|
/*fRelayTxes=*/random_context.randbool(),
|
||||||
/*fBloomFilter=*/random_context.randbool(),
|
/*fBloomFilter=*/random_context.randbool(),
|
||||||
|
|
|
@ -86,7 +86,7 @@ class TimeoutsTest(BitcoinTestFramework):
|
||||||
]
|
]
|
||||||
|
|
||||||
with self.nodes[0].assert_debug_log(expected_msgs=expected_timeout_logs):
|
with self.nodes[0].assert_debug_log(expected_msgs=expected_timeout_logs):
|
||||||
self.mock_forward(5)
|
self.mock_forward(2)
|
||||||
no_verack_node.wait_for_disconnect(timeout=1)
|
no_verack_node.wait_for_disconnect(timeout=1)
|
||||||
no_version_node.wait_for_disconnect(timeout=1)
|
no_version_node.wait_for_disconnect(timeout=1)
|
||||||
no_send_node.wait_for_disconnect(timeout=1)
|
no_send_node.wait_for_disconnect(timeout=1)
|
||||||
|
|
Loading…
Add table
Reference in a new issue