@@ -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 }
0 commit comments