示例#1
0
def test_close_channel_event_no_offchain_transfers(
    get_accounts,
    token_network,
    create_channel,
    event_handler,
):
    """ closeChannel succeeds and emits an event even with nonce 0 and no balance proofs """
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)

    channel_identifier = create_channel(A, B)[0]

    # No off-chain transfers have occured
    # There is no signature data here, because it was never provided to A
    txn_hash = token_network.functions.closeChannel(
        channel_identifier,
        B,
        EMPTY_BALANCE_HASH,
        0,
        EMPTY_ADDITIONAL_HASH,
        EMPTY_SIGNATURE,
    ).transact({'from': A})

    ev_handler.add(txn_hash, ChannelEvent.CLOSED,
                   check_channel_closed(channel_identifier, A, 0))
    ev_handler.check()
示例#2
0
def test_close_channel_event(
    get_accounts,
    token_network,
    create_channel,
    channel_deposit,
    create_balance_proof,
    event_handler,
):
    """ A successful closeChannel call produces a CLOSED event """
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)
    deposit_A = 10

    channel_identifier = create_channel(A, B)[0]
    channel_deposit(channel_identifier, A, deposit_A, B)
    balance_proof = create_balance_proof(
        channel_identifier,
        B,
        transferred_amount=5,
        locked_amount=0,
        nonce=3,
    )

    txn_hash = token_network.functions.closeChannel(
        channel_identifier,
        B,
        *balance_proof,
    ).transact({'from': A})

    ev_handler.add(txn_hash, ChannelEvent.CLOSED,
                   check_channel_closed(channel_identifier, A, 3))
    ev_handler.check()
def test_close_channel_event(
    get_accounts: Callable,
    token_network: Contract,
    create_channel: Callable,
    channel_deposit: Callable,
    create_balance_proof: Callable,
    event_handler: Callable,
    create_balance_proof_countersignature: Callable,
) -> None:
    """ A successful closeChannel call produces a CLOSED event """
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)
    deposit_A = 10

    channel_identifier = create_channel(A, B)[0]
    channel_deposit(channel_identifier, A, deposit_A, B)
    balance_proof = create_balance_proof(channel_identifier,
                                         B,
                                         transferred_amount=5,
                                         locked_amount=0,
                                         nonce=3)
    close_sig = create_balance_proof_countersignature(
        A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof)

    txn_hash = token_network.functions.closeChannel(
        channel_identifier, B, A, *balance_proof,
        close_sig).call_and_transact({"from": A})

    ev_handler.add(
        txn_hash,
        ChannelEvent.CLOSED,
        check_channel_closed(channel_identifier, A, 3, balance_proof[0]),
    )
    ev_handler.check()
def test_close_channel_event_no_offchain_transfers(
    get_accounts: Callable,
    token_network: Contract,
    create_channel: Callable,
    event_handler: Callable,
    create_close_signature_for_no_balance_proof: Callable,
) -> None:
    """ closeChannel succeeds and emits an event even with nonce 0 and no balance proofs """
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)

    channel_identifier = create_channel(A, B)[0]
    closing_sig = create_close_signature_for_no_balance_proof(
        A, channel_identifier)

    # No off-chain transfers have occured
    # There is no signature data here, because it was never provided to A
    txn_hash = token_network.functions.closeChannel(
        channel_identifier,
        B,
        A,
        EMPTY_BALANCE_HASH,
        0,
        EMPTY_ADDITIONAL_HASH,
        EMPTY_SIGNATURE,
        closing_sig,
    ).call_and_transact({"from": A})

    ev_handler.add(
        txn_hash,
        ChannelEvent.CLOSED,
        check_channel_closed(channel_identifier, A, 0, EMPTY_BALANCE_HASH),
    )
    ev_handler.check()
示例#5
0
def test_close_channel_event_no_offchain_transfers(
    get_accounts,
    token_network,
    create_channel,
    create_balance_proof,
    event_handler,
):
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)

    channel_identifier = create_channel(A, B)[0]

    # No off-chain transfers have occured
    # There is no signature data here, because it was never provided to A
    txn_hash = token_network.functions.closeChannel(
        B,
        fake_bytes(32),
        0,
        fake_bytes(32),
        fake_bytes(64),
    ).transact({'from': A})

    ev_handler.add(txn_hash, EVENT_CHANNEL_CLOSED,
                   check_channel_closed(channel_identifier, A))
    ev_handler.check()
示例#6
0
def test_close_channel_event_no_offchain_transfers(get_accounts, token_network,
                                                   create_channel,
                                                   create_balance_proof,
                                                   event_handler):
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)

    channel_identifier = create_channel(A, B)
    balance_proof = create_balance_proof(channel_identifier, B, 0, 0, 0)

    txn_hash = token_network.transact({'from': A}).closeChannel(*balance_proof)

    ev_handler.add(txn_hash, E_CHANNEL_CLOSED,
                   check_channel_closed(channel_identifier, A))
    ev_handler.check()
示例#7
0
def test_close_channel_event(get_accounts, token_network, create_channel,
                             channel_deposit, create_balance_proof,
                             event_handler):
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)
    deposit_A = 10

    channel_identifier = create_channel(A, B)
    channel_deposit(channel_identifier, A, deposit_A)
    balance_proof = create_balance_proof(channel_identifier, B, 5, 0, 3)

    txn_hash = token_network.transact({'from': A}).closeChannel(*balance_proof)

    ev_handler.add(txn_hash, E_CHANNEL_CLOSED,
                   check_channel_closed(channel_identifier, A))
    ev_handler.check()
示例#8
0
def test_close_channel_event(
        get_accounts,
        token_network,
        create_channel,
        channel_deposit,
        create_balance_proof,
        event_handler,
):
    ev_handler = event_handler(token_network)
    (A, B) = get_accounts(2)
    deposit_A = 10

    channel_identifier = create_channel(A, B)[0]
    channel_deposit(channel_identifier, A, deposit_A, B)
    balance_proof = create_balance_proof(channel_identifier, B, 5, 0, 3)

    txn_hash = token_network.functions.closeChannel(
        channel_identifier,
        B,
        *balance_proof,
    ).transact({'from': A})

    ev_handler.add(txn_hash, ChannelEvent.CLOSED, check_channel_closed(channel_identifier, A))
    ev_handler.check()
示例#9
0
def test_msc_happy_path(token_network, monitoring_service_external,
                        get_accounts, create_channel, channel_deposit,
                        create_balance_proof,
                        create_balance_proof_update_signature,
                        create_reward_proof, event_handler,
                        raiden_service_bundle, custom_token):
    # setup: two parties + MS
    ev_handler = event_handler(token_network)
    (A, B, MS) = get_accounts(3)
    reward_amount = 10
    # mint some tokens
    custom_token.functions.mint(50).transact({'from': MS})
    custom_token.functions.mint(50).transact({'from': A})
    custom_token.functions.mint(50).transact({'from': B})
    # register MS in the RaidenServiceBundle contract
    custom_token.functions.approve(raiden_service_bundle.address,
                                   20).transact({'from': MS})
    raiden_service_bundle.functions.deposit(20).transact({'from': MS})
    ms_balance_after_deposit = monitoring_service_external.functions.balances(
        MS).call()
    # raiden node deposit
    custom_token.functions.approve(monitoring_service_external.address,
                                   20).transact({'from': B})
    monitoring_service_external.functions.deposit(B, 20).transact({'from': B})

    # 1) open a channel (c1, c2)
    channel_identifier = create_channel(A, B)[0]
    txn_hash = channel_deposit(A, 20, B)
    txn_hash = channel_deposit(B, 20, A)
    # 2) create balance proof
    balance_proof_A = create_balance_proof(channel_identifier,
                                           B,
                                           transferred_amount=10,
                                           nonce=1)
    balance_proof_B = create_balance_proof(channel_identifier,
                                           A,
                                           transferred_amount=20,
                                           nonce=2)
    non_closing_signature_B = create_balance_proof_update_signature(
        B, channel_identifier, *balance_proof_B)
    # 2a) create reward proof
    reward_proof = create_reward_proof(
        B,
        channel_identifier,
        reward_amount,
        token_network.address,
        nonce=balance_proof_B[1],
    )
    # 3) c1 closes channel
    txn_hash = token_network.functions.closeChannel(
        B, *balance_proof_A).transact({'from': A})
    ev_handler.add(txn_hash, EVENT_CHANNEL_CLOSED,
                   check_channel_closed(channel_identifier, A))
    ev_handler.check()
    # 4) MS calls `MSC::monitor()` using c1's BP and reward proof

    txn_hash = monitoring_service_external.functions.monitor(
        A,
        B,
        balance_proof_B[0],  # balance_hash
        balance_proof_B[1],  # nonce
        balance_proof_B[2],  # additional_hash
        balance_proof_B[3],  # closing signature
        non_closing_signature_B,  # non-closing signature
        reward_proof[1],  # reward amount
        token_network.address,  # token network address
        reward_proof[5]  # reward proof signature
    ).transact({'from': MS})
    # 5) MSC calls TokenNetwork updateTransfer
    # 6) channel is settled
    token_network.web3.testing.mine(8)
    token_network.functions.settleChannel(
        A,  # participant1
        20,  # participant1_transferred_amount
        0,  # participant1_locked_amount
        b'\x00' * 32,  # participant1_locksroot
        B,  # participant2
        10,  # participant2_transferred_amount
        0,  # participant2_locked_amount
        b'\x00' * 32,  # participant2_locksroot
    ).transact()
    # 7) MS claims the reward
    monitoring_service_external.functions.claimReward(
        token_network.address,
        A,
        B,
    ).transact({'from': MS})
    ms_balance_after_reward = monitoring_service_external.functions.balances(
        MS).call()
    assert ms_balance_after_reward == (ms_balance_after_deposit +
                                       reward_amount)
def test_close_nonce_zero(
    get_accounts: Callable,
    token_network: Contract,
    create_channel: Callable,
    create_balance_proof: Callable,
    create_balance_proof_countersignature: Callable,
    event_handler: Callable,
) -> None:
    """ closeChannel with a balance proof with nonce zero should not change the channel state """
    (A, B) = get_accounts(2)
    vals_B = ChannelValues(
        deposit=20,
        transferred=5,
        locksroot=fake_bytes(32, "03"),
        locked_amounts=LockedAmounts(claimable_locked=3, unclaimable_locked=4),
        nonce=0,
    )
    # Create channel and deposit
    channel_identifier = create_channel(A, B)[0]

    # Create balance proofs
    balance_proof_B = create_balance_proof(
        channel_identifier,
        B,
        vals_B.transferred,
        vals_B.locked_amounts.locked,
        vals_B.nonce,
        vals_B.locksroot,
    )
    close_sig_A = create_balance_proof_countersignature(
        A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B)

    (
        _,
        _,
        B_is_the_closer,
        B_balance_hash,
        B_nonce,
        _,
        _,
    ) = token_network.functions.getChannelParticipantInfo(
        channel_identifier, B, A).call()
    assert B_is_the_closer is False
    assert B_balance_hash == EMPTY_BALANCE_HASH
    assert B_nonce == 0

    ev_handler = event_handler(token_network)

    close_tx = token_network.functions.closeChannel(
        channel_identifier, B, A, *balance_proof_B,
        close_sig_A).call_and_transact({"from": A})

    ev_handler.add(
        close_tx,
        ChannelEvent.CLOSED,
        check_channel_closed(channel_identifier, A, 0, balance_proof_B[0]),
    )
    ev_handler.check()

    # Even though we somehow provide valid values for the balance proof, they are not taken into
    # consideration if the nonce is 0.
    # The Raiden client enforces that the nonce is > 0 if off-chain transfers are made.
    (
        _,
        _,
        B_is_the_closer,
        B_balance_hash,
        B_nonce,
        _,
        _,
    ) = token_network.functions.getChannelParticipantInfo(
        channel_identifier, B, A).call()
    assert B_is_the_closer is False
    assert B_balance_hash == EMPTY_BALANCE_HASH
    assert B_nonce == 0
示例#11
0
def test_msc_happy_path(
    token_network,
    monitoring_service_external,
    get_accounts,
    create_channel,
    channel_deposit,
    create_balance_proof,
    create_balance_proof_update_signature,
    create_reward_proof,
    event_handler,
    raiden_service_bundle,
    custom_token,
):
    # setup: two parties + MS
    token_network_ev_handler = event_handler(token_network)
    ms_ev_handler = event_handler(monitoring_service_external)
    (A, B, MS) = get_accounts(3)
    reward_amount = 10
    # mint some tokens
    custom_token.functions.mint(50).transact({'from': MS})
    custom_token.functions.mint(50).transact({'from': A})
    custom_token.functions.mint(50).transact({'from': B})
    # register MS in the RaidenServiceBundle contract
    custom_token.functions.approve(raiden_service_bundle.address,
                                   20).transact({'from': MS})
    raiden_service_bundle.functions.deposit(20).transact({'from': MS})
    ms_balance_after_deposit = monitoring_service_external.functions.balances(
        MS).call()
    # raiden node deposit
    custom_token.functions.approve(monitoring_service_external.address,
                                   20).transact({'from': B})
    txn_hash = monitoring_service_external.functions.deposit(B, 20).transact(
        {'from': B})
    ms_ev_handler.add(
        txn_hash,
        MonitoringServiceEvent.NEW_DEPOSIT,
        check_ms_new_deposit(B, 20),
    )
    ms_ev_handler.check()

    # 1) open a channel (c1, c2)
    channel_identifier = create_channel(A, B)[0]
    txn_hash = channel_deposit(channel_identifier, A, 20, B)
    txn_hash = channel_deposit(channel_identifier, B, 20, A)

    # 2) create balance proof
    balance_proof_A = create_balance_proof(channel_identifier,
                                           B,
                                           transferred_amount=10,
                                           nonce=1)
    balance_proof_B = create_balance_proof(channel_identifier,
                                           A,
                                           transferred_amount=20,
                                           nonce=2)
    non_closing_signature_B = create_balance_proof_update_signature(
        B,
        channel_identifier,
        *balance_proof_B,
    )
    # 2a) create reward proof
    reward_proof = create_reward_proof(
        B,
        channel_identifier,
        reward_amount,
        token_network.address,
        nonce=balance_proof_B[1],
    )

    # 3) c1 closes channel
    txn_hash = token_network.functions.closeChannel(
        channel_identifier,
        B,
        *balance_proof_A,
    ).transact({'from': A})
    token_network_ev_handler.add(
        txn_hash,
        ChannelEvent.CLOSED,
        check_channel_closed(channel_identifier, A, balance_proof_A[1]),
    )
    token_network_ev_handler.check()

    # 4) MS calls `MSC::monitor()` using c1's BP and reward proof
    txn_hash = monitoring_service_external.functions.monitor(
        A,
        B,
        balance_proof_B[0],  # balance_hash
        balance_proof_B[1],  # nonce
        balance_proof_B[2],  # additional_hash
        balance_proof_B[3],  # closing signature
        non_closing_signature_B,  # non-closing signature
        reward_proof[1],  # reward amount
        token_network.address,  # token network address
        reward_proof[5],  # reward proof signature
    ).transact({'from': MS})
    ms_ev_handler.add(
        txn_hash,
        MonitoringServiceEvent.NEW_BALANCE_PROOF_RECEIVED,
        check_new_balance_proof_received(
            token_network.address,
            channel_identifier,
            reward_amount,
            balance_proof_B[1],
            MS,
            B,
        ),
    )
    ms_ev_handler.check()

    # 5) MSC calls TokenNetwork updateTransfer
    # 6) channel is settled
    token_network.web3.testing.mine(8)
    token_network.functions.settleChannel(
        channel_identifier,
        B,  # participant2
        10,  # participant2_transferred_amount
        0,  # participant2_locked_amount
        EMPTY_LOCKSROOT,  # participant2_locksroot
        A,  # participant1
        20,  # participant1_transferred_amount
        0,  # participant1_locked_amount
        EMPTY_LOCKSROOT,  # participant1_locksroot
    ).transact()

    # 7) MS claims the reward
    monitoring_service_external.functions.claimReward(
        channel_identifier,
        token_network.address,
        A,
        B,
    ).transact({'from': MS})
    reward_identifier = Web3.sha3(
        encode_single('uint256', channel_identifier) +
        Web3.toBytes(hexstr=token_network.address), )
    ms_ev_handler.add(
        txn_hash,
        MonitoringServiceEvent.REWARD_CLAIMED,
        check_reward_claimed(
            MS,
            reward_amount,
            reward_identifier,
        ),
    )
    ms_ev_handler.check()
    ms_balance_after_reward = monitoring_service_external.functions.balances(
        MS).call()
    assert ms_balance_after_reward == (ms_balance_after_deposit +
                                       reward_amount)
示例#12
0
def test_close_nonce_zero(
    get_accounts,
    token_network,
    create_channel,
    create_balance_proof,
    event_handler,
):
    """ closeChannel with a balance proof with nonce zero should not change the channel state """
    (A, B) = get_accounts(2)
    vals_B = ChannelValues(
        deposit=20,
        transferred=5,
        locksroot=fake_bytes(32, '03'),
        claimable_locked=3,
        unclaimable_locked=4,
        nonce=0,
    )
    # Create channel and deposit
    channel_identifier = create_channel(A, B)[0]

    # Create balance proofs
    balance_proof_B = create_balance_proof(
        channel_identifier,
        B,
        vals_B.transferred,
        vals_B.locked,
        vals_B.nonce,
        vals_B.locksroot,
    )

    (
        _,
        _,
        B_is_the_closer,
        B_balance_hash,
        B_nonce,
        _,
        _,
    ) = token_network.functions.getChannelParticipantInfo(
        channel_identifier, B, A).call()
    assert B_is_the_closer is False
    assert B_balance_hash == EMPTY_BALANCE_HASH
    assert B_nonce == 0

    ev_handler = event_handler(token_network)

    close_tx = token_network.functions.closeChannel(
        channel_identifier,
        B,
        *balance_proof_B,
    ).transact({'from': A})

    ev_handler.add(close_tx, ChannelEvent.CLOSED,
                   check_channel_closed(channel_identifier, A, 0))
    ev_handler.check()

    # Even though we somehow provide valid values for the balance proof, they are not taken into
    # consideration if the nonce is 0.
    # The Raiden client enforces that the nonce is > 0 if off-chain transfers are made.
    (
        _,
        _,
        B_is_the_closer,
        B_balance_hash,
        B_nonce,
        _,
        _,
    ) = token_network.functions.getChannelParticipantInfo(
        channel_identifier, B, A).call()
    assert B_is_the_closer is False
    assert B_balance_hash == EMPTY_BALANCE_HASH
    assert B_nonce == 0