aboutsummaryrefslogtreecommitdiff
path: root/src/coins.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-04-25 11:29:23 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2017-05-26 13:33:40 -0700
commit422634e2f5ac1ff74cd358144cecbac63007adc4 (patch)
treebf1b3f471a9685b0754731f1679a922ad22ef1a2 /src/coins.h
parent7d991b55dbf0b0f6e21c0680ee3ebd09df09012f (diff)
Introduce Coin, a single unspent output
Diffstat (limited to 'src/coins.h')
-rw-r--r--src/coins.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/coins.h b/src/coins.h
index 47ef90114b..69c24ab45a 100644
--- a/src/coins.h
+++ b/src/coins.h
@@ -20,6 +20,68 @@
#include <boost/foreach.hpp>
#include <unordered_map>
+/**
+ * A UTXO entry.
+ *
+ * Serialized format:
+ * - VARINT((coinbase ? 1 : 0) | (height << 1))
+ * - the non-spent CTxOut (via CTxOutCompressor)
+ */
+class Coin
+{
+public:
+ //! whether the containing transaction was a coinbase
+ bool fCoinBase;
+
+ //! unspent transaction output
+ CTxOut out;
+
+ //! at which height the containing transaction was included in the active block chain
+ uint32_t nHeight;
+
+ //! construct a Coin from a CTxOut and height/coinbase properties.
+ Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : fCoinBase(fCoinBaseIn), out(std::move(outIn)), nHeight(nHeightIn) {}
+ Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : fCoinBase(fCoinBaseIn), out(outIn), nHeight(nHeightIn) {}
+
+ void Clear() {
+ out.SetNull();
+ fCoinBase = false;
+ nHeight = 0;
+ }
+
+ //! empty constructor
+ Coin() : fCoinBase(false), nHeight(0) { }
+
+ bool IsCoinBase() const {
+ return fCoinBase;
+ }
+
+ template<typename Stream>
+ void Serialize(Stream &s) const {
+ assert(!IsPruned());
+ uint32_t code = nHeight * 2 + fCoinBase;
+ ::Serialize(s, VARINT(code));
+ ::Serialize(s, CTxOutCompressor(REF(out)));
+ }
+
+ template<typename Stream>
+ void Unserialize(Stream &s) {
+ uint32_t code = 0;
+ ::Unserialize(s, VARINT(code));
+ nHeight = code >> 1;
+ fCoinBase = code & 1;
+ ::Unserialize(s, REF(CTxOutCompressor(out)));
+ }
+
+ bool IsPruned() const {
+ return out.IsNull();
+ }
+
+ size_t DynamicMemoryUsage() const {
+ return memusage::DynamicUsage(out.scriptPubKey);
+ }
+};
+
/**
* Pruned version of CTransaction: only retains metadata and unspent transaction outputs
*