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