Skip to content

Commit 7d1c036

Browse files
jnewberydergoegge
authored andcommitted
[net processing] Replace fHaveWitness with CanServeWitnesses()
1 parent f65e83d commit 7d1c036

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

src/net_processing.cpp

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,6 @@ struct CNodeState {
432432
bool m_requested_hb_cmpctblocks{false};
433433
/** Whether this peer will send us cmpctblocks if we request them. */
434434
bool m_provides_cmpctblocks{false};
435-
//! Whether this peer can give us witnesses
436-
bool fHaveWitness{false};
437435

438436
/** State used to enforce CHAIN_SYNC_TIMEOUT and EXTRA_PEER_CHECK_INTERVAL logic.
439437
*
@@ -514,7 +512,8 @@ class PeerManagerImpl final : public PeerManager
514512
/** Implement PeerManager */
515513
void StartScheduledTasks(CScheduler& scheduler) override;
516514
void CheckForStaleTipAndEvictPeers() override;
517-
std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override;
515+
std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override
516+
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
518517
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
519518
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
520519
void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
@@ -600,7 +599,7 @@ class PeerManagerImpl final : public PeerManager
600599
*/
601600
bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer);
602601
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
603-
void HeadersDirectFetchBlocks(CNode& pfrom, const CBlockIndex* pindexLast);
602+
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast);
604603
/** Update peer state based on received headers message */
605604
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers);
606605

@@ -679,7 +678,7 @@ class PeerManagerImpl final : public PeerManager
679678
/** Get a pointer to a mutable CNodeState. */
680679
CNodeState* State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
681680

682-
uint32_t GetFetchFlags(const CNode& pfrom) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
681+
uint32_t GetFetchFlags(const Peer& peer) const;
683682

684683
std::atomic<std::chrono::microseconds> m_next_inv_to_inbounds{0us};
685684

@@ -800,7 +799,7 @@ class PeerManagerImpl final : public PeerManager
800799
/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
801800
* at most count entries.
802801
*/
803-
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
802+
void FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
804803

805804
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
806805

@@ -991,6 +990,12 @@ static bool IsLimitedPeer(const Peer& peer)
991990
(peer.m_their_services & NODE_NETWORK_LIMITED));
992991
}
993992

993+
/** Whether this peer can serve us witness data */
994+
static bool CanServeWitnesses(const Peer& peer)
995+
{
996+
return peer.m_their_services & NODE_WITNESS;
997+
}
998+
994999
std::chrono::microseconds PeerManagerImpl::NextInvToInbounds(std::chrono::microseconds now,
9951000
std::chrono::seconds average_interval)
9961001
{
@@ -1184,17 +1189,17 @@ void PeerManagerImpl::UpdateBlockAvailability(NodeId nodeid, const uint256 &hash
11841189
}
11851190
}
11861191

1187-
void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller)
1192+
void PeerManagerImpl::FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller)
11881193
{
11891194
if (count == 0)
11901195
return;
11911196

11921197
vBlocks.reserve(vBlocks.size() + count);
1193-
CNodeState *state = State(nodeid);
1198+
CNodeState *state = State(peer.m_id);
11941199
assert(state != nullptr);
11951200

11961201
// Make sure pindexBestKnownBlock is up to date, we'll need it.
1197-
ProcessBlockAvailability(nodeid);
1202+
ProcessBlockAvailability(peer.m_id);
11981203

11991204
if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < m_chainman.ActiveChain().Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
12001205
// This peer has nothing interesting.
@@ -1242,7 +1247,7 @@ void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count
12421247
// We consider the chain that this peer is on invalid.
12431248
return;
12441249
}
1245-
if (!State(nodeid)->fHaveWitness && DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) {
1250+
if (!CanServeWitnesses(peer) && DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) {
12461251
// We wouldn't download this block or its descendants from this peer.
12471252
return;
12481253
}
@@ -1253,7 +1258,7 @@ void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count
12531258
// The block is not already downloaded, and not yet in flight.
12541259
if (pindex->nHeight > nWindowEnd) {
12551260
// We reached the end of the window.
1256-
if (vBlocks.size() == 0 && waitingfor != nodeid) {
1261+
if (vBlocks.size() == 0 && waitingfor != peer.m_id) {
12571262
// We aren't able to fetch anything, but we would be if the download window was one larger.
12581263
nodeStaller = waitingfor;
12591264
}
@@ -1621,12 +1626,14 @@ std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBl
16211626
if (fImporting) return "Importing...";
16221627
if (fReindex) return "Reindexing...";
16231628

1624-
LOCK(cs_main);
16251629
// Ensure this peer exists and hasn't been disconnected
1626-
CNodeState* state = State(peer_id);
1627-
if (state == nullptr) return "Peer does not exist";
1630+
PeerRef peer = GetPeerRef(peer_id);
1631+
if (peer == nullptr) return "Peer does not exist";
1632+
16281633
// Ignore pre-segwit peers
1629-
if (!state->fHaveWitness) return "Pre-SegWit peer";
1634+
if (!CanServeWitnesses(*peer)) return "Pre-SegWit peer";
1635+
1636+
LOCK(cs_main);
16301637

16311638
// Mark block as in-flight unless it already is (for this peer).
16321639
// If a block was already in-flight for a different peer, its BLOCKTXN
@@ -2227,10 +2234,10 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
22272234
}
22282235
}
22292236

2230-
uint32_t PeerManagerImpl::GetFetchFlags(const CNode& pfrom) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
2237+
uint32_t PeerManagerImpl::GetFetchFlags(const Peer& peer) const
22312238
{
22322239
uint32_t nFetchFlags = 0;
2233-
if (State(pfrom.GetId())->fHaveWitness) {
2240+
if (CanServeWitnesses(peer)) {
22342241
nFetchFlags |= MSG_WITNESS_FLAG;
22352242
}
22362243
return nFetchFlags;
@@ -2325,7 +2332,7 @@ bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& loc
23252332
* We require that the given tip have at least as much work as our tip, and for
23262333
* our current tip to be "close to synced" (see CanDirectFetch()).
23272334
*/
2328-
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const CBlockIndex* pindexLast)
2335+
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast)
23292336
{
23302337
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
23312338

@@ -2340,7 +2347,7 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const CBlockIndex*
23402347
while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
23412348
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
23422349
!IsBlockRequested(pindexWalk->GetBlockHash()) &&
2343-
(!DeploymentActiveAt(*pindexWalk, m_chainman, Consensus::DEPLOYMENT_SEGWIT) || State(pfrom.GetId())->fHaveWitness)) {
2350+
(!DeploymentActiveAt(*pindexWalk, m_chainman, Consensus::DEPLOYMENT_SEGWIT) || CanServeWitnesses(peer))) {
23442351
// We don't have this block, and it's not yet in flight.
23452352
vToFetch.push_back(pindexWalk);
23462353
}
@@ -2362,7 +2369,7 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const CBlockIndex*
23622369
// Can't download any more from this peer
23632370
break;
23642371
}
2365-
uint32_t nFetchFlags = GetFetchFlags(pfrom);
2372+
uint32_t nFetchFlags = GetFetchFlags(peer);
23662373
vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
23672374
BlockRequested(pfrom.GetId(), *pindex);
23682375
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
@@ -2507,7 +2514,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
25072514
UpdatePeerStateForReceivedHeaders(pfrom, pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
25082515

25092516
// Consider immediately downloading blocks.
2510-
HeadersDirectFetchBlocks(pfrom, pindexLast);
2517+
HeadersDirectFetchBlocks(pfrom, peer, pindexLast);
25112518

25122519
return;
25132520
}
@@ -2901,12 +2908,6 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
29012908
if (fRelay) pfrom.m_relays_txs = true;
29022909
}
29032910

2904-
if((nServices & NODE_WITNESS))
2905-
{
2906-
LOCK(cs_main);
2907-
State(pfrom.GetId())->fHaveWitness = true;
2908-
}
2909-
29102911
// Potentially mark this peer as a preferred download peer.
29112912
{
29122913
LOCK(cs_main);
@@ -3789,7 +3790,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
37893790
// We requested this block for some reason, but our mempool will probably be useless
37903791
// so we just grab the block via normal getdata
37913792
std::vector<CInv> vInv(1);
3792-
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
3793+
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
37933794
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
37943795
}
37953796
return;
@@ -3825,7 +3826,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
38253826
} else if (status == READ_STATUS_FAILED) {
38263827
// Duplicate txindexes, the block is now in-flight, so just request it
38273828
std::vector<CInv> vInv(1);
3828-
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
3829+
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
38293830
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
38303831
return;
38313832
}
@@ -3868,7 +3869,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
38683869
// We requested this block, but its far into the future, so our
38693870
// mempool will probably be useless - request the block normally
38703871
std::vector<CInv> vInv(1);
3871-
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom), cmpctblock.header.GetHash());
3872+
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
38723873
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
38733874
return;
38743875
} else {
@@ -3952,7 +3953,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
39523953
} else if (status == READ_STATUS_FAILED) {
39533954
// Might have collided, fall back to getdata now :(
39543955
std::vector<CInv> invs;
3955-
invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom), resp.blockhash));
3956+
invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(*peer), resp.blockhash));
39563957
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, invs));
39573958
} else {
39583959
// Block is either okay, or possibly we received
@@ -5266,9 +5267,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
52665267
if (CanServeBlocks(*peer) && ((sync_blocks_and_headers_from_peer && !IsLimitedPeer(*peer)) || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
52675268
std::vector<const CBlockIndex*> vToDownload;
52685269
NodeId staller = -1;
5269-
FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
5270+
FindNextBlocksToDownload(*peer, MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
52705271
for (const CBlockIndex *pindex : vToDownload) {
5271-
uint32_t nFetchFlags = GetFetchFlags(*pto);
5272+
uint32_t nFetchFlags = GetFetchFlags(*peer);
52725273
vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
52735274
BlockRequested(pto->GetId(), *pindex);
52745275
LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
@@ -5295,7 +5296,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
52955296
if (!AlreadyHaveTx(gtxid)) {
52965297
LogPrint(BCLog::NET, "Requesting %s %s peer=%d\n", gtxid.IsWtxid() ? "wtx" : "tx",
52975298
gtxid.GetHash().ToString(), pto->GetId());
5298-
vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*pto)), gtxid.GetHash());
5299+
vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*peer)), gtxid.GetHash());
52995300
if (vGetData.size() >= MAX_GETDATA_SZ) {
53005301
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
53015302
vGetData.clear();

0 commit comments

Comments
 (0)