コード例 #1
0
    def get(A, values_A, B, values_B, pre_account_balance_A,
            pre_account_balance_B, pre_balance_contract):
        # Calculate how much A and B should receive
        settlement = get_settlement_amounts(values_A, values_B)
        # Calculate how much A and B receive according to onchain computation
        on_chain_settlement = get_onchain_settlement_amounts(
            values_A, values_B)

        common_settle_state_tests(A, settlement.participant1_balance, B,
                                  settlement.participant2_balance,
                                  pre_account_balance_A, pre_account_balance_B,
                                  pre_balance_contract)
        common_settle_state_tests(A, on_chain_settlement.participant1_balance,
                                  B, on_chain_settlement.participant2_balance,
                                  pre_account_balance_A, pre_account_balance_B,
                                  pre_balance_contract)

        locked_amount1 = token_network.functions.getParticipantLockedAmount(
            A, B, values_A.locksroot).call()
        assert locked_amount1 == settlement.participant1_locked
        assert locked_amount1 == on_chain_settlement.participant1_locked

        locked_amount2 = token_network.functions.getParticipantLockedAmount(
            B, A, values_B.locksroot).call()
        assert locked_amount2 == settlement.participant2_locked
        assert locked_amount2 == on_chain_settlement.participant2_locked
コード例 #2
0
def test_settle_single_direct_transfer_for_closing_party(
    web3,
    get_accounts,
    custom_token,
    token_network,
    create_channel,
    channel_deposit,
    create_balance_proof,
):
    """ Test settle of a channel with one direct transfer to the participant
    that called close.
    """
    (A, B) = get_accounts(2)
    (vals_A, vals_B) = (
        ChannelValues(deposit=1, withdrawn=0, transferred=0),
        ChannelValues(deposit=10, withdrawn=0, transferred=5),
    )
    settle_timeout = TEST_SETTLE_TIMEOUT_MIN

    channel_identifier = create_channel(A, B)[0]
    channel_deposit(channel_identifier, A, vals_A.deposit, B)
    channel_deposit(channel_identifier, B, vals_B.deposit, A)

    balance_proof_B = create_balance_proof(channel_identifier, B,
                                           vals_B.transferred,
                                           vals_B.locked_amounts.locked, 1,
                                           EMPTY_LOCKSROOT)
    token_network.functions.closeChannel(
        channel_identifier, B, *balance_proof_B).call_and_transact({"from": A})

    pre_balance_A = custom_token.functions.balanceOf(A).call()
    pre_balance_B = custom_token.functions.balanceOf(B).call()
    pre_balance_contract = custom_token.functions.balanceOf(
        token_network.address).call()

    web3.testing.mine(settle_timeout + 1)
    token_network.functions.settleChannel(
        channel_identifier=channel_identifier,
        participant1=A,
        participant1_transferred_amount=0,
        participant1_locked_amount=0,
        participant1_locksroot=EMPTY_LOCKSROOT,
        participant2=B,
        participant2_transferred_amount=vals_B.transferred,
        participant2_locked_amount=0,
        participant2_locksroot=EMPTY_LOCKSROOT,
    ).call_and_transact({"from": A})

    # Calculate how much A and B should receive
    expected_settlement = get_settlement_amounts(vals_A, vals_B)
    # Calculate how much A and B receive according to onchain computation
    onchain_settlement = get_onchain_settlement_amounts(vals_A, vals_B)

    assert expected_settlement.participant1_balance == onchain_settlement.participant1_balance
    assert expected_settlement.participant2_balance == onchain_settlement.participant2_balance
    assert custom_token.functions.balanceOf(A).call() == pre_balance_A + 6
    assert custom_token.functions.balanceOf(B).call() == pre_balance_B + 5
    assert (custom_token.functions.balanceOf(
        token_network.address).call() == pre_balance_contract - 11)
コード例 #3
0
    def get(channel_identifier, A, values_A, B, values_B,
            pre_account_balance_A, pre_account_balance_B,
            pre_balance_contract):
        # Calculate how much A and B should receive
        settlement = get_settlement_amounts(values_A, values_B)
        # Calculate how much A and B receive according to onchain computation
        on_chain_settlement = get_onchain_settlement_amounts(
            values_A, values_B)

        common_settle_state_tests(custom_token, token_network,
                                  channel_identifier, A,
                                  settlement.participant1_balance, B,
                                  settlement.participant2_balance,
                                  pre_account_balance_A, pre_account_balance_B,
                                  pre_balance_contract)
        common_settle_state_tests(custom_token, token_network,
                                  channel_identifier, A,
                                  on_chain_settlement.participant1_balance, B,
                                  on_chain_settlement.participant2_balance,
                                  pre_account_balance_A, pre_account_balance_B,
                                  pre_balance_contract)

        info_A = token_network.functions.getChannelParticipantInfo(
            channel_identifier, A, B).call()
        (_, _, _, _, _, locksroot_A, locked_amount_A) = info_A
        assert locked_amount_A == settlement.participant1_locked
        assert locked_amount_A == on_chain_settlement.participant1_locked
        if locked_amount_A > 0:
            assert locksroot_A == values_A.locksroot

        info_B = token_network.functions.getChannelParticipantInfo(
            channel_identifier, B, A).call()
        (_, _, _, _, _, locksroot_B, locked_amount_B) = info_B
        assert locked_amount_B == settlement.participant2_locked
        assert locked_amount_B == on_chain_settlement.participant2_locked
        if locked_amount_B > 0:
            assert locksroot_B == values_B.locksroot

        (
            settle_block_number,
            state,
        ) = token_network.functions.getChannelInfo(channel_identifier, A,
                                                   B).call()
        assert settle_block_number == 0
        if locked_amount_A > 0 or locked_amount_B > 0:
            assert state == ChannelState.SETTLED
        else:
            assert state == ChannelState.REMOVED
コード例 #4
0
def test_settle_single_direct_transfer_for_counterparty(
    web3: Web3,
    get_accounts: Callable,
    custom_token: Contract,
    token_network: Contract,
    create_channel: Callable,
    channel_deposit: Callable,
    create_balance_proof: Callable,
    create_balance_proof_countersignature: Callable,
    create_close_signature_for_no_balance_proof: Callable,
) -> None:
    """Test settle of a channel with one direct transfer to the participant
    that did not call close.
    """
    (A, B) = get_accounts(2)
    (vals_A, vals_B) = (
        ChannelValues(deposit=10, withdrawn=0, transferred=5),
        ChannelValues(deposit=1, withdrawn=0, transferred=0),
    )
    settle_timeout = TEST_SETTLE_TIMEOUT_MIN

    channel_identifier = create_channel(A, B)[0]
    channel_deposit(channel_identifier, A, vals_A.deposit, B)
    channel_deposit(channel_identifier, B, vals_B.deposit, A)
    closing_sig = create_close_signature_for_no_balance_proof(
        A, channel_identifier)
    call_and_transact(
        token_network.functions.closeChannel(
            channel_identifier,
            B,
            A,
            EMPTY_BALANCE_HASH,
            0,
            EMPTY_ADDITIONAL_HASH,
            EMPTY_SIGNATURE,
            closing_sig,
        ),
        {"from": A},
    )

    balance_proof_A = create_balance_proof(
        channel_identifier,
        A,
        vals_A.transferred,
        vals_A.locked_amounts.locked,
        1,
        LOCKSROOT_OF_NO_LOCKS,
    )

    balance_proof_update_signature_B = create_balance_proof_countersignature(
        participant=B,
        channel_identifier=channel_identifier,
        msg_type=MessageTypeId.BALANCE_PROOF_UPDATE,
        **balance_proof_A._asdict(),
    )
    call_and_transact(
        token_network.functions.updateNonClosingBalanceProof(
            channel_identifier,
            A,
            B,
            *balance_proof_A._asdict().values(),
            balance_proof_update_signature_B,
        ),
        {"from": B},
    )

    pre_balance_A = custom_token.functions.balanceOf(A).call()
    pre_balance_B = custom_token.functions.balanceOf(B).call()
    pre_balance_contract = custom_token.functions.balanceOf(
        token_network.address).call()

    mine_blocks(web3, settle_timeout + 1)
    call_and_transact(
        token_network.functions.settleChannel(
            channel_identifier=channel_identifier,
            participant1=B,
            participant1_transferred_amount=0,
            participant1_locked_amount=0,
            participant1_locksroot=LOCKSROOT_OF_NO_LOCKS,
            participant2=A,
            participant2_transferred_amount=vals_A.transferred,
            participant2_locked_amount=0,
            participant2_locksroot=LOCKSROOT_OF_NO_LOCKS,
        ),
        {"from": B},
    )

    # Calculate how much A and B should receive
    expected_settlement = get_settlement_amounts(vals_B, vals_A)
    # Calculate how much A and B receive according to onchain computation
    onchain_settlement = get_onchain_settlement_amounts(vals_B, vals_A)

    assert expected_settlement.participant1_balance == onchain_settlement.participant1_balance
    assert expected_settlement.participant2_balance == onchain_settlement.participant2_balance
    assert custom_token.functions.balanceOf(A).call() == pre_balance_A + 5
    assert custom_token.functions.balanceOf(B).call() == pre_balance_B + 6
    assert (custom_token.functions.balanceOf(
        token_network.address).call() == pre_balance_contract - 11)
コード例 #5
0
def test_settle_single_direct_transfer_for_counterparty(
    web3,
    get_accounts,
    custom_token,
    token_network,
    create_channel,
    channel_deposit,
    create_balance_proof,
    create_balance_proof_update_signature,
):
    """ Test settle of a channel with one direct transfer to the participant
    that did not call close.
    """
    (A, B) = get_accounts(2)
    (vals_A, vals_B) = (
        ChannelValues(deposit=10, withdrawn=0, transferred=5, locked=0),
        ChannelValues(deposit=1, withdrawn=0, transferred=0, locked=0),
    )
    settle_timeout = TEST_SETTLE_TIMEOUT_MIN

    channel_identifier = create_channel(A, B)[0]
    channel_deposit(channel_identifier, A, vals_A.deposit, B)
    channel_deposit(channel_identifier, B, vals_B.deposit, A)
    token_network.functions.closeChannel(
        channel_identifier,
        B,
        EMPTY_LOCKSROOT,
        0,
        EMPTY_ADDITIONAL_HASH,
        EMPTY_SIGNATURE,
    ).transact({'from': A})

    balance_proof_A = create_balance_proof(
        channel_identifier,
        A,
        vals_A.transferred,
        vals_A.locked,
        1,
        EMPTY_LOCKSROOT,
    )

    balance_proof_update_signature_B = create_balance_proof_update_signature(
        B,
        channel_identifier,
        *balance_proof_A,
    )
    token_network.functions.updateNonClosingBalanceProof(
        channel_identifier,
        A,
        B,
        *balance_proof_A,
        balance_proof_update_signature_B,
    ).transact({'from': B})

    pre_balance_A = custom_token.functions.balanceOf(A).call()
    pre_balance_B = custom_token.functions.balanceOf(B).call()
    pre_balance_contract = custom_token.functions.balanceOf(
        token_network.address).call()

    web3.testing.mine(settle_timeout)
    token_network.functions.settleChannel(
        channel_identifier,
        B,
        0,
        0,
        EMPTY_LOCKSROOT,
        A,
        vals_A.transferred,
        0,
        EMPTY_LOCKSROOT,
    ).transact({'from': B})

    # Calculate how much A and B should receive
    expected_settlement = get_settlement_amounts(vals_B, vals_A)
    # Calculate how much A and B receive according to onchain computation
    onchain_settlement = get_onchain_settlement_amounts(vals_B, vals_A)

    assert (expected_settlement.participant1_balance ==
            onchain_settlement.participant1_balance)
    assert (expected_settlement.participant2_balance ==
            onchain_settlement.participant2_balance)
    assert custom_token.functions.balanceOf(A).call() == pre_balance_A + 5
    assert custom_token.functions.balanceOf(B).call() == pre_balance_B + 6
    assert custom_token.functions.balanceOf(
        token_network.address, ).call() == pre_balance_contract - 11
コード例 #6
0
    def f(participants, channel_values, expected_final_balance_A0,
          expected_final_balance_B0):
        (A, B) = participants
        (vals_A, vals_B, balance_proof_type) = channel_values
        assert were_balance_proofs_valid(vals_A, vals_B)

        # Start channel lifecycle
        channel_identifier = create_channel_and_deposit(
            A, B, vals_A.deposit, vals_B.deposit)
        withdraw_channel(channel_identifier, A, vals_A.withdrawn, B)
        withdraw_channel(channel_identifier, B, vals_B.withdrawn, A)

        # For the purpose of this test, it is not important when the secrets are revealed,
        # as long as the secrets connected to pending transfers that should be finalized,
        # are revealed before their expiration.

        # Mock pending transfers data for A -> B
        pending_transfers_tree_A = get_pending_transfers_tree(
            web3,
            unlockable_amount=vals_A.locked_amounts.claimable_locked,
            expired_amount=vals_A.locked_amounts.unclaimable_locked,
        )
        vals_A.locksroot = pending_transfers_tree_A.merkle_root
        # Reveal A's secrets.
        reveal_secrets(A, pending_transfers_tree_A.unlockable)

        # Mock pending transfers data for B -> A
        pending_transfers_tree_B = get_pending_transfers_tree(
            web3,
            unlockable_amount=vals_B.locked_amounts.claimable_locked,
            expired_amount=vals_B.locked_amounts.unclaimable_locked,
        )
        vals_B.locksroot = pending_transfers_tree_B.merkle_root
        # Reveal B's secrets
        reveal_secrets(B, pending_transfers_tree_B.unlockable)

        close_and_update_channel(channel_identifier, A, vals_A, B, vals_B)

        web3.testing.mine(TEST_SETTLE_TIMEOUT_MIN)

        pre_balance_A = custom_token.functions.balanceOf(A).call()
        pre_balance_B = custom_token.functions.balanceOf(B).call()
        pre_balance_contract = custom_token.functions.balanceOf(
            token_network.address).call()

        call_settle(token_network, channel_identifier, A, vals_A, B, vals_B)

        # We do the balance & state tests here for each channel and also compare with
        # the expected settlement amounts
        settle_state_tests(
            channel_identifier,
            A,
            vals_A,
            B,
            vals_B,
            pre_balance_A,
            pre_balance_B,
            pre_balance_contract,
        )

        # We compute again the settlement amounts here to compare with the other channel
        # settlement test values, which should be equal

        # Calculate how much A and B should receive
        settlement_equivalent = get_settlement_amounts(vals_A, vals_B)

        # Calculate how much A and B receive according to onchain computation
        settlement_onchain_equivalent = get_onchain_settlement_amounts(
            vals_A, vals_B)

        assert (settlement_equivalent.participant1_balance ==
                settlement_onchain_equivalent.participant1_balance)
        assert (settlement_equivalent.participant2_balance ==
                settlement_onchain_equivalent.participant2_balance)
        assert (settlement_equivalent.participant1_locked ==
                settlement_onchain_equivalent.participant1_locked)
        assert (settlement_equivalent.participant2_locked ==
                settlement_onchain_equivalent.participant2_locked)

        assert (get_unlocked_amount(secret_registry_contract,
                                    pending_transfers_tree_B.packed_transfers)
                == vals_B.locked_amounts.claimable_locked)

        # A unlocks B's pending transfers
        info_B = token_network.functions.getChannelParticipantInfo(
            channel_identifier, B, A).call()
        assert (settlement_equivalent.participant2_locked == info_B[
            ParticipantInfoIndex.LOCKED_AMOUNT])

        if info_B[ParticipantInfoIndex.LOCKED_AMOUNT] == 0:
            with pytest.raises(TransactionFailed):
                token_network.functions.unlock(
                    channel_identifier, A, B,
                    pending_transfers_tree_B.packed_transfers).call()
        else:
            token_network.functions.unlock(
                channel_identifier, A, B,
                pending_transfers_tree_B.packed_transfers).call_and_transact()

        # The locked amount should have been removed from contract storage
        info_B = token_network.functions.getChannelParticipantInfo(
            channel_identifier, B, A).call()
        assert info_B[ParticipantInfoIndex.LOCKED_AMOUNT] == 0
        assert info_B[ParticipantInfoIndex.LOCKSROOT] == EMPTY_LOCKSROOT

        # B unlocks A's pending transfers
        info_A = token_network.functions.getChannelParticipantInfo(
            channel_identifier, A, B).call()
        assert (settlement_equivalent.participant1_locked == info_A[
            ParticipantInfoIndex.LOCKED_AMOUNT])

        if info_A[ParticipantInfoIndex.LOCKED_AMOUNT] == 0:
            with pytest.raises(TransactionFailed):
                token_network.functions.unlock(
                    channel_identifier, B, A,
                    pending_transfers_tree_A.packed_transfers).call()
        else:
            token_network.functions.unlock(
                channel_identifier, B, A,
                pending_transfers_tree_A.packed_transfers).call_and_transact()

        # The locked amount should have been removed from contract storage
        info_A = token_network.functions.getChannelParticipantInfo(
            channel_identifier, A, B).call()
        assert info_A[ParticipantInfoIndex.LOCKED_AMOUNT] == 0
        assert info_A[ParticipantInfoIndex.LOCKSROOT] == EMPTY_LOCKSROOT

        # Do the post settlement and unlock tests for valid balance proofs
        balance_A = custom_token.functions.balanceOf(A).call()
        balance_B = custom_token.functions.balanceOf(B).call()

        # We MUST ensure balance correctness for valid last balance proofs
        if balance_proof_type == "valid":
            # Calculate how much A and B should receive after the channel is settled and
            # unlock is called by both.
            (
                expected_final_balance_A,
                expected_final_balance_B,
            ) = get_expected_after_settlement_unlock_amounts(vals_A, vals_B)
            expected_balance_A = pre_balance_A + expected_final_balance_A
            expected_balance_B = pre_balance_B + expected_final_balance_B

            assert balance_A == expected_balance_A
            assert balance_B <= expected_balance_B

        # For balance proofs where one of them is old, we need to compare with the expected
        # final balances for the valid last balance proofs
        expected_balance_A = pre_balance_A + expected_final_balance_A0
        expected_balance_B = pre_balance_B + expected_final_balance_B0

        # Tests for when B has submitted an old balance proof for A
        # A must not receive less tokens than expected with a valid last balance proof
        # B must not receive more tokens than expected with a valid last balance proof
        if balance_proof_type == "old_last":
            assert balance_A >= expected_balance_A
            assert balance_B <= expected_balance_B

        # Tests for when A has submitted an old balance proof for B
        # A must not receive more tokens than expected with a valid last balance proof
        # B must not receive less tokens than expected with a valid last balance proof
        if balance_proof_type == "last_old":
            assert balance_A <= expected_balance_A
            assert balance_B >= expected_balance_B

        # Regardless of the tokens received by the two participants, we must make sure tokens
        # are not stolen from the other channels. And we must make sure tokens are not locked in
        # the contract after the entire channel cycle is finalized.
        final_contract_balance = custom_token.functions.balanceOf(
            token_network.address).call()
        assert final_contract_balance == pre_balance_contract - get_total_available_deposit(
            vals_A, vals_B)
        assert custom_token.functions.balanceOf(
            token_network.address).call() == (
                pre_balance_contract -
                (expected_final_balance_A0 + expected_final_balance_B0))
コード例 #7
0
def test_settle_channel_state(
    web3,
    get_accounts,
    custom_token,
    token_network,
    create_channel_and_deposit,
    withdraw_channel,
    close_and_update_channel,
    settle_state_tests,
    channel_test_values,
):
    number_of_channels = 5
    accounts = get_accounts(2 * number_of_channels)
    (vals_A0, vals_B0) = channel_test_values

    # We mimic old balance proofs here, with a high locked amount and lower transferred amount
    # We expect to have the same settlement values as the original values

    def equivalent_transfers(balance_proof):
        new_balance_proof = deepcopy(balance_proof)
        new_balance_proof.locked = randint(
            balance_proof.locked,
            balance_proof.transferred + balance_proof.locked,
        )
        new_balance_proof.transferred = (balance_proof.transferred +
                                         balance_proof.locked -
                                         new_balance_proof.locked)
        return new_balance_proof

    vals_A_reversed = deepcopy(vals_A0)
    vals_A_reversed.locked = vals_A0.transferred
    vals_A_reversed.transferred = vals_A0.locked

    vals_B_reversed = deepcopy(vals_B0)
    vals_B_reversed.locked = vals_B0.transferred
    vals_B_reversed.transferred = vals_B0.locked

    new_values = [
        (vals_A0, vals_B0),
        (vals_A_reversed, vals_B_reversed),
    ] + [
        sorted(
            [
                equivalent_transfers(vals_A0),
                equivalent_transfers(vals_B0),
            ],
            key=lambda x: x.transferred + x.locked,
            reverse=False,
        ) for no in range(0, number_of_channels - 1)
    ]

    # Calculate how much A and B should receive
    settlement = get_settlement_amounts(vals_A0, vals_B0)
    # Calculate how much A and B receive according to onchain computation
    settlement2 = get_onchain_settlement_amounts(vals_A0, vals_B0)

    for no in range(0, number_of_channels + 1):
        A = accounts[no]
        B = accounts[no + 1]
        (vals_A, vals_B) = new_values[no]
        vals_A.locksroot = fake_bytes(32, '02')
        vals_B.locksroot = fake_bytes(32, '03')

        create_channel_and_deposit(A, B, vals_A.deposit, vals_B.deposit)

        withdraw_channel(A, vals_A.withdrawn, B)
        withdraw_channel(B, vals_B.withdrawn, A)

        close_and_update_channel(
            A,
            vals_A,
            B,
            vals_B,
        )

        web3.testing.mine(SETTLE_TIMEOUT_MIN)

        pre_balance_A = custom_token.functions.balanceOf(A).call()
        pre_balance_B = custom_token.functions.balanceOf(B).call()
        pre_balance_contract = custom_token.functions.balanceOf(
            token_network.address).call()

        call_settle(token_network, A, vals_A, B, vals_B)

        # We do the balance & state tests here for each channel and also compare with
        # the expected settlement amounts
        settle_state_tests(
            A,
            vals_A,
            B,
            vals_B,
            pre_balance_A,
            pre_balance_B,
            pre_balance_contract,
        )

        # We compute again the settlement amounts here to compare with the other channel
        # settlement test values, which should be equal

        # Calculate how much A and B should receive
        settlement_equivalent = get_settlement_amounts(vals_A, vals_B)
        assert (settlement.participant1_balance +
                settlement.participant2_locked ==
                settlement_equivalent.participant1_balance +
                settlement_equivalent.participant2_locked)
        assert (settlement.participant2_balance +
                settlement.participant1_locked ==
                settlement_equivalent.participant2_balance +
                settlement_equivalent.participant1_locked)

        # Calculate how much A and B receive according to onchain computation
        settlement2_equivalent = get_onchain_settlement_amounts(vals_A, vals_B)
        assert (settlement2.participant1_balance +
                settlement2.participant2_locked ==
                settlement2_equivalent.participant1_balance +
                settlement2_equivalent.participant2_locked)
        assert (settlement2.participant2_balance +
                settlement2.participant1_locked ==
                settlement2_equivalent.participant2_balance +
                settlement2_equivalent.participant1_locked)
コード例 #8
0
def test_settle_single_direct_transfer_for_closing_party(
    web3: Web3,
    get_accounts: Callable,
    custom_token: Contract,
    token_network: Contract,
    create_channel: Callable,
    channel_deposit: Callable,
    create_balance_proof: Callable,
    create_balance_proof_countersignature: Callable,
) -> None:
    """ Test settle of a channel with one direct transfer to the participant
    that called close.
    """
    (A, B) = get_accounts(2)
    (vals_A, vals_B) = (
        ChannelValues(deposit=1, withdrawn=0, transferred=0),
        ChannelValues(deposit=10, withdrawn=0, transferred=5),
    )
    settle_timeout = TEST_SETTLE_TIMEOUT_MIN

    channel_identifier = create_channel(A, B)[0]
    channel_deposit(channel_identifier, A, vals_A.deposit, B)
    channel_deposit(channel_identifier, B, vals_B.deposit, A)

    balance_proof_B = create_balance_proof(
        channel_identifier,
        B,
        vals_B.transferred,
        vals_B.locked_amounts.locked,
        1,
        LOCKSROOT_OF_NO_LOCKS,
    )
    closing_sig_A = create_balance_proof_countersignature(
        participant=A,
        channel_identifier=channel_identifier,
        msg_type=MessageTypeId.BALANCE_PROOF,
        **balance_proof_B._asdict(),
    )
    token_network.functions.closeChannel(channel_identifier, B, A,
                                         *balance_proof_B._asdict().values(),
                                         closing_sig_A).call_and_transact(
                                             {"from": A})

    pre_balance_A = custom_token.functions.balanceOf(A).call()
    pre_balance_B = custom_token.functions.balanceOf(B).call()
    pre_balance_contract = custom_token.functions.balanceOf(
        token_network.address).call()

    web3.testing.mine(settle_timeout + 1)
    token_network.functions.settleChannel(
        channel_identifier=channel_identifier,
        participant1=A,
        participant1_transferred_amount=0,
        participant1_locked_amount=0,
        participant1_locksroot=LOCKSROOT_OF_NO_LOCKS,
        participant2=B,
        participant2_transferred_amount=vals_B.transferred,
        participant2_locked_amount=0,
        participant2_locksroot=LOCKSROOT_OF_NO_LOCKS,
    ).call_and_transact({"from": A})

    # Calculate how much A and B should receive
    expected_settlement = get_settlement_amounts(vals_A, vals_B)
    # Calculate how much A and B receive according to onchain computation
    onchain_settlement = get_onchain_settlement_amounts(vals_A, vals_B)

    assert expected_settlement.participant1_balance == onchain_settlement.participant1_balance
    assert expected_settlement.participant2_balance == onchain_settlement.participant2_balance
    assert custom_token.functions.balanceOf(A).call() == pre_balance_A + 6
    assert custom_token.functions.balanceOf(B).call() == pre_balance_B + 5
    assert (custom_token.functions.balanceOf(
        token_network.address).call() == pre_balance_contract - 11)
コード例 #9
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,
                        )