Skip to content

Commit bb985a7

Browse files
committed
[net processing] Only relay blocks by cmpctblock and cache for fast relay if segwit is enabled
This introduces an early exit in PeerManagerImpl::NewPoWValidBlock() if segwit has not been activated for the block. This means that we won't cache the block/compact block for fast relay and won't relay the cmpctblock immediately to peers that have requested hb compact blocks. This is fine because any block where segwit is not yet activated is buried deep in the chain, and so compact block relay will not be effective. It's ok not to cache the block/compact block for fast relay for the same reason - the block must be very deeply buried in the block chain. ProcessBlockAvailability() also won't get called for all nodes. This is also fine, since that function only updates hashLastUnknownBlock and pindexBestKnownBlock, and is called early in every SendMessages() call.
1 parent 3b6bfbc commit bb985a7

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

src/net_processing.cpp

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ class PeerManagerImpl final : public PeerManager
678678
std::shared_ptr<const CBlock> m_most_recent_block GUARDED_BY(m_most_recent_block_mutex);
679679
std::shared_ptr<const CBlockHeaderAndShortTxIDs> m_most_recent_compact_block GUARDED_BY(m_most_recent_block_mutex);
680680
uint256 m_most_recent_block_hash GUARDED_BY(m_most_recent_block_mutex);
681-
bool m_most_recent_compact_block_has_witnesses GUARDED_BY(m_most_recent_block_mutex){false};
682681

683682
/** Height of the highest block announced using BIP 152 high-bandwidth mode. */
684683
int m_highest_fast_announce{0};
@@ -1630,7 +1629,8 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
16301629
return;
16311630
m_highest_fast_announce = pindex->nHeight;
16321631

1633-
bool fWitnessEnabled = DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT);
1632+
if (!DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT)) return;
1633+
16341634
uint256 hashBlock(pblock->GetHash());
16351635
const std::shared_future<CSerializedNetMsg> lazy_ser{
16361636
std::async(std::launch::deferred, [&] { return msgMaker.Make(NetMsgType::CMPCTBLOCK, *pcmpctblock); })};
@@ -1640,10 +1640,9 @@ void PeerManagerImpl::NewPoWValidBlock(const CBlockIndex *pindex, const std::sha
16401640
m_most_recent_block_hash = hashBlock;
16411641
m_most_recent_block = pblock;
16421642
m_most_recent_compact_block = pcmpctblock;
1643-
m_most_recent_compact_block_has_witnesses = fWitnessEnabled;
16441643
}
16451644

1646-
m_connman.ForEachNode([this, pindex, fWitnessEnabled, &lazy_ser, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
1645+
m_connman.ForEachNode([this, pindex, &lazy_ser, &hashBlock](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
16471646
AssertLockHeld(::cs_main);
16481647

16491648
if (pnode->GetCommonVersion() < INVALID_CB_NO_BAN_VERSION || pnode->fDisconnect)
@@ -1847,12 +1846,10 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
18471846
{
18481847
std::shared_ptr<const CBlock> a_recent_block;
18491848
std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
1850-
bool fWitnessesPresentInARecentCompactBlock;
18511849
{
18521850
LOCK(m_most_recent_block_mutex);
18531851
a_recent_block = m_most_recent_block;
18541852
a_recent_compact_block = m_most_recent_compact_block;
1855-
fWitnessesPresentInARecentCompactBlock = m_most_recent_compact_block_has_witnesses;
18561853
}
18571854

18581855
bool need_activate_chain = false;
@@ -1965,17 +1962,15 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
19651962
// they won't have a useful mempool to match against a compact block,
19661963
// and we don't feel like constructing the object for them, so
19671964
// instead we respond with the full, non-compact block.
1968-
bool fPeerWantsWitness = State(pfrom.GetId())->m_provides_cmpctblocks;
1969-
int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
19701965
if (CanDirectFetch() && pindex->nHeight >= m_chainman.ActiveChain().Height() - MAX_CMPCTBLOCK_DEPTH) {
1971-
if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock) && a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
1972-
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
1966+
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
1967+
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
19731968
} else {
1974-
CBlockHeaderAndShortTxIDs cmpctblock(*pblock, fPeerWantsWitness);
1975-
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
1969+
CBlockHeaderAndShortTxIDs cmpctblock(*pblock, true);
1970+
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
19761971
}
19771972
} else {
1978-
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCK, *pblock));
1973+
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock));
19791974
}
19801975
}
19811976
}
@@ -2134,10 +2129,9 @@ void PeerManagerImpl::SendBlockTransactions(CNode& pfrom, const CBlock& block, c
21342129
}
21352130
resp.txn[i] = block.vtx[req.indexes[i]];
21362131
}
2137-
LOCK(cs_main);
2132+
21382133
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
2139-
int nSendFlags = State(pfrom.GetId())->m_provides_cmpctblocks ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
2140-
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
2134+
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCKTXN, resp));
21412135
}
21422136

21432137
void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
@@ -3230,9 +3224,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
32303224
// expensive disk reads, because it will require the peer to
32313225
// actually receive all the data read from disk over the network.
32323226
LogPrint(BCLog::NET, "Peer %d sent us a getblocktxn for a block > %i deep\n", pfrom.GetId(), MAX_BLOCKTXN_DEPTH);
3233-
CInv inv;
3234-
WITH_LOCK(cs_main, inv.type = State(pfrom.GetId())->m_provides_cmpctblocks ? MSG_WITNESS_BLOCK : MSG_BLOCK);
3235-
inv.hash = req.blockhash;
3227+
CInv inv{MSG_WITNESS_BLOCK, req.blockhash};
32363228
WITH_LOCK(peer->m_getdata_requests_mutex, peer->m_getdata_requests.push_back(inv));
32373229
// The message processing loop will go around again (without pausing) and we'll respond then
32383230
return;
@@ -3607,12 +3599,6 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
36073599
return;
36083600
}
36093601

3610-
if (DeploymentActiveAt(*pindex, m_chainman, Consensus::DEPLOYMENT_SEGWIT) && !nodestate->m_provides_cmpctblocks) {
3611-
// Don't bother trying to process compact blocks from v1 peers
3612-
// after segwit activates.
3613-
return;
3614-
}
3615-
36163602
// We want to be a bit conservative just to be extra careful about DoS
36173603
// possibilities in compact block processing...
36183604
if (pindex->nHeight <= m_chainman.ActiveChain().Height() + 2) {

0 commit comments

Comments
 (0)