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(); it
= mapCoins
.erase(it
)) {
150 // Ignore non-dirty entries (optimization).
151 if (!(it
->second
.flags
& CCoinsCacheEntry::DIRTY
)) {
154 CCoinsMap::iterator itUs
= cacheCoins
.find(it
->first
);
155 if (itUs
== cacheCoins
.end()) {
156 // The parent cache does not have an entry, while the child does
157 // We can ignore it if it's both FRESH and pruned in the child
158 if (!(it
->second
.flags
& CCoinsCacheEntry::FRESH
&& it
->second
.coin
.IsSpent())) {
159 // Otherwise we will need to create it in the parent
160 // and move the data up and mark it as dirty
161 CCoinsCacheEntry
& entry
= cacheCoins
[it
->first
];
162 entry
.coin
= std::move(it
->second
.coin
);
163 cachedCoinsUsage
+= entry
.coin
.DynamicMemoryUsage();
164 entry
.flags
= CCoinsCacheEntry::DIRTY
;
165 // We can mark it FRESH in the parent if it was FRESH in the child
166 // Otherwise it might have just been flushed from the parent's cache
167 // and already exist in the grandparent
168 if (it
->second
.flags
& CCoinsCacheEntry::FRESH
) {
169 entry
.flags
|= CCoinsCacheEntry::FRESH
;
173 // Assert that the child cache entry was not marked FRESH if the
174 // parent cache entry has unspent outputs. If this ever happens,
175 // it means the FRESH flag was misapplied and there is a logic
176 // error in the calling code.
177 if ((it
->second
.flags
& CCoinsCacheEntry::FRESH
) && !itUs
->second
.coin
.IsSpent()) {
178 throw std::logic_error("FRESH flag misapplied to cache entry for base transaction with spendable outputs");
181 // Found the entry in the parent cache
182 if ((itUs
->second
.flags
& CCoinsCacheEntry::FRESH
) && it
->second
.coin
.IsSpent()) {
183 // The grandparent does not have an entry, and the child is
184 // modified and being pruned. This means we can just delete
185 // it from the parent.
186 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
187 cacheCoins
.erase(itUs
);
189 // A normal modification.
190 cachedCoinsUsage
-= itUs
->second
.coin
.DynamicMemoryUsage();
191 itUs
->second
.coin
= std::move(it
->second
.coin
);
192 cachedCoinsUsage
+= itUs
->second
.coin
.DynamicMemoryUsage();
193 itUs
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
194 // NOTE: It is possible the child has a FRESH flag here in
195 // the event the entry we found in the parent is pruned. But
196 // we must not copy that FRESH flag to the parent as that
197 // pruned state likely still needs to be communicated to the
202 hashBlock
= hashBlockIn
;
206 bool CCoinsViewCache::Flush() {
207 bool fOk
= base
->BatchWrite(cacheCoins
, hashBlock
);
209 cachedCoinsUsage
= 0;
213 void CCoinsViewCache::Uncache(const COutPoint
& hash
)
215 CCoinsMap::iterator it
= cacheCoins
.find(hash
);
216 if (it
!= cacheCoins
.end() && it
->second
.flags
== 0) {
217 cachedCoinsUsage
-= it
->second
.coin
.DynamicMemoryUsage();
218 cacheCoins
.erase(it
);
222 unsigned int CCoinsViewCache::GetCacheSize() const {
223 return cacheCoins
.size();
226 CAmount
CCoinsViewCache::GetValueIn(const CTransaction
& tx
) const
232 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
233 nResult
+= AccessCoin(tx
.vin
[i
].prevout
).out
.nValue
;
238 bool CCoinsViewCache::HaveInputs(const CTransaction
& tx
) const
240 if (!tx
.IsCoinBase()) {
241 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++) {
242 if (!HaveCoin(tx
.vin
[i
].prevout
)) {
250 static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT
= WITNESS_SCALE_FACTOR
* ::GetSerializeSize(CTxOut(), SER_NETWORK
, PROTOCOL_VERSION
);
251 static const size_t MAX_OUTPUTS_PER_BLOCK
= MAX_BLOCK_WEIGHT
/ MIN_TRANSACTION_OUTPUT_WEIGHT
;
253 const Coin
& AccessByTxid(const CCoinsViewCache
& view
, const uint256
& txid
)
255 COutPoint
iter(txid
, 0);
256 while (iter
.n
< MAX_OUTPUTS_PER_BLOCK
) {
257 const Coin
& alternate
= view
.AccessCoin(iter
);
258 if (!alternate
.IsSpent()) return alternate
;