* A UTXO entry. * * Serialized format: * - VARINT((coinbase ? 1 : 0) | (height << 1)) * - the non-spent CTxOut (via TxOutCompression) */
| 28 | * - the non-spent CTxOut (via TxOutCompression) |
| 29 | */ |
| 30 | class Coin |
| 31 | { |
| 32 | public: |
| 33 | //! unspent transaction output |
| 34 | CTxOut out; |
| 35 | |
| 36 | //! whether containing transaction was a coinbase |
| 37 | unsigned int fCoinBase : 1; |
| 38 | |
| 39 | //! at which height this containing transaction was included in the active block chain |
| 40 | uint32_t nHeight : 31; |
| 41 | |
| 42 | //! construct a Coin from a CTxOut and height/coinbase information. |
| 43 | Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {} |
| 44 | Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {} |
| 45 | |
| 46 | void Clear() { |
| 47 | out.SetNull(); |
| 48 | fCoinBase = false; |
| 49 | nHeight = 0; |
| 50 | } |
| 51 | |
| 52 | //! empty constructor |
| 53 | Coin() : fCoinBase(false), nHeight(0) { } |
| 54 | |
| 55 | bool IsCoinBase() const { |
| 56 | return fCoinBase; |
| 57 | } |
| 58 | |
| 59 | template<typename Stream> |
| 60 | void Serialize(Stream &s) const { |
| 61 | assert(!IsSpent()); |
| 62 | uint32_t code = nHeight * uint32_t{2} + fCoinBase; |
| 63 | ::Serialize(s, VARINT(code)); |
| 64 | ::Serialize(s, Using<TxOutCompression>(out)); |
| 65 | } |
| 66 | |
| 67 | template<typename Stream> |
| 68 | void Unserialize(Stream &s) { |
| 69 | uint32_t code = 0; |
| 70 | ::Unserialize(s, VARINT(code)); |
| 71 | nHeight = code >> 1; |
| 72 | fCoinBase = code & 1; |
| 73 | ::Unserialize(s, Using<TxOutCompression>(out)); |
| 74 | } |
| 75 | |
| 76 | /** Either this coin never existed (see e.g. coinEmpty in coins.cpp), or it |
| 77 | * did exist and has been spent. |
| 78 | */ |
| 79 | bool IsSpent() const { |
| 80 | return out.IsNull(); |
| 81 | } |
| 82 | |
| 83 | size_t DynamicMemoryUsage() const { |
| 84 | return memusage::DynamicUsage(out.scriptPubKey); |
| 85 | } |
| 86 | }; |
| 87 |
no outgoing calls
no test coverage detected