Rename CSerialsCacheEntry.
This commit is contained in:
@@ -41,7 +41,7 @@ bool CCoins::Spend(uint32_t nPos)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool CCoinsView::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const { return false; }
|
bool CCoinsView::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const { return false; }
|
||||||
bool CCoinsView::GetNullifier(const uint256 &serial) const { return false; }
|
bool CCoinsView::GetNullifier(const uint256 &nullifier) const { return false; }
|
||||||
bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) const { return false; }
|
bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) const { return false; }
|
||||||
bool CCoinsView::HaveCoins(const uint256 &txid) const { return false; }
|
bool CCoinsView::HaveCoins(const uint256 &txid) const { return false; }
|
||||||
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
|
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
|
||||||
@@ -57,7 +57,7 @@ bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; }
|
|||||||
CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
|
CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
|
||||||
|
|
||||||
bool CCoinsViewBacked::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const { return base->GetAnchorAt(rt, tree); }
|
bool CCoinsViewBacked::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const { return base->GetAnchorAt(rt, tree); }
|
||||||
bool CCoinsViewBacked::GetNullifier(const uint256 &serial) const { return base->GetNullifier(serial); }
|
bool CCoinsViewBacked::GetNullifier(const uint256 &nullifier) const { return base->GetNullifier(nullifier); }
|
||||||
bool CCoinsViewBacked::GetCoins(const uint256 &txid, CCoins &coins) const { return base->GetCoins(txid, coins); }
|
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); }
|
bool CCoinsViewBacked::HaveCoins(const uint256 &txid) const { return base->HaveCoins(txid); }
|
||||||
uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
|
uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
|
||||||
@@ -128,16 +128,16 @@ bool CCoinsViewCache::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tr
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCoinsViewCache::GetNullifier(const uint256 &serial) const {
|
bool CCoinsViewCache::GetNullifier(const uint256 &nullifier) const {
|
||||||
CNullifiersMap::iterator it = cacheNullifiers.find(serial);
|
CNullifiersMap::iterator it = cacheNullifiers.find(nullifier);
|
||||||
if (it != cacheNullifiers.end())
|
if (it != cacheNullifiers.end())
|
||||||
return it->second.entered;
|
return it->second.entered;
|
||||||
|
|
||||||
CSerialsCacheEntry entry;
|
CNullifiersCacheEntry entry;
|
||||||
bool tmp = base->GetNullifier(serial);
|
bool tmp = base->GetNullifier(nullifier);
|
||||||
entry.entered = tmp;
|
entry.entered = tmp;
|
||||||
|
|
||||||
cacheNullifiers.insert(std::make_pair(serial, entry));
|
cacheNullifiers.insert(std::make_pair(nullifier, entry));
|
||||||
|
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
@@ -185,10 +185,10 @@ void CCoinsViewCache::PopAnchor(const uint256 &newrt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CCoinsViewCache::SetNullifier(const uint256 &serial, bool spent) {
|
void CCoinsViewCache::SetNullifier(const uint256 &nullifier, bool spent) {
|
||||||
std::pair<CNullifiersMap::iterator, bool> ret = cacheNullifiers.insert(std::make_pair(serial, CSerialsCacheEntry()));
|
std::pair<CNullifiersMap::iterator, bool> ret = cacheNullifiers.insert(std::make_pair(nullifier, CNullifiersCacheEntry()));
|
||||||
ret.first->second.entered = spent;
|
ret.first->second.entered = spent;
|
||||||
ret.first->second.flags |= CSerialsCacheEntry::DIRTY;
|
ret.first->second.flags |= CNullifiersCacheEntry::DIRTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) const {
|
bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) const {
|
||||||
@@ -328,22 +328,22 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins,
|
|||||||
|
|
||||||
for (CNullifiersMap::iterator child_it = mapNullifiers.begin(); child_it != mapNullifiers.end();)
|
for (CNullifiersMap::iterator child_it = mapNullifiers.begin(); child_it != mapNullifiers.end();)
|
||||||
{
|
{
|
||||||
if (child_it->second.flags & CSerialsCacheEntry::DIRTY) { // Ignore non-dirty entries (optimization).
|
if (child_it->second.flags & CNullifiersCacheEntry::DIRTY) { // Ignore non-dirty entries (optimization).
|
||||||
CNullifiersMap::iterator parent_it = cacheNullifiers.find(child_it->first);
|
CNullifiersMap::iterator parent_it = cacheNullifiers.find(child_it->first);
|
||||||
|
|
||||||
if (parent_it == cacheNullifiers.end()) {
|
if (parent_it == cacheNullifiers.end()) {
|
||||||
if (child_it->second.entered) {
|
if (child_it->second.entered) {
|
||||||
// Parent doesn't have an entry, but child has a SPENT serial.
|
// Parent doesn't have an entry, but child has a SPENT nullifier.
|
||||||
// Move the spent serial up.
|
// Move the spent nullifier up.
|
||||||
|
|
||||||
CSerialsCacheEntry& entry = cacheNullifiers[child_it->first];
|
CNullifiersCacheEntry& entry = cacheNullifiers[child_it->first];
|
||||||
entry.entered = true;
|
entry.entered = true;
|
||||||
entry.flags = CSerialsCacheEntry::DIRTY;
|
entry.flags = CNullifiersCacheEntry::DIRTY;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (parent_it->second.entered != child_it->second.entered) {
|
if (parent_it->second.entered != child_it->second.entered) {
|
||||||
parent_it->second.entered = child_it->second.entered;
|
parent_it->second.entered = child_it->second.entered;
|
||||||
parent_it->second.flags |= CSerialsCacheEntry::DIRTY;
|
parent_it->second.flags |= CNullifiersCacheEntry::DIRTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -396,10 +396,10 @@ bool CCoinsViewCache::HaveJoinSplitRequirements(const CTransaction& tx) const
|
|||||||
|
|
||||||
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit)
|
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit)
|
||||||
{
|
{
|
||||||
BOOST_FOREACH(const uint256& serial, joinsplit.nullifiers)
|
BOOST_FOREACH(const uint256& nullifier, joinsplit.nullifiers)
|
||||||
{
|
{
|
||||||
if (GetNullifier(serial)) {
|
if (GetNullifier(nullifier)) {
|
||||||
// If the serial is set, this transaction
|
// If the nullifier is set, this transaction
|
||||||
// double-spends!
|
// double-spends!
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/coins.h
22
src/coins.h
@@ -309,21 +309,21 @@ struct CAnchorsCacheEntry
|
|||||||
CAnchorsCacheEntry() : entered(false), flags(0) {}
|
CAnchorsCacheEntry() : entered(false), flags(0) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CSerialsCacheEntry
|
struct CNullifiersCacheEntry
|
||||||
{
|
{
|
||||||
bool entered; // If the serial is spent or not
|
bool entered; // If the nullifier is spent or not
|
||||||
unsigned char flags;
|
unsigned char flags;
|
||||||
|
|
||||||
enum Flags {
|
enum Flags {
|
||||||
DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
|
DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
|
||||||
};
|
};
|
||||||
|
|
||||||
CSerialsCacheEntry() : entered(false), flags(0) {}
|
CNullifiersCacheEntry() : entered(false), flags(0) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::unordered_map<uint256, CCoinsCacheEntry, CCoinsKeyHasher> CCoinsMap;
|
typedef boost::unordered_map<uint256, CCoinsCacheEntry, CCoinsKeyHasher> CCoinsMap;
|
||||||
typedef boost::unordered_map<uint256, CAnchorsCacheEntry, CCoinsKeyHasher> CAnchorsMap;
|
typedef boost::unordered_map<uint256, CAnchorsCacheEntry, CCoinsKeyHasher> CAnchorsMap;
|
||||||
typedef boost::unordered_map<uint256, CSerialsCacheEntry, CCoinsKeyHasher> CNullifiersMap;
|
typedef boost::unordered_map<uint256, CNullifiersCacheEntry, CCoinsKeyHasher> CNullifiersMap;
|
||||||
|
|
||||||
struct CCoinsStats
|
struct CCoinsStats
|
||||||
{
|
{
|
||||||
@@ -346,8 +346,8 @@ public:
|
|||||||
//! Retrieve the tree at a particular anchored root in the chain
|
//! Retrieve the tree at a particular anchored root in the chain
|
||||||
virtual bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
|
virtual bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
|
||||||
|
|
||||||
//! Determine whether a serial is spent or not
|
//! Determine whether a nullifier is spent or not
|
||||||
virtual bool GetNullifier(const uint256 &serial) const;
|
virtual bool GetNullifier(const uint256 &nullifier) const;
|
||||||
|
|
||||||
//! Retrieve the CCoins (unspent transaction outputs) for a given txid
|
//! Retrieve the CCoins (unspent transaction outputs) for a given txid
|
||||||
virtual bool GetCoins(const uint256 &txid, CCoins &coins) const;
|
virtual bool GetCoins(const uint256 &txid, CCoins &coins) const;
|
||||||
@@ -387,7 +387,7 @@ protected:
|
|||||||
public:
|
public:
|
||||||
CCoinsViewBacked(CCoinsView *viewIn);
|
CCoinsViewBacked(CCoinsView *viewIn);
|
||||||
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
|
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
|
||||||
bool GetNullifier(const uint256 &serial) const;
|
bool GetNullifier(const uint256 &nullifier) const;
|
||||||
bool GetCoins(const uint256 &txid, CCoins &coins) const;
|
bool GetCoins(const uint256 &txid, CCoins &coins) const;
|
||||||
bool HaveCoins(const uint256 &txid) const;
|
bool HaveCoins(const uint256 &txid) const;
|
||||||
uint256 GetBestBlock() const;
|
uint256 GetBestBlock() const;
|
||||||
@@ -451,7 +451,7 @@ public:
|
|||||||
|
|
||||||
// Standard CCoinsView methods
|
// Standard CCoinsView methods
|
||||||
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
|
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
|
||||||
bool GetNullifier(const uint256 &serial) const;
|
bool GetNullifier(const uint256 &nullifier) const;
|
||||||
bool GetCoins(const uint256 &txid, CCoins &coins) const;
|
bool GetCoins(const uint256 &txid, CCoins &coins) const;
|
||||||
bool HaveCoins(const uint256 &txid) const;
|
bool HaveCoins(const uint256 &txid) const;
|
||||||
uint256 GetBestBlock() const;
|
uint256 GetBestBlock() const;
|
||||||
@@ -472,8 +472,8 @@ public:
|
|||||||
// the new current root.
|
// the new current root.
|
||||||
void PopAnchor(const uint256 &rt);
|
void PopAnchor(const uint256 &rt);
|
||||||
|
|
||||||
// Marks a serial as spent or not.
|
// Marks a nullifier as spent or not.
|
||||||
void SetNullifier(const uint256 &serial, bool spent);
|
void SetNullifier(const uint256 &nullifier, bool spent);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a pointer to CCoins in the cache, or NULL if not found. This is
|
* Return a pointer to CCoins in the cache, or NULL if not found. This is
|
||||||
@@ -515,7 +515,7 @@ public:
|
|||||||
//! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
|
//! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
|
||||||
bool HaveInputs(const CTransaction& tx) const;
|
bool HaveInputs(const CTransaction& tx) const;
|
||||||
|
|
||||||
//! Check whether all joinsplit requirements (anchors/serials) are satisfied
|
//! Check whether all joinsplit requirements (anchors/nullifiers) are satisfied
|
||||||
bool HaveJoinSplitRequirements(const CTransaction& tx) const;
|
bool HaveJoinSplitRequirements(const CTransaction& tx) const;
|
||||||
|
|
||||||
//! Return priority of tx at height nHeight
|
//! Return priority of tx at height nHeight
|
||||||
|
|||||||
@@ -163,28 +163,28 @@ uint256 appendRandomCommitment(ZCIncrementalMerkleTree &tree)
|
|||||||
|
|
||||||
BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
|
BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(serials_test)
|
BOOST_AUTO_TEST_CASE(nullifiers_test)
|
||||||
{
|
{
|
||||||
CCoinsViewTest base;
|
CCoinsViewTest base;
|
||||||
CCoinsViewCacheTest cache(&base);
|
CCoinsViewCacheTest cache(&base);
|
||||||
|
|
||||||
uint256 myserial = GetRandHash();
|
uint256 nf = GetRandHash();
|
||||||
|
|
||||||
BOOST_CHECK(!cache.GetNullifier(myserial));
|
BOOST_CHECK(!cache.GetNullifier(nf));
|
||||||
cache.SetNullifier(myserial, true);
|
cache.SetNullifier(nf, true);
|
||||||
BOOST_CHECK(cache.GetNullifier(myserial));
|
BOOST_CHECK(cache.GetNullifier(nf));
|
||||||
cache.Flush();
|
cache.Flush();
|
||||||
|
|
||||||
CCoinsViewCacheTest cache2(&base);
|
CCoinsViewCacheTest cache2(&base);
|
||||||
|
|
||||||
BOOST_CHECK(cache2.GetNullifier(myserial));
|
BOOST_CHECK(cache2.GetNullifier(nf));
|
||||||
cache2.SetNullifier(myserial, false);
|
cache2.SetNullifier(nf, false);
|
||||||
BOOST_CHECK(!cache2.GetNullifier(myserial));
|
BOOST_CHECK(!cache2.GetNullifier(nf));
|
||||||
cache2.Flush();
|
cache2.Flush();
|
||||||
|
|
||||||
CCoinsViewCacheTest cache3(&base);
|
CCoinsViewCacheTest cache3(&base);
|
||||||
|
|
||||||
BOOST_CHECK(!cache3.GetNullifier(myserial));
|
BOOST_CHECK(!cache3.GetNullifier(nf));
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(anchors_flush_test)
|
BOOST_AUTO_TEST_CASE(anchors_flush_test)
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (CNullifiersMap::iterator it = mapNullifiers.begin(); it != mapNullifiers.end();) {
|
for (CNullifiersMap::iterator it = mapNullifiers.begin(); it != mapNullifiers.end();) {
|
||||||
if (it->second.flags & CSerialsCacheEntry::DIRTY) {
|
if (it->second.flags & CNullifiersCacheEntry::DIRTY) {
|
||||||
BatchWriteSerial(batch, it->first, it->second.entered);
|
BatchWriteSerial(batch, it->first, it->second.entered);
|
||||||
// TODO: changed++?
|
// TODO: changed++?
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user