coins.h


SUBMITTED BY: Guest

DATE: June 8, 2014, 8:28 p.m.

FORMAT: C++

SIZE: 14.6 kB

HITS: 914

  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. #include "core.h"
  8. #include "serialize.h"
  9. #include "uint256.h"
  10. #include <assert.h>
  11. #include <stdint.h>
  12. #include <boost/foreach.hpp>
  13. /** pruned version of CTransaction: only retains metadata and unspent transaction outputs
  14. *
  15. * Serialized format:
  16. * - VARINT(nVersion)
  17. * - VARINT(nCode)
  18. * - unspentness bitvector, for vout[2] and further; least significant byte first
  19. * - the non-spent CTxOuts (via CTxOutCompressor)
  20. * - VARINT(nHeight)
  21. *
  22. * The nCode value consists of:
  23. * - bit 1: IsCoinBase()
  24. * - bit 2: vout[0] is not spent
  25. * - bit 4: vout[1] is not spent
  26. * - The higher bits encode N, the number of non-zero bytes in the following bitvector.
  27. * - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at
  28. * least one non-spent output).
  29. *
  30. * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e
  31. * <><><--------------------------------------------><---->
  32. * | \ | /
  33. * version code vout[1] height
  34. *
  35. * - version = 1
  36. * - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow)
  37. * - unspentness bitvector: as 0 non-zero bytes follow, it has length 0
  38. * - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35
  39. * * 8358: compact amount representation for 60000000000 (600 DOGE)
  40. * * 00: special txout type pay-to-pubkey-hash
  41. * * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160
  42. * - height = 203998
  43. *
  44. *
  45. * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b
  46. * <><><--><--------------------------------------------------><----------------------------------------------><---->
  47. * / \ \ | | /
  48. * version code unspentness vout[4] vout[16] height
  49. *
  50. * - version = 1
  51. * - code = 9 (coinbase, neither vout[0] or vout[1] are unspent,
  52. * 2 (1, +1 because both bit 2 and bit 4 are unset) non-zero bitvector bytes follow)
  53. * - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent
  54. * - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee
  55. * * 86ef97d579: compact amount representation for 234925952 (2.35 DOGE)
  56. * * 00: special txout type pay-to-pubkey-hash
  57. * * 61b01caab50f1b8e9c50a5057eb43c2d9563a4ee: address uint160
  58. * - vout[16]: bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4
  59. * * bbd123: compact amount representation for 110397 (0.001 DOGE)
  60. * * 00: special txout type pay-to-pubkey-hash
  61. * * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160
  62. * - height = 120891
  63. */
  64. class CCoins
  65. {
  66. public:
  67. // whether transaction is a coinbase
  68. bool fCoinBase;
  69. // unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped
  70. std::vector<CTxOut> vout;
  71. // at which height this transaction was included in the active block chain
  72. int nHeight;
  73. // version of the CTransaction; accesses to this value should probably check for nHeight as well,
  74. // as new tx version will probably only be introduced at certain heights
  75. int nVersion;
  76. // construct a CCoins from a CTransaction, at a given height
  77. CCoins(const CTransaction &tx, int nHeightIn) : fCoinBase(tx.IsCoinBase()), vout(tx.vout), nHeight(nHeightIn), nVersion(tx.nVersion) {
  78. ClearUnspendable();
  79. }
  80. // empty constructor
  81. CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { }
  82. // remove spent outputs at the end of vout
  83. void Cleanup() {
  84. while (vout.size() > 0 && vout.back().IsNull())
  85. vout.pop_back();
  86. if (vout.empty())
  87. std::vector<CTxOut>().swap(vout);
  88. }
  89. void ClearUnspendable() {
  90. BOOST_FOREACH(CTxOut &txout, vout) {
  91. if (txout.scriptPubKey.IsUnspendable())
  92. txout.SetNull();
  93. }
  94. Cleanup();
  95. }
  96. void swap(CCoins &to) {
  97. std::swap(to.fCoinBase, fCoinBase);
  98. to.vout.swap(vout);
  99. std::swap(to.nHeight, nHeight);
  100. std::swap(to.nVersion, nVersion);
  101. }
  102. // equality test
  103. friend bool operator==(const CCoins &a, const CCoins &b) {
  104. // Empty CCoins objects are always equal.
  105. if (a.IsPruned() && b.IsPruned())
  106. return true;
  107. return a.fCoinBase == b.fCoinBase &&
  108. a.nHeight == b.nHeight &&
  109. a.nVersion == b.nVersion &&
  110. a.vout == b.vout;
  111. }
  112. friend bool operator!=(const CCoins &a, const CCoins &b) {
  113. return !(a == b);
  114. }
  115. void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const;
  116. bool IsCoinBase() const {
  117. return fCoinBase;
  118. }
  119. unsigned int GetSerializeSize(int nType, int nVersion) const {
  120. unsigned int nSize = 0;
  121. unsigned int nMaskSize = 0, nMaskCode = 0;
  122. CalcMaskSize(nMaskSize, nMaskCode);
  123. bool fFirst = vout.size() > 0 && !vout[0].IsNull();
  124. bool fSecond = vout.size() > 1 && !vout[1].IsNull();
  125. assert(fFirst || fSecond || nMaskCode);
  126. unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
  127. // version
  128. nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
  129. // size of header code
  130. nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
  131. // spentness bitmask
  132. nSize += nMaskSize;
  133. // txouts themself
  134. for (unsigned int i = 0; i < vout.size(); i++)
  135. if (!vout[i].IsNull())
  136. nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
  137. // height
  138. nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
  139. return nSize;
  140. }
  141. template<typename Stream>
  142. void Serialize(Stream &s, int nType, int nVersion) const {
  143. unsigned int nMaskSize = 0, nMaskCode = 0;
  144. CalcMaskSize(nMaskSize, nMaskCode);
  145. bool fFirst = vout.size() > 0 && !vout[0].IsNull();
  146. bool fSecond = vout.size() > 1 && !vout[1].IsNull();
  147. assert(fFirst || fSecond || nMaskCode);
  148. unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
  149. // version
  150. ::Serialize(s, VARINT(this->nVersion), nType, nVersion);
  151. // header code
  152. ::Serialize(s, VARINT(nCode), nType, nVersion);
  153. // spentness bitmask
  154. for (unsigned int b = 0; b<nMaskSize; b++) {
  155. unsigned char chAvail = 0;
  156. for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++)
  157. if (!vout[2+b*8+i].IsNull())
  158. chAvail |= (1 << i);
  159. ::Serialize(s, chAvail, nType, nVersion);
  160. }
  161. // txouts themself
  162. for (unsigned int i = 0; i < vout.size(); i++) {
  163. if (!vout[i].IsNull())
  164. ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion);
  165. }
  166. // coinbase height
  167. ::Serialize(s, VARINT(nHeight), nType, nVersion);
  168. }
  169. template<typename Stream>
  170. void Unserialize(Stream &s, int nType, int nVersion) {
  171. unsigned int nCode = 0;
  172. // version
  173. ::Unserialize(s, VARINT(this->nVersion), nType, nVersion);
  174. // header code
  175. ::Unserialize(s, VARINT(nCode), nType, nVersion);
  176. fCoinBase = nCode & 1;
  177. std::vector<bool> vAvail(2, false);
  178. vAvail[0] = nCode & 2;
  179. vAvail[1] = nCode & 4;
  180. unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
  181. // spentness bitmask
  182. while (nMaskCode > 0) {
  183. unsigned char chAvail = 0;
  184. ::Unserialize(s, chAvail, nType, nVersion);
  185. for (unsigned int p = 0; p < 8; p++) {
  186. bool f = (chAvail & (1 << p)) != 0;
  187. vAvail.push_back(f);
  188. }
  189. if (chAvail != 0)
  190. nMaskCode--;
  191. }
  192. // txouts themself
  193. vout.assign(vAvail.size(), CTxOut());
  194. for (unsigned int i = 0; i < vAvail.size(); i++) {
  195. if (vAvail[i])
  196. ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion);
  197. }
  198. // coinbase height
  199. ::Unserialize(s, VARINT(nHeight), nType, nVersion);
  200. Cleanup();
  201. }
  202. // mark an outpoint spent, and construct undo information
  203. bool Spend(const COutPoint &out, CTxInUndo &undo);
  204. // mark a vout spent
  205. bool Spend(int nPos);
  206. // check whether a particular output is still available
  207. bool IsAvailable(unsigned int nPos) const {
  208. return (nPos < vout.size() && !vout[nPos].IsNull());
  209. }
  210. // check whether the entire CCoins is spent
  211. // note that only !IsPruned() CCoins can be serialized
  212. bool IsPruned() const {
  213. BOOST_FOREACH(const CTxOut &out, vout)
  214. if (!out.IsNull())
  215. return false;
  216. return true;
  217. }
  218. };
  219. struct CCoinsStats
  220. {
  221. int nHeight;
  222. uint256 hashBlock;
  223. uint64_t nTransactions;
  224. uint64_t nTransactionOutputs;
  225. uint64_t nSerializedSize;
  226. uint256 hashSerialized;
  227. int64_t nTotalAmount;
  228. CCoinsStats() : nHeight(0), hashBlock(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), hashSerialized(0), nTotalAmount(0) {}
  229. };
  230. /** Abstract view on the open txout dataset. */
  231. class CCoinsView
  232. {
  233. public:
  234. // Retrieve the CCoins (unspent transaction outputs) for a given txid
  235. virtual bool GetCoins(const uint256 &txid, CCoins &coins);
  236. // Modify the CCoins for a given txid
  237. virtual bool SetCoins(const uint256 &txid, const CCoins &coins);
  238. // Just check whether we have data for a given txid.
  239. // This may (but cannot always) return true for fully spent transactions
  240. virtual bool HaveCoins(const uint256 &txid);
  241. // Retrieve the block hash whose state this CCoinsView currently represents
  242. virtual uint256 GetBestBlock();
  243. // Modify the currently active block hash
  244. virtual bool SetBestBlock(const uint256 &hashBlock);
  245. // Do a bulk modification (multiple SetCoins + one SetBestBlock)
  246. virtual bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
  247. // Calculate statistics about the unspent transaction output set
  248. virtual bool GetStats(CCoinsStats &stats);
  249. // As we use CCoinsViews polymorphically, have a virtual destructor
  250. virtual ~CCoinsView() {}
  251. };
  252. /** CCoinsView backed by another CCoinsView */
  253. class CCoinsViewBacked : public CCoinsView
  254. {
  255. protected:
  256. CCoinsView *base;
  257. public:
  258. CCoinsViewBacked(CCoinsView &viewIn);
  259. bool GetCoins(const uint256 &txid, CCoins &coins);
  260. bool SetCoins(const uint256 &txid, const CCoins &coins);
  261. bool HaveCoins(const uint256 &txid);
  262. uint256 GetBestBlock();
  263. bool SetBestBlock(const uint256 &hashBlock);
  264. void SetBackend(CCoinsView &viewIn);
  265. bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
  266. bool GetStats(CCoinsStats &stats);
  267. };
  268. /** CCoinsView that adds a memory cache for transactions to another CCoinsView */
  269. class CCoinsViewCache : public CCoinsViewBacked
  270. {
  271. protected:
  272. uint256 hashBlock;
  273. std::map<uint256,CCoins> cacheCoins;
  274. public:
  275. CCoinsViewCache(CCoinsView &baseIn, bool fDummy = false);
  276. // Standard CCoinsView methods
  277. bool GetCoins(const uint256 &txid, CCoins &coins);
  278. bool SetCoins(const uint256 &txid, const CCoins &coins);
  279. bool HaveCoins(const uint256 &txid);
  280. uint256 GetBestBlock();
  281. bool SetBestBlock(const uint256 &hashBlock);
  282. bool BatchWrite(const std::map<uint256, CCoins> &mapCoins, const uint256 &hashBlock);
  283. // Return a modifiable reference to a CCoins. Check HaveCoins first.
  284. // Many methods explicitly require a CCoinsViewCache because of this method, to reduce
  285. // copying.
  286. CCoins &GetCoins(const uint256 &txid);
  287. // Push the modifications applied to this cache to its base.
  288. // Failure to call this method before destruction will cause the changes to be forgotten.
  289. bool Flush();
  290. // Calculate the size of the cache (in number of transactions)
  291. unsigned int GetCacheSize();
  292. /** Amount of dogecoins coming in to a transaction
  293. Note that lightweight clients may not know anything besides the hash of previous transactions,
  294. so may not be able to calculate this.
  295. @param[in] tx transaction for which we are checking input total
  296. @return Sum of value of all inputs (scriptSigs)
  297. */
  298. int64_t GetValueIn(const CTransaction& tx);
  299. // Check whether all prevouts of the transaction are present in the UTXO set represented by this view
  300. bool HaveInputs(const CTransaction& tx);
  301. // Return priority of tx at height nHeight
  302. double GetPriority(const CTransaction &tx, int nHeight);
  303. const CTxOut &GetOutputFor(const CTxIn& input);
  304. private:
  305. std::map<uint256,CCoins>::iterator FetchCoins(const uint256 &txid);
  306. };
  307. #endif

comments powered by Disqus