Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
coins.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_COINS_H
6 #define BITCOIN_COINS_H
7 
8 #include "core.h"
9 #include "serialize.h"
10 #include "uint256.h"
11 
12 #include <assert.h>
13 #include <stdint.h>
14 
15 #include <boost/foreach.hpp>
16 
68 class CCoins
69 {
70 public:
71  // whether transaction is a coinbase
72  bool fCoinBase;
73 
74  // unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
75  std::vector<CTxOut> vout;
76 
77  // at which height this transaction was included in the active block chain
78  int nHeight;
79 
80  // version of the CTransaction; accesses to this value should probably check for nHeight as well,
81  // as new tx version will probably only be introduced at certain heights
82  int nVersion;
83 
84  // construct a CCoins from a CTransaction, at a given height
85  CCoins(const CTransaction &tx, int nHeightIn) : fCoinBase(tx.IsCoinBase()), vout(tx.vout), nHeight(nHeightIn), nVersion(tx.nVersion) {
87  }
88 
89  // empty constructor
90  CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
91 
92  // remove spent outputs at the end of vout
93  void Cleanup() {
94  while (vout.size() > 0 && vout.back().IsNull())
95  vout.pop_back();
96  if (vout.empty())
97  std::vector<CTxOut>().swap(vout);
98  }
99 
101  BOOST_FOREACH(CTxOut &txout, vout) {
102  if (txout.scriptPubKey.IsUnspendable())
103  txout.SetNull();
104  }
105  Cleanup();
106  }
107 
108  void swap(CCoins &to) {
109  std::swap(to.fCoinBase, fCoinBase);
110  to.vout.swap(vout);
111  std::swap(to.nHeight, nHeight);
112  std::swap(to.nVersion, nVersion);
113  }
114 
115  // equality test
116  friend bool operator==(const CCoins &a, const CCoins &b) {
117  // Empty CCoins objects are always equal.
118  if (a.IsPruned() && b.IsPruned())
119  return true;
120  return a.fCoinBase == b.fCoinBase &&
121  a.nHeight == b.nHeight &&
122  a.nVersion == b.nVersion &&
123  a.vout == b.vout;
124  }
125  friend bool operator!=(const CCoins &a, const CCoins &b) {
126  return !(a == b);
127  }
128 
129  void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const;
130 
131  bool IsCoinBase() const {
132  return fCoinBase;
133  }
134 
135  unsigned int GetSerializeSize(int nType, int nVersion) const {
136  unsigned int nSize = 0;
137  unsigned int nMaskSize = 0, nMaskCode = 0;
138  CalcMaskSize(nMaskSize, nMaskCode);
139  bool fFirst = vout.size() > 0 && !vout[0].IsNull();
140  bool fSecond = vout.size() > 1 && !vout[1].IsNull();
141  assert(fFirst || fSecond || nMaskCode);
142  unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
143  // version
144  nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
145  // size of header code
146  nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
147  // spentness bitmask
148  nSize += nMaskSize;
149  // txouts themself
150  for (unsigned int i = 0; i < vout.size(); i++)
151  if (!vout[i].IsNull())
152  nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
153  // height
154  nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
155  return nSize;
156  }
157 
158  template<typename Stream>
159  void Serialize(Stream &s, int nType, int nVersion) const {
160  unsigned int nMaskSize = 0, nMaskCode = 0;
161  CalcMaskSize(nMaskSize, nMaskCode);
162  bool fFirst = vout.size() > 0 && !vout[0].IsNull();
163  bool fSecond = vout.size() > 1 && !vout[1].IsNull();
164  assert(fFirst || fSecond || nMaskCode);
165  unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
166  // version
167  ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
168  // header code
169  ::Serialize(s, VARINT(nCode), nType, nVersion);
170  // spentness bitmask
171  for (unsigned int b = 0; b<nMaskSize; b++) {
172  unsigned char chAvail = 0;
173  for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
174  if (!vout[2+b*8+i].IsNull())
175  chAvail |= (1 << i);
176  ::Serialize(s, chAvail, nType, nVersion);
177  }
178  // txouts themself
179  for (unsigned int i = 0; i < vout.size(); i++) {
180  if (!vout[i].IsNull())
181  ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
182  }
183  // coinbase height
184  ::Serialize(s, VARINT(nHeight), nType, nVersion);
185  }
186 
187  template<typename Stream>
188  void Unserialize(Stream &s, int nType, int nVersion) {
189  unsigned int nCode = 0;
190  // version
191  ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
192  // header code
193  ::Unserialize(s, VARINT(nCode), nType, nVersion);
194  fCoinBase = nCode & 1;
195  std::vector<bool> vAvail(2, false);
196  vAvail[0] = nCode & 2;
197  vAvail[1] = nCode & 4;
198  unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
199  // spentness bitmask
200  while (nMaskCode > 0) {
201  unsigned char chAvail = 0;
202  ::Unserialize(s, chAvail, nType, nVersion);
203  for (unsigned int p = 0; p < 8; p++) {
204  bool f = (chAvail & (1 << p)) != 0;
205  vAvail.push_back(f);
206  }
207  if (chAvail != 0)
208  nMaskCode--;
209  }
210  // txouts themself
211  vout.assign(vAvail.size(), CTxOut());
212  for (unsigned int i = 0; i < vAvail.size(); i++) {
213  if (vAvail[i])
214  ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion);
215  }
216  // coinbase height
217  ::Unserialize(s, VARINT(nHeight), nType, nVersion);
218  Cleanup();
219  }
220 
221  // mark an outpoint spent, and construct undo information
222  bool Spend(const COutPoint &out, CTxInUndo &undo);
223 
224  // mark a vout spent
225  bool Spend(int nPos);
226 
227  // check whether a particular output is still available
228  bool IsAvailable(unsigned int nPos) const {
229  return (nPos < vout.size() && !vout[nPos].IsNull());
230  }
231 
232  // check whether the entire CCoins is spent
233  // note that only !IsPruned() CCoins can be serialized
234  bool IsPruned() const {
235  BOOST_FOREACH(const CTxOut &out, vout)
236  if (!out.IsNull())
237  return false;
238  return true;
239  }
240 };
241 
242 
244 {
245  int nHeight;
247  uint64_t nTransactions;
249  uint64_t nSerializedSize;
251  int64_t nTotalAmount;
252 
253  CCoinsStats() : nHeight(0), hashBlock(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), hashSerialized(0), nTotalAmount(0) {}
254 };
255 
256 
259 {
260 public:
261  // Retrieve the CCoins (unspent transaction outputs) for a given txid
262  virtual bool GetCoins(const uint256 &txid, CCoins &coins);
263 
264  // Modify the CCoins for a given txid
265  virtual bool SetCoins(const uint256 &txid, const CCoins &coins);
266 
267  // Just check whether we have data for a given txid.
268  // This may (but cannot always) return true for fully spent transactions
269  virtual bool HaveCoins(const uint256 &txid);
270 
271  // Retrieve the block hash whose state this CCoinsView currently represents
272  virtual uint256 GetBestBlock();
273 
274  // Modify the currently active block hash
275  virtual bool SetBestBlock(const uint256 &hashBlock);
276 
277  // Do a bulk modification (multiple SetCoins + one SetBestBlock)
278  virtual bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
279 
280  // Calculate statistics about the unspent transaction output set
281  virtual bool GetStats(CCoinsStats &stats);
282 
283  // As we use CCoinsViews polymorphically, have a virtual destructor
284  virtual ~CCoinsView() {}
285 };
286 
287 
290 {
291 protected:
293 
294 public:
295  CCoinsViewBacked(CCoinsView &viewIn);
296  bool GetCoins(const uint256 &txid, CCoins &coins);
297  bool SetCoins(const uint256 &txid, const CCoins &coins);
298  bool HaveCoins(const uint256 &txid);
300  bool SetBestBlock(const uint256 &hashBlock);
301  void SetBackend(CCoinsView &viewIn);
302  bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
303  bool GetStats(CCoinsStats &stats);
304 };
305 
306 
309 {
310 protected:
312  std::map<uint256,CCoins> cacheCoins;
313 
314 public:
315  CCoinsViewCache(CCoinsView &baseIn, bool fDummy = false);
316 
317  // Standard CCoinsView methods
318  bool GetCoins(const uint256 &txid, CCoins &coins);
319  bool SetCoins(const uint256 &txid, const CCoins &coins);
320  bool HaveCoins(const uint256 &txid);
322  bool SetBestBlock(const uint256 &hashBlock);
323  bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
324 
325  // Return a modifiable reference to a CCoins. Check HaveCoins first.
326  // Many methods explicitly require a CCoinsViewCache because of this method, to reduce
327  // copying.
328  CCoins &GetCoins(const uint256 &txid);
329 
330  // Push the modifications applied to this cache to its base.
331  // Failure to call this method before destruction will cause the changes to be forgotten.
332  bool Flush();
333 
334  // Calculate the size of the cache (in number of transactions)
335  unsigned int GetCacheSize();
336 
344  int64_t GetValueIn(const CTransaction& tx);
345 
346  // Check whether all prevouts of the transaction are present in the UTXO set represented by this view
347  bool HaveInputs(const CTransaction& tx);
348 
349  // Return priority of tx at height nHeight
350  double GetPriority(const CTransaction &tx, int nHeight);
351 
352  const CTxOut &GetOutputFor(const CTxIn& input);
353 
354 private:
355  std::map<uint256,CCoins>::iterator FetchCoins(const uint256 &txid);
356 };
357 
358 #endif
void ClearUnspendable()
Definition: coins.h:100
CCoins()
Definition: coins.h:90
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: coins.h:135
#define VARINT(obj)
Definition: serialize.h:310
uint64_t nTransactionOutputs
Definition: coins.h:248
int64_t nTotalAmount
Definition: coins.h:251
uint256 hashBlock
Definition: coins.h:246
CCoinsStats()
Definition: coins.h:253
CScript scriptPubKey
Definition: core.h:123
void Serialize(Stream &s, int nType, int nVersion) const
Definition: coins.h:159
bool Flush()
Definition: coins.cpp:131
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:68
std::vector< CTxOut > vout
Definition: coins.h:75
int nHeight
Definition: coins.h:245
bool IsNull() const
Definition: core.h:144
wrapper for CTxOut that provides a more compact serialization
Definition: core.h:256
uint256 GetBestBlock()
Definition: coins.cpp:66
void Cleanup()
Definition: coins.h:93
int nHeight
Definition: coins.h:78
void Unserialize(Stream &s, int nType, int nVersion)
Definition: coins.h:188
uint64_t nTransactions
Definition: coins.h:247
uint256 GetBestBlock()
Definition: coins.cpp:113
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:68
bool GetStats(CCoinsStats &stats)
Definition: coins.cpp:70
virtual uint256 GetBestBlock()
Definition: coins.cpp:56
Undo information for a CTxIn.
Definition: core.h:287
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: coins.cpp:74
bool IsAvailable(unsigned int nPos) const
Definition: coins.h:228
virtual ~CCoinsView()
Definition: coins.h:284
CCoins(const CTransaction &tx, int nHeightIn)
Definition: coins.h:85
friend bool operator==(const CCoins &a, const CCoins &b)
Definition: coins.h:116
uint256 hashSerialized
Definition: coins.h:250
virtual bool SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:57
int nVersion
Definition: coins.h:82
Abstract view on the open txout dataset.
Definition: coins.h:258
bool HaveCoins(const uint256 &txid)
Definition: coins.cpp:109
An input of a transaction.
Definition: core.h:70
virtual bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: coins.cpp:53
bool HaveInputs(const CTransaction &tx)
Definition: coins.cpp:161
CCoinsView * base
Definition: coins.h:292
std::map< uint256, CCoins >::iterator FetchCoins(const uint256 &txid)
Definition: coins.cpp:86
uint64_t nSerializedSize
Definition: coins.h:249
void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const
Definition: coins.cpp:12
bool BatchWrite(const std::map< uint256, CCoins > &mapCoins, const uint256 &hashBlock)
Definition: coins.cpp:124
bool IsUnspendable() const
Definition: script.h:666
const CTxOut & GetOutputFor(const CTxIn &input)
Definition: coins.cpp:142
virtual bool HaveCoins(const uint256 &txid)
Definition: coins.cpp:55
CCoinsViewCache(CCoinsView &baseIn, bool fDummy=false)
Definition: coins.cpp:72
void swap(CCoins &to)
Definition: coins.h:108
An output of a transaction.
Definition: core.h:119
CCoinsViewBacked(CCoinsView &viewIn)
Definition: coins.cpp:62
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:22
virtual bool GetStats(CCoinsStats &stats)
Definition: coins.cpp:59
bool fCoinBase
Definition: coins.h:72
256-bit unsigned integer
Definition: uint256.h:531
uint256 hashBlock
Definition: coins.h:311
bool SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:67
bool Spend(const COutPoint &out, CTxInUndo &undo)
Definition: coins.cpp:30
double GetPriority(const CTransaction &tx, int nHeight)
Definition: coins.cpp:182
virtual bool BatchWrite(const std::map< uint256, CCoins > &mapCoins, const uint256 &hashBlock)
Definition: coins.cpp:58
bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: coins.cpp:64
bool HaveCoins(const uint256 &txid)
Definition: coins.cpp:65
bool IsPruned() const
Definition: coins.h:234
bool BatchWrite(const std::map< uint256, CCoins > &mapCoins, const uint256 &hashBlock)
Definition: coins.cpp:69
bool IsCoinBase() const
Definition: coins.h:131
int64_t GetValueIn(const CTransaction &tx)
Amount of bitcoins coming in to a transaction Note that lightweight clients may not know anything bes...
Definition: coins.cpp:149
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: coins.cpp:63
bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: coins.cpp:104
bool SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:119
unsigned int GetCacheSize()
Definition: coins.cpp:138
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
CCoinsView backed by another CCoinsView.
Definition: coins.h:289
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:308
T & REF(const T &val)
Definition: serialize.h:35
friend bool operator!=(const CCoins &a, const CCoins &b)
Definition: coins.h:125
std::map< uint256, CCoins > cacheCoins
Definition: coins.h:312
virtual bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: coins.cpp:54