Calculate fees in getblock using BlockUndo data (rpc/rest/zmq)

https://github.com/bitcoin/bitcoin/pulls/18772

Host: robot-dreams  -  PR author: robot-visions

Notes

  • A transaction’s fee is not set explicitly. The fee can be implicitly calculated from the difference between total input value and the total output value.

  • This PR updates the getblock RPC to include an extra “fee” field for transactions whenever:

    • verbosity >= 2
    • Undo data (which can be used for efficient fee calculation) exists for that block
  • We discussed block undo data and how it’s stored on disk in a previous review club meeting.

  • Some extra changes were added to prevent fuzz test failures; we discussed fuzz testing and how it’s used in Bitcoin Core in a previous review club meeting.

Questions

  1. What is CTxUndo?

    • How are we using it for fee calculation?
    • Where else is it used?
    • Why doesn’t a coinbase transaction have Undo data?
  2. What’s the IO cost to read Undo data for a block?

  3. How could we calculate the fee if Undo data didn’t exist for a block, and what’s the IO cost?

    • If there’s a txindex?
    • If there’s no txindex?
  4. What does CHECK_NONFATAL do?

    • What other options are there (e.g. asserts), and when should we use each option?
  5. Knowing the transaction fee is useful, but does only returning it in some circumstances actually provide a better client experience, or is it preferable for clients to always get exactly what they expect?

    • Should we also calculate the fee if Undo data is missing but a txindex is available?
  6. At the moment, the fact that we might return “fee” is undocumented. Is it worth increasing the complexity of the documentation to say “we SOMETIMES return the fee; here’s when we do so”?

  7. We add some extra checks to prevent integer overflow in fuzz tests. But does it make sense for fuzz tests to generate inputs that violate function preconditions?