Repository: Chia-Network/chia-blockchain
File: chia/_tests/wallet/sync/test_wallet_sync.py (Python)

Code before:
            for coin in coin_list:
                assert coin.puzzle_hash == p
            # The other ones are included
            assert len(coin_list) == 2

    # None for puzzle hashes returns all coins and no proofs
    res3 = await full_node_api.request_additions(RequestAdditions(last_block.height, last_block.header_hash, None))

    assert res3 is not None
    response = RespondAdditions.from_bytes(res3.data)
    assert response.height == last_block.height
    assert response.header_hash == last_block.header_hash
    assert response.proofs is None
    assert len(response.coins) == 12
    assert sum(len(c_list) for _, c_list in response.coins) == 24

    # [] for puzzle hashes returns nothing
    res4 = await full_node_api.request_additions(RequestAdditions(last_block.height, last_block.header_hash, []))
    assert res4 is not None
    response = RespondAdditions.from_bytes(res4.data)
    assert response.proofs == []
    assert len(response.coins) == 0


@pytest.mark.anyio
async def test_request_removals_too_many_coin_names(
    simulator_and_wallet: OldSimulatorsAndWallets, self_hostname: str
) -> None:
    full_nodes, wallets, _ = simulator_and_wallet
    wallet_node, wallet_server = wallets[0]
    wallet = wallet_node.wallet_state_manager.main_wallet
    async with wallet.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
        ph = await action_scope.get_puzzle_hash(wallet.wallet_state_manager)

    full_node_api = full_nodes[0]
    await wallet_server.start_client(PeerInfo(self_hostname, full_node_api.full_node.server.get_port()), None)

    for _ in range(2):
        await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(ph))

    await full_node_api.wait_for_wallet_synced(wallet_node=wallet_node, timeout=20)

    last_block = full_node_api.full_node.blockchain.get_peak()
    assert last_block is not None

    too_many = [std_hash(i.to_bytes(4, "big")) for i in range(MAX_COIN_HASHES_PER_REQUEST + 1)]
    res = await full_node_api.request_removals(RequestRemovals(last_block.height, last_block.header_hash, too_many))
    assert res is not None
    reject = RejectRemovalsRequest.from_bytes(res.data)
    assert reject.height == last_block.height
    assert reject.header_hash == last_block.header_hash

Review comment (suggestion):
We can write `test_request_additions_errors` and `test_request_removals_too_many_coin_names` to become significantly simpler/faster using something like `one_node_one_block`.

Code after:
            for coin in coin_list:
                assert coin.puzzle_hash == p
            # The other ones are included
            assert len(coin_list) == 2

    # None for puzzle hashes returns all coins and no proofs
    res3 = await full_node_api.request_additions(RequestAdditions(last_block.height, last_block.header_hash, None))

    assert res3 is not None
    response = RespondAdditions.from_bytes(res3.data)
    assert response.height == last_block.height
    assert response.header_hash == last_block.header_hash
    assert response.proofs is None
    assert len(response.coins) == 12
    assert sum(len(c_list) for _, c_list in response.coins) == 24

    # [] for puzzle hashes returns nothing
    res4 = await full_node_api.request_additions(RequestAdditions(last_block.height, last_block.header_hash, []))
    assert res4 is not None
    response = RespondAdditions.from_bytes(res4.data)
    assert response.proofs == []
    assert len(response.coins) == 0


@pytest.mark.anyio
async def test_request_removals_too_many_coin_names(
    simulator_and_wallet: OldSimulatorsAndWallets, self_hostname: str
) -> None:
    full_nodes, wallets, _ = simulator_and_wallet
    wallet_node, wallet_server = wallets[0]
    wallet = wallet_node.wallet_state_manager.main_wallet
    async with wallet.wallet_state_manager.new_action_scope(DEFAULT_TX_CONFIG, push=True) as action_scope:
        ph = await action_scope.get_puzzle_hash(wallet.wallet_state_manager)

    full_node_api = full_nodes[0]
    await wallet_server.start_client(PeerInfo(self_hostname, full_node_api.full_node.server.get_port()), None)

    for _ in range(2):
        await full_node_api.farm_new_transaction_block(FarmNewBlockProtocol(ph))

    await full_node_api.wait_for_wallet_synced(wallet_node=wallet_node, timeout=20)

    last_block = full_node_api.full_node.blockchain.get_peak()
    assert last_block is not None

    too_many = [bytes32.random() for _ in range(MAX_COIN_HASHES_PER_REQUEST + 1)]
    res = await full_node_api.request_removals(RequestRemovals(last_block.height, last_block.header_hash, too_many))
    assert res is not None
    reject = RejectRemovalsRequest.from_bytes(res.data)
    assert reject.height == last_block.height
    assert reject.header_hash == last_block.header_hash