]>
Commit | Line | Data |
---|---|---|
a0fa20a1 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
fa94b9d5 | 3 | // Distributed under the MIT software license, see the accompanying |
a0fa20a1 | 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 | ||
4a4e912b | 9 | #define KOMODO_ENABLE_INTEREST //enabling this is a hardfork, activate with new RR method |
10 | ||
561e9e9d | 11 | #include "compressor.h" |
046392dc | 12 | #include "memusage.h" |
a0fa20a1 PW |
13 | #include "serialize.h" |
14 | #include "uint256.h" | |
15 | ||
16 | #include <assert.h> | |
17 | #include <stdint.h> | |
18 | ||
19 | #include <boost/foreach.hpp> | |
bc42503f | 20 | #include <boost/unordered_map.hpp> |
e1ff849d | 21 | #include "zcash/IncrementalMerkleTree.hpp" |
a0fa20a1 | 22 | |
fa94b9d5 MF |
23 | /** |
24 | * Pruned version of CTransaction: only retains metadata and unspent transaction outputs | |
a0fa20a1 PW |
25 | * |
26 | * Serialized format: | |
27 | * - VARINT(nVersion) | |
28 | * - VARINT(nCode) | |
29 | * - unspentness bitvector, for vout[2] and further; least significant byte first | |
30 | * - the non-spent CTxOuts (via CTxOutCompressor) | |
31 | * - VARINT(nHeight) | |
32 | * | |
33 | * The nCode value consists of: | |
34 | * - bit 1: IsCoinBase() | |
35 | * - bit 2: vout[0] is not spent | |
36 | * - bit 4: vout[1] is not spent | |
37 | * - The higher bits encode N, the number of non-zero bytes in the following bitvector. | |
38 | * - In case both bit 2 and bit 4 are unset, they encode N-1, as there must be at | |
39 | * least one non-spent output). | |
40 | * | |
41 | * Example: 0104835800816115944e077fe7c803cfa57f29b36bf87c1d358bb85e | |
42 | * <><><--------------------------------------------><----> | |
43 | * | \ | / | |
44 | * version code vout[1] height | |
45 | * | |
46 | * - version = 1 | |
47 | * - code = 4 (vout[1] is not spent, and 0 non-zero bytes of bitvector follow) | |
48 | * - unspentness bitvector: as 0 non-zero bytes follow, it has length 0 | |
49 | * - vout[1]: 835800816115944e077fe7c803cfa57f29b36bf87c1d35 | |
50 | * * 8358: compact amount representation for 60000000000 (600 BTC) | |
51 | * * 00: special txout type pay-to-pubkey-hash | |
52 | * * 816115944e077fe7c803cfa57f29b36bf87c1d35: address uint160 | |
53 | * - height = 203998 | |
54 | * | |
55 | * | |
56 | * Example: 0109044086ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4eebbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa486af3b | |
57 | * <><><--><--------------------------------------------------><----------------------------------------------><----> | |
58 | * / \ \ | | / | |
59 | * version code unspentness vout[4] vout[16] height | |
60 | * | |
61 | * - version = 1 | |
62 | * - code = 9 (coinbase, neither vout[0] or vout[1] are unspent, | |
63 | * 2 (1, +1 because both bit 2 and bit 4 are unset) non-zero bitvector bytes follow) | |
64 | * - unspentness bitvector: bits 2 (0x04) and 14 (0x4000) are set, so vout[2+2] and vout[14+2] are unspent | |
65 | * - vout[4]: 86ef97d5790061b01caab50f1b8e9c50a5057eb43c2d9563a4ee | |
66 | * * 86ef97d579: compact amount representation for 234925952 (2.35 BTC) | |
67 | * * 00: special txout type pay-to-pubkey-hash | |
68 | * * 61b01caab50f1b8e9c50a5057eb43c2d9563a4ee: address uint160 | |
69 | * - vout[16]: bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4 | |
70 | * * bbd123: compact amount representation for 110397 (0.001 BTC) | |
71 | * * 00: special txout type pay-to-pubkey-hash | |
72 | * * 8c988f1a4a4de2161e0f50aac7f17e7f9555caa4: address uint160 | |
73 | * - height = 120891 | |
74 | */ | |
75 | class CCoins | |
76 | { | |
77 | public: | |
fa94b9d5 | 78 | //! whether transaction is a coinbase |
a0fa20a1 PW |
79 | bool fCoinBase; |
80 | ||
fa94b9d5 | 81 | //! unspent transaction outputs; spent outputs are .IsNull(); spent outputs at the end of the array are dropped |
a0fa20a1 PW |
82 | std::vector<CTxOut> vout; |
83 | ||
fa94b9d5 | 84 | //! at which height this transaction was included in the active block chain |
a0fa20a1 PW |
85 | int nHeight; |
86 | ||
fa94b9d5 MF |
87 | //! version of the CTransaction; accesses to this value should probably check for nHeight as well, |
88 | //! as new tx version will probably only be introduced at certain heights | |
a0fa20a1 | 89 | int nVersion; |
a130c5cb | 90 | //uint32_t nLockTime; |
a0fa20a1 | 91 | |
f28aec01 PW |
92 | void FromTx(const CTransaction &tx, int nHeightIn) { |
93 | fCoinBase = tx.IsCoinBase(); | |
94 | vout = tx.vout; | |
95 | nHeight = nHeightIn; | |
96 | nVersion = tx.nVersion; | |
a130c5cb | 97 | //nLockTime = tx.nLockTime; |
a0fa20a1 PW |
98 | ClearUnspendable(); |
99 | } | |
100 | ||
fa94b9d5 | 101 | //! construct a CCoins from a CTransaction, at a given height |
f28aec01 PW |
102 | CCoins(const CTransaction &tx, int nHeightIn) { |
103 | FromTx(tx, nHeightIn); | |
104 | } | |
105 | ||
106 | void Clear() { | |
107 | fCoinBase = false; | |
108 | std::vector<CTxOut>().swap(vout); | |
109 | nHeight = 0; | |
110 | nVersion = 0; | |
111 | } | |
112 | ||
fa94b9d5 | 113 | //! empty constructor |
a0fa20a1 PW |
114 | CCoins() : fCoinBase(false), vout(0), nHeight(0), nVersion(0) { } |
115 | ||
fa94b9d5 | 116 | //!remove spent outputs at the end of vout |
a0fa20a1 PW |
117 | void Cleanup() { |
118 | while (vout.size() > 0 && vout.back().IsNull()) | |
119 | vout.pop_back(); | |
120 | if (vout.empty()) | |
121 | std::vector<CTxOut>().swap(vout); | |
122 | } | |
123 | ||
124 | void ClearUnspendable() { | |
125 | BOOST_FOREACH(CTxOut &txout, vout) { | |
126 | if (txout.scriptPubKey.IsUnspendable()) | |
127 | txout.SetNull(); | |
128 | } | |
129 | Cleanup(); | |
130 | } | |
131 | ||
132 | void swap(CCoins &to) { | |
133 | std::swap(to.fCoinBase, fCoinBase); | |
134 | to.vout.swap(vout); | |
135 | std::swap(to.nHeight, nHeight); | |
136 | std::swap(to.nVersion, nVersion); | |
137 | } | |
138 | ||
fa94b9d5 | 139 | //! equality test |
a0fa20a1 PW |
140 | friend bool operator==(const CCoins &a, const CCoins &b) { |
141 | // Empty CCoins objects are always equal. | |
142 | if (a.IsPruned() && b.IsPruned()) | |
143 | return true; | |
144 | return a.fCoinBase == b.fCoinBase && | |
145 | a.nHeight == b.nHeight && | |
146 | a.nVersion == b.nVersion && | |
147 | a.vout == b.vout; | |
148 | } | |
149 | friend bool operator!=(const CCoins &a, const CCoins &b) { | |
150 | return !(a == b); | |
151 | } | |
152 | ||
153 | void CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const; | |
154 | ||
155 | bool IsCoinBase() const { | |
156 | return fCoinBase; | |
157 | } | |
158 | ||
159 | unsigned int GetSerializeSize(int nType, int nVersion) const { | |
160 | unsigned int nSize = 0; | |
161 | unsigned int nMaskSize = 0, nMaskCode = 0; | |
162 | CalcMaskSize(nMaskSize, nMaskCode); | |
163 | bool fFirst = vout.size() > 0 && !vout[0].IsNull(); | |
164 | bool fSecond = vout.size() > 1 && !vout[1].IsNull(); | |
165 | assert(fFirst || fSecond || nMaskCode); | |
166 | unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0); | |
167 | // version | |
168 | nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion); | |
169 | // size of header code | |
170 | nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion); | |
171 | // spentness bitmask | |
172 | nSize += nMaskSize; | |
173 | // txouts themself | |
174 | for (unsigned int i = 0; i < vout.size(); i++) | |
175 | if (!vout[i].IsNull()) | |
176 | nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion); | |
177 | // height | |
178 | nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion); | |
179 | return nSize; | |
180 | } | |
181 | ||
182 | template<typename Stream> | |
183 | void Serialize(Stream &s, int nType, int nVersion) const { | |
184 | unsigned int nMaskSize = 0, nMaskCode = 0; | |
185 | CalcMaskSize(nMaskSize, nMaskCode); | |
186 | bool fFirst = vout.size() > 0 && !vout[0].IsNull(); | |
187 | bool fSecond = vout.size() > 1 && !vout[1].IsNull(); | |
188 | assert(fFirst || fSecond || nMaskCode); | |
189 | unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0); | |
190 | // version | |
191 | ::Serialize(s, VARINT(this->nVersion), nType, nVersion); | |
192 | // header code | |
193 | ::Serialize(s, VARINT(nCode), nType, nVersion); | |
194 | // spentness bitmask | |
195 | for (unsigned int b = 0; b<nMaskSize; b++) { | |
196 | unsigned char chAvail = 0; | |
197 | for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++) | |
198 | if (!vout[2+b*8+i].IsNull()) | |
199 | chAvail |= (1 << i); | |
200 | ::Serialize(s, chAvail, nType, nVersion); | |
201 | } | |
202 | // txouts themself | |
203 | for (unsigned int i = 0; i < vout.size(); i++) { | |
204 | if (!vout[i].IsNull()) | |
205 | ::Serialize(s, CTxOutCompressor(REF(vout[i])), nType, nVersion); | |
206 | } | |
207 | // coinbase height | |
208 | ::Serialize(s, VARINT(nHeight), nType, nVersion); | |
209 | } | |
210 | ||
211 | template<typename Stream> | |
212 | void Unserialize(Stream &s, int nType, int nVersion) { | |
213 | unsigned int nCode = 0; | |
214 | // version | |
215 | ::Unserialize(s, VARINT(this->nVersion), nType, nVersion); | |
216 | // header code | |
217 | ::Unserialize(s, VARINT(nCode), nType, nVersion); | |
218 | fCoinBase = nCode & 1; | |
219 | std::vector<bool> vAvail(2, false); | |
8d657a65 E |
220 | vAvail[0] = (nCode & 2) != 0; |
221 | vAvail[1] = (nCode & 4) != 0; | |
a0fa20a1 PW |
222 | unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1); |
223 | // spentness bitmask | |
224 | while (nMaskCode > 0) { | |
225 | unsigned char chAvail = 0; | |
226 | ::Unserialize(s, chAvail, nType, nVersion); | |
227 | for (unsigned int p = 0; p < 8; p++) { | |
228 | bool f = (chAvail & (1 << p)) != 0; | |
229 | vAvail.push_back(f); | |
230 | } | |
231 | if (chAvail != 0) | |
232 | nMaskCode--; | |
233 | } | |
234 | // txouts themself | |
235 | vout.assign(vAvail.size(), CTxOut()); | |
236 | for (unsigned int i = 0; i < vAvail.size(); i++) { | |
237 | if (vAvail[i]) | |
238 | ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion); | |
239 | } | |
240 | // coinbase height | |
241 | ::Unserialize(s, VARINT(nHeight), nType, nVersion); | |
242 | Cleanup(); | |
243 | } | |
244 | ||
fa94b9d5 | 245 | //! mark a vout spent |
c444c620 | 246 | bool Spend(uint32_t nPos); |
a0fa20a1 | 247 | |
fa94b9d5 | 248 | //! check whether a particular output is still available |
a0fa20a1 PW |
249 | bool IsAvailable(unsigned int nPos) const { |
250 | return (nPos < vout.size() && !vout[nPos].IsNull()); | |
251 | } | |
252 | ||
fa94b9d5 MF |
253 | //! check whether the entire CCoins is spent |
254 | //! note that only !IsPruned() CCoins can be serialized | |
a0fa20a1 PW |
255 | bool IsPruned() const { |
256 | BOOST_FOREACH(const CTxOut &out, vout) | |
257 | if (!out.IsNull()) | |
258 | return false; | |
259 | return true; | |
260 | } | |
046392dc PW |
261 | |
262 | size_t DynamicMemoryUsage() const { | |
263 | size_t ret = memusage::DynamicUsage(vout); | |
264 | BOOST_FOREACH(const CTxOut &out, vout) { | |
265 | const std::vector<unsigned char> *script = &out.scriptPubKey; | |
266 | ret += memusage::DynamicUsage(*script); | |
267 | } | |
268 | return ret; | |
269 | } | |
a0fa20a1 PW |
270 | }; |
271 | ||
bc42503f PW |
272 | class CCoinsKeyHasher |
273 | { | |
274 | private: | |
275 | uint256 salt; | |
276 | ||
277 | public: | |
278 | CCoinsKeyHasher(); | |
fa94b9d5 MF |
279 | |
280 | /** | |
281 | * This *must* return size_t. With Boost 1.46 on 32-bit systems the | |
282 | * unordered_map will behave unpredictably if the custom hasher returns a | |
283 | * uint64_t, resulting in failures when syncing the chain (#4634). | |
284 | */ | |
6c23b082 | 285 | size_t operator()(const uint256& key) const { |
bc42503f PW |
286 | return key.GetHash(salt); |
287 | } | |
288 | }; | |
289 | ||
058b08c1 PW |
290 | struct CCoinsCacheEntry |
291 | { | |
292 | CCoins coins; // The actual cached data. | |
293 | unsigned char flags; | |
294 | ||
295 | enum Flags { | |
296 | DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view. | |
297 | FRESH = (1 << 1), // The parent view does not have this entry (or it is pruned). | |
298 | }; | |
299 | ||
300 | CCoinsCacheEntry() : coins(), flags(0) {} | |
301 | }; | |
302 | ||
9f25631d SB |
303 | struct CAnchorsCacheEntry |
304 | { | |
305 | bool entered; // This will be false if the anchor is removed from the cache | |
434f3284 | 306 | ZCIncrementalMerkleTree tree; // The tree itself |
9f25631d SB |
307 | unsigned char flags; |
308 | ||
309 | enum Flags { | |
310 | DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view. | |
311 | }; | |
312 | ||
434f3284 | 313 | CAnchorsCacheEntry() : entered(false), flags(0) {} |
9f25631d SB |
314 | }; |
315 | ||
9e511dbb | 316 | struct CNullifiersCacheEntry |
45d6bee9 | 317 | { |
9e511dbb | 318 | bool entered; // If the nullifier is spent or not |
45d6bee9 SB |
319 | unsigned char flags; |
320 | ||
321 | enum Flags { | |
322 | DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view. | |
323 | }; | |
324 | ||
9e511dbb | 325 | CNullifiersCacheEntry() : entered(false), flags(0) {} |
45d6bee9 SB |
326 | }; |
327 | ||
058b08c1 | 328 | typedef boost::unordered_map<uint256, CCoinsCacheEntry, CCoinsKeyHasher> CCoinsMap; |
9f25631d | 329 | typedef boost::unordered_map<uint256, CAnchorsCacheEntry, CCoinsKeyHasher> CAnchorsMap; |
9e511dbb | 330 | typedef boost::unordered_map<uint256, CNullifiersCacheEntry, CCoinsKeyHasher> CNullifiersMap; |
a0fa20a1 PW |
331 | |
332 | struct CCoinsStats | |
333 | { | |
334 | int nHeight; | |
335 | uint256 hashBlock; | |
336 | uint64_t nTransactions; | |
337 | uint64_t nTransactionOutputs; | |
338 | uint64_t nSerializedSize; | |
339 | uint256 hashSerialized; | |
a372168e | 340 | CAmount nTotalAmount; |
a0fa20a1 | 341 | |
4f152496 | 342 | CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), nTotalAmount(0) {} |
a0fa20a1 PW |
343 | }; |
344 | ||
345 | ||
346 | /** Abstract view on the open txout dataset. */ | |
347 | class CCoinsView | |
348 | { | |
349 | public: | |
9f25631d | 350 | //! Retrieve the tree at a particular anchored root in the chain |
434f3284 | 351 | virtual bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const; |
9f25631d | 352 | |
9e511dbb SB |
353 | //! Determine whether a nullifier is spent or not |
354 | virtual bool GetNullifier(const uint256 &nullifier) const; | |
45d6bee9 | 355 | |
fa94b9d5 | 356 | //! Retrieve the CCoins (unspent transaction outputs) for a given txid |
a3dc587a | 357 | virtual bool GetCoins(const uint256 &txid, CCoins &coins) const; |
a0fa20a1 | 358 | |
fa94b9d5 MF |
359 | //! Just check whether we have data for a given txid. |
360 | //! This may (but cannot always) return true for fully spent transactions | |
a3dc587a | 361 | virtual bool HaveCoins(const uint256 &txid) const; |
a0fa20a1 | 362 | |
fa94b9d5 | 363 | //! Retrieve the block hash whose state this CCoinsView currently represents |
a3dc587a | 364 | virtual uint256 GetBestBlock() const; |
a0fa20a1 | 365 | |
9f25631d SB |
366 | //! Get the current "tip" or the latest anchored tree root in the chain |
367 | virtual uint256 GetBestAnchor() const; | |
368 | ||
fa94b9d5 MF |
369 | //! Do a bulk modification (multiple CCoins changes + BestBlock change). |
370 | //! The passed mapCoins can be modified. | |
9f25631d SB |
371 | virtual bool BatchWrite(CCoinsMap &mapCoins, |
372 | const uint256 &hashBlock, | |
373 | const uint256 &hashAnchor, | |
45d6bee9 | 374 | CAnchorsMap &mapAnchors, |
bb64be52 | 375 | CNullifiersMap &mapNullifiers); |
a0fa20a1 | 376 | |
fa94b9d5 | 377 | //! Calculate statistics about the unspent transaction output set |
a3dc587a | 378 | virtual bool GetStats(CCoinsStats &stats) const; |
a0fa20a1 | 379 | |
fa94b9d5 | 380 | //! As we use CCoinsViews polymorphically, have a virtual destructor |
a0fa20a1 PW |
381 | virtual ~CCoinsView() {} |
382 | }; | |
383 | ||
384 | ||
385 | /** CCoinsView backed by another CCoinsView */ | |
386 | class CCoinsViewBacked : public CCoinsView | |
387 | { | |
388 | protected: | |
389 | CCoinsView *base; | |
390 | ||
391 | public: | |
7c70438d | 392 | CCoinsViewBacked(CCoinsView *viewIn); |
434f3284 | 393 | bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const; |
9e511dbb | 394 | bool GetNullifier(const uint256 &nullifier) const; |
a3dc587a | 395 | bool GetCoins(const uint256 &txid, CCoins &coins) const; |
a3dc587a DK |
396 | bool HaveCoins(const uint256 &txid) const; |
397 | uint256 GetBestBlock() const; | |
9f25631d | 398 | uint256 GetBestAnchor() const; |
a0fa20a1 | 399 | void SetBackend(CCoinsView &viewIn); |
9f25631d SB |
400 | bool BatchWrite(CCoinsMap &mapCoins, |
401 | const uint256 &hashBlock, | |
402 | const uint256 &hashAnchor, | |
45d6bee9 | 403 | CAnchorsMap &mapAnchors, |
bb64be52 | 404 | CNullifiersMap &mapNullifiers); |
a3dc587a | 405 | bool GetStats(CCoinsStats &stats) const; |
a0fa20a1 PW |
406 | }; |
407 | ||
408 | ||
f28aec01 PW |
409 | class CCoinsViewCache; |
410 | ||
fa94b9d5 MF |
411 | /** |
412 | * A reference to a mutable cache entry. Encapsulating it allows us to run | |
f28aec01 | 413 | * cleanup code after the modification is finished, and keeping track of |
fa94b9d5 MF |
414 | * concurrent modifications. |
415 | */ | |
f28aec01 PW |
416 | class CCoinsModifier |
417 | { | |
418 | private: | |
419 | CCoinsViewCache& cache; | |
420 | CCoinsMap::iterator it; | |
046392dc PW |
421 | size_t cachedCoinUsage; // Cached memory usage of the CCoins object before modification |
422 | CCoinsModifier(CCoinsViewCache& cache_, CCoinsMap::iterator it_, size_t usage); | |
f28aec01 PW |
423 | |
424 | public: | |
058b08c1 PW |
425 | CCoins* operator->() { return &it->second.coins; } |
426 | CCoins& operator*() { return it->second.coins; } | |
f28aec01 PW |
427 | ~CCoinsModifier(); |
428 | friend class CCoinsViewCache; | |
429 | }; | |
430 | ||
a0fa20a1 PW |
431 | /** CCoinsView that adds a memory cache for transactions to another CCoinsView */ |
432 | class CCoinsViewCache : public CCoinsViewBacked | |
433 | { | |
434 | protected: | |
f28aec01 PW |
435 | /* Whether this cache has an active modifier. */ |
436 | bool hasModifier; | |
a3dc587a | 437 | |
046392dc | 438 | |
fa94b9d5 MF |
439 | /** |
440 | * Make mutable so that we can "fill the cache" even from Get-methods | |
441 | * declared as "const". | |
442 | */ | |
a3dc587a DK |
443 | mutable uint256 hashBlock; |
444 | mutable CCoinsMap cacheCoins; | |
9f25631d SB |
445 | mutable uint256 hashAnchor; |
446 | mutable CAnchorsMap cacheAnchors; | |
1d184d53 | 447 | mutable CNullifiersMap cacheNullifiers; |
a0fa20a1 | 448 | |
046392dc PW |
449 | /* Cached dynamic memory usage for the inner CCoins objects. */ |
450 | mutable size_t cachedCoinsUsage; | |
451 | ||
a0fa20a1 | 452 | public: |
7c70438d | 453 | CCoinsViewCache(CCoinsView *baseIn); |
f28aec01 | 454 | ~CCoinsViewCache(); |
a0fa20a1 PW |
455 | |
456 | // Standard CCoinsView methods | |
434f3284 | 457 | bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const; |
9e511dbb | 458 | bool GetNullifier(const uint256 &nullifier) const; |
a3dc587a | 459 | bool GetCoins(const uint256 &txid, CCoins &coins) const; |
a3dc587a DK |
460 | bool HaveCoins(const uint256 &txid) const; |
461 | uint256 GetBestBlock() const; | |
9f25631d | 462 | uint256 GetBestAnchor() const; |
c9d1a81c | 463 | void SetBestBlock(const uint256 &hashBlock); |
9f25631d SB |
464 | bool BatchWrite(CCoinsMap &mapCoins, |
465 | const uint256 &hashBlock, | |
466 | const uint256 &hashAnchor, | |
45d6bee9 | 467 | CAnchorsMap &mapAnchors, |
bb64be52 | 468 | CNullifiersMap &mapNullifiers); |
9f25631d SB |
469 | |
470 | ||
471 | // Adds the tree to mapAnchors and sets the current commitment | |
472 | // root to this root. | |
434f3284 | 473 | void PushAnchor(const ZCIncrementalMerkleTree &tree); |
9f25631d SB |
474 | |
475 | // Removes the current commitment root from mapAnchors and sets | |
476 | // the new current root. | |
477 | void PopAnchor(const uint256 &rt); | |
a0fa20a1 | 478 | |
9e511dbb SB |
479 | // Marks a nullifier as spent or not. |
480 | void SetNullifier(const uint256 &nullifier, bool spent); | |
45d6bee9 | 481 | |
fa94b9d5 MF |
482 | /** |
483 | * Return a pointer to CCoins in the cache, or NULL if not found. This is | |
484 | * more efficient than GetCoins. Modifications to other cache entries are | |
485 | * allowed while accessing the returned pointer. | |
486 | */ | |
629d75fa PW |
487 | const CCoins* AccessCoins(const uint256 &txid) const; |
488 | ||
fa94b9d5 MF |
489 | /** |
490 | * Return a modifiable reference to a CCoins. If no entry with the given | |
491 | * txid exists, a new one is created. Simultaneous modifications are not | |
492 | * allowed. | |
493 | */ | |
f28aec01 | 494 | CCoinsModifier ModifyCoins(const uint256 &txid); |
a0fa20a1 | 495 | |
fa94b9d5 MF |
496 | /** |
497 | * Push the modifications applied to this cache to its base. | |
498 | * Failure to call this method before destruction will cause the changes to be forgotten. | |
499 | * If false is returned, the state of this cache (and its backing view) will be undefined. | |
500 | */ | |
a0fa20a1 PW |
501 | bool Flush(); |
502 | ||
fa94b9d5 | 503 | //! Calculate the size of the cache (in number of transactions) |
a3dc587a | 504 | unsigned int GetCacheSize() const; |
a0fa20a1 | 505 | |
046392dc PW |
506 | //! Calculate the size of the cache (in bytes) |
507 | size_t DynamicMemoryUsage() const; | |
508 | ||
fa94b9d5 MF |
509 | /** |
510 | * Amount of bitcoins coming in to a transaction | |
511 | * Note that lightweight clients may not know anything besides the hash of previous transactions, | |
512 | * so may not be able to calculate this. | |
513 | * | |
514 | * @param[in] tx transaction for which we are checking input total | |
515 | * @return Sum of value of all inputs (scriptSigs) | |
a0fa20a1 | 516 | */ |
17878015 | 517 | CAmount GetValueIn(int32_t nHeight,int64_t *interestp,const CTransaction& tx,uint32_t prevblocktime) const; |
a0fa20a1 | 518 | |
fa94b9d5 | 519 | //! Check whether all prevouts of the transaction are present in the UTXO set represented by this view |
a3dc587a | 520 | bool HaveInputs(const CTransaction& tx) const; |
a0fa20a1 | 521 | |
9e511dbb | 522 | //! Check whether all joinsplit requirements (anchors/nullifiers) are satisfied |
ee964faf | 523 | bool HaveJoinSplitRequirements(const CTransaction& tx) const; |
a8ac403d | 524 | |
fa94b9d5 | 525 | //! Return priority of tx at height nHeight |
a3dc587a | 526 | double GetPriority(const CTransaction &tx, int nHeight) const; |
4d707d51 | 527 | |
a3dc587a | 528 | const CTxOut &GetOutputFor(const CTxIn& input) const; |
54b9f8e8 | 529 | const CScript &GetSpendFor(const CTxIn& input) const; |
a0fa20a1 | 530 | |
f28aec01 PW |
531 | friend class CCoinsModifier; |
532 | ||
a0fa20a1 | 533 | private: |
dd638dd7 | 534 | CCoinsMap::iterator FetchCoins(const uint256 &txid); |
a3dc587a | 535 | CCoinsMap::const_iterator FetchCoins(const uint256 &txid) const; |
228d2385 LD |
536 | |
537 | /** | |
538 | * By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache. | |
539 | */ | |
540 | CCoinsViewCache(const CCoinsViewCache &); | |
a0fa20a1 PW |
541 | }; |
542 | ||
093303a8 | 543 | #endif // BITCOIN_COINS_H |