Skip to content

Commit d223bc9

Browse files
Use unique_ptr for pcoinscatcher/pcoinsdbview/pcoinsTip/pblocktree
* pcoinscatcher (CCoinsViewErrorCatcher) * pcoinsdbview (CCoinsViewDB) * pcoinsTip (CCoinsViewCache) * pblocktree (CBlockTreeDB) * Remove variables shadowing pcoinsdbview
1 parent b45c597 commit d223bc9

File tree

9 files changed

+47
-57
lines changed

9 files changed

+47
-57
lines changed

src/init.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class CCoinsViewErrorCatcher final : public CCoinsViewBacked
152152
// Writes do not need similar protection, as failure to write is handled by the caller.
153153
};
154154

155-
static CCoinsViewErrorCatcher *pcoinscatcher = nullptr;
155+
static std::unique_ptr<CCoinsViewErrorCatcher> pcoinscatcher;
156156
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
157157

158158
void Interrupt(boost::thread_group& threadGroup)
@@ -235,14 +235,10 @@ void Shutdown()
235235
if (pcoinsTip != nullptr) {
236236
FlushStateToDisk();
237237
}
238-
delete pcoinsTip;
239-
pcoinsTip = nullptr;
240-
delete pcoinscatcher;
241-
pcoinscatcher = nullptr;
242-
delete pcoinsdbview;
243-
pcoinsdbview = nullptr;
244-
delete pblocktree;
245-
pblocktree = nullptr;
238+
pcoinsTip.reset();
239+
pcoinscatcher.reset();
240+
pcoinsdbview.reset();
241+
pblocktree.reset();
246242
}
247243
#ifdef ENABLE_WALLET
248244
StopWallets();
@@ -1406,12 +1402,10 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14061402
do {
14071403
try {
14081404
UnloadBlockIndex();
1409-
delete pcoinsTip;
1410-
delete pcoinsdbview;
1411-
delete pcoinscatcher;
1412-
delete pblocktree;
1413-
1414-
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReset);
1405+
pcoinsTip.reset();
1406+
pcoinsdbview.reset();
1407+
pcoinscatcher.reset();
1408+
pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReset));
14151409

14161410
if (fReset) {
14171411
pblocktree->WriteReindexing(true);
@@ -1462,8 +1456,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14621456
// At this point we're either in reindex or we've loaded a useful
14631457
// block tree into mapBlockIndex!
14641458

1465-
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReset || fReindexChainState);
1466-
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
1459+
pcoinsdbview.reset(new CCoinsViewDB(nCoinDBCache, false, fReset || fReindexChainState));
1460+
pcoinscatcher.reset(new CCoinsViewErrorCatcher(pcoinsdbview.get()));
14671461

14681462
// If necessary, upgrade from older database format.
14691463
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
@@ -1473,13 +1467,13 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14731467
}
14741468

14751469
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
1476-
if (!ReplayBlocks(chainparams, pcoinsdbview)) {
1470+
if (!ReplayBlocks(chainparams, pcoinsdbview.get())) {
14771471
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
14781472
break;
14791473
}
14801474

14811475
// The on-disk coinsdb is now in a good state, create the cache
1482-
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
1476+
pcoinsTip.reset(new CCoinsViewCache(pcoinscatcher.get()));
14831477

14841478
bool is_coinsview_empty = fReset || fReindexChainState || pcoinsTip->GetBestBlock().IsNull();
14851479
if (!is_coinsview_empty) {
@@ -1521,7 +1515,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15211515
}
15221516
}
15231517

1524-
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, gArgs.GetArg("-checklevel", DEFAULT_CHECKLEVEL),
1518+
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview.get(), gArgs.GetArg("-checklevel", DEFAULT_CHECKLEVEL),
15251519
gArgs.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
15261520
strLoadError = _("Corrupted block database detected");
15271521
break;

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21062106

21072107
if (!AlreadyHave(inv) &&
21082108
AcceptToMemoryPool(mempool, state, ptx, &fMissingInputs, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2109-
mempool.check(pcoinsTip);
2109+
mempool.check(pcoinsTip.get());
21102110
RelayTransaction(tx, connman);
21112111
for (unsigned int i = 0; i < tx.vout.size(); i++) {
21122112
vWorkQueue.emplace_back(inv.hash, i);
@@ -2173,7 +2173,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
21732173
recentRejects->insert(orphanHash);
21742174
}
21752175
}
2176-
mempool.check(pcoinsTip);
2176+
mempool.check(pcoinsTip.get());
21772177
}
21782178
}
21792179

src/qt/test/rpcnestedtests.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class RPCNestedTests : public QObject
1717

1818
private Q_SLOTS:
1919
void rpcNestedTests();
20-
21-
private:
22-
CCoinsViewDB *pcoinsdbview;
2320
};
2421

2522
#endif // BITCOIN_QT_TEST_RPC_NESTED_TESTS_H

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ UniValue gettxoutsetinfo(const JSONRPCRequest& request)
928928

929929
CCoinsStats stats;
930930
FlushStateToDisk();
931-
if (GetUTXOStats(pcoinsdbview, stats)) {
931+
if (GetUTXOStats(pcoinsdbview.get(), stats)) {
932932
ret.push_back(Pair("height", (int64_t)stats.nHeight));
933933
ret.push_back(Pair("bestblock", stats.hashBlock.GetHex()));
934934
ret.push_back(Pair("transactions", (int64_t)stats.nTransactions));
@@ -996,7 +996,7 @@ UniValue gettxout(const JSONRPCRequest& request)
996996
Coin coin;
997997
if (fMempool) {
998998
LOCK(mempool.cs);
999-
CCoinsViewMemPool view(pcoinsTip, mempool);
999+
CCoinsViewMemPool view(pcoinsTip.get(), mempool);
10001000
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
10011001
return NullUniValue;
10021002
}
@@ -1048,7 +1048,7 @@ UniValue verifychain(const JSONRPCRequest& request)
10481048
if (!request.params[1].isNull())
10491049
nCheckDepth = request.params[1].get_int();
10501050

1051-
return CVerifyDB().VerifyDB(Params(), pcoinsTip, nCheckLevel, nCheckDepth);
1051+
return CVerifyDB().VerifyDB(Params(), pcoinsTip.get(), nCheckLevel, nCheckDepth);
10521052
}
10531053

10541054
/** Implementation of IsSuperMajority with better feedback */

src/test/test_bitcoin.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
8181
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
8282

8383
mempool.setSanityCheck(1.0);
84-
pblocktree = new CBlockTreeDB(1 << 20, true);
85-
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
86-
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
84+
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
85+
pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true));
86+
pcoinsTip.reset(new CCoinsViewCache(pcoinsdbview.get()));
8787
if (!LoadGenesisBlock(chainparams)) {
8888
throw std::runtime_error("LoadGenesisBlock failed.");
8989
}
@@ -110,9 +110,9 @@ TestingSetup::~TestingSetup()
110110
g_connman.reset();
111111
peerLogic.reset();
112112
UnloadBlockIndex();
113-
delete pcoinsTip;
114-
delete pcoinsdbview;
115-
delete pblocktree;
113+
pcoinsTip.reset();
114+
pcoinsdbview.reset();
115+
pblocktree.reset();
116116
fs::remove_all(pathTemp);
117117
}
118118

src/test/test_bitcoin.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ struct CConnmanTest {
5757

5858
class PeerLogicValidation;
5959
struct TestingSetup: public BasicTestingSetup {
60-
CCoinsViewDB *pcoinsdbview;
6160
fs::path pathTemp;
6261
boost::thread_group threadGroup;
6362
CConnman* connman;

src/test/txvalidationcache_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void ValidateCheckInputsForAllFlags(CMutableTransaction &tx, uint32_t failing_fl
119119
// WITNESS requires P2SH
120120
test_flags |= SCRIPT_VERIFY_P2SH;
121121
}
122-
bool ret = CheckInputs(tx, state, pcoinsTip, true, test_flags, true, add_to_cache, txdata, nullptr);
122+
bool ret = CheckInputs(tx, state, pcoinsTip.get(), true, test_flags, true, add_to_cache, txdata, nullptr);
123123
// CheckInputs should succeed iff test_flags doesn't intersect with
124124
// failing_flags
125125
bool expected_return_value = !(test_flags & failing_flags);
@@ -135,13 +135,13 @@ void ValidateCheckInputsForAllFlags(CMutableTransaction &tx, uint32_t failing_fl
135135
if (ret && add_to_cache) {
136136
// Check that we get a cache hit if the tx was valid
137137
std::vector<CScriptCheck> scriptchecks;
138-
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip, true, test_flags, true, add_to_cache, txdata, &scriptchecks));
138+
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, test_flags, true, add_to_cache, txdata, &scriptchecks));
139139
BOOST_CHECK(scriptchecks.empty());
140140
} else {
141141
// Check that we get script executions to check, if the transaction
142142
// was invalid, or we didn't add to cache.
143143
std::vector<CScriptCheck> scriptchecks;
144-
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip, true, test_flags, true, add_to_cache, txdata, &scriptchecks));
144+
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, test_flags, true, add_to_cache, txdata, &scriptchecks));
145145
BOOST_CHECK_EQUAL(scriptchecks.size(), tx.vin.size());
146146
}
147147
}
@@ -201,13 +201,13 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
201201
CValidationState state;
202202
PrecomputedTransactionData ptd_spend_tx(spend_tx);
203203

204-
BOOST_CHECK(!CheckInputs(spend_tx, state, pcoinsTip, true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, nullptr));
204+
BOOST_CHECK(!CheckInputs(spend_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, nullptr));
205205

206206
// If we call again asking for scriptchecks (as happens in
207207
// ConnectBlock), we should add a script check object for this -- we're
208208
// not caching invalidity (if that changes, delete this test case).
209209
std::vector<CScriptCheck> scriptchecks;
210-
BOOST_CHECK(CheckInputs(spend_tx, state, pcoinsTip, true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, &scriptchecks));
210+
BOOST_CHECK(CheckInputs(spend_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, &scriptchecks));
211211
BOOST_CHECK_EQUAL(scriptchecks.size(), 1);
212212

213213
// Test that CheckInputs returns true iff DERSIG-enforcing flags are
@@ -268,7 +268,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
268268
invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
269269
CValidationState state;
270270
PrecomputedTransactionData txdata(invalid_with_cltv_tx);
271-
BOOST_CHECK(CheckInputs(invalid_with_cltv_tx, state, pcoinsTip, true, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true, txdata, nullptr));
271+
BOOST_CHECK(CheckInputs(invalid_with_cltv_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true, txdata, nullptr));
272272
}
273273

274274
// TEST CHECKSEQUENCEVERIFY
@@ -296,7 +296,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
296296
invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
297297
CValidationState state;
298298
PrecomputedTransactionData txdata(invalid_with_csv_tx);
299-
BOOST_CHECK(CheckInputs(invalid_with_csv_tx, state, pcoinsTip, true, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true, txdata, nullptr));
299+
BOOST_CHECK(CheckInputs(invalid_with_csv_tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true, txdata, nullptr));
300300
}
301301

302302
// TODO: add tests for remaining script flags
@@ -358,12 +358,12 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
358358
CValidationState state;
359359
PrecomputedTransactionData txdata(tx);
360360
// This transaction is now invalid under segwit, because of the second input.
361-
BOOST_CHECK(!CheckInputs(tx, state, pcoinsTip, true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, nullptr));
361+
BOOST_CHECK(!CheckInputs(tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, nullptr));
362362

363363
std::vector<CScriptCheck> scriptchecks;
364364
// Make sure this transaction was not cached (ie because the first
365365
// input was valid)
366-
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip, true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, &scriptchecks));
366+
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, &scriptchecks));
367367
// Should get 2 script checks back -- caching is on a whole-transaction basis.
368368
BOOST_CHECK_EQUAL(scriptchecks.size(), 2);
369369
}

src/validation.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& loc
201201
return chain.Genesis();
202202
}
203203

204-
CCoinsViewDB *pcoinsdbview = nullptr;
205-
CCoinsViewCache *pcoinsTip = nullptr;
206-
CBlockTreeDB *pblocktree = nullptr;
204+
std::unique_ptr<CCoinsViewDB> pcoinsdbview;
205+
std::unique_ptr<CCoinsViewCache> pcoinsTip;
206+
std::unique_ptr<CBlockTreeDB> pblocktree;
207207

208208
enum FlushStateMode {
209209
FLUSH_STATE_NONE,
@@ -295,7 +295,7 @@ bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool
295295
}
296296
else {
297297
// pcoinsTip contains the UTXO set for chainActive.Tip()
298-
CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
298+
CCoinsViewMemPool viewMemPool(pcoinsTip.get(), mempool);
299299
std::vector<int> prevheights;
300300
prevheights.resize(tx.vin.size());
301301
for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
@@ -424,7 +424,7 @@ void UpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool f
424424
mempool.UpdateTransactionsFromBlock(vHashUpdate);
425425

426426
// We also need to remove any now-immature transactions
427-
mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
427+
mempool.removeForReorg(pcoinsTip.get(), chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
428428
// Re-limit mempool size, in case we added any transactions
429429
LimitMempoolSize(mempool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
430430
}
@@ -557,7 +557,7 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
557557
LockPoints lp;
558558
{
559559
LOCK(pool.cs);
560-
CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
560+
CCoinsViewMemPool viewMemPool(pcoinsTip.get(), pool);
561561
view.SetBackend(viewMemPool);
562562

563563
// do all inputs exist?
@@ -2105,7 +2105,7 @@ bool static DisconnectTip(CValidationState& state, const CChainParams& chainpara
21052105
// Apply the block atomically to the chain state.
21062106
int64_t nStart = GetTimeMicros();
21072107
{
2108-
CCoinsViewCache view(pcoinsTip);
2108+
CCoinsViewCache view(pcoinsTip.get());
21092109
assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
21102110
if (DisconnectBlock(block, pindexDelete, view) != DISCONNECT_OK)
21112111
return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
@@ -2235,7 +2235,7 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
22352235
int64_t nTime3;
22362236
LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * MILLI, nTimeReadFromDisk * MICRO);
22372237
{
2238-
CCoinsViewCache view(pcoinsTip);
2238+
CCoinsViewCache view(pcoinsTip.get());
22392239
bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, chainparams);
22402240
GetMainSignals().BlockChecked(blockConnecting, state);
22412241
if (!rv) {
@@ -2413,7 +2413,7 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
24132413
// any disconnected transactions back to the mempool.
24142414
UpdateMempoolForReorg(disconnectpool, true);
24152415
}
2416-
mempool.check(pcoinsTip);
2416+
mempool.check(pcoinsTip.get());
24172417

24182418
// Callbacks/notifications for a new best chain.
24192419
if (fInvalidFound)
@@ -3275,7 +3275,7 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
32753275
{
32763276
AssertLockHeld(cs_main);
32773277
assert(pindexPrev && pindexPrev == chainActive.Tip());
3278-
CCoinsViewCache viewNew(pcoinsTip);
3278+
CCoinsViewCache viewNew(pcoinsTip.get());
32793279
CBlockIndex indexDummy(block);
32803280
indexDummy.pprev = pindexPrev;
32813281
indexDummy.nHeight = pindexPrev->nHeight + 1;

src/validation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,13 @@ bool ResetBlockFailureFlags(CBlockIndex *pindex);
444444
extern CChain chainActive;
445445

446446
/** Global variable that points to the coins database (protected by cs_main) */
447-
extern CCoinsViewDB *pcoinsdbview;
447+
extern std::unique_ptr<CCoinsViewDB> pcoinsdbview;
448448

449449
/** Global variable that points to the active CCoinsView (protected by cs_main) */
450-
extern CCoinsViewCache *pcoinsTip;
450+
extern std::unique_ptr<CCoinsViewCache> pcoinsTip;
451451

452452
/** Global variable that points to the active block tree (protected by cs_main) */
453-
extern CBlockTreeDB *pblocktree;
453+
extern std::unique_ptr<CBlockTreeDB> pblocktree;
454454

455455
/**
456456
* Return the spend height, which is one more than the inputs.GetBestBlock().

0 commit comments

Comments
 (0)