Skip to content

Commit 0b5a997

Browse files
committed
Merge #8637: Compact Block Tweaks (rebase of #8235)
3ac6de0 Align constant names for maximum compact block / blocktxn depth (Pieter Wuille) b2e93a3 Add cmpctblock to debug help list (instagibbs) fe998e9 More agressively filter compact block requests (Matt Corallo) 02a337d Dont remove a "preferred" cmpctblock peer if they provide a block (Matt Corallo)
2 parents 744d265 + 3ac6de0 commit 0b5a997

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

qa/rpc-tests/p2p-compactblocks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def test_incorrect_blocktxn_response(self, node, test_node, version):
591591
def test_getblocktxn_handler(self, node, test_node, version):
592592
# bitcoind won't respond for blocks whose height is more than 15 blocks
593593
# deep.
594-
MAX_GETBLOCKTXN_DEPTH = 15
594+
MAX_GETBLOCKTXN_DEPTH = 10
595595
chain_height = node.getblockcount()
596596
current_height = chain_height
597597
while (current_height >= chain_height - MAX_GETBLOCKTXN_DEPTH):
@@ -632,9 +632,9 @@ def test_getblocktxn_handler(self, node, test_node, version):
632632

633633
def test_compactblocks_not_at_tip(self, node, test_node):
634634
# Test that requesting old compactblocks doesn't work.
635-
MAX_CMPCTBLOCK_DEPTH = 11
635+
MAX_CMPCTBLOCK_DEPTH = 5
636636
new_blocks = []
637-
for i in range(MAX_CMPCTBLOCK_DEPTH):
637+
for i in range(MAX_CMPCTBLOCK_DEPTH + 1):
638638
test_node.clear_block_announcement()
639639
new_blocks.append(node.generate(1)[0])
640640
wait_until(test_node.received_block_announcement, timeout=30)

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ std::string HelpMessage(HelpMessageMode mode)
423423
strUsage += HelpMessageOpt("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT));
424424
strUsage += HelpMessageOpt("-bip9params=deployment:start:end", "Use given start/end times for specified BIP9 deployment (regtest-only)");
425425
}
426-
string debugCategories = "addrman, alert, bench, coindb, db, http, libevent, lock, mempool, mempoolrej, net, proxy, prune, rand, reindex, rpc, selectcoins, tor, zmq"; // Don't translate these and qt below
426+
string debugCategories = "addrman, alert, bench, cmpctblock, coindb, db, http, libevent, lock, mempool, mempoolrej, net, proxy, prune, rand, reindex, rpc, selectcoins, tor, zmq"; // Don't translate these and qt below
427427
if (mode == HMM_BITCOIN_QT)
428428
debugCategories += ", qt";
429429
strUsage += HelpMessageOpt("-debug=<category>", strprintf(_("Output debugging information (default: %u, supplying <category> is optional)"), 0) + ". " +

src/main.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,13 @@ void MaybeSetPeerAsAnnouncingHeaderAndIDs(const CNodeState* nodestate, CNode* pf
485485
return;
486486
}
487487
if (nodestate->fProvidesHeaderAndIDs) {
488-
BOOST_FOREACH(const NodeId nodeid, lNodesAnnouncingHeaderAndIDs)
489-
if (nodeid == pfrom->GetId())
488+
for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
489+
if (*it == pfrom->GetId()) {
490+
lNodesAnnouncingHeaderAndIDs.erase(it);
491+
lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
490492
return;
493+
}
494+
}
491495
bool fAnnounceUsingCMPCTBLOCK = false;
492496
uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
493497
if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
@@ -4875,7 +4879,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
48754879
// and we don't feel like constructing the object for them, so
48764880
// instead we respond with the full, non-compact block.
48774881
bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
4878-
if (mi->second->nHeight >= chainActive.Height() - 10) {
4882+
if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
48794883
CBlockHeaderAndShortTxIDs cmpctblock(block, fPeerWantsWitness);
48804884
pfrom->PushMessageWithFlag(fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::CMPCTBLOCK, cmpctblock);
48814885
} else
@@ -5403,8 +5407,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
54035407
return true;
54045408
}
54055409

5406-
if (it->second->nHeight < chainActive.Height() - 15) {
5407-
LogPrint("net", "Peer %d sent us a getblocktxn for a block > 15 deep", pfrom->id);
5410+
if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
5411+
LogPrint("net", "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->id, MAX_BLOCKTXN_DEPTH);
54085412
return true;
54095413
}
54105414

@@ -5733,6 +5737,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
57335737
return true;
57345738
}
57355739

5740+
if (!fAlreadyInFlight && mapBlocksInFlight.size() == 1 && pindex->pprev->IsValid(BLOCK_VALID_CHAIN)) {
5741+
// We seem to be rather well-synced, so it appears pfrom was the first to provide us
5742+
// with this block! Let's get them to announce using compact blocks in the future.
5743+
MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom, connman);
5744+
}
5745+
57365746
BlockTransactionsRequest req;
57375747
for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
57385748
if (!partialBlock.IsTxAvailable(i))

src/main.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ static const unsigned int BLOCK_STALLING_TIMEOUT = 2;
9090
/** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
9191
* less than this number, we reached its tip. Changing this value is a protocol upgrade. */
9292
static const unsigned int MAX_HEADERS_RESULTS = 2000;
93+
/** Maximum depth of blocks we're willing to serve as compact blocks to peers
94+
* when requested. For older blocks, a regular BLOCK response will be sent. */
95+
static const int MAX_CMPCTBLOCK_DEPTH = 5;
96+
/** Maximum depth of blocks we're willing to respond to GETBLOCKTXN requests for. */
97+
static const int MAX_BLOCKTXN_DEPTH = 10;
9398
/** Size of the "block download window": how far ahead of our current height do we fetch?
9499
* Larger windows tolerate larger download speed differences between peer, but increase the potential
95100
* degree of disordering of blocks on disk (which make reindexing and in the future perhaps pruning

0 commit comments

Comments
 (0)