Repository: bitcoin/bitcoin
File: src/test/fuzz/partially_downloaded_block.cpp (C++)

Code before:
    auto init_status{pdb.InitData(cmpctblock, extra_txn)};

    std::vector<CTransactionRef> missing;
    // Whether we skipped a transaction that should be included in `missing`.
    // FillBlock should never return READ_STATUS_OK if that is the case.
    bool skipped_missing{false};
    for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
        // If init_status == READ_STATUS_OK then a available transaction in the
        // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
        // that transaction as available above (i.e. available.count(i) > 0).
        // The reverse is not true, due to possible compact block short id
        // collisions (i.e. available.count(i) > 0 does not imply
        // IsTxAvailable(i) == true).
        if (init_status == READ_STATUS_OK) {
            assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
        }

        bool skip{fuzzed_data_provider.ConsumeBool()};
        if (!pdb.IsTxAvailable(i) && !skip) {
            missing.push_back(block->vtx[i]);
        }

        skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
    }

    // Mock CheckBlock
    bool fail_check_block{fuzzed_data_provider.ConsumeBool()};
    auto validation_result =
        fuzzed_data_provider.PickValueInArray(
            {BlockValidationResult::BLOCK_RESULT_UNSET,
             BlockValidationResult::BLOCK_CONSENSUS,
             BlockValidationResult::BLOCK_CACHED_INVALID,
             BlockValidationResult::BLOCK_INVALID_HEADER,
             BlockValidationResult::BLOCK_MUTATED,
             BlockValidationResult::BLOCK_MISSING_PREV,
             BlockValidationResult::BLOCK_INVALID_PREV,
             BlockValidationResult::BLOCK_TIME_FUTURE,
             BlockValidationResult::BLOCK_HEADER_LOW_WORK});
    pdb.m_check_block_mock = FuzzedCheckBlock(
        fail_check_block ?
            std::optional<BlockValidationResult>{validation_result} :
            std::nullopt);

    CBlock reconstructed_block;
    auto fill_status{pdb.FillBlock(reconstructed_block, missing)};
    switch (fill_status) {
    case READ_STATUS_OK:
        assert(!skipped_missing);
        assert(!fail_check_block);
        assert(block->GetHash() == reconstructed_block.GetHash());
        break;

Review comment (bug):
If `fail_block_mutated` is true, doesn't this mean the txid merkle commitment won't be found valid? I might be misreading the comment.

Code after:
    // FillBlock should never return READ_STATUS_OK if that is the case.
    bool skipped_missing{false};
    for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
        // If init_status == READ_STATUS_OK then a available transaction in the
        // compact block (i.e. IsTxAvailable(i) == true) implies that we marked
        // that transaction as available above (i.e. available.count(i) > 0).
        // The reverse is not true, due to possible compact block short id
        // collisions (i.e. available.count(i) > 0 does not imply
        // IsTxAvailable(i) == true).
        if (init_status == READ_STATUS_OK) {
            assert(!pdb.IsTxAvailable(i) || available.count(i) > 0);
        }

        bool skip{fuzzed_data_provider.ConsumeBool()};
        if (!pdb.IsTxAvailable(i) && !skip) {
            missing.push_back(block->vtx[i]);
        }

        skipped_missing |= (!pdb.IsTxAvailable(i) && skip);
    }

    bool segwit_active{fuzzed_data_provider.ConsumeBool()};

    // Mock IsBlockMutated
    bool fail_block_mutated{fuzzed_data_provider.ConsumeBool()};
    pdb.m_check_block_mutated_mock = FuzzedIsBlockMutated(fail_block_mutated);

    CBlock reconstructed_block;
    auto fill_status{pdb.FillBlock(reconstructed_block, missing, segwit_active)};
    switch (fill_status) {
    case READ_STATUS_OK:
        assert(!skipped_missing);
        assert(!fail_block_mutated);
        assert(block->GetHash() == reconstructed_block.GetHash());
        break;
    case READ_STATUS_FAILED:
        assert(fail_block_mutated);
        break;
    case READ_STATUS_INVALID:
        break;
    }
}