Add mapSerials infrastructure to CCoinsView.
This adds the TXDB/CCoinsViewCache primitives necessary for writing consensus rules for mapSerials later.
This commit is contained in:
30
src/txdb.cpp
30
src/txdb.cpp
@@ -18,6 +18,7 @@
|
||||
using namespace std;
|
||||
|
||||
static const char DB_ANCHOR = 'A';
|
||||
static const char DB_SERIAL = 's';
|
||||
static const char DB_COINS = 'c';
|
||||
static const char DB_BLOCK_FILES = 'f';
|
||||
static const char DB_TXINDEX = 't';
|
||||
@@ -42,6 +43,13 @@ void static BatchWriteAnchor(CLevelDBBatch &batch,
|
||||
}
|
||||
}
|
||||
|
||||
void static BatchWriteSerial(CLevelDBBatch &batch, const uint256 &serial, const bool &entered) {
|
||||
if (!entered)
|
||||
batch.Erase(make_pair(DB_SERIAL, serial));
|
||||
else
|
||||
batch.Write(make_pair(DB_SERIAL, serial), true);
|
||||
}
|
||||
|
||||
void static BatchWriteCoins(CLevelDBBatch &batch, const uint256 &hash, const CCoins &coins) {
|
||||
if (coins.IsPruned())
|
||||
batch.Erase(make_pair(DB_COINS, hash));
|
||||
@@ -81,6 +89,16 @@ bool CCoinsViewDB::GetAnchorAt(const uint256 &rt, libzerocash::IncrementalMerkle
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CCoinsViewDB::GetSerial(const uint256 &serial) const {
|
||||
bool spent = false;
|
||||
bool read = db.Read(make_pair(DB_SERIAL, serial), spent);
|
||||
|
||||
// We should never read false from the database.
|
||||
assert(spent != read);
|
||||
|
||||
return spent;
|
||||
}
|
||||
|
||||
bool CCoinsViewDB::GetCoins(const uint256 &txid, CCoins &coins) const {
|
||||
return db.Read(make_pair(DB_COINS, txid), coins);
|
||||
}
|
||||
@@ -106,7 +124,8 @@ uint256 CCoinsViewDB::GetBestAnchor() const {
|
||||
bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
|
||||
const uint256 &hashBlock,
|
||||
const uint256 &hashAnchor,
|
||||
CAnchorsMap &mapAnchors) {
|
||||
CAnchorsMap &mapAnchors,
|
||||
CSerialsMap &mapSerials) {
|
||||
CLevelDBBatch batch;
|
||||
size_t count = 0;
|
||||
size_t changed = 0;
|
||||
@@ -129,6 +148,15 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
|
||||
mapAnchors.erase(itOld);
|
||||
}
|
||||
|
||||
for (CSerialsMap::iterator it = mapSerials.begin(); it != mapSerials.end();) {
|
||||
if (it->second.flags & CSerialsCacheEntry::DIRTY) {
|
||||
BatchWriteSerial(batch, it->first, it->second.entered);
|
||||
// TODO: changed++?
|
||||
}
|
||||
CSerialsMap::iterator itOld = it++;
|
||||
mapSerials.erase(itOld);
|
||||
}
|
||||
|
||||
if (!hashBlock.IsNull())
|
||||
BatchWriteHashBestChain(batch, hashBlock);
|
||||
if (!hashAnchor.IsNull())
|
||||
|
||||
Reference in New Issue
Block a user