def test_channel_erc20_event(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        event_handler,
        txn_gas,
        print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver) = get_accounts(2)
    deposit = 1000
    gas_used = 0

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)

    # Approve token allowance
    txn_hash_approve = token_instance.transact({"from": sender}).approve(uraiden_instance.address, deposit)
    gas_used += txn_gas(txn_hash_approve)

    # Create channel
    txn_hash = uraiden_instance.transact({"from": sender}).createChannel(receiver, deposit)

    # Check creation event
    ev_handler.add(
        txn_hash,
        uraiden_events['created'],
        checkCreatedEvent(sender, receiver, deposit)
    )
    ev_handler.check()

    print_gas(txn_hash, 'channel_20_create', gas_used)
def test_add_trusted_contracts_state(owner, get_accounts, uraiden_instance, delegate_contract, print_gas):
    (A, B) = get_accounts(2)
    trusted_contract1 = delegate_contract()
    trusted_contract2 = delegate_contract()
    trusted_contract3 = delegate_contract()
    trusted_contract4 = delegate_contract()

    assert not uraiden_instance.call().trusted_contracts(trusted_contract1.address)
    assert not uraiden_instance.call().trusted_contracts(trusted_contract2.address)
    assert not uraiden_instance.call().trusted_contracts(trusted_contract3.address)
    assert not uraiden_instance.call().trusted_contracts(trusted_contract4.address)

    uraiden_instance.transact({'from': owner}).addTrustedContracts([A])
    assert not uraiden_instance.call().trusted_contracts(A)

    txn_hash = uraiden_instance.transact({'from': owner}).addTrustedContracts([trusted_contract1.address])
    assert uraiden_instance.call().trusted_contracts(trusted_contract1.address)

    print_gas(txn_hash, 'add 1 trusted contract')

    txn_hash = uraiden_instance.transact({'from': owner}).addTrustedContracts([
        trusted_contract2.address,
        trusted_contract3.address,
        A,
        trusted_contract4.address
    ])
    assert uraiden_instance.call().trusted_contracts(trusted_contract2.address)
    assert uraiden_instance.call().trusted_contracts(trusted_contract3.address)
    assert uraiden_instance.call().trusted_contracts(trusted_contract4.address)
    assert not uraiden_instance.call().trusted_contracts(A)

    print_gas(txn_hash, 'add 3 trusted contracts')
def test_channel_erc223_event(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        event_handler,
        print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver) = get_accounts(2)
    deposit = 1000
    txdata = bytes.fromhex(sender[2:] + receiver[2:])

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)

    txn_hash = token_instance.transact({"from": sender}).transfer(
        uraiden_instance.address,
        1000,
        txdata
    )

    # Check creation event
    ev_handler.add(
        txn_hash,
        uraiden_events['created'],
        checkCreatedEvent(sender, receiver, deposit)
    )
    ev_handler.check()

    print_gas(txn_hash, 'channel_223_create')
Exemple #4
0
def test_cooperative_close_call_receiver(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel,
        print_gas):
    (sender, receiver, open_block_number, balance_msg_sig, closing_sig) = get_channel()
    balance = channel_params['balance']

    sender_verified = uraiden_instance.call().extractBalanceProofSignature(
        receiver,
        open_block_number,
        balance,
        balance_msg_sig
    )
    assert is_same_address(sender_verified, sender)

    receiver_verified = uraiden_instance.call().extractClosingSignature(
        sender,
        open_block_number,
        balance,
        closing_sig
    )
    assert is_same_address(receiver_verified, receiver)

    # Cooperative close can be called by anyone
    txn_hash = uraiden_instance.transact({"from": receiver}).cooperativeClose(
        receiver,
        open_block_number,
        balance,
        balance_msg_sig,
        closing_sig
    )

    print_gas(txn_hash, 'cooperativeClose')
def test_channel_erc20_event(owner, get_accounts, uraiden_instance,
                             token_instance, event_handler, txn_gas,
                             print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver) = get_accounts(2)
    deposit = 1000
    gas_used = 0

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit + 100)

    # Approve token allowance
    txn_hash_approve = token_instance.transact({
        "from": sender
    }).approve(uraiden_instance.address, deposit)
    gas_used += txn_gas(txn_hash_approve)

    # Create channel
    txn_hash = uraiden_instance.transact({
        "from": sender
    }).createChannelERC20(receiver, deposit)

    # Check creation event
    ev_handler.add(txn_hash, uraiden_events['created'],
                   checkCreatedEvent(sender, receiver, deposit))
    ev_handler.check()

    print_gas(txn_hash, 'channel_20_create', gas_used)
def test_channel_topup_223(get_accounts, uraiden_instance, token_instance,
                           get_channel, event_handler, print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver, A, B) = get_accounts(4)
    channel_deposit = 700
    channel = get_channel(uraiden_instance, token_instance, channel_deposit,
                          sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel
    top_up_deposit = 14

    # address 20 bytes
    # padded block number from uint32 (4 bytes) to 32 bytes
    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(
        8)
    top_up_data = bytes.fromhex(top_up_data)

    top_up_data_wrong_receiver = sender[2:].zfill(64) + hex(
        open_block_number)[2:].zfill(8)

    top_up_data_wrong_block = receiver[2:].zfill(64) + hex(open_block_number +
                                                           30)[2:].zfill(8)

    with pytest.raises(tester.TransactionFailed):
        token.transact({
            "from": sender
        }).transfer(uraiden_instance.address, top_up_deposit,
                    top_up_data_wrong_receiver)
    with pytest.raises(tester.TransactionFailed):
        token.transact({
            "from": sender
        }).transfer(uraiden_instance.address, top_up_deposit,
                    top_up_data_wrong_block)
    with pytest.raises(tester.TransactionFailed):
        token.transact({
            "from": sender
        }).transfer(uraiden_instance.address, 0, top_up_data)

    # Call Token - this calls uraiden_instance.tokenFallback
    txn_hash = token.transact({
        "from": sender
    }).transfer(uraiden_instance.address, top_up_deposit, top_up_data)

    channel_data = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit + top_up_deposit  # deposit

    print_gas(txn_hash, 'test_channel_topup_223')

    # Check topup event
    ev_handler.add(
        txn_hash, uraiden_events['topup'],
        checkToppedUpEvent(sender, receiver, open_block_number, top_up_deposit,
                           channel_deposit + top_up_deposit))
    ev_handler.check()
Exemple #7
0
def test_settle_state(
        web3,
        channel_params,
        contract_params,
        uraiden_instance,
        token_instance,
        get_channel,
        print_gas):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']
    deposit = channel_params['deposit']

    # Trigger a challenge period
    uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        balance
    )
    web3.testing.mine(contract_params['challenge_period'] + 1)

    # Keep track of pre closing balances
    receiver_pre_balance = token_instance.call().balanceOf(receiver)
    sender_pre_balance = token_instance.call().balanceOf(sender)
    contract_pre_balance = token_instance.call().balanceOf(uraiden_instance.address)

    txn_hash = uraiden_instance.transact({"from": sender}).settle(receiver, open_block_number)

    # Check post closing balances
    receiver_post_balance = receiver_pre_balance + balance
    sender_post_balance = sender_pre_balance + (deposit - balance)
    contract_post_balance = contract_pre_balance - deposit

    assert token_instance.call().balanceOf(receiver) == receiver_post_balance
    assert token_instance.call().balanceOf(sender) == sender_post_balance
    assert token_instance.call().balanceOf(uraiden_instance.address) == contract_post_balance

    channel_settle_tests(uraiden_instance, token_instance,
        (sender, receiver, open_block_number)
    )

    # Channel does not exist anymore, so this will fail
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)

    web3.testing.mine(30)

    # Cannot be called another time
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).settle(receiver, open_block_number)

    print_gas(txn_hash, 'settle')
def test_uncooperative_close_state(contract_params, channel_params,
                                   uraiden_instance, get_channel, get_block,
                                   print_gas):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    txn_hash = uraiden_instance.transact({
        "from": sender
    }).uncooperativeClose(receiver, open_block_number, balance)

    channel_info = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    # settle_block_number
    assert channel_info[2] == get_block(
        txn_hash) + contract_params['challenge_period']
    # closing_balance
    assert channel_info[3] == balance

    print_gas(txn_hash, 'uncooperativeClose')
def test_channel_topup_223(
    get_accounts,
    uraiden_instance,
    token_instance,
    get_channel,
    event_handler,
    print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver, A, B) = get_accounts(4)
    channel_deposit = 700
    channel = get_channel(uraiden_instance, token_instance, channel_deposit, sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel
    top_up_deposit = 14

    # address 20 bytes
    # padded block number from uint32 (4 bytes) to 32 bytes
    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(8)
    top_up_data = bytes.fromhex(top_up_data)

    top_up_data_wrong_receiver = sender[2:].zfill(64) + hex(open_block_number)[2:].zfill(8)

    top_up_data_wrong_block = receiver[2:].zfill(64) + hex(open_block_number+30)[2:].zfill(8)

    with pytest.raises(tester.TransactionFailed):
        token.transact({"from": sender}).transfer(uraiden_instance.address, top_up_deposit, top_up_data_wrong_receiver)
    with pytest.raises(tester.TransactionFailed):
        token.transact({"from": sender}).transfer(uraiden_instance.address, top_up_deposit, top_up_data_wrong_block)
    with pytest.raises(tester.TransactionFailed):
        token.transact({"from": sender}).transfer(uraiden_instance.address, 0, top_up_data)

    # Call Token - this calls uraiden_instance.tokenFallback
    txn_hash = token.transact({"from": sender}).transfer(uraiden_instance.address, top_up_deposit, top_up_data)

    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit + top_up_deposit  # deposit

    print_gas(txn_hash, 'test_channel_topup_223')

    # Check topup event
    ev_handler.add(txn_hash, uraiden_events['topup'], checkToppedUpEvent(sender, receiver, open_block_number, top_up_deposit, channel_deposit + top_up_deposit))
    ev_handler.check()
Exemple #10
0
def test_add_trusted_contracts_state(owner, get_accounts, uraiden_instance,
                                     delegate_contract, print_gas):
    (A, B) = get_accounts(2)
    trusted_contract1 = delegate_contract()
    trusted_contract2 = delegate_contract()
    trusted_contract3 = delegate_contract()
    trusted_contract4 = delegate_contract()

    assert not uraiden_instance.call().trusted_contracts(
        trusted_contract1.address)
    assert not uraiden_instance.call().trusted_contracts(
        trusted_contract2.address)
    assert not uraiden_instance.call().trusted_contracts(
        trusted_contract3.address)
    assert not uraiden_instance.call().trusted_contracts(
        trusted_contract4.address)

    uraiden_instance.transact({'from': owner}).addTrustedContracts([A])
    assert not uraiden_instance.call().trusted_contracts(A)

    txn_hash = uraiden_instance.transact({
        'from': owner
    }).addTrustedContracts([trusted_contract1.address])
    assert uraiden_instance.call().trusted_contracts(trusted_contract1.address)

    print_gas(txn_hash, 'add 1 trusted contract')

    txn_hash = uraiden_instance.transact({
        'from': owner
    }).addTrustedContracts([
        trusted_contract2.address, trusted_contract3.address, A,
        trusted_contract4.address
    ])
    assert uraiden_instance.call().trusted_contracts(trusted_contract2.address)
    assert uraiden_instance.call().trusted_contracts(trusted_contract3.address)
    assert uraiden_instance.call().trusted_contracts(trusted_contract4.address)
    assert not uraiden_instance.call().trusted_contracts(A)

    print_gas(txn_hash, 'add 3 trusted contracts')
def test_uncooperative_close_state(
        contract_params,
        channel_params,
        uraiden_instance,
        get_channel,
        get_block,
        print_gas):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    txn_hash = uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        balance
    )

    channel_info = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    # settle_block_number
    assert channel_info[2] == get_block(txn_hash) + contract_params['challenge_period']
    # closing_balance
    assert channel_info[3] == balance

    print_gas(txn_hash, 'uncooperativeClose')
Exemple #12
0
def test_close_by_sender(get_accounts, uraiden_instance, token_instance,
                         get_channel, print_gas, event_handler):
    token = token_instance
    (sender, receiver, A) = get_accounts(3)
    ev_handler = event_handler(uraiden_instance)
    channel_deposit = 800
    top_up_deposit = 14
    channel = get_channel(uraiden_instance, token_instance, channel_deposit,
                          sender, receiver)
    (sender, receiver, open_block_number) = channel

    balance = channel_deposit - 1

    balance_message_hash = balance_proof_hash(receiver, open_block_number,
                                              balance,
                                              uraiden_instance.address)
    balance_message_hash_false_receiver = balance_proof_hash(
        A, open_block_number, balance, uraiden_instance.address)

    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    balance_msg_sig_false_signer, addr = sign.check(balance_message_hash,
                                                    tester.k4)
    balance_msg_sig_false_receiver, addr = sign.check(
        balance_message_hash_false_receiver, tester.k4)

    closing_sig, addr = sign.check(sol_sha3(balance_msg_sig), tester.k3)
    closing_sig_false_signer, addr = sign.check(sol_sha3(balance_msg_sig),
                                                tester.k4)
    closing_sig_false_receiver, addr = sign.check(
        sol_sha3(balance_msg_sig_false_receiver), tester.k3)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).cooperativeClose(A, open_block_number, balance, balance_msg_sig,
                            closing_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).cooperativeClose(receiver, open_block_number - 3, balance,
                            balance_msg_sig, closing_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).cooperativeClose(receiver, open_block_number, balance + 5,
                            balance_msg_sig, closing_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig_false_signer, closing_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig_false_receiver, closing_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig, closing_sig_false_signer)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig, closing_sig_false_receiver)

    channel_pre_close_tests(uraiden_instance, token_instance, channel,
                            top_up_deposit)

    receiver_pre_balance = token.call().balanceOf(receiver)
    sender_pre_balance = token.call().balanceOf(sender)
    contract_pre_balance = token.call().balanceOf(uraiden_instance.address)

    txn_hash = uraiden_instance.transact({
        'from': sender
    }).cooperativeClose(receiver, open_block_number, balance, balance_msg_sig,
                        closing_sig)

    # TODO: raise Exception
    # ev_handler.add(txn_hash, uraiden_events['closed'], checkClosedEvent(sender, receiver, open_block_number, balance))
    # ev_handler.check()

    channel_deposit += top_up_deposit
    receiver_post_balance = receiver_pre_balance + balance
    sender_post_balance = sender_pre_balance + (channel_deposit - balance)
    contract_post_balance = contract_pre_balance - channel_deposit

    assert token.call().balanceOf(receiver) == receiver_post_balance
    assert token.call().balanceOf(sender) == sender_post_balance
    assert token.call().balanceOf(
        uraiden_instance.address) == contract_post_balance

    channel_settle_tests(uraiden_instance, token_instance, channel)

    ev_handler.add(
        txn_hash, uraiden_events['settled'],
        checkSettledEvent(sender, receiver, open_block_number, balance))
    ev_handler.check()

    print_gas(txn_hash, 'test_close_by_sender')
def test_channel_topup_20(
    get_accounts,
    uraiden_instance,
    token_instance,
    get_channel,
    txn_gas,
    event_handler,
    print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver, A) = get_accounts(3)
    channel_deposit = 999
    channel = get_channel(uraiden_instance, token_instance, channel_deposit, sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel
    top_up_deposit = 14

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': sender}).topUp(
            receiver,
            open_block_number,
            top_up_deposit
        )

    # Approve token allowance
    txn_hash = token.transact({"from": sender}).approve(uraiden_instance.address, top_up_deposit)
    gas_used_approve = txn_gas(txn_hash)

    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).topUp(0x0, top_up_deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).topUp('0x0', top_up_deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).topUp(fake_address, top_up_deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).topUp(receiver, -3)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).topUp(receiver, MAX_UINT192 + 1)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': A}).topUp(receiver, open_block_number, top_up_deposit)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': sender}).topUp(A, open_block_number, top_up_deposit)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': sender}).topUp(receiver, open_block_number, 0)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': sender}).topUp(receiver, 0, top_up_deposit)

    txn_hash = uraiden_instance.transact({'from': sender}).topUp(
        receiver,
        open_block_number,
        top_up_deposit
    )

    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit + top_up_deposit  # deposit

    print_gas(txn_hash, 'test_channel_topup_20', gas_used_approve)

    # Check topup event
    ev_handler.add(txn_hash, uraiden_events['topup'], checkToppedUpEvent(
        sender,
        receiver,
        open_block_number,
        top_up_deposit,
        channel_deposit + top_up_deposit)
    )
    ev_handler.check()
    def get(uraiden_instance, token_instance, deposit, sender=None, receiver=None):
        contract_type = request.param
        ev_handler = event_handler(uraiden_instance)
        gas_used_create = 0

        if not sender:
            (sender, receiver) = get_accounts(2)

        # Supply accounts with tokens
        token_instance.transact({"from": owner}).transfer(sender, deposit + 500)
        token_instance.transact({"from": owner}).transfer(receiver, 100)

        # Memorize balances for tests
        uraiden_pre_balance = token_instance.call().balanceOf(uraiden_instance.address)
        sender_pre_balance = token_instance.call().balanceOf(sender)
        receiver_pre_balance = token_instance.call().balanceOf(receiver)

        # Create channel (ERC20 or ERC223 logic)
        if contract_type == '20':
            txn_hash = token_instance.transact({"from": sender}).approve(
                uraiden_instance.address,
                deposit
            )
            gas_used_create += txn_gas(txn_hash)
            txn_hash = uraiden_instance.transact({"from": sender}).createChannelERC20(
                receiver,
                deposit
            )
            message = 'test_channel_20_create'
        else:
            txdata = receiver[2:].zfill(40)
            txdata = bytes.fromhex(txdata)
            txn_hash = token_instance.transact({"from": sender}).transfer(
                uraiden_instance.address,
                deposit,
                txdata
            )
            message = 'test_channel_223_create'

        # Check token balances post channel creation
        uraiden_balance = uraiden_pre_balance + deposit
        assert token_instance.call().balanceOf(uraiden_instance.address) == uraiden_balance
        assert token_instance.call().balanceOf(sender) == sender_pre_balance - deposit
        assert token_instance.call().balanceOf(receiver) == receiver_pre_balance

        # Check creation event
        ev_handler.add(
            txn_hash,
            uraiden_events['created'],
            checkCreatedEvent(sender, receiver, deposit)
        )
        ev_handler.check()

        open_block_number = get_block(txn_hash)
        channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
        assert channel_data[0] == uraiden_instance.call().getKey(
            sender,
            receiver,
            open_block_number
        )
        assert channel_data[1] == deposit
        assert channel_data[2] == 0
        assert channel_data[3] == 0

        print_gas(txn_hash, message, gas_used_create)

        return (sender, receiver, open_block_number)
Exemple #15
0
def test_withdraw_state(contract_params, channel_params, uraiden_instance,
                        token_instance, get_channel, get_block, print_gas):
    (sender, receiver, open_block_number) = get_channel()[:3]
    deposit = channel_params['deposit']
    balance1 = 20
    balance2 = deposit

    balance_message_hash = balance_proof_hash(receiver, open_block_number,
                                              balance1,
                                              uraiden_instance.address)
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    assert is_same_address(addr, sender)

    # Memorize balances for tests
    uraiden_pre_balance = token_instance.call().balanceOf(
        uraiden_instance.address)
    sender_pre_balance = token_instance.call().balanceOf(sender)
    receiver_pre_balance = token_instance.call().balanceOf(receiver)

    txn_hash = uraiden_instance.transact({
        "from": receiver
    }).withdraw(open_block_number, balance1, balance_msg_sig)

    # Check channel info
    channel_info = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    # deposit
    assert channel_info[1] == deposit
    assert channel_info[2] == 0
    assert channel_info[3] == 0
    assert channel_info[4] == balance1

    # Check token balances post withrawal
    uraiden_balance = uraiden_pre_balance - balance1
    assert token_instance.call().balanceOf(
        uraiden_instance.address) == uraiden_balance
    assert token_instance.call().balanceOf(sender) == sender_pre_balance
    assert token_instance.call().balanceOf(
        receiver) == receiver_pre_balance + balance1

    print_gas(txn_hash, 'withdraw')

    balance_message_hash = balance_proof_hash(receiver, open_block_number,
                                              balance2,
                                              uraiden_instance.address)
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    assert is_same_address(addr, sender)

    txn_hash = uraiden_instance.transact({
        "from": receiver
    }).withdraw(open_block_number, balance2, balance_msg_sig)

    channel_info = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    # deposit
    assert channel_info[1] == deposit
    assert channel_info[2] == 0
    assert channel_info[3] == 0
    assert channel_info[4] == balance2

    # Check token balances post withrawal
    uraiden_balance = uraiden_pre_balance - deposit
    assert token_instance.call().balanceOf(
        uraiden_instance.address) == uraiden_balance
    assert token_instance.call().balanceOf(sender) == sender_pre_balance
    assert token_instance.call().balanceOf(
        receiver) == receiver_pre_balance + deposit

    print_gas(txn_hash, 'withdraw')
Exemple #16
0
def test_close_by_receiver(get_accounts, uraiden_instance, token_instance,
                           get_channel, print_gas, event_handler):
    token = token_instance
    (sender, receiver, A) = get_accounts(3)
    ev_handler = event_handler(uraiden_instance)
    channel_deposit = 800
    top_up_deposit = 14

    channel = get_channel(uraiden_instance, token_instance, channel_deposit,
                          sender, receiver)
    (sender, receiver, open_block_number) = channel

    channel_pre_close_tests(uraiden_instance, token_instance, channel,
                            top_up_deposit)

    balance = channel_deposit - 1

    balance_message_hash = balance_proof_hash(receiver, open_block_number,
                                              balance,
                                              uraiden_instance.address)
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    balance_msg_sig_false, addr2 = sign.check(balance_message_hash, tester.k4)
    assert addr == sender

    contract_verified_address = uraiden_instance.call().verifyBalanceProof(
        receiver, open_block_number, balance, balance_msg_sig)
    assert contract_verified_address == sender

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': A
        }).uncooperativeClose(receiver, open_block_number, balance,
                              balance_msg_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': receiver
        }).uncooperativeClose(receiver, open_block_number + 1, balance,
                              balance_msg_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': receiver
        }).uncooperativeClose(receiver, open_block_number, balance + 1,
                              balance_msg_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': receiver
        }).uncooperativeClose(receiver, open_block_number, balance,
                              balance_msg_sig_false)

    receiver_pre_balance = token.call().balanceOf(receiver)
    sender_pre_balance = token.call().balanceOf(sender)
    contract_pre_balance = token.call().balanceOf(uraiden_instance.address)

    txn_hash = uraiden_instance.transact({
        'from': receiver
    }).uncooperativeClose(receiver, open_block_number, balance,
                          balance_msg_sig)

    channel_deposit += top_up_deposit
    receiver_post_balance = receiver_pre_balance + balance
    sender_post_balance = sender_pre_balance + (channel_deposit - balance)
    contract_post_balance = contract_pre_balance - channel_deposit

    assert token.call().balanceOf(receiver) == receiver_post_balance
    assert token.call().balanceOf(sender) == sender_post_balance
    assert token.call().balanceOf(
        uraiden_instance.address) == contract_post_balance

    channel_settle_tests(uraiden_instance, token_instance, channel)

    # TODO:
    # with pytest.raises(Exception):
    #    ev_handler.add(txn_hash, uraiden_events['closed'], checkClosedEvent(sender, receiver, open_block_number, balance))
    ev_handler.add(
        txn_hash, uraiden_events['settled'],
        checkSettledEvent(sender, receiver, open_block_number, balance))
    ev_handler.check()

    print_gas(txn_hash, 'test_close_by_receiver')
Exemple #17
0
def test_close_by_sender_challenge_settle_by_sender2(
        web3, contract_params, get_accounts, uraiden_instance, token_instance,
        get_channel, print_gas, txn_gas, event_handler):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    channel_deposit = 800
    top_up_deposit = 14
    channel = get_channel(uraiden_instance, token_instance, channel_deposit)
    (sender, receiver, open_block_number) = channel

    balance = 0

    balance_message_hash = balance_proof_hash(receiver, open_block_number,
                                              balance,
                                              uraiden_instance.address)
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)

    channel_pre_close_tests(uraiden_instance, token_instance, channel,
                            top_up_deposit)

    txn_hash1 = uraiden_instance.transact({
        'from': sender
    }).uncooperativeClose(receiver, open_block_number, balance,
                          balance_msg_sig)

    channel_data = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    assert channel_data[2] != 0  # settle_block_number

    ev_handler.add(
        txn_hash1, uraiden_events['closed'],
        checkClosedEvent(sender, receiver, open_block_number, balance))
    ev_handler.check()

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).settle(receiver, open_block_number)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': receiver
        }).settle(receiver, open_block_number)

    web3.testing.mine(contract_params['challenge_period'] + 1)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': receiver
        }).settle(receiver, open_block_number)

    receiver_pre_balance = token.call().balanceOf(receiver)
    sender_pre_balance = token.call().balanceOf(sender)
    contract_pre_balance = token.call().balanceOf(uraiden_instance.address)

    txn_hash2 = uraiden_instance.transact({
        'from': sender
    }).settle(receiver, open_block_number)

    channel_deposit += top_up_deposit
    receiver_post_balance = receiver_pre_balance + balance
    sender_post_balance = sender_pre_balance + (channel_deposit - balance)
    contract_post_balance = contract_pre_balance - channel_deposit

    assert token.call().balanceOf(receiver) == receiver_post_balance
    assert token.call().balanceOf(sender) == sender_post_balance
    assert token.call().balanceOf(
        uraiden_instance.address) == contract_post_balance

    channel_settle_tests(uraiden_instance, token_instance, channel)

    ev_handler.add(
        txn_hash2, uraiden_events['settled'],
        checkSettledEvent(sender, receiver, open_block_number, balance))
    ev_handler.check()

    print_gas(txn_hash1, 'test_close_by_sender_challenge_settle_by_sender2',
              txn_gas(txn_hash2))
def test_withdraw_state(
        contract_params,
        channel_params,
        uraiden_instance,
        token_instance,
        get_channel,
        get_block,
        print_gas):
    (sender, receiver, open_block_number) = get_channel()[:3]
    deposit = channel_params['deposit']
    balance1 = 20
    balance2 = deposit

    balance_message_hash = balance_proof_hash(
        receiver,
        open_block_number,
        balance1,
        uraiden_instance.address
    )
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    assert is_same_address(addr, sender)

    # Memorize balances for tests
    uraiden_pre_balance = token_instance.call().balanceOf(uraiden_instance.address)
    sender_pre_balance = token_instance.call().balanceOf(sender)
    receiver_pre_balance = token_instance.call().balanceOf(receiver)

    txn_hash = uraiden_instance.transact({"from": receiver}).withdraw(
        open_block_number,
        balance1,
        balance_msg_sig
    )

    # Check channel info
    channel_info = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    # deposit
    assert channel_info[1] == deposit
    assert channel_info[2] == 0
    assert channel_info[3] == 0
    assert channel_info[4] == balance1

    # Check token balances post withrawal
    uraiden_balance = uraiden_pre_balance - balance1
    assert token_instance.call().balanceOf(uraiden_instance.address) == uraiden_balance
    assert token_instance.call().balanceOf(sender) == sender_pre_balance
    assert token_instance.call().balanceOf(receiver) == receiver_pre_balance + balance1

    print_gas(txn_hash, 'withdraw')

    balance_message_hash = balance_proof_hash(
        receiver,
        open_block_number,
        balance2,
        uraiden_instance.address
    )
    balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2)
    assert is_same_address(addr, sender)

    txn_hash = uraiden_instance.transact({"from": receiver}).withdraw(
        open_block_number,
        balance2,
        balance_msg_sig
    )

    channel_info = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    # deposit
    assert channel_info[1] == deposit
    assert channel_info[2] == 0
    assert channel_info[3] == 0
    assert channel_info[4] == balance2

    # Check token balances post withrawal
    uraiden_balance = uraiden_pre_balance - deposit
    assert token_instance.call().balanceOf(uraiden_instance.address) == uraiden_balance
    assert token_instance.call().balanceOf(sender) == sender_pre_balance
    assert token_instance.call().balanceOf(receiver) == receiver_pre_balance + deposit

    print_gas(txn_hash, 'withdraw')
def test_channel_topup_20(get_accounts, uraiden_instance, token_instance,
                          get_channel, txn_gas, event_handler, print_gas):
    token = token_instance
    ev_handler = event_handler(uraiden_instance)
    (sender, receiver, A) = get_accounts(3)
    channel_deposit = 999
    channel = get_channel(uraiden_instance, token_instance, channel_deposit,
                          sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel
    top_up_deposit = 14

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).topUp(receiver, open_block_number, top_up_deposit)

    # Approve token allowance
    txn_hash = token.transact({
        "from": sender
    }).approve(uraiden_instance.address, top_up_deposit)
    gas_used_approve = txn_gas(txn_hash)

    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).topUp(0x0, top_up_deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({
            "from": sender
        }).topUp('0x0', top_up_deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({
            "from": sender
        }).topUp(fake_address, top_up_deposit)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).topUp(receiver, -3)
    with pytest.raises(TypeError):
        uraiden_instance.transact({
            "from": sender
        }).topUp(receiver, MAX_UINT192 + 1)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': A
        }).topUp(receiver, open_block_number, top_up_deposit)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).topUp(A, open_block_number, top_up_deposit)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).topUp(receiver, open_block_number, 0)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': sender
        }).topUp(receiver, 0, top_up_deposit)

    txn_hash = uraiden_instance.transact({
        'from': sender
    }).topUp(receiver, open_block_number, top_up_deposit)

    channel_data = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit + top_up_deposit  # deposit

    print_gas(txn_hash, 'test_channel_topup_20', gas_used_approve)

    # Check topup event
    ev_handler.add(
        txn_hash, uraiden_events['topup'],
        checkToppedUpEvent(sender, receiver, open_block_number, top_up_deposit,
                           channel_deposit + top_up_deposit))
    ev_handler.check()