Changeset View
Changeset View
Standalone View
Standalone View
src/coins.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_COINS_H | #ifndef BITCOIN_COINS_H | ||||
#define BITCOIN_COINS_H | #define BITCOIN_COINS_H | ||||
#include "compressor.h" | #include "compressor.h" | ||||
#include "core_memusage.h" | #include "core_memusage.h" | ||||
#include "hash.h" | #include "hash.h" | ||||
#include "memusage.h" | #include "memusage.h" | ||||
#include "serialize.h" | #include "serialize.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include "utxocommit.h" | |||||
#include <cassert> | #include <cassert> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <unordered_map> | #include <unordered_map> | ||||
class CUtxoCommit; | |||||
/** | /** | ||||
* A UTXO entry. | * A UTXO entry. | ||||
* | * | ||||
* Serialized format: | * Serialized format: | ||||
* - VARINT((coinbase ? 1 : 0) | (height << 1)) | * - VARINT((coinbase ? 1 : 0) | (height << 1)) | ||||
* - the non-spent CTxOut (via CTxOutCompressor) | * - the non-spent CTxOut (via CTxOutCompressor) | ||||
*/ | */ | ||||
class Coin { | class Coin { | ||||
Show All 21 Lines | public: | ||||
const CTxOut &GetTxOut() const { return out; } | const CTxOut &GetTxOut() const { return out; } | ||||
void Clear() { | void Clear() { | ||||
out.SetNull(); | out.SetNull(); | ||||
nHeightAndIsCoinBase = 0; | nHeightAndIsCoinBase = 0; | ||||
} | } | ||||
template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Serialize(Stream &s) const { | ||||
assert(!IsSpent()); | |||||
::Serialize(s, VARINT(nHeightAndIsCoinBase)); | ::Serialize(s, VARINT(nHeightAndIsCoinBase)); | ||||
// only compress for disk format | // only compress for disk format | ||||
if (s.GetType() & SER_DISK) { | if (s.GetType() & SER_DISK) { | ||||
assert(!IsSpent()); | |||||
::Serialize(s, CTxOutCompressor(REF(out))); | ::Serialize(s, CTxOutCompressor(REF(out))); | ||||
} else { | } else { | ||||
::Serialize(s, REF(out)); | ::Serialize(s, REF(out)); | ||||
} | } | ||||
} | } | ||||
template <typename Stream> void Unserialize(Stream &s) { | template <typename Stream> void Unserialize(Stream &s) { | ||||
::Unserialize(s, VARINT(nHeightAndIsCoinBase)); | ::Unserialize(s, VARINT(nHeightAndIsCoinBase)); | ||||
Show All 34 Lines | struct CCoinsCacheEntry { | ||||
// The actual cached data. | // The actual cached data. | ||||
Coin coin; | Coin coin; | ||||
uint8_t flags; | uint8_t flags; | ||||
enum Flags { | enum Flags { | ||||
// This cache entry is potentially different from the version in the | // This cache entry is potentially different from the version in the | ||||
// parent view. | // parent view. | ||||
DIRTY = (1 << 0), | DIRTY = (1 << 0), | ||||
// The parent view does not have this entry (or it is pruned). | // Neither the commitDelta nor the parent view does not have this entry | ||||
// (or it is pruned) | |||||
FRESH = (1 << 1), | FRESH = (1 << 1), | ||||
/* Note that FRESH is a performance optimization with which we can erase | /* 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 | 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 | changes to the parent cache. It is always safe to not mark FRESH if | ||||
that condition is not guaranteed. */ | that condition is not guaranteed. */ | ||||
// The commitDelta is not yet updated with this entry | |||||
UNCOMMITED = (1 << 2), | |||||
}; | }; | ||||
CCoinsCacheEntry() : flags(0) {} | CCoinsCacheEntry() : flags(0) {} | ||||
explicit CCoinsCacheEntry(Coin coinIn) | explicit CCoinsCacheEntry(Coin coinIn) | ||||
: coin(std::move(coinIn)), flags(0) {} | : coin(std::move(coinIn)), flags(0) {} | ||||
}; | }; | ||||
typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> | typedef std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher> | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | public: | ||||
//! Get a cursor to iterate over the whole state | //! Get a cursor to iterate over the whole state | ||||
virtual CCoinsViewCursor *Cursor() const; | virtual CCoinsViewCursor *Cursor() const; | ||||
//! As we use CCoinsViews polymorphically, have a virtual destructor | //! As we use CCoinsViews polymorphically, have a virtual destructor | ||||
virtual ~CCoinsView() {} | virtual ~CCoinsView() {} | ||||
//! Estimate database size (0 if not implemented) | //! Estimate database size (0 if not implemented) | ||||
virtual size_t EstimateSize() const { return 0; } | virtual size_t EstimateSize() const { return 0; } | ||||
virtual CUtxoCommit GetCommitment(); | |||||
//! Updates the commitment with pending UNCOMMITTED changes | |||||
virtual void UpdateCommitment(CCoinsMap &mapCoins); | |||||
}; | }; | ||||
/** CCoinsView backed by another CCoinsView */ | /** CCoinsView backed by another CCoinsView */ | ||||
class CCoinsViewBacked : public CCoinsView { | class CCoinsViewBacked : public CCoinsView { | ||||
protected: | protected: | ||||
CCoinsView *base; | CCoinsView *base; | ||||
public: | public: | ||||
CCoinsViewBacked(CCoinsView *viewIn); | CCoinsViewBacked(CCoinsView *viewIn); | ||||
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; | bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; | ||||
bool HaveCoin(const COutPoint &outpoint) const override; | bool HaveCoin(const COutPoint &outpoint) const override; | ||||
uint256 GetBestBlock() const override; | uint256 GetBestBlock() const override; | ||||
std::vector<uint256> GetHeadBlocks() const override; | std::vector<uint256> GetHeadBlocks() const override; | ||||
void SetBackend(CCoinsView &viewIn); | void SetBackend(CCoinsView &viewIn); | ||||
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; | bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; | ||||
CCoinsViewCursor *Cursor() const override; | CCoinsViewCursor *Cursor() const override; | ||||
size_t EstimateSize() const override; | size_t EstimateSize() const override; | ||||
CUtxoCommit GetCommitment() override; | |||||
}; | }; | ||||
/** | /** | ||||
* CCoinsView that adds a memory cache for transactions to another CCoinsView | * CCoinsView that adds a memory cache for transactions to another CCoinsView | ||||
*/ | */ | ||||
class CCoinsViewCache : public CCoinsViewBacked { | class CCoinsViewCache : public CCoinsViewBacked { | ||||
protected: | protected: | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | public: | ||||
* Push the modifications applied to this cache to its base. | * Push the modifications applied to this cache to its base. | ||||
* Failure to call this method before destruction will cause the changes to | * 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 | * be forgotten. If false is returned, the state of this cache (and its | ||||
* backing view) will be undefined. | * backing view) will be undefined. | ||||
*/ | */ | ||||
bool Flush(); | bool Flush(); | ||||
/** | /** | ||||
* Returns the UTXO commitment from the current full set | |||||
* This is the combination of the backed commitment and the commitment delta | |||||
*/ | |||||
CUtxoCommit GetCommitment(); | |||||
void UpdateCommitment(CCoinsMap &mapCoins); | |||||
/** | |||||
* Removes the UTXO with the given outpoint from the cache, if it is not | * Removes the UTXO with the given outpoint from the cache, if it is not | ||||
* modified. | * modified. | ||||
*/ | */ | ||||
void Uncache(const COutPoint &outpoint); | void Uncache(const COutPoint &outpoint); | ||||
//! Calculate the size of the cache (in number of transaction outputs) | //! Calculate the size of the cache (in number of transaction outputs) | ||||
unsigned int GetCacheSize() const; | unsigned int GetCacheSize() const; | ||||
▲ Show 20 Lines • Show All 51 Lines • Show Last 20 Lines |