2013-11-05 02:47:07 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 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
|
|
|
|
|
2014-10-19 04:28:43 +02:00
|
|
|
#include "compressor.h"
|
2015-07-17 19:46:18 +02:00
|
|
|
#include "core_memusage.h"
|
2016-05-06 20:47:12 +02:00
|
|
|
#include "hash.h"
|
2015-05-04 01:31:11 +02:00
|
|
|
#include "memusage.h"
|
2013-11-05 02:47:07 +01:00
|
|
|
#include "serialize.h"
|
|
|
|
#include "uint256.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
2017-04-25 01:01:44 +02:00
|
|
|
#include <unordered_map>
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
/**
|
|
|
|
* A UTXO entry.
|
2013-11-05 02:47:07 +01:00
|
|
|
*
|
|
|
|
* Serialized format:
|
2017-06-02 00:47:58 +02:00
|
|
|
* - VARINT((coinbase ? 1 : 0) | (height << 1))
|
|
|
|
* - the non-spent CTxOut (via CTxOutCompressor)
|
2013-11-05 02:47:07 +01:00
|
|
|
*/
|
2017-06-02 00:47:58 +02:00
|
|
|
class Coin
|
2013-11-05 02:47:07 +01:00
|
|
|
{
|
|
|
|
public:
|
2017-06-02 00:47:58 +02:00
|
|
|
//! unspent transaction output
|
|
|
|
CTxOut out;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
//! whether containing transaction was a coinbase
|
|
|
|
unsigned int fCoinBase : 1;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
//! at which height this containing transaction was included in the active block chain
|
|
|
|
uint32_t nHeight : 31;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-02 00:47:58 +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) {}
|
2014-09-03 09:01:24 +02:00
|
|
|
|
|
|
|
void Clear() {
|
2017-06-02 00:47:58 +02:00
|
|
|
out.SetNull();
|
2014-09-03 09:01:24 +02:00
|
|
|
fCoinBase = false;
|
|
|
|
nHeight = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
//! empty constructor
|
2017-06-02 00:47:58 +02:00
|
|
|
Coin() : fCoinBase(false), nHeight(0) { }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
|
|
|
bool IsCoinBase() const {
|
|
|
|
return fCoinBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Serialize(Stream &s) const {
|
2017-06-02 00:47:58 +02:00
|
|
|
assert(!IsSpent());
|
|
|
|
uint32_t code = nHeight * 2 + fCoinBase;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Serialize(s, VARINT(code));
|
|
|
|
::Serialize(s, CTxOutCompressor(REF(out)));
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2016-11-09 12:32:57 +01:00
|
|
|
void Unserialize(Stream &s) {
|
2017-06-02 00:47:58 +02:00
|
|
|
uint32_t code = 0;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, VARINT(code));
|
2017-06-02 00:47:58 +02:00
|
|
|
nHeight = code >> 1;
|
|
|
|
fCoinBase = code & 1;
|
2016-11-09 12:32:57 +01:00
|
|
|
::Unserialize(s, REF(CTxOutCompressor(out)));
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
bool IsSpent() const {
|
|
|
|
return out.IsNull();
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
2015-05-04 01:31:11 +02:00
|
|
|
|
|
|
|
size_t DynamicMemoryUsage() const {
|
2017-06-02 00:47:58 +02:00
|
|
|
return memusage::DynamicUsage(out.scriptPubKey);
|
2015-05-04 01:31:11 +02:00
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
class SaltedOutpointHasher
|
2014-07-09 17:25:09 +02:00
|
|
|
{
|
|
|
|
private:
|
2016-05-06 20:47:12 +02:00
|
|
|
/** Salt */
|
2017-10-12 10:13:48 +02:00
|
|
|
const uint64_t k0, k1;
|
2014-07-09 17:25:09 +02:00
|
|
|
|
|
|
|
public:
|
2017-06-02 00:47:58 +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-06-02 00:47:58 +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-06-02 00:47:58 +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).
|
2017-01-04 20:56:05 +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-06-02 00:47:58 +02:00
|
|
|
CCoinsCacheEntry() : flags(0) {}
|
|
|
|
explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
|
2014-09-03 09:37:47 +02:00
|
|
|
};
|
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> CCoinsMap;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2016-04-15 16:36:39 +02:00
|
|
|
/** Cursor for iterating over CoinsView state */
|
|
|
|
class CCoinsViewCursor
|
2013-11-05 02:47:07 +01:00
|
|
|
{
|
2016-04-15 16:36:39 +02:00
|
|
|
public:
|
|
|
|
CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
|
2017-06-02 00:47:58 +02:00
|
|
|
virtual ~CCoinsViewCursor() {}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
virtual bool GetKey(COutPoint &key) const = 0;
|
|
|
|
virtual bool GetValue(Coin &coin) const = 0;
|
2016-04-15 16:36:39 +02:00
|
|
|
virtual unsigned int GetValueSize() const = 0;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2016-04-15 16:36:39 +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-27 08:49:44 +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-06-02 00:47:58 +02:00
|
|
|
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-27 08:49:44 +02:00
|
|
|
//! Just check whether a given outpoint is unspent.
|
2017-06-02 00:47:58 +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-06-02 00:47:58 +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-04-15 16:36:39 +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-06-02 00:47:58 +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-06-02 00:47:58 +02:00
|
|
|
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
|
|
|
|
bool HaveCoin(const COutPoint &outpoint) const override;
|
|
|
|
uint256 GetBestBlock() const override;
|
2013-11-05 02:47:07 +01:00
|
|
|
void SetBackend(CCoinsView &viewIn);
|
2017-06-02 00:47:58 +02:00
|
|
|
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
|
|
|
|
CCoinsViewCursor *Cursor() const override;
|
|
|
|
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
|
|
|
|
* declared as "const".
|
|
|
|
*/
|
2014-07-19 16:42:48 +02:00
|
|
|
mutable uint256 hashBlock;
|
|
|
|
mutable CCoinsMap cacheCoins;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-02 00:47:58 +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
|
|
|
|
|
|
|
// Standard CCoinsView methods
|
2017-06-12 16:19:08 +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-12 16:19:08 +02:00
|
|
|
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
|
|
|
|
CCoinsViewCursor* Cursor() const override {
|
|
|
|
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-06-02 00:47:58 +02:00
|
|
|
* Check if we have the given utxo already loaded in this cache.
|
|
|
|
* 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-06-02 00:47:58 +02:00
|
|
|
bool HaveCoinInCache(const COutPoint &outpoint) const;
|
2015-10-23 00:49:53 +02:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
2017-06-02 00:47:58 +02:00
|
|
|
* Return a reference to Coin in the cache, or a pruned one if not found. This is
|
2017-06-21 03:18:09 +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.
|
2014-10-31 01:43:19 +01:00
|
|
|
*/
|
2017-06-02 00:47:58 +02:00
|
|
|
const Coin& AccessCoin(const COutPoint &output) const;
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
2017-06-02 00:47:58 +02:00
|
|
|
* Add a coin. Set potential_overwrite to true if a non-pruned version may
|
|
|
|
* already exist.
|
2014-10-31 01:43:19 +01:00
|
|
|
*/
|
2017-06-02 00:47:58 +02:00
|
|
|
void AddCoin(const COutPoint& outpoint, Coin&& coin, bool potential_overwrite);
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2015-11-03 03:27:15 +01:00
|
|
|
/**
|
2017-06-02 00:47:58 +02:00
|
|
|
* 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.
|
2015-11-03 03:27:15 +01:00
|
|
|
*/
|
2017-06-21 03:18:09 +02:00
|
|
|
bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
|
2015-11-03 03:27:15 +01: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-06-02 00:47:58 +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-06-02 00:47:58 +02:00
|
|
|
void Uncache(const COutPoint &outpoint);
|
2015-10-22 02:41:40 +02:00
|
|
|
|
2017-06-02 00:47:58 +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;
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
2015-04-03 00:51:08 +02:00
|
|
|
* Amount of dash coming in to a transaction
|
2014-10-31 01:43:19 +01:00
|
|
|
* Note that lightweight clients may not know anything besides the hash of previous transactions,
|
|
|
|
* so may not be able to calculate this.
|
|
|
|
*
|
|
|
|
* @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-06-02 00:47:58 +02:00
|
|
|
CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
|
2014-12-31 04:19:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache.
|
|
|
|
*/
|
|
|
|
CCoinsViewCache(const CCoinsViewCache &);
|
2013-11-05 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
//! 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.
|
2017-06-28 20:23:11 +02: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-06-02 00:47:58 +02:00
|
|
|
const Coin& AccessByTxid(const CCoinsViewCache& cache, const uint256& txid);
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_COINS_H
|