Skip to content

Commit 422634e

Browse files
committed
Introduce Coin, a single unspent output
1 parent 7d991b5 commit 422634e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/coins.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,68 @@
2020
#include <boost/foreach.hpp>
2121
#include <unordered_map>
2222

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+
2385
/**
2486
* Pruned version of CTransaction: only retains metadata and unspent transaction outputs
2587
*

0 commit comments

Comments
 (0)