2014-10-31 01:43:19 +01:00
|
|
|
// Copyright (c) 2012-2014 The Bitcoin developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-05 02:47:07 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "coins.h"
|
|
|
|
|
2014-07-09 17:25:09 +02:00
|
|
|
#include "random.h"
|
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* calculate number of bytes for the bitmask, and its number of non-zero bytes
|
|
|
|
* each bit in the bitmask represents the availability of one output, but the
|
|
|
|
* availabilities of the first two outputs are encoded separately
|
|
|
|
*/
|
2013-11-05 02:47:07 +01:00
|
|
|
void CCoins::CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) const {
|
|
|
|
unsigned int nLastUsedByte = 0;
|
|
|
|
for (unsigned int b = 0; 2+b*8 < vout.size(); b++) {
|
|
|
|
bool fZero = true;
|
|
|
|
for (unsigned int i = 0; i < 8 && 2+b*8+i < vout.size(); i++) {
|
|
|
|
if (!vout[2+b*8+i].IsNull()) {
|
|
|
|
fZero = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!fZero) {
|
|
|
|
nLastUsedByte = b + 1;
|
|
|
|
nNonzeroBytes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nBytes += nLastUsedByte;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCoins::Spend(const COutPoint &out, CTxInUndo &undo) {
|
|
|
|
if (out.n >= vout.size())
|
|
|
|
return false;
|
|
|
|
if (vout[out.n].IsNull())
|
|
|
|
return false;
|
|
|
|
undo = CTxInUndo(vout[out.n]);
|
|
|
|
vout[out.n].SetNull();
|
|
|
|
Cleanup();
|
|
|
|
if (vout.size() == 0) {
|
|
|
|
undo.nHeight = nHeight;
|
|
|
|
undo.fCoinBase = fCoinBase;
|
|
|
|
undo.nVersion = this->nVersion;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCoins::Spend(int nPos) {
|
|
|
|
CTxInUndo undo;
|
|
|
|
COutPoint out(0, nPos);
|
|
|
|
return Spend(out, undo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) const { return false; }
|
|
|
|
bool CCoinsView::HaveCoins(const uint256 &txid) const { return false; }
|
|
|
|
uint256 CCoinsView::GetBestBlock() const { return uint256(0); }
|
2014-08-24 02:08:05 +02:00
|
|
|
bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
|
|
|
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewBacked::GetCoins(const uint256 &txid, CCoins &coins) const { return base->GetCoins(txid, coins); }
|
|
|
|
bool CCoinsViewBacked::HaveCoins(const uint256 &txid) const { return base->HaveCoins(txid); }
|
|
|
|
uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
|
2013-11-05 02:47:07 +01:00
|
|
|
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
|
2014-08-24 02:08:05 +02:00
|
|
|
bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewBacked::GetStats(CCoinsStats &stats) const { return base->GetStats(stats); }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-07-09 17:25:09 +02:00
|
|
|
CCoinsKeyHasher::CCoinsKeyHasher() : salt(GetRandHash()) {}
|
|
|
|
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false), hashBlock(0) { }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-09-03 09:01:24 +02:00
|
|
|
CCoinsViewCache::~CCoinsViewCache()
|
|
|
|
{
|
|
|
|
assert(!hasModifier);
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
2014-09-03 09:01:24 +02:00
|
|
|
CCoinsMap::const_iterator CCoinsViewCache::FetchCoins(const uint256 &txid) const {
|
2014-07-09 17:25:09 +02:00
|
|
|
CCoinsMap::iterator it = cacheCoins.find(txid);
|
|
|
|
if (it != cacheCoins.end())
|
2013-11-05 02:47:07 +01:00
|
|
|
return it;
|
|
|
|
CCoins tmp;
|
2014-09-03 09:37:47 +02:00
|
|
|
if (!base->GetCoins(txid, tmp))
|
2013-11-05 02:47:07 +01:00
|
|
|
return cacheCoins.end();
|
2014-09-03 09:37:47 +02:00
|
|
|
CCoinsMap::iterator ret = cacheCoins.insert(std::make_pair(txid, CCoinsCacheEntry())).first;
|
|
|
|
tmp.swap(ret->second.coins);
|
|
|
|
if (ret->second.coins.IsPruned()) {
|
|
|
|
// The parent only has an empty entry for this txid; we can consider our
|
|
|
|
// version as fresh.
|
|
|
|
ret->second.flags = CCoinsCacheEntry::FRESH;
|
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-03 09:37:47 +02:00
|
|
|
bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) const {
|
|
|
|
CCoinsMap::const_iterator it = FetchCoins(txid);
|
|
|
|
if (it != cacheCoins.end()) {
|
|
|
|
coins = it->second.coins;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-07-19 16:42:48 +02:00
|
|
|
}
|
|
|
|
|
2014-09-03 09:01:24 +02:00
|
|
|
CCoinsModifier CCoinsViewCache::ModifyCoins(const uint256 &txid) {
|
|
|
|
assert(!hasModifier);
|
2014-09-03 09:37:47 +02:00
|
|
|
std::pair<CCoinsMap::iterator, bool> ret = cacheCoins.insert(std::make_pair(txid, CCoinsCacheEntry()));
|
2014-09-03 09:01:24 +02:00
|
|
|
if (ret.second) {
|
2014-09-03 09:37:47 +02:00
|
|
|
if (!base->GetCoins(txid, ret.first->second.coins)) {
|
|
|
|
// The parent view does not have this entry; mark it as fresh.
|
|
|
|
ret.first->second.coins.Clear();
|
|
|
|
ret.first->second.flags = CCoinsCacheEntry::FRESH;
|
|
|
|
} else if (ret.first->second.coins.IsPruned()) {
|
|
|
|
// The parent view only has a pruned entry for this; mark it as fresh.
|
|
|
|
ret.first->second.flags = CCoinsCacheEntry::FRESH;
|
|
|
|
}
|
2014-09-03 09:01:24 +02:00
|
|
|
}
|
2014-09-03 09:37:47 +02:00
|
|
|
// Assume that whenever ModifyCoins is called, the entry will be modified.
|
|
|
|
ret.first->second.flags |= CCoinsCacheEntry::DIRTY;
|
2014-09-03 09:01:24 +02:00
|
|
|
return CCoinsModifier(*this, ret.first);
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
2014-09-02 21:21:15 +02:00
|
|
|
const CCoins* CCoinsViewCache::AccessCoins(const uint256 &txid) const {
|
|
|
|
CCoinsMap::const_iterator it = FetchCoins(txid);
|
|
|
|
if (it == cacheCoins.end()) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2014-09-03 09:37:47 +02:00
|
|
|
return &it->second.coins;
|
2014-09-02 21:21:15 +02:00
|
|
|
}
|
2014-07-19 16:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCoinsViewCache::HaveCoins(const uint256 &txid) const {
|
|
|
|
CCoinsMap::const_iterator it = FetchCoins(txid);
|
2014-07-10 17:35:22 +02:00
|
|
|
// We're using vtx.empty() instead of IsPruned here for performance reasons,
|
2014-10-31 01:43:19 +01:00
|
|
|
// as we only care about the case where a transaction was replaced entirely
|
2014-07-10 17:35:22 +02:00
|
|
|
// in a reorganization (which wipes vout entirely, as opposed to spending
|
|
|
|
// which just cleans individual outputs).
|
2014-09-03 09:37:47 +02:00
|
|
|
return (it != cacheCoins.end() && !it->second.coins.vout.empty());
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
uint256 CCoinsViewCache::GetBestBlock() const {
|
2013-11-05 02:47:07 +01:00
|
|
|
if (hashBlock == uint256(0))
|
|
|
|
hashBlock = base->GetBestBlock();
|
|
|
|
return hashBlock;
|
|
|
|
}
|
|
|
|
|
2014-09-03 09:25:32 +02:00
|
|
|
void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
|
2013-11-05 02:47:07 +01:00
|
|
|
hashBlock = hashBlockIn;
|
|
|
|
}
|
|
|
|
|
2014-08-24 02:08:05 +02:00
|
|
|
bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
|
2014-09-03 09:01:24 +02:00
|
|
|
assert(!hasModifier);
|
2014-08-24 02:08:05 +02:00
|
|
|
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
|
2014-09-03 09:37:47 +02:00
|
|
|
if (it->second.flags & CCoinsCacheEntry::DIRTY) { // Ignore non-dirty entries (optimization).
|
|
|
|
CCoinsMap::iterator itUs = cacheCoins.find(it->first);
|
|
|
|
if (itUs == cacheCoins.end()) {
|
|
|
|
if (!it->second.coins.IsPruned()) {
|
|
|
|
// The parent cache does not have an entry, while the child
|
|
|
|
// cache does have (a non-pruned) one. Move the data up, and
|
|
|
|
// mark it as fresh (if the grandparent did have it, we
|
|
|
|
// would have pulled it in at first GetCoins).
|
|
|
|
assert(it->second.flags & CCoinsCacheEntry::FRESH);
|
|
|
|
CCoinsCacheEntry& entry = cacheCoins[it->first];
|
|
|
|
entry.coins.swap(it->second.coins);
|
|
|
|
entry.flags = CCoinsCacheEntry::DIRTY | CCoinsCacheEntry::FRESH;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((itUs->second.flags & CCoinsCacheEntry::FRESH) && it->second.coins.IsPruned()) {
|
|
|
|
// The grandparent does not have an entry, and the child is
|
|
|
|
// modified and being pruned. This means we can just delete
|
|
|
|
// it from the parent.
|
|
|
|
cacheCoins.erase(itUs);
|
|
|
|
} else {
|
|
|
|
// A normal modification.
|
|
|
|
itUs->second.coins.swap(it->second.coins);
|
|
|
|
itUs->second.flags |= CCoinsCacheEntry::DIRTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-24 02:08:05 +02:00
|
|
|
CCoinsMap::iterator itOld = it++;
|
|
|
|
mapCoins.erase(itOld);
|
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
hashBlock = hashBlockIn;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCoinsViewCache::Flush() {
|
|
|
|
bool fOk = base->BatchWrite(cacheCoins, hashBlock);
|
2014-08-24 02:08:05 +02:00
|
|
|
cacheCoins.clear();
|
2013-11-05 02:47:07 +01:00
|
|
|
return fOk;
|
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
unsigned int CCoinsViewCache::GetCacheSize() const {
|
2013-11-05 02:47:07 +01:00
|
|
|
return cacheCoins.size();
|
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
|
2013-11-05 02:47:07 +01:00
|
|
|
{
|
2014-09-02 21:21:15 +02:00
|
|
|
const CCoins* coins = AccessCoins(input.prevout.hash);
|
|
|
|
assert(coins && coins->IsAvailable(input.prevout.n));
|
|
|
|
return coins->vout[input.prevout.n];
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
|
2013-11-05 02:47:07 +01:00
|
|
|
{
|
|
|
|
if (tx.IsCoinBase())
|
|
|
|
return 0;
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nResult = 0;
|
2013-11-05 02:47:07 +01:00
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++)
|
|
|
|
nResult += GetOutputFor(tx.vin[i]).nValue;
|
|
|
|
|
|
|
|
return nResult;
|
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
|
2013-11-05 02:47:07 +01:00
|
|
|
{
|
|
|
|
if (!tx.IsCoinBase()) {
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++) {
|
|
|
|
const COutPoint &prevout = tx.vin[i].prevout;
|
2014-09-02 21:21:15 +02:00
|
|
|
const CCoins* coins = AccessCoins(prevout.hash);
|
|
|
|
if (!coins || !coins->IsAvailable(prevout.n)) {
|
2013-11-05 02:47:07 +01:00
|
|
|
return false;
|
2014-09-02 21:21:15 +02:00
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-11 08:35:14 +01:00
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
double CCoinsViewCache::GetPriority(const CTransaction &tx, int nHeight) const
|
2013-11-11 08:35:14 +01:00
|
|
|
{
|
|
|
|
if (tx.IsCoinBase())
|
|
|
|
return 0.0;
|
|
|
|
double dResult = 0.0;
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin)
|
|
|
|
{
|
2014-09-02 21:21:15 +02:00
|
|
|
const CCoins* coins = AccessCoins(txin.prevout.hash);
|
|
|
|
assert(coins);
|
|
|
|
if (!coins->IsAvailable(txin.prevout.n)) continue;
|
|
|
|
if (coins->nHeight < nHeight) {
|
|
|
|
dResult += coins->vout[txin.prevout.n].nValue * (nHeight-coins->nHeight);
|
2013-11-11 08:35:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx.ComputePriority(dResult);
|
|
|
|
}
|
2014-09-03 09:01:24 +02:00
|
|
|
|
2015-01-04 17:15:02 +01:00
|
|
|
CCoinsModifier::CCoinsModifier(CCoinsViewCache& cache_, CCoinsMap::iterator it_) : cache(cache_), it(it_) {
|
|
|
|
assert(!cache.hasModifier);
|
|
|
|
cache.hasModifier = true;
|
|
|
|
}
|
2014-09-03 09:01:24 +02:00
|
|
|
|
2014-09-03 09:37:47 +02:00
|
|
|
CCoinsModifier::~CCoinsModifier()
|
|
|
|
{
|
2014-09-03 09:01:24 +02:00
|
|
|
assert(cache.hasModifier);
|
|
|
|
cache.hasModifier = false;
|
2014-09-03 09:37:47 +02:00
|
|
|
it->second.coins.Cleanup();
|
|
|
|
if ((it->second.flags & CCoinsCacheEntry::FRESH) && it->second.coins.IsPruned()) {
|
|
|
|
cache.cacheCoins.erase(it);
|
|
|
|
}
|
2014-09-03 09:01:24 +02:00
|
|
|
}
|