Skip to content

Commit

Permalink
fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
ohcee committed Jul 29, 2024
1 parent a106d07 commit 792683b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ std::map<libzerocoin::PublicCoin, uint256> cacheMints;
std::map<uint256, uint256> cacheSpentPubcoins;

bool FlushCacheToDatabase(const CBlockIndex* pindex, CValidationState& state) {
LogPrintf("Flushing cache to database. Cache sizes: spends=%d, mints=%d, pubcoins=%d\n", cacheSpends.size(), cacheMints.size(), cacheSpentPubcoins.size());

if (!pzerocoinDB->WriteCoinSpendBatch(cacheSpends)) {
LogPrintf("Failed to write coin spends to database\n");
return state.Error("Failed to record coin serials to database");
Expand All @@ -105,18 +107,22 @@ bool FlushCacheToDatabase(const CBlockIndex* pindex, CValidationState& state) {
cacheSpends.clear();
cacheMints.clear();
cacheSpentPubcoins.clear();

LogPrintf("Cache successfully flushed to database\n");
return true;
}

bool CacheAndFlushZerocoinData(CValidationState& state, const CBlockIndex* pindex, const std::map<libzerocoin::CoinSpend, uint256>& mapSpends, const std::map<libzerocoin::PublicCoin, uint256>& mapMints, const std::map<uint256, uint256>& mapSpentPubcoinsInBlock) {
// Add items to cache
LogPrintf("Adding %d spends, %d mints, and %d pubcoin spends to cache\n", mapSpends.size(), mapMints.size(), mapSpentPubcoinsInBlock.size());

cacheSpends.insert(mapSpends.begin(), mapSpends.end());
cacheMints.insert(mapMints.begin(), mapMints.end());
if (pindex->nHeight >= Params().HeightLightZerocoin()) {
cacheSpentPubcoins.insert(mapSpentPubcoinsInBlock.begin(), mapSpentPubcoinsInBlock.end());
}

LogPrintf("Cache sizes after insertion: spends=%d, mints=%d, pubcoins=%d\n", cacheSpends.size(), cacheMints.size(), cacheSpentPubcoins.size());

// Check if cache size threshold is reached
if (cacheSpends.size() >= CACHE_SIZE_THRESHOLD || cacheMints.size() >= CACHE_SIZE_THRESHOLD || cacheSpentPubcoins.size() >= CACHE_SIZE_THRESHOLD) {
LogPrintf("Cache size threshold reached, flushing to database\n");
Expand All @@ -127,11 +133,16 @@ bool CacheAndFlushZerocoinData(CValidationState& state, const CBlockIndex* pinde
}

bool ProcessZerocoinData(CValidationState& state, const CBlockIndex* pindex, const std::map<libzerocoin::CoinSpend, uint256>& mapSpends, const std::map<libzerocoin::PublicCoin, uint256>& mapMints, const std::map<uint256, uint256>& mapSpentPubcoinsInBlock) {
LogPrintf("Processing zerocoin data for block at height %d\n", pindex->nHeight);

if (!CacheAndFlushZerocoinData(state, pindex, mapSpends, mapMints, mapSpentPubcoinsInBlock))
return state.Error("Failed to process zerocoin");

LogPrintf("Successfully processed zerocoin data for block at height %d\n", pindex->nHeight);
return true;
}


/**
* Global state
*/
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static const bool DEFAULT_PEERBLOOMFILTERS = true;
static const int DEFAULT_STOPATHEIGHT = 0;

// Define the cache size threshold
const size_t CACHE_SIZE_THRESHOLD = 100;
const size_t CACHE_SIZE_THRESHOLD = 10;

bool CacheAndFlushZerocoinData(CValidationState& state, const CBlockIndex* pindex,
const std::map<uint256, uint256>& mapSpends,
Expand Down

0 comments on commit 792683b

Please sign in to comment.