0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

[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.
This commit is contained in:
glozow 2023-08-23 15:35:26 +01:00
parent 7d7f7a1189
commit 3f01a3dab1
2 changed files with 19 additions and 0 deletions

View file

@ -982,6 +982,7 @@ bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
if (ptx) { if (ptx) {
if (outpoint.n < ptx->vout.size()) { if (outpoint.n < ptx->vout.size()) {
coin = Coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false); coin = Coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false);
m_non_base_coins.emplace(outpoint);
return true; return true;
} else { } else {
return false; return false;
@ -994,8 +995,14 @@ void CCoinsViewMemPool::PackageAddTransaction(const CTransactionRef& tx)
{ {
for (unsigned int n = 0; n < tx->vout.size(); ++n) { for (unsigned int n = 0; n < tx->vout.size(); ++n) {
m_temp_added.emplace(COutPoint(tx->GetHash(), n), Coin(tx->vout[n], MEMPOOL_HEIGHT, false)); m_temp_added.emplace(COutPoint(tx->GetHash(), n), Coin(tx->vout[n], MEMPOOL_HEIGHT, false));
m_non_base_coins.emplace(COutPoint(tx->GetHash(), n));
} }
} }
void CCoinsViewMemPool::Reset()
{
m_temp_added.clear();
m_non_base_coins.clear();
}
size_t CTxMemPool::DynamicMemoryUsage() const { size_t CTxMemPool::DynamicMemoryUsage() const {
LOCK(cs); LOCK(cs);

View file

@ -839,15 +839,27 @@ class CCoinsViewMemPool : public CCoinsViewBacked
* validation, since we can access transaction outputs without submitting them to mempool. * validation, since we can access transaction outputs without submitting them to mempool.
*/ */
std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added; std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
/**
* Set of all coins that have been fetched from mempool or created using PackageAddTransaction
* (not base). Used to track the origin of a coin, see GetNonBaseCoins().
*/
mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
protected: protected:
const CTxMemPool& mempool; const CTxMemPool& mempool;
public: public:
CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn); CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
/** GetCoin, returning whether it exists and is not spent. Also updates m_non_base_coins if the
* coin is not fetched from base. */
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
/** Add the coins created by this transaction. These coins are only temporarily stored in /** Add the coins created by this transaction. These coins are only temporarily stored in
* m_temp_added and cannot be flushed to the back end. Only used for package validation. */ * m_temp_added and cannot be flushed to the back end. Only used for package validation. */
void PackageAddTransaction(const CTransactionRef& tx); void PackageAddTransaction(const CTransactionRef& tx);
/** Get all coins in m_non_base_coins. */
std::unordered_set<COutPoint, SaltedOutpointHasher> GetNonBaseCoins() const { return m_non_base_coins; }
/** Clear m_temp_added and m_non_base_coins. */
void Reset();
}; };
/** /**