Skip to content

Conversation

@random-zebra
Copy link

Another attempt at backporting bitcoin#7917.
Previously in #1334, but put in stand-by few months ago due to some issue after reindex.

Tested both syncing from scratch and using -reindex flags. It seems all good now.
Reindex is still slower than resync for some reason, but at least not as much as in master (about 63% faster now).

@random-zebra
Copy link
Author

Rebased.

Copy link

@furszy furszy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial review ACK 788564a, performing a reindex and will do a second final round.

furszy
furszy previously approved these changes Sep 28, 2020
Copy link

@furszy furszy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 788564a

@random-zebra
Copy link
Author

Rebased on master, after #1872 merge

Copy link

@furszy furszy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 2d30933 after rebase

@Fuzzbawls
Copy link
Collaborator

Fuzzbawls commented Oct 3, 2020

This is going to need additional work in order to function as intended. The intention of this optimization was to do an initial first pass through the blk files to build the chain index, then do a second pass activating said chain index.

Given how our POS consensus works (validating the stake input), the first pass fails on the first POS block because, at this point, the blockIndex, chainActive, and the txindex have not been populated yet, leading to AcceptBlock() failing. At this point, the client falls back to network sync, or stalls in the event that there are no peer connections, making "offline reindexing" impossible.


For reference purposes, here is a table with full reindex timings for both mainnet and testnet using the current master branch, which I will update once this PR is functioning in an offline environment:

Intel Core i9-9900K, 64GB DDR4 3200 RAM, 7200RPM HDD, Windows 10 (WSL), GUI Client, Empty wallet
Branch Testnet Time Testnet Height Mainnet Time Mainnet Height
master ~51m25s 1777537 ~5hr10m 2532969
pr-1864 TBD 1777537 TBD 2532969
Intel Core i9-9900K, 64GB DDR4 3200 RAM, NVME SSD, Windows 10 (WSL), GUI Client, Empty wallet
Branch Testnet Time Testnet Height Mainnet Height Mainnet Time
master ~45m 1777537 ~3hr9m 2532969
pr-1864 TBD 1777537 TBD 2532969

@random-zebra
Copy link
Author

Good finding. Closing for now... this will need more work that can only be done after 5.0

random-zebra added a commit to random-zebra/PIVX that referenced this pull request Oct 10, 2020
…oney supply

dcc032c [Cleanup] Remove ZC_WrappedSerialsSupply no longer needed (random-zebra)
0e7921e [DB] Also prune invalid/fraudulent/frozen outputs from the coins view (random-zebra)
b8647bd [Tests] Fix expected PIV supply in mining_pos_reorg (random-zebra)
40b7ea3 [DB] Prune zerocoin mints from coins DB at startup (random-zebra)
b89bb91 [BUG] Don't try to spend zc mint coins from the cache in DisconnectBlock (random-zebra)
cfc06ed [Core] Don't add zerocoin mints to the coins cache (random-zebra)
e8a782c [Cleanup] Remove unused function GetInvalidUTXOValue (random-zebra)
db7d8fd [Core] Remove nMoneySupply global and RecalculatePIVSupply function (random-zebra)
b674fde [DB] Remove money supply databasing (random-zebra)
373ee90 [Cleanup] Remove --reindexmoneysupply startup flag (random-zebra)
02c9aaa [RPC] Return total available utxo value as moneysupply (random-zebra)
cdf9bc1 [Core] Implement CCoinsViewCache::GetTotalAmount (random-zebra)

Pull request description:

  Instead of updating and databasing independently `nMoneySupply`, just use the sum of all unspent tx outputs, returned by the coins cache.

  This has the following advantages:
    - Cleaner code: we don't need to add special cases for exceptional one-time events, such as wrapped serials, accumulator recalculation, etc.
    - More robust: with the old code, `nMoneySupply` is databased at every block and does not  follow the chainstate flushing strategy, so it's very easy to get an inconsistent state (see PIVX-Project#1873).
  Even more so, considering the absence of any consistency check between the actual chain height and the height at which the supply was updated.
    - More accurate number: this returns only the __spendable__ supply, so it excludes all unspendable outputs (OP_RETURN), such as proposals / finalized budgets collaterals, that were originally counted in.
  Those outputs, in fact, are effectively "burnt" coins and should not be part of the total supply calculation.

  Disadvantage:
    - Calculating the supply from the coins DB is a bit slow, but the only place where this is done is in the RPC `getinfo`.

  This PR removes the global variable `nMoneySupply` and all related code (Closes PIVX-Project#1873) and gets rid of the startup flag `--reindexmoneysupply` no longer needed (PIVX-Project#1864 introduces `reindex-chainstate`).

  ⚠️ This also fixes the following bug with zerocoin txes:
  Currently, on master, `gettxoutsetinfo` reports a total amount of about **811 million PIV**!
  This is due to improper handling of zerocoin-related "utxos".
  Basically mints are added to the coins cache (`AddCoin` only skips OP_RETURN coins, not mints) and *never* pruned/spent (as they are skipped inside `UpdateCoins`), except in `DisconnectBlock`.

  Here we completely remove zerocoin mint outputs from the coins DB.
  This makes sense, as originally they were not meant to be linked when spending, so should have been considered unspendable outputs, and not included in the first place. With the introduction of Public Spends, mint outputs are now referenced when spending, but we don't need to keep them in the cache either, as double spending is checked only via the zerocoin serial (and, as said, the coins aren't even spent in `UpdateCoins`).
  We also remove all those utxos flagged as invalid.

  Removing zerocoin mints from the unspent coins DB also gives a nice reduction to the utxoset size on disk (about 37%, from 428 Mb to 270 Mb).

ACKs for top commit:
  Fuzzbawls:
    ACK dcc032c

Tree-SHA512: da50dd81b9e3a56291812cf35158e1858f5a0c6ebdf1e5018d1e1d20b834dbdccdaa4cb0d82830c6f0a405c71428355615fd57f582065b992d42fa9a7313235e
@Fuzzbawls Fuzzbawls removed this from the 5.0.0 milestone Dec 4, 2020
furszy added a commit that referenced this pull request Jun 28, 2021
73d7288 [Doc] Mention reindex-chainstate in the release notes (random-zebra)
c45c0ec Skip MoneySupply update during reindex/reindex-chainstate (random-zebra)
efe193f Scan for better chains in ThreadImport (random-zebra)
0aceea3 Make ProcessNewBlock dbp const and update comment (Pieter Wuille)
8255e82 [Cleanup] Remove fVerifyingBlocks global flag (random-zebra)
30c7aed Add -reindex-chainstate that does not rebuild block index (random-zebra)
79d5cb2 Verify DB with original default check-level 3 (random-zebra)
ed96859 Reduce default number of blocks to check at startup (Pieter Wuille)
d4516fa Log/report in 10% steps during VerifyDB (Jonas Schnelli)
cce2c9e doc: Mention dbcache increase in release notes (random-zebra)
11dc022 [GUI] Update default db cache size in the options model (random-zebra)
9b7f764 Bump `-dbcache` default to 300MiB (Wladimir J. van der Laan)

Pull request description:

  This was attempted already in #1864 and #1907.
  Third time's a charm.

  Now the speed is acceptable and (unlike what was happening in #1907) the process can be interrupted.
  Further, as it is performed in ThreadImport, the GUI is open and functional during the chainstate reindex, instead of being seemingly stuck at the splash screen.

  This PR also includes a few improvements coming from bitcoin#10919, bitcoin#8273, bitcoin#8136 and bitcoin#8611.

  Here's a quick comparison of the data I had testing here (empty wallet with GUI, empty pivx.conf / default values).
  The reindexes have been performed with 0 network connections.

  <table>
  <tr>
  <th>Mainnet (height=2847011)</th><th>Master</th><th>this PR</th>
  </tr><tr>
  <td>online sync</td><td>8 hr 13 min</td><td>6 hr 42 min</td>
  </tr><tr>
  <td>-reindex (offline)</td><td>7 hr 14 min</td><td>6 hr 21 min</td>
  </tr><tr>
  <td>-reindex-chainstate (offline)</td><td>N/A</td><td>6 hr 13 min</td>
  </tr><tr>
  <td>startup (synced node)</td><td>100 sec</td><td>83 sec</td>
  </tr><tr>
  <td>RAM (synced node)</td><td>1.65 GiB</td><td>1.5 GiB</td>
  </tr><tr>
  <td>utxo_cache (synced node)</td><td>28.9MiB</td><td>39.5MiB</td>
  </tr>
  </table>

ACKs for top commit:
  furszy:
    re-ACK 73d7288, tested again.

Tree-SHA512: fb828d89692ccb6105eae5411b70a3c7a129d98d3101121cf604171de1b818a8c68b3771cc69fcf0a04642fe1327ee7ce360a72ecd7ef019b1614bd83c081de5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants