Skip to content

Commit db32a65

Browse files
committed
Track tip update time and last new block announcement from each peer
1 parent 2d4327d commit db32a65

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/net_processing.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ namespace {
127127
/** Number of outbound peers with m_chain_sync.m_protect. */
128128
int g_outbound_peers_with_protect_from_disconnect = 0;
129129

130+
131+
/** When our tip was last updated. */
132+
int64_t g_last_tip_update = 0;
133+
130134
/** Relay map, protected by cs_main. */
131135
typedef std::map<uint256, CTransactionRef> MapRelay;
132136
MapRelay mapRelay;
@@ -231,6 +235,9 @@ struct CNodeState {
231235

232236
ChainSyncTimeoutState m_chain_sync;
233237

238+
//! Time of last new block announcement
239+
int64_t m_last_block_announcement;
240+
234241
CNodeState(CAddress addrIn, std::string addrNameIn) : address(addrIn), name(addrNameIn) {
235242
fCurrentlyConnected = false;
236243
nMisbehavior = 0;
@@ -254,6 +261,7 @@ struct CNodeState {
254261
fWantsCmpctWitness = false;
255262
fSupportsDesiredCmpctVersion = false;
256263
m_chain_sync = { 0, nullptr, false, false };
264+
m_last_block_announcement = 0;
257265
}
258266
};
259267

@@ -797,6 +805,8 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb
797805
}
798806
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
799807
}
808+
809+
g_last_tip_update = GetTime();
800810
}
801811

802812
// All of the following cache a recent block, and are protected by cs_most_recent_block
@@ -1215,6 +1225,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
12151225
return true;
12161226
}
12171227

1228+
bool received_new_header = false;
12181229
const CBlockIndex *pindexLast = nullptr;
12191230
{
12201231
LOCK(cs_main);
@@ -1255,6 +1266,12 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
12551266
}
12561267
hashLastBlock = header.GetHash();
12571268
}
1269+
1270+
// If we don't have the last header, then they'll have given us
1271+
// something new (if these headers are valid).
1272+
if (mapBlockIndex.find(hashLastBlock) == mapBlockIndex.end()) {
1273+
received_new_header = true;
1274+
}
12581275
}
12591276

12601277
CValidationState state;
@@ -1319,6 +1336,10 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
13191336
// because it is set in UpdateBlockAvailability. Some nullptr checks
13201337
// are still present, however, as belt-and-suspenders.
13211338

1339+
if (received_new_header && pindexLast->nChainWork > chainActive.Tip()->nChainWork) {
1340+
nodestate->m_last_block_announcement = GetTime();
1341+
}
1342+
13221343
if (nCount == MAX_HEADERS_RESULTS) {
13231344
// Headers message had its maximum size; the peer may have more headers.
13241345
// TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
@@ -2219,6 +2240,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22192240
CBlockHeaderAndShortTxIDs cmpctblock;
22202241
vRecv >> cmpctblock;
22212242

2243+
bool received_new_header = false;
2244+
22222245
{
22232246
LOCK(cs_main);
22242247

@@ -2228,6 +2251,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22282251
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256()));
22292252
return true;
22302253
}
2254+
2255+
if (mapBlockIndex.find(cmpctblock.header.GetHash()) == mapBlockIndex.end()) {
2256+
received_new_header = true;
2257+
}
22312258
}
22322259

22332260
const CBlockIndex *pindex = nullptr;
@@ -2266,6 +2293,14 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22662293
assert(pindex);
22672294
UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
22682295

2296+
CNodeState *nodestate = State(pfrom->GetId());
2297+
2298+
// If this was a new header with more work than our tip, update the
2299+
// peer's last block announcement time
2300+
if (received_new_header && pindex->nChainWork > chainActive.Tip()->nChainWork) {
2301+
nodestate->m_last_block_announcement = GetTime();
2302+
}
2303+
22692304
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
22702305
bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
22712306

@@ -2288,8 +2323,6 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22882323
if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
22892324
return true;
22902325

2291-
CNodeState *nodestate = State(pfrom->GetId());
2292-
22932326
if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
22942327
// Don't bother trying to process compact blocks from v1 peers
22952328
// after segwit activates.

0 commit comments

Comments
 (0)