Exercises

  1. What is the difference between contextual and context-free validation checks?

    Click for answer

    Contextual checks require some knowledge of the current "state", e.g. ChainState, chain tip or UTXO set.

    Context-free checks only require the information required in the transaction itself.

    For more info, see glozow’s notes on transaction "Validation and Submission to the Mempool".

  2. What are some examples of each?

    Click for answer
  3. In which function(s) do UTXO-related validity checks happen?

    Click for answer

    ConnectBlock()

  4. What type of validation checks are CheckBlockHeader() and CheckBlock() performing?

    Click for answer

    context-free

  5. Which class is in charge of managing the current blockchain?

    Click for answer

    ChainstateManager()

  6. Which class is in charge of managing the UTXO set?

    Click for answer

    CCoinsViews()

  7. Which functions are called when a longer chain is found that we need to re-org onto?

    TODO

  8. Are there any areas of the codebase where the same consensus or validation checks are performed twice?

    Click for answer

    Again see glozow’s notes for examples

  9. Why does CheckInputsFromMempoolAndCache exist?

    Click for answer

    To prevent us from re-checking the scripts of transactions already in our mempool during consensus validation on learning about a new block

  10. Which function(s) are in charge of validating the merkle root of a block?

    Click for answer

    BlockMerkleRoot() and BlockWitnessMerkleRoot() construct a vector of merkle leaves, which is then passed to ComputeMerkleRoot() for calculation.

  11. Can you find any evidence (e.g. PRs) which have been made in an effort to modularize consensus code?

    Click for answer

    A few examples: PR#10279, PR#20158

  12. What is the function of BlockManager()?

    Click for answer

    It manages the current most-work chaintip and pruning of unneeded blocks (*.blk) and associated undo (*.rev) files

  13. What stops a malicious node from sending multiple invalid headers to try and use up a nodes' disk space? (hint: these might be stored in BlockManager.m_failed_blocks)

    Click for answer

    Even invalid headers would need a valid proof of work which would be too costly to construct for a spammer

  14. Which functions are responsible for writing consensus-valid blocks to disk?

    Click for answer

    src/node/blockstorage.h#SaveBlockToDisk

  15. Are there any other components to Bitcoin Core which, similarly to the block storage database, are not themselves performing validation but can still be consensus-critical?

    Not sure myself, sounds like an interesting question though!

  16. In which module (and class) is signature verification handled?

    Click for answer

    src/script/interpreter.cpp#BaseSignatureChecker

  17. Which function is used to calculate the Merkle root of a block, and from where is it called?

    Click for answer

    src/consensus/merkle.cpp#ComputeMerkleRoot is used to compute the merkle root.

    It is called from src/chainparams.cpp#CreateGenesisBlock, src/miner.cpp#IncrementExtraNonce & src/miner.cpp#RegenerateCommitments and from src/validation.cpp#CheckBlock to validate incoming blocks.

  18. Practical question on Merkle root calculation

    TODO, add more Exercises

    • Modify the code which is used to add new opcodes to a CScript without breaking consensus.