aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorglozow <gloriajzhao@gmail.com>2023-08-23 15:35:26 +0100
committerglozow <gloriajzhao@gmail.com>2023-09-13 16:14:17 +0100
commit3f01a3dab1c4ee37fd4093b6a0a3b622f53e231d (patch)
tree04ce2605c1053305c307bf74bb3f16dec56bf2f5 /src
parent7d7f7a1189432b1b6245ba25df572229870567cb (diff)
downloadbitcoin-3f01a3dab1c4ee37fd4093b6a0a3b622f53e231d.tar.xz
[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.
Diffstat (limited to 'src')
-rw-r--r--src/txmempool.cpp7
-rw-r--r--src/txmempool.h12
2 files changed, 19 insertions, 0 deletions
diff --git a/src/txmempool.cpp b/src/txmempool.cpp
index 79b2b4ec94..70985dec00 100644
--- a/src/txmempool.cpp
+++ b/src/txmempool.cpp
@@ -982,6 +982,7 @@ bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
if (ptx) {
if (outpoint.n < ptx->vout.size()) {
coin = Coin(ptx->vout[outpoint.n], MEMPOOL_HEIGHT, false);
+ m_non_base_coins.emplace(outpoint);
return true;
} else {
return false;
@@ -994,8 +995,14 @@ void CCoinsViewMemPool::PackageAddTransaction(const CTransactionRef& tx)
{
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_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 {
LOCK(cs);
diff --git a/src/txmempool.h b/src/txmempool.h
index a1867eb895..4e68b711a2 100644
--- a/src/txmempool.h
+++ b/src/txmempool.h
@@ -839,15 +839,27 @@ class CCoinsViewMemPool : public CCoinsViewBacked
* validation, since we can access transaction outputs without submitting them to mempool.
*/
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:
const CTxMemPool& mempool;
public:
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;
/** 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. */
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();
};
/**