2013-11-05 02:47:07 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2014-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-05 02:47:07 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2014-08-28 22:21:03 +02:00
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
#ifndef BITCOIN_COINS_H
|
|
|
|
#define BITCOIN_COINS_H
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <compressor.h>
|
|
|
|
#include <core_memusage.h>
|
2018-08-24 23:48:23 +02:00
|
|
|
#include <crypto/siphash.h>
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <memusage.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <uint256.h>
|
2013-11-05 02:47:07 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2017-04-21 14:26:40 +02:00
|
|
|
#include <unordered_map>
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-04-25 20:29:23 +02:00
|
|
|
/**
|
|
|
|
* A UTXO entry.
|
|
|
|
*
|
|
|
|
* Serialized format:
|
|
|
|
* - VARINT((coinbase ? 1 : 0) | (height << 1))
|
|
|
|
* - the non-spent CTxOut (via CTxOutCompressor)
|
|
|
|
*/
|
|
|
|
class Coin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
//! unspent transaction output
|
|
|
|
CTxOut out;
|
|
|
|
|
2017-04-25 20:29:42 +02:00
|
|
|
//! whether containing transaction was a coinbase
|
|
|
|
unsigned int fCoinBase : 1;
|
|
|
|
|
|
|
|
//! at which height this containing transaction was included in the active block chain
|
|
|
|
uint32_t nHeight : 31;
|
2017-04-25 20:29:23 +02:00
|
|
|
|
2017-04-25 20:29:42 +02:00
|
|
|
//! construct a Coin from a CTxOut and height/coinbase information.
|
|
|
|
Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {}
|
|
|
|
Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {}
|
2017-04-25 20:29:23 +02:00
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
out.SetNull();
|
|
|
|
fCoinBase = false;
|
|
|
|
nHeight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! empty constructor
|
|
|
|
Coin() : fCoinBase(false), nHeight(0) { }
|
|
|
|
|
|
|
|
bool IsCoinBase() const {
|
|
|
|
return fCoinBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream &s) const {
|
2017-05-31 02:58:54 +02:00
|
|
|
assert(!IsSpent());
|
2017-04-25 20:29:23 +02:00
|
|
|
uint32_t code = nHeight * 2 + fCoinBase;
|
|
|
|
::Serialize(s, VARINT(code));
|
|
|
|
::Serialize(s, CTxOutCompressor(REF(out)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream &s) {
|
|
|
|
uint32_t code = 0;
|
|
|
|
::Unserialize(s, VARINT(code));
|
|
|
|
nHeight = code >> 1;
|
|
|
|
fCoinBase = code & 1;
|
2017-07-08 04:09:55 +02:00
|
|
|
::Unserialize(s, CTxOutCompressor(out));
|
2017-04-25 20:29:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 02:58:54 +02:00
|
|
|
bool IsSpent() const {
|
2017-04-25 20:29:23 +02:00
|
|
|
return out.IsNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DynamicMemoryUsage() const {
|
|
|
|
return memusage::DynamicUsage(out.scriptPubKey);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
class SaltedOutpointHasher
|
2014-07-09 17:25:09 +02:00
|
|
|
{
|
|
|
|
private:
|
2016-05-06 20:47:12 +02:00
|
|
|
/** Salt */
|
2016-06-07 16:29:03 +02:00
|
|
|
const uint64_t k0, k1;
|
2014-07-09 17:25:09 +02:00
|
|
|
|
|
|
|
public:
|
2017-04-25 20:29:39 +02:00
|
|
|
SaltedOutpointHasher();
|
2014-10-31 01:43:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This *must* return size_t. With Boost 1.46 on 32-bit systems the
|
|
|
|
* unordered_map will behave unpredictably if the custom hasher returns a
|
|
|
|
* uint64_t, resulting in failures when syncing the chain (#4634).
|
|
|
|
*/
|
2017-04-25 20:29:39 +02:00
|
|
|
size_t operator()(const COutPoint& id) const {
|
|
|
|
return SipHashUint256Extra(k0, k1, id.hash, id.n);
|
2014-07-09 17:25:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-03 09:37:47 +02:00
|
|
|
struct CCoinsCacheEntry
|
|
|
|
{
|
2017-05-04 09:15:36 +02:00
|
|
|
Coin coin; // The actual cached data.
|
2014-09-03 09:37:47 +02:00
|
|
|
unsigned char flags;
|
|
|
|
|
|
|
|
enum Flags {
|
|
|
|
DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
|
|
|
|
FRESH = (1 << 1), // The parent view does not have this entry (or it is pruned).
|
2016-11-07 21:30:41 +01:00
|
|
|
/* Note that FRESH is a performance optimization with which we can
|
|
|
|
* erase coins that are fully spent if we know we do not need to
|
|
|
|
* flush the changes to the parent cache. It is always safe to
|
|
|
|
* not mark FRESH if that condition is not guaranteed.
|
|
|
|
*/
|
2014-09-03 09:37:47 +02:00
|
|
|
};
|
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
CCoinsCacheEntry() : flags(0) {}
|
2017-05-04 09:15:36 +02:00
|
|
|
explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
|
2014-09-03 09:37:47 +02:00
|
|
|
};
|
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2016-03-28 18:18:30 +02:00
|
|
|
/** Cursor for iterating over CoinsView state */
|
|
|
|
class CCoinsViewCursor
|
2013-11-05 02:47:07 +01:00
|
|
|
{
|
2016-03-28 18:18:30 +02:00
|
|
|
public:
|
|
|
|
CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
|
2017-04-25 20:29:39 +02:00
|
|
|
virtual ~CCoinsViewCursor() {}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
virtual bool GetKey(COutPoint &key) const = 0;
|
|
|
|
virtual bool GetValue(Coin &coin) const = 0;
|
2016-03-28 18:18:30 +02:00
|
|
|
virtual unsigned int GetValueSize() const = 0;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2016-03-28 18:18:30 +02:00
|
|
|
virtual bool Valid() const = 0;
|
|
|
|
virtual void Next() = 0;
|
|
|
|
|
|
|
|
//! Get best block at the time this cursor was created
|
|
|
|
const uint256 &GetBestBlock() const { return hashBlock; }
|
|
|
|
private:
|
|
|
|
uint256 hashBlock;
|
|
|
|
};
|
2013-11-05 02:47:07 +01:00
|
|
|
|
|
|
|
/** Abstract view on the open txout dataset. */
|
|
|
|
class CCoinsView
|
|
|
|
{
|
|
|
|
public:
|
2017-06-13 21:17:30 +02:00
|
|
|
/** Retrieve the Coin (unspent transaction output) for a given outpoint.
|
|
|
|
* Returns true only when an unspent coin was found, which is returned in coin.
|
|
|
|
* When false is returned, coin's value is unspecified.
|
|
|
|
*/
|
2017-05-31 02:58:54 +02:00
|
|
|
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-13 21:17:30 +02:00
|
|
|
//! Just check whether a given outpoint is unspent.
|
2017-05-31 02:58:54 +02:00
|
|
|
virtual bool HaveCoin(const COutPoint &outpoint) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Retrieve the block hash whose state this CCoinsView currently represents
|
2014-07-19 16:42:48 +02:00
|
|
|
virtual uint256 GetBestBlock() const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-04-19 18:34:30 +02:00
|
|
|
//! Retrieve the range of blocks that may have been only partially written.
|
|
|
|
//! If the database is in a consistent state, the result is the empty vector.
|
|
|
|
//! Otherwise, a two-element vector is returned consisting of the new and
|
|
|
|
//! the old block hash, in that order.
|
|
|
|
virtual std::vector<uint256> GetHeadBlocks() const;
|
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
//! Do a bulk modification (multiple Coin changes + BestBlock change).
|
2014-10-31 01:43:19 +01:00
|
|
|
//! The passed mapCoins can be modified.
|
2014-08-24 02:08:05 +02:00
|
|
|
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2016-03-28 18:18:30 +02:00
|
|
|
//! Get a cursor to iterate over the whole state
|
|
|
|
virtual CCoinsViewCursor *Cursor() const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! As we use CCoinsViews polymorphically, have a virtual destructor
|
2013-11-05 02:47:07 +01:00
|
|
|
virtual ~CCoinsView() {}
|
2017-05-13 00:19:19 +02:00
|
|
|
|
|
|
|
//! Estimate database size (0 if not implemented)
|
|
|
|
virtual size_t EstimateSize() const { return 0; }
|
2013-11-05 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** CCoinsView backed by another CCoinsView */
|
|
|
|
class CCoinsViewBacked : public CCoinsView
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
CCoinsView *base;
|
|
|
|
|
|
|
|
public:
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewBacked(CCoinsView *viewIn);
|
2017-05-31 02:58:54 +02:00
|
|
|
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
|
|
|
|
bool HaveCoin(const COutPoint &outpoint) const override;
|
2017-04-25 20:29:39 +02:00
|
|
|
uint256 GetBestBlock() const override;
|
2017-04-19 18:34:30 +02:00
|
|
|
std::vector<uint256> GetHeadBlocks() const override;
|
2013-11-05 02:47:07 +01:00
|
|
|
void SetBackend(CCoinsView &viewIn);
|
2017-04-25 20:29:39 +02:00
|
|
|
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
|
|
|
|
CCoinsViewCursor *Cursor() const override;
|
2017-05-13 00:19:19 +02:00
|
|
|
size_t EstimateSize() const override;
|
2013-11-05 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** CCoinsView that adds a memory cache for transactions to another CCoinsView */
|
|
|
|
class CCoinsViewCache : public CCoinsViewBacked
|
|
|
|
{
|
|
|
|
protected:
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* Make mutable so that we can "fill the cache" even from Get-methods
|
2018-07-24 17:59:49 +02:00
|
|
|
* declared as "const".
|
2014-10-31 01:43:19 +01:00
|
|
|
*/
|
2014-07-19 16:42:48 +02:00
|
|
|
mutable uint256 hashBlock;
|
|
|
|
mutable CCoinsMap cacheCoins;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
/* Cached dynamic memory usage for the inner Coin objects. */
|
2015-05-04 01:31:11 +02:00
|
|
|
mutable size_t cachedCoinsUsage;
|
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
public:
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewCache(CCoinsView *baseIn);
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-09-16 12:06:05 +02:00
|
|
|
/**
|
|
|
|
* By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache on top of a base cache.
|
|
|
|
*/
|
|
|
|
CCoinsViewCache(const CCoinsViewCache &) = delete;
|
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
// Standard CCoinsView methods
|
2017-06-08 15:28:28 +02:00
|
|
|
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
|
|
|
|
bool HaveCoin(const COutPoint &outpoint) const override;
|
|
|
|
uint256 GetBestBlock() const override;
|
2014-09-03 09:25:32 +02:00
|
|
|
void SetBestBlock(const uint256 &hashBlock);
|
2017-06-08 15:28:28 +02:00
|
|
|
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
|
|
|
|
CCoinsViewCursor* Cursor() const override {
|
2017-06-07 19:00:11 +02:00
|
|
|
throw std::logic_error("CCoinsViewCache cursor iteration not supported.");
|
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2015-10-23 00:49:53 +02:00
|
|
|
/**
|
2017-04-25 20:29:39 +02:00
|
|
|
* Check if we have the given utxo already loaded in this cache.
|
2017-05-31 02:58:54 +02:00
|
|
|
* The semantics are the same as HaveCoin(), but no calls to
|
2015-10-23 00:49:53 +02:00
|
|
|
* the backing CCoinsView are made.
|
|
|
|
*/
|
2017-05-31 02:58:54 +02:00
|
|
|
bool HaveCoinInCache(const COutPoint &outpoint) const;
|
2014-09-02 21:21:15 +02:00
|
|
|
|
2017-04-25 20:29:29 +02:00
|
|
|
/**
|
2017-04-25 20:29:39 +02:00
|
|
|
* Return a reference to Coin in the cache, or a pruned one if not found. This is
|
2017-06-06 02:28:08 +02:00
|
|
|
* more efficient than GetCoin.
|
|
|
|
*
|
|
|
|
* Generally, do not hold the reference returned for more than a short scope.
|
|
|
|
* While the current implementation allows for modifications to the contents
|
|
|
|
* of the cache while holding the reference, this behavior should not be relied
|
|
|
|
* on! To be safe, best to not hold the returned reference through any other
|
|
|
|
* calls to this cache.
|
2017-04-25 20:29:29 +02:00
|
|
|
*/
|
2017-04-25 20:29:39 +02:00
|
|
|
const Coin& AccessCoin(const COutPoint &output) const;
|
2017-04-25 20:29:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-06-05 17:50:47 +02:00
|
|
|
bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
|
2017-04-25 20:29:29 +02:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-11-05 02:47:07 +01:00
|
|
|
bool Flush();
|
|
|
|
|
2015-10-22 02:41:40 +02:00
|
|
|
/**
|
2017-04-25 20:29:39 +02:00
|
|
|
* Removes the UTXO with the given outpoint from the cache, if it is
|
2015-10-22 02:41:40 +02:00
|
|
|
* not modified.
|
|
|
|
*/
|
2017-04-25 20:29:39 +02:00
|
|
|
void Uncache(const COutPoint &outpoint);
|
2015-10-22 02:41:40 +02:00
|
|
|
|
2017-04-25 20:29:39 +02:00
|
|
|
//! Calculate the size of the cache (in number of transaction outputs)
|
2014-07-19 16:42:48 +02:00
|
|
|
unsigned int GetCacheSize() const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2015-05-04 01:31:11 +02:00
|
|
|
//! Calculate the size of the cache (in bytes)
|
|
|
|
size_t DynamicMemoryUsage() const;
|
|
|
|
|
2018-07-24 17:59:49 +02:00
|
|
|
/**
|
2014-10-31 01:43:19 +01:00
|
|
|
* Amount of bitcoins coming in to a transaction
|
|
|
|
* Note that lightweight clients may not know anything besides the hash of previous transactions,
|
|
|
|
* so may not be able to calculate this.
|
|
|
|
*
|
2018-08-20 14:19:43 +02:00
|
|
|
* @param[in] tx transaction for which we are checking input total
|
|
|
|
* @return Sum of value of all inputs (scriptSigs)
|
2013-11-05 02:47:07 +01:00
|
|
|
*/
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount GetValueIn(const CTransaction& tx) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
|
2014-07-19 16:42:48 +02:00
|
|
|
bool HaveInputs(const CTransaction& tx) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
|
|
|
private:
|
2017-05-31 02:58:54 +02:00
|
|
|
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
2017-04-25 20:29:29 +02:00
|
|
|
//! Utility function to add all of a transaction's outputs to a cache.
|
2019-01-25 04:44:54 +01:00
|
|
|
//! When check is false, this assumes that overwrites are only possible for coinbase transactions.
|
|
|
|
//! When check is true, the underlying view may be queried to determine whether an addition is
|
|
|
|
//! an overwrite.
|
2017-04-25 20:29:29 +02:00
|
|
|
// TODO: pass in a boolean to limit these possible overwrites to known
|
|
|
|
// (pre-BIP34) cases.
|
2017-04-19 18:34:30 +02:00
|
|
|
void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, bool check = false);
|
2017-04-25 20:29:29 +02:00
|
|
|
|
|
|
|
//! Utility function to find any unspent output with a given txid.
|
2019-01-25 04:44:54 +01:00
|
|
|
//! This function can be quite expensive because in the event of a transaction
|
|
|
|
//! which is not found in the cache, it can cause up to MAX_OUTPUTS_PER_BLOCK
|
|
|
|
//! lookups to database, so it should be used with care.
|
2017-04-25 20:29:39 +02:00
|
|
|
const Coin& AccessByTxid(const CCoinsViewCache& cache, const uint256& txid);
|
2017-04-25 20:29:29 +02:00
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_COINS_H
|