1 // Copyright (c) 2012-2015 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.
13 * calculate number of bytes for the bitmask, and its number of non-zero bytes
14 * each bit in the bitmask represents the availability of one output, but the
15 * availabilities of the first two outputs are encoded separately
17 void CCoins::CalcMaskSize(unsigned int &nBytes
, unsigned int &nNonzeroBytes
) const {
18 unsigned int nLastUsedByte
= 0;
19 for (unsigned int b
= 0; 2+b
*8 < vout
.size(); b
++) {
21 for (unsigned int i
= 0; i
< 8 && 2+b
*8+i
< vout
.size(); i
++) {
22 if (!vout
[2+b
*8+i
].IsNull()) {
28 nLastUsedByte
= b
+ 1;
32 nBytes
+= nLastUsedByte
;
35 bool CCoins::Spend(uint32_t nPos
)
37 if (nPos
>= vout
.size() || vout
[nPos
].IsNull())
44 bool CCoinsView::GetCoins(const uint256
&txid
, CCoins
&coins
) const { return false; }
45 bool CCoinsView::HaveCoins(const uint256
&txid
) const { return false; }
46 uint256
CCoinsView::GetBestBlock() const { return uint256(); }
47 bool CCoinsView::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return false; }
48 CCoinsViewCursor
*CCoinsView::Cursor() const { return 0; }
51 CCoinsViewBacked::CCoinsViewBacked(CCoinsView
*viewIn
) : base(viewIn
) { }
52 bool CCoinsViewBacked::GetCoins(const uint256
&txid
, CCoins
&coins
) const { return base
->GetCoins(txid
, coins
); }
53 bool CCoinsViewBacked::HaveCoins(const uint256
&txid
) const { return base
->HaveCoins(txid
); }
54 uint256
CCoinsViewBacked::GetBestBlock() const { return base
->GetBestBlock(); }
55 void CCoinsViewBacked::SetBackend(CCoinsView
&viewIn
) { base
= &viewIn
; }
56 bool CCoinsViewBacked::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) { return base
->BatchWrite(mapCoins
, hashBlock
); }
57 CCoinsViewCursor
*CCoinsViewBacked::Cursor() const { return base
->Cursor(); }
59 SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits
<uint64_t>::max())), k1(GetRand(std::numeric_limits
<uint64_t>::max())) {}
61 CCoinsViewCache::CCoinsViewCache(CCoinsView
*baseIn
) : CCoinsViewBacked(baseIn
), hasModifier(false), cachedCoinsUsage(0) { }
63 CCoinsViewCache::~CCoinsViewCache()
68 size_t CCoinsViewCache::DynamicMemoryUsage() const {
69 return memusage::DynamicUsage(cacheCoins
) + cachedCoinsUsage
;
72 CCoinsMap::const_iterator
CCoinsViewCache::FetchCoins(const uint256
&txid
) const {
73 CCoinsMap::iterator it
= cacheCoins
.find(txid
);
74 if (it
!= cacheCoins
.end())
77 if (!base
->GetCoins(txid
, tmp
))
78 return cacheCoins
.end();
79 CCoinsMap::iterator ret
= cacheCoins
.insert(std::make_pair(txid
, CCoinsCacheEntry())).first
;
80 tmp
.swap(ret
->second
.coins
);
81 if (ret
->second
.coins
.IsPruned()) {
82 // The parent only has an empty entry for this txid; we can consider our
84 ret
->second
.flags
= CCoinsCacheEntry::FRESH
;
86 cachedCoinsUsage
+= ret
->second
.coins
.DynamicMemoryUsage();
90 bool CCoinsViewCache::GetCoins(const uint256
&txid
, CCoins
&coins
) const {
91 CCoinsMap::const_iterator it
= FetchCoins(txid
);
92 if (it
!= cacheCoins
.end()) {
93 coins
= it
->second
.coins
;
99 CCoinsModifier
CCoinsViewCache::ModifyCoins(const uint256
&txid
) {
100 assert(!hasModifier
);
101 std::pair
<CCoinsMap::iterator
, bool> ret
= cacheCoins
.insert(std::make_pair(txid
, CCoinsCacheEntry()));
102 size_t cachedCoinUsage
= 0;
104 if (!base
->GetCoins(txid
, ret
.first
->second
.coins
)) {
105 // The parent view does not have this entry; mark it as fresh.
106 ret
.first
->second
.coins
.Clear();
107 ret
.first
->second
.flags
= CCoinsCacheEntry::FRESH
;
108 } else if (ret
.first
->second
.coins
.IsPruned()) {
109 // The parent view only has a pruned entry for this; mark it as fresh.
110 ret
.first
->second
.flags
= CCoinsCacheEntry::FRESH
;
113 cachedCoinUsage
= ret
.first
->second
.coins
.DynamicMemoryUsage();
115 // Assume that whenever ModifyCoins is called, the entry will be modified.
116 ret
.first
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
117 return CCoinsModifier(*this, ret
.first
, cachedCoinUsage
);
120 // ModifyNewCoins has to know whether the new outputs its creating are for a
121 // coinbase or not. If they are for a coinbase, it can not mark them as fresh.
122 // This is to ensure that the historical duplicate coinbases before BIP30 was
123 // in effect will still be properly overwritten when spent.
124 CCoinsModifier
CCoinsViewCache::ModifyNewCoins(const uint256
&txid
, bool coinbase
) {
125 assert(!hasModifier
);
126 std::pair
<CCoinsMap::iterator
, bool> ret
= cacheCoins
.insert(std::make_pair(txid
, CCoinsCacheEntry()));
127 ret
.first
->second
.coins
.Clear();
129 ret
.first
->second
.flags
= CCoinsCacheEntry::FRESH
;
131 ret
.first
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
132 return CCoinsModifier(*this, ret
.first
, 0);
135 const CCoins
* CCoinsViewCache::AccessCoins(const uint256
&txid
) const {
136 CCoinsMap::const_iterator it
= FetchCoins(txid
);
137 if (it
== cacheCoins
.end()) {
140 return &it
->second
.coins
;
144 bool CCoinsViewCache::HaveCoins(const uint256
&txid
) const {
145 CCoinsMap::const_iterator it
= FetchCoins(txid
);
146 // We're using vtx.empty() instead of IsPruned here for performance reasons,
147 // as we only care about the case where a transaction was replaced entirely
148 // in a reorganization (which wipes vout entirely, as opposed to spending
149 // which just cleans individual outputs).
150 return (it
!= cacheCoins
.end() && !it
->second
.coins
.vout
.empty());
153 bool CCoinsViewCache::HaveCoinsInCache(const uint256
&txid
) const {
154 CCoinsMap::const_iterator it
= cacheCoins
.find(txid
);
155 return it
!= cacheCoins
.end();
158 uint256
CCoinsViewCache::GetBestBlock() const {
159 if (hashBlock
.IsNull())
160 hashBlock
= base
->GetBestBlock();
164 void CCoinsViewCache::SetBestBlock(const uint256
&hashBlockIn
) {
165 hashBlock
= hashBlockIn
;
168 bool CCoinsViewCache::BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlockIn
) {
169 assert(!hasModifier
);
170 for (CCoinsMap::iterator it
= mapCoins
.begin(); it
!= mapCoins
.end();) {
171 if (it
->second
.flags
& CCoinsCacheEntry::DIRTY
) { // Ignore non-dirty entries (optimization).
172 CCoinsMap::iterator itUs
= cacheCoins
.find(it
->first
);
173 if (itUs
== cacheCoins
.end()) {
174 // The parent cache does not have an entry, while the child does
175 // We can ignore it if it's both FRESH and pruned in the child
176 if (!(it
->second
.flags
& CCoinsCacheEntry::FRESH
&& it
->second
.coins
.IsPruned())) {
177 // Otherwise we will need to create it in the parent
178 // and move the data up and mark it as dirty
179 CCoinsCacheEntry
& entry
= cacheCoins
[it
->first
];
180 entry
.coins
.swap(it
->second
.coins
);
181 cachedCoinsUsage
+= entry
.coins
.DynamicMemoryUsage();
182 entry
.flags
= CCoinsCacheEntry::DIRTY
;
183 // We can mark it FRESH in the parent if it was FRESH in the child
184 // Otherwise it might have just been flushed from the parent's cache
185 // and already exist in the grandparent
186 if (it
->second
.flags
& CCoinsCacheEntry::FRESH
)
187 entry
.flags
|= CCoinsCacheEntry::FRESH
;
190 // Found the entry in the parent cache
191 if ((itUs
->second
.flags
& CCoinsCacheEntry::FRESH
) && it
->second
.coins
.IsPruned()) {
192 // The grandparent does not have an entry, and the child is
193 // modified and being pruned. This means we can just delete
194 // it from the parent.
195 cachedCoinsUsage
-= itUs
->second
.coins
.DynamicMemoryUsage();
196 cacheCoins
.erase(itUs
);
198 // A normal modification.
199 cachedCoinsUsage
-= itUs
->second
.coins
.DynamicMemoryUsage();
200 itUs
->second
.coins
.swap(it
->second
.coins
);
201 cachedCoinsUsage
+= itUs
->second
.coins
.DynamicMemoryUsage();
202 itUs
->second
.flags
|= CCoinsCacheEntry::DIRTY
;
206 CCoinsMap::iterator itOld
= it
++;
207 mapCoins
.erase(itOld
);
209 hashBlock
= hashBlockIn
;
213 bool CCoinsViewCache::Flush() {
214 bool fOk
= base
->BatchWrite(cacheCoins
, hashBlock
);
216 cachedCoinsUsage
= 0;
220 void CCoinsViewCache::Uncache(const uint256
& hash
)
222 CCoinsMap::iterator it
= cacheCoins
.find(hash
);
223 if (it
!= cacheCoins
.end() && it
->second
.flags
== 0) {
224 cachedCoinsUsage
-= it
->second
.coins
.DynamicMemoryUsage();
225 cacheCoins
.erase(it
);
229 unsigned int CCoinsViewCache::GetCacheSize() const {
230 return cacheCoins
.size();
233 const CTxOut
&CCoinsViewCache::GetOutputFor(const CTxIn
& input
) const
235 const CCoins
* coins
= AccessCoins(input
.prevout
.hash
);
236 assert(coins
&& coins
->IsAvailable(input
.prevout
.n
));
237 return coins
->vout
[input
.prevout
.n
];
240 CAmount
CCoinsViewCache::GetValueIn(const CTransaction
& tx
) const
246 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
247 nResult
+= GetOutputFor(tx
.vin
[i
]).nValue
;
252 bool CCoinsViewCache::HaveInputs(const CTransaction
& tx
) const
254 if (!tx
.IsCoinBase()) {
255 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++) {
256 const COutPoint
&prevout
= tx
.vin
[i
].prevout
;
257 const CCoins
* coins
= AccessCoins(prevout
.hash
);
258 if (!coins
|| !coins
->IsAvailable(prevout
.n
)) {
266 double CCoinsViewCache::GetPriority(const CTransaction
&tx
, int nHeight
, CAmount
&inChainInputValue
) const
268 inChainInputValue
= 0;
271 double dResult
= 0.0;
272 BOOST_FOREACH(const CTxIn
& txin
, tx
.vin
)
274 const CCoins
* coins
= AccessCoins(txin
.prevout
.hash
);
276 if (!coins
->IsAvailable(txin
.prevout
.n
)) continue;
277 if (coins
->nHeight
<= nHeight
) {
278 dResult
+= (double)(coins
->vout
[txin
.prevout
.n
].nValue
) * (nHeight
-coins
->nHeight
);
279 inChainInputValue
+= coins
->vout
[txin
.prevout
.n
].nValue
;
282 return tx
.ComputePriority(dResult
);
285 CCoinsModifier::CCoinsModifier(CCoinsViewCache
& cache_
, CCoinsMap::iterator it_
, size_t usage
) : cache(cache_
), it(it_
), cachedCoinUsage(usage
) {
286 assert(!cache
.hasModifier
);
287 cache
.hasModifier
= true;
290 CCoinsModifier::~CCoinsModifier()
292 assert(cache
.hasModifier
);
293 cache
.hasModifier
= false;
294 it
->second
.coins
.Cleanup();
295 cache
.cachedCoinsUsage
-= cachedCoinUsage
; // Subtract the old usage
296 if ((it
->second
.flags
& CCoinsCacheEntry::FRESH
) && it
->second
.coins
.IsPruned()) {
297 cache
.cacheCoins
.erase(it
);
299 // If the coin still exists after the modification, add the new usage
300 cache
.cachedCoinsUsage
+= it
->second
.coins
.DynamicMemoryUsage();
304 CCoinsViewCursor::~CCoinsViewCursor()