]>
Commit | Line | Data |
---|---|---|
a0fa20a1 PW |
1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | // Copyright (c) 2009-2013 The Bitcoin developers | |
3 | // Distributed under the MIT/X11 software license, see the accompanying | |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | |
093303a8 | 5 | |
a0fa20a1 PW |
6 | #ifndef BITCOIN_COINS_H |
7 | #define BITCOIN_COINS_H | |
8 | ||
9 | #include "core.h" | |
10 | #include "serialize.h" | |
11 | #include "uint256.h" | |
12 | ||
13 | #include <assert.h> | |
14 | #include <stdint.h> | |
15 | ||
16 | #include <boost/foreach.hpp> | |
bc42503f | 17 | #include <boost/unordered_map.hpp> |
a0fa20a1 PW |
18 | |
19 | /** pruned version of CTransaction: only retains metadata and unspent transaction outputs | |
20 | * | |
21 | * Serialized format: | |
22 | * - VARINT(nVersion) | |
23 | * - VARINT(nCode) | |
24 | * - unspentness bitvector, for vout[2] and further; least significant byte first | |
25 | * - the non-spent CTxOuts (via CTxOutCompressor) | |
26 | * - VARINT(nHeight) | |
27 | * | |
28 | * The nCode value consists of: | |
29 | * - bit 1: IsCoinBase() | |
30 | * - bit 2: vout[0] is not spent | |
31 | * - bit 4: vout[1] is not spent | |
32 | * - The higher bits encode N, the number of non-zero bytes in the following bitvector. | |
33 | * - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at | |
34 | * least one non-spent output). | |
35 | * | |
36 | * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e | |
37 | * <><><--------------------------------------------><----> | |
38 | * | \ | / | |
39 | * version code vout[1] height | |
40 | * | |
41 | * - version = 1 | |
42 | * - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow) | |
43 | * - unspentness bitvector: as 0 non-zero bytes follow, it has length 0 | |
44 | * - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35 | |
45 | * * 8358: compact amount representation for 60000000000 (600 BTC) | |
46 | * * 00: special txout type pay-to-pubkey-hash | |
47 | * * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160 | |
48 | * - height = 203998 | |
49 | * | |
50 | * | |
51 | * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b | |
52 | * <><><--><--------------------------------------------------><----------------------------------------------><----> | |
53 | * / \ \ | | / | |
54 | * version code unspentness vout[4] vout[16] height | |
55 | * | |
56 | * - version = 1 | |
57 | * - code = 9 (coinbase, neither vout[0] or vout[1] are unspent, | |
58 | * 2 (1, +1 because both bit 2 and bit 4 are unset) non-zero bitvector bytes follow) | |
59 | * - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent | |
60 | * - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee | |
61 | * * 86ef97d579: compact amount representation for 234925952 (2.35 BTC) | |
62 | * * 00: special txout type pay-to-pubkey-hash | |
63 | * * 61b01caab50f1b8e9c50a5057eb43c2d9563a4ee: address uint160 | |
64 | * - vout[16]: bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4 | |
65 | * * bbd123: compact amount representation for 110397 (0.001 BTC) | |
66 | * * 00: special txout type pay-to-pubkey-hash | |
67 | * * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160 | |
68 | * - height = 120891 | |
69 | */ | |
70 | class CCoins | |
71 | { | |
72 | public: | |
73 | // whether transaction is a coinbase | |
74 | bool fCoinBase; | |
75 | ||
76 | // unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped | |
77 | std::vector<CTxOut> vout; | |
78 | ||
79 | // at which height this transaction was included in the active block chain | |
80 | int nHeight; | |
81 | ||
82 | // version of the CTransaction; accesses to this value should probably check for nHeight as well, | |
83 | // as new tx version will probably only be introduced at certain heights | |
84 | int nVersion; | |
85 | ||
86 | // construct a CCoins from a CTransaction, at a given height | |
87 | CCoins(const CTransaction &tx, int nHeightIn) : fCoinBase(tx.IsCoinBase()), vout(tx.vout), nHeight(nHeightIn), nVersion(tx.nVersion) { | |
88 | ClearUnspendable(); | |
89 | } | |
90 | ||
91 | // empty constructor | |
92 | CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { } | |
93 | ||
94 | // remove spent outputs at the end of vout | |
95 | void Cleanup() { | |
96 | while (vout.size() > 0 && vout.back().IsNull()) | |
97 | vout.pop_back(); | |
98 | if (vout.empty()) | |
99 | std::vector<CTxOut>().swap(vout); | |
100 | } | |
101 | ||
102 | void ClearUnspendable() { | |
103 | BOOST_FOREACH(CTxOut &txout, vout) { | |
104 | if (txout.scriptPubKey.IsUnspendable()) | |
105 | txout.SetNull(); | |
106 | } | |
107 | Cleanup(); | |
108 | } | |
109 | ||
110 | void swap(CCoins &to) { | |
111 | std::swap(to.fCoinBase, fCoinBase); | |
112 | to.vout.swap(vout); | |
113 | std::swap(to.nHeight, nHeight); | |
114 | std::swap(to.nVersion, nVersion); | |
115 | } | |
116 | ||
117 | // equality test | |
118 | friend bool operator==(const CCoins &a, const CCoins &b) { | |
119 | // Empty CCoins objects are always equal. | |
120 | if (a.IsPruned() && b.IsPruned()) | |
121 | return true; | |
122 | return a.fCoinBase == b.fCoinBase && | |
123 | a.nHeight == b.nHeight && | |
124 | a.nVersion == b.nVersion && | |
125 | a.vout == b.vout; | |
126 | } | |
127 | friend bool operator!=(const CCoins &a, const CCoins &b) { | |
128 | return !(a == b); | |
129 | } | |
130 | ||
131 | void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const; | |
132 | ||
133 | bool IsCoinBase() const { | |
134 | return fCoinBase; | |
135 | } | |
136 | ||
137 | unsigned int GetSerializeSize(int nType, int nVersion) const { | |
138 | unsigned int nSize = 0; | |
139 | unsigned int nMaskSize = 0, nMaskCode = 0; | |
140 | CalcMaskSize(nMaskSize, nMaskCode); | |
141 | bool fFirst = vout.size() > 0 && !vout[0].IsNull(); | |
142 | bool fSecond = vout.size() > 1 && !vout[1].IsNull(); | |
143 | assert(fFirst || fSecond || nMaskCode); | |
144 | unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0); | |
145 | // version | |
146 | nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion); | |
147 | // size of header code | |
148 | nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion); | |
149 | // spentness bitmask | |
150 | nSize += nMaskSize; | |
151 | // txouts themself | |
152 | for (unsigned int i = 0; i < vout.size(); i++) | |
153 | if (!vout[i].IsNull()) | |
154 | nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion); | |
155 | // height | |
156 | nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion); | |
157 | return nSize; | |
158 | } | |
159 | ||
160 | template<typename Stream> | |
161 | void Serialize(Stream &s, int nType, int nVersion) const { | |
162 | unsigned int nMaskSize = 0, nMaskCode = 0; | |
163 | CalcMaskSize(nMaskSize, nMaskCode); | |
164 | bool fFirst = vout.size() > 0 && !vout[0].IsNull(); | |
165 | bool fSecond = vout.size() > 1 && !vout[1].IsNull(); | |
166 | assert(fFirst || fSecond || nMaskCode); | |
167 | unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0); | |
168 | // version | |
169 | ::Serialize(s, VARINT(this->nVersion), nType, nVersion); | |
170 | // header code | |
171 | ::Serialize(s, VARINT(nCode), nType, nVersion); | |
172 | // spentness bitmask | |
173 | for (unsigned int b = 0; b<nMaskSize; b++) { | |
174 | unsigned char chAvail = 0; | |
175 | for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++) | |
176 | if (!vout[2+b*8+i].IsNull()) | |
177 | chAvail |= (1 << i); | |
178 | ::Serialize(s, chAvail, nType, nVersion); | |
179 | } | |
180 | // txouts themself | |
181 | for (unsigned int i = 0; i < vout.size(); i++) { | |
182 | if (!vout[i].IsNull()) | |
183 | ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion); | |
184 | } | |
185 | // coinbase height | |
186 | ::Serialize(s, VARINT(nHeight), nType, nVersion); | |
187 | } | |
188 | ||
189 | template<typename Stream> | |
190 | void Unserialize(Stream &s, int nType, int nVersion) { | |
191 | unsigned int nCode = 0; | |
192 | // version | |
193 | ::Unserialize(s, VARINT(this->nVersion), nType, nVersion); | |
194 | // header code | |
195 | ::Unserialize(s, VARINT(nCode), nType, nVersion); | |
196 | fCoinBase = nCode & 1; | |
197 | std::vector<bool> vAvail(2, false); | |
198 | vAvail[0] = nCode & 2; | |
199 | vAvail[1] = nCode & 4; | |
200 | unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1); | |
201 | // spentness bitmask | |
202 | while (nMaskCode > 0) { | |
203 | unsigned char chAvail = 0; | |
204 | ::Unserialize(s, chAvail, nType, nVersion); | |
205 | for (unsigned int p = 0; p < 8; p++) { | |
206 | bool f = (chAvail & (1 << p)) != 0; | |
207 | vAvail.push_back(f); | |
208 | } | |
209 | if (chAvail != 0) | |
210 | nMaskCode--; | |
211 | } | |
212 | // txouts themself | |
213 | vout.assign(vAvail.size(), CTxOut()); | |
214 | for (unsigned int i = 0; i < vAvail.size(); i++) { | |
215 | if (vAvail[i]) | |
216 | ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion); | |
217 | } | |
218 | // coinbase height | |
219 | ::Unserialize(s, VARINT(nHeight), nType, nVersion); | |
220 | Cleanup(); | |
221 | } | |
222 | ||
223 | // mark an outpoint spent, and construct undo information | |
224 | bool Spend(const COutPoint &out, CTxInUndo &undo); | |
225 | ||
226 | // mark a vout spent | |
227 | bool Spend(int nPos); | |
228 | ||
229 | // check whether a particular output is still available | |
230 | bool IsAvailable(unsigned int nPos) const { | |
231 | return (nPos < vout.size() && !vout[nPos].IsNull()); | |
232 | } | |
233 | ||
234 | // check whether the entire CCoins is spent | |
235 | // note that only !IsPruned() CCoins can be serialized | |
236 | bool IsPruned() const { | |
237 | BOOST_FOREACH(const CTxOut &out, vout) | |
238 | if (!out.IsNull()) | |
239 | return false; | |
240 | return true; | |
241 | } | |
242 | }; | |
243 | ||
bc42503f PW |
244 | class CCoinsKeyHasher |
245 | { | |
246 | private: | |
247 | uint256 salt; | |
248 | ||
249 | public: | |
250 | CCoinsKeyHasher(); | |
6c23b082 WL |
251 | // This *must* return size_t. With Boost 1.46 on 32-bit systems the |
252 | // unordered_map will behave unpredictably if the custom hasher returns a | |
253 | // uint64_t, resulting in failures when syncing the chain (#4634). | |
254 | size_t operator()(const uint256& key) const { | |
bc42503f PW |
255 | return key.GetHash(salt); |
256 | } | |
257 | }; | |
258 | ||
259 | typedef boost::unordered_map<uint256, CCoins, CCoinsKeyHasher> CCoinsMap; | |
a0fa20a1 PW |
260 | |
261 | struct CCoinsStats | |
262 | { | |
263 | int nHeight; | |
264 | uint256 hashBlock; | |
265 | uint64_t nTransactions; | |
266 | uint64_t nTransactionOutputs; | |
267 | uint64_t nSerializedSize; | |
268 | uint256 hashSerialized; | |
269 | int64_t nTotalAmount; | |
270 | ||
271 | CCoinsStats() : nHeight(0), hashBlock(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), hashSerialized(0), nTotalAmount(0) {} | |
272 | }; | |
273 | ||
274 | ||
275 | /** Abstract view on the open txout dataset. */ | |
276 | class CCoinsView | |
277 | { | |
278 | public: | |
279 | // Retrieve the CCoins (unspent transaction outputs) for a given txid | |
a3dc587a | 280 | virtual bool GetCoins(const uint256 &txid, CCoins &coins) const; |
a0fa20a1 PW |
281 | |
282 | // Modify the CCoins for a given txid | |
283 | virtual bool SetCoins(const uint256 &txid, const CCoins &coins); | |
284 | ||
285 | // Just check whether we have data for a given txid. | |
286 | // This may (but cannot always) return true for fully spent transactions | |
a3dc587a | 287 | virtual bool HaveCoins(const uint256 &txid) const; |
a0fa20a1 PW |
288 | |
289 | // Retrieve the block hash whose state this CCoinsView currently represents | |
a3dc587a | 290 | virtual uint256 GetBestBlock() const; |
a0fa20a1 PW |
291 | |
292 | // Modify the currently active block hash | |
293 | virtual bool SetBestBlock(const uint256 &hashBlock); | |
294 | ||
b0875eb3 PW |
295 | // Do a bulk modification (multiple SetCoins + one SetBestBlock). |
296 | // The passed mapCoins can be modified. | |
297 | virtual bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock); | |
a0fa20a1 PW |
298 | |
299 | // Calculate statistics about the unspent transaction output set | |
a3dc587a | 300 | virtual bool GetStats(CCoinsStats &stats) const; |
a0fa20a1 PW |
301 | |
302 | // As we use CCoinsViews polymorphically, have a virtual destructor | |
303 | virtual ~CCoinsView() {} | |
304 | }; | |
305 | ||
306 | ||
307 | /** CCoinsView backed by another CCoinsView */ | |
308 | class CCoinsViewBacked : public CCoinsView | |
309 | { | |
310 | protected: | |
311 | CCoinsView *base; | |
312 | ||
313 | public: | |
314 | CCoinsViewBacked(CCoinsView &viewIn); | |
a3dc587a | 315 | bool GetCoins(const uint256 &txid, CCoins &coins) const; |
a0fa20a1 | 316 | bool SetCoins(const uint256 &txid, const CCoins &coins); |
a3dc587a DK |
317 | bool HaveCoins(const uint256 &txid) const; |
318 | uint256 GetBestBlock() const; | |
a0fa20a1 PW |
319 | bool SetBestBlock(const uint256 &hashBlock); |
320 | void SetBackend(CCoinsView &viewIn); | |
b0875eb3 | 321 | bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock); |
a3dc587a | 322 | bool GetStats(CCoinsStats &stats) const; |
a0fa20a1 PW |
323 | }; |
324 | ||
325 | ||
326 | /** CCoinsView that adds a memory cache for transactions to another CCoinsView */ | |
327 | class CCoinsViewCache : public CCoinsViewBacked | |
328 | { | |
329 | protected: | |
a3dc587a DK |
330 | |
331 | /* Make mutable so that we can "fill the cache" even from Get-methods | |
332 | declared as "const". */ | |
333 | mutable uint256 hashBlock; | |
334 | mutable CCoinsMap cacheCoins; | |
a0fa20a1 PW |
335 | |
336 | public: | |
337 | CCoinsViewCache(CCoinsView &baseIn, bool fDummy = false); | |
338 | ||
339 | // Standard CCoinsView methods | |
a3dc587a | 340 | bool GetCoins(const uint256 &txid, CCoins &coins) const; |
a0fa20a1 | 341 | bool SetCoins(const uint256 &txid, const CCoins &coins); |
a3dc587a DK |
342 | bool HaveCoins(const uint256 &txid) const; |
343 | uint256 GetBestBlock() const; | |
a0fa20a1 | 344 | bool SetBestBlock(const uint256 &hashBlock); |
b0875eb3 | 345 | bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock); |
a0fa20a1 PW |
346 | |
347 | // Return a modifiable reference to a CCoins. Check HaveCoins first. | |
348 | // Many methods explicitly require a CCoinsViewCache because of this method, to reduce | |
349 | // copying. | |
350 | CCoins &GetCoins(const uint256 &txid); | |
a3dc587a | 351 | const CCoins &GetCoins(const uint256 &txid) const; |
a0fa20a1 PW |
352 | |
353 | // Push the modifications applied to this cache to its base. | |
354 | // Failure to call this method before destruction will cause the changes to be forgotten. | |
b0875eb3 | 355 | // If false is returned, the state of this cache (and its backing view) will be undefined. |
a0fa20a1 PW |
356 | bool Flush(); |
357 | ||
358 | // Calculate the size of the cache (in number of transactions) | |
a3dc587a | 359 | unsigned int GetCacheSize() const; |
a0fa20a1 PW |
360 | |
361 | /** Amount of bitcoins coming in to a transaction | |
362 | Note that lightweight clients may not know anything besides the hash of previous transactions, | |
363 | so may not be able to calculate this. | |
364 | ||
365 | @param[in] tx transaction for which we are checking input total | |
366 | @return Sum of value of all inputs (scriptSigs) | |
a0fa20a1 | 367 | */ |
a3dc587a | 368 | int64_t GetValueIn(const CTransaction& tx) const; |
a0fa20a1 PW |
369 | |
370 | // Check whether all prevouts of the transaction are present in the UTXO set represented by this view | |
a3dc587a | 371 | bool HaveInputs(const CTransaction& tx) const; |
a0fa20a1 | 372 | |
4d707d51 | 373 | // Return priority of tx at height nHeight |
a3dc587a | 374 | double GetPriority(const CTransaction &tx, int nHeight) const; |
4d707d51 | 375 | |
a3dc587a | 376 | const CTxOut &GetOutputFor(const CTxIn& input) const; |
a0fa20a1 PW |
377 | |
378 | private: | |
dd638dd7 | 379 | CCoinsMap::iterator FetchCoins(const uint256 &txid); |
a3dc587a | 380 | CCoinsMap::const_iterator FetchCoins(const uint256 &txid) const; |
a0fa20a1 PW |
381 | }; |
382 | ||
093303a8 | 383 | #endif // BITCOIN_COINS_H |