Skip to content

Commit 494ea2a

Browse files
committed
[Core] Guard nMoneySupply and mapZerocoinSupply with cs_main LOCK
1 parent ec2f82d commit 494ea2a

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

src/consensus/zerocoin_verify.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ bool ContextualCheckZerocoinSpendNoSerialCheck(const CTransaction& tx, const lib
191191

192192
bool RecalculatePIVSupply(int nHeightStart, bool fSkipZpiv)
193193
{
194+
AssertLockHeld(cs_main);
195+
194196
const Consensus::Params& consensus = Params().GetConsensus();
195197
const int chainHeight = chainActive.Height();
196198
if (nHeightStart > chainHeight)

src/init.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,43 +1488,48 @@ bool AppInit2()
14881488
bool fReindexZerocoin = GetBoolArg("-reindexzerocoin", false);
14891489
bool fReindexMoneySupply = GetBoolArg("-reindexmoneysupply", false);
14901490

1491-
// initialize PIV and zPIV supply to 0
1492-
mapZerocoinSupply.clear();
1493-
for (auto& denom : libzerocoin::zerocoinDenomList) mapZerocoinSupply.insert(std::make_pair(denom, 0));
1494-
nMoneySupply = 0;
1495-
1496-
// Load PIV and zPIV supply from DB
1497-
const int chainHeight = WITH_LOCK(cs_main, return chainActive.Height());
1498-
if (chainHeight >= 0) {
1499-
const uint256& tipHash = WITH_LOCK(cs_main, return chainActive[chainHeight]->GetBlockHash());
1500-
CLegacyBlockIndex bi;
1501-
1502-
// Load zPIV supply map
1503-
if (!fReindexZerocoin && chainHeight >= consensus.height_start_ZC && !zerocoinDB->ReadZCSupply(mapZerocoinSupply)) {
1504-
// try first reading legacy block index from DB
1505-
if (pblocktree->ReadLegacyBlockIndex(tipHash, bi) && !bi.mapZerocoinSupply.empty()) {
1506-
mapZerocoinSupply = bi.mapZerocoinSupply;
1507-
} else {
1508-
// reindex from disk
1509-
fReindexZerocoin = true;
1491+
int chainHeight;
1492+
{
1493+
LOCK(cs_main);
1494+
chainHeight = chainActive.Height();
1495+
1496+
// initialize PIV and zPIV supply to 0
1497+
mapZerocoinSupply.clear();
1498+
for (auto& denom : libzerocoin::zerocoinDenomList) mapZerocoinSupply.insert(std::make_pair(denom, 0));
1499+
nMoneySupply = 0;
1500+
1501+
// Load PIV and zPIV supply from DB
1502+
if (chainHeight >= 0) {
1503+
const uint256& tipHash = chainActive[chainHeight]->GetBlockHash();
1504+
CLegacyBlockIndex bi;
1505+
1506+
// Load zPIV supply map
1507+
if (!fReindexZerocoin && chainHeight >= consensus.height_start_ZC && !zerocoinDB->ReadZCSupply(mapZerocoinSupply)) {
1508+
// try first reading legacy block index from DB
1509+
if (pblocktree->ReadLegacyBlockIndex(tipHash, bi) && !bi.mapZerocoinSupply.empty()) {
1510+
mapZerocoinSupply = bi.mapZerocoinSupply;
1511+
} else {
1512+
// reindex from disk
1513+
fReindexZerocoin = true;
1514+
}
15101515
}
15111516

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;
1517+
// Load PIV supply amount
1518+
if (!fReindexMoneySupply && !pblocktree->ReadMoneySupply(nMoneySupply)) {
1519+
// try first reading legacy block index from DB
1520+
if (pblocktree->ReadLegacyBlockIndex(tipHash, bi)) {
1521+
nMoneySupply = bi.nMoneySupply;
1522+
} else {
1523+
// reindex from disk
1524+
fReindexMoneySupply = true;
1525+
}
15221526
}
15231527
}
15241528
}
15251529

15261530
// Drop all information from the zerocoinDB and repopulate
15271531
if (fReindexZerocoin && chainHeight >= consensus.height_start_ZC) {
1532+
LOCK(cs_main);
15281533
uiInterface.InitMessage(_("Reindexing zerocoin database..."));
15291534
std::string strError = ReindexZerocoinDB();
15301535
if (strError != "") {
@@ -1535,6 +1540,7 @@ bool AppInit2()
15351540

15361541
// Recalculate money supply
15371542
if (fReindexMoneySupply) {
1543+
LOCK(cs_main);
15381544
// Skip zpiv if already reindexed
15391545
RecalculatePIVSupply(1, fReindexZerocoin);
15401546
}

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,8 @@ void DataBaseAccChecksum(CBlockIndex* pindex, bool fWrite)
20352035

20362036
bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
20372037
{
2038+
AssertLockHeld(cs_main);
2039+
20382040
if (pindex->GetBlockHash() != view.GetBestBlock())
20392041
LogPrintf("%s : pindex=%s view=%s\n", __func__, pindex->GetBlockHash().GetHex(), view.GetBestBlock().GetHex());
20402042
assert(pindex->GetBlockHash() == view.GetBestBlock());

src/zpivchain.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ bool IsSerialInBlockchain(const uint256& hashSerial, int& nHeightTx, uint256& tx
254254

255255
std::string ReindexZerocoinDB()
256256
{
257+
AssertLockHeld(cs_main);
258+
257259
if (!zerocoinDB->WipeCoins("spends") || !zerocoinDB->WipeCoins("mints")) {
258260
return _("Failed to wipe zerocoinDB");
259261
}
@@ -405,6 +407,8 @@ std::list<libzerocoin::CoinDenomination> ZerocoinSpendListFromBlock(const CBlock
405407

406408
int64_t GetZerocoinSupply()
407409
{
410+
AssertLockHeld(cs_main);
411+
408412
if (mapZerocoinSupply.empty())
409413
return 0;
410414

@@ -417,6 +421,8 @@ int64_t GetZerocoinSupply()
417421

418422
bool UpdateZPIVSupplyConnect(const CBlock& block, CBlockIndex* pindex, bool fJustCheck)
419423
{
424+
AssertLockHeld(cs_main);
425+
420426
const Consensus::Params& consensus = Params().GetConsensus();
421427
if (pindex->nHeight < consensus.height_start_ZC)
422428
return true;
@@ -474,6 +480,8 @@ bool UpdateZPIVSupplyConnect(const CBlock& block, CBlockIndex* pindex, bool fJus
474480

475481
bool UpdateZPIVSupplyDisconnect(const CBlock& block, CBlockIndex* pindex)
476482
{
483+
AssertLockHeld(cs_main);
484+
477485
const Consensus::Params& consensus = Params().GetConsensus();
478486
if (pindex->nHeight < consensus.height_start_ZC)
479487
return true;

0 commit comments

Comments
 (0)