Skip to content

Commit 744d265

Browse files
committed
Merge #8223: [c++11] Use std::unique_ptr for block creation.
9fce062 [c++11] Use std::unique_ptr for block creation. (Daniel Kraft)
2 parents e10af96 + 9fce062 commit 744d265

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

@@ -122,14 +123,14 @@ void BlockAssembler::resetBlock()
122123
blockFinished = false;
123124
}
124125

125-
CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
126+
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
126127
{
127128
resetBlock();
128129

129130
pblocktemplate.reset(new CBlockTemplate());
130131

131132
if(!pblocktemplate.get())
132-
return NULL;
133+
return nullptr;
133134
pblock = &pblocktemplate->block; // pointer for convenience
134135

135136
// Add dummy coinbase tx as first transaction
@@ -194,7 +195,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
194195
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
195196
}
196197

197-
return pblocktemplate.release();
198+
return std::move(pblocktemplate);
198199
}
199200

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

src/miner.h

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

169169
private:
170170
// 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>
@@ -515,24 +516,19 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
515516
// Update block
516517
static CBlockIndex* pindexPrev;
517518
static int64_t nStart;
518-
static CBlockTemplate* pblocktemplate;
519+
static std::unique_ptr<CBlockTemplate> pblocktemplate;
519520
if (pindexPrev != chainActive.Tip() ||
520521
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
521522
{
522523
// Clear pindexPrev so future calls make a new block, despite any failures from here on
523-
pindexPrev = NULL;
524+
pindexPrev = nullptr;
524525

525526
// Store the pindexBest used before CreateNewBlock, to avoid races
526527
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
527528
CBlockIndex* pindexPrevNew = chainActive.Tip();
528529
nStart = GetTime();
529530

530531
// Create new block
531-
if(pblocktemplate)
532-
{
533-
delete pblocktemplate;
534-
pblocktemplate = NULL;
535-
}
536532
CScript scriptDummy = CScript() << OP_TRUE;
537533
pblocktemplate = BlockAssembler(Params()).CreateNewBlock(scriptDummy);
538534
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);
@@ -184,7 +186,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
184186
// Note that by default, these tests run with size accounting enabled.
185187
const CChainParams& chainparams = Params(CBaseChainParams::MAIN);
186188
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
187-
CBlockTemplate *pblocktemplate;
189+
std::unique_ptr<CBlockTemplate> pblocktemplate;
188190
CMutableTransaction tx,tx2;
189191
CScript script;
190192
uint256 hash;
@@ -226,11 +228,9 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
226228
BOOST_CHECK(state.IsValid());
227229
pblock->hashPrevBlock = pblock->GetHash();
228230
}
229-
delete pblocktemplate;
230231

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

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

275274
// block size > limit
@@ -290,7 +289,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
290289
tx.vin[0].prevout.hash = hash;
291290
}
292291
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
293-
delete pblocktemplate;
294292
mempool.clear();
295293

296294
// orphan in mempool, template creation fails
@@ -314,7 +312,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
314312
hash = tx.GetHash();
315313
mempool.addUnchecked(hash, entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
316314
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
317-
delete pblocktemplate;
318315
mempool.clear();
319316

320317
// coinbase in mempool, template creation fails
@@ -372,7 +369,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
372369
chainActive.SetTip(next);
373370
}
374371
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
375-
delete pblocktemplate;
376372
// Extend to a 210000-long block chain.
377373
while (chainActive.Tip()->nHeight < 210000) {
378374
CBlockIndex* prev = chainActive.Tip();
@@ -385,7 +381,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
385381
chainActive.SetTip(next);
386382
}
387383
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
388-
delete pblocktemplate;
389384
// Delete the dummy blocks again.
390385
while (chainActive.Tip()->nHeight > nHeight) {
391386
CBlockIndex* del = chainActive.Tip();
@@ -478,7 +473,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
478473
// but relative locked txs will if inconsistently added to mempool.
479474
// For now these will still generate a valid template until BIP68 soft fork
480475
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3);
481-
delete pblocktemplate;
482476
// However if we advance height by 1 and time by 512, all of them should be mined
483477
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
484478
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
@@ -487,7 +481,6 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
487481

488482
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
489483
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5);
490-
delete pblocktemplate;
491484

492485
chainActive.Tip()->nHeight--;
493486
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>
@@ -111,7 +113,7 @@ CBlock
111113
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
112114
{
113115
const CChainParams& chainparams = Params();
114-
CBlockTemplate *pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
116+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
115117
CBlock& block = pblocktemplate->block;
116118

117119
// Replace mempool-selected txns with just coinbase plus passed-in txns:
@@ -128,7 +130,6 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
128130
ProcessNewBlock(state, chainparams, NULL, &block, true, NULL, connman);
129131

130132
CBlock result = block;
131-
delete pblocktemplate;
132133
return result;
133134
}
134135

0 commit comments

Comments
 (0)