1 // Copyright (c) 2012-2019 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.
4 
5 #include <coins.h>
6 
7 #include <consensus/consensus.h>
8 #include <logging.h>
9 #include <random.h>
10 #include <version.h>
11 
GetCoin(const COutPoint & outpoint,Coin & coin) const12 bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; }
GetBestBlock() const13 uint256 CCoinsView::GetBestBlock() const { return uint256(); }
GetHeadBlocks() const14 std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); }
BatchWrite(CCoinsMap & mapCoins,const uint256 & hashBlock)15 bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
Cursor() const16 CCoinsViewCursor *CCoinsView::Cursor() const { return nullptr; }
17 
HaveCoin(const COutPoint & outpoint) const18 bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
19 {
20     Coin coin;
21     return GetCoin(outpoint, coin);
22 }
23 
CCoinsViewBacked(CCoinsView * viewIn)24 CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
GetCoin(const COutPoint & outpoint,Coin & coin) const25 bool CCoinsViewBacked::GetCoin(const COutPoint &outpoint, Coin &coin) const { return base->GetCoin(outpoint, coin); }
HaveCoin(const COutPoint & outpoint) const26 bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); }
GetBestBlock() const27 uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
GetHeadBlocks() const28 std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); }
SetBackend(CCoinsView & viewIn)29 void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
BatchWrite(CCoinsMap & mapCoins,const uint256 & hashBlock)30 bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
Cursor() const31 CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
EstimateSize() const32 size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
33 
SaltedOutpointHasher()34 SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
35 
CCoinsViewCache(CCoinsView * baseIn)36 CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) {}
37 
DynamicMemoryUsage() const38 size_t CCoinsViewCache::DynamicMemoryUsage() const {
39     return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
40 }
41 
FetchCoin(const COutPoint & outpoint) const42 CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
43     CCoinsMap::iterator it = cacheCoins.find(outpoint);
44     if (it != cacheCoins.end())
45         return it;
46     Coin tmp;
47     if (!base->GetCoin(outpoint, tmp))
48         return cacheCoins.end();
49     CCoinsMap::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
50     if (ret->second.coin.IsSpent()) {
51         // The parent only has an empty entry for this outpoint; we can consider our
52         // version as fresh.
53         ret->second.flags = CCoinsCacheEntry::FRESH;
54     }
55     cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
56     return ret;
57 }
58 
GetCoin(const COutPoint & outpoint,Coin & coin) const59 bool CCoinsViewCache::GetCoin(const COutPoint &outpoint, Coin &coin) const {
60     CCoinsMap::const_iterator it = FetchCoin(outpoint);
61     if (it != cacheCoins.end()) {
62         coin = it->second.coin;
63         return !coin.IsSpent();
64     }
65     return false;
66 }
67 
AddCoin(const COutPoint & outpoint,Coin && coin,bool possible_overwrite)68 void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
69     assert(!coin.IsSpent());
70     if (coin.out.scriptPubKey.IsUnspendable()) return;
71     CCoinsMap::iterator it;
72     bool inserted;
73     std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
74     bool fresh = false;
75     if (!inserted) {
76         cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
77     }
78     if (!possible_overwrite) {
79         if (!it->second.coin.IsSpent()) {
80             throw std::logic_error("Adding new coin that replaces non-pruned entry");
81         }
82         fresh = !(it->second.flags & CCoinsCacheEntry::DIRTY);
83     }
84     it->second.coin = std::move(coin);
85     it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
86     cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
87 }
88 
AddCoins(CCoinsViewCache & cache,const CTransaction & tx,int nHeight,bool check)89 void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check) {
90     bool fCoinbase = tx.IsCoinBase();
91     bool fCoinstake = tx.IsCoinStake();
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, fCoinstake), overwrite);
98     }
99 }
100 
SpendCoin(const COutPoint & outpoint,Coin * moveout)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();
105     if (moveout) {
106         *moveout = std::move(it->second.coin);
107     }
108     if (it->second.flags & CCoinsCacheEntry::FRESH) {
109         cacheCoins.erase(it);
110     } else {
111         it->second.flags |= CCoinsCacheEntry::DIRTY;
112         it->second.coin.Clear();
113     }
114     return true;
115 }
116 
117 static const Coin coinEmpty;
118 
AccessCoin(const COutPoint & outpoint) const119 const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
120     CCoinsMap::const_iterator it = FetchCoin(outpoint);
121     if (it == cacheCoins.end()) {
122         return coinEmpty;
123     } else {
124         return it->second.coin;
125     }
126 }
127 
HaveCoin(const COutPoint & outpoint) const128 bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const {
129     CCoinsMap::const_iterator it = FetchCoin(outpoint);
130     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
131 }
132 
HaveCoinInCache(const COutPoint & outpoint) const133 bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
134     CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
135     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
136 }
137 
GetBestBlock() const138 uint256 CCoinsViewCache::GetBestBlock() const {
139     if (hashBlock.IsNull())
140         hashBlock = base->GetBestBlock();
141     return hashBlock;
142 }
143 
SetBestBlock(const uint256 & hashBlockIn)144 void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
145     hashBlock = hashBlockIn;
146 }
147 
BatchWrite(CCoinsMap & mapCoins,const uint256 & 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)) {
152             continue;
153         }
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;
170                 }
171             }
172         } else {
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");
179             }
180 
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);
188             } else {
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
198                 // grandparent.
199             }
200         }
201     }
202     hashBlock = hashBlockIn;
203     return true;
204 }
205 
Flush()206 bool CCoinsViewCache::Flush() {
207     bool fOk = base->BatchWrite(cacheCoins, hashBlock);
208     cacheCoins.clear();
209     cachedCoinsUsage = 0;
210     return fOk;
211 }
212 
Uncache(const COutPoint & hash)213 void CCoinsViewCache::Uncache(const COutPoint& hash)
214 {
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);
219     }
220 }
221 
GetCacheSize() const222 unsigned int CCoinsViewCache::GetCacheSize() const {
223     return cacheCoins.size();
224 }
225 
GetValueIn(const CTransaction & tx) const226 CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
227 {
228     if (tx.IsCoinBase())
229         return 0;
230 
231     CAmount nResult = 0;
232     for (unsigned int i = 0; i < tx.vin.size(); i++)
233         nResult += AccessCoin(tx.vin[i].prevout).out.nValue;
234 
235     return nResult;
236 }
237 
HaveInputs(const CTransaction & tx) const238 bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
239 {
240     if (!tx.IsCoinBase()) {
241         for (unsigned int i = 0; i < tx.vin.size(); i++) {
242             if (!HaveCoin(tx.vin[i].prevout)) {
243                 return false;
244             }
245         }
246     }
247     return true;
248 }
249 
250 static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut(), PROTOCOL_VERSION);
251 static const size_t MAX_OUTPUTS_PER_BLOCK = dgpMaxBlockWeight / MIN_TRANSACTION_OUTPUT_WEIGHT;
252 
AccessByTxid(const CCoinsViewCache & view,const uint256 & txid)253 const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
254 {
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;
259         ++iter.n;
260     }
261     return coinEmpty;
262 }
263 
GetCoin(const COutPoint & outpoint,Coin & coin) const264 bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) const {
265     try {
266         return CCoinsViewBacked::GetCoin(outpoint, coin);
267     } catch(const std::runtime_error& e) {
268         for (auto f : m_err_callbacks) {
269             f();
270         }
271         LogPrintf("Error reading from database: %s\n", e.what());
272         // Starting the shutdown sequence and returning false to the caller would be
273         // interpreted as 'entry not found' (as opposed to unable to read data), and
274         // could lead to invalid interpretation. Just exit immediately, as we can't
275         // continue anyway, and all writes should be atomic.
276         std::abort();
277     }
278 }
279 
GetOutputFor(const CTxIn & input) const280 const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
281 {
282     const Coin& coins = AccessCoin(input.prevout);
283     return coins.out;
284 }
285