Skip to content

Commit c47f5b7

Browse files
committed
Cache witness-enabled state with recent-compact-block-cache
1 parent efc135f commit c47f5b7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/net_processing.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ static CCriticalSection cs_most_recent_block;
775775
static std::shared_ptr<const CBlock> most_recent_block;
776776
static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_block;
777777
static uint256 most_recent_block_hash;
778+
static bool fWitnessesPresentInMostRecentCompactBlock;
778779

779780
void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
780781
std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
@@ -795,6 +796,7 @@ void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std:
795796
most_recent_block_hash = hashBlock;
796797
most_recent_block = pblock;
797798
most_recent_compact_block = pcmpctblock;
799+
fWitnessesPresentInMostRecentCompactBlock = fWitnessEnabled;
798800
}
799801

800802
connman->ForEachNode([this, &pcmpctblock, pindex, &msgMaker, fWitnessEnabled, &hashBlock](CNode* pnode) {
@@ -981,10 +983,12 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
981983
BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
982984
std::shared_ptr<const CBlock> a_recent_block;
983985
std::shared_ptr<const CBlockHeaderAndShortTxIDs> a_recent_compact_block;
986+
bool fWitnessesPresentInARecentCompactBlock;
984987
{
985988
LOCK(cs_most_recent_block);
986989
a_recent_block = most_recent_block;
987990
a_recent_compact_block = most_recent_compact_block;
991+
fWitnessesPresentInARecentCompactBlock = fWitnessesPresentInMostRecentCompactBlock;
988992
}
989993
if (mi != mapBlockIndex.end())
990994
{
@@ -1077,7 +1081,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
10771081
bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
10781082
int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
10791083
if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
1080-
if (fPeerWantsWitness && a_recent_compact_block && a_recent_compact_block->header.GetHash() == mi->second->GetBlockHash()) {
1084+
if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock) && a_recent_compact_block && a_recent_compact_block->header.GetHash() == mi->second->GetBlockHash()) {
10811085
connman.PushMessage(pfrom, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
10821086
} else {
10831087
CBlockHeaderAndShortTxIDs cmpctblock(*pblock, fPeerWantsWitness);
@@ -2941,7 +2945,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
29412945
{
29422946
LOCK(cs_most_recent_block);
29432947
if (most_recent_block_hash == pBestIndex->GetBlockHash()) {
2944-
if (state.fWantsCmpctWitness)
2948+
if (state.fWantsCmpctWitness || !fWitnessesPresentInMostRecentCompactBlock)
29452949
connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, *most_recent_compact_block));
29462950
else {
29472951
CBlockHeaderAndShortTxIDs cmpctblock(*most_recent_block, state.fWantsCmpctWitness);

0 commit comments

Comments
 (0)