Skip to content

Commit 51e0057

Browse files
committed
Limit memory usage of mapZerocoinSupply
The current code retains functionality to validate past zerocoin transactions. The ported code uses too much memory to store trivial zerocoin related data. Supply items in the map are now only stored: - When zerocoin spends were possible - When the supply denomination <> 0
1 parent a9e7b4a commit 51e0057

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/chain.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class CBlockIndex
219219
unsigned int nStakeModifierChecksum; // checksum of index; in-memeory only
220220

221221
//! zerocoin specific fields
222-
std::map<libzerocoin::CoinDenomination, int64_t> mapZerocoinSupply;
222+
std::map<libzerocoin::CoinDenomination, uint16_t> mapZerocoinSupply;
223223
std::vector<libzerocoin::CoinDenomination> vMintDenominationsInBlock;
224224

225225
//! ATP specific fields
@@ -267,10 +267,7 @@ class CBlockIndex
267267
nStakeModifierV2 = uint256();
268268
nStakeModifierChecksum = 0;
269269

270-
// Start supply of each denomination with 0s
271-
for (auto& denom : libzerocoin::zerocoinDenomList) {
272-
mapZerocoinSupply.insert(std::make_pair(denom, 0));
273-
}
270+
mapZerocoinSupply.clear();
274271
vMintDenominationsInBlock.clear();
275272
nAccumulatorCheckpoint = uint256();
276273

@@ -419,7 +416,12 @@ class CBlockIndex
419416
*/
420417
int64_t GetZcMints(libzerocoin::CoinDenomination denom) const
421418
{
422-
return mapZerocoinSupply.at(denom);
419+
auto it = mapZerocoinSupply.find(denom);
420+
if (it != mapZerocoinSupply.end()) {
421+
return it->second;
422+
} else {
423+
return 0;
424+
}
423425
}
424426

425427
/**
@@ -529,7 +531,7 @@ class CDiskBlockIndex : public CBlockIndex
529531
READWRITE(nNonce);
530532

531533
READWRITE(VARINT(nFlags));
532-
if(this->nVersion > 7) {
534+
if(this->nVersion > 7 && this->nVersion <= 11) {
533535
READWRITE(nAccumulatorCheckpoint);
534536
READWRITE(mapZerocoinSupply);
535537
READWRITE(vMintDenominationsInBlock);

src/xion/xionchain.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ std::list<libzerocoin::CoinDenomination> ZerocoinSpendListFromBlock(const CBlock
373373

374374
bool UpdateXIONSupply(const CBlock& block, CBlockIndex* pindex, bool fJustCheck)
375375
{
376+
// Only update xION supply when zerocoin mint amount can change
377+
if (pindex->nVersion <= 3 || pindex->nVersion > 11) return true;
378+
376379
std::list<CZerocoinMint> listMints;
377380
bool fFilterInvalid = false;//pindex->nHeight >= Params().Zerocoin_Block_RecalculateAccumulators();
378381
BlockToZerocoinMintList(block, listMints, fFilterInvalid);
@@ -381,7 +384,8 @@ bool UpdateXIONSupply(const CBlock& block, CBlockIndex* pindex, bool fJustCheck)
381384
// Initialize zerocoin supply to the supply from previous block
382385
if (pindex->pprev && pindex->pprev->GetBlockHeader().nVersion > 3) {
383386
for (auto& denom : libzerocoin::zerocoinDenomList) {
384-
pindex->mapZerocoinSupply.at(denom) = pindex->pprev->GetZcMints(denom);
387+
uint16_t nMints = pindex->pprev->GetZcMints(denom);
388+
if (nMints != 0) pindex->mapZerocoinSupply[denom] = nMints;
385389
}
386390
}
387391

@@ -393,7 +397,7 @@ bool UpdateXIONSupply(const CBlock& block, CBlockIndex* pindex, bool fJustCheck)
393397
for (auto& m : listMints) {
394398
libzerocoin::CoinDenomination denom = m.GetDenomination();
395399
pindex->vMintDenominationsInBlock.push_back(m.GetDenomination());
396-
pindex->mapZerocoinSupply.at(denom)++;
400+
pindex->mapZerocoinSupply[denom] = pindex->GetZcMints(denom) + 1;
397401

398402
/*
399403
//Remove any of our own mints from the mintpool
@@ -420,17 +424,20 @@ bool UpdateXIONSupply(const CBlock& block, CBlockIndex* pindex, bool fJustCheck)
420424
}
421425

422426
for (auto& denom : listSpends) {
423-
pindex->mapZerocoinSupply.at(denom)--;
424-
nAmountZerocoinSpent += libzerocoin::ZerocoinDenominationToAmount(denom);
427+
uint16_t nMints = pindex->GetZcMints(denom);
425428

426429
// zerocoin failsafe
427-
if (pindex->GetZcMints(denom) < 0)
430+
if (nMints == 0)
428431
return error("Block contains zerocoins that spend more than are in the available supply to spend");
432+
433+
pindex->mapZerocoinSupply[denom] = nMints - 1;
434+
nAmountZerocoinSpent += libzerocoin::ZerocoinDenominationToAmount(denom);
435+
429436
}
430437
}
431438

432439
for (auto& denom : libzerocoin::zerocoinDenomList)
433-
LogPrint(BCLog::ZEROCOIN, "%s coins for denomination %d pubcoin %s\n", __func__, denom, pindex->mapZerocoinSupply.at(denom));
440+
LogPrint(BCLog::ZEROCOIN, "%s coins for denomination %d pubcoin %s\n", __func__, denom, pindex->GetZcMints(denom));
434441

435442
return true;
436443
}

0 commit comments

Comments
 (0)