Repository: Chia-Network/chia-blockchain
File: chia/_tests/blockchain/test_blockchain.py (Python)

Code before:

        first_peak = b.get_peak()
        fork_info2 = None
        # Create one AugmentedBlockchain instance and reuse it across fork chain validations
        aug_chain: AugmentedBlockchain | None = AugmentedBlockchain(b)

        for reorg_block in reorg_blocks:
            if (reorg_block.height % 100) == 0:
                peak = b.get_peak()
                assert peak is not None
                print(
                    f"reorg chain: {reorg_block.height:4} "
                    f"weight: {reorg_block.weight:7} "
                    f"peak: {str(peak.header_hash)[:6]}"
                )

            if reorg_block.height < num_blocks_chain_2_start:
                await _validate_and_add_block(
                    b, reorg_block, expected_result=AddBlockResult.ALREADY_HAVE_BLOCK, augmented_blockchain=aug_chain
                )
            else:
                if fork_info2 is None:
                    fork_info2 = ForkInfo(reorg_block.height - 1, reorg_block.height - 1, reorg_block.prev_header_hash)
                peak = b.get_peak()
                assert peak is not None
                if reorg_block.weight > peak.weight or (
                    reorg_block.weight == peak.weight and reorg_block.total_iters < peak.total_iters
                ):
                    expected_result = AddBlockResult.NEW_PEAK
                    aug_chain = None
                else:
                    expected_result = AddBlockResult.ADDED_AS_ORPHAN
                # Create fresh instance for each NEW_PEAK block like the full node
                await _validate_and_add_block(
                    b,
                    reorg_block,
                    expected_result=expected_result,
                    fork_info=fork_info2,
                    augmented_blockchain=aug_chain,
                )

        # if these asserts fires, there was no reorg
        peak = b.get_peak()
        assert peak is not None
        assert first_peak != peak
        assert peak is not None
        assert peak.weight > chain_1_weight
        second_peak = peak

        if light_blocks:
            assert peak.height > chain_1_height

Review comment (suggestion):
it makes me a bit nervous to have this be conditional. If something is changed with the chains, this test may not trigger a reorg anymore, but it will still pass. At least that's what it looks like.

I'm thinking that maybe it would be possible to fix this by adjusting `chain_1_weight` instead, to depend on which chain we're using.

Code after:
        b.clean_block_records()

        first_peak = b.get_peak()
        fork_info2 = None
        # Create one AugmentedBlockchain instance and reuse it across fork chain validations
        aug_chain: AugmentedBlockchain | None = AugmentedBlockchain(b)

        for reorg_block in reorg_blocks:
            if (reorg_block.height % 100) == 0:
                peak = b.get_peak()
                assert peak is not None
                print(
                    f"reorg chain: {reorg_block.height:4} "
                    f"weight: {reorg_block.weight:7} "
                    f"peak: {str(peak.header_hash)[:6]}"
                )

            if reorg_block.height < num_blocks_chain_2_start:
                await _validate_and_add_block(
                    b, reorg_block, expected_result=AddBlockResult.ALREADY_HAVE_BLOCK, augmented_blockchain=aug_chain
                )
            else:
                if fork_info2 is None:
                    fork_info2 = ForkInfo(reorg_block.height - 1, reorg_block.height - 1, reorg_block.prev_header_hash)
                if consensus_mode < ConsensusMode.HARD_FORK_3_0:
                    expected_result = (
                        AddBlockResult.ADDED_AS_ORPHAN
                        if reorg_block.weight <= chain_1_weight
                        else AddBlockResult.NEW_PEAK
                    )
                else:
                    peak = b.get_peak()
                    assert peak is not None
                    is_new_peak = reorg_block.weight > peak.weight or (
                        reorg_block.weight == peak.weight and reorg_block.total_iters < peak.total_iters
                    )
                    expected_result = AddBlockResult.NEW_PEAK if is_new_peak else AddBlockResult.ADDED_AS_ORPHAN
                if expected_result == AddBlockResult.NEW_PEAK:
                    aug_chain = None
                # Create fresh instance for each NEW_PEAK block like the full node
                await _validate_and_add_block(
                    b,
                    reorg_block,
                    expected_result=expected_result,
                    fork_info=fork_info2,
                    augmented_blockchain=aug_chain,
                )

        # if these asserts fires, there was no reorg
        peak = b.get_peak()
        assert peak is not None