Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
Show First 20 Lines • Show All 928 Lines • ▼ Show 20 Lines | |||||
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) { | void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CNodeState *state = State(node); | CNodeState *state = State(node); | ||||
if (state) { | if (state) { | ||||
state->m_last_block_announcement = time_in_seconds; | state->m_last_block_announcement = time_in_seconds; | ||||
} | } | ||||
} | } | ||||
static bool IsOutboundDisconnectionCandidate(const CNode &node) { | |||||
return node.IsOutboundOrBlockRelayConn(); | |||||
} | |||||
void PeerLogicValidation::InitializeNode(const Config &config, CNode *pnode) { | void PeerLogicValidation::InitializeNode(const Config &config, CNode *pnode) { | ||||
CAddress addr = pnode->addr; | CAddress addr = pnode->addr; | ||||
std::string addrName = pnode->GetAddrName(); | std::string addrName = pnode->GetAddrName(); | ||||
NodeId nodeid = pnode->GetId(); | NodeId nodeid = pnode->GetId(); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, | mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, | ||||
std::forward_as_tuple(nodeid), | std::forward_as_tuple(nodeid), | ||||
▲ Show 20 Lines • Show All 1,183 Lines • ▼ Show 20 Lines | if (!chainman.ProcessNewBlockHeaders(config, headers, state, &pindexLast)) { | ||||
// This peer has too little work on their headers chain to help | // This peer has too little work on their headers chain to help | ||||
// us sync -- disconnect if using an outbound slot (unless | // us sync -- disconnect if using an outbound slot (unless | ||||
// whitelisted or addnode). | // whitelisted or addnode). | ||||
// Note: We compare their tip to nMinimumChainWork (rather than | // Note: We compare their tip to nMinimumChainWork (rather than | ||||
// ::ChainActive().Tip()) because we won't start block download | // ::ChainActive().Tip()) because we won't start block download | ||||
// until we have a headers chain that has at least | // until we have a headers chain that has at least | ||||
// nMinimumChainWork, even if a peer has a chain past our tip, | // nMinimumChainWork, even if a peer has a chain past our tip, | ||||
// as an anti-DoS measure. | // as an anti-DoS measure. | ||||
if (IsOutboundDisconnectionCandidate(pfrom)) { | if (pfrom.IsOutboundOrBlockRelayConn()) { | ||||
LogPrintf("Disconnecting outbound peer %d -- headers " | LogPrintf("Disconnecting outbound peer %d -- headers " | ||||
"chain has insufficient work\n", | "chain has insufficient work\n", | ||||
pfrom.GetId()); | pfrom.GetId()); | ||||
pfrom.fDisconnect = true; | pfrom.fDisconnect = true; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (!pfrom.fDisconnect && IsOutboundDisconnectionCandidate(pfrom) && | if (!pfrom.fDisconnect && pfrom.IsOutboundOrBlockRelayConn() && | ||||
nodestate->pindexBestKnownBlock != nullptr && | nodestate->pindexBestKnownBlock != nullptr && | ||||
pfrom.m_tx_relay != nullptr) { | pfrom.m_tx_relay != nullptr) { | ||||
// If this is an outbound full-relay peer, check to see if we should | // If this is an outbound full-relay peer, check to see if we should | ||||
// protect it from the bad/lagging chain logic. Note that | // protect it from the bad/lagging chain logic. Note that | ||||
// block-relay-only peers are already implicitly protected, so we | // block-relay-only peers are already implicitly protected, so we | ||||
// only consider setting m_protect for the full-relay peers. | // only consider setting m_protect for the full-relay peers. | ||||
if (g_outbound_peers_with_protect_from_disconnect < | if (g_outbound_peers_with_protect_from_disconnect < | ||||
MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT && | MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT && | ||||
▲ Show 20 Lines • Show All 2,194 Lines • ▼ Show 20 Lines | |||||
void PeerLogicValidation::ConsiderEviction(CNode &pto, | void PeerLogicValidation::ConsiderEviction(CNode &pto, | ||||
int64_t time_in_seconds) { | int64_t time_in_seconds) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
CNodeState &state = *State(pto.GetId()); | CNodeState &state = *State(pto.GetId()); | ||||
const CNetMsgMaker msgMaker(pto.GetSendVersion()); | const CNetMsgMaker msgMaker(pto.GetSendVersion()); | ||||
if (!state.m_chain_sync.m_protect && | if (!state.m_chain_sync.m_protect && pto.IsOutboundOrBlockRelayConn() && | ||||
IsOutboundDisconnectionCandidate(pto) && state.fSyncStarted) { | state.fSyncStarted) { | ||||
// This is an outbound peer subject to disconnection if they don't | // This is an outbound peer subject to disconnection if they don't | ||||
// announce a block with as much work as the current tip within | // announce a block with as much work as the current tip within | ||||
// CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if their | // CHAIN_SYNC_TIMEOUT + HEADERS_RESPONSE_TIME seconds (note: if their | ||||
// chain has more work than ours, we should sync to it, unless it's | // chain has more work than ours, we should sync to it, unless it's | ||||
// invalid, in which case we should find that out and disconnect from | // invalid, in which case we should find that out and disconnect from | ||||
// them elsewhere). | // them elsewhere). | ||||
if (state.pindexBestKnownBlock != nullptr && | if (state.pindexBestKnownBlock != nullptr && | ||||
state.pindexBestKnownBlock->nChainWork >= | state.pindexBestKnownBlock->nChainWork >= | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds) { | ||||
// ties broken by choosing the more recent connection (higher node id) | // ties broken by choosing the more recent connection (higher node id) | ||||
NodeId worst_peer = -1; | NodeId worst_peer = -1; | ||||
int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max(); | int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max(); | ||||
connman->ForEachNode([&](CNode *pnode) { | connman->ForEachNode([&](CNode *pnode) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
// Ignore non-outbound peers, or nodes marked for disconnect already | // Ignore non-outbound peers, or nodes marked for disconnect already | ||||
if (!IsOutboundDisconnectionCandidate(*pnode) || pnode->fDisconnect) { | if (!pnode->IsOutboundOrBlockRelayConn() || pnode->fDisconnect) { | ||||
return; | return; | ||||
} | } | ||||
CNodeState *state = State(pnode->GetId()); | CNodeState *state = State(pnode->GetId()); | ||||
if (state == nullptr) { | if (state == nullptr) { | ||||
// shouldn't be possible, but just in case | // shouldn't be possible, but just in case | ||||
return; | return; | ||||
} | } | ||||
// Don't evict our protected peers | // Don't evict our protected peers | ||||
▲ Show 20 Lines • Show All 820 Lines • Show Last 20 Lines |