Skip to content

Commit a01a9f2

Browse files
committed
Introduce Coin, a single unspent output
>>> backports bitcoin/bitcoin@422634e
1 parent 69ec4a3 commit a01a9f2

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
@@ -19,6 +19,68 @@
1919

2020
#include <unordered_map>
2121

22+
/**
23+
* A UTXO entry.
24+
*
25+
* Serialized format:
26+
* - VARINT((coinbase ? 1 : 0) | (height << 1))
27+
* - the non-spent CTxOut (via CTxOutCompressor)
28+
*/
29+
class Coin
30+
{
31+
public:
32+
//! whether the containing transaction was a coinbase
33+
bool fCoinBase;
34+
35+
//! unspent transaction output
36+
CTxOut out;
37+
38+
//! at which height the containing transaction was included in the active block chain
39+
uint32_t nHeight;
40+
41+
//! construct a Coin from a CTxOut and height/coinbase properties.
42+
Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : fCoinBase(fCoinBaseIn), out(std::move(outIn)), nHeight(nHeightIn) {}
43+
Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : fCoinBase(fCoinBaseIn), out(outIn), nHeight(nHeightIn) {}
44+
45+
void Clear() {
46+
out.SetNull();
47+
fCoinBase = false;
48+
nHeight = 0;
49+
}
50+
51+
//! empty constructor
52+
Coin() : fCoinBase(false), nHeight(0) { }
53+
54+
bool IsCoinBase() const {
55+
return fCoinBase;
56+
}
57+
58+
template<typename Stream>
59+
void Serialize(Stream &s) const {
60+
assert(!IsPruned());
61+
uint32_t code = nHeight * 2 + fCoinBase;
62+
::Serialize(s, VARINT(code));
63+
::Serialize(s, CTxOutCompressor(REF(out)));
64+
}
65+
66+
template<typename Stream>
67+
void Unserialize(Stream &s) {
68+
uint32_t code = 0;
69+
::Unserialize(s, VARINT(code));
70+
nHeight = code >> 1;
71+
fCoinBase = code & 1;
72+
::Unserialize(s, REF(CTxOutCompressor(out)));
73+
}
74+
75+
bool IsPruned() const {
76+
return out.IsNull();
77+
}
78+
79+
size_t DynamicMemoryUsage() const {
80+
return memusage::DynamicUsage(out.scriptPubKey);
81+
}
82+
};
83+
2284
/**
2385
2486
****Note - for PIVX we added fCoinStake to the 2nd bit. Keep in mind when reading the following and adjust as needed.

0 commit comments

Comments
 (0)