aboutsummaryrefslogtreecommitdiff
path: root/src/coins.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-04-25 11:29:29 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2017-06-01 11:56:06 -0700
commit000391132608343c66488d62625c714814959bc9 (patch)
tree0d50712acb21403a32a3a013e54bdedc2b28aeb8 /src/coins.h
parentbd83111a0fcfdb97204a0180bcf861d3b53bb6c2 (diff)
downloadbitcoin-000391132608343c66488d62625c714814959bc9.tar.xz
Introduce new per-txout CCoinsViewCache functions
The new functions are: * CCoinsViewCache::AddCoin: Add a single COutPoint/Coin pair. * CCoinsViewCache::SpendCoin: Remove a single COutPoint. * AddCoins: utility function that invokes CCoinsViewCache::AddCoin for each output in a CTransaction. * AccessByTxid: utility function that searches for any output with a given txid. * CCoinsViewCache::AccessCoin: retrieve the Coin for a COutPoint. * CCoinsViewCache::HaveCoins: check whether a non-empty Coin exists for a given COutPoint. The AddCoin and SpendCoin methods will eventually replace ModifyCoins and ModifyNewCoins, AddCoins will replace CCoins::FromTx, and the new AccessCoins and HaveCoins functions will replace their per-txid counterparts. Note that AccessCoin for now returns a copy of the Coin object. In a later commit it will be change to returning a const reference (which keeps working in all call sites).
Diffstat (limited to 'src/coins.h')
-rw-r--r--src/coins.h33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/coins.h b/src/coins.h
index 69c24ab45a..5879530f95 100644
--- a/src/coins.h
+++ b/src/coins.h
@@ -452,6 +452,7 @@ public:
// Standard CCoinsView methods
bool GetCoins(const uint256 &txid, CCoins &coins) const;
bool HaveCoins(const uint256 &txid) const;
+ bool HaveCoins(const COutPoint &outpoint) const;
uint256 GetBestBlock() const;
void SetBestBlock(const uint256 &hashBlock);
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
@@ -471,6 +472,14 @@ public:
const CCoins* AccessCoins(const uint256 &txid) const;
/**
+ * Return a copy of a Coin in the cache, or a pruned one if not found. This is
+ * more efficient than GetCoins. Modifications to other cache entries are
+ * allowed while accessing the returned pointer.
+ * TODO: return a reference to a Coin after changing CCoinsViewCache storage.
+ */
+ const Coin AccessCoin(const COutPoint &output) const;
+
+ /**
* Return a modifiable reference to a CCoins. If no entry with the given
* txid exists, a new one is created. Simultaneous modifications are not
* allowed.
@@ -489,6 +498,19 @@ public:
CCoinsModifier ModifyNewCoins(const uint256 &txid, bool coinbase);
/**
+ * Add a coin. Set potential_overwrite to true if a non-pruned version may
+ * already exist.
+ */
+ void AddCoin(const COutPoint& outpoint, Coin&& coin, bool potential_overwrite);
+
+ /**
+ * Spend a coin. Pass moveto in order to get the deleted data.
+ * If no unspent output exists for the passed outpoint, this call
+ * has no effect.
+ */
+ void SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
+
+ /**
* Push the modifications applied to this cache to its base.
* Failure to call this method before destruction will cause the changes to be forgotten.
* If false is returned, the state of this cache (and its backing view) will be undefined.
@@ -525,7 +547,7 @@ public:
friend class CCoinsModifier;
private:
- CCoinsMap::const_iterator FetchCoins(const uint256 &txid) const;
+ CCoinsMap::iterator FetchCoins(const uint256 &txid) const;
/**
* By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache.
@@ -533,4 +555,13 @@ private:
CCoinsViewCache(const CCoinsViewCache &);
};
+//! Utility function to add all of a transaction's outputs to a cache.
+// It assumes that overwrites are only possible for coinbase transactions,
+// TODO: pass in a boolean to limit these possible overwrites to known
+// (pre-BIP34) cases.
+void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight);
+
+//! Utility function to find any unspent output with a given txid.
+const Coin AccessByTxid(const CCoinsViewCache& cache, const uint256& txid);
+
#endif // BITCOIN_COINS_H