Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
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_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()
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
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()