Skip to content

Commit

Permalink
change hardcoded character constants to a set of descriptive named co…
Browse files Browse the repository at this point in the history
…nstants for database keys

backports bitcoin/bitcoin@14d023f
  • Loading branch information
random-zebra committed May 20, 2020
1 parent d45c58e commit 2251db3
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@

#include <boost/thread.hpp>

static const char DB_COINS = 'c';
static const char DB_BLOCK_FILES = 'f';
static const char DB_TXINDEX = 't';
static const char DB_BLOCK_INDEX = 'b';

static const char DB_BEST_BLOCK = 'B';
static const char DB_FLAG = 'F';
static const char DB_REINDEX_FLAG = 'R';
static const char DB_LAST_BLOCK = 'l';
static const char DB_MONEY_SUPPLY = 'M';

void static BatchWriteCoins(CLevelDBBatch& batch, const uint256& hash, const CCoins& coins)
{
if (coins.IsPruned())
batch.Erase(std::make_pair('c', hash));
batch.Erase(std::make_pair(DB_COINS, hash));
else
batch.Write(std::make_pair('c', hash), coins);
batch.Write(std::make_pair(DB_COINS, hash), coins);
}

void static BatchWriteHashBestChain(CLevelDBBatch& batch, const uint256& hash)
{
batch.Write('B', hash);
batch.Write(DB_BEST_BLOCK, hash);
}

CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe)
Expand All @@ -34,18 +44,18 @@ CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(Get

bool CCoinsViewDB::GetCoins(const uint256& txid, CCoins& coins) const
{
return db.Read(std::make_pair('c', txid), coins);
return db.Read(std::make_pair(DB_COINS, txid), coins);
}

bool CCoinsViewDB::HaveCoins(const uint256& txid) const
{
return db.Exists(std::make_pair('c', txid));
return db.Exists(std::make_pair(DB_COINS, txid));
}

uint256 CCoinsViewDB::GetBestBlock() const
{
uint256 hashBestChain;
if (!db.Read('B', hashBestChain))
if (!db.Read(DB_BEST_BLOCK, hashBestChain))
return UINT256_ZERO;
return hashBestChain;
}
Expand Down Expand Up @@ -77,31 +87,31 @@ CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CLevel

bool CBlockTreeDB::WriteBlockIndex(const CDiskBlockIndex& blockindex)
{
return Write(std::make_pair('b', blockindex.GetBlockHash()), blockindex);
return Write(std::make_pair(DB_BLOCK_INDEX, blockindex.GetBlockHash()), blockindex);
}

bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo& info)
{
return Read(std::make_pair('f', nFile), info);
return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
}

bool CBlockTreeDB::WriteReindexing(bool fReindexing)
{
if (fReindexing)
return Write('R', '1');
return Write(DB_REINDEX_FLAG, '1');
else
return Erase('R');
return Erase(DB_REINDEX_FLAG);
}

bool CBlockTreeDB::ReadReindexing(bool& fReindexing)
{
fReindexing = Exists('R');
fReindexing = Exists(DB_REINDEX_FLAG);
return true;
}

bool CBlockTreeDB::ReadLastBlockFile(int& nFile)
{
return Read('l', nFile);
return Read(DB_LAST_BLOCK, nFile);
}

bool CCoinsViewDB::GetStats(CCoinsStats& stats) const
Expand All @@ -123,7 +133,7 @@ bool CCoinsViewDB::GetStats(CCoinsStats& stats) const
CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
char chType;
ssKey >> chType;
if (chType == 'c') {
if (chType == DB_COINS) {
leveldb::Slice slValue = pcursor->value();
CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
CCoins coins;
Expand All @@ -132,7 +142,7 @@ bool CCoinsViewDB::GetStats(CCoinsStats& stats) const
ssKey >> txhash;
ss << txhash;
ss << VARINT(coins.nVersion);
ss << (coins.fCoinBase ? 'c' : 'n');
ss << (coins.fCoinBase ? DB_COINS : 'n');
ss << VARINT(coins.nHeight);
stats.nTransactions++;
for (unsigned int i = 0; i < coins.vout.size(); i++) {
Expand All @@ -158,52 +168,50 @@ bool CCoinsViewDB::GetStats(CCoinsStats& stats) const
return true;
}

static const char MONEYSUPPLY = 'M';

bool CBlockTreeDB::WriteMoneySupply(const int64_t& nSupply)
{
return Write(MONEYSUPPLY, nSupply);
return Write(DB_MONEY_SUPPLY, nSupply);
}

bool CBlockTreeDB::ReadMoneySupply(int64_t& nSupply) const
{
return Read(MONEYSUPPLY, nSupply);
return Read(DB_MONEY_SUPPLY, nSupply);
}

bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
CLevelDBBatch batch;
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
batch.Write(std::make_pair('f', it->first), *it->second);
batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
}
batch.Write('l', nLastFile);
batch.Write(DB_LAST_BLOCK, nLastFile);
for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
batch.Write(std::make_pair('b', (*it)->GetBlockHash()), CDiskBlockIndex(*it));
batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
}
return WriteBatch(batch, true);
}

bool CBlockTreeDB::ReadTxIndex(const uint256& txid, CDiskTxPos& pos)
{
return Read(std::make_pair('t', txid), pos);
return Read(std::make_pair(DB_TXINDEX, txid), pos);
}

bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >& vect)
{
CLevelDBBatch batch;
for (std::vector<std::pair<uint256, CDiskTxPos> >::const_iterator it = vect.begin(); it != vect.end(); it++)
batch.Write(std::make_pair('t', it->first), it->second);
batch.Write(std::make_pair(DB_TXINDEX, it->first), it->second);
return WriteBatch(batch);
}

bool CBlockTreeDB::WriteFlag(const std::string& name, bool fValue)
{
return Write(std::make_pair('F', name), fValue ? '1' : '0');
return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
}

bool CBlockTreeDB::ReadFlag(const std::string& name, bool& fValue)
{
char ch;
if (!Read(std::make_pair('F', name), ch))
if (!Read(std::make_pair(DB_FLAG, name), ch))
return false;
fValue = ch == '1';
return true;
Expand All @@ -224,7 +232,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator());

CDataStream ssKeySet(SER_DISK, CLIENT_VERSION);
ssKeySet << std::make_pair('b', UINT256_ZERO);
ssKeySet << std::make_pair(DB_BLOCK_INDEX, UINT256_ZERO);
pcursor->Seek(ssKeySet.str());

// Load mapBlockIndex
Expand All @@ -235,7 +243,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
char chType;
ssKey >> chType;
if (chType == 'b') {
if (chType == DB_BLOCK_INDEX) {
leveldb::Slice slValue = pcursor->value();
CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
CDiskBlockIndex diskindex;
Expand Down Expand Up @@ -282,7 +290,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts()

bool CBlockTreeDB::ReadLegacyBlockIndex(const uint256& blockHash, CLegacyBlockIndex& biRet)
{
return Read(std::make_pair('b', blockHash), biRet);
return Read(std::make_pair(DB_BLOCK_INDEX, blockHash), biRet);
}

CZerocoinDB::CZerocoinDB(size_t nCacheSize, bool fMemory, bool fWipe) : CLevelDBWrapper(GetDataDir() / "zerocoin", nCacheSize, fMemory, fWipe)
Expand Down

0 comments on commit 2251db3

Please sign in to comment.