Skip to content

Commit 77a2c2f

Browse files
committed
[net processing] Move nStartingHeight to Peer
1 parent 717a374 commit 77a2c2f

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

src/net.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
590590
stats.m_manual_connection = IsManualConn();
591591
X(m_bip152_highbandwidth_to);
592592
X(m_bip152_highbandwidth_from);
593-
X(nStartingHeight);
594593
{
595594
LOCK(cs_vSend);
596595
X(mapSendBytesPerMsgCmd);

src/net.h

-1
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,6 @@ class CNode
994994

995995
public:
996996
uint256 hashContinue;
997-
std::atomic<int> nStartingHeight{-1};
998997
// We selected peer as (compact blocks) high-bandwidth peer (BIP152)
999998
std::atomic<bool> m_bip152_highbandwidth_to{false};
1000999
// Peer selected us as (compact blocks) high-bandwidth peer (BIP152)

src/net_processing.cpp

+13-8
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
875875
PeerRef peer = GetPeerRef(nodeid);
876876
if (peer == nullptr) return false;
877877
stats.m_misbehavior_score = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
878+
stats.nStartingHeight = peer->nStartingHeight;
878879

879880
return true;
880881
}
@@ -1769,7 +1770,9 @@ void PeerManager::SendBlockTransactions(CNode& pfrom, const CBlock& block, const
17691770
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
17701771
}
17711772

1772-
void PeerManager::ProcessHeadersMessage(CNode& pfrom, const std::vector<CBlockHeader>& headers, bool via_compact_block)
1773+
void PeerManager::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
1774+
const std::vector<CBlockHeader>& headers,
1775+
bool via_compact_block)
17731776
{
17741777
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
17751778
size_t nCount = headers.size();
@@ -1859,7 +1862,8 @@ void PeerManager::ProcessHeadersMessage(CNode& pfrom, const std::vector<CBlockHe
18591862
// Headers message had its maximum size; the peer may have more headers.
18601863
// TODO: optimize: if pindexLast is an ancestor of ::ChainActive().Tip or pindexBestHeader, continue
18611864
// from there instead.
1862-
LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom.GetId(), pfrom.nStartingHeight);
1865+
LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n",
1866+
pindexLast->nHeight, pfrom.GetId(), peer.nStartingHeight);
18631867
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexLast), uint256()));
18641868
}
18651869

@@ -2365,7 +2369,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
23652369
LOCK(pfrom.cs_SubVer);
23662370
pfrom.cleanSubVer = cleanSubVer;
23672371
}
2368-
pfrom.nStartingHeight = nStartingHeight;
2372+
peer->nStartingHeight = nStartingHeight;
23692373

23702374
// set nodes not relaying blocks and tx and not serving (parts) of the historical blockchain as "clients"
23712375
pfrom.fClient = (!(nServices & NODE_NETWORK) && !(nServices & NODE_NETWORK_LIMITED));
@@ -2445,7 +2449,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
24452449

24462450
LogPrint(BCLog::NET, "receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
24472451
cleanSubVer, pfrom.nVersion,
2448-
pfrom.nStartingHeight, addrMe.ToString(), pfrom.GetId(),
2452+
peer->nStartingHeight, addrMe.ToString(), pfrom.GetId(),
24492453
remoteAddr);
24502454

24512455
int64_t nTimeOffset = nTime - GetTime();
@@ -2479,7 +2483,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
24792483

24802484
if (!pfrom.IsInboundConn()) {
24812485
LogPrintf("New outbound peer connected: version: %d, blocks=%d, peer=%d%s (%s)\n",
2482-
pfrom.nVersion.load(), pfrom.nStartingHeight,
2486+
pfrom.nVersion.load(), peer->nStartingHeight,
24832487
pfrom.GetId(), (fLogIPs ? strprintf(", peeraddr=%s", pfrom.addr.ToString()) : ""),
24842488
pfrom.ConnectionTypeAsString());
24852489
}
@@ -3321,7 +3325,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
33213325
// the peer if the header turns out to be for an invalid block.
33223326
// Note that if a peer tries to build on an invalid chain, that
33233327
// will be detected and the peer will be disconnected/discouraged.
3324-
return ProcessHeadersMessage(pfrom, {cmpctblock.header}, /*via_compact_block=*/true);
3328+
return ProcessHeadersMessage(pfrom, *peer, {cmpctblock.header}, /*via_compact_block=*/true);
33253329
}
33263330

33273331
if (fBlockReconstructed) {
@@ -3464,7 +3468,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
34643468
ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
34653469
}
34663470

3467-
return ProcessHeadersMessage(pfrom, headers, /*via_compact_block=*/false);
3471+
return ProcessHeadersMessage(pfrom, *peer, headers, /*via_compact_block=*/false);
34683472
}
34693473

34703474
if (msg_type == NetMsgType::BLOCK)
@@ -4072,6 +4076,7 @@ class CompareInvMempoolOrder
40724076

40734077
bool PeerManager::SendMessages(CNode* pto)
40744078
{
4079+
PeerRef peer = GetPeerRef(pto->GetId());
40754080
const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
40764081

40774082
// We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
@@ -4197,7 +4202,7 @@ bool PeerManager::SendMessages(CNode* pto)
41974202
got back an empty response. */
41984203
if (pindexStart->pprev)
41994204
pindexStart = pindexStart->pprev;
4200-
LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), pto->nStartingHeight);
4205+
LogPrint(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), peer->nStartingHeight);
42014206
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexStart), uint256()));
42024207
}
42034208
}

src/net_processing.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct CNodeStateStats {
3636
int m_misbehavior_score = 0;
3737
int nSyncHeight = -1;
3838
int nCommonHeight = -1;
39+
int nStartingHeight = -1;
3940
std::vector<int> vHeightInFlight;
4041
};
4142

@@ -62,6 +63,9 @@ struct Peer {
6263
/** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */
6364
bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
6465

66+
/** This peer's reported block height when we connected */
67+
std::atomic<int> nStartingHeight{-1};
68+
6569
/** Set of txids to reconsider once their parent transactions have been accepted **/
6670
std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
6771

@@ -182,7 +186,9 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
182186

183187
void ProcessOrphanTx(std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
184188
/** Process a single headers message from a peer. */
185-
void ProcessHeadersMessage(CNode& pfrom, const std::vector<CBlockHeader>& headers, bool via_compact_block);
189+
void ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
190+
const std::vector<CBlockHeader>& headers,
191+
bool via_compact_block);
186192

187193
void SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req);
188194

src/qt/rpcconsole.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,6 @@ void RPCConsole::updateDetailWidget()
11091109
ui->peerVersion->setText(QString::number(stats->nodeStats.nVersion));
11101110
ui->peerSubversion->setText(QString::fromStdString(stats->nodeStats.cleanSubVer));
11111111
ui->peerDirection->setText(stats->nodeStats.fInbound ? tr("Inbound") : tr("Outbound"));
1112-
ui->peerHeight->setText(QString::number(stats->nodeStats.nStartingHeight));
11131112
if (stats->nodeStats.m_permissionFlags == PF_NONE) {
11141113
ui->peerPermissions->setText(tr("N/A"));
11151114
} else {
@@ -1135,6 +1134,8 @@ void RPCConsole::updateDetailWidget()
11351134
ui->peerCommonHeight->setText(QString("%1").arg(stats->nodeStateStats.nCommonHeight));
11361135
else
11371136
ui->peerCommonHeight->setText(tr("Unknown"));
1137+
1138+
ui->peerHeight->setText(QString::number(stats->nodeStateStats.nStartingHeight));
11381139
}
11391140

11401141
ui->detailWidget->show();

src/rpc/net.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ static RPCHelpMan getpeerinfo()
133133
{RPCResult::Type::STR, "connection_type", "Type of connection: \n" + Join(CONNECTION_TYPE_DOC, ",\n") + ".\n"
134134
"Please note this output is unlikely to be stable in upcoming releases as we iterate to\n"
135135
"best capture connection behaviors."},
136-
{RPCResult::Type::NUM, "startingheight", "The starting height (block) of the peer"},
137136
{RPCResult::Type::NUM, "banscore", "The ban score (DEPRECATED, returned only if config option -deprecatedrpc=banscore is passed)"},
137+
{RPCResult::Type::NUM, "startingheight", "The starting height (block) of the peer"},
138138
{RPCResult::Type::NUM, "synced_headers", "The last header we have in common with this peer"},
139139
{RPCResult::Type::NUM, "synced_blocks", "The last block we have in common with this peer"},
140140
{RPCResult::Type::ARR, "inflight", "",
@@ -224,12 +224,12 @@ static RPCHelpMan getpeerinfo()
224224
// addnode is deprecated in v0.21 for removal in v0.22
225225
obj.pushKV("addnode", stats.m_manual_connection);
226226
}
227-
obj.pushKV("startingheight", stats.nStartingHeight);
228227
if (fStateStats) {
229228
if (IsDeprecatedRPCEnabled("banscore")) {
230229
// banscore is deprecated in v0.21 for removal in v0.22
231230
obj.pushKV("banscore", statestats.m_misbehavior_score);
232231
}
232+
obj.pushKV("startingheight", statestats.nStartingHeight);
233233
obj.pushKV("synced_headers", statestats.nSyncHeight);
234234
obj.pushKV("synced_blocks", statestats.nCommonHeight);
235235
UniValue heights(UniValue::VARR);

0 commit comments

Comments
 (0)