Skip to content

Commit b9300d8

Browse files
committed
Revert "refactor: Simplify extra_txn to be a vec of CTransactionRef instead of a vec of pair<Wtxid, CTransactionRef>"
This reverts commit a8203e9.
1 parent df5a50e commit b9300d8

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

src/bench/blockencodings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void BlockEncodingBench(benchmark::Bench& bench, size_t n_pool, size_t n_
6565

6666
LOCK2(cs_main, pool.cs);
6767

68-
std::vector<CTransactionRef> extratxn;
68+
std::vector<std::pair<Wtxid, CTransactionRef>> extratxn;
6969
extratxn.reserve(n_extra);
7070

7171
// bump up the size of txs
@@ -94,7 +94,7 @@ static void BlockEncodingBench(benchmark::Bench& bench, size_t n_pool, size_t n_
9494
AddTx(refs[i], /*fee=*/refs[i]->vout[0].nValue, pool);
9595
}
9696
for (size_t i = n_pool; i < n_pool + n_extra; ++i) {
97-
extratxn.push_back(refs[i]);
97+
extratxn.emplace_back(refs[i]->GetWitnessHash(), refs[i]);
9898
}
9999

100100
BenchCBHAST cmpctblock{rng, 3000};

src/blockencodings.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const Wtxid& wtxid) const {
4545
return SipHashUint256(shorttxidk0, shorttxidk1, wtxid.ToUint256()) & 0xffffffffffffL;
4646
}
4747

48-
ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<CTransactionRef>& extra_txn) {
48+
/* Reconstructing a compact block is in the hot-path for block relay,
49+
* so we want to do it as quickly as possible. Because this often
50+
* involves iterating over the entire mempool, we put all the data we
51+
* need (ie the wtxid and a reference to the actual transaction data)
52+
* in a vector and iterate over the vector directly. This allows optimal
53+
* CPU caching behaviour, at a cost of only 40 bytes per transaction.
54+
*/
55+
ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<Wtxid, CTransactionRef>>& extra_txn)
56+
{
4957
LogDebug(BCLog::CMPCTBLOCK, "Initializing PartiallyDownloadedBlock for block %s using a cmpctblock of %u bytes\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock));
5058
if (cmpctblock.header.IsNull() || (cmpctblock.shorttxids.empty() && cmpctblock.prefilledtxn.empty()))
5159
return READ_STATUS_INVALID;
@@ -133,14 +141,11 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
133141
}
134142

135143
for (size_t i = 0; i < extra_txn.size(); i++) {
136-
if (extra_txn[i] == nullptr) {
137-
continue;
138-
}
139-
uint64_t shortid = cmpctblock.GetShortID(extra_txn[i]->GetWitnessHash());
144+
uint64_t shortid = cmpctblock.GetShortID(extra_txn[i].first);
140145
std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid);
141146
if (idit != shorttxids.end()) {
142147
if (!have_txn[idit->second]) {
143-
txn_available[idit->second] = extra_txn[i];
148+
txn_available[idit->second] = extra_txn[i].second;
144149
have_txn[idit->second] = true;
145150
mempool_count++;
146151
extra_count++;
@@ -152,7 +157,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
152157
// Note that we don't want duplication between extra_txn and mempool to
153158
// trigger this case, so we compare witness hashes first
154159
if (txn_available[idit->second] &&
155-
txn_available[idit->second]->GetWitnessHash() != extra_txn[i]->GetWitnessHash()) {
160+
txn_available[idit->second]->GetWitnessHash() != extra_txn[i].second->GetWitnessHash()) {
156161
txn_available[idit->second].reset();
157162
mempool_count--;
158163
extra_count--;

src/blockencodings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ class PartiallyDownloadedBlock {
144144

145145
explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
146146

147-
// extra_txn is a list of extra orphan/conflicted/etc transactions to look at
148-
ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<CTransactionRef>& extra_txn);
147+
// extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
148+
ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<Wtxid, CTransactionRef>>& extra_txn);
149149
bool IsTxAvailable(size_t index) const;
150150
// segwit_active enforces witness mutation checks just before reporting a healthy status
151151
ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing, bool segwit_active);

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ class PeerManagerImpl final : public PeerManager
974974
/** Orphan/conflicted/etc transactions that are kept for compact block reconstruction.
975975
* The last -blockreconstructionextratxn/DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN of
976976
* these are kept in a ring buffer */
977-
std::vector<CTransactionRef> vExtraTxnForCompact GUARDED_BY(g_msgproc_mutex);
977+
std::vector<std::pair<Wtxid, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_msgproc_mutex);
978978
/** Offset into vExtraTxnForCompact to insert the next tx */
979979
size_t vExtraTxnForCompactIt GUARDED_BY(g_msgproc_mutex) = 0;
980980

@@ -1768,7 +1768,7 @@ void PeerManagerImpl::AddToCompactExtraTransactions(const CTransactionRef& tx)
17681768
return;
17691769
if (!vExtraTxnForCompact.size())
17701770
vExtraTxnForCompact.resize(m_opts.max_extra_txs);
1771-
vExtraTxnForCompact[vExtraTxnForCompactIt] = tx;
1771+
vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
17721772
vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % m_opts.max_extra_txs;
17731773
}
17741774

src/test/blockencodings_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include <boost/test/unit_test.hpp>
1616

17-
const std::vector<CTransactionRef> empty_extra_txn;
17+
const std::vector<std::pair<Wtxid, CTransactionRef>> empty_extra_txn;
1818

1919
BOOST_FIXTURE_TEST_SUITE(blockencodings_tests, RegTestingSetup)
2020

@@ -318,7 +318,7 @@ BOOST_AUTO_TEST_CASE(ReceiveWithExtraTransactions) {
318318
const CTransactionRef non_block_tx = MakeTransactionRef(std::move(mtx));
319319

320320
CBlock block(BuildBlockTestCase(rand_ctx));
321-
std::vector<CTransactionRef> extra_txn;
321+
std::vector<std::pair<Wtxid, CTransactionRef>> extra_txn;
322322
extra_txn.resize(10);
323323

324324
LOCK2(cs_main, pool.cs);
@@ -342,9 +342,9 @@ BOOST_AUTO_TEST_CASE(ReceiveWithExtraTransactions) {
342342
BOOST_CHECK( partial_block.IsTxAvailable(2));
343343

344344
// Add an unrelated tx to extra_txn:
345-
extra_txn[0] = non_block_tx;
345+
extra_txn[0] = {non_block_tx->GetWitnessHash(), non_block_tx};
346346
// and a tx from the block that's not in the mempool:
347-
extra_txn[1] = block.vtx[1];
347+
extra_txn[1] = {block.vtx[1]->GetWitnessHash(), block.vtx[1]};
348348

349349
BOOST_CHECK(partial_block_with_extra.InitData(cmpctblock, extra_txn) == READ_STATUS_OK);
350350
BOOST_CHECK(partial_block_with_extra.IsTxAvailable(0));

src/test/fuzz/partially_downloaded_block.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
6767
// The coinbase is always available
6868
available.insert(0);
6969

70-
std::vector<CTransactionRef> extra_txn;
70+
std::vector<std::pair<Wtxid, CTransactionRef>> extra_txn;
7171
for (size_t i = 1; i < block->vtx.size(); ++i) {
7272
auto tx{block->vtx[i]};
7373

7474
bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
7575
bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
7676

7777
if (add_to_extra_txn) {
78-
extra_txn.emplace_back(tx);
78+
extra_txn.emplace_back(tx->GetWitnessHash(), tx);
7979
available.insert(i);
8080
}
8181

0 commit comments

Comments
 (0)