Skip to content

Commit 1662b43

Browse files
committed
Make CBlock::vtx a vector of shared_ptr<CTransaction>
1 parent da60506 commit 1662b43

24 files changed

+161
-149
lines changed

src/blockencodings.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock& block, bool f
2424
//TODO: Use our mempool prior to block acceptance to predictively fill more than just the coinbase
2525
prefilledtxn[0] = {0, block.vtx[0]};
2626
for (size_t i = 1; i < block.vtx.size(); i++) {
27-
const CTransaction& tx = block.vtx[i];
27+
const CTransaction& tx = *block.vtx[i];
2828
shorttxids[i - 1] = GetShortID(fUseWTXID ? tx.GetWitnessHash() : tx.GetHash());
2929
}
3030
}
@@ -59,7 +59,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
5959

6060
int32_t lastprefilledindex = -1;
6161
for (size_t i = 0; i < cmpctblock.prefilledtxn.size(); i++) {
62-
if (cmpctblock.prefilledtxn[i].tx.IsNull())
62+
if (cmpctblock.prefilledtxn[i].tx->IsNull())
6363
return READ_STATUS_INVALID;
6464

6565
lastprefilledindex += cmpctblock.prefilledtxn[i].index + 1; //index is a uint16_t, so cant overflow here
@@ -71,7 +71,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
7171
// have neither a prefilled txn or a shorttxid!
7272
return READ_STATUS_INVALID;
7373
}
74-
txn_available[lastprefilledindex] = std::make_shared<CTransaction>(cmpctblock.prefilledtxn[i].tx);
74+
txn_available[lastprefilledindex] = cmpctblock.prefilledtxn[i].tx;
7575
}
7676
prefilled_count = cmpctblock.prefilledtxn.size();
7777

@@ -142,7 +142,7 @@ bool PartiallyDownloadedBlock::IsTxAvailable(size_t index) const {
142142
return txn_available[index] ? true : false;
143143
}
144144

145-
ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<CTransaction>& vtx_missing) const {
145+
ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<std::shared_ptr<const CTransaction>>& vtx_missing) const {
146146
assert(!header.IsNull());
147147
block = header;
148148
block.vtx.resize(txn_available.size());
@@ -154,7 +154,7 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
154154
return READ_STATUS_INVALID;
155155
block.vtx[i] = vtx_missing[tx_missing_offset++];
156156
} else
157-
block.vtx[i] = *txn_available[i];
157+
block.vtx[i] = txn_available[i];
158158
}
159159
if (vtx_missing.size() != tx_missing_offset)
160160
return READ_STATUS_INVALID;
@@ -172,8 +172,8 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
172172

173173
LogPrint("cmpctblock", "Successfully reconstructed block %s with %lu txn prefilled, %lu txn from mempool and %lu txn requested\n", header.GetHash().ToString(), prefilled_count, mempool_count, vtx_missing.size());
174174
if (vtx_missing.size() < 5) {
175-
for(const CTransaction& tx : vtx_missing)
176-
LogPrint("cmpctblock", "Reconstructed block %s required tx %s\n", header.GetHash().ToString(), tx.GetHash().ToString());
175+
for (const auto& tx : vtx_missing)
176+
LogPrint("cmpctblock", "Reconstructed block %s required tx %s\n", header.GetHash().ToString(), tx->GetHash().ToString());
177177
}
178178

179179
return READ_STATUS_OK;

src/blockencodings.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class CTxMemPool;
1414
// Dumb helper to handle CTransaction compression at serialize-time
1515
struct TransactionCompressor {
1616
private:
17-
CTransaction& tx;
17+
std::shared_ptr<const CTransaction>& tx;
1818
public:
19-
TransactionCompressor(CTransaction& txIn) : tx(txIn) {}
19+
TransactionCompressor(std::shared_ptr<const CTransaction>& txIn) : tx(txIn) {}
2020

2121
ADD_SERIALIZE_METHODS;
2222

@@ -72,7 +72,7 @@ class BlockTransactions {
7272
public:
7373
// A BlockTransactions message
7474
uint256 blockhash;
75-
std::vector<CTransaction> txn;
75+
std::vector<std::shared_ptr<const CTransaction>> txn;
7676

7777
BlockTransactions() {}
7878
BlockTransactions(const BlockTransactionsRequest& req) :
@@ -104,7 +104,7 @@ struct PrefilledTransaction {
104104
// Used as an offset since last prefilled tx in CBlockHeaderAndShortTxIDs,
105105
// as a proper transaction-in-block-index in PartiallyDownloadedBlock
106106
uint16_t index;
107-
CTransaction tx;
107+
std::shared_ptr<const CTransaction> tx;
108108

109109
ADD_SERIALIZE_METHODS;
110110

@@ -202,7 +202,7 @@ class PartiallyDownloadedBlock {
202202

203203
ReadStatus InitData(const CBlockHeaderAndShortTxIDs& cmpctblock);
204204
bool IsTxAvailable(size_t index) const;
205-
ReadStatus FillBlock(CBlock& block, const std::vector<CTransaction>& vtx_missing) const;
205+
ReadStatus FillBlock(CBlock& block, const std::vector<std::shared_ptr<const CTransaction>>& vtx_missing) const;
206206
};
207207

208208
#endif

src/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
3131
genesis.nBits = nBits;
3232
genesis.nNonce = nNonce;
3333
genesis.nVersion = nVersion;
34-
genesis.vtx.push_back(txNew);
34+
genesis.vtx.push_back(std::make_shared<const CTransaction>(std::move(txNew)));
3535
genesis.hashPrevBlock.SetNull();
3636
genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
3737
return genesis;

src/consensus/merkle.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ uint256 BlockMerkleRoot(const CBlock& block, bool* mutated)
160160
std::vector<uint256> leaves;
161161
leaves.resize(block.vtx.size());
162162
for (size_t s = 0; s < block.vtx.size(); s++) {
163-
leaves[s] = block.vtx[s].GetHash();
163+
leaves[s] = block.vtx[s]->GetHash();
164164
}
165165
return ComputeMerkleRoot(leaves, mutated);
166166
}
@@ -171,7 +171,7 @@ uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated)
171171
leaves.resize(block.vtx.size());
172172
leaves[0].SetNull(); // The witness hash of the coinbase is 0.
173173
for (size_t s = 1; s < block.vtx.size(); s++) {
174-
leaves[s] = block.vtx[s].GetWitnessHash();
174+
leaves[s] = block.vtx[s]->GetWitnessHash();
175175
}
176176
return ComputeMerkleRoot(leaves, mutated);
177177
}
@@ -181,7 +181,7 @@ std::vector<uint256> BlockMerkleBranch(const CBlock& block, uint32_t position)
181181
std::vector<uint256> leaves;
182182
leaves.resize(block.vtx.size());
183183
for (size_t s = 0; s < block.vtx.size(); s++) {
184-
leaves[s] = block.vtx[s].GetHash();
184+
leaves[s] = block.vtx[s]->GetHash();
185185
}
186186
return ComputeMerkleBranch(leaves, position);
187187
}

src/core_memusage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ static inline size_t RecursiveDynamicUsage(const CMutableTransaction& tx) {
6969

7070
static inline size_t RecursiveDynamicUsage(const CBlock& block) {
7171
size_t mem = memusage::DynamicUsage(block.vtx);
72-
for (std::vector<CTransaction>::const_iterator it = block.vtx.begin(); it != block.vtx.end(); it++) {
73-
mem += RecursiveDynamicUsage(*it);
72+
for (const auto& tx : block.vtx) {
73+
mem += memusage::DynamicUsage(tx) + RecursiveDynamicUsage(*tx);
7474
}
7575
return mem;
7676
}

0 commit comments

Comments
 (0)