|
|
@ -22,6 +22,11 @@ static const char DB_NULLIFIER = 's'; |
|
|
|
static const char DB_COINS = 'c'; |
|
|
|
static const char DB_BLOCK_FILES = 'f'; |
|
|
|
static const char DB_TXINDEX = 't'; |
|
|
|
static const char DB_ADDRESSINDEX = 'a'; |
|
|
|
static const char DB_ADDRESSUNSPENTINDEX = 'u'; |
|
|
|
static const char DB_TIMESTAMPINDEX = 's'; |
|
|
|
static const char DB_BLOCKHASHINDEX = 'z'; |
|
|
|
static const char DB_SPENTINDEX = 'p'; |
|
|
|
static const char DB_BLOCK_INDEX = 'b'; |
|
|
|
|
|
|
|
static const char DB_BEST_BLOCK = 'B'; |
|
|
@ -68,7 +73,7 @@ void static BatchWriteHashBestAnchor(CLevelDBBatch &batch, const uint256 &hash) |
|
|
|
CCoinsViewDB::CCoinsViewDB(std::string dbName, size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / dbName, nCacheSize, fMemory, fWipe) { |
|
|
|
} |
|
|
|
|
|
|
|
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe) { |
|
|
|
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe, false, 64) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -158,7 +163,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, |
|
|
|
return db.WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CLevelDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) { |
|
|
|
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe, bool compression, int maxOpenFiles) : CLevelDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe, compression, maxOpenFiles) { |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) { |
|
|
@ -260,6 +265,197 @@ bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> |
|
|
|
return WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value) { |
|
|
|
return Read(make_pair(DB_SPENTINDEX, key), value); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect) { |
|
|
|
CLevelDBBatch batch; |
|
|
|
for (std::vector<std::pair<CSpentIndexKey,CSpentIndexValue> >::const_iterator it=vect.begin(); it!=vect.end(); it++) { |
|
|
|
if (it->second.IsNull()) { |
|
|
|
batch.Erase(make_pair(DB_SPENTINDEX, it->first)); |
|
|
|
} else { |
|
|
|
batch.Write(make_pair(DB_SPENTINDEX, it->first), it->second); |
|
|
|
} |
|
|
|
} |
|
|
|
return WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect) { |
|
|
|
CLevelDBBatch batch; |
|
|
|
for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=vect.begin(); it!=vect.end(); it++) { |
|
|
|
if (it->second.IsNull()) { |
|
|
|
batch.Erase(make_pair(DB_ADDRESSUNSPENTINDEX, it->first)); |
|
|
|
} else { |
|
|
|
batch.Write(make_pair(DB_ADDRESSUNSPENTINDEX, it->first), it->second); |
|
|
|
} |
|
|
|
} |
|
|
|
return WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::ReadAddressUnspentIndex(uint160 addressHash, int type, |
|
|
|
std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs) { |
|
|
|
|
|
|
|
boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator()); |
|
|
|
|
|
|
|
CDataStream ssKeySet(SER_DISK, CLIENT_VERSION); |
|
|
|
ssKeySet << make_pair(DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey(type, addressHash)); |
|
|
|
pcursor->Seek(ssKeySet.str()); |
|
|
|
|
|
|
|
while (pcursor->Valid()) { |
|
|
|
boost::this_thread::interruption_point(); |
|
|
|
try { |
|
|
|
leveldb::Slice slKey = pcursor->key(); |
|
|
|
CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); |
|
|
|
char chType; |
|
|
|
ssKey >> chType; |
|
|
|
CAddressUnspentKey auk; |
|
|
|
ssKey >> auk; |
|
|
|
if (chType == DB_ADDRESSUNSPENTINDEX && auk.hashBytes == addressHash) { |
|
|
|
try { |
|
|
|
leveldb::Slice slValue = pcursor->value(); |
|
|
|
CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); |
|
|
|
CAddressUnspentValue nValue; |
|
|
|
ssValue >> nValue; |
|
|
|
unspentOutputs.push_back(make_pair(auk, nValue)); |
|
|
|
pcursor->Next(); |
|
|
|
} catch (const std::exception& e) { |
|
|
|
return error("failed to get address unspent value"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
break; |
|
|
|
} |
|
|
|
} catch (const std::exception& e) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) { |
|
|
|
CLevelDBBatch batch; |
|
|
|
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=vect.begin(); it!=vect.end(); it++) |
|
|
|
batch.Write(make_pair(DB_ADDRESSINDEX, it->first), it->second); |
|
|
|
return WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount > >&vect) { |
|
|
|
CLevelDBBatch batch; |
|
|
|
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::const_iterator it=vect.begin(); it!=vect.end(); it++) |
|
|
|
batch.Erase(make_pair(DB_ADDRESSINDEX, it->first)); |
|
|
|
return WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::ReadAddressIndex(uint160 addressHash, int type, |
|
|
|
std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex, |
|
|
|
int start, int end) { |
|
|
|
|
|
|
|
boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator()); |
|
|
|
|
|
|
|
CDataStream ssKeySet(SER_DISK, CLIENT_VERSION); |
|
|
|
if (start > 0 && end > 0) { |
|
|
|
ssKeySet << make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorHeightKey(type, addressHash, start)); |
|
|
|
} else { |
|
|
|
ssKeySet << make_pair(DB_ADDRESSINDEX, CAddressIndexIteratorKey(type, addressHash)); |
|
|
|
} |
|
|
|
pcursor->Seek(ssKeySet.str()); |
|
|
|
|
|
|
|
while (pcursor->Valid()) { |
|
|
|
boost::this_thread::interruption_point(); |
|
|
|
try { |
|
|
|
leveldb::Slice slKey = pcursor->key(); |
|
|
|
CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); |
|
|
|
char chType; |
|
|
|
ssKey >> chType; |
|
|
|
CAddressIndexKey aik; |
|
|
|
ssKey >> aik; |
|
|
|
if (chType == DB_ADDRESSINDEX && aik.hashBytes == addressHash) { |
|
|
|
if (end > 0 && aik.blockHeight > end) { |
|
|
|
break; |
|
|
|
} |
|
|
|
try { |
|
|
|
leveldb::Slice slValue = pcursor->value(); |
|
|
|
CDataStream ssValue(slValue.data(), slValue.data()+slValue.size(), SER_DISK, CLIENT_VERSION); |
|
|
|
CAmount nValue; |
|
|
|
ssValue >> nValue; |
|
|
|
addressIndex.push_back(make_pair(aik, nValue)); |
|
|
|
pcursor->Next(); |
|
|
|
} catch (const std::exception& e) { |
|
|
|
return error("failed to get address index value"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
break; |
|
|
|
} |
|
|
|
} catch (const std::exception& e) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::WriteTimestampIndex(const CTimestampIndexKey ×tampIndex) { |
|
|
|
CLevelDBBatch batch; |
|
|
|
batch.Write(make_pair(DB_TIMESTAMPINDEX, timestampIndex), 0); |
|
|
|
return WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::ReadTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector<std::pair<uint256, unsigned int> > &hashes) { |
|
|
|
|
|
|
|
boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator()); |
|
|
|
|
|
|
|
CDataStream ssKeySet(SER_DISK, CLIENT_VERSION); |
|
|
|
ssKeySet << make_pair(DB_TIMESTAMPINDEX, CTimestampIndexIteratorKey(low)); |
|
|
|
pcursor->Seek(ssKeySet.str()); |
|
|
|
|
|
|
|
while (pcursor->Valid()) { |
|
|
|
boost::this_thread::interruption_point(); |
|
|
|
try { |
|
|
|
leveldb::Slice slKey = pcursor->key(); |
|
|
|
CDataStream ssKey(slKey.data(), slKey.data()+slKey.size(), SER_DISK, CLIENT_VERSION); |
|
|
|
char chType; |
|
|
|
ssKey >> chType; |
|
|
|
CTimestampIndexKey tik; |
|
|
|
ssKey >> tik; |
|
|
|
if (chType == DB_TIMESTAMPINDEX && tik.timestamp < high) { |
|
|
|
if (fActiveOnly) { |
|
|
|
if (blockOnchainActive(tik.blockHash)) { |
|
|
|
hashes.push_back(std::make_pair(tik.blockHash, tik.timestamp)); |
|
|
|
} |
|
|
|
} else { |
|
|
|
hashes.push_back(std::make_pair(tik.blockHash, tik.timestamp)); |
|
|
|
} |
|
|
|
|
|
|
|
pcursor->Next(); |
|
|
|
} else { |
|
|
|
break; |
|
|
|
} |
|
|
|
} catch (const std::exception& e) { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::WriteTimestampBlockIndex(const CTimestampBlockIndexKey &blockhashIndex, const CTimestampBlockIndexValue &logicalts) { |
|
|
|
CLevelDBBatch batch; |
|
|
|
batch.Write(make_pair(DB_BLOCKHASHINDEX, blockhashIndex), logicalts); |
|
|
|
return WriteBatch(batch); |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::ReadTimestampBlockIndex(const uint256 &hash, unsigned int <imestamp) { |
|
|
|
|
|
|
|
CTimestampBlockIndexValue(lts); |
|
|
|
if (!Read(std::make_pair(DB_BLOCKHASHINDEX, hash), lts)) |
|
|
|
return false; |
|
|
|
|
|
|
|
ltimestamp = lts.ltimestamp; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) { |
|
|
|
return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0'); |
|
|
|
} |
|
|
@ -272,6 +468,17 @@ bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
bool CBlockTreeDB::blockOnchainActive(const uint256 &hash) { |
|
|
|
CBlockIndex* pblockindex = mapBlockIndex[hash]; |
|
|
|
|
|
|
|
if (!chainActive.Contains(pblockindex)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool CBlockTreeDB::LoadBlockIndexGuts() |
|
|
|
{ |
|
|
|
boost::scoped_ptr<leveldb::Iterator> pcursor(NewIterator()); |
|
|
|