def test_channel_settle_old_balance_proof_values(
    get_accounts,
    assign_tokens,
    channel_test_values,
    create_channel_and_deposit,
    test_settlement_outcome,
    tested_range,
):
    """ Test the settlement implementation when both/one of the balance proofs are outdated """
    (A, B, C, D) = get_accounts(4)
    (vals_A0, vals_B0) = channel_test_values["valid_last"]
    assert are_balance_proofs_valid(vals_A0, vals_B0)
    assert not is_balance_proof_old(vals_A0, vals_B0)

    # Mint additional tokens for participants
    assign_tokens(A, 400)
    assign_tokens(B, 200)
    # We make sure the contract has more tokens than A, B will deposit
    create_channel_and_deposit(C, D, 40, 60)

    # Calculate the final expected balances after the channel lifecycle, (after settlement and
    # unlocks), when we know how the locked amounts will be distributed.
    # This is a very important check. This must be true for the last known balance proofs,
    # but also for a last known balance proof + an old balance proof provided on purpose or not.
    # Where a valid balance proof is a balance proof that respects the Raiden client
    # value contraints, as defined here:
    # https://github.com/raiden-network/raiden-contracts/issues/188#issuecomment-404752095
    (
        expected_final_balance_A0,
        expected_final_balance_B0,
    ) = get_expected_after_settlement_unlock_amounts(vals_A0, vals_B0)

    if tested_range == "one_old":

        test_settlement_outcome(
            (A, B),
            (vals_A0, vals_B0, "valid"),
            expected_final_balance_A0,
            expected_final_balance_B0,
        )

        if "old_last" in channel_test_values:
            for vals_A in channel_test_values["old_last"]:
                vals_B = vals_B0
                test_settlement_outcome(
                    (A, B),
                    (vals_A, vals_B, "old_last"),
                    expected_final_balance_A0,
                    expected_final_balance_B0,
                )

        if "last_old" in channel_test_values:
            for vals_B in channel_test_values["last_old"]:
                vals_A = vals_A0
                test_settlement_outcome(
                    (A, B),
                    (vals_A, vals_B, "last_old"),
                    expected_final_balance_A0,
                    expected_final_balance_B0,
                )

    elif tested_range == "both_old_1":

        if "old_last" in channel_test_values and "last_old" in channel_test_values:
            for vals_A in channel_test_values["old_last"][:5]:
                for vals_B in channel_test_values["last_old"][:5]:
                    # We only need to test for cases  where the we have the same argument ordering
                    # for settleChannel, keeping the order of balance calculations
                    B_total = vals_B.transferred + vals_B.locked_amounts.locked
                    A_total = vals_A.transferred + vals_A.locked_amounts.locked
                    if B_total >= A_total:
                        test_settlement_outcome(
                            (A, B),
                            (vals_A, vals_B, "invalid"),
                            expected_final_balance_A0,
                            expected_final_balance_B0,
                        )

    else:

        if "old_last" in channel_test_values and "last_old" in channel_test_values:
            for vals_A in channel_test_values["old_last"][5:]:
                for vals_B in channel_test_values["last_old"][5:]:
                    # We only need to test for cases  where the we have the same argument ordering
                    # for settleChannel, keeping the order of balance calculations
                    B_total = vals_B.transferred + vals_B.locked_amounts.locked
                    A_total = vals_A.transferred + vals_A.locked_amounts.locked
                    if B_total >= A_total:
                        test_settlement_outcome(
                            (A, B),
                            (vals_A, vals_B, "invalid"),
                            expected_final_balance_A0,
                            expected_final_balance_B0,
                        )
Beispiel #2
0
def test_channel_settle_old_balance_proof_values(
    web3,
    get_accounts,
    assign_tokens,
    channel_test_values,
    create_channel_and_deposit,
    test_settlement_outcome,
    tested_range,
):
    """ Test the settlement implementation when both/one of the balance proofs are outdated """
    (A, B, C, D) = get_accounts(4)
    (vals_A0, vals_B0) = channel_test_values['valid_last']
    assert are_balance_proofs_valid(vals_A0, vals_B0)
    assert not is_balance_proof_old(vals_A0, vals_B0)

    # Mint additional tokens for participants
    assign_tokens(A, 400)
    assign_tokens(B, 200)
    # We make sure the contract has more tokens than A, B will deposit
    create_channel_and_deposit(C, D, 40, 60)

    # Calculate how much A and B should receive while not knowing who will receive the
    # locked amounts
    expected_settlement0 = get_settlement_amounts(vals_A0, vals_B0)
    # Calculate how much A and B receive according to onchain computation
    expected_settlement_onchain0 = get_onchain_settlement_amounts(
        vals_A0, vals_B0)

    # Calculate the final expected balances after the channel lifecycle, (after settlement and
    # unlocks), when we know how the locked amounts will be distributed.
    # This is a very important check. This must be true for the last known balance proofs,
    # but also for a last known balance proof + an old balance proof provided on purpose or not.
    # Where a valid balance proof is a balance proof that respects the Raiden client
    # value contraints, as defined here:
    # https://github.com/raiden-network/raiden-contracts/issues/188#issuecomment-404752095
    (
        expected_final_balance_A0,
        expected_final_balance_B0,
    ) = get_expected_after_settlement_unlock_amounts(vals_A0, vals_B0)

    if tested_range is 'one_old':

        test_settlement_outcome(
            (A, B),
            (vals_A0, vals_B0, 'valid'),
            expected_settlement0,
            expected_settlement_onchain0,
            expected_final_balance_A0,
            expected_final_balance_B0,
        )

        if 'old_last' in channel_test_values:
            for vals_A in channel_test_values['old_last']:
                vals_B = vals_B0
                test_settlement_outcome(
                    (A, B),
                    (vals_A, vals_B, 'old_last'),
                    expected_settlement0,
                    expected_settlement_onchain0,
                    expected_final_balance_A0,
                    expected_final_balance_B0,
                )

        if 'last_old' in channel_test_values:
            for vals_B in channel_test_values['last_old']:
                vals_A = vals_A0
                test_settlement_outcome(
                    (A, B),
                    (vals_A, vals_B, 'last_old'),
                    expected_settlement0,
                    expected_settlement_onchain0,
                    expected_final_balance_A0,
                    expected_final_balance_B0,
                )

    elif tested_range is 'both_old_1':

        if 'old_last' in channel_test_values and 'last_old' in channel_test_values:
            for vals_A in channel_test_values['old_last'][:5]:
                for vals_B in channel_test_values['last_old'][:5]:
                    # We only need to test for cases  where the we have the same argument ordering
                    # for settleChannel, keeping the order of balance calculations
                    if vals_B.transferred + vals_B.locked >= vals_A.transferred + vals_A.locked:
                        test_settlement_outcome(
                            (A, B),
                            (vals_A, vals_B, 'invalid'),
                            expected_settlement0,
                            expected_settlement_onchain0,
                            expected_final_balance_A0,
                            expected_final_balance_B0,
                        )

    else:

        if 'old_last' in channel_test_values and 'last_old' in channel_test_values:
            for vals_A in channel_test_values['old_last'][5:]:
                for vals_B in channel_test_values['last_old'][5:]:
                    # We only need to test for cases  where the we have the same argument ordering
                    # for settleChannel, keeping the order of balance calculations
                    if vals_B.transferred + vals_B.locked >= vals_A.transferred + vals_A.locked:
                        test_settlement_outcome(
                            (A, B),
                            (vals_A, vals_B, 'invalid'),
                            expected_settlement0,
                            expected_settlement_onchain0,
                            expected_final_balance_A0,
                            expected_final_balance_B0,
                        )