mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 21:42:47 +01:00
323 lines
13 KiB
C++
323 lines
13 KiB
C++
// Copyright (c) 2012-2016 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include "coins.h"
|
|
|
|
#include "consensus/consensus.h"
|
|
#include "memusage.h"
|
|
#include "random.h"
|
|
|
|
#include <assert.h>
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
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(uint32_t nPos)
|
|
{
|
|
if (nPos >= vout.size() || vout[nPos].IsNull())
|
|
return false;
|
|
vout[nPos].SetNull();
|
|
Cleanup();
|
|
return true;
|
|
}
|
|
|
|
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(); }
|
|
bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
|
|
CCoinsViewCursor *CCoinsView::Cursor() const { return 0; }
|
|
|
|
|
|
CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
|
|
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(); }
|
|
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
|
|
bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
|
|
CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
|
|
size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
|
|
|
|
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
|
|
|
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) { }
|
|
|
|
size_t CCoinsViewCache::DynamicMemoryUsage() const {
|
|
return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
|
|
}
|
|
|
|
CCoinsMap::iterator CCoinsViewCache::FetchCoins(const uint256 &txid) const {
|
|
CCoinsMap::iterator it = cacheCoins.find(txid);
|
|
if (it != cacheCoins.end())
|
|
return it;
|
|
CCoins tmp;
|
|
if (!base->GetCoins(txid, tmp))
|
|
return cacheCoins.end();
|
|
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;
|
|
}
|
|
cachedCoinsUsage += ret->second.coins.DynamicMemoryUsage();
|
|
return ret;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
|
|
assert(!coin.IsPruned());
|
|
if (coin.out.scriptPubKey.IsUnspendable()) return;
|
|
CCoinsMap::iterator it;
|
|
bool inserted;
|
|
std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint.hash), std::tuple<>());
|
|
bool fresh = false;
|
|
if (!inserted) {
|
|
cachedCoinsUsage -= it->second.coins.DynamicMemoryUsage();
|
|
}
|
|
if (!possible_overwrite) {
|
|
if (it->second.coins.IsAvailable(outpoint.n)) {
|
|
throw std::logic_error("Adding new coin that replaces non-pruned entry");
|
|
}
|
|
fresh = it->second.coins.IsPruned() && !(it->second.flags & CCoinsCacheEntry::DIRTY);
|
|
}
|
|
if (it->second.coins.vout.size() <= outpoint.n) {
|
|
it->second.coins.vout.resize(outpoint.n + 1);
|
|
}
|
|
it->second.coins.vout[outpoint.n] = std::move(coin.out);
|
|
it->second.coins.nHeight = coin.nHeight;
|
|
it->second.coins.fCoinBase = coin.fCoinBase;
|
|
it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
|
|
cachedCoinsUsage += it->second.coins.DynamicMemoryUsage();
|
|
}
|
|
|
|
void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight) {
|
|
bool fCoinbase = tx.IsCoinBase();
|
|
const uint256& txid = tx.GetHash();
|
|
for (size_t i = 0; i < tx.vout.size(); ++i) {
|
|
// Pass fCoinbase as the possible_overwrite flag to AddCoin, in order to correctly
|
|
// deal with the pre-BIP30 occurrances of duplicate coinbase transactions.
|
|
cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), fCoinbase);
|
|
}
|
|
}
|
|
|
|
void CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
|
|
CCoinsMap::iterator it = FetchCoins(outpoint.hash);
|
|
if (it == cacheCoins.end()) return;
|
|
cachedCoinsUsage -= it->second.coins.DynamicMemoryUsage();
|
|
if (moveout && it->second.coins.IsAvailable(outpoint.n)) {
|
|
*moveout = Coin(it->second.coins.vout[outpoint.n], it->second.coins.nHeight, it->second.coins.fCoinBase);
|
|
}
|
|
it->second.coins.Spend(outpoint.n); // Ignore return value: SpendCoin has no effect if no UTXO found.
|
|
if (it->second.coins.IsPruned() && it->second.flags & CCoinsCacheEntry::FRESH) {
|
|
cacheCoins.erase(it);
|
|
} else {
|
|
cachedCoinsUsage += it->second.coins.DynamicMemoryUsage();
|
|
it->second.flags |= CCoinsCacheEntry::DIRTY;
|
|
}
|
|
}
|
|
|
|
const CCoins* CCoinsViewCache::AccessCoins(const uint256 &txid) const {
|
|
CCoinsMap::const_iterator it = FetchCoins(txid);
|
|
if (it == cacheCoins.end()) {
|
|
return NULL;
|
|
} else {
|
|
return &it->second.coins;
|
|
}
|
|
}
|
|
|
|
static const Coin coinEmpty;
|
|
|
|
const Coin CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
|
|
CCoinsMap::const_iterator it = FetchCoins(outpoint.hash);
|
|
if (it == cacheCoins.end() || !it->second.coins.IsAvailable(outpoint.n)) {
|
|
return coinEmpty;
|
|
} else {
|
|
return Coin(it->second.coins.vout[outpoint.n], it->second.coins.nHeight, it->second.coins.fCoinBase);
|
|
}
|
|
}
|
|
|
|
|
|
bool CCoinsViewCache::HaveCoins(const uint256 &txid) const {
|
|
CCoinsMap::const_iterator it = FetchCoins(txid);
|
|
// We're using vtx.empty() instead of IsPruned here for performance reasons,
|
|
// as we only care about the case where a transaction was replaced entirely
|
|
// in a reorganization (which wipes vout entirely, as opposed to spending
|
|
// which just cleans individual outputs).
|
|
return (it != cacheCoins.end() && !it->second.coins.vout.empty());
|
|
}
|
|
|
|
bool CCoinsViewCache::HaveCoins(const COutPoint &outpoint) const {
|
|
CCoinsMap::const_iterator it = FetchCoins(outpoint.hash);
|
|
return (it != cacheCoins.end() && it->second.coins.IsAvailable(outpoint.n));
|
|
}
|
|
|
|
bool CCoinsViewCache::HaveCoinsInCache(const uint256 &txid) const {
|
|
CCoinsMap::const_iterator it = cacheCoins.find(txid);
|
|
return it != cacheCoins.end();
|
|
}
|
|
|
|
uint256 CCoinsViewCache::GetBestBlock() const {
|
|
if (hashBlock.IsNull())
|
|
hashBlock = base->GetBestBlock();
|
|
return hashBlock;
|
|
}
|
|
|
|
void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
|
|
hashBlock = hashBlockIn;
|
|
}
|
|
|
|
bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
|
|
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
|
|
if (it->second.flags & CCoinsCacheEntry::DIRTY) { // Ignore non-dirty entries (optimization).
|
|
CCoinsMap::iterator itUs = cacheCoins.find(it->first);
|
|
if (itUs == cacheCoins.end()) {
|
|
// The parent cache does not have an entry, while the child does
|
|
// We can ignore it if it's both FRESH and pruned in the child
|
|
if (!(it->second.flags & CCoinsCacheEntry::FRESH && it->second.coins.IsPruned())) {
|
|
// Otherwise we will need to create it in the parent
|
|
// and move the data up and mark it as dirty
|
|
CCoinsCacheEntry& entry = cacheCoins[it->first];
|
|
entry.coins.swap(it->second.coins);
|
|
cachedCoinsUsage += entry.coins.DynamicMemoryUsage();
|
|
entry.flags = CCoinsCacheEntry::DIRTY;
|
|
// We can mark it FRESH in the parent if it was FRESH in the child
|
|
// Otherwise it might have just been flushed from the parent's cache
|
|
// and already exist in the grandparent
|
|
if (it->second.flags & CCoinsCacheEntry::FRESH)
|
|
entry.flags |= CCoinsCacheEntry::FRESH;
|
|
}
|
|
} else {
|
|
// Assert that the child cache entry was not marked FRESH if the
|
|
// parent cache entry has unspent outputs. If this ever happens,
|
|
// it means the FRESH flag was misapplied and there is a logic
|
|
// error in the calling code.
|
|
if ((it->second.flags & CCoinsCacheEntry::FRESH) && !itUs->second.coins.IsPruned())
|
|
throw std::logic_error("FRESH flag misapplied to cache entry for base transaction with spendable outputs");
|
|
|
|
// Found the entry in the parent cache
|
|
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.
|
|
cachedCoinsUsage -= itUs->second.coins.DynamicMemoryUsage();
|
|
cacheCoins.erase(itUs);
|
|
} else {
|
|
// A normal modification.
|
|
cachedCoinsUsage -= itUs->second.coins.DynamicMemoryUsage();
|
|
itUs->second.coins.swap(it->second.coins);
|
|
cachedCoinsUsage += itUs->second.coins.DynamicMemoryUsage();
|
|
itUs->second.flags |= CCoinsCacheEntry::DIRTY;
|
|
// NOTE: It is possible the child has a FRESH flag here in
|
|
// the event the entry we found in the parent is pruned. But
|
|
// we must not copy that FRESH flag to the parent as that
|
|
// pruned state likely still needs to be communicated to the
|
|
// grandparent.
|
|
}
|
|
}
|
|
}
|
|
CCoinsMap::iterator itOld = it++;
|
|
mapCoins.erase(itOld);
|
|
}
|
|
hashBlock = hashBlockIn;
|
|
return true;
|
|
}
|
|
|
|
bool CCoinsViewCache::Flush() {
|
|
bool fOk = base->BatchWrite(cacheCoins, hashBlock);
|
|
cacheCoins.clear();
|
|
cachedCoinsUsage = 0;
|
|
return fOk;
|
|
}
|
|
|
|
void CCoinsViewCache::Uncache(const uint256& hash)
|
|
{
|
|
CCoinsMap::iterator it = cacheCoins.find(hash);
|
|
if (it != cacheCoins.end() && it->second.flags == 0) {
|
|
cachedCoinsUsage -= it->second.coins.DynamicMemoryUsage();
|
|
cacheCoins.erase(it);
|
|
}
|
|
}
|
|
|
|
unsigned int CCoinsViewCache::GetCacheSize() const {
|
|
return cacheCoins.size();
|
|
}
|
|
|
|
const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
|
|
{
|
|
const CCoins* coins = AccessCoins(input.prevout.hash);
|
|
assert(coins && coins->IsAvailable(input.prevout.n));
|
|
return coins->vout[input.prevout.n];
|
|
}
|
|
|
|
CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
|
|
{
|
|
if (tx.IsCoinBase())
|
|
return 0;
|
|
|
|
CAmount nResult = 0;
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++)
|
|
nResult += GetOutputFor(tx.vin[i]).nValue;
|
|
|
|
return nResult;
|
|
}
|
|
|
|
bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
|
|
{
|
|
if (!tx.IsCoinBase()) {
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++) {
|
|
const COutPoint &prevout = tx.vin[i].prevout;
|
|
const CCoins* coins = AccessCoins(prevout.hash);
|
|
if (!coins || !coins->IsAvailable(prevout.n)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
CCoinsViewCursor::~CCoinsViewCursor()
|
|
{
|
|
}
|
|
|
|
static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_BASE_SIZE / ::GetSerializeSize(CTxOut(), SER_NETWORK, PROTOCOL_VERSION); // TODO: merge with similar definition in undo.h.
|
|
|
|
const Coin AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
|
|
{
|
|
COutPoint iter(txid, 0);
|
|
while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
|
|
const Coin& alternate = view.AccessCoin(iter);
|
|
if (!alternate.IsPruned()) return alternate;
|
|
++iter.n;
|
|
}
|
|
return coinEmpty;
|
|
}
|