Skip to content

Commit bf031a5

Browse files
committed
Merge bitcoin/bitcoin#29752: refactor: Use typesafe Wtxid in compact block encodings
a8203e9 refactor: Simplify `extra_txn` to be a vec of CTransactionRef instead of a vec of pair<Wtxid, CTransactionRef> (AngusP) c3c1843 refactor: Use typesafe Wtxid in compact block encoding message, instead of ambiguous uint256. (AngusP) Pull request description: The first commit replaces `uint256` with typesafe `Wtxid` (or `Txid`) types introduced in #28107. The second commit then simplifies the extra tx `vector` to just be of `CTransactionRef`s instead of a `std::pair<Wtxid, CTransactionRef>`, as it's easy to get a `Wtxid` from a transaction ref. ACKs for top commit: glozow: ACK a8203e9 dergoegge: ACK a8203e9 Tree-SHA512: b4ba1423a8059f9fc118782bd8a668213d229c822f22b01267de74d6ea97fe4f2aad46b5da7c0178ecc9905293e9a0eafba1d75330297c055e27fd53c8c8ebfd
2 parents f348ec7 + a8203e9 commit bf031a5

5 files changed

+20
-17
lines changed

src/blockencodings.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ void CBlockHeaderAndShortTxIDs::FillShortTxIDSelector() const {
4040
shorttxidk1 = shorttxidhash.GetUint64(1);
4141
}
4242

43-
uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const uint256& txhash) const {
43+
uint64_t CBlockHeaderAndShortTxIDs::GetShortID(const Wtxid& wtxid) const {
4444
static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids calculation assumes 6-byte shorttxids");
45-
return SipHashUint256(shorttxidk0, shorttxidk1, txhash) & 0xffffffffffffL;
45+
return SipHashUint256(shorttxidk0, shorttxidk1, wtxid) & 0xffffffffffffL;
4646
}
4747

4848

4949

50-
ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn) {
50+
ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<CTransactionRef>& extra_txn) {
5151
if (cmpctblock.header.IsNull() || (cmpctblock.shorttxids.empty() && cmpctblock.prefilledtxn.empty()))
5252
return READ_STATUS_INVALID;
5353
if (cmpctblock.shorttxids.size() + cmpctblock.prefilledtxn.size() > MAX_BLOCK_WEIGHT / MIN_SERIALIZABLE_TRANSACTION_WEIGHT)
@@ -134,11 +134,14 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
134134
}
135135

136136
for (size_t i = 0; i < extra_txn.size(); i++) {
137-
uint64_t shortid = cmpctblock.GetShortID(extra_txn[i].first);
137+
if (extra_txn[i] == nullptr) {
138+
continue;
139+
}
140+
uint64_t shortid = cmpctblock.GetShortID(extra_txn[i]->GetWitnessHash());
138141
std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid);
139142
if (idit != shorttxids.end()) {
140143
if (!have_txn[idit->second]) {
141-
txn_available[idit->second] = extra_txn[i].second;
144+
txn_available[idit->second] = extra_txn[i];
142145
have_txn[idit->second] = true;
143146
mempool_count++;
144147
extra_count++;
@@ -150,7 +153,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
150153
// Note that we don't want duplication between extra_txn and mempool to
151154
// trigger this case, so we compare witness hashes first
152155
if (txn_available[idit->second] &&
153-
txn_available[idit->second]->GetWitnessHash() != extra_txn[i].second->GetWitnessHash()) {
156+
txn_available[idit->second]->GetWitnessHash() != extra_txn[i]->GetWitnessHash()) {
154157
txn_available[idit->second].reset();
155158
mempool_count--;
156159
extra_count--;

src/blockencodings.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class CBlockHeaderAndShortTxIDs {
111111

112112
CBlockHeaderAndShortTxIDs(const CBlock& block);
113113

114-
uint64_t GetShortID(const uint256& txhash) const;
114+
uint64_t GetShortID(const Wtxid& wtxid) const;
115115

116116
size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
117117

@@ -142,7 +142,7 @@ class PartiallyDownloadedBlock {
142142
explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
143143

144144
// extra_txn is a list of extra transactions to look at, in <witness hash, reference> form
145-
ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<std::pair<uint256, CTransactionRef>>& extra_txn);
145+
ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, const std::vector<CTransactionRef>& extra_txn);
146146
bool IsTxAvailable(size_t index) const;
147147
ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing);
148148
};

src/net_processing.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ class PeerManagerImpl final : public PeerManager
10061006
/** Orphan/conflicted/etc transactions that are kept for compact block reconstruction.
10071007
* The last -blockreconstructionextratxn/DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN of
10081008
* these are kept in a ring buffer */
1009-
std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(g_msgproc_mutex);
1009+
std::vector<CTransactionRef> vExtraTxnForCompact GUARDED_BY(g_msgproc_mutex);
10101010
/** Offset into vExtraTxnForCompact to insert the next tx */
10111011
size_t vExtraTxnForCompactIt GUARDED_BY(g_msgproc_mutex) = 0;
10121012

@@ -1802,7 +1802,7 @@ void PeerManagerImpl::AddToCompactExtraTransactions(const CTransactionRef& tx)
18021802
return;
18031803
if (!vExtraTxnForCompact.size())
18041804
vExtraTxnForCompact.resize(m_opts.max_extra_txs);
1805-
vExtraTxnForCompact[vExtraTxnForCompactIt] = std::make_pair(tx->GetWitnessHash(), tx);
1805+
vExtraTxnForCompact[vExtraTxnForCompactIt] = tx;
18061806
vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % m_opts.max_extra_txs;
18071807
}
18081808

src/test/blockencodings_tests.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

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

17-
std::vector<std::pair<uint256, CTransactionRef>> extra_txn;
17+
std::vector<CTransactionRef> extra_txn;
1818

1919
BOOST_FIXTURE_TEST_SUITE(blockencodings_tests, RegTestingSetup)
2020

@@ -126,7 +126,7 @@ class TestHeaderAndShortIDs {
126126
explicit TestHeaderAndShortIDs(const CBlock& block) :
127127
TestHeaderAndShortIDs(CBlockHeaderAndShortTxIDs{block}) {}
128128

129-
uint64_t GetShortID(const uint256& txhash) const {
129+
uint64_t GetShortID(const Wtxid& txhash) const {
130130
DataStream stream{};
131131
stream << *this;
132132
CBlockHeaderAndShortTxIDs base;
@@ -155,8 +155,8 @@ BOOST_AUTO_TEST_CASE(NonCoinbasePreforwardRTTest)
155155
shortIDs.prefilledtxn.resize(1);
156156
shortIDs.prefilledtxn[0] = {1, block.vtx[1]};
157157
shortIDs.shorttxids.resize(2);
158-
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[0]->GetHash());
159-
shortIDs.shorttxids[1] = shortIDs.GetShortID(block.vtx[2]->GetHash());
158+
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[0]->GetWitnessHash());
159+
shortIDs.shorttxids[1] = shortIDs.GetShortID(block.vtx[2]->GetWitnessHash());
160160

161161
DataStream stream{};
162162
stream << shortIDs;
@@ -226,7 +226,7 @@ BOOST_AUTO_TEST_CASE(SufficientPreforwardRTTest)
226226
shortIDs.prefilledtxn[0] = {0, block.vtx[0]};
227227
shortIDs.prefilledtxn[1] = {1, block.vtx[2]}; // id == 1 as it is 1 after index 1
228228
shortIDs.shorttxids.resize(1);
229-
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[1]->GetHash());
229+
shortIDs.shorttxids[0] = shortIDs.GetShortID(block.vtx[1]->GetWitnessHash());
230230

231231
DataStream stream{};
232232
stream << shortIDs;

src/test/fuzz/partially_downloaded_block.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
6060
// The coinbase is always available
6161
available.insert(0);
6262

63-
std::vector<std::pair<uint256, CTransactionRef>> extra_txn;
63+
std::vector<CTransactionRef> extra_txn;
6464
for (size_t i = 1; i < block->vtx.size(); ++i) {
6565
auto tx{block->vtx[i]};
6666

6767
bool add_to_extra_txn{fuzzed_data_provider.ConsumeBool()};
6868
bool add_to_mempool{fuzzed_data_provider.ConsumeBool()};
6969

7070
if (add_to_extra_txn) {
71-
extra_txn.emplace_back(tx->GetWitnessHash(), tx);
71+
extra_txn.emplace_back(tx);
7272
available.insert(i);
7373
}
7474

0 commit comments

Comments
 (0)