2014-12-16 17:47:57 -08:00
|
|
|
// Copyright (c) 2012-2014 The Bitcoin Core developers
|
2014-10-30 17:43:19 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2013-11-04 17:47:07 -08:00
|
|
|
|
|
|
|
#include "coins.h"
|
|
|
|
|
2015-05-03 16:31:11 -07:00
|
|
|
#include "memusage.h"
|
2014-07-09 08:25:09 -07:00
|
|
|
#include "random.h"
|
2016-11-23 11:39:01 -08:00
|
|
|
#include "version.h"
|
2017-04-13 14:40:20 -07:00
|
|
|
#include "policy/fees.h"
|
2014-07-09 08:25:09 -07:00
|
|
|
|
2013-11-04 17:47:07 -08:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-10-30 17:43:19 -07: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-04 17:47:07 -08: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;
|
|
|
|
}
|
|
|
|
|
2014-10-18 17:57:02 -07:00
|
|
|
bool CCoins::Spend(uint32_t nPos)
|
|
|
|
{
|
|
|
|
if (nPos >= vout.size() || vout[nPos].IsNull())
|
2013-11-04 17:47:07 -08:00
|
|
|
return false;
|
2014-10-18 17:57:02 -07:00
|
|
|
vout[nPos].SetNull();
|
2013-11-04 17:47:07 -08:00
|
|
|
Cleanup();
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-01 09:31:09 -07:00
|
|
|
bool CCoinsView::GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const { return false; }
|
|
|
|
bool CCoinsView::GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const { return false; }
|
2018-04-25 16:55:33 -07:00
|
|
|
bool CCoinsView::GetNullifier(const uint256 &nullifier, ShieldedType type) const { return false; }
|
2014-07-19 07:42:48 -07:00
|
|
|
bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) const { return false; }
|
|
|
|
bool CCoinsView::HaveCoins(const uint256 &txid) const { return false; }
|
2014-12-15 00:11:16 -08:00
|
|
|
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
|
2018-04-27 15:00:21 -07:00
|
|
|
uint256 CCoinsView::GetBestAnchor(ShieldedType type) const { return uint256(); };
|
2019-11-22 00:25:12 -08:00
|
|
|
HistoryIndex CCoinsView::GetHistoryLength(uint32_t epochId) const { return 0; }
|
|
|
|
HistoryNode CCoinsView::GetHistoryAt(uint32_t epochId, HistoryIndex index) const { return HistoryNode(); }
|
|
|
|
uint256 CCoinsView::GetHistoryRoot(uint32_t epochId) const { return uint256(); }
|
|
|
|
|
2016-01-04 01:54:47 -08:00
|
|
|
bool CCoinsView::BatchWrite(CCoinsMap &mapCoins,
|
|
|
|
const uint256 &hashBlock,
|
2018-04-25 18:15:30 -07:00
|
|
|
const uint256 &hashSproutAnchor,
|
2018-04-27 15:00:21 -07:00
|
|
|
const uint256 &hashSaplingAnchor,
|
2018-04-25 17:38:34 -07:00
|
|
|
CAnchorsSproutMap &mapSproutAnchors,
|
2018-05-06 17:45:05 -07:00
|
|
|
CAnchorsSaplingMap &mapSaplingAnchors,
|
2018-04-25 10:26:38 -07:00
|
|
|
CNullifiersMap &mapSproutNullifiers,
|
2019-11-22 00:25:12 -08:00
|
|
|
CNullifiersMap &mapSaplingNullifiers,
|
|
|
|
CHistoryCacheMap &historyCacheMap) { return false; }
|
2014-07-19 07:42:48 -07:00
|
|
|
bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; }
|
2013-11-04 17:47:07 -08:00
|
|
|
|
|
|
|
|
2014-09-23 18:19:04 -07:00
|
|
|
CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2018-08-01 09:31:09 -07:00
|
|
|
bool CCoinsViewBacked::GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const { return base->GetSproutAnchorAt(rt, tree); }
|
|
|
|
bool CCoinsViewBacked::GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const { return base->GetSaplingAnchorAt(rt, tree); }
|
2018-04-25 16:55:33 -07:00
|
|
|
bool CCoinsViewBacked::GetNullifier(const uint256 &nullifier, ShieldedType type) const { return base->GetNullifier(nullifier, type); }
|
2014-07-19 07:42:48 -07: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(); }
|
2018-04-27 15:00:21 -07:00
|
|
|
uint256 CCoinsViewBacked::GetBestAnchor(ShieldedType type) const { return base->GetBestAnchor(type); }
|
2019-11-22 00:25:12 -08:00
|
|
|
HistoryIndex CCoinsViewBacked::GetHistoryLength(uint32_t epochId) const { return base->GetHistoryLength(epochId); }
|
|
|
|
HistoryNode CCoinsViewBacked::GetHistoryAt(uint32_t epochId, HistoryIndex index) const { return base->GetHistoryAt(epochId, index); }
|
|
|
|
uint256 CCoinsViewBacked::GetHistoryRoot(uint32_t epochId) const { return base->GetHistoryRoot(epochId); }
|
2013-11-04 17:47:07 -08:00
|
|
|
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
|
2016-01-04 01:54:47 -08:00
|
|
|
bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins,
|
|
|
|
const uint256 &hashBlock,
|
2018-04-25 18:15:30 -07:00
|
|
|
const uint256 &hashSproutAnchor,
|
2018-04-27 15:00:21 -07:00
|
|
|
const uint256 &hashSaplingAnchor,
|
2018-04-25 17:38:34 -07:00
|
|
|
CAnchorsSproutMap &mapSproutAnchors,
|
2018-05-06 17:45:05 -07:00
|
|
|
CAnchorsSaplingMap &mapSaplingAnchors,
|
2018-04-25 10:26:38 -07:00
|
|
|
CNullifiersMap &mapSproutNullifiers,
|
2019-11-22 00:25:12 -08:00
|
|
|
CNullifiersMap &mapSaplingNullifiers,
|
|
|
|
CHistoryCacheMap &historyCacheMap) { return base->BatchWrite(mapCoins, hashBlock, hashSproutAnchor, hashSaplingAnchor, mapSproutAnchors, mapSaplingAnchors, mapSproutNullifiers, mapSaplingNullifiers, historyCacheMap); }
|
2014-07-19 07:42:48 -07:00
|
|
|
bool CCoinsViewBacked::GetStats(CCoinsStats &stats) const { return base->GetStats(stats); }
|
2013-11-04 17:47:07 -08:00
|
|
|
|
2014-07-09 08:25:09 -07:00
|
|
|
CCoinsKeyHasher::CCoinsKeyHasher() : salt(GetRandHash()) {}
|
|
|
|
|
2015-05-03 16:31:11 -07:00
|
|
|
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false), cachedCoinsUsage(0) { }
|
2013-11-04 17:47:07 -08:00
|
|
|
|
2014-09-03 00:01:24 -07:00
|
|
|
CCoinsViewCache::~CCoinsViewCache()
|
|
|
|
{
|
|
|
|
assert(!hasModifier);
|
2013-11-04 17:47:07 -08:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:31:11 -07:00
|
|
|
size_t CCoinsViewCache::DynamicMemoryUsage() const {
|
2016-06-27 09:52:34 -07:00
|
|
|
return memusage::DynamicUsage(cacheCoins) +
|
2018-04-25 17:38:34 -07:00
|
|
|
memusage::DynamicUsage(cacheSproutAnchors) +
|
2018-05-06 17:45:05 -07:00
|
|
|
memusage::DynamicUsage(cacheSaplingAnchors) +
|
2018-04-25 10:26:38 -07:00
|
|
|
memusage::DynamicUsage(cacheSproutNullifiers) +
|
2018-04-18 12:18:57 -07:00
|
|
|
memusage::DynamicUsage(cacheSaplingNullifiers) +
|
2019-11-22 00:25:12 -08:00
|
|
|
memusage::DynamicUsage(historyCacheMap) +
|
2016-06-27 09:52:34 -07:00
|
|
|
cachedCoinsUsage;
|
2015-05-03 16:31:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 00:01:24 -07:00
|
|
|
CCoinsMap::const_iterator CCoinsViewCache::FetchCoins(const uint256 &txid) const {
|
2014-07-09 08:25:09 -07:00
|
|
|
CCoinsMap::iterator it = cacheCoins.find(txid);
|
|
|
|
if (it != cacheCoins.end())
|
2013-11-04 17:47:07 -08:00
|
|
|
return it;
|
|
|
|
CCoins tmp;
|
2014-09-03 00:37:47 -07:00
|
|
|
if (!base->GetCoins(txid, tmp))
|
2013-11-04 17:47:07 -08:00
|
|
|
return cacheCoins.end();
|
2014-09-03 00:37:47 -07: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;
|
|
|
|
}
|
2015-07-17 10:46:18 -07:00
|
|
|
cachedCoinsUsage += ret->second.coins.DynamicMemoryUsage();
|
2013-11-04 17:47:07 -08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2018-08-01 09:31:09 -07:00
|
|
|
bool CCoinsViewCache::GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const {
|
2018-04-25 17:38:34 -07:00
|
|
|
CAnchorsSproutMap::const_iterator it = cacheSproutAnchors.find(rt);
|
|
|
|
if (it != cacheSproutAnchors.end()) {
|
2016-01-04 01:54:47 -08:00
|
|
|
if (it->second.entered) {
|
2016-04-02 22:31:26 -07:00
|
|
|
tree = it->second.tree;
|
2016-01-04 01:54:47 -08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 14:00:00 -07:00
|
|
|
if (!base->GetSproutAnchorAt(rt, tree)) {
|
2016-01-04 01:54:47 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:38:34 -07:00
|
|
|
CAnchorsSproutMap::iterator ret = cacheSproutAnchors.insert(std::make_pair(rt, CAnchorsSproutCacheEntry())).first;
|
2016-01-14 14:56:33 -08:00
|
|
|
ret->second.entered = true;
|
2016-04-02 22:31:26 -07:00
|
|
|
ret->second.tree = tree;
|
2017-03-13 21:42:54 -07:00
|
|
|
cachedCoinsUsage += ret->second.tree.DynamicMemoryUsage();
|
2016-01-04 01:54:47 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-01 09:31:09 -07:00
|
|
|
bool CCoinsViewCache::GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const {
|
2018-05-06 17:45:05 -07:00
|
|
|
CAnchorsSaplingMap::const_iterator it = cacheSaplingAnchors.find(rt);
|
|
|
|
if (it != cacheSaplingAnchors.end()) {
|
|
|
|
if (it->second.entered) {
|
|
|
|
tree = it->second.tree;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!base->GetSaplingAnchorAt(rt, tree)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAnchorsSaplingMap::iterator ret = cacheSaplingAnchors.insert(std::make_pair(rt, CAnchorsSaplingCacheEntry())).first;
|
|
|
|
ret->second.entered = true;
|
|
|
|
ret->second.tree = tree;
|
|
|
|
cachedCoinsUsage += ret->second.tree.DynamicMemoryUsage();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:55:33 -07:00
|
|
|
bool CCoinsViewCache::GetNullifier(const uint256 &nullifier, ShieldedType type) const {
|
2018-04-23 13:37:17 -07:00
|
|
|
CNullifiersMap* cacheToUse;
|
|
|
|
switch (type) {
|
2018-04-25 16:55:33 -07:00
|
|
|
case SPROUT:
|
2018-04-25 10:26:38 -07:00
|
|
|
cacheToUse = &cacheSproutNullifiers;
|
2018-04-23 13:37:17 -07:00
|
|
|
break;
|
2018-04-25 16:55:33 -07:00
|
|
|
case SAPLING:
|
2018-04-23 13:37:17 -07:00
|
|
|
cacheToUse = &cacheSaplingNullifiers;
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-07 12:49:17 -07:00
|
|
|
throw std::runtime_error("Unknown shielded type");
|
2018-04-23 13:37:17 -07:00
|
|
|
}
|
2018-04-18 12:18:57 -07:00
|
|
|
CNullifiersMap::iterator it = cacheToUse->find(nullifier);
|
|
|
|
if (it != cacheToUse->end())
|
2016-01-04 02:34:58 -08:00
|
|
|
return it->second.entered;
|
|
|
|
|
2016-07-14 16:02:39 -07:00
|
|
|
CNullifiersCacheEntry entry;
|
2018-04-23 13:37:17 -07:00
|
|
|
bool tmp = base->GetNullifier(nullifier, type);
|
2016-01-04 02:34:58 -08:00
|
|
|
entry.entered = tmp;
|
|
|
|
|
2018-04-18 12:18:57 -07:00
|
|
|
cacheToUse->insert(std::make_pair(nullifier, entry));
|
2016-01-04 02:34:58 -08:00
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2019-11-22 00:25:12 -08:00
|
|
|
HistoryIndex CCoinsViewCache::GetHistoryLength(uint32_t epochId) const {
|
|
|
|
HistoryCache& historyCache = SelectHistoryCache(epochId);
|
|
|
|
return historyCache.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryNode CCoinsViewCache::GetHistoryAt(uint32_t epochId, HistoryIndex index) const {
|
|
|
|
HistoryCache& historyCache = SelectHistoryCache(epochId);
|
|
|
|
|
|
|
|
if (index >= historyCache.length) {
|
|
|
|
// Caller should ensure that it is limiting history
|
|
|
|
// request to 0..GetHistoryLength(epochId)-1 range
|
|
|
|
throw std::runtime_error("Invalid history request");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= historyCache.updateDepth) {
|
|
|
|
return historyCache.appends[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return base->GetHistoryAt(epochId, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 CCoinsViewCache::GetHistoryRoot(uint32_t epochId) const {
|
|
|
|
return SelectHistoryCache(epochId).root;
|
|
|
|
}
|
|
|
|
|
2018-04-27 15:52:33 -07:00
|
|
|
template<typename Tree, typename Cache, typename CacheIterator, typename CacheEntry>
|
|
|
|
void CCoinsViewCache::AbstractPushAnchor(
|
|
|
|
const Tree &tree,
|
|
|
|
ShieldedType type,
|
|
|
|
Cache &cacheAnchors,
|
|
|
|
uint256 &hash
|
|
|
|
)
|
|
|
|
{
|
2016-04-02 22:31:26 -07:00
|
|
|
uint256 newrt = tree.root();
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2018-04-27 15:52:33 -07:00
|
|
|
auto currentRoot = GetBestAnchor(type);
|
2016-01-04 01:54:47 -08:00
|
|
|
|
|
|
|
// We don't want to overwrite an anchor we already have.
|
2018-05-07 12:43:55 -07:00
|
|
|
// This occurs when a block doesn't modify mapAnchors at all,
|
2016-07-14 15:10:41 -07:00
|
|
|
// because there are no joinsplits. We could get around this a
|
2018-05-07 12:43:55 -07:00
|
|
|
// different way (make all blocks modify mapAnchors somehow)
|
2016-01-04 01:54:47 -08:00
|
|
|
// but this is simpler to reason about.
|
|
|
|
if (currentRoot != newrt) {
|
2018-04-27 15:52:33 -07:00
|
|
|
auto insertRet = cacheAnchors.insert(std::make_pair(newrt, CacheEntry()));
|
|
|
|
CacheIterator ret = insertRet.first;
|
2016-01-04 01:54:47 -08:00
|
|
|
|
|
|
|
ret->second.entered = true;
|
2016-04-02 22:31:26 -07:00
|
|
|
ret->second.tree = tree;
|
2018-04-27 15:52:33 -07:00
|
|
|
ret->second.flags = CacheEntry::DIRTY;
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2016-06-27 09:52:34 -07:00
|
|
|
if (insertRet.second) {
|
|
|
|
// An insert took place
|
2017-03-13 21:42:54 -07:00
|
|
|
cachedCoinsUsage += ret->second.tree.DynamicMemoryUsage();
|
2016-06-27 09:52:34 -07:00
|
|
|
}
|
|
|
|
|
2018-04-27 15:52:33 -07:00
|
|
|
hash = newrt;
|
2016-01-04 01:54:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 09:31:09 -07:00
|
|
|
template<> void CCoinsViewCache::PushAnchor(const SproutMerkleTree &tree)
|
2018-05-16 09:31:53 -07:00
|
|
|
{
|
2018-08-01 09:31:09 -07:00
|
|
|
AbstractPushAnchor<SproutMerkleTree, CAnchorsSproutMap, CAnchorsSproutMap::iterator, CAnchorsSproutCacheEntry>(
|
2018-04-27 15:52:33 -07:00
|
|
|
tree,
|
|
|
|
SPROUT,
|
|
|
|
cacheSproutAnchors,
|
|
|
|
hashSproutAnchor
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-01 09:31:09 -07:00
|
|
|
template<> void CCoinsViewCache::PushAnchor(const SaplingMerkleTree &tree)
|
2018-05-16 09:31:53 -07:00
|
|
|
{
|
2018-08-01 09:31:09 -07:00
|
|
|
AbstractPushAnchor<SaplingMerkleTree, CAnchorsSaplingMap, CAnchorsSaplingMap::iterator, CAnchorsSaplingCacheEntry>(
|
2018-05-06 20:38:45 -07:00
|
|
|
tree,
|
|
|
|
SAPLING,
|
|
|
|
cacheSaplingAnchors,
|
|
|
|
hashSaplingAnchor
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:45:05 -07:00
|
|
|
template<>
|
|
|
|
void CCoinsViewCache::BringBestAnchorIntoCache(
|
|
|
|
const uint256 ¤tRoot,
|
2018-08-01 09:31:09 -07:00
|
|
|
SproutMerkleTree &tree
|
2018-05-06 17:45:05 -07:00
|
|
|
)
|
|
|
|
{
|
|
|
|
assert(GetSproutAnchorAt(currentRoot, tree));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void CCoinsViewCache::BringBestAnchorIntoCache(
|
|
|
|
const uint256 ¤tRoot,
|
2018-08-01 09:31:09 -07:00
|
|
|
SaplingMerkleTree &tree
|
2018-05-06 17:45:05 -07:00
|
|
|
)
|
|
|
|
{
|
|
|
|
assert(GetSaplingAnchorAt(currentRoot, tree));
|
|
|
|
}
|
|
|
|
|
2019-11-22 00:25:12 -08:00
|
|
|
void draftMMRNode(std::vector<uint32_t> &indices,
|
|
|
|
std::vector<HistoryEntry> &entries,
|
|
|
|
HistoryNode nodeData,
|
|
|
|
uint32_t h,
|
|
|
|
uint32_t peak_pos)
|
|
|
|
{
|
|
|
|
HistoryEntry newEntry = h == 0
|
|
|
|
? libzcash::LeafToEntry(nodeData)
|
|
|
|
// peak_pos - (1 << h) is the mmr position of left child, -1 to that is this position of entry in
|
|
|
|
// array representation.
|
|
|
|
//
|
|
|
|
// peak_pos - 1 is the mmr position of right child, -1 to that is this position of entry in
|
|
|
|
// array representation
|
|
|
|
: libzcash::NodeToEntry(nodeData, peak_pos - (1 << h) - 1, peak_pos - 2);
|
|
|
|
|
|
|
|
indices.push_back(peak_pos - 1);
|
|
|
|
entries.push_back(newEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t log2i(uint32_t x) {
|
|
|
|
assert(x > 0);
|
2020-03-14 01:43:25 -07:00
|
|
|
int log = 0;
|
|
|
|
while (x >>= 1) ++log;
|
|
|
|
return log;
|
2019-11-22 00:25:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Computes floor(log2(x+1))
|
|
|
|
static inline uint32_t floor_log2i(uint32_t x) {
|
|
|
|
return log2i(x + 1) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CCoinsViewCache::PreloadHistoryTree(uint32_t epochId, bool extra, std::vector<HistoryEntry> &entries, std::vector<uint32_t> &entry_indices) {
|
|
|
|
auto treeLength = GetHistoryLength(epochId);
|
|
|
|
|
|
|
|
if (treeLength <= 0) {
|
|
|
|
throw std::runtime_error("Invalid PreloadHistoryTree state called - tree should exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t last_peak_pos = 0;
|
|
|
|
uint32_t last_peak_h = 0;
|
|
|
|
uint32_t h = 0;
|
|
|
|
uint32_t peak_pos = 0;
|
|
|
|
uint32_t total_peaks = 0;
|
|
|
|
|
|
|
|
if (treeLength == 1) {
|
|
|
|
entries.push_back(libzcash::LeafToEntry(GetHistoryAt(epochId, 0)));
|
|
|
|
entry_indices.push_back(0);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
// First possible peak is calculated above.
|
|
|
|
h = floor_log2i(treeLength);
|
|
|
|
peak_pos = (1 << (h + 1)) - 1;
|
|
|
|
|
|
|
|
// Collecting all peaks starting from first possible one.
|
|
|
|
while (h != 0) {
|
|
|
|
|
|
|
|
// If peak_pos is out of bounds of the tree, left child of it calculated,
|
|
|
|
// and that means that we drop down one level in the tree.
|
|
|
|
if (peak_pos > treeLength) {
|
|
|
|
// left child, -2^h
|
|
|
|
peak_pos = peak_pos - (1 << h);
|
|
|
|
h = h - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the peak exists, we take it and then continue with its right sibling
|
|
|
|
// (which may not exist and that will be covered in next iteration).
|
|
|
|
if (peak_pos <= treeLength) {
|
|
|
|
draftMMRNode(entry_indices, entries, GetHistoryAt(epochId, peak_pos-1), h, peak_pos);
|
|
|
|
|
|
|
|
last_peak_pos = peak_pos;
|
|
|
|
last_peak_h = h;
|
|
|
|
|
|
|
|
// right sibling
|
|
|
|
peak_pos = peak_pos + (1 << (h + 1)) - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
total_peaks = entries.size();
|
|
|
|
|
|
|
|
// early return if we don't extra nodes
|
|
|
|
if (!extra) return total_peaks;
|
|
|
|
|
|
|
|
h = last_peak_h;
|
|
|
|
peak_pos = last_peak_pos;
|
|
|
|
|
|
|
|
|
|
|
|
// P
|
|
|
|
// / \
|
|
|
|
// / \
|
|
|
|
// / \ \
|
|
|
|
// / \ \
|
|
|
|
// _A_ \ \
|
|
|
|
// / \_ B \
|
|
|
|
// / \ / \ / \ C
|
|
|
|
// /\ /\ /\ /\ /\ /\ /\
|
|
|
|
// D E
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// For extra peaks needed for deletion, we do extra pass on right slope of the last peak
|
|
|
|
// and add those nodes + their siblings. Extra would be (D, E) for the picture above.
|
|
|
|
while (h > 0) {
|
|
|
|
uint32_t left_pos = peak_pos - (1<<h);
|
|
|
|
uint32_t right_pos = peak_pos - 1;
|
|
|
|
h = h - 1;
|
|
|
|
|
|
|
|
// drafting left child
|
|
|
|
draftMMRNode(entry_indices, entries, GetHistoryAt(epochId, left_pos-1), h, left_pos);
|
|
|
|
|
|
|
|
// drafting right child
|
|
|
|
draftMMRNode(entry_indices, entries, GetHistoryAt(epochId, right_pos-1), h, right_pos);
|
|
|
|
|
|
|
|
// continuing on right slope
|
|
|
|
peak_pos = right_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_peaks;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryCache& CCoinsViewCache::SelectHistoryCache(uint32_t epochId) const {
|
|
|
|
auto entry = historyCacheMap.find(epochId);
|
|
|
|
|
|
|
|
if (entry != historyCacheMap.end()) {
|
|
|
|
return entry->second;
|
|
|
|
} else {
|
|
|
|
auto cache = HistoryCache(
|
|
|
|
base->GetHistoryLength(epochId),
|
|
|
|
base->GetHistoryRoot(epochId),
|
|
|
|
epochId
|
|
|
|
);
|
|
|
|
return historyCacheMap.insert({epochId, cache}).first->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCoinsViewCache::PushHistoryNode(uint32_t epochId, const HistoryNode node) {
|
|
|
|
HistoryCache& historyCache = SelectHistoryCache(epochId);
|
|
|
|
|
|
|
|
if (historyCache.length == 0) {
|
|
|
|
// special case, it just goes into the cache right away
|
|
|
|
historyCache.Extend(node);
|
|
|
|
|
|
|
|
if (librustzcash_mmr_hash_node(epochId, node.data(), historyCache.root.begin()) != 0) {
|
|
|
|
throw std::runtime_error("hashing node failed");
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HistoryEntry> entries;
|
|
|
|
std::vector<uint32_t> entry_indices;
|
|
|
|
|
|
|
|
PreloadHistoryTree(epochId, false, entries, entry_indices);
|
|
|
|
|
|
|
|
uint256 newRoot;
|
|
|
|
std::array<HistoryNode, 32> appendBuf;
|
|
|
|
|
|
|
|
uint32_t appends = librustzcash_mmr_append(
|
|
|
|
epochId,
|
|
|
|
historyCache.length,
|
|
|
|
entry_indices.data(),
|
|
|
|
entries.data()->data(),
|
|
|
|
entry_indices.size(),
|
|
|
|
node.data(),
|
|
|
|
newRoot.begin(),
|
|
|
|
appendBuf.data()->data()
|
|
|
|
);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < appends; i++) {
|
|
|
|
historyCache.Extend(appendBuf[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
historyCache.root = newRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCoinsViewCache::PopHistoryNode(uint32_t epochId) {
|
|
|
|
HistoryCache& historyCache = SelectHistoryCache(epochId);
|
|
|
|
uint256 newRoot;
|
|
|
|
|
|
|
|
switch (historyCache.length) {
|
|
|
|
case 0:
|
|
|
|
// Caller is not expected to pop from empty tree! Caller should
|
|
|
|
// switch to previous epoch and pop history from there.
|
|
|
|
throw std::runtime_error("popping history node from empty history");
|
|
|
|
case 1:
|
|
|
|
// Just resetting tree to empty
|
|
|
|
historyCache.Truncate(0);
|
|
|
|
historyCache.root = uint256();
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
// - A tree with one leaf has length 1.
|
|
|
|
// - A tree with two leaves has length 3.
|
|
|
|
throw std::runtime_error("a history tree cannot have two nodes");
|
|
|
|
case 3:
|
|
|
|
// After removing a leaf from a tree with two leaves, we are left
|
|
|
|
// with a single-node tree, whose root is just the hash of that
|
|
|
|
// node.
|
|
|
|
if (librustzcash_mmr_hash_node(
|
|
|
|
epochId,
|
|
|
|
GetHistoryAt(epochId, 0).data(),
|
|
|
|
newRoot.begin()
|
|
|
|
) != 0) {
|
|
|
|
throw std::runtime_error("hashing node failed");
|
|
|
|
}
|
|
|
|
historyCache.Truncate(1);
|
|
|
|
historyCache.root = newRoot;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
// This is a non-elementary pop, so use the full tree logic.
|
|
|
|
std::vector<HistoryEntry> entries;
|
|
|
|
std::vector<uint32_t> entry_indices;
|
|
|
|
|
|
|
|
uint32_t peak_count = PreloadHistoryTree(epochId, true, entries, entry_indices);
|
|
|
|
|
|
|
|
uint32_t numberOfDeletes = librustzcash_mmr_delete(
|
|
|
|
epochId,
|
|
|
|
historyCache.length,
|
|
|
|
entry_indices.data(),
|
|
|
|
entries.data()->data(),
|
|
|
|
peak_count,
|
|
|
|
entries.size() - peak_count,
|
|
|
|
newRoot.begin()
|
|
|
|
);
|
|
|
|
|
|
|
|
historyCache.Truncate(historyCache.length - numberOfDeletes);
|
|
|
|
historyCache.root = newRoot;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 15:31:12 -07:00
|
|
|
template<typename Tree, typename Cache, typename CacheEntry>
|
|
|
|
void CCoinsViewCache::AbstractPopAnchor(
|
|
|
|
const uint256 &newrt,
|
|
|
|
ShieldedType type,
|
|
|
|
Cache &cacheAnchors,
|
|
|
|
uint256 &hash
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto currentRoot = GetBestAnchor(type);
|
2016-01-04 01:54:47 -08:00
|
|
|
|
|
|
|
// Blocks might not change the commitment tree, in which
|
|
|
|
// case restoring the "old" anchor during a reorg must
|
|
|
|
// have no effect.
|
|
|
|
if (currentRoot != newrt) {
|
2016-12-05 12:42:30 -08:00
|
|
|
// Bring the current best anchor into our local cache
|
|
|
|
// so that its tree exists in memory.
|
|
|
|
{
|
2018-04-27 15:31:12 -07:00
|
|
|
Tree tree;
|
2018-05-06 17:45:05 -07:00
|
|
|
BringBestAnchorIntoCache(currentRoot, tree);
|
2016-12-05 12:42:30 -08:00
|
|
|
}
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2016-12-05 12:42:30 -08:00
|
|
|
// Mark the anchor as unentered, removing it from view
|
2018-04-27 15:31:12 -07:00
|
|
|
cacheAnchors[currentRoot].entered = false;
|
2016-12-05 12:42:30 -08:00
|
|
|
|
|
|
|
// Mark the cache entry as dirty so it's propagated
|
2018-04-27 15:31:12 -07:00
|
|
|
cacheAnchors[currentRoot].flags = CacheEntry::DIRTY;
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2016-12-05 12:42:30 -08:00
|
|
|
// Mark the new root as the best anchor
|
2018-04-27 15:31:12 -07:00
|
|
|
hash = newrt;
|
2016-01-04 01:54:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:05:11 -07:00
|
|
|
void CCoinsViewCache::PopAnchor(const uint256 &newrt, ShieldedType type) {
|
|
|
|
switch (type) {
|
|
|
|
case SPROUT:
|
2018-08-01 09:31:09 -07:00
|
|
|
AbstractPopAnchor<SproutMerkleTree, CAnchorsSproutMap, CAnchorsSproutCacheEntry>(
|
2018-05-06 18:05:11 -07:00
|
|
|
newrt,
|
|
|
|
SPROUT,
|
|
|
|
cacheSproutAnchors,
|
|
|
|
hashSproutAnchor
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case SAPLING:
|
2018-08-01 09:31:09 -07:00
|
|
|
AbstractPopAnchor<SaplingMerkleTree, CAnchorsSaplingMap, CAnchorsSaplingCacheEntry>(
|
2018-05-06 18:05:11 -07:00
|
|
|
newrt,
|
|
|
|
SAPLING,
|
|
|
|
cacheSaplingAnchors,
|
|
|
|
hashSaplingAnchor
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-07 12:49:17 -07:00
|
|
|
throw std::runtime_error("Unknown shielded type");
|
2018-05-06 18:05:11 -07:00
|
|
|
}
|
2018-04-27 15:31:12 -07:00
|
|
|
}
|
|
|
|
|
2018-04-18 12:18:57 -07:00
|
|
|
void CCoinsViewCache::SetNullifiers(const CTransaction& tx, bool spent) {
|
2019-06-16 04:39:05 -07:00
|
|
|
for (const JSDescription &joinsplit : tx.vJoinSplit) {
|
2018-04-18 12:18:57 -07:00
|
|
|
for (const uint256 &nullifier : joinsplit.nullifiers) {
|
2018-04-25 10:26:38 -07:00
|
|
|
std::pair<CNullifiersMap::iterator, bool> ret = cacheSproutNullifiers.insert(std::make_pair(nullifier, CNullifiersCacheEntry()));
|
2018-04-18 12:18:57 -07:00
|
|
|
ret.first->second.entered = spent;
|
|
|
|
ret.first->second.flags |= CNullifiersCacheEntry::DIRTY;
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 16:16:43 -07:00
|
|
|
for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
|
|
|
|
std::pair<CNullifiersMap::iterator, bool> ret = cacheSaplingNullifiers.insert(std::make_pair(spendDescription.nullifier, CNullifiersCacheEntry()));
|
|
|
|
ret.first->second.entered = spent;
|
|
|
|
ret.first->second.flags |= CNullifiersCacheEntry::DIRTY;
|
|
|
|
}
|
2016-01-04 02:34:58 -08:00
|
|
|
}
|
|
|
|
|
2014-09-03 00:37:47 -07: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 07:42:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 00:01:24 -07:00
|
|
|
CCoinsModifier CCoinsViewCache::ModifyCoins(const uint256 &txid) {
|
|
|
|
assert(!hasModifier);
|
2014-09-03 00:37:47 -07:00
|
|
|
std::pair<CCoinsMap::iterator, bool> ret = cacheCoins.insert(std::make_pair(txid, CCoinsCacheEntry()));
|
2015-05-03 16:31:11 -07:00
|
|
|
size_t cachedCoinUsage = 0;
|
2014-09-03 00:01:24 -07:00
|
|
|
if (ret.second) {
|
2014-09-03 00:37:47 -07: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;
|
|
|
|
}
|
2015-05-03 16:31:11 -07:00
|
|
|
} else {
|
2015-07-17 10:46:18 -07:00
|
|
|
cachedCoinUsage = ret.first->second.coins.DynamicMemoryUsage();
|
2014-09-03 00:01:24 -07:00
|
|
|
}
|
2014-09-03 00:37:47 -07:00
|
|
|
// Assume that whenever ModifyCoins is called, the entry will be modified.
|
|
|
|
ret.first->second.flags |= CCoinsCacheEntry::DIRTY;
|
2015-05-03 16:31:11 -07:00
|
|
|
return CCoinsModifier(*this, ret.first, cachedCoinUsage);
|
2013-11-04 17:47:07 -08:00
|
|
|
}
|
|
|
|
|
2015-11-02 18:27:15 -08:00
|
|
|
CCoinsModifier CCoinsViewCache::ModifyNewCoins(const uint256 &txid) {
|
|
|
|
assert(!hasModifier);
|
|
|
|
std::pair<CCoinsMap::iterator, bool> ret = cacheCoins.insert(std::make_pair(txid, CCoinsCacheEntry()));
|
|
|
|
ret.first->second.coins.Clear();
|
|
|
|
ret.first->second.flags = CCoinsCacheEntry::FRESH;
|
|
|
|
ret.first->second.flags |= CCoinsCacheEntry::DIRTY;
|
|
|
|
return CCoinsModifier(*this, ret.first, 0);
|
|
|
|
}
|
|
|
|
|
2014-09-02 12:21:15 -07: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 00:37:47 -07:00
|
|
|
return &it->second.coins;
|
2014-09-02 12:21:15 -07:00
|
|
|
}
|
2014-07-19 07:42:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCoinsViewCache::HaveCoins(const uint256 &txid) const {
|
|
|
|
CCoinsMap::const_iterator it = FetchCoins(txid);
|
2014-07-10 08:35:22 -07:00
|
|
|
// We're using vtx.empty() instead of IsPruned here for performance reasons,
|
2014-10-30 17:43:19 -07:00
|
|
|
// as we only care about the case where a transaction was replaced entirely
|
2014-07-10 08:35:22 -07:00
|
|
|
// in a reorganization (which wipes vout entirely, as opposed to spending
|
|
|
|
// which just cleans individual outputs).
|
2014-09-03 00:37:47 -07:00
|
|
|
return (it != cacheCoins.end() && !it->second.coins.vout.empty());
|
2013-11-04 17:47:07 -08:00
|
|
|
}
|
|
|
|
|
2014-07-19 07:42:48 -07:00
|
|
|
uint256 CCoinsViewCache::GetBestBlock() const {
|
2014-12-15 00:11:16 -08:00
|
|
|
if (hashBlock.IsNull())
|
2013-11-04 17:47:07 -08:00
|
|
|
hashBlock = base->GetBestBlock();
|
|
|
|
return hashBlock;
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2018-04-27 15:00:21 -07:00
|
|
|
uint256 CCoinsViewCache::GetBestAnchor(ShieldedType type) const {
|
|
|
|
switch (type) {
|
|
|
|
case SPROUT:
|
|
|
|
if (hashSproutAnchor.IsNull())
|
|
|
|
hashSproutAnchor = base->GetBestAnchor(type);
|
|
|
|
return hashSproutAnchor;
|
|
|
|
break;
|
|
|
|
case SAPLING:
|
|
|
|
if (hashSaplingAnchor.IsNull())
|
|
|
|
hashSaplingAnchor = base->GetBestAnchor(type);
|
|
|
|
return hashSaplingAnchor;
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-07 12:49:17 -07:00
|
|
|
throw std::runtime_error("Unknown shielded type");
|
2018-04-27 15:00:21 -07:00
|
|
|
}
|
2016-01-04 01:54:47 -08:00
|
|
|
}
|
|
|
|
|
2014-09-03 00:25:32 -07:00
|
|
|
void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
|
2013-11-04 17:47:07 -08:00
|
|
|
hashBlock = hashBlockIn;
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:18:57 -07:00
|
|
|
void BatchWriteNullifiers(CNullifiersMap &mapNullifiers, CNullifiersMap &cacheNullifiers)
|
|
|
|
{
|
|
|
|
for (CNullifiersMap::iterator child_it = mapNullifiers.begin(); child_it != mapNullifiers.end();) {
|
|
|
|
if (child_it->second.flags & CNullifiersCacheEntry::DIRTY) { // Ignore non-dirty entries (optimization).
|
|
|
|
CNullifiersMap::iterator parent_it = cacheNullifiers.find(child_it->first);
|
|
|
|
|
|
|
|
if (parent_it == cacheNullifiers.end()) {
|
|
|
|
CNullifiersCacheEntry& entry = cacheNullifiers[child_it->first];
|
|
|
|
entry.entered = child_it->second.entered;
|
|
|
|
entry.flags = CNullifiersCacheEntry::DIRTY;
|
|
|
|
} else {
|
|
|
|
if (parent_it->second.entered != child_it->second.entered) {
|
|
|
|
parent_it->second.entered = child_it->second.entered;
|
|
|
|
parent_it->second.flags |= CNullifiersCacheEntry::DIRTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CNullifiersMap::iterator itOld = child_it++;
|
|
|
|
mapNullifiers.erase(itOld);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:45:05 -07:00
|
|
|
template<typename Map, typename MapIterator, typename MapEntry>
|
|
|
|
void BatchWriteAnchors(
|
|
|
|
Map &mapAnchors,
|
|
|
|
Map &cacheAnchors,
|
|
|
|
size_t &cachedCoinsUsage
|
|
|
|
)
|
|
|
|
{
|
|
|
|
for (MapIterator child_it = mapAnchors.begin(); child_it != mapAnchors.end();)
|
|
|
|
{
|
|
|
|
if (child_it->second.flags & MapEntry::DIRTY) {
|
|
|
|
MapIterator parent_it = cacheAnchors.find(child_it->first);
|
|
|
|
|
|
|
|
if (parent_it == cacheAnchors.end()) {
|
|
|
|
MapEntry& entry = cacheAnchors[child_it->first];
|
|
|
|
entry.entered = child_it->second.entered;
|
|
|
|
entry.tree = child_it->second.tree;
|
|
|
|
entry.flags = MapEntry::DIRTY;
|
|
|
|
|
|
|
|
cachedCoinsUsage += entry.tree.DynamicMemoryUsage();
|
|
|
|
} else {
|
|
|
|
if (parent_it->second.entered != child_it->second.entered) {
|
|
|
|
// The parent may have removed the entry.
|
|
|
|
parent_it->second.entered = child_it->second.entered;
|
|
|
|
parent_it->second.flags |= MapEntry::DIRTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MapIterator itOld = child_it++;
|
|
|
|
mapAnchors.erase(itOld);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 00:25:12 -08:00
|
|
|
void BatchWriteHistory(CHistoryCacheMap& historyCacheMap, CHistoryCacheMap& historyCacheMapIn) {
|
|
|
|
for (auto nextHistoryCache = historyCacheMapIn.begin(); nextHistoryCache != historyCacheMapIn.end(); nextHistoryCache++) {
|
|
|
|
auto historyCacheIn = nextHistoryCache->second;
|
|
|
|
auto epochId = nextHistoryCache->first;
|
|
|
|
|
|
|
|
auto historyCache = historyCacheMap.find(epochId);
|
|
|
|
if (historyCache != historyCacheMap.end()) {
|
|
|
|
// delete old entries since updateDepth
|
|
|
|
historyCache->second.Truncate(historyCacheIn.updateDepth);
|
|
|
|
|
|
|
|
// Replace/append new/updated entries. HistoryCache.Extend
|
|
|
|
// auto-indexes the nodes, so we need to extend in the same order as
|
|
|
|
// this cache is indexed.
|
|
|
|
for (size_t i = historyCacheIn.updateDepth; i < historyCacheIn.length; i++) {
|
|
|
|
historyCache->second.Extend(historyCacheIn.appends[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the lengths should now match
|
|
|
|
assert(historyCache->second.length == historyCacheIn.length);
|
|
|
|
|
|
|
|
// write current root
|
|
|
|
historyCache->second.root = historyCacheIn.root;
|
|
|
|
} else {
|
|
|
|
// Just insert the history cache into its parent
|
|
|
|
historyCacheMap.insert({epochId, historyCacheIn});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 01:54:47 -08:00
|
|
|
bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins,
|
|
|
|
const uint256 &hashBlockIn,
|
2018-04-25 18:15:30 -07:00
|
|
|
const uint256 &hashSproutAnchorIn,
|
2018-04-27 15:00:21 -07:00
|
|
|
const uint256 &hashSaplingAnchorIn,
|
2018-04-25 17:38:34 -07:00
|
|
|
CAnchorsSproutMap &mapSproutAnchors,
|
2018-05-06 17:45:05 -07:00
|
|
|
CAnchorsSaplingMap &mapSaplingAnchors,
|
2018-04-25 10:26:38 -07:00
|
|
|
CNullifiersMap &mapSproutNullifiers,
|
2019-11-22 00:25:12 -08:00
|
|
|
CNullifiersMap &mapSaplingNullifiers,
|
|
|
|
CHistoryCacheMap &historyCacheMapIn) {
|
2014-09-03 00:01:24 -07:00
|
|
|
assert(!hasModifier);
|
2014-08-23 17:08:05 -07:00
|
|
|
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
|
2014-09-03 00:37:47 -07: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);
|
2015-07-17 10:46:18 -07:00
|
|
|
cachedCoinsUsage += entry.coins.DynamicMemoryUsage();
|
2014-09-03 00:37:47 -07:00
|
|
|
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.
|
2015-07-17 10:46:18 -07:00
|
|
|
cachedCoinsUsage -= itUs->second.coins.DynamicMemoryUsage();
|
2014-09-03 00:37:47 -07:00
|
|
|
cacheCoins.erase(itUs);
|
|
|
|
} else {
|
|
|
|
// A normal modification.
|
2015-07-17 10:46:18 -07:00
|
|
|
cachedCoinsUsage -= itUs->second.coins.DynamicMemoryUsage();
|
2014-09-03 00:37:47 -07:00
|
|
|
itUs->second.coins.swap(it->second.coins);
|
2015-07-17 10:46:18 -07:00
|
|
|
cachedCoinsUsage += itUs->second.coins.DynamicMemoryUsage();
|
2014-09-03 00:37:47 -07:00
|
|
|
itUs->second.flags |= CCoinsCacheEntry::DIRTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-23 17:08:05 -07:00
|
|
|
CCoinsMap::iterator itOld = it++;
|
|
|
|
mapCoins.erase(itOld);
|
|
|
|
}
|
2016-01-04 01:54:47 -08:00
|
|
|
|
2018-05-06 17:45:05 -07:00
|
|
|
::BatchWriteAnchors<CAnchorsSproutMap, CAnchorsSproutMap::iterator, CAnchorsSproutCacheEntry>(mapSproutAnchors, cacheSproutAnchors, cachedCoinsUsage);
|
|
|
|
::BatchWriteAnchors<CAnchorsSaplingMap, CAnchorsSaplingMap::iterator, CAnchorsSaplingCacheEntry>(mapSaplingAnchors, cacheSaplingAnchors, cachedCoinsUsage);
|
2016-01-04 02:34:58 -08:00
|
|
|
|
2018-04-25 10:26:38 -07:00
|
|
|
::BatchWriteNullifiers(mapSproutNullifiers, cacheSproutNullifiers);
|
2018-04-18 12:18:57 -07:00
|
|
|
::BatchWriteNullifiers(mapSaplingNullifiers, cacheSaplingNullifiers);
|
2016-01-04 02:34:58 -08:00
|
|
|
|
2019-11-22 00:25:12 -08:00
|
|
|
::BatchWriteHistory(historyCacheMap, historyCacheMapIn);
|
|
|
|
|
2018-04-25 18:15:30 -07:00
|
|
|
hashSproutAnchor = hashSproutAnchorIn;
|
2018-04-27 15:00:21 -07:00
|
|
|
hashSaplingAnchor = hashSaplingAnchorIn;
|
2013-11-04 17:47:07 -08:00
|
|
|
hashBlock = hashBlockIn;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCoinsViewCache::Flush() {
|
2019-11-22 00:25:12 -08:00
|
|
|
bool fOk = base->BatchWrite(cacheCoins,
|
|
|
|
hashBlock,
|
|
|
|
hashSproutAnchor,
|
|
|
|
hashSaplingAnchor,
|
|
|
|
cacheSproutAnchors,
|
|
|
|
cacheSaplingAnchors,
|
|
|
|
cacheSproutNullifiers,
|
|
|
|
cacheSaplingNullifiers,
|
|
|
|
historyCacheMap);
|
2014-08-23 17:08:05 -07:00
|
|
|
cacheCoins.clear();
|
2018-04-25 17:38:34 -07:00
|
|
|
cacheSproutAnchors.clear();
|
2018-05-06 17:45:05 -07:00
|
|
|
cacheSaplingAnchors.clear();
|
2018-04-25 10:26:38 -07:00
|
|
|
cacheSproutNullifiers.clear();
|
2018-04-18 12:18:57 -07:00
|
|
|
cacheSaplingNullifiers.clear();
|
2019-11-22 00:25:12 -08:00
|
|
|
historyCacheMap.clear();
|
2015-05-03 16:31:11 -07:00
|
|
|
cachedCoinsUsage = 0;
|
2013-11-04 17:47:07 -08:00
|
|
|
return fOk;
|
|
|
|
}
|
|
|
|
|
2014-07-19 07:42:48 -07:00
|
|
|
unsigned int CCoinsViewCache::GetCacheSize() const {
|
2013-11-04 17:47:07 -08:00
|
|
|
return cacheCoins.size();
|
|
|
|
}
|
|
|
|
|
2014-07-19 07:42:48 -07:00
|
|
|
const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
|
2013-11-04 17:47:07 -08:00
|
|
|
{
|
2014-09-02 12:21:15 -07:00
|
|
|
const CCoins* coins = AccessCoins(input.prevout.hash);
|
|
|
|
assert(coins && coins->IsAvailable(input.prevout.n));
|
|
|
|
return coins->vout[input.prevout.n];
|
2013-11-04 17:47:07 -08:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:46:19 -07:00
|
|
|
CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
|
2013-11-04 17:47:07 -08:00
|
|
|
{
|
|
|
|
if (tx.IsCoinBase())
|
|
|
|
return 0;
|
|
|
|
|
2014-04-22 15:46:19 -07:00
|
|
|
CAmount nResult = 0;
|
2013-11-04 17:47:07 -08:00
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++)
|
|
|
|
nResult += GetOutputFor(tx.vin[i]).nValue;
|
|
|
|
|
2018-05-08 05:51:54 -07:00
|
|
|
nResult += tx.GetShieldedValueIn();
|
2016-01-04 05:02:01 -08:00
|
|
|
|
2013-11-04 17:47:07 -08:00
|
|
|
return nResult;
|
|
|
|
}
|
|
|
|
|
2018-10-26 06:44:18 -07:00
|
|
|
bool CCoinsViewCache::HaveShieldedRequirements(const CTransaction& tx) const
|
2016-01-07 11:09:58 -08:00
|
|
|
{
|
2018-08-01 09:31:09 -07:00
|
|
|
boost::unordered_map<uint256, SproutMerkleTree, CCoinsKeyHasher> intermediates;
|
2016-05-26 13:00:46 -07:00
|
|
|
|
2019-06-16 04:39:05 -07:00
|
|
|
BOOST_FOREACH(const JSDescription &joinsplit, tx.vJoinSplit)
|
2016-01-07 11:09:58 -08:00
|
|
|
{
|
2016-07-14 16:02:39 -07:00
|
|
|
BOOST_FOREACH(const uint256& nullifier, joinsplit.nullifiers)
|
2016-01-08 07:37:17 -08:00
|
|
|
{
|
2018-04-25 16:55:33 -07:00
|
|
|
if (GetNullifier(nullifier, SPROUT)) {
|
2016-07-14 16:02:39 -07:00
|
|
|
// If the nullifier is set, this transaction
|
2016-01-08 07:37:17 -08:00
|
|
|
// double-spends!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 09:31:09 -07:00
|
|
|
SproutMerkleTree tree;
|
2016-07-14 15:10:41 -07:00
|
|
|
auto it = intermediates.find(joinsplit.anchor);
|
2016-05-26 13:00:46 -07:00
|
|
|
if (it != intermediates.end()) {
|
|
|
|
tree = it->second;
|
2018-04-27 14:00:00 -07:00
|
|
|
} else if (!GetSproutAnchorAt(joinsplit.anchor, tree)) {
|
2016-01-07 11:09:58 -08:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-26 13:00:46 -07:00
|
|
|
|
2016-07-14 15:10:41 -07:00
|
|
|
BOOST_FOREACH(const uint256& commitment, joinsplit.commitments)
|
2016-05-26 13:00:46 -07:00
|
|
|
{
|
|
|
|
tree.append(commitment);
|
|
|
|
}
|
|
|
|
|
|
|
|
intermediates.insert(std::make_pair(tree.root(), tree));
|
2016-01-07 11:09:58 -08:00
|
|
|
}
|
2018-04-24 16:16:43 -07:00
|
|
|
|
|
|
|
for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
|
2018-04-25 16:55:33 -07:00
|
|
|
if (GetNullifier(spendDescription.nullifier, SAPLING)) // Prevent double spends
|
2018-04-24 16:16:43 -07:00
|
|
|
return false;
|
2016-01-07 11:09:58 -08:00
|
|
|
|
2018-08-01 09:31:09 -07:00
|
|
|
SaplingMerkleTree tree;
|
2018-05-06 22:27:52 -07:00
|
|
|
if (!GetSaplingAnchorAt(spendDescription.anchor, tree)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-06 17:45:05 -07:00
|
|
|
|
2016-01-07 11:09:58 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-19 07:42:48 -07:00
|
|
|
bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
|
2013-11-04 17:47:07 -08:00
|
|
|
{
|
|
|
|
if (!tx.IsCoinBase()) {
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++) {
|
|
|
|
const COutPoint &prevout = tx.vin[i].prevout;
|
2014-09-02 12:21:15 -07:00
|
|
|
const CCoins* coins = AccessCoins(prevout.hash);
|
|
|
|
if (!coins || !coins->IsAvailable(prevout.n)) {
|
2013-11-04 17:47:07 -08:00
|
|
|
return false;
|
2014-09-02 12:21:15 -07:00
|
|
|
}
|
2013-11-04 17:47:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-10 23:35:14 -08:00
|
|
|
|
2014-07-19 07:42:48 -07:00
|
|
|
double CCoinsViewCache::GetPriority(const CTransaction &tx, int nHeight) const
|
2013-11-10 23:35:14 -08:00
|
|
|
{
|
|
|
|
if (tx.IsCoinBase())
|
|
|
|
return 0.0;
|
2017-04-13 14:40:20 -07:00
|
|
|
|
2018-09-28 09:32:30 -07:00
|
|
|
// Shielded transfers do not reveal any information about the value or age of a note, so we
|
2017-04-13 14:40:20 -07:00
|
|
|
// cannot apply the priority algorithm used for transparent utxos. Instead, we just
|
2018-09-28 09:32:30 -07:00
|
|
|
// use the maximum priority for all (partially or fully) shielded transactions.
|
|
|
|
// (Note that coinbase transactions cannot contain JoinSplits, or Sapling shielded Spends or Outputs.)
|
2017-04-13 14:40:20 -07:00
|
|
|
|
2019-06-16 04:39:05 -07:00
|
|
|
if (tx.vJoinSplit.size() > 0 || tx.vShieldedSpend.size() > 0 || tx.vShieldedOutput.size() > 0) {
|
2017-04-13 14:40:20 -07:00
|
|
|
return MAX_PRIORITY;
|
|
|
|
}
|
|
|
|
|
2018-09-28 11:16:05 -07:00
|
|
|
// FIXME: this logic is partially duplicated between here and CreateNewBlock in miner.cpp.
|
2013-11-10 23:35:14 -08:00
|
|
|
double dResult = 0.0;
|
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin)
|
|
|
|
{
|
2014-09-02 12:21:15 -07: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-10 23:35:14 -08:00
|
|
|
}
|
|
|
|
}
|
2016-11-23 11:39:01 -08:00
|
|
|
|
2013-11-10 23:35:14 -08:00
|
|
|
return tx.ComputePriority(dResult);
|
|
|
|
}
|
2014-09-03 00:01:24 -07:00
|
|
|
|
2015-05-03 16:31:11 -07:00
|
|
|
CCoinsModifier::CCoinsModifier(CCoinsViewCache& cache_, CCoinsMap::iterator it_, size_t usage) : cache(cache_), it(it_), cachedCoinUsage(usage) {
|
2015-01-04 08:15:02 -08:00
|
|
|
assert(!cache.hasModifier);
|
|
|
|
cache.hasModifier = true;
|
|
|
|
}
|
2014-09-03 00:01:24 -07:00
|
|
|
|
2014-09-03 00:37:47 -07:00
|
|
|
CCoinsModifier::~CCoinsModifier()
|
|
|
|
{
|
2014-09-03 00:01:24 -07:00
|
|
|
assert(cache.hasModifier);
|
|
|
|
cache.hasModifier = false;
|
2014-09-03 00:37:47 -07:00
|
|
|
it->second.coins.Cleanup();
|
2015-05-03 16:31:11 -07:00
|
|
|
cache.cachedCoinsUsage -= cachedCoinUsage; // Subtract the old usage
|
2014-09-03 00:37:47 -07:00
|
|
|
if ((it->second.flags & CCoinsCacheEntry::FRESH) && it->second.coins.IsPruned()) {
|
|
|
|
cache.cacheCoins.erase(it);
|
2015-05-03 16:31:11 -07:00
|
|
|
} else {
|
|
|
|
// If the coin still exists after the modification, add the new usage
|
2015-07-17 10:46:18 -07:00
|
|
|
cache.cachedCoinsUsage += it->second.coins.DynamicMemoryUsage();
|
2014-09-03 00:37:47 -07:00
|
|
|
}
|
2014-09-03 00:01:24 -07:00
|
|
|
}
|