6 #ifndef BITCOIN_COINS_H
7 #define BITCOIN_COINS_H
19 #include <boost/foreach.hpp>
20 #include <boost/unordered_map.hpp>
105 std::vector<CTxOut>().swap(
vout);
115 while (
vout.size() > 0 &&
vout.back().IsNull())
118 std::vector<CTxOut>().swap(
vout);
150 void CalcMaskSize(
unsigned int &nBytes,
unsigned int &nNonzeroBytes)
const;
156 template<
typename Stream>
158 unsigned int nMaskSize = 0, nMaskCode = 0;
160 bool fFirst =
vout.size() > 0 && !
vout[0].IsNull();
161 bool fSecond =
vout.size() > 1 && !
vout[1].IsNull();
162 assert(fFirst || fSecond || nMaskCode);
163 unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (
fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
169 for (
unsigned int b = 0; b<nMaskSize; b++) {
170 unsigned char chAvail = 0;
171 for (
unsigned int i = 0; i < 8 && 2+b*8+i <
vout.size(); i++)
172 if (!
vout[2+b*8+i].IsNull())
177 for (
unsigned int i = 0; i <
vout.size(); i++) {
178 if (!
vout[i].IsNull())
185 template<
typename Stream>
187 unsigned int nCode = 0;
193 std::vector<bool> vAvail(2,
false);
194 vAvail[0] = (nCode & 2) != 0;
195 vAvail[1] = (nCode & 4) != 0;
196 unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
198 while (nMaskCode > 0) {
199 unsigned char chAvail = 0;
201 for (
unsigned int p = 0; p < 8; p++) {
202 bool f = (chAvail & (1 << p)) != 0;
210 for (
unsigned int i = 0; i < vAvail.size(); i++) {
220 bool Spend(uint32_t nPos);
224 return (nPos <
vout.size() && !
vout[nPos].IsNull());
237 size_t ret = memusage::DynamicUsage(
vout);
282 typedef boost::unordered_map<uint256, CCoinsCacheEntry, SaltedTxidHasher>
CCoinsMap;
359 CCoinsMap::iterator
it;
int64_t CAmount
Amount in satoshis (Can be negative)
Pruned version of CTransaction: only retains metadata and unspent transaction outputs.
bool Spend(uint32_t nPos)
mark a vout spent
std::vector< CTxOut > vout
unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are d...
CCoins(const CTransaction &tx, int nHeightIn)
construct a CCoins from a CTransaction, at a given height
friend bool operator!=(const CCoins &a, const CCoins &b)
CCoins()
empty constructor
bool IsPruned() const
check whether the entire CCoins is spent note that only !IsPruned() CCoins can be serialized
friend bool operator==(const CCoins &a, const CCoins &b)
equality test
void Cleanup()
remove spent outputs at the end of vout
void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const
calculate number of bytes for the bitmask, and its number of non-zero bytes each bit in the bitmask r...
size_t DynamicMemoryUsage() const
int nVersion
version of the CTransaction; accesses to this value should probably check for nHeight as well,...
bool IsAvailable(unsigned int nPos) const
check whether a particular output is still available
void FromTx(const CTransaction &tx, int nHeightIn)
void Serialize(Stream &s) const
bool fCoinBase
whether transaction is a coinbase
void Unserialize(Stream &s)
int nHeight
at which height this transaction was included in the active block chain
A reference to a mutable cache entry.
CCoinsModifier(CCoinsViewCache &cache_, CCoinsMap::iterator it_, size_t usage)
CCoinsView backed by another CCoinsView.
CCoinsViewCursor * Cursor() const
Get a cursor to iterate over the whole state.
uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
void SetBackend(CCoinsView &viewIn)
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple CCoins changes + BestBlock change).
bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
CCoinsViewBacked(CCoinsView *viewIn)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
CAmount GetValueIn(const CTransaction &tx) const
Amount of bitcoins coming in to a transaction Note that lightweight clients may not know anything bes...
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
uint256 hashBlock
Make mutable so that we can "fill the cache" even from Get-methods declared as "const".
CCoinsViewCache(CCoinsView *baseIn)
const CTxOut & GetOutputFor(const CTxIn &input) const
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple CCoins changes + BestBlock change).
bool HaveInputs(const CTransaction &tx) const
Check whether all prevouts of the transaction are present in the UTXO set represented by this view.
bool HaveCoinsInCache(const uint256 &txid) const
Check if we have the given tx already loaded in this cache.
const CCoins * AccessCoins(const uint256 &txid) const
Return a pointer to CCoins in the cache, or NULL if not found.
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transactions)
CCoinsMap::const_iterator FetchCoins(const uint256 &txid) const
double GetPriority(const CTransaction &tx, int nHeight, CAmount &inChainInputValue) const
Return priority of tx at height nHeight.
void SetBestBlock(const uint256 &hashBlock)
bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
void Uncache(const uint256 &txid)
Removes the transaction with the given hash from the cache, if it is not modified.
CCoinsModifier ModifyCoins(const uint256 &txid)
Return a modifiable reference to a CCoins.
bool Flush()
Push the modifications applied to this cache to its base.
CCoinsModifier ModifyNewCoins(const uint256 &txid, bool coinbase)
Return a modifiable reference to a CCoins.
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
CCoinsViewCache(const CCoinsViewCache &)
By making the copy constructor private, we prevent accidentally using it when one intends to create a...
Cursor for iterating over CoinsView state.
virtual bool Valid() const =0
CCoinsViewCursor(const uint256 &hashBlockIn)
virtual bool GetKey(uint256 &key) const =0
virtual unsigned int GetValueSize() const =0
virtual bool GetValue(CCoins &coins) const =0
const uint256 & GetBestBlock() const
Get best block at the time this cursor was created.
virtual ~CCoinsViewCursor()
Abstract view on the open txout dataset.
virtual CCoinsViewCursor * Cursor() const
Get a cursor to iterate over the whole state.
virtual bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
virtual ~CCoinsView()
As we use CCoinsViews polymorphically, have a virtual destructor.
virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple CCoins changes + BestBlock change).
virtual bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
An input of a transaction.
wrapper for CTxOut that provides a more compact serialization
An output of a transaction.
size_t operator()(const uint256 &txid) const
This must return size_t.
boost::unordered_map< uint256, CCoinsCacheEntry, SaltedTxidHasher > CCoinsMap
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...