1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "consensus/consensus.h"
13 bool CCoinsView::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const { return false; }
14 bool CCoinsView::HaveCoin(const COutPoint
&outpoint
) const { return false; }
15 uint256
CCoinsView::GetBestBlock() const { return uint256(); }
16 bool CCoinsView::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return false; }
17 CCoinsViewCursor
*CCoinsView::Cursor() const { return 0; }
20 CCoinsViewBacked::CCoinsViewBacked(CCoinsView
*viewIn
) : base(viewIn
) { }
21 bool CCoinsViewBacked::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const { return base
->GetCoin(outpoint
, coin
); }
22 bool CCoinsViewBacked::HaveCoin(const COutPoint
&outpoint
) const { return base
->HaveCoin(outpoint
); }
23 uint256
CCoinsViewBacked::GetBestBlock() const { return base
->GetBestBlock(); }
24 void CCoinsViewBacked::SetBackend(CCoinsView
&viewIn
) { base
= &viewIn
; }
25 bool CCoinsViewBacked::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return base
->BatchWrite(mapCoins
, hashBlock
); }
26 CCoinsViewCursor
*CCoinsViewBacked::Cursor() const { return base
->Cursor(); }
27 size_t CCoinsViewBacked::EstimateSize() const { return base
->EstimateSize(); }
29 SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits
<uint64_t>::max())), k1(GetRand(std::numeric_limits
<uint64_t>::max())) {}
31 CCoinsViewCache::CCoinsViewCache(CCoinsView
*baseIn
) : CCoinsViewBacked(baseIn
), cachedCoinsUsage(0) {}
33 size_t CCoinsViewCache::DynamicMemoryUsage() const {
34 return memusage::DynamicUsage(cacheCoins
) + cachedCoinsUsage
;
37 CCoinsMap::iterator
CCoinsViewCache::FetchCoin(const COutPoint
&outpoint
) const {
38 CCoinsMap::iterator it
= cacheCoins
.find(outpoint
);
39 if (it
!= cacheCoins
.end())
42 if (!base
->GetCoin(outpoint
, tmp
))
43 return cacheCoins
.end();
44 CCoinsMap::iterator ret
= cacheCoins
.emplace(std::piecewise_construct
, std::forward_as_tuple(outpoint
), std::forward_as_tuple(std::move(tmp
))).first
;
45 if (ret
->second
.coin
.IsSpent()) {
46 // The parent only has an empty entry for this outpoint; we can consider our
48 ret
->second
.flags
= CCoinsCacheEntry::FRESH
;
50 cachedCoinsUsage
+= ret
->second
.coin
.DynamicMemoryUsage();
54 bool CCoinsViewCache::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const {
55 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
56 if (it
!= cacheCoins
.end()) {
57 coin
= it
->second
.coin
;
63 void CCoinsViewCache::AddCoin(const COutPoint
&outpoint
, Coin
&& coin
, bool possible_overwrite
) {
64 assert(!coin
.IsSpent());
65 if (coin
.out
.scriptPubKey
.IsUnspendable()) return;
66 CCoinsMap::iterator it
;
68 std::tie(it
, inserted
) = cacheCoins
.emplace(std::piecewise_construct
, std::forward_as_tuple(outpoint
), std::tuple
<>());
71 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
73 if (!possible_overwrite
) {
74 if (!it
->second
.coin
.IsSpent()) {
75 throw std::logic_error("Adding new coin that replaces non-pruned entry");
77 fresh
= !(it
->second
.flags
& CCoinsCacheEntry::DIRTY
);
79 it
->second
.coin
= std::move(coin
);
80 it
->second
.flags
|= CCoinsCacheEntry::DIRTY
| (fresh
? CCoinsCacheEntry::FRESH
: 0);
81 cachedCoinsUsage
+= it
->second
.coin
.DynamicMemoryUsage();
84 void AddCoins(CCoinsViewCache
& cache
, const CTransaction
&tx
, int nHeight
) {
85 bool fCoinbase
= tx
.IsCoinBase();
86 const uint256
& txid
= tx
.GetHash();
87 for (size_t i
= 0; i
< tx
.vout
.size(); ++i
) {
88 // Pass fCoinbase as the possible_overwrite flag to AddCoin, in order to correctly
89 // deal with the pre-BIP30 occurrances of duplicate coinbase transactions.
90 cache
.AddCoin(COutPoint(txid
, i
), Coin(tx
.vout
[i
], nHeight
, fCoinbase
), fCoinbase
);
94 void CCoinsViewCache::SpendCoin(const COutPoint
&outpoint
, Coin
* moveout
) {
95 CCoinsMap::iterator it
= FetchCoin(outpoint
);
96 if (it
== cacheCoins
.end()) return;
97 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
99 *moveout
= std::move(it
->second
.coin
);
101 if (it
->second
.flags
& CCoinsCacheEntry::FRESH
) {
102 cacheCoins
.erase(it
);
104 it
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
105 it
->second
.coin
.Clear();
109 static const Coin coinEmpty
;
111 const Coin
& CCoinsViewCache::AccessCoin(const COutPoint
&outpoint
) const {
112 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
113 if (it
== cacheCoins
.end()) {
116 return it
->second
.coin
;
120 bool CCoinsViewCache::HaveCoin(const COutPoint
&outpoint
) const {
121 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
122 return (it
!= cacheCoins
.end() && !it
->second
.coin
.IsSpent());
125 bool CCoinsViewCache::HaveCoinInCache(const COutPoint
&outpoint
) const {
126 CCoinsMap::const_iterator it
= cacheCoins
.find(outpoint
);
127 return it
!= cacheCoins
.end();
130 uint256
CCoinsViewCache::GetBestBlock() const {
131 if (hashBlock
.IsNull())
132 hashBlock
= base
->GetBestBlock();
136 void CCoinsViewCache::SetBestBlock(const uint256
&hashBlockIn
) {
137 hashBlock
= hashBlockIn
;
140 bool CCoinsViewCache::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlockIn
) {
141 for (CCoinsMap::iterator it
= mapCoins
.begin(); it
!= mapCoins
.end();) {
142 if (it
->second
.flags
& CCoinsCacheEntry::DIRTY
) { // Ignore non-dirty entries (optimization).
143 CCoinsMap::iterator itUs
= cacheCoins
.find(it
->first
);
144 if (itUs
== cacheCoins
.end()) {
145 // The parent cache does not have an entry, while the child does
146 // We can ignore it if it's both FRESH and pruned in the child
147 if (!(it
->second
.flags
& CCoinsCacheEntry::FRESH
&& it
->second
.coin
.IsSpent())) {
148 // Otherwise we will need to create it in the parent
149 // and move the data up and mark it as dirty
150 CCoinsCacheEntry
& entry
= cacheCoins
[it
->first
];
151 entry
.coin
= std::move(it
->second
.coin
);
152 cachedCoinsUsage
+= entry
.coin
.DynamicMemoryUsage();
153 entry
.flags
= CCoinsCacheEntry::DIRTY
;
154 // We can mark it FRESH in the parent if it was FRESH in the child
155 // Otherwise it might have just been flushed from the parent's cache
156 // and already exist in the grandparent
157 if (it
->second
.flags
& CCoinsCacheEntry::FRESH
)
158 entry
.flags
|= CCoinsCacheEntry::FRESH
;
161 // Assert that the child cache entry was not marked FRESH if the
162 // parent cache entry has unspent outputs. If this ever happens,
163 // it means the FRESH flag was misapplied and there is a logic
164 // error in the calling code.
165 if ((it
->second
.flags
& CCoinsCacheEntry::FRESH
) && !itUs
->second
.coin
.IsSpent())
166 throw std::logic_error("FRESH flag misapplied to cache entry for base transaction with spendable outputs");
168 // Found the entry in the parent cache
169 if ((itUs
->second
.flags
& CCoinsCacheEntry::FRESH
) && it
->second
.coin
.IsSpent()) {
170 // The grandparent does not have an entry, and the child is
171 // modified and being pruned. This means we can just delete
172 // it from the parent.
173 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
174 cacheCoins
.erase(itUs
);
176 // A normal modification.
177 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
178 itUs
->second
.coin
= std::move(it
->second
.coin
);
179 cachedCoinsUsage
+= itUs
->second
.coin
.DynamicMemoryUsage();
180 itUs
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
181 // NOTE: It is possible the child has a FRESH flag here in
182 // the event the entry we found in the parent is pruned. But
183 // we must not copy that FRESH flag to the parent as that
184 // pruned state likely still needs to be communicated to the
189 CCoinsMap::iterator itOld
= it
++;
190 mapCoins
.erase(itOld
);
192 hashBlock
= hashBlockIn
;
196 bool CCoinsViewCache::Flush() {
197 bool fOk
= base
->BatchWrite(cacheCoins
, hashBlock
);
199 cachedCoinsUsage
= 0;
203 void CCoinsViewCache::Uncache(const COutPoint
& hash
)
205 CCoinsMap::iterator it
= cacheCoins
.find(hash
);
206 if (it
!= cacheCoins
.end() && it
->second
.flags
== 0) {
207 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
208 cacheCoins
.erase(it
);
212 unsigned int CCoinsViewCache::GetCacheSize() const {
213 return cacheCoins
.size();
216 CAmount
CCoinsViewCache::GetValueIn(const CTransaction
& tx
) const
222 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
223 nResult
+= AccessCoin(tx
.vin
[i
].prevout
).out
.nValue
;
228 bool CCoinsViewCache::HaveInputs(const CTransaction
& tx
) const
230 if (!tx
.IsCoinBase()) {
231 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++) {
232 if (!HaveCoin(tx
.vin
[i
].prevout
)) {
240 static const size_t MAX_OUTPUTS_PER_BLOCK
= MAX_BLOCK_BASE_SIZE
/ ::GetSerializeSize(CTxOut(), SER_NETWORK
, PROTOCOL_VERSION
); // TODO: merge with similar definition in undo.h.
242 const Coin
& AccessByTxid(const CCoinsViewCache
& view
, const uint256
& txid
)
244 COutPoint
iter(txid
, 0);
245 while (iter
.n
< MAX_OUTPUTS_PER_BLOCK
) {
246 const Coin
& alternate
= view
.AccessCoin(iter
);
247 if (!alternate
.IsSpent()) return alternate
;