Skip to content

Commit 361e584

Browse files
committed
[Chain] Remove CBlockIndex::nMoneySupply, replace with global instance
1 parent 15d22a6 commit 361e584

File tree

8 files changed

+84
-38
lines changed

8 files changed

+84
-38
lines changed

src/chain.h

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ class CBlockIndex
215215
// char vector holding the stake modifier bytes. It is empty for PoW blocks.
216216
// Modifier V1 is 64 bit while modifier V2 is 256 bit.
217217
std::vector<unsigned char> vStakeModifier{};
218-
int64_t nMoneySupply{0};
219218
unsigned int nFlags{0};
220219

221220
//! block header
@@ -275,7 +274,7 @@ class CBlockIndex
275274

276275
// New serialization introduced with 4.0.99
277276
static const int DBI_OLD_SER_VERSION = 4009900;
278-
static const int DBI_SER_VERSION_NO_ZC = 4009902; // removes mapZerocoinSupply
277+
static const int DBI_SER_VERSION_NO_ZC = 4009902; // removes mapZerocoinSupply, nMoneySupply
279278

280279
class CDiskBlockIndex : public CBlockIndex
281280
{
@@ -310,8 +309,23 @@ class CDiskBlockIndex : public CBlockIndex
310309
if (nStatus & BLOCK_HAVE_UNDO)
311310
READWRITE(VARINT(nUndoPos));
312311

313-
if (nSerVersion > DBI_OLD_SER_VERSION) {
314-
// Serialization with CLIENT_VERSION > 4009900
312+
if (nSerVersion >= DBI_SER_VERSION_NO_ZC) {
313+
// Serialization with CLIENT_VERSION = 4009902+
314+
READWRITE(nFlags);
315+
READWRITE(this->nVersion);
316+
READWRITE(vStakeModifier);
317+
READWRITE(hashPrev);
318+
READWRITE(hashMerkleRoot);
319+
READWRITE(nTime);
320+
READWRITE(nBits);
321+
READWRITE(nNonce);
322+
if(this->nVersion > 3 && this->nVersion < 7)
323+
READWRITE(nAccumulatorCheckpoint);
324+
325+
} else if (nSerVersion > DBI_OLD_SER_VERSION) {
326+
// Serialization with CLIENT_VERSION = 4009901
327+
std::map<libzerocoin::CoinDenomination, int64_t> mapZerocoinSupply;
328+
int64_t nMoneySupply = 0;
315329
READWRITE(nMoneySupply);
316330
READWRITE(nFlags);
317331
READWRITE(this->nVersion);
@@ -322,18 +336,15 @@ class CDiskBlockIndex : public CBlockIndex
322336
READWRITE(nBits);
323337
READWRITE(nNonce);
324338
if(this->nVersion > 3) {
325-
// zc supply removed in 4.0.99.2
326-
if (nSerVersion < DBI_SER_VERSION_NO_ZC) {
327-
std::map<libzerocoin::CoinDenomination, int64_t> mapZerocoinSupply;
328-
READWRITE(mapZerocoinSupply);
329-
}
339+
READWRITE(mapZerocoinSupply);
330340
if(this->nVersion < 7) READWRITE(nAccumulatorCheckpoint);
331341
}
332342

333343
} else {
334-
// Serialization with CLIENT_VERSION <= 4009900
344+
// Serialization with CLIENT_VERSION = 4009900-
335345
int64_t nMint = 0;
336-
uint256 hashNext;
346+
uint256 hashNext{};
347+
int64_t nMoneySupply = 0;
337348
READWRITE(nMint);
338349
READWRITE(nMoneySupply);
339350
READWRITE(nFlags);
@@ -410,6 +421,7 @@ class CLegacyBlockIndex : public CBlockIndex
410421
COutPoint prevoutStake{};
411422
unsigned int nStakeTime = 0;
412423
std::vector<libzerocoin::CoinDenomination> vMintDenominationsInBlock;
424+
int64_t nMoneySupply = 0;
413425

414426

415427
ADD_SERIALIZE_METHODS;
@@ -420,6 +432,11 @@ class CLegacyBlockIndex : public CBlockIndex
420432
if (!(nType & SER_GETHASH))
421433
READWRITE(VARINT(nSerVersion));
422434

435+
if (nSerVersion >= DBI_SER_VERSION_NO_ZC) {
436+
// no extra serialized field
437+
return;
438+
}
439+
423440
READWRITE(VARINT(nHeight));
424441
READWRITE(VARINT(nStatus));
425442
READWRITE(VARINT(nTx));
@@ -431,7 +448,7 @@ class CLegacyBlockIndex : public CBlockIndex
431448
READWRITE(VARINT(nUndoPos));
432449

433450
if (nSerVersion > DBI_OLD_SER_VERSION) {
434-
// Serialization with CLIENT_VERSION > 4009900
451+
// Serialization with CLIENT_VERSION = 4009901
435452
READWRITE(nMoneySupply);
436453
READWRITE(nFlags);
437454
READWRITE(this->nVersion);
@@ -442,15 +459,12 @@ class CLegacyBlockIndex : public CBlockIndex
442459
READWRITE(nBits);
443460
READWRITE(nNonce);
444461
if(this->nVersion > 3) {
445-
// zc supply removed in 4.0.99.2
446-
if (nSerVersion < DBI_SER_VERSION_NO_ZC) {
447-
READWRITE(mapZerocoinSupply);
448-
}
462+
READWRITE(mapZerocoinSupply);
449463
if(this->nVersion < 7) READWRITE(nAccumulatorCheckpoint);
450464
}
451465

452466
} else {
453-
// Serialization with CLIENT_VERSION <= 4009900
467+
// Serialization with CLIENT_VERSION = 4009900-
454468
READWRITE(nMint);
455469
READWRITE(nMoneySupply);
456470
READWRITE(nFlags);

src/consensus/zerocoin_verify.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ bool RecalculatePIVSupply(int nHeightStart, bool fSkipZpiv)
197197
return false;
198198

199199
CBlockIndex* pindex = chainActive[nHeightStart];
200-
CAmount nSupplyPrev = pindex->pprev->nMoneySupply;
201200
if (nHeightStart == consensus.height_start_ZC)
202-
nSupplyPrev = CAmount(5449796547496199);
201+
nMoneySupply = CAmount(5449796547496199);
203202

204203
if (!fSkipZpiv) {
205204
// initialize supply to 0
@@ -246,8 +245,7 @@ bool RecalculatePIVSupply(int nHeightStart, bool fSkipZpiv)
246245
}
247246

248247
// Rewrite money supply
249-
pindex->nMoneySupply = nSupplyPrev + nValueOut - nValueIn;
250-
nSupplyPrev = pindex->nMoneySupply;
248+
nMoneySupply += (nValueOut - nValueIn);
251249

252250
// Rewrite zpiv supply too
253251
if (!fSkipZpiv && pindex->nHeight >= consensus.height_start_ZC) {
@@ -257,14 +255,14 @@ bool RecalculatePIVSupply(int nHeightStart, bool fSkipZpiv)
257255
// Add fraudulent funds to the supply and remove any recovered funds.
258256
if (pindex->nHeight == consensus.height_ZC_RecalcAccumulators) {
259257
const CAmount nInvalidAmountFiltered = 268200*COIN; //Amount of invalid coins filtered through exchanges, that should be considered valid
260-
LogPrintf("%s : Original money supply=%s\n", __func__, FormatMoney(pindex->nMoneySupply));
258+
LogPrintf("%s : Original money supply=%s\n", __func__, FormatMoney(nMoneySupply));
261259

262-
pindex->nMoneySupply += nInvalidAmountFiltered;
263-
LogPrintf("%s : Adding filtered funds to supply + %s : supply=%s\n", __func__, FormatMoney(nInvalidAmountFiltered), FormatMoney(pindex->nMoneySupply));
260+
nMoneySupply += nInvalidAmountFiltered;
261+
LogPrintf("%s : Adding filtered funds to supply + %s : supply=%s\n", __func__, FormatMoney(nInvalidAmountFiltered), FormatMoney(nMoneySupply));
264262

265263
CAmount nLocked = GetInvalidUTXOValue();
266-
pindex->nMoneySupply -= nLocked;
267-
LogPrintf("%s : Removing locked from supply - %s : supply=%s\n", __func__, FormatMoney(nLocked), FormatMoney(pindex->nMoneySupply));
264+
nMoneySupply -= nLocked;
265+
LogPrintf("%s : Removing locked from supply - %s : supply=%s\n", __func__, FormatMoney(nLocked), FormatMoney(nMoneySupply));
268266
}
269267

270268
assert(pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex)));

src/init.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,26 +1486,40 @@ bool AppInit2()
14861486
invalid_out::LoadSerials();
14871487

14881488
bool fReindexZerocoin = GetBoolArg("-reindexzerocoin", false);
1489+
bool fReindexMoneySupply = GetBoolArg("-reindexmoneysupply", false);
14891490

1490-
// initialize zPIV supply to 0
1491+
// initialize PIV and zPIV supply to 0
14911492
mapZerocoinSupply.clear();
14921493
for (auto& denom : libzerocoin::zerocoinDenomList) mapZerocoinSupply.insert(std::make_pair(denom, 0));
1494+
nMoneySupply = 0;
14931495

1494-
// Load zPIV supply from DB
1496+
// Load PIV and zPIV supply from DB
14951497
const int chainHeight = WITH_LOCK(cs_main, return chainActive.Height());
14961498
if (chainHeight >= 0) {
14971499
const uint256& tipHash = WITH_LOCK(cs_main, return chainActive[chainHeight]->GetBlockHash());
14981500
CLegacyBlockIndex bi;
14991501

1500-
// Read zPIV supply map
1502+
// Load zPIV supply map
15011503
if (!fReindexZerocoin && chainHeight >= consensus.height_start_ZC && !zerocoinDB->ReadZCSupply(mapZerocoinSupply)) {
1502-
// try first reading legacy block index from disk
1504+
// try first reading legacy block index from DB
15031505
if (pblocktree->ReadLegacyBlockIndex(tipHash, bi) && !bi.mapZerocoinSupply.empty()) {
15041506
mapZerocoinSupply = bi.mapZerocoinSupply;
15051507
} else {
15061508
// reindex from disk
15071509
fReindexZerocoin = true;
15081510
}
1511+
1512+
}
1513+
1514+
// Load PIV supply amount
1515+
if (!fReindexMoneySupply && !pblocktree->ReadMoneySupply(nMoneySupply)) {
1516+
// try first reading legacy block index from DB
1517+
if (pblocktree->ReadLegacyBlockIndex(tipHash, bi)) {
1518+
nMoneySupply = bi.nMoneySupply;
1519+
} else {
1520+
// reindex from disk
1521+
fReindexMoneySupply = true;
1522+
}
15091523
}
15101524
}
15111525

@@ -1519,8 +1533,8 @@ bool AppInit2()
15191533
}
15201534
}
15211535

1522-
// Recalculate money supply for blocks that are impacted by accounting issue after zerocoin activation
1523-
if (GetBoolArg("-reindexmoneysupply", false)) {
1536+
// Recalculate money supply
1537+
if (fReindexMoneySupply) {
15241538
// Skip zpiv if already reindexed
15251539
RecalculatePIVSupply(1, fReindexZerocoin);
15261540
}

src/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ BlockMap mapBlockIndex;
8080
CChain chainActive;
8181
CBlockIndex* pindexBestHeader = NULL;
8282
int64_t nTimeBestReceived = 0;
83+
int64_t nMoneySupply;
8384

8485
// Best block section
8586
Mutex g_best_block_mutex;
@@ -2416,10 +2417,8 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
24162417
pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
24172418
}
24182419

2419-
// track money supply and mint amount info
2420-
CAmount nMoneySupplyPrev = pindex->pprev ? pindex->pprev->nMoneySupply : 0;
2421-
pindex->nMoneySupply = nMoneySupplyPrev + nValueOut - nValueIn;
2422-
const int64_t nMint = pindex->nMoneySupply - nMoneySupplyPrev + nFees;
2420+
// track mint amount info
2421+
const int64_t nMint = (nValueOut - nValueIn) + nFees;
24232422

24242423
int64_t nTime1 = GetTimeMicros();
24252424
nTimeConnect += nTime1 - nTimeStart;
@@ -2529,6 +2528,9 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
25292528
nMoneySupply -= nLocked;
25302529
}
25312530

2531+
// Update PIV money supply
2532+
nMoneySupply += (nValueOut - nValueIn);
2533+
25322534
int64_t nTime3 = GetTimeMicros();
25332535
nTimeIndex += nTime3 - nTime2;
25342536
LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeIndex * 0.000001);
@@ -2607,6 +2609,10 @@ bool static FlushStateToDisk(CValidationState& state, FlushStateMode mode)
26072609
if (!mapZerocoinSupply.empty() && !zerocoinDB->WriteZCSupply(mapZerocoinSupply)) {
26082610
return AbortNode(state, "Failed to write zerocoin supply to DB");
26092611
}
2612+
// Flush money supply
2613+
if (!pblocktree->WriteMoneySupply(nMoneySupply)) {
2614+
return AbortNode(state, "Failed to write money supply to DB");
2615+
}
26102616
}
26112617
// Finally flush the chainstate (which may refer to block index entries).
26122618
if (!pcoinsTip->Flush()) {

src/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ extern uint64_t nLastBlockTx;
127127
extern uint64_t nLastBlockSize;
128128
extern const std::string strMessageMagic;
129129
extern int64_t nTimeBestReceived;
130+
extern int64_t nMoneySupply;
130131

131132
// Best block section
132133
extern Mutex g_best_block_mutex;

src/rpc/misc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ UniValue getinfo(const UniValue& params, bool fHelp)
144144
return obj;
145145
}
146146

147-
obj.push_back(Pair("moneysupply",ValueFromAmount(chainActive.Tip()->nMoneySupply)));
147+
obj.push_back(Pair("moneysupply",ValueFromAmount(nMoneySupply)));
148148
UniValue zpivObj(UniValue::VOBJ);
149149
for (auto denom : libzerocoin::zerocoinDenomList) {
150150
if (mapZerocoinSupply.empty())

src/txdb.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ bool CCoinsViewDB::GetStats(CCoinsStats& stats) const
158158
return true;
159159
}
160160

161+
static const char MONEYSUPPLY = 'M';
162+
163+
bool CBlockTreeDB::WriteMoneySupply(const int64_t& nSupply)
164+
{
165+
return Write(MONEYSUPPLY, nSupply);
166+
}
167+
168+
bool CBlockTreeDB::ReadMoneySupply(int64_t& nSupply) const
169+
{
170+
return Read(MONEYSUPPLY, nSupply);
171+
}
172+
161173
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
162174
CLevelDBBatch batch;
163175
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
@@ -248,7 +260,6 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
248260
pindexNew->nAccumulatorCheckpoint = diskindex.nAccumulatorCheckpoint;
249261

250262
//Proof Of Stake
251-
pindexNew->nMoneySupply = diskindex.nMoneySupply;
252263
pindexNew->nFlags = diskindex.nFlags;
253264
pindexNew->vStakeModifier = diskindex.vStakeModifier;
254265

src/txdb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class CBlockTreeDB : public CLevelDBWrapper
9595
bool ReadInt(const std::string& name, int& nValue);
9696
bool LoadBlockIndexGuts();
9797
bool ReadLegacyBlockIndex(const uint256& blockHash, CLegacyBlockIndex& biRet);
98+
bool WriteMoneySupply(const int64_t& nSupply);
99+
bool ReadMoneySupply(int64_t& nSupply) const;
98100
};
99101

100102
/** Zerocoin database (zerocoin/) */

0 commit comments

Comments
 (0)