Skip to content

Commit

Permalink
[net processing] Move nStartingHeight to Peer
Browse files Browse the repository at this point in the history
Summary:
Partial backport of [[bitcoin/bitcoin#19829 | core#19829]]:
bitcoin/bitcoin@77a2c2f

Ref T1696.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Maniphest Tasks: T1696

Differential Revision: https://reviews.bitcoinabc.org/D10866
  • Loading branch information
jnewbery authored and Fabcien committed Jan 24, 2022
1 parent 7a72a41 commit eab6942
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ void CNode::copyStats(CNodeStats &stats) {
}
stats.fInbound = IsInboundConn();
stats.m_manual_connection = IsManualConn();
stats.nStartingHeight = nStartingHeight;
{
LOCK(cs_vSend);
stats.mapSendBytesPerMsgCmd = mapSendBytesPerMsgCmd;
Expand Down
1 change: 0 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,6 @@ class CNode {

public:
BlockHash hashContinue;
std::atomic<int> nStartingHeight{-1};

// flood relay
std::vector<CAddress> vAddrToSend;
Expand Down
26 changes: 17 additions & 9 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,13 @@ bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
}
}

return GetPeerRef(nodeid) != nullptr;
PeerRef peer = GetPeerRef(nodeid);
if (peer == nullptr) {
return false;
}
stats.nStartingHeight = peer->nStartingHeight;

return true;
}

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2292,7 +2298,7 @@ void PeerManager::SendBlockTransactions(CNode &pfrom, const CBlock &block,
}

void PeerManager::ProcessHeadersMessage(
const Config &config, CNode &pfrom,
const Config &config, CNode &pfrom, const Peer &peer,
const std::vector<CBlockHeader> &headers, bool via_compact_block) {
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
size_t nCount = headers.size();
Expand Down Expand Up @@ -2404,7 +2410,7 @@ void PeerManager::ProcessHeadersMessage(
LogPrint(
BCLog::NET,
"more getheaders (%d) to end to peer=%d (startheight:%d)\n",
pindexLast->nHeight, pfrom.GetId(), pfrom.nStartingHeight);
pindexLast->nHeight, pfrom.GetId(), peer.nStartingHeight);
m_connman.PushMessage(
&pfrom, msgMaker.Make(NetMsgType::GETHEADERS,
::ChainActive().GetLocator(pindexLast),
Expand Down Expand Up @@ -3015,7 +3021,7 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
LOCK(pfrom.cs_SubVer);
pfrom.cleanSubVer = cleanSubVer;
}
pfrom.nStartingHeight = nStartingHeight;
peer->nStartingHeight = nStartingHeight;

// set nodes not relaying blocks and tx and not serving (parts) of the
// historical blockchain as "clients"
Expand Down Expand Up @@ -3104,7 +3110,7 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
"receive version message: [%s] %s: version %d, blocks=%d, "
"us=%s, peer=%d%s\n",
pfrom.addr.ToString(), cleanSubVer, pfrom.nVersion,
pfrom.nStartingHeight, addrMe.ToString(), pfrom.GetId(),
peer->nStartingHeight, addrMe.ToString(), pfrom.GetId(),
remoteAddr);

// Ignore time offsets that are improbable (before the Genesis block)
Expand Down Expand Up @@ -3144,7 +3150,7 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
LogPrintf(
"New outbound peer connected: version: %d, blocks=%d, "
"peer=%d%s (%s)\n",
pfrom.nVersion.load(), pfrom.nStartingHeight, pfrom.GetId(),
pfrom.nVersion.load(), peer->nStartingHeight, pfrom.GetId(),
(fLogIPs ? strprintf(", peeraddr=%s", pfrom.addr.ToString())
: ""),
pfrom.m_tx_relay == nullptr ? "block-relay" : "full-relay");
Expand Down Expand Up @@ -4061,7 +4067,8 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
// disconnect the peer if the header turns out to be for an invalid
// block. Note that if a peer tries to build on an invalid chain,
// that will be detected and the peer will be banned.
return ProcessHeadersMessage(config, pfrom, {cmpctblock.header},
return ProcessHeadersMessage(config, pfrom, *peer,
{cmpctblock.header},
/*via_compact_block=*/true);
}

Expand Down Expand Up @@ -4232,7 +4239,7 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
ReadCompactSize(vRecv);
}

return ProcessHeadersMessage(config, pfrom, headers,
return ProcessHeadersMessage(config, pfrom, *peer, headers,
/*via_compact_block=*/false);
}

Expand Down Expand Up @@ -5193,6 +5200,7 @@ class CompareInvMempoolOrder {

bool PeerManager::SendMessages(const Config &config, CNode *pto,
std::atomic<bool> &interruptMsgProc) {
PeerRef peer = GetPeerRef(pto->GetId());
const Consensus::Params &consensusParams = m_chainparams.GetConsensus();

// We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
Expand Down Expand Up @@ -5355,7 +5363,7 @@ bool PeerManager::SendMessages(const Config &config, CNode *pto,
LogPrint(
BCLog::NET,
"initial getheaders (%d) to peer=%d (startheight:%d)\n",
pindexStart->nHeight, pto->GetId(), pto->nStartingHeight);
pindexStart->nHeight, pto->GetId(), peer->nStartingHeight);
m_connman.PushMessage(
pto, msgMaker.Make(NetMsgType::GETHEADERS,
::ChainActive().GetLocator(pindexStart),
Expand Down
5 changes: 5 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct CNodeStateStats {
int m_misbehavior_score = 0;
int nSyncHeight = -1;
int nCommonHeight = -1;
int nStartingHeight = -1;
std::vector<int> vHeightInFlight;
};

Expand Down Expand Up @@ -74,6 +75,9 @@ struct Peer {
* (unless it has the noban permission). */
bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};

/** This peer's reported block height when we connected */
std::atomic<int> nStartingHeight{-1};

/**
* Set of txids to reconsider once their parent transactions have been
* accepted
Expand Down Expand Up @@ -254,6 +258,7 @@ class PeerManager final : public CValidationInterface,
EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
/** Process a single headers message from a peer. */
void ProcessHeadersMessage(const Config &config, CNode &pfrom,
const Peer &peer,
const std::vector<CBlockHeader> &headers,
bool via_compact_block);

Expand Down
4 changes: 3 additions & 1 deletion src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,6 @@ void RPCConsole::updateDetailWidget() {
QString::fromStdString(stats->nodeStats.cleanSubVer));
ui->peerDirection->setText(stats->nodeStats.fInbound ? tr("Inbound")
: tr("Outbound"));
ui->peerHeight->setText(QString::number(stats->nodeStats.nStartingHeight));
if (stats->nodeStats.m_permissionFlags == PF_NONE) {
ui->peerPermissions->setText(tr("N/A"));
} else {
Expand Down Expand Up @@ -1325,6 +1324,9 @@ void RPCConsole::updateDetailWidget() {
} else {
ui->peerCommonHeight->setText(tr("Unknown"));
}

ui->peerHeight->setText(
QString::number(stats->nodeStateStats.nStartingHeight));
}

ui->detailWidget->show();
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ static RPCHelpMan getpeerinfo() {
// addnode is deprecated in v0.24.5 for removal in v0.25.x
obj.pushKV("addnode", stats.m_manual_connection);
}
obj.pushKV("startingheight", stats.nStartingHeight);
if (fStateStats) {
obj.pushKV("startingheight", statestats.nStartingHeight);
obj.pushKV("synced_headers", statestats.nSyncHeight);
obj.pushKV("synced_blocks", statestats.nCommonHeight);
UniValue heights(UniValue::VARR);
Expand Down

0 comments on commit eab6942

Please sign in to comment.