mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 06:43:45 +01:00
Merge bitcoin/bitcoin#23758: net: Use type-safe mockable time for peer connection time
fad943821escripted-diff: Rename touched member variables (MarcoFalke)fa663a4c0dUse mockable time for peer connection time (MarcoFalke)fad7ead146refactor: 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: ACKfad943821eshaavan: ACKfad943821eTree-SHA512: af9bdfc695ab727b100c6810a7289d29b02b0ea9fa4fee9cc1f3eeefb52c8c465ea2734bae0c1c63b3b0d6264ba2c493268bc970ef6916570eb166de77829d82
This commit is contained in:
@@ -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
|
||||
/** How frequently to check for extra outbound peers and disconnect, in seconds */
|
||||
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 */
|
||||
static constexpr int64_t MINIMUM_CONNECT_TIME = 30;
|
||||
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
|
||||
static constexpr std::chrono::seconds MINIMUM_CONNECT_TIME{30};
|
||||
/** SHA256("main address relay")[0:8] */
|
||||
static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL;
|
||||
/// 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);
|
||||
|
||||
/** 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 */
|
||||
void ReattemptInitialBroadcast(CScheduler& scheduler);
|
||||
@@ -2511,7 +2511,7 @@ void PeerManagerImpl::ProcessBlock(CNode& node, const std::shared_ptr<const CBlo
|
||||
bool new_block{false};
|
||||
m_chainman.ProcessNewBlock(m_chainparams, block, force_processing, &new_block);
|
||||
if (new_block) {
|
||||
node.nLastBlockTime = GetTime();
|
||||
node.m_last_block_time = GetTime<std::chrono::seconds>();
|
||||
} else {
|
||||
LOCK(cs_main);
|
||||
mapBlockSource.erase(block->GetHash());
|
||||
@@ -3314,7 +3314,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
|
||||
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",
|
||||
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
|
||||
// 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.
|
||||
// Note that we use higher nodeid as a measure for most recent connection.
|
||||
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) {
|
||||
if (!pnode->IsBlockOnlyConn() || pnode->fDisconnect) return;
|
||||
if (pnode->GetId() > youngest_peer.first) {
|
||||
next_youngest_peer = youngest_peer;
|
||||
youngest_peer.first = pnode->GetId();
|
||||
youngest_peer.second = pnode->nLastBlockTime;
|
||||
youngest_peer.second = pnode->m_last_block_time;
|
||||
}
|
||||
});
|
||||
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.
|
||||
CNodeState *node_state = State(pnode->GetId());
|
||||
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;
|
||||
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;
|
||||
} else {
|
||||
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;
|
||||
});
|
||||
@@ -4308,12 +4309,13 @@ void PeerManagerImpl::EvictExtraOutboundPeers(int64_t time_in_seconds)
|
||||
// Also don't disconnect any peer we're trying to download a
|
||||
// block from.
|
||||
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);
|
||||
pnode->fDisconnect = true;
|
||||
return true;
|
||||
} 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;
|
||||
}
|
||||
});
|
||||
@@ -4335,7 +4337,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
|
||||
|
||||
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) {
|
||||
// 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>();
|
||||
|
||||
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());
|
||||
pto->fDisconnect = true;
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user