Skip to content

Commit 9fce062

Browse files
committed
[c++11] Use std::unique_ptr for block creation.
CreateNewBlock returns a pointer for which the caller takes ownership. Use std::unique_ptr to make this explicit and simplify handling of these objects in getblocktemplate.
1 parent ed2cd59 commit 9fce062

File tree

5 files changed

+15
-24
lines changed

5 files changed

+15
-24
lines changed

src/miner.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <boost/thread.hpp>
3030
#include <boost/tuple/tuple.hpp>
3131
#include <queue>
32+
#include <utility>
3233

3334
using namespace std;
3435

@@ -102,14 +103,14 @@ void BlockAssembler::resetBlock()
102103
blockFinished = false;
103104
}
104105

105-
CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
106+
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
106107
{
107108
resetBlock();
108109

109110
pblocktemplate.reset(new CBlockTemplate());
110111

111112
if(!pblocktemplate.get())
112-
return NULL;
113+
return nullptr;
113114
pblock = &pblocktemplate->block; // pointer for convenience
114115

115116
// Add dummy coinbase tx as first transaction
@@ -164,7 +165,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
164165
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
165166
}
166167

167-
return pblocktemplate.release();
168+
return std::move(pblocktemplate);
168169
}
169170

170171
bool BlockAssembler::isStillDependent(CTxMemPool::txiter iter)

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class BlockAssembler
160160
public:
161161
BlockAssembler(const CChainParams& chainparams);
162162
/** Construct a new block template with coinbase to scriptPubKeyIn */
163-
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
163+
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
164164

165165
private:
166166
// utility functions

src/rpc/mining.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "utilstrencodings.h"
2323
#include "validationinterface.h"
2424

25+
#include <memory>
2526
#include <stdint.h>
2627

2728
#include <boost/assign/list_of.hpp>
@@ -508,24 +509,19 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
508509
// Update block
509510
static CBlockIndex* pindexPrev;
510511
static int64_t nStart;
511-
static CBlockTemplate* pblocktemplate;
512+
static std::unique_ptr<CBlockTemplate> pblocktemplate;
512513
if (pindexPrev != chainActive.Tip() ||
513514
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
514515
{
515516
// Clear pindexPrev so future calls make a new block, despite any failures from here on
516-
pindexPrev = NULL;
517+
pindexPrev = nullptr;
517518

518519
// Store the pindexBest used before CreateNewBlock, to avoid races
519520
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
520521
CBlockIndex* pindexPrevNew = chainActive.Tip();
521522
nStart = GetTime();
522523

523524
// Create new block
524-
if(pblocktemplate)
525-
{
526-
delete pblocktemplate;
527-
pblocktemplate = NULL;
528-
}
529525
CScript scriptDummy = CScript() << OP_TRUE;
530526
pblocktemplate = BlockAssembler(Params()).CreateNewBlock(scriptDummy);
531527
if (!pblocktemplate)

src/test/miner_tests.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "test/test_bitcoin.h"
2020

21+
#include <memory>
22+
2123
#include <boost/test/unit_test.hpp>
2224

2325
BOOST_FIXTURE_TEST_SUITE(miner_tests, TestingSetup)
@@ -105,7 +107,7 @@ void TestPackageSelection(const CChainParams& chainparams, CScript scriptPubKey,
105107
uint256 hashHighFeeTx = tx.GetHash();
106108
mempool.addUnchecked(hashHighFeeTx, entry.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
107109

108-
CBlockTemplate *pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
110+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
109111
BOOST_CHECK(pblocktemplate->block.vtx[1].GetHash() == hashParentTx);
110112
BOOST_CHECK(pblocktemplate->block.vtx[2].GetHash() == hashHighFeeTx);
111113
BOOST_CHECK(pblocktemplate->block.vtx[3].GetHash() == hashMediumFeeTx);
@@ -183,7 +185,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
183185
{
184186
const CChainParams& chainparams = Params(CBaseChainParams::MAIN);
185187
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
186-
CBlockTemplate *pblocktemplate;
188+
std::unique_ptr<CBlockTemplate> pblocktemplate;
187189
CMutableTransaction tx,tx2;
188190
CScript script;
189191
uint256 hash;
@@ -225,11 +227,9 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
225227
BOOST_CHECK(state.IsValid());
226228
pblock->hashPrevBlock = pblock->GetHash();
227229
}
228-
delete pblocktemplate;
229230

230231
// Just to make sure we can still make simple blocks
231232
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
232-
delete pblocktemplate;
233233

234234
const CAmount BLOCKSUBSIDY = 50*COIN;
235235
const CAmount LOWFEE = CENT;
@@ -268,7 +268,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
268268
tx.vin[0].prevout.hash = hash;
269269
}
270270
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
271-
delete pblocktemplate;
272271
mempool.clear();
273272

274273
// block size > limit
@@ -289,7 +288,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
289288
tx.vin[0].prevout.hash = hash;
290289
}
291290
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
292-
delete pblocktemplate;
293291
mempool.clear();
294292

295293
// orphan in mempool, template creation fails
@@ -313,7 +311,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
313311
hash = tx.GetHash();
314312
mempool.addUnchecked(hash, entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
315313
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
316-
delete pblocktemplate;
317314
mempool.clear();
318315

319316
// coinbase in mempool, template creation fails
@@ -371,7 +368,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
371368
chainActive.SetTip(next);
372369
}
373370
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
374-
delete pblocktemplate;
375371
// Extend to a 210000-long block chain.
376372
while (chainActive.Tip()->nHeight < 210000) {
377373
CBlockIndex* prev = chainActive.Tip();
@@ -384,7 +380,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
384380
chainActive.SetTip(next);
385381
}
386382
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
387-
delete pblocktemplate;
388383
// Delete the dummy blocks again.
389384
while (chainActive.Tip()->nHeight > nHeight) {
390385
CBlockIndex* del = chainActive.Tip();
@@ -477,7 +472,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
477472
// but relative locked txs will if inconsistently added to mempool.
478473
// For now these will still generate a valid template until BIP68 soft fork
479474
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3);
480-
delete pblocktemplate;
481475
// However if we advance height by 1 and time by 512, all of them should be mined
482476
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
483477
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
@@ -486,7 +480,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
486480

487481
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
488482
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5);
489-
delete pblocktemplate;
490483

491484
chainActive.Tip()->nHeight--;
492485
SetMockTime(0);

src/test/test_bitcoin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include "test/testutil.h"
2424

25+
#include <memory>
26+
2527
#include <boost/filesystem.hpp>
2628
#include <boost/test/unit_test.hpp>
2729
#include <boost/thread.hpp>
@@ -98,7 +100,7 @@ CBlock
98100
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
99101
{
100102
const CChainParams& chainparams = Params();
101-
CBlockTemplate *pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
103+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
102104
CBlock& block = pblocktemplate->block;
103105

104106
// Replace mempool-selected txns with just coinbase plus passed-in txns:
@@ -115,7 +117,6 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
115117
ProcessNewBlock(state, chainparams, NULL, &block, true, NULL);
116118

117119
CBlock result = block;
118-
delete pblocktemplate;
119120
return result;
120121
}
121122

0 commit comments

Comments
 (0)