Skip to content

Commit fae6ab6

Browse files
committed
refactor: pcoinsTip -> CChainState::CoinsTip()
This aliasing makes subsequent commits easier to review; eventually CoinsTip() will return the CCoinsViewCache managed by CChainState.
1 parent e5fdda6 commit fae6ab6

File tree

13 files changed

+70
-58
lines changed

13 files changed

+70
-58
lines changed

src/interfaces/node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class NodeImpl : public Node
232232
bool getUnspentOutput(const COutPoint& output, Coin& coin) override
233233
{
234234
LOCK(::cs_main);
235-
return ::pcoinsTip->GetCoin(output, coin);
235+
return ::ChainstateActive().CoinsTip().GetCoin(output, coin);
236236
}
237237
std::string getWalletDir() override
238238
{

src/net_processing.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,11 +1291,12 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
12911291
LOCK(g_cs_orphans);
12921292
if (mapOrphanTransactions.count(inv.hash)) return true;
12931293
}
1294+
const CCoinsViewCache& coins_cache = ::ChainstateActive().CoinsTip();
12941295

12951296
return recentRejects->contains(inv.hash) ||
12961297
mempool.exists(inv.hash) ||
1297-
pcoinsTip->HaveCoinInCache(COutPoint(inv.hash, 0)) || // Best effort: only try output 0 and 1
1298-
pcoinsTip->HaveCoinInCache(COutPoint(inv.hash, 1));
1298+
coins_cache.HaveCoinInCache(COutPoint(inv.hash, 0)) || // Best effort: only try output 0 and 1
1299+
coins_cache.HaveCoinInCache(COutPoint(inv.hash, 1));
12991300
}
13001301
case MSG_BLOCK:
13011302
case MSG_WITNESS_BLOCK:
@@ -1844,7 +1845,7 @@ void static ProcessOrphanTx(CConnman* connman, std::set<uint256>& orphan_work_se
18441845
EraseOrphanTx(orphanHash);
18451846
done = true;
18461847
}
1847-
mempool.check(pcoinsTip.get());
1848+
mempool.check(&::ChainstateActive().CoinsTip());
18481849
}
18491850
}
18501851

@@ -2497,7 +2498,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
24972498

24982499
if (!AlreadyHave(inv) &&
24992500
AcceptToMemoryPool(mempool, state, ptx, &fMissingInputs, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
2500-
mempool.check(pcoinsTip.get());
2501+
mempool.check(&::ChainstateActive().CoinsTip());
25012502
RelayTransaction(tx.GetHash(), *connman);
25022503
for (unsigned int i = 0; i < tx.vout.size(); i++) {
25032504
auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(inv.hash, i));

src/node/coin.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
void FindCoins(std::map<COutPoint, Coin>& coins)
1111
{
1212
LOCK2(cs_main, ::mempool.cs);
13-
assert(pcoinsTip);
14-
CCoinsViewCache& chain_view = *::pcoinsTip;
13+
CCoinsViewCache& chain_view = ::ChainstateActive().CoinsTip();
1514
CCoinsViewMemPool mempool_view(&chain_view, ::mempool);
1615
for (auto& coin : coins) {
1716
if (!mempool_view.GetCoin(coin.first, coin.second)) {

src/node/transaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ TransactionError BroadcastTransaction(const CTransactionRef tx, std::string& err
2525
LOCK(cs_main);
2626
// If the transaction is already confirmed in the chain, don't do anything
2727
// and return early.
28-
CCoinsViewCache &view = *pcoinsTip;
28+
CCoinsViewCache &view = ::ChainstateActive().CoinsTip();
2929
for (size_t o = 0; o < tx->vout.size(); o++) {
3030
const Coin& existingCoin = view.AccessCoin(COutPoint(hashTx, o));
3131
// IsSpent doesnt mean the coin is spent, it means the output doesnt' exist.

src/rest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,12 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
503503
if (fCheckMemPool) {
504504
// use db+mempool as cache backend in case user likes to query mempool
505505
LOCK2(cs_main, mempool.cs);
506-
CCoinsViewCache& viewChain = *pcoinsTip;
506+
CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip();
507507
CCoinsViewMemPool viewMempool(&viewChain, mempool);
508508
process_utxos(viewMempool, mempool);
509509
} else {
510510
LOCK(cs_main); // no need to lock mempool!
511-
process_utxos(*pcoinsTip, CTxMemPool());
511+
process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool());
512512
}
513513

514514
for (size_t i = 0; i < hits.size(); ++i) {

src/rpc/blockchain.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,19 +1126,21 @@ UniValue gettxout(const JSONRPCRequest& request)
11261126
fMempool = request.params[2].get_bool();
11271127

11281128
Coin coin;
1129+
CCoinsViewCache* coins_view = &::ChainstateActive().CoinsTip();
1130+
11291131
if (fMempool) {
11301132
LOCK(mempool.cs);
1131-
CCoinsViewMemPool view(pcoinsTip.get(), mempool);
1133+
CCoinsViewMemPool view(coins_view, mempool);
11321134
if (!view.GetCoin(out, coin) || mempool.isSpent(out)) {
11331135
return NullUniValue;
11341136
}
11351137
} else {
1136-
if (!pcoinsTip->GetCoin(out, coin)) {
1138+
if (!coins_view->GetCoin(out, coin)) {
11371139
return NullUniValue;
11381140
}
11391141
}
11401142

1141-
const CBlockIndex* pindex = LookupBlockIndex(pcoinsTip->GetBestBlock());
1143+
const CBlockIndex* pindex = LookupBlockIndex(coins_view->GetBestBlock());
11421144
ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
11431145
if (coin.nHeight == MEMPOOL_HEIGHT) {
11441146
ret.pushKV("confirmations", 0);
@@ -1180,7 +1182,8 @@ static UniValue verifychain(const JSONRPCRequest& request)
11801182
if (!request.params[1].isNull())
11811183
nCheckDepth = request.params[1].get_int();
11821184

1183-
return CVerifyDB().VerifyDB(Params(), pcoinsTip.get(), nCheckLevel, nCheckDepth);
1185+
return CVerifyDB().VerifyDB(
1186+
Params(), &::ChainstateActive().CoinsTip(), nCheckLevel, nCheckDepth);
11841187
}
11851188

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

src/rpc/rawtransaction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static UniValue gettxoutproof(const JSONRPCRequest& request)
259259

260260
// Loop through txids and try to find which block they're in. Exit loop once a block is found.
261261
for (const auto& tx : setTxids) {
262-
const Coin& coin = AccessByTxid(*pcoinsTip, tx);
262+
const Coin& coin = AccessByTxid(::ChainstateActive().CoinsTip(), tx);
263263
if (!coin.IsSpent()) {
264264
pblockindex = ::ChainActive()[coin.nHeight];
265265
break;
@@ -636,7 +636,7 @@ static UniValue combinerawtransaction(const JSONRPCRequest& request)
636636
{
637637
LOCK(cs_main);
638638
LOCK(mempool.cs);
639-
CCoinsViewCache &viewChain = *pcoinsTip;
639+
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
640640
CCoinsViewMemPool viewMempool(&viewChain, mempool);
641641
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
642642

@@ -1505,7 +1505,7 @@ UniValue utxoupdatepsbt(const JSONRPCRequest& request)
15051505
CCoinsViewCache view(&viewDummy);
15061506
{
15071507
LOCK2(cs_main, mempool.cs);
1508-
CCoinsViewCache &viewChain = *pcoinsTip;
1508+
CCoinsViewCache &viewChain = ::ChainstateActive().CoinsTip();
15091509
CCoinsViewMemPool viewMempool(&viewChain, mempool);
15101510
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
15111511

src/test/miner_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
372372
CBlockIndex* prev = ::ChainActive().Tip();
373373
CBlockIndex* next = new CBlockIndex();
374374
next->phashBlock = new uint256(InsecureRand256());
375-
pcoinsTip->SetBestBlock(next->GetBlockHash());
375+
::ChainstateActive().CoinsTip().SetBestBlock(next->GetBlockHash());
376376
next->pprev = prev;
377377
next->nHeight = prev->nHeight + 1;
378378
next->BuildSkip();
@@ -384,7 +384,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
384384
CBlockIndex* prev = ::ChainActive().Tip();
385385
CBlockIndex* next = new CBlockIndex();
386386
next->phashBlock = new uint256(InsecureRand256());
387-
pcoinsTip->SetBestBlock(next->GetBlockHash());
387+
::ChainstateActive().CoinsTip().SetBestBlock(next->GetBlockHash());
388388
next->pprev = prev;
389389
next->nHeight = prev->nHeight + 1;
390390
next->BuildSkip();
@@ -414,7 +414,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
414414
while (::ChainActive().Tip()->nHeight > nHeight) {
415415
CBlockIndex* del = ::ChainActive().Tip();
416416
::ChainActive().SetTip(del->pprev);
417-
pcoinsTip->SetBestBlock(del->pprev->GetBlockHash());
417+
::ChainstateActive().CoinsTip().SetBestBlock(del->pprev->GetBlockHash());
418418
delete del->phashBlock;
419419
delete del;
420420
}

src/test/txvalidationcache_tests.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
9797
BOOST_CHECK_EQUAL(mempool.size(), 0U);
9898
}
9999

100-
// Run CheckInputs (using pcoinsTip) on the given transaction, for all script
100+
// Run CheckInputs (using CoinsTip()) on the given transaction, for all script
101101
// flags. Test that CheckInputs passes for all flags that don't overlap with
102102
// the failing_flags argument, but otherwise fails.
103103
// CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY (and future NOP codes that may
@@ -125,7 +125,7 @@ static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t fail
125125
// WITNESS requires P2SH
126126
test_flags |= SCRIPT_VERIFY_P2SH;
127127
}
128-
bool ret = CheckInputs(tx, state, pcoinsTip.get(), true, test_flags, true, add_to_cache, txdata, nullptr);
128+
bool ret = CheckInputs(tx, state, &::ChainstateActive().CoinsTip(), true, test_flags, true, add_to_cache, txdata, nullptr);
129129
// CheckInputs should succeed iff test_flags doesn't intersect with
130130
// failing_flags
131131
bool expected_return_value = !(test_flags & failing_flags);
@@ -135,13 +135,13 @@ static void ValidateCheckInputsForAllFlags(const CTransaction &tx, uint32_t fail
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.get(), true, test_flags, true, add_to_cache, txdata, &scriptchecks));
138+
BOOST_CHECK(CheckInputs(tx, state, &::ChainstateActive().CoinsTip(), 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.get(), true, test_flags, true, add_to_cache, txdata, &scriptchecks));
144+
BOOST_CHECK(CheckInputs(tx, state, &::ChainstateActive().CoinsTip(), true, test_flags, true, add_to_cache, txdata, &scriptchecks));
145145
BOOST_CHECK_EQUAL(scriptchecks.size(), tx.vin.size());
146146
}
147147
}
@@ -204,13 +204,13 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
204204
CValidationState state;
205205
PrecomputedTransactionData ptd_spend_tx(spend_tx);
206206

207-
BOOST_CHECK(!CheckInputs(CTransaction(spend_tx), state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, nullptr));
207+
BOOST_CHECK(!CheckInputs(CTransaction(spend_tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, true, true, ptd_spend_tx, nullptr));
208208

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

216216
// Test that CheckInputs returns true iff DERSIG-enforcing flags are
@@ -227,7 +227,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
227227
block = CreateAndProcessBlock({spend_tx}, p2pk_scriptPubKey);
228228
LOCK(cs_main);
229229
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() == block.GetHash());
230-
BOOST_CHECK(pcoinsTip->GetBestBlock() == block.GetHash());
230+
BOOST_CHECK(::ChainstateActive().CoinsTip().GetBestBlock() == block.GetHash());
231231

232232
// Test P2SH: construct a transaction that is valid without P2SH, and
233233
// then test validity with P2SH.
@@ -272,7 +272,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
272272
invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
273273
CValidationState state;
274274
PrecomputedTransactionData txdata(invalid_with_cltv_tx);
275-
BOOST_CHECK(CheckInputs(CTransaction(invalid_with_cltv_tx), state, pcoinsTip.get(), true, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true, txdata, nullptr));
275+
BOOST_CHECK(CheckInputs(CTransaction(invalid_with_cltv_tx), state, ::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true, txdata, nullptr));
276276
}
277277

278278
// TEST CHECKSEQUENCEVERIFY
@@ -300,7 +300,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
300300
invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
301301
CValidationState state;
302302
PrecomputedTransactionData txdata(invalid_with_csv_tx);
303-
BOOST_CHECK(CheckInputs(CTransaction(invalid_with_csv_tx), state, pcoinsTip.get(), true, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true, txdata, nullptr));
303+
BOOST_CHECK(CheckInputs(CTransaction(invalid_with_csv_tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true, txdata, nullptr));
304304
}
305305

306306
// TODO: add tests for remaining script flags
@@ -362,12 +362,12 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
362362
CValidationState state;
363363
PrecomputedTransactionData txdata(tx);
364364
// This transaction is now invalid under segwit, because of the second input.
365-
BOOST_CHECK(!CheckInputs(CTransaction(tx), state, pcoinsTip.get(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, nullptr));
365+
BOOST_CHECK(!CheckInputs(CTransaction(tx), state, &::ChainstateActive().CoinsTip(), true, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true, true, txdata, nullptr));
366366

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

src/txmempool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ class CTxMemPool
497497
*
498498
* 1. Locking both `cs_main` and `mempool.cs` will give a view of mempool
499499
* that is consistent with current chain tip (`::ChainActive()` and
500-
* `pcoinsTip`) and is fully populated. Fully populated means that if the
500+
* `CoinsTip()`) and is fully populated. Fully populated means that if the
501501
* current active chain is missing transactions that were present in a
502502
* previously active chain, all the missing transactions will have been
503503
* re-added to the mempool and should be present if they meet size and

0 commit comments

Comments
 (0)