Skip to content

Commit 782f37a

Browse files
domob1812galpHub
authored andcommitted
Define CTxMemPool::lookupOutpoint.
The memory pool mainly tracks transactions by txid; however, in some places, we actually need to look up the transaction by the output hash it creates (which will be different from the txid after segwit light). This introduces a new method CTxMemPool::lookupOutpoint, which is meant to be used for these situations (even though for now it does the same as lookup). It also updates the code to use the new method where this is appropriate.
1 parent b4431a6 commit 782f37a

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

divi/src/BlockMemoryPoolTransactionCollector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ std::vector<TxPriority> BlockMemoryPoolTransactionCollector::PrioritizeMempoolTr
206206
// Read prev transaction
207207
if (!view.HaveCoins(txin.prevout.hash)) {
208208
CTransaction prevTx;
209-
if(!mempool_.lookup(txin.prevout.hash, prevTx)) {
209+
if(!mempool_.lookupOutpoint(txin.prevout.hash, prevTx)) {
210210
// This should never happen; all transactions in the memory
211211
// pool should connect to either transactions in the chain
212212
// or other transactions in the memory pool.

divi/src/rpcblockchain.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ Value getrawmempool(const Array& params, bool fHelp)
218218
info.push_back(Pair("currentpriority", e.GetPriority(chainActive.Height())));
219219
const CTransaction& tx = e.GetTx();
220220
set<string> setDepends;
221-
BOOST_FOREACH (const CTxIn& txin, tx.vin) {
222-
if (mempool.exists(txin.prevout.hash))
221+
for (const CTxIn& txin : tx.vin) {
222+
CTransaction dummyResult;
223+
if (mempool.lookupOutpoint(txin.prevout.hash, dummyResult))
223224
setDepends.insert(txin.prevout.hash.ToString());
224225
}
225226
Array depends(setDepends.begin(), setDepends.end());

divi/src/txmempool.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache* pcoins, unsigned in
638638
const CTransaction& tx = entry.second.GetTx();
639639
for (const auto& txin : tx.vin) {
640640
CTransaction tx2;
641-
if (lookup(txin.prevout.hash, tx2))
641+
if (lookupOutpoint(txin.prevout.hash, tx2))
642642
continue;
643643
const CCoins* coins = pcoins->AccessCoins(txin.prevout.hash);
644644
if (fSanityCheck) assert(coins);
@@ -723,7 +723,7 @@ void CTxMemPool::check(const CCoinsViewCache* pcoins, const BlockMap& blockIndex
723723
for (const auto& txin : tx.vin) {
724724
// Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
725725
CTransaction tx2;
726-
if (lookup(txin.prevout.hash, tx2)) {
726+
if (lookupOutpoint(txin.prevout.hash, tx2)) {
727727
assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
728728
fDependsWait = true;
729729
} else {
@@ -803,6 +803,13 @@ bool CTxMemPool::lookupBareTxid(const uint256& btxid, CTransaction& result) cons
803803
return true;
804804
}
805805

806+
bool CTxMemPool::lookupOutpoint(const uint256& hash, CTransaction& result) const
807+
{
808+
/* For now (until we add the UTXO hasher and segwit light), the outpoint
809+
is just the transaction ID. */
810+
return lookup(hash, result);
811+
}
812+
806813
CFeeRate CTxMemPool::estimateFee(int nBlocks) const
807814
{
808815
LOCK(cs);
@@ -882,7 +889,7 @@ bool CCoinsViewMemPool::GetCoins(const uint256& txid, CCoins& coins) const
882889
// conflict with the underlying cache, and it cannot have pruned entries (as it contains full)
883890
// transactions. First checking the underlying cache risks returning a pruned entry instead.
884891
CTransaction tx;
885-
if (mempool.lookup(txid, tx)) {
892+
if (mempool.lookupOutpoint(txid, tx)) {
886893
coins = CCoins(tx, MEMPOOL_HEIGHT);
887894
return true;
888895
}
@@ -891,5 +898,9 @@ bool CCoinsViewMemPool::GetCoins(const uint256& txid, CCoins& coins) const
891898

892899
bool CCoinsViewMemPool::HaveCoins(const uint256& txid) const
893900
{
894-
return mempool.exists(txid) || base->HaveCoins(txid);
901+
CTransaction dummy;
902+
if (mempool.lookupOutpoint(txid, dummy))
903+
return true;
904+
905+
return base->HaveCoins(txid);
895906
}

divi/src/txmempool.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class CTxMemPool
201201
bool lookup(const uint256& hash, CTransaction& result) const;
202202
bool lookupBareTxid(const uint256& btxid, CTransaction& result) const;
203203

204+
/** Looks up a transaction by its outpoint for spending, taking potential changes
205+
* from the raw txid (e.g. segwit light) into account. */
206+
bool lookupOutpoint(const uint256& hash, CTransaction& result) const;
207+
204208
/** Estimate fee rate needed to get into the next nBlocks */
205209
CFeeRate estimateFee(int nBlocks) const;
206210

0 commit comments

Comments
 (0)