Skip to content

Commit 3f01a3d

Browse files
committed
[CCoinsViewMemPool] track non-base coins and allow Reset
Temporary coins should not be available in separate subpackage submissions. Any mempool coins that are cached in m_view should be removed whenever mempool contents change, as they may be spent or no longer exist.
1 parent 7d7f7a1 commit 3f01a3d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/txmempool.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
982982
if (ptx) {
983983
if (outpoint.n < ptx->vout.size()) {
984984
coin = Coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false);
985+
m_non_base_coins.emplace(outpoint);
985986
return true;
986987
} else {
987988
return false;
@@ -994,8 +995,14 @@ void CCoinsViewMemPool::PackageAddTransaction(const CTransactionRef& tx)
994995
{
995996
for (unsigned int n = 0; n < tx->vout.size(); ++n) {
996997
m_temp_added.emplace(COutPoint(tx->GetHash(), n), Coin(tx->vout[n], MEMPOOL_HEIGHT, false));
998+
m_non_base_coins.emplace(COutPoint(tx->GetHash(), n));
997999
}
9981000
}
1001+
void CCoinsViewMemPool::Reset()
1002+
{
1003+
m_temp_added.clear();
1004+
m_non_base_coins.clear();
1005+
}
9991006

10001007
size_t CTxMemPool::DynamicMemoryUsage() const {
10011008
LOCK(cs);

src/txmempool.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,15 +839,27 @@ class CCoinsViewMemPool : public CCoinsViewBacked
839839
* validation, since we can access transaction outputs without submitting them to mempool.
840840
*/
841841
std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
842+
843+
/**
844+
* Set of all coins that have been fetched from mempool or created using PackageAddTransaction
845+
* (not base). Used to track the origin of a coin, see GetNonBaseCoins().
846+
*/
847+
mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
842848
protected:
843849
const CTxMemPool& mempool;
844850

845851
public:
846852
CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
853+
/** GetCoin, returning whether it exists and is not spent. Also updates m_non_base_coins if the
854+
* coin is not fetched from base. */
847855
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
848856
/** Add the coins created by this transaction. These coins are only temporarily stored in
849857
* m_temp_added and cannot be flushed to the back end. Only used for package validation. */
850858
void PackageAddTransaction(const CTransactionRef& tx);
859+
/** Get all coins in m_non_base_coins. */
860+
std::unordered_set<COutPoint, SaltedOutpointHasher> GetNonBaseCoins() const { return m_non_base_coins; }
861+
/** Clear m_temp_added and m_non_base_coins. */
862+
void Reset();
851863
};
852864

853865
/**

0 commit comments

Comments
 (0)