Skip to content

Commit b4e4ba4

Browse files
committed
Introduce convenience type CTransactionRef
1 parent 1662b43 commit b4e4ba4

16 files changed

+63
-57
lines changed

src/blockencodings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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<std::shared_ptr<const CTransaction>>& vtx_missing) const {
145+
ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing) const {
146146
assert(!header.IsNull());
147147
block = header;
148148
block.vtx.resize(txn_available.size());

src/blockencodings.h

Lines changed: 6 additions & 6 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-
std::shared_ptr<const CTransaction>& tx;
17+
CTransactionRef& tx;
1818
public:
19-
TransactionCompressor(std::shared_ptr<const CTransaction>& txIn) : tx(txIn) {}
19+
TransactionCompressor(CTransactionRef& 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<std::shared_ptr<const CTransaction>> txn;
75+
std::vector<CTransactionRef> 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-
std::shared_ptr<const CTransaction> tx;
107+
CTransactionRef tx;
108108

109109
ADD_SERIALIZE_METHODS;
110110

@@ -193,7 +193,7 @@ class CBlockHeaderAndShortTxIDs {
193193

194194
class PartiallyDownloadedBlock {
195195
protected:
196-
std::vector<std::shared_ptr<const CTransaction> > txn_available;
196+
std::vector<CTransactionRef> txn_available;
197197
size_t prefilled_count = 0, mempool_count = 0;
198198
CTxMemPool* pool;
199199
public:
@@ -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<std::shared_ptr<const CTransaction>>& vtx_missing) const;
205+
ReadStatus FillBlock(CBlock& block, const std::vector<CTransactionRef>& 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(std::make_shared<const CTransaction>(std::move(txNew)));
34+
genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
3535
genesis.hashPrevBlock.SetNull();
3636
genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
3737
return genesis;

src/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ namespace {
233233
int nPeersWithValidatedDownloads = 0;
234234

235235
/** Relay map, protected by cs_main. */
236-
typedef std::map<uint256, std::shared_ptr<const CTransaction>> MapRelay;
236+
typedef std::map<uint256, CTransactionRef> MapRelay;
237237
MapRelay mapRelay;
238238
/** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
239239
std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
@@ -1639,7 +1639,7 @@ bool GetTransaction(const uint256 &hash, CTransaction &txOut, const Consensus::P
16391639

16401640
LOCK(cs_main);
16411641

1642-
std::shared_ptr<const CTransaction> ptx = mempool.get(hash);
1642+
CTransactionRef ptx = mempool.get(hash);
16431643
if (ptx)
16441644
{
16451645
txOut = *ptx;
@@ -2845,7 +2845,7 @@ static int64_t nTimePostConnect = 0;
28452845
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
28462846
* corresponding to pindexNew, to bypass loading it again from disk.
28472847
*/
2848-
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, std::vector<std::shared_ptr<const CTransaction>> &txConflicted, std::vector<std::tuple<std::shared_ptr<const CTransaction>,CBlockIndex*,int>> &txChanged)
2848+
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, std::vector<CTransactionRef> &txConflicted, std::vector<std::tuple<CTransactionRef,CBlockIndex*,int>> &txChanged)
28492849
{
28502850
assert(pindexNew->pprev == chainActive.Tip());
28512851
// Read block from disk.
@@ -2968,7 +2968,7 @@ static void PruneBlockIndexCandidates() {
29682968
* Try to make some progress towards making pindexMostWork the active block.
29692969
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
29702970
*/
2971-
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, std::vector<std::shared_ptr<const CTransaction>>& txConflicted, std::vector<std::tuple<std::shared_ptr<const CTransaction>,CBlockIndex*,int>>& txChanged)
2971+
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, std::vector<CTransactionRef>& txConflicted, std::vector<std::tuple<CTransactionRef,CBlockIndex*,int>>& txChanged)
29722972
{
29732973
AssertLockHeld(cs_main);
29742974
const CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -3069,7 +3069,7 @@ static void NotifyHeaderTip() {
30693069
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
30703070
CBlockIndex *pindexMostWork = NULL;
30713071
CBlockIndex *pindexNewTip = NULL;
3072-
std::vector<std::tuple<std::shared_ptr<const CTransaction>,CBlockIndex*,int>> txChanged;
3072+
std::vector<std::tuple<CTransactionRef,CBlockIndex*,int>> txChanged;
30733073
if (pblock)
30743074
txChanged.reserve(pblock->vtx.size());
30753075
do {
@@ -3079,7 +3079,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
30793079
break;
30803080

30813081
const CBlockIndex *pindexFork;
3082-
std::vector<std::shared_ptr<const CTransaction>> txConflicted;
3082+
std::vector<CTransactionRef> txConflicted;
30833083
bool fInitialDownload;
30843084
{
30853085
LOCK(cs_main);
@@ -3523,7 +3523,7 @@ void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPr
35233523
tx.wit.vtxinwit.resize(1);
35243524
tx.wit.vtxinwit[0].scriptWitness.stack.resize(1);
35253525
tx.wit.vtxinwit[0].scriptWitness.stack[0] = nonce;
3526-
block.vtx[0] = std::make_shared<const CTransaction>(std::move(tx));
3526+
block.vtx[0] = MakeTransactionRef(std::move(tx));
35273527
}
35283528
}
35293529

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
178178
coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
179179
coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
180180
coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;
181-
pblock->vtx[0] = std::make_shared<const CTransaction>(std::move(coinbaseTx));
181+
pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
182182
pblocktemplate->vchCoinbaseCommitment = GenerateCoinbaseCommitment(*pblock, pindexPrev, chainparams.GetConsensus());
183183
pblocktemplate->vTxFees[0] = -nFees;
184184

@@ -605,6 +605,6 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned
605605
txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
606606
assert(txCoinbase.vin[0].scriptSig.size() <= 100);
607607

608-
pblock->vtx[0] = std::make_shared<const CTransaction>(std::move(txCoinbase));
608+
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
609609
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
610610
}

src/primitives/block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class CBlock : public CBlockHeader
7373
{
7474
public:
7575
// network and disk
76-
std::vector<std::shared_ptr<const CTransaction>> vtx;
76+
std::vector<CTransactionRef> vtx;
7777

7878
// memory only
7979
mutable bool fChecked;

src/primitives/transaction.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,12 @@ struct CMutableTransaction
475475
uint256 GetHash() const;
476476
};
477477

478+
typedef std::shared_ptr<const CTransaction> CTransactionRef;
479+
static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
480+
template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
481+
static inline CTransactionRef MakeTransactionRef(const CTransactionRef& txIn) { return txIn; }
482+
static inline CTransactionRef MakeTransactionRef(CTransactionRef&& txIn) { return std::move(txIn); }
483+
478484
/** Compute the weight of a transaction, as defined by BIP 141 */
479485
int64_t GetTransactionWeight(const CTransaction &tx);
480486

src/test/blockencodings_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ static CBlock BuildBlockTestCase() {
2626
tx.vout[0].nValue = 42;
2727

2828
block.vtx.resize(3);
29-
block.vtx[0] = std::make_shared<const CTransaction>(tx);
29+
block.vtx[0] = MakeTransactionRef(tx);
3030
block.nVersion = 42;
3131
block.hashPrevBlock = GetRandHash();
3232
block.nBits = 0x207fffff;
3333

3434
tx.vin[0].prevout.hash = GetRandHash();
3535
tx.vin[0].prevout.n = 0;
36-
block.vtx[1] = std::make_shared<const CTransaction>(tx);
36+
block.vtx[1] = MakeTransactionRef(tx);
3737

3838
tx.vin.resize(10);
3939
for (size_t i = 0; i < tx.vin.size(); i++) {
4040
tx.vin[i].prevout.hash = GetRandHash();
4141
tx.vin[i].prevout.n = 0;
4242
}
43-
block.vtx[2] = std::make_shared<const CTransaction>(tx);
43+
block.vtx[2] = MakeTransactionRef(tx);
4444

4545
bool mutated;
4646
block.hashMerkleRoot = BlockMerkleRoot(block, &mutated);
@@ -80,12 +80,12 @@ BOOST_AUTO_TEST_CASE(SimpleRoundTripTest)
8080

8181
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
8282

83-
std::vector<std::shared_ptr<const CTransaction>> removed;
83+
std::vector<CTransactionRef> removed;
8484
pool.removeRecursive(*block.vtx[2], &removed);
8585
BOOST_CHECK_EQUAL(removed.size(), 1);
8686

8787
CBlock block2;
88-
std::vector<std::shared_ptr<const CTransaction>> vtx_missing;
88+
std::vector<CTransactionRef> vtx_missing;
8989
BOOST_CHECK(partialBlock.FillBlock(block2, vtx_missing) == READ_STATUS_INVALID); // No transactions
9090

9191
vtx_missing.push_back(block.vtx[2]); // Wrong transaction
@@ -181,7 +181,7 @@ BOOST_AUTO_TEST_CASE(NonCoinbasePreforwardRTTest)
181181
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
182182

183183
CBlock block2;
184-
std::vector<std::shared_ptr<const CTransaction>> vtx_missing;
184+
std::vector<CTransactionRef> vtx_missing;
185185
BOOST_CHECK(partialBlock.FillBlock(block2, vtx_missing) == READ_STATUS_INVALID); // No transactions
186186

187187
vtx_missing.push_back(block.vtx[1]); // Wrong transaction
@@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE(SufficientPreforwardRTTest)
240240
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[1]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);
241241

242242
CBlock block2;
243-
std::vector<std::shared_ptr<const CTransaction>> vtx_missing;
243+
std::vector<CTransactionRef> vtx_missing;
244244
BOOST_CHECK(partialBlock.FillBlock(block2, vtx_missing) == READ_STATUS_OK);
245245
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block2.GetHash().ToString());
246246
bool mutated;
@@ -266,7 +266,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
266266

267267
CBlock block;
268268
block.vtx.resize(1);
269-
block.vtx[0] = std::make_shared<const CTransaction>(std::move(coinbase));
269+
block.vtx[0] = MakeTransactionRef(std::move(coinbase));
270270
block.nVersion = 42;
271271
block.hashPrevBlock = GetRandHash();
272272
block.nBits = 0x207fffff;
@@ -291,7 +291,7 @@ BOOST_AUTO_TEST_CASE(EmptyBlockRoundTripTest)
291291
BOOST_CHECK(partialBlock.IsTxAvailable(0));
292292

293293
CBlock block2;
294-
std::vector<std::shared_ptr<const CTransaction>> vtx_missing;
294+
std::vector<CTransactionRef> vtx_missing;
295295
BOOST_CHECK(partialBlock.FillBlock(block2, vtx_missing) == READ_STATUS_OK);
296296
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block2.GetHash().ToString());
297297
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block2, &mutated).ToString());

src/test/mempool_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
5555

5656

5757
CTxMemPool testPool(CFeeRate(0));
58-
std::vector<std::shared_ptr<const CTransaction>> removed;
58+
std::vector<CTransactionRef> removed;
5959

6060
// Nothing in pool, remove should do nothing:
6161
testPool.removeRecursive(txParent, &removed);
@@ -410,8 +410,8 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest)
410410
CheckSort<ancestor_score>(pool, sortedOrder);
411411

412412
/* after tx6 is mined, tx7 should move up in the sort */
413-
std::vector<std::shared_ptr<const CTransaction>> vtx;
414-
vtx.push_back(std::make_shared<const CTransaction>(tx6));
413+
std::vector<CTransactionRef> vtx;
414+
vtx.push_back(MakeTransactionRef(tx6));
415415
pool.removeForBlock(vtx, 1, NULL, false);
416416

417417
sortedOrder.erase(sortedOrder.begin()+1);
@@ -546,7 +546,7 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
546546
pool.addUnchecked(tx5.GetHash(), entry.Fee(1000LL).FromTx(tx5, &pool));
547547
pool.addUnchecked(tx7.GetHash(), entry.Fee(9000LL).FromTx(tx7, &pool));
548548

549-
std::vector<std::shared_ptr<const CTransaction>> vtx;
549+
std::vector<CTransactionRef> vtx;
550550
SetMockTime(42);
551551
SetMockTime(42 + CTxMemPool::ROLLING_FEE_HALFLIFE);
552552
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 1000);

src/test/merkle_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static uint256 BlockBuildMerkleTree(const CBlock& block, bool* fMutated, std::ve
1515
{
1616
vMerkleTree.clear();
1717
vMerkleTree.reserve(block.vtx.size() * 2 + 16); // Safe upper bound for the number of total nodes.
18-
for (std::vector<std::shared_ptr<const CTransaction>>::const_iterator it(block.vtx.begin()); it != block.vtx.end(); ++it)
18+
for (std::vector<CTransactionRef>::const_iterator it(block.vtx.begin()); it != block.vtx.end(); ++it)
1919
vMerkleTree.push_back((*it)->GetHash());
2020
int j = 0;
2121
bool mutated = false;
@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
8686
for (int j = 0; j < ntx; j++) {
8787
CMutableTransaction mtx;
8888
mtx.nLockTime = j;
89-
block.vtx[j] = std::make_shared<const CTransaction>(mtx);
89+
block.vtx[j] = MakeTransactionRef(std::move(mtx));
9090
}
9191
// Compute the root of the block before mutating it.
9292
bool unmutatedMutated = false;

0 commit comments

Comments
 (0)