|
20 | 20 | #include <boost/foreach.hpp>
|
21 | 21 | #include <unordered_map>
|
22 | 22 |
|
| 23 | +/** |
| 24 | + * A UTXO entry. |
| 25 | + * |
| 26 | + * Serialized format: |
| 27 | + * - VARINT((coinbase ? 1 : 0) | (height << 1)) |
| 28 | + * - the non-spent CTxOut (via CTxOutCompressor) |
| 29 | + */ |
| 30 | +class Coin |
| 31 | +{ |
| 32 | +public: |
| 33 | + //! whether the containing transaction was a coinbase |
| 34 | + bool fCoinBase; |
| 35 | + |
| 36 | + //! unspent transaction output |
| 37 | + CTxOut out; |
| 38 | + |
| 39 | + //! at which height the containing transaction was included in the active block chain |
| 40 | + uint32_t nHeight; |
| 41 | + |
| 42 | + //! construct a Coin from a CTxOut and height/coinbase properties. |
| 43 | + Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : fCoinBase(fCoinBaseIn), out(std::move(outIn)), nHeight(nHeightIn) {} |
| 44 | + Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : fCoinBase(fCoinBaseIn), out(outIn), 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(!IsPruned()); |
| 62 | + uint32_t code = nHeight * 2 + fCoinBase; |
| 63 | + ::Serialize(s, VARINT(code)); |
| 64 | + ::Serialize(s, CTxOutCompressor(REF(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, REF(CTxOutCompressor(out))); |
| 74 | + } |
| 75 | + |
| 76 | + bool IsPruned() const { |
| 77 | + return out.IsNull(); |
| 78 | + } |
| 79 | + |
| 80 | + size_t DynamicMemoryUsage() const { |
| 81 | + return memusage::DynamicUsage(out.scriptPubKey); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
23 | 85 | /**
|
24 | 86 | * Pruned version of CTransaction: only retains metadata and unspent transaction outputs
|
25 | 87 | *
|
|
0 commit comments