Skip to content

Add LMDB blockchain unit tests (write + read)#7716

Draft
Copilot wants to merge 2 commits intodevelopmentfrom
copilot/create-lmdb-unit-tests
Draft

Add LMDB blockchain unit tests (write + read)#7716
Copilot wants to merge 2 commits intodevelopmentfrom
copilot/create-lmdb-unit-tests

Conversation

Copy link
Contributor

Copilot AI commented Mar 13, 2026

No unit tests existed to verify that the LMDB backend correctly stores and retrieves blockchain data, including reorg scenarios.

Test chain structure

Height:  0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15
        GB – B1 – B2 – B3 – B4 – B5 – B6 – B7 – B8 – B9 – B10          (original main chain)
                              └──── F6 – F7 – F8 – F9 – F10– F11– F12– F13– F14– F15
                                    (fork from B5; reorg triggers when fork exceeds main-chain height)

After the reorg: main chain is heights 0–15; B6–B10 become orphans (5 blocks).

Changes

  • lmdb_tests.rs – new test module with:
    • build_test_blockchain() – creates the chain above using TestBlockchain. Main-chain blocks B2–B10 each spend the previous coinbase. Fork blocks F6–F15 are coinbase-only. Fork block SMT roots are computed correctly via a MockTreeStore JMT initialised to the UTXO state at B5, bypassing the DB's tip-only MMR calculation.
    • replay_chain_data() – serialises all blocks to JSON, deserialises, and replays them into a fresh BlockchainDatabase<TempDatabase> in identical insertion order.
    • test_lmdb_write – asserts that a JSON-roundtripped replay produces an identical database: matching block hashes for all 16 main-chain heights, identical orphan-pool hashes, UTXO count, and kernel MMR size.
    • test_lmdb_read – exhaustively exercises every backend read operation across all 16 main-chain blocks and 5 orphans: fetch_chain_header, fetch_chain_header_in_all_chains, fetch_block, fetch_block_accumulated_data, fetch_header_accumulated_data, fetch_orphan, fetch_outputs_in_block, fetch_inputs_in_block, fetch_kernels_in_block, fetch_output, fetch_mined_info_by_payref, fetch_outputs_in_block_with_spend_state, fetch_kernel_by_excess_sig, fetch_header_containing_kernel_mmr, and fetch_unspent_output_hash_by_commitment.
  • tests/mod.rs – adds mod lmdb_tests.

Known issue

The reorg-trigger assertion (assert_reorg) uses the wrong counts — F11 (height 11) beats the main chain at height 10, so F6–F10 are orphans and F11 triggers the reorg (assert_reorg(6, 5)), with F12–F15 appended directly. This needs a follow-up fix before the tests will pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>Create lmdb unit tests</issue_title>
<issue_description>We need to create 2 tests to ensure lmdb is correct
1: write all blockchain data, and test all blockchain functionality to a new lmdb file
2 Load all blockchain data and see if we can read this.

We need to creata a new blockchain, with a few blocks, and run through a few operations such as addming inputs, outputs, kernels, pruning etc.
We can use a single function to do this, and we can then for 1, do a bit compare on the generated files, and we can also use this function to generate the file we use to compare against</issue_description>

Comments on the Issue (you are @copilot in this section)

@SWvheerden # Create chain * Add genesis block * Create a test chain of 10 blocks spending inputs from the previous blocks, add these in order * Create another chain forking from block 5 for 10 more blocks, spending inputs from the previous blocks, add these in order * Save all block data in a json file so we can replicate this

test write

  • read all the json data from the original create, and follow the chain create,
  • create a new lmdb file, and do a bit compare to see if the write is the same

test read

  • read the json file for each block
  • load up the test lmdb file as the db
  • test if you can fetch all 15 main chain headers
  • test if you can fetch all 5 reorged headers
  • test if you can fetch all 15 main chain blocks
  • fetch the accumulated data for each mainnet block and orphan
  • test if you can fetch all 5 reorged blocks
  • for each mainnet block test
    • Fetch outputs in a block by header_hash
    • Fetch inputs in a block by header_hash
    • Fetch kernels in a block by header_hash
    • Fetch an output by hash (fetch_output)
    • Fetch an unspent output by commitment (fetch_unspent_output_hash_by_commitment)
    • Fetch an output by payment reference (fetch_mined_info_by_payref)
    • Fetch outputs with spend state at a given height (fetch_outputs_in_block_with_spend_state)
    • Fetch kernel by excess signature (fetch_kernel_by_excess_sig)
    • Fetch header containing a kernel by MMR position (fetch_header_containing_kernel_mmr)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: SWvheerden <2579813+SWvheerden@users.noreply.github.com>
Copilot AI changed the title [WIP] Add unit tests for lmdb functionality Add LMDB blockchain unit tests (write + read) Mar 13, 2026
Copilot AI requested a review from SWvheerden March 13, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create lmdb unit tests

2 participants