1 // Copyright (c) 2012-2017 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>
10 bool CCoinsView::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const { return false; }
11 uint256
CCoinsView::GetBestBlock() const { return uint256(); }
12 std::vector
<uint256
> CCoinsView::GetHeadBlocks() const { return std::vector
<uint256
>(); }
13 bool CCoinsView::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return false; }
14 CCoinsViewCursor
*CCoinsView::Cursor() const { return nullptr; }
16 bool CCoinsView::HaveCoin(const COutPoint
&outpoint
) const
19 return GetCoin(outpoint
, coin
);
22 CCoinsViewBacked::CCoinsViewBacked(CCoinsView
*viewIn
) : base(viewIn
) { }
23 bool CCoinsViewBacked::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const { return base
->GetCoin(outpoint
, coin
); }
24 bool CCoinsViewBacked::HaveCoin(const COutPoint
&outpoint
) const { return base
->HaveCoin(outpoint
); }
25 uint256
CCoinsViewBacked::GetBestBlock() const { return base
->GetBestBlock(); }
26 std::vector
<uint256
> CCoinsViewBacked::GetHeadBlocks() const { return base
->GetHeadBlocks(); }
27 void CCoinsViewBacked::SetBackend(CCoinsView
&viewIn
) { base
= &viewIn
; }
28 bool CCoinsViewBacked::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return base
->BatchWrite(mapCoins
, hashBlock
); }
29 CCoinsViewCursor
*CCoinsViewBacked::Cursor() const { return base
->Cursor(); }
30 size_t CCoinsViewBacked::EstimateSize() const { return base
->EstimateSize(); }
32 SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits
<uint64_t>::max())), k1(GetRand(std::numeric_limits
<uint64_t>::max())) {}
34 CCoinsViewCache::CCoinsViewCache(CCoinsView
*baseIn
) : CCoinsViewBacked(baseIn
), cachedCoinsUsage(0) {}
36 size_t CCoinsViewCache::DynamicMemoryUsage() const {
37 return memusage::DynamicUsage(cacheCoins
) + cachedCoinsUsage
;
40 CCoinsMap::iterator
CCoinsViewCache::FetchCoin(const COutPoint
&outpoint
) const {
41 CCoinsMap::iterator it
= cacheCoins
.find(outpoint
);
42 if (it
!= cacheCoins
.end())
45 if (!base
->GetCoin(outpoint
, tmp
))
46 return cacheCoins
.end();
47 CCoinsMap::iterator ret
= cacheCoins
.emplace(std::piecewise_construct
, std::forward_as_tuple(outpoint
), std::forward_as_tuple(std::move(tmp
))).first
;
48 if (ret
->second
.coin
.IsSpent()) {
49 // The parent only has an empty entry for this outpoint; we can consider our
51 ret
->second
.flags
= CCoinsCacheEntry::FRESH
;
53 cachedCoinsUsage
+= ret
->second
.coin
.DynamicMemoryUsage();
57 bool CCoinsViewCache::GetCoin(const COutPoint
&outpoint
, Coin
&coin
) const {
58 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
59 if (it
!= cacheCoins
.end()) {
60 coin
= it
->second
.coin
;
61 return !coin
.IsSpent();
66 void CCoinsViewCache::AddCoin(const COutPoint
&outpoint
, Coin
&& coin
, bool possible_overwrite
) {
67 assert(!coin
.IsSpent());
68 if (coin
.out
.scriptPubKey
.IsUnspendable()) return;
69 CCoinsMap::iterator it
;
71 std::tie(it
, inserted
) = cacheCoins
.emplace(std::piecewise_construct
, std::forward_as_tuple(outpoint
), std::tuple
<>());
74 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
76 if (!possible_overwrite
) {
77 if (!it
->second
.coin
.IsSpent()) {
78 throw std::logic_error("Adding new coin that replaces non-pruned entry");
80 fresh
= !(it
->second
.flags
& CCoinsCacheEntry::DIRTY
);
82 it
->second
.coin
= std::move(coin
);
83 it
->second
.flags
|= CCoinsCacheEntry::DIRTY
| (fresh
? CCoinsCacheEntry::FRESH
: 0);
84 cachedCoinsUsage
+= it
->second
.coin
.DynamicMemoryUsage();
87 void AddCoins(CCoinsViewCache
& cache
, const CTransaction
&tx
, int nHeight
, bool check
) {
88 bool fCoinbase
= tx
.IsCoinBase();
89 const uint256
& txid
= tx
.GetHash();
90 for (size_t i
= 0; i
< tx
.vout
.size(); ++i
) {
91 bool overwrite
= check
? cache
.HaveCoin(COutPoint(txid
, i
)) : fCoinbase
;
92 // Always set the possible_overwrite flag to AddCoin for coinbase txn, in order to correctly
93 // deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
94 cache
.AddCoin(COutPoint(txid
, i
), Coin(tx
.vout
[i
], nHeight
, fCoinbase
), overwrite
);
98 bool CCoinsViewCache::SpendCoin(const COutPoint
&outpoint
, Coin
* moveout
) {
99 CCoinsMap::iterator it
= FetchCoin(outpoint
);
100 if (it
== cacheCoins
.end()) return false;
101 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
103 *moveout
= std::move(it
->second
.coin
);
105 if (it
->second
.flags
& CCoinsCacheEntry::FRESH
) {
106 cacheCoins
.erase(it
);
108 it
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
109 it
->second
.coin
.Clear();
114 static const Coin coinEmpty
;
116 const Coin
& CCoinsViewCache::AccessCoin(const COutPoint
&outpoint
) const {
117 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
118 if (it
== cacheCoins
.end()) {
121 return it
->second
.coin
;
125 bool CCoinsViewCache::HaveCoin(const COutPoint
&outpoint
) const {
126 CCoinsMap::const_iterator it
= FetchCoin(outpoint
);
127 return (it
!= cacheCoins
.end() && !it
->second
.coin
.IsSpent());
130 bool CCoinsViewCache::HaveCoinInCache(const COutPoint
&outpoint
) const {
131 CCoinsMap::const_iterator it
= cacheCoins
.find(outpoint
);
132 return (it
!= cacheCoins
.end() && !it
->second
.coin
.IsSpent());
135 uint256
CCoinsViewCache::GetBestBlock() const {
136 if (hashBlock
.IsNull())
137 hashBlock
= base
->GetBestBlock();
141 void CCoinsViewCache::SetBestBlock(const uint256
&hashBlockIn
) {
142 hashBlock
= hashBlockIn
;
145 bool CCoinsViewCache::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlockIn
) {
146 for (CCoinsMap::iterator it
= mapCoins
.begin(); it
!= mapCoins
.end(); it
= mapCoins
.erase(it
)) {
147 // Ignore non-dirty entries (optimization).
148 if (!(it
->second
.flags
& CCoinsCacheEntry::DIRTY
)) {
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
;
170 // Assert that the child cache entry was not marked FRESH if the
171 // parent cache entry has unspent outputs. If this ever happens,
172 // it means the FRESH flag was misapplied and there is a logic
173 // error in the calling code.
174 if ((it
->second
.flags
& CCoinsCacheEntry::FRESH
) && !itUs
->second
.coin
.IsSpent()) {
175 throw std::logic_error("FRESH flag misapplied to cache entry for base transaction with spendable outputs");
178 // Found the entry in the parent cache
179 if ((itUs
->second
.flags
& CCoinsCacheEntry::FRESH
) && it
->second
.coin
.IsSpent()) {
180 // The grandparent does not have an entry, and the child is
181 // modified and being pruned. This means we can just delete
182 // it from the parent.
183 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
184 cacheCoins
.erase(itUs
);
186 // A normal modification.
187 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
188 itUs
->second
.coin
= std::move(it
->second
.coin
);
189 cachedCoinsUsage
+= itUs
->second
.coin
.DynamicMemoryUsage();
190 itUs
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
191 // NOTE: It is possible the child has a FRESH flag here in
192 // the event the entry we found in the parent is pruned. But
193 // we must not copy that FRESH flag to the parent as that
194 // pruned state likely still needs to be communicated to the
199 hashBlock
= hashBlockIn
;
203 bool CCoinsViewCache::Flush() {
204 bool fOk
= base
->BatchWrite(cacheCoins
, hashBlock
);
206 cachedCoinsUsage
= 0;
210 void CCoinsViewCache::Uncache(const COutPoint
& hash
)
212 CCoinsMap::iterator it
= cacheCoins
.find(hash
);
213 if (it
!= cacheCoins
.end() && it
->second
.flags
== 0) {
214 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
215 cacheCoins
.erase(it
);
219 unsigned int CCoinsViewCache::GetCacheSize() const {
220 return cacheCoins
.size();
223 CAmount
CCoinsViewCache::GetValueIn(const CTransaction
& tx
) const
229 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
230 nResult
+= AccessCoin(tx
.vin
[i
].prevout
).out
.nValue
;
235 bool CCoinsViewCache::HaveInputs(const CTransaction
& tx
) const
237 if (!tx
.IsCoinBase()) {
238 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++) {
239 if (!HaveCoin(tx
.vin
[i
].prevout
)) {
247 static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT
= WITNESS_SCALE_FACTOR
* ::GetSerializeSize(CTxOut(), SER_NETWORK
, PROTOCOL_VERSION
);
248 static const size_t MAX_OUTPUTS_PER_BLOCK
= MAX_BLOCK_WEIGHT
/ MIN_TRANSACTION_OUTPUT_WEIGHT
;
250 const Coin
& AccessByTxid(const CCoinsViewCache
& view
, const uint256
& txid
)
252 COutPoint
iter(txid
, 0);
253 while (iter
.n
< MAX_OUTPUTS_PER_BLOCK
) {
254 const Coin
& alternate
= view
.AccessCoin(iter
);
255 if (!alternate
.IsSpent()) return alternate
;