Repository: bitcoin/bitcoin
File: src/test/kernel/test_kernel.cpp (C++)

Code before:
    if (block_tree_db_in_memory) {
        chainman_opts.UpdateBlockTreeDbInMemory(block_tree_db_in_memory);
    }
    if (chainstate_db_in_memory) {
        chainman_opts.UpdateChainstateDbInMemory(chainstate_db_in_memory);
    }

    auto chainman{std::make_unique<ChainMan>(context, chainman_opts)};
    return chainman;
}

void chainman_reindex_test(TestDirectory& test_directory)
{
    auto notifications{std::make_shared<TestKernelNotifications>()};
    auto context{create_context(notifications, ChainType::MAINNET)};
    auto chainman{create_chainman(test_directory, true, false, false, false, context)};

    std::vector<std::string> import_files;
    BOOST_CHECK(chainman->ImportBlocks(import_files));

    // Sanity check some block retrievals
    auto chain{chainman->GetChain()};
    BOOST_CHECK_THROW(chain.GetByHeight(1000), std::runtime_error);
    auto genesis_index{chain.Genesis()};
    BOOST_CHECK(!genesis_index.GetPrevious());
    auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
    auto first_index{chain.GetByHeight(0)};
    auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
    check_equal(genesis_block_raw, first_block_raw);
    auto height{first_index.GetHeight()};
    BOOST_CHECK_EQUAL(height, 0);

    auto next_index{chain.GetByHeight(first_index.GetHeight() + 1)};
    BOOST_CHECK(chain.Contains(next_index));
    auto next_block_data{chainman->ReadBlock(next_index).value().ToBytes()};
    auto tip_index{chain.Tip()};
    auto tip_block_data{chainman->ReadBlock(tip_index).value().ToBytes()};
    auto second_index{chain.GetByHeight(1)};
    auto second_block{chainman->ReadBlock(second_index).value()};
    auto second_block_data{second_block.ToBytes()};
    auto second_height{second_index.GetHeight()};
    BOOST_CHECK_EQUAL(second_height, 1);
    check_equal(next_block_data, tip_block_data);
    check_equal(next_block_data, second_block_data);

    auto second_hash{second_index.GetHash()};
    auto another_second_index{chainman->GetBlockTreeEntry(second_hash)};
    BOOST_CHECK(another_second_index);
    auto another_second_height{another_second_index->GetHeight()};
    auto second_block_hash{second_block.GetHash()};
    check_equal(second_block_hash.ToBytes(), second_hash.ToBytes());

Review comment (suggestion):
```C
    auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
    auto first_index{chain.GetByHeight(0)};
    auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
```

Not sure if this part of the test still makes sense, the double retrieval of the data of block 0 `auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};` 
we are now testing if `chainman->ReadBlock` will retrieve the same block twice not if it will get the expected block.

We test on line 761 we test if `readblock` retrieves the correct block. 

Code after:
    if (block_tree_db_in_memory) {
        chainman_opts.UpdateBlockTreeDbInMemory(block_tree_db_in_memory);
    }
    if (chainstate_db_in_memory) {
        chainman_opts.UpdateChainstateDbInMemory(chainstate_db_in_memory);
    }

    auto chainman{std::make_unique<ChainMan>(context, chainman_opts)};
    return chainman;
}

void chainman_reindex_test(TestDirectory& test_directory)
{
    auto notifications{std::make_shared<TestKernelNotifications>()};
    auto context{create_context(notifications, ChainType::MAINNET)};
    auto chainman{create_chainman(test_directory, true, false, false, false, context)};

    std::vector<std::string> import_files;
    BOOST_CHECK(chainman->ImportBlocks(import_files));

    // Sanity check some block retrievals
    auto chain{chainman->GetChain()};
    BOOST_CHECK_THROW(chain.GetByHeight(1000), std::runtime_error);
    auto genesis_index{chain.Entries().front()};
    BOOST_CHECK(!genesis_index.GetPrevious());
    auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
    auto first_index{chain.GetByHeight(0)};
    auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
    check_equal(genesis_block_raw, first_block_raw);
    auto height{first_index.GetHeight()};
    BOOST_CHECK_EQUAL(height, 0);

    auto next_index{chain.GetByHeight(first_index.GetHeight() + 1)};
    BOOST_CHECK(chain.Contains(next_index));
    auto next_block_data{chainman->ReadBlock(next_index).value().ToBytes()};
    auto tip_index{chain.Entries().back()};
    auto tip_block_data{chainman->ReadBlock(tip_index).value().ToBytes()};
    auto second_index{chain.GetByHeight(1)};
    auto second_block{chainman->ReadBlock(second_index).value()};
    auto second_block_data{second_block.ToBytes()};
    auto second_height{second_index.GetHeight()};
    BOOST_CHECK_EQUAL(second_height, 1);
    check_equal(next_block_data, tip_block_data);
    check_equal(next_block_data, second_block_data);

    auto second_hash{second_index.GetHash()};
    auto another_second_index{chainman->GetBlockTreeEntry(second_hash)};
    BOOST_CHECK(another_second_index);
    auto another_second_height{another_second_index->GetHeight()};
    auto second_block_hash{second_block.GetHash()};
    check_equal(second_block_hash.ToBytes(), second_hash.ToBytes());