Anoncoin  0.9.4
P2P Digital Currency
coins.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core 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 
6 #include "coins.h"
7 
8 #include <assert.h>
9 
10 // calculate number of bytes for the bitmask, and its number of non-zero bytes
11 // each bit in the bitmask represents the availability of one output, but the
12 // availabilities of the first two outputs are encoded separately
13 void CCoins::CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const {
14  unsigned int nLastUsedByte = 0;
15  for (unsigned int b = 0; 2+b*8 < vout.size(); b++) {
16  bool fZero = true;
17  for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++) {
18  if (!vout[2+b*8+i].IsNull()) {
19  fZero = false;
20  continue;
21  }
22  }
23  if (!fZero) {
24  nLastUsedByte = b + 1;
25  nNonzeroBytes++;
26  }
27  }
28  nBytes += nLastUsedByte;
29 }
30 
31 bool CCoins::Spend(const COutPoint &out, CTxInUndo &undo) {
32  if (out.n >= vout.size())
33  return false;
34  if (vout[out.n].IsNull())
35  return false;
36  undo = CTxInUndo(vout[out.n]);
37  vout[out.n].SetNull();
38  Cleanup();
39  if (vout.size() == 0) {
40  undo.nHeight = nHeight;
41  undo.fCoinBase = fCoinBase;
42  undo.nVersion = this->nVersion;
43  }
44  return true;
45 }
46 
47 bool CCoins::Spend(int nPos) {
48  CTxInUndo undo;
49  COutPoint out(0, nPos);
50  return Spend(out, undo);
51 }
52 
53 
54 bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) { return false; }
55 bool CCoinsView::SetCoins(const uint256 &txid, const CCoins &coins) { return false; }
56 bool CCoinsView::HaveCoins(const uint256 &txid) { return false; }
58 bool CCoinsView::SetBestBlock(const uint256 &hashBlock) { return false; }
59 bool CCoinsView::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) { return false; }
60 bool CCoinsView::GetStats(CCoinsStats &stats) { return false; }
61 
62 
63 CCoinsViewBacked::CCoinsViewBacked(CCoinsView &viewIn) : base(&viewIn) { }
64 bool CCoinsViewBacked::GetCoins(const uint256 &txid, CCoins &coins) { return base->GetCoins(txid, coins); }
65 bool CCoinsViewBacked::SetCoins(const uint256 &txid, const CCoins &coins) { return base->SetCoins(txid, coins); }
66 bool CCoinsViewBacked::HaveCoins(const uint256 &txid) { return base->HaveCoins(txid); }
68 bool CCoinsViewBacked::SetBestBlock(const uint256 &hashBlock) { return base->SetBestBlock(hashBlock); }
69 void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
70 bool CCoinsViewBacked::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
71 bool CCoinsViewBacked::GetStats(CCoinsStats &stats) { return base->GetStats(stats); }
72 
73 CCoinsViewCache::CCoinsViewCache(CCoinsView &baseIn, bool fDummy) : CCoinsViewBacked(baseIn), hashBlock(0) { }
74 
75 bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) {
76  if (cacheCoins.count(txid)) {
77  coins = cacheCoins[txid];
78  return true;
79  }
80  if (base->GetCoins(txid, coins)) {
81  cacheCoins[txid] = coins;
82  return true;
83  }
84  return false;
85 }
86 
87 std::map<uint256,CCoins>::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) {
88  std::map<uint256,CCoins>::iterator it = cacheCoins.lower_bound(txid);
89  if (it != cacheCoins.end() && it->first == txid)
90  return it;
91  CCoins tmp;
92  if (!base->GetCoins(txid,tmp))
93  return cacheCoins.end();
94  std::map<uint256,CCoins>::iterator ret = cacheCoins.insert(it, std::make_pair(txid, CCoins()));
95  tmp.swap(ret->second);
96  return ret;
97 }
98 
100  std::map<uint256,CCoins>::iterator it = FetchCoins(txid);
101  assert(it != cacheCoins.end());
102  return it->second;
103 }
104 
105 bool CCoinsViewCache::SetCoins(const uint256 &txid, const CCoins &coins) {
106  cacheCoins[txid] = coins;
107  return true;
108 }
109 
111  return FetchCoins(txid) != cacheCoins.end();
112 }
113 
115  if (hashBlock == uint256(0))
117  return hashBlock;
118 }
119 
120 bool CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
121  hashBlock = hashBlockIn;
122  return true;
123 }
124 
125 bool CCoinsViewCache::BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlockIn) {
126  for (std::map<uint256, CCoins>::const_iterator it = mapCoins.begin(); it != mapCoins.end(); it++)
127  cacheCoins[it->first] = it->second;
128  hashBlock = hashBlockIn;
129  return true;
130 }
131 
133  bool fOk = base->BatchWrite(cacheCoins, hashBlock);
134  if (fOk)
135  cacheCoins.clear();
136  return fOk;
137 }
138 
140  return cacheCoins.size();
141 }
142 
144 {
145  const CCoins &coins = GetCoins(input.prevout.hash);
146  assert(coins.IsAvailable(input.prevout.n));
147  return coins.vout[input.prevout.n];
148 }
149 
151 {
152  if (tx.IsCoinBase())
153  return 0;
154 
155  int64_t nResult = 0;
156  for (unsigned int i = 0; i < tx.vin.size(); i++)
157  nResult += GetOutputFor(tx.vin[i]).nValue;
158 
159  return nResult;
160 }
161 
163 {
164  if (!tx.IsCoinBase()) {
165  // first check whether information about the prevout hash is available
166  for (unsigned int i = 0; i < tx.vin.size(); i++) {
167  const COutPoint &prevout = tx.vin[i].prevout;
168  if (!HaveCoins(prevout.hash))
169  return false;
170  }
171 
172  // then check whether the actual outputs are available
173  for (unsigned int i = 0; i < tx.vin.size(); i++) {
174  const COutPoint &prevout = tx.vin[i].prevout;
175  const CCoins &coins = GetCoins(prevout.hash);
176  if (!coins.IsAvailable(prevout.n))
177  return false;
178  }
179  }
180  return true;
181 }
182 
183 double CCoinsViewCache::GetPriority(const CTransaction &tx, int nHeight)
184 {
185  if (tx.IsCoinBase())
186  return 0.0;
187  double dResult = 0.0;
188  BOOST_FOREACH(const CTxIn& txin, tx.vin)
189  {
190  const CCoins &coins = GetCoins(txin.prevout.hash);
191  if (!coins.IsAvailable(txin.prevout.n)) continue;
192  if (coins.nHeight < nHeight) {
193  dResult += coins.vout[txin.prevout.n].nValue * (nHeight-coins.nHeight);
194  }
195  }
196  return tx.ComputePriority(dResult);
197 }
bool Flush()
Definition: coins.cpp:132
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:69
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
Definition: core.cpp:122
std::vector< CTxOut > vout
Definition: coins.h:76
uint256 GetBestBlock()
Definition: coins.cpp:67
void Cleanup()
Definition: coins.h:94
int nHeight
Definition: coins.h:79
uint256 GetBestBlock()
Definition: coins.cpp:114
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:69
unsigned int n
Definition: core.h:28
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 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 fCoinBase
Definition: core.h:287
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
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
std::vector< CTxIn > vin
Definition: core.h:186
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
unsigned int nHeight
Definition: core.h:288
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 BatchWrite(const std::map< uint256, CCoins > &mapCoins, const uint256 &hashBlock)
Definition: coins.cpp:70
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 IsCoinBase() const
Definition: core.h:228
bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: coins.cpp:105
int nVersion
Definition: core.h:289
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
COutPoint prevout
Definition: core.h:75
int64_t nValue
Definition: core.h:124
std::map< uint256, CCoins > cacheCoins
Definition: coins.h:313
uint256 hash
Definition: core.h:27
virtual bool SetCoins(const uint256 &txid, const CCoins &coins)
Definition: coins.cpp:55