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 uint256
CCoinsView::GetBestBlock() const { return uint256(); }
15 std::vector
<uint256
> CCoinsView::GetHeadBlocks() const { return std::vector
<uint256
>(); }
16 bool CCoinsView::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return false; }
17 CCoinsViewCursor
*CCoinsView::Cursor() const { return nullptr; }
19 bool CCoinsView::HaveCoin(const COutPoint
&outpoint
) const
22 return GetCoin(outpoint
, coin
);
25 CCoinsViewBacked::CCoinsViewBacked(CCoinsView
*viewIn
) : base(viewIn
) { }
26 bool CCoinsViewBacked::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const { return base
->GetCoin(outpoint
, coin
); }
27 bool CCoinsViewBacked::HaveCoin(const COutPoint
&outpoint
) const { return base
->HaveCoin(outpoint
); }
28 uint256
CCoinsViewBacked::GetBestBlock() const { return base
->GetBestBlock(); }
29 std::vector
<uint256
> CCoinsViewBacked::GetHeadBlocks() const { return base
->GetHeadBlocks(); }
30 void CCoinsViewBacked::SetBackend(CCoinsView
&viewIn
) { base
= &viewIn
; }
31 bool CCoinsViewBacked::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return base
->BatchWrite(mapCoins
, hashBlock
); }
32 CCoinsViewCursor
*CCoinsViewBacked::Cursor() const { return base
->Cursor(); }
33 size_t CCoinsViewBacked::EstimateSize() const { return base
->EstimateSize(); }
35 SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits
<uint64_t>::max())), k1(GetRand(std::numeric_limits
<uint64_t>::max())) {}
37 CCoinsViewCache::CCoinsViewCache(CCoinsView
*baseIn
) : CCoinsViewBacked(baseIn
), cachedCoinsUsage(0) {}
39 size_t CCoinsViewCache::DynamicMemoryUsage() const {
40 return memusage::DynamicUsage(cacheCoins
) + cachedCoinsUsage
;
43 CCoinsMap::iterator
CCoinsViewCache::FetchCoin(const COutPoint
&outpoint
) const {
44 CCoinsMap::iterator it
= cacheCoins
.find(outpoint
);
45 if (it
!= cacheCoins
.end())
48 if (!base
->GetCoin(outpoint
, tmp
))
49 return cacheCoins
.end();
50 CCoinsMap::iterator ret
= cacheCoins
.emplace(std::piecewise_construct
, std::forward_as_tuple(outpoint
), std::forward_as_tuple(std::move(tmp
))).first
;
51 if (ret
->second
.coin
.IsSpent()) {
52 // The parent only has an empty entry for this outpoint; we can consider our
54 ret
->second
.flags
= CCoinsCacheEntry::FRESH
;
56 cachedCoinsUsage
+= ret
->second
.coin
.DynamicMemoryUsage();
60 bool CCoinsViewCache::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const {
61 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
62 if (it
!= cacheCoins
.end()) {
63 coin
= it
->second
.coin
;
64 return !coin
.IsSpent();
69 void CCoinsViewCache::AddCoin(const COutPoint
&outpoint
, Coin
&& coin
, bool possible_overwrite
) {
70 assert(!coin
.IsSpent());
71 if (coin
.out
.scriptPubKey
.IsUnspendable()) return;
72 CCoinsMap::iterator it
;
74 std::tie(it
, inserted
) = cacheCoins
.emplace(std::piecewise_construct
, std::forward_as_tuple(outpoint
), std::tuple
<>());
77 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
79 if (!possible_overwrite
) {
80 if (!it
->second
.coin
.IsSpent()) {
81 throw std::logic_error("Adding new coin that replaces non-pruned entry");
83 fresh
= !(it
->second
.flags
& CCoinsCacheEntry::DIRTY
);
85 it
->second
.coin
= std::move(coin
);
86 it
->second
.flags
|= CCoinsCacheEntry::DIRTY
| (fresh
? CCoinsCacheEntry::FRESH
: 0);
87 cachedCoinsUsage
+= it
->second
.coin
.DynamicMemoryUsage();
90 void AddCoins(CCoinsViewCache
& cache
, const CTransaction
&tx
, int nHeight
, bool check
) {
91 bool fCoinbase
= tx
.IsCoinBase();
92 const uint256
& txid
= tx
.GetHash();
93 for (size_t i
= 0; i
< tx
.vout
.size(); ++i
) {
94 bool overwrite
= check
? cache
.HaveCoin(COutPoint(txid
, i
)) : fCoinbase
;
95 // Always set the possible_overwrite flag to AddCoin for coinbase txn, in order to correctly
96 // deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
97 cache
.AddCoin(COutPoint(txid
, i
), Coin(tx
.vout
[i
], nHeight
, fCoinbase
), overwrite
);
101 bool CCoinsViewCache::SpendCoin(const COutPoint
&outpoint
, Coin
* moveout
) {
102 CCoinsMap::iterator it
= FetchCoin(outpoint
);
103 if (it
== cacheCoins
.end()) return false;
104 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
106 *moveout
= std::move(it
->second
.coin
);
108 if (it
->second
.flags
& CCoinsCacheEntry::FRESH
) {
109 cacheCoins
.erase(it
);
111 it
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
112 it
->second
.coin
.Clear();
117 static const Coin coinEmpty
;
119 const Coin
& CCoinsViewCache::AccessCoin(const COutPoint
&outpoint
) const {
120 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
121 if (it
== cacheCoins
.end()) {
124 return it
->second
.coin
;
128 bool CCoinsViewCache::HaveCoin(const COutPoint
&outpoint
) const {
129 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
130 return (it
!= cacheCoins
.end() && !it
->second
.coin
.IsSpent());
133 bool CCoinsViewCache::HaveCoinInCache(const COutPoint
&outpoint
) const {
134 CCoinsMap::const_iterator it
= cacheCoins
.find(outpoint
);
135 return (it
!= cacheCoins
.end() && !it
->second
.coin
.IsSpent());
138 uint256
CCoinsViewCache::GetBestBlock() const {
139 if (hashBlock
.IsNull())
140 hashBlock
= base
->GetBestBlock();
144 void CCoinsViewCache::SetBestBlock(const uint256
&hashBlockIn
) {
145 hashBlock
= hashBlockIn
;
148 bool CCoinsViewCache::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlockIn
) {
149 for (CCoinsMap::iterator it
= mapCoins
.begin(); it
!= mapCoins
.end();) {
150 if (it
->second
.flags
& CCoinsCacheEntry::DIRTY
) { // Ignore non-dirty entries (optimization).
151 CCoinsMap::iterator itUs
= cacheCoins
.find(it
->first
);
152 if (itUs
== cacheCoins
.end()) {
153 // The parent cache does not have an entry, while the child does
154 // We can ignore it if it's both FRESH and pruned in the child
155 if (!(it
->second
.flags
& CCoinsCacheEntry::FRESH
&& it
->second
.coin
.IsSpent())) {
156 // Otherwise we will need to create it in the parent
157 // and move the data up and mark it as dirty
158 CCoinsCacheEntry
& entry
= cacheCoins
[it
->first
];
159 entry
.coin
= std::move(it
->second
.coin
);
160 cachedCoinsUsage
+= entry
.coin
.DynamicMemoryUsage();
161 entry
.flags
= CCoinsCacheEntry::DIRTY
;
162 // We can mark it FRESH in the parent if it was FRESH in the child
163 // Otherwise it might have just been flushed from the parent's cache
164 // and already exist in the grandparent
165 if (it
->second
.flags
& CCoinsCacheEntry::FRESH
)
166 entry
.flags
|= CCoinsCacheEntry::FRESH
;
169 // Assert that the child cache entry was not marked FRESH if the
170 // parent cache entry has unspent outputs. If this ever happens,
171 // it means the FRESH flag was misapplied and there is a logic
172 // error in the calling code.
173 if ((it
->second
.flags
& CCoinsCacheEntry::FRESH
) && !itUs
->second
.coin
.IsSpent())
174 throw std::logic_error("FRESH flag misapplied to cache entry for base transaction with spendable outputs");
176 // Found the entry in the parent cache
177 if ((itUs
->second
.flags
& CCoinsCacheEntry::FRESH
) && it
->second
.coin
.IsSpent()) {
178 // The grandparent does not have an entry, and the child is
179 // modified and being pruned. This means we can just delete
180 // it from the parent.
181 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
182 cacheCoins
.erase(itUs
);
184 // A normal modification.
185 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
186 itUs
->second
.coin
= std::move(it
->second
.coin
);
187 cachedCoinsUsage
+= itUs
->second
.coin
.DynamicMemoryUsage();
188 itUs
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
189 // NOTE: It is possible the child has a FRESH flag here in
190 // the event the entry we found in the parent is pruned. But
191 // we must not copy that FRESH flag to the parent as that
192 // pruned state likely still needs to be communicated to the
197 CCoinsMap::iterator itOld
= it
++;
198 mapCoins
.erase(itOld
);
200 hashBlock
= hashBlockIn
;
204 bool CCoinsViewCache::Flush() {
205 bool fOk
= base
->BatchWrite(cacheCoins
, hashBlock
);
207 cachedCoinsUsage
= 0;
211 void CCoinsViewCache::Uncache(const COutPoint
& hash
)
213 CCoinsMap::iterator it
= cacheCoins
.find(hash
);
214 if (it
!= cacheCoins
.end() && it
->second
.flags
== 0) {
215 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
216 cacheCoins
.erase(it
);
220 unsigned int CCoinsViewCache::GetCacheSize() const {
221 return cacheCoins
.size();
224 CAmount
CCoinsViewCache::GetValueIn(const CTransaction
& tx
) const
230 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
231 nResult
+= AccessCoin(tx
.vin
[i
].prevout
).out
.nValue
;
236 bool CCoinsViewCache::HaveInputs(const CTransaction
& tx
) const
238 if (!tx
.IsCoinBase()) {
239 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++) {
240 if (!HaveCoin(tx
.vin
[i
].prevout
)) {
248 static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT
= WITNESS_SCALE_FACTOR
* ::GetSerializeSize(CTxOut(), SER_NETWORK
, PROTOCOL_VERSION
);
249 static const size_t MAX_OUTPUTS_PER_BLOCK
= MAX_BLOCK_WEIGHT
/ MIN_TRANSACTION_OUTPUT_WEIGHT
;
251 const Coin
& AccessByTxid(const CCoinsViewCache
& view
, const uint256
& txid
)
253 COutPoint
iter(txid
, 0);
254 while (iter
.n
< MAX_OUTPUTS_PER_BLOCK
) {
255 const Coin
& alternate
= view
.AccessCoin(iter
);
256 if (!alternate
.IsSpent()) return alternate
;