Skip to content

Commit

Permalink
update cache loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ohcee authored Jul 10, 2024
1 parent 05186cb commit c3cca7f
Showing 1 changed file with 44 additions and 85 deletions.
129 changes: 44 additions & 85 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,47 +532,34 @@ CZerocoinDB::CZerocoinDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapp
}

//TODO: add prefixes for zerocoindb to the top of the file insteadof using chars when doing database operations
bool CZerocoinDB::WriteCoinMintBatch(const std::map<libzerocoin::PublicCoin, uint256>& mintInfo) {
LogPrint(BCLog::ZEROCOINDB, "Starting WriteCoinMintBatch with %u mints\n", (unsigned int)mintInfo.size());

const size_t FLUSH_SIZE = 1073741824; // Set flush size to 1 GB
std::vector<std::pair<std::pair<char, uint256>, uint256>> cache;
bool CZerocoinDB::WriteCoinMintBatch(const std::map<libzerocoin::PublicCoin, uint256>& mintInfo)
{
const size_t FLUSH_SIZE = 1073741824; // 1 GB
size_t currentCacheSize = 0; // Track current cache size in bytes
std::vector<std::pair<std::pair<char, uint256>, uint256>> cache;

auto it = mintInfo.begin(); // Iterator for traversing the map

// Loop through the entire map
while (it != mintInfo.end()) {
// Add items to the cache until FLUSH_SIZE is reached or the end of the map is reached
for (; it != mintInfo.end(); ++it) {
libzerocoin::PublicCoin pubCoin = it->first; // Get the public coin
uint256 hash = GetPubCoinHash(pubCoin.getValue()); // Calculate the hash of the coin's value
size_t elementSize = sizeof('m') + hash.size() + it->second.size(); // Approximate size of the element in bytes
if (currentCacheSize + elementSize > FLUSH_SIZE) {
break; // If adding the next element exceeds FLUSH_SIZE, break out of the loop
}
cache.emplace_back(std::make_pair('m', hash), it->second); // Add to cache
currentCacheSize += elementSize; // Update current cache size
}
for (auto it = mintInfo.begin(); it != mintInfo.end(); ++it) {
libzerocoin::PublicCoin pubCoin = it->first;
uint256 hash = GetPubCoinHash(pubCoin.getValue());
size_t elementSize = sizeof('m') + hash.size() + it->second.size(); // Approximate size of the element in bytes
cache.emplace_back(std::make_pair('m', hash), it->second);
currentCacheSize += elementSize;

// Write to batch when cache reaches FLUSH_SIZE
if (currentCacheSize >= FLUSH_SIZE) {
LogPrint(BCLog::ZEROCOINDB, "Flushing %u coin mints to batch\n", (unsigned int)cache.size());
CDBBatch batch(*this);
for (const auto& item : cache) {
batch.Write(item.first, item.second);
}
if (!WriteBatch(batch, true)) {
return false; // If writing the batch fails, return false
}
cache.clear();
cache.clear(); // Clear the cache
currentCacheSize = 0; // Reset current cache size
}
}

// Write remaining items in cache
// Flush remaining items
if (!cache.empty()) {
LogPrint(BCLog::ZEROCOINDB, "Flushing remaining %u coin mints to batch\n", (unsigned int)cache.size());
CDBBatch batch(*this);
for (const auto& item : cache) {
batch.Write(item.first, item.second);
Expand All @@ -582,11 +569,9 @@ bool CZerocoinDB::WriteCoinMintBatch(const std::map<libzerocoin::PublicCoin, uin
}
}

LogPrint(BCLog::ZEROCOINDB, "Total coin mints written to db: %u\n", (unsigned int)mintInfo.size());
return true;
}


bool CZerocoinDB::ReadCoinMint(const CBigNum& bnPubcoin, uint256& hashTx)
{
return ReadCoinMint(GetPubCoinHash(bnPubcoin), hashTx);
Expand All @@ -603,49 +588,36 @@ bool CZerocoinDB::EraseCoinMint(const CBigNum& bnPubcoin)
return Erase(std::make_pair('m', hash));
}

bool CZerocoinDB::WriteCoinSpendBatch(const std::map<libzerocoin::CoinSpend, uint256>& spendInfo) {
LogPrint(BCLog::ZEROCOINDB, "Starting WriteCoinSpendBatch with %u spends\n", (unsigned int)spendInfo.size());

const size_t FLUSH_SIZE = 1073741824; // Set flush size to 1 GB
bool CZerocoinDB::WriteCoinSpendBatch(const std::map<libzerocoin::CoinSpend, uint256>& spendInfo)
{
const size_t FLUSH_SIZE = 1073741824; // 1 GB
size_t currentCacheSize = 0; // Track current cache size in bytes
std::vector<std::pair<std::pair<char, uint256>, uint256>> cache;
size_t currentCacheSize = 0;

auto it = spendInfo.begin(); // Iterator for traversing the map

// Loop through the entire map
while (it != spendInfo.end()) {
// Add items to the cache until FLUSH_SIZE is reached or the end of the map is reached
for (; it != spendInfo.end(); ++it) {
CBigNum bnSerial = it->first.getCoinSerialNumber(); // Get the coin serial number
CDataStream ss(SER_GETHASH, 0); // Create a data stream for hashing
ss << bnSerial; // Serialize the serial number into the data stream
uint256 hash = Hash(ss.begin(), ss.end()); // Compute the hash of the serialized data
size_t elementSize = sizeof('s') + hash.size() + it->second.size(); // Approximate size of the element in bytes
if (currentCacheSize + elementSize > FLUSH_SIZE) {
break; // If adding the next element exceeds FLUSH_SIZE, break out of the loop
}
cache.emplace_back(std::make_pair('s', hash), it->second); // Add to cache
currentCacheSize += elementSize; // Update current cache size
}

// Write to batch when cache reaches FLUSH_SIZE
for (auto it = spendInfo.begin(); it != spendInfo.end(); ++it) {
CBigNum bnSerial = it->first.getCoinSerialNumber();
CDataStream ss(SER_GETHASH, 0);
ss << bnSerial;
uint256 hash = Hash(ss.begin(), ss.end());
size_t elementSize = sizeof('s') + hash.size() + it->second.size(); // Approximate size of the element in bytes
cache.emplace_back(std::make_pair('s', hash), it->second);
currentCacheSize += elementSize;

if (currentCacheSize >= FLUSH_SIZE) {
LogPrint(BCLog::ZEROCOINDB, "Flushing %u coin spends to batch\n", (unsigned int)cache.size());
CDBBatch batch(*this);
for (const auto& item : cache) {
batch.Write(item.first, item.second);
}
if (!WriteBatch(batch, true)) {
return false; // If writing the batch fails, return false
}
cache.clear();
currentCacheSize = 0;
cache.clear(); // Clear the cache
currentCacheSize = 0; // Reset current cache size
}
}

// Write remaining items in cache
// Flush remaining items
if (!cache.empty()) {
LogPrint(BCLog::ZEROCOINDB, "Flushing remaining %u coin spends to batch\n", (unsigned int)cache.size());
CDBBatch batch(*this);
for (const auto& item : cache) {
batch.Write(item.first, item.second);
Expand All @@ -655,10 +627,10 @@ bool CZerocoinDB::WriteCoinSpendBatch(const std::map<libzerocoin::CoinSpend, uin
}
}

LogPrint(BCLog::ZEROCOINDB, "Total coin spends written to db: %u\n", (unsigned int)spendInfo.size());
return true;
}


bool CZerocoinDB::ReadCoinSpend(const CBigNum& bnSerial, uint256& txHash)
{
CDataStream ss(SER_GETHASH, 0);
Expand All @@ -682,47 +654,34 @@ bool CZerocoinDB::EraseCoinSpend(const CBigNum& bnSerial)
return Erase(std::make_pair('s', hash));
}

bool CZerocoinDB::WritePubcoinSpendBatch(std::map<uint256, uint256>& mapPubcoinSpends, const uint256& hashBlock) {
LogPrint(BCLog::ZEROCOINDB, "Starting WritePubcoinSpendBatch with %u spends\n", (unsigned int)mapPubcoinSpends.size());

const size_t FLUSH_SIZE = 1073741824; // Set flush size to 1 GB
bool CZerocoinDB::WritePubcoinSpendBatch(std::map<uint256, uint256>& mapPubcoinSpends, const uint256& hashBlock)
{
const size_t FLUSH_SIZE = 1073741824; // 1 GB
size_t currentCacheSize = 0; // Track current cache size in bytes
std::vector<std::pair<std::pair<char, uint256>, std::pair<uint256, uint256>>> cache;
size_t currentCacheSize = 0;

auto it = mapPubcoinSpends.begin(); // Iterator for traversing the map

// Loop through the entire map
while (it != mapPubcoinSpends.end()) {
// Add items to the cache until FLUSH_SIZE is reached or the end of the map is reached
for (; it != mapPubcoinSpends.end(); ++it) {
const uint256& hashPubcoin = it->first; // Get the hash of the public coin
const uint256& txid = it->second; // Get the associated transaction ID
size_t elementSize = sizeof('l') + hashPubcoin.size() + txid.size() + hashBlock.size(); // Approximate size of the element in bytes
if (currentCacheSize + elementSize > FLUSH_SIZE) {
break; // If adding the next element exceeds FLUSH_SIZE, break out of the loop
}
cache.emplace_back(std::make_pair('l', hashPubcoin), std::make_pair(txid, hashBlock)); // Add to cache
currentCacheSize += elementSize;
}

// Write to batch when cache reaches FLUSH_SIZE
for (const auto& pair : mapPubcoinSpends) {
const uint256& hashPubcoin = pair.first;
const uint256& txid = pair.second;
size_t elementSize = sizeof('l') + hashPubcoin.size() + txid.size() + hashBlock.size(); // Approximate size of the element in bytes
cache.emplace_back(std::make_pair('l', hashPubcoin), std::make_pair(txid, hashBlock));
currentCacheSize += elementSize;

if (currentCacheSize >= FLUSH_SIZE) {
LogPrint(BCLog::ZEROCOINDB, "Flushing %u pubcoin spends to batch\n", (unsigned int)cache.size());
CDBBatch batch(*this);
for (const auto& item : cache) {
batch.Write(item.first, item.second);
}
if (!WriteBatch(batch, true)) {
return false; // If writing the batch fails, return false
}
cache.clear();
currentCacheSize = 0;
cache.clear(); // Clear the cache
currentCacheSize = 0; // Reset current cache size
}
}

// Write remaining items in cache
// Flush remaining items
if (!cache.empty()) {
LogPrint(BCLog::ZEROCOINDB, "Flushing remaining %u pubcoin spends to batch\n", (unsigned int)cache.size());
CDBBatch batch(*this);
for (const auto& item : cache) {
batch.Write(item.first, item.second);
Expand All @@ -732,10 +691,10 @@ bool CZerocoinDB::WritePubcoinSpendBatch(std::map<uint256, uint256>& mapPubcoinS
}
}

LogPrint(BCLog::ZEROCOINDB, "Total pubcoin spends written to db: %u\n", (unsigned int)mapPubcoinSpends.size());
return true;
}


bool CZerocoinDB::ReadPubcoinSpend(const uint256& hashPubcoin, uint256& txHash, uint256& hashBlock)
{
std::pair<uint256, uint256> pairTxHashblock;
Expand Down

0 comments on commit c3cca7f

Please sign in to comment.