def test_withdraw_fail_in_challenge_period(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = 30

    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)
    assert is_same_address(addr, sender)

    # Should fail if the channel is already in a challenge period
    uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        balance
    )

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

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": receiver}).withdraw(
            open_block_number,
            balance,
            balance_msg_sig
        )
示例#2
0
def test_delegate_remove_trusted_contract(owner, get_accounts,
                                          uraiden_instance, token_instance,
                                          delegate_instance):
    (sender, receiver) = get_accounts(2)
    deposit = 1000

    # Fund delegate with tokens
    token_instance.transact({
        "from": owner
    }).transfer(delegate_instance.address, deposit * 3)

    # Create channel through delegate
    delegate_instance.transact({
        "from": sender
    }).createChannelERC20(sender, receiver, deposit)

    # Remove trusted contract
    uraiden_instance.transact({
        "from": owner
    }).removeTrustedContracts([delegate_instance.address])

    # Delegate create channel should fail now
    with pytest.raises(tester.TransactionFailed):
        delegate_instance.transact({
            "from": sender
        }).createChannelERC20(sender, receiver, deposit)
def test_cooperative_close_call_delegate(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel):
    A = get_accounts(1, 5)[0]
    (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
    uraiden_instance.transact({"from": A}).cooperativeClose(
        receiver,
        open_block_number,
        balance,
        balance_msg_sig,
        closing_sig
    )
def test_uncooperative_close_fail_no_channel(channel_params, get_accounts,
                                             uraiden_instance, get_channel):
    A = get_accounts(1, 5)[0]
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    # Should fail if called by anyone else than the sender
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": receiver
        }).uncooperativeClose(receiver, open_block_number, balance)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": A
        }).uncooperativeClose(receiver, open_block_number, balance)

    # Should fail if the channel does not exist
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).uncooperativeClose(A, open_block_number, balance)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).uncooperativeClose(receiver, open_block_number - 1, balance)

    uraiden_instance.transact({
        "from": sender
    }).uncooperativeClose(receiver, open_block_number, balance)
def test_cooperative_close_fail_diff_receiver(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel):
    A = get_accounts(1, 5)[0]
    (sender, receiver, open_block_number, balance_msg_sig, closing_sig) = get_channel()
    balance = channel_params['balance']

    balance_message_hash_A = balance_proof_hash(
        A,
        open_block_number,
        balance,
        uraiden_instance.address
    )
    balance_msg_sig_A, addr = sign.check(balance_message_hash_A, tester.k2)

    # Should fail if someone tries to use a closing signature from another receiver
    # with the same sender, block, balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).cooperativeClose(
            A,
            open_block_number,
            balance,
            balance_msg_sig_A,
            closing_sig
        )
def test_uncooperative_close_fail_in_challenge_period(
        channel_params,
        get_accounts,
        uraiden_instance,
        token_instance,
        get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    # Should fail if the channel is already in a challenge period
    uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        balance
    )

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

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            receiver,
            open_block_number,
            balance
        )
def test_settle_no_channel(web3, contract_params, channel_params,
                           uraiden_instance, get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

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

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

    uraiden_instance.transact({
        "from": sender
    }).settle(receiver, open_block_number)
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_settle_no_channel(
        web3,
        contract_params,
        channel_params,
        uraiden_instance,
        get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

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

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

    uraiden_instance.transact({"from": sender}).settle(receiver, open_block_number)
def test_settle_event(
        web3,
        channel_params,
        contract_params,
        uraiden_instance,
        get_channel,
        event_handler):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    ev_handler = event_handler(uraiden_instance)

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

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

    ev_handler.add(txn_hash, uraiden_events['settled'], checkSettledEvent(
        sender,
        receiver,
        open_block_number,
        balance,
        balance)
    )
    ev_handler.check()
def test_add_trusted_contracts_only_owner(owner, get_accounts, uraiden_instance, delegate_contract):
    (A, B) = get_accounts(2)
    trusted_contract = delegate_contract()

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': A}).addTrustedContracts([trusted_contract.address])

    uraiden_instance.transact({'from': owner}).addTrustedContracts([trusted_contract.address])
    assert uraiden_instance.call().trusted_contracts(trusted_contract.address)
def test_channel_erc223_create(owner, get_accounts, uraiden_instance,
                               token_instance):
    token = token_instance
    (sender, receiver, C, D) = get_accounts(4)
    deposit = 1000
    txdata = bytes.fromhex(receiver[2:].zfill(40))
    txdata_fake = txdata[1:]

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

    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(0x0, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(fake_address, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, -2, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, MAX_UINT256 + 1, txdata)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(empty_address, deposit, txdata)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, deposit, bytearray(10))
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, deposit, txdata_fake)

    # tokenFallback only callable by token
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': C
        }).tokenFallback(sender, 10, txdata)

    assert token_instance.call().balanceOf(uraiden_instance.address) == 0

    # Deposit 0 is possible now
    token_instance.transact({
        "from": sender
    }).transfer(uraiden_instance.address, 0, txdata)

    token_instance.transact({
        "from": sender
    }).transfer(uraiden_instance.address, deposit, txdata)
示例#13
0
def test_cooperative_close_state(
        web3,
        contract_params,
        channel_params,
        uraiden_instance,
        token_instance,
        get_channel,
        get_block):
    (sender, receiver, open_block_number, balance_msg_sig, closing_sig) = get_channel()
    balance = channel_params['balance']
    deposit = channel_params['deposit']

    # 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)

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

    # 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": receiver}).cooperativeClose(
            receiver,
            open_block_number,
            balance,
            balance_msg_sig,
            closing_sig
        )
示例#14
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')
示例#15
0
def test_add_trusted_contracts_only_owner(owner, get_accounts,
                                          uraiden_instance, delegate_contract):
    (A, B) = get_accounts(2)
    trusted_contract = delegate_contract()

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            'from': A
        }).addTrustedContracts([trusted_contract.address])

    uraiden_instance.transact({
        'from': owner
    }).addTrustedContracts([trusted_contract.address])
    assert uraiden_instance.call().trusted_contracts(trusted_contract.address)
def test_uncooperative_close_fail_big_balance(channel_params, get_accounts,
                                              uraiden_instance, token_instance,
                                              get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    deposit = channel_params['deposit']

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).uncooperativeClose(receiver, open_block_number, deposit + 1)

    uraiden_instance.transact({
        "from": sender
    }).uncooperativeClose(receiver, open_block_number, deposit)
示例#17
0
def test_close_call(get_accounts, uraiden_instance, token_instance,
                    get_channel):
    (sender, receiver, A) = get_accounts(3)
    channel_deposit = 450
    channel = get_channel(uraiden_instance, token_instance, channel_deposit,
                          sender, receiver)
    (sender, receiver, open_block_number) = channel
    balance = channel_deposit - 10

    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)

    # Cannot close what was not opened
    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': sender
        }).uncooperativeClose(A, open_block_number, balance, balance_msg_sig)

    # Cannot close if arguments not correct
    with pytest.raises(ValueError):
        uraiden_instance.transact({
            'from': sender
        }).initChallengePeriod(receiver, open_block_number, balance)
    with pytest.raises(ValueError):
        uraiden_instance.transact({
            'from': receiver
        }).settleChannel(sender, receiver, open_block_number, balance)
示例#18
0
def test_close_after_withdraw(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_hash1 = balance_proof_hash(receiver, open_block_number,
                                               balance1,
                                               uraiden_instance.address)
    balance_msg_sig1, addr = sign.check(balance_message_hash1, tester.k2)
    assert is_same_address(addr, sender)

    # Withdraw some tokens
    txn_hash = uraiden_instance.transact({
        "from": receiver
    }).withdraw(open_block_number, balance1, balance_msg_sig1)

    # Cooperatively close the channel
    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)

    closing_msg_hash = closing_message_hash(sender, open_block_number,
                                            balance2, uraiden_instance.address)
    closing_sig, addr = sign.check(closing_msg_hash, tester.k3)

    # 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)

    uraiden_instance.transact({
        "from": receiver
    }).cooperativeClose(receiver, open_block_number, balance2, balance_msg_sig,
                        closing_sig)

    # Check post closing balances
    receiver_post_balance = receiver_pre_balance + (balance2 - balance1)
    sender_post_balance = sender_pre_balance + (deposit - balance2)
    uraiden_post_balance = uraiden_pre_balance - (balance2 - balance1)

    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) == uraiden_post_balance
def test_channel_erc223_create(owner, get_accounts, uraiden_instance, token_instance):
    token = token_instance
    (sender, receiver, C, D) = get_accounts(4)
    deposit = 1000
    txdata = bytes.fromhex(sender[2:] + receiver[2:])
    txdata_fake = txdata[1:]

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

    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(0x0, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(fake_address, deposit, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(uraiden_instance.address, -2, txdata)
    with pytest.raises(TypeError):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            MAX_UINT256 + 1,
            txdata
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(empty_address, deposit, txdata)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            deposit,
            encode_hex(bytearray(10))
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            deposit,
            txdata_fake
        )

    # tokenFallback only callable by token
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': C}).tokenFallback(sender, 10, txdata)

    assert token_instance.call().balanceOf(uraiden_instance.address) == 0

    # Deposit 0 is possible now
    token_instance.transact({"from": sender}).transfer(uraiden_instance.address, 0, txdata)

    token_instance.transact({"from": sender}).transfer(uraiden_instance.address, deposit, txdata)
示例#20
0
def test_cooperative_close_event(
        channel_params,
        uraiden_instance,
        get_channel,
        event_handler):
    (sender, receiver, open_block_number, balance_msg_sig, closing_sig) = get_channel()
    balance = channel_params['balance']

    ev_handler = event_handler(uraiden_instance)

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

    ev_handler.add(txn_hash, uraiden_events['settled'], checkClosedEvent(
        sender,
        receiver,
        open_block_number,
        balance)
    )
    ev_handler.check()
def test_withdraw_event(
        channel_params,
        uraiden_instance,
        get_channel,
        event_handler):
    (sender, receiver, open_block_number) = get_channel()[:3]
    ev_handler = event_handler(uraiden_instance)
    balance = 30

    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)
    assert is_same_address(addr, sender)

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

    ev_handler.add(txn_hash, uraiden_events['withdraw'], checkWithdrawEvent(
        sender,
        receiver,
        open_block_number,
        balance)
    )
    ev_handler.check()
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_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)
示例#24
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_cooperative_close_fail_diff_receiver(channel_params, get_accounts,
                                              uraiden_instance, get_channel):
    A = get_accounts(1, 5)[0]
    (sender, receiver, open_block_number, balance_msg_sig,
     closing_sig) = get_channel()
    balance = channel_params['balance']

    balance_message_hash_A = balance_proof_hash(A, open_block_number, balance,
                                                uraiden_instance.address)
    balance_msg_sig_A, addr = sign.check(balance_message_hash_A, tester.k2)

    # Should fail if someone tries to use a closing signature from another receiver
    # with the same sender, block, balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).cooperativeClose(A, open_block_number, balance, balance_msg_sig_A,
                            closing_sig)
def test_uncooperative_close_uint32_overflow(web3, channel_params,
                                             get_uraiden_contract,
                                             token_instance, get_channel):
    challenge_period = MAX_UINT32 - 20
    uraiden_instance = get_uraiden_contract(
        [token_instance.address, challenge_period])

    (sender, receiver, open_block_number) = get_channel(uraiden_instance)[:3]
    balance = channel_params['balance']

    block_number = web3.eth.getBlock('latest')['number']
    web3.testing.mine(MAX_UINT32 - 1 - block_number - challenge_period)

    assert web3.eth.getBlock(
        'latest')['number'] + challenge_period == MAX_UINT32 - 1

    uraiden_instance.transact({
        "from": sender
    }).uncooperativeClose(receiver, open_block_number, balance)
def test_cooperative_close_call_sender(channel_params, get_accounts,
                                       uraiden_instance, get_channel):
    (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 sender_verified == sender

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

    # Cooperative close can be called by anyone
    uraiden_instance.transact({
        "from": sender
    }).cooperativeClose(receiver, open_block_number, balance, balance_msg_sig,
                        closing_sig)
示例#28
0
def test_remove_trusted_contracts_event(owner, get_accounts, uraiden_instance,
                                        delegate_contract, event_handler):
    (A, B) = get_accounts(2)
    ev_handler = event_handler(uraiden_instance)
    trusted_contract1 = delegate_contract()
    trusted_contract2 = delegate_contract()

    uraiden_instance.transact({
        'from': owner
    }).addTrustedContracts(
        [trusted_contract1.address, trusted_contract2.address])

    txn_hash = uraiden_instance.transact({
        'from': owner
    }).removeTrustedContracts([trusted_contract1.address])

    ev_handler.add(txn_hash, uraiden_events['trusted'],
                   checkTrustedEvent(trusted_contract1.address, False))
    ev_handler.check()
def test_uncooperative_close_fail_no_channel(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel):
    A = get_accounts(1, 5)[0]
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    # Should fail if called by anyone else than the sender
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": receiver}).uncooperativeClose(
            receiver,
            open_block_number,
            balance
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": A}).uncooperativeClose(
            receiver,
            open_block_number,
            balance
        )

    # Should fail if the channel does not exist
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            A,
            open_block_number,
            balance
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            receiver,
            open_block_number - 1,
            balance
        )

    uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        balance
    )
def test_channel_erc20_create_bounty_limit(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        get_block):
    token = token_instance
    (sender, receiver) = get_accounts(2)

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

    # Approve token allowance
    token_instance.transact({"from": sender}).approve(
        uraiden_instance.address,
        channel_deposit_bugbounty_limit + 1
    )

    pre_balance = token_instance.call().balanceOf(uraiden_instance.address)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).createChannel(
            receiver,
            channel_deposit_bugbounty_limit + 1
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).createChannel(
            receiver,
            channel_deposit_bugbounty_limit + 100
        )

    txn_hash = uraiden_instance.transact({"from": sender}).createChannel(
        receiver,
        channel_deposit_bugbounty_limit
    )

    post_balance = pre_balance + channel_deposit_bugbounty_limit
    assert token_instance.call().balanceOf(uraiden_instance.address) == post_balance
    open_block_number = get_block(txn_hash)

    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit_bugbounty_limit
def test_remove_trusted_contracts_event(owner, get_accounts, uraiden_instance, delegate_contract, event_handler):
    (A, B) = get_accounts(2)
    ev_handler = event_handler(uraiden_instance)
    trusted_contract1 = delegate_contract()
    trusted_contract2 = delegate_contract()

    uraiden_instance.transact({'from': owner}).addTrustedContracts(
        [trusted_contract1.address, trusted_contract2.address]
    )

    txn_hash = uraiden_instance.transact({'from': owner}).removeTrustedContracts(
        [trusted_contract1.address]
    )

    ev_handler.add(
        txn_hash,
        uraiden_events['trusted'],
        checkTrustedEvent(trusted_contract1.address, False)
    )
    ev_handler.check()
def test_settle_event(web3, channel_params, contract_params, uraiden_instance,
                      get_channel, event_handler):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    ev_handler = event_handler(uraiden_instance)

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

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

    ev_handler.add(
        txn_hash, uraiden_events['settled'],
        checkSettledEvent(sender, receiver, open_block_number, balance))
    ev_handler.check()
def test_uncooperative_close_fail_in_challenge_period(channel_params,
                                                      get_accounts,
                                                      uraiden_instance,
                                                      token_instance,
                                                      get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    # Should fail if the channel is already in a challenge period
    uraiden_instance.transact({
        "from": sender
    }).uncooperativeClose(receiver, open_block_number, balance)

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

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).uncooperativeClose(receiver, open_block_number, balance)
def test_uncooperative_close_fail_big_balance(
        channel_params,
        get_accounts,
        uraiden_instance,
        token_instance,
        get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    deposit = channel_params['deposit']

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            receiver,
            open_block_number,
            deposit + 1
        )

    uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        deposit
    )
示例#35
0
def test_cooperative_close_fail_no_channel(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel):
    A = get_accounts(1, 5)[0]
    (sender, receiver, open_block_number, balance_msg_sig, closing_sig) = get_channel()
    balance = channel_params['balance']

    # Should fail if the channel does not exist
    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 - 1,
            balance,
            balance_msg_sig,
            closing_sig
        )

    uraiden_instance.transact({"from": sender}).cooperativeClose(
        receiver,
        open_block_number,
        balance,
        balance_msg_sig,
        closing_sig
    )
示例#36
0
def test_withdraw_fail_no_channel(channel_params, get_accounts,
                                  uraiden_instance, get_channel):
    A = get_accounts(1, 5)[0]
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = 10

    balance_message_hash_A = balance_proof_hash(A, open_block_number, balance,
                                                uraiden_instance.address)
    balance_msg_sig_A, addr = sign.check(balance_message_hash_A, tester.k5)
    assert is_same_address(addr, A)

    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)
    assert is_same_address(addr, sender)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).withdraw(open_block_number, balance, balance_msg_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": A
        }).withdraw(open_block_number, balance, balance_msg_sig)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": receiver
        }).withdraw(open_block_number, balance, balance_msg_sig_A)
def test_balance_big(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance_ok = channel_params['deposit']
    balance_big = channel_params['deposit'] + 1

    balance_message_hash_big = balance_proof_hash(
        receiver,
        open_block_number,
        balance_big,
        uraiden_instance.address
    )
    balance_msg_sig_big, addr = sign.check(balance_message_hash_big, tester.k2)
    assert is_same_address(addr, sender)

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

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": receiver}).withdraw(
            open_block_number,
            balance_big,
            balance_msg_sig_big
        )

    uraiden_instance.transact({"from": receiver}).withdraw(
        open_block_number,
        balance_ok,
        balance_msg_sig
    )
示例#38
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_delegate_remove_trusted_contract(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        delegate_instance):
    (sender, receiver) = get_accounts(2)
    deposit = 1000

    # Fund delegate with tokens
    token_instance.transact({"from": owner}).transfer(delegate_instance.address, deposit * 3)

    # Create channel through delegate
    delegate_instance.transact({"from": sender}).createChannelERC20(sender, receiver, deposit)

    # Remove trusted contract
    uraiden_instance.transact({"from": owner}).removeTrustedContracts([
        delegate_instance.address
    ])

    # Delegate create channel should fail now
    with pytest.raises(tester.TransactionFailed):
        delegate_instance.transact({"from": sender}).createChannelERC20(sender, receiver, deposit)
def test_remove_trusted_contracts_call(owner, get_accounts, uraiden_instance, delegate_contract):
    (A, B) = get_accounts(2)
    trusted_contract1 = delegate_contract()
    trusted_contract2 = delegate_contract()

    uraiden_instance.transact({'from': owner}).addTrustedContracts(
        [trusted_contract1.address, trusted_contract2.address]
    )

    with pytest.raises(TypeError):
        uraiden_instance.transact({'from': owner}).removeTrustedContracts([fake_address])

    uraiden_instance.transact({'from': owner}).removeTrustedContracts([])
    uraiden_instance.transact({'from': owner}).removeTrustedContracts(
        [empty_address, trusted_contract1.address]
    )
def test_uncooperative_close_uint32_overflow(
        web3,
        channel_params,
        get_uraiden_contract,
        token_instance,
        get_channel):
    challenge_period = MAX_UINT32 - 20
    uraiden_instance = get_uraiden_contract(
        [token_instance.address, challenge_period, []]
    )

    (sender, receiver, open_block_number) = get_channel(uraiden_instance)[:3]
    balance = channel_params['balance']

    block_number = web3.eth.getBlock('latest')['number']
    web3.testing.mine(MAX_UINT32 - 1 - block_number - challenge_period)

    assert web3.eth.getBlock('latest')['number'] + challenge_period == MAX_UINT32 - 1

    uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        balance
    )
示例#42
0
def test_withdraw_fail_in_challenge_period(channel_params, get_accounts,
                                           uraiden_instance, get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = 30

    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)
    assert is_same_address(addr, sender)

    # Should fail if the channel is already in a challenge period
    uraiden_instance.transact({
        "from": sender
    }).uncooperativeClose(receiver, open_block_number, balance)

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

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": receiver
        }).withdraw(open_block_number, balance, balance_msg_sig)
def test_cooperative_close_fail_wrong_balance(channel_params, get_accounts,
                                              uraiden_instance, token_instance,
                                              get_channel):
    (sender, receiver, open_block_number, balance_msg_sig,
     closing_sig) = get_channel()
    balance = channel_params['balance']
    deposit = channel_params['deposit']

    balance_message_hash_fake = balance_proof_hash(receiver, open_block_number,
                                                   balance - 1,
                                                   uraiden_instance.address)
    balance_msg_sig_fake, addr = sign.check(balance_message_hash_fake,
                                            tester.k2)

    closing_msg_hash_fake = closing_message_hash(sender, open_block_number,
                                                 balance - 1,
                                                 uraiden_instance.address)
    closing_sig_fake, addr = sign.check(closing_msg_hash_fake, tester.k3)

    # Wrong balance as an argument
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).cooperativeClose(receiver, open_block_number, balance + 1,
                            balance_msg_sig, closing_sig)

    # Sender signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig_fake, closing_sig)

    # Receiver signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": sender
        }).cooperativeClose(receiver, open_block_number, balance,
                            balance_msg_sig, closing_sig_fake)

    uraiden_instance.transact({
        "from": sender
    }).cooperativeClose(receiver, open_block_number, balance, balance_msg_sig,
                        closing_sig)
示例#44
0
def test_remove_trusted_contracts_call(owner, get_accounts, uraiden_instance,
                                       delegate_contract):
    (A, B) = get_accounts(2)
    trusted_contract1 = delegate_contract()
    trusted_contract2 = delegate_contract()

    uraiden_instance.transact({
        'from': owner
    }).addTrustedContracts(
        [trusted_contract1.address, trusted_contract2.address])

    with pytest.raises(TypeError):
        uraiden_instance.transact({
            'from': owner
        }).removeTrustedContracts([fake_address])

    uraiden_instance.transact({'from': owner}).removeTrustedContracts([])
    uraiden_instance.transact({
        'from': owner
    }).removeTrustedContracts([empty_address, trusted_contract1.address])
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')
示例#46
0
def test_withdraw_event(channel_params, uraiden_instance, get_channel,
                        event_handler):
    (sender, receiver, open_block_number) = get_channel()[:3]
    ev_handler = event_handler(uraiden_instance)
    balance = 30

    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)
    assert is_same_address(addr, sender)

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

    ev_handler.add(
        txn_hash, uraiden_events['withdraw'],
        checkWithdrawEvent(sender, receiver, open_block_number, balance))
    ev_handler.check()
def test_channel_topup_20_bounty_limit(
    get_accounts,
    owner,
    uraiden_instance,
    token_instance,
    get_channel):
    token = token_instance
    (sender, receiver, A) = get_accounts(3)
    channel_deposit = 1
    channel = get_channel(uraiden_instance, token_instance, channel_deposit, sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel

    # See how many tokens we need to reach channel_deposit_bugbounty_limit
    added_deposit = channel_deposit_bugbounty_limit - channel_deposit

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

    # Approve token allowance
    txn_hash = token.transact({"from": sender}).approve(uraiden_instance.address, added_deposit + 1)

    pre_balance = token.call().balanceOf(uraiden_instance.address)

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

    uraiden_instance.transact({'from': sender}).topUp(
        receiver,
        open_block_number,
        added_deposit
    )

    post_balance = pre_balance + added_deposit
    assert token_instance.call().balanceOf(uraiden_instance.address) == post_balance

    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit_bugbounty_limit
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_withdraw_fail_no_channel(
        channel_params,
        get_accounts,
        uraiden_instance,
        get_channel):
    A = get_accounts(1, 5)[0]
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = 10

    balance_message_hash_A = balance_proof_hash(
        A,
        open_block_number,
        balance,
        uraiden_instance.address
    )
    balance_msg_sig_A, addr = sign.check(balance_message_hash_A, tester.k5)
    assert is_same_address(addr, A)

    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)
    assert is_same_address(addr, sender)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).withdraw(
            open_block_number,
            balance,
            balance_msg_sig
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": A}).withdraw(
            open_block_number,
            balance,
            balance_msg_sig
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": receiver}).withdraw(
            open_block_number,
            balance,
            balance_msg_sig_A
        )
def test_channel_topup_20_bounty_limit(get_accounts, owner, uraiden_instance,
                                       token_instance, get_channel):
    token = token_instance
    (sender, receiver, A) = get_accounts(3)
    channel_deposit = 1
    channel = get_channel(uraiden_instance, token_instance, channel_deposit,
                          sender, receiver)[:3]
    (sender, receiver, open_block_number) = channel

    # See how many tokens we need to reach channel_deposit_bugbounty_limit
    added_deposit = channel_deposit_bugbounty_limit - channel_deposit

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

    # Approve token allowance
    txn_hash = token.transact({
        "from": sender
    }).approve(uraiden_instance.address, added_deposit + 1)

    pre_balance = token.call().balanceOf(uraiden_instance.address)

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

    uraiden_instance.transact({
        'from': sender
    }).topUp(receiver, open_block_number, added_deposit)

    post_balance = pre_balance + added_deposit
    assert token_instance.call().balanceOf(
        uraiden_instance.address) == post_balance

    channel_data = uraiden_instance.call().getChannelInfo(
        sender, receiver, open_block_number)
    assert channel_data[1] == channel_deposit_bugbounty_limit
示例#51
0
def test_balance_remaining_big(channel_params, get_accounts, uraiden_instance,
                               get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance1 = 30
    balance2_big = channel_params['deposit'] + 1
    balance2_ok = channel_params['deposit']

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

    balance_message_hash2_big = balance_proof_hash(receiver, open_block_number,
                                                   balance2_big,
                                                   uraiden_instance.address)
    balance_msg_sig2_big, addr = sign.check(balance_message_hash2_big,
                                            tester.k2)
    assert is_same_address(addr, sender)

    balance_message_hash2_ok = balance_proof_hash(receiver, open_block_number,
                                                  balance2_ok,
                                                  uraiden_instance.address)
    balance_msg_sig2_ok, addr = sign.check(balance_message_hash2_ok, tester.k2)
    assert is_same_address(addr, sender)

    uraiden_instance.transact({
        "from": receiver
    }).withdraw(open_block_number, balance1, balance_msg_sig1)

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({
            "from": receiver
        }).withdraw(open_block_number, balance2_big, balance_msg_sig2_big)

    uraiden_instance.transact({
        "from": receiver
    }).withdraw(open_block_number, balance2_ok, balance_msg_sig2_ok)
def test_settle_call(
        web3,
        contract_params,
        channel_params,
        uraiden_instance,
        token_instance,
        get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

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

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

    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).settle(0x0, open_block_number)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).settle(fake_address, open_block_number)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).settle(receiver, -2)
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).settle(receiver, MAX_UINT32 + 1)

    uraiden_instance.transact({"from": sender}).settle(receiver, open_block_number)
def test_uncooperative_close_call(channel_params, uraiden_instance, get_channel):
    (sender, receiver, open_block_number) = get_channel()[:3]
    balance = channel_params['balance']

    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            0x0,
            open_block_number,
            balance
        )
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            fake_address,
            open_block_number,
            balance
        )
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            receiver,
            -2,
            balance
        )
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            receiver,
            MAX_UINT32 + 1,
            balance
        )
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            receiver,
            open_block_number,
            -2
        )
    with pytest.raises(TypeError):
        uraiden_instance.transact({"from": sender}).uncooperativeClose(
            receiver,
            open_block_number,
            MAX_UINT256 + 1
        )

    uraiden_instance.transact({"from": sender}).uncooperativeClose(
        receiver,
        open_block_number,
        balance
    )
def test_cooperative_close_fail_wrong_balance(
        channel_params,
        get_accounts,
        uraiden_instance,
        token_instance,
        get_channel):
    (sender, receiver, open_block_number, balance_msg_sig, closing_sig) = get_channel()
    balance = channel_params['balance']
    deposit = channel_params['deposit']

    balance_message_hash_fake = balance_proof_hash(
        receiver,
        open_block_number,
        balance - 1,
        uraiden_instance.address
    )
    balance_msg_sig_fake, addr = sign.check(balance_message_hash_fake, tester.k2)

    closing_msg_hash_fake = closing_message_hash(
        sender,
        open_block_number,
        balance - 1,
        uraiden_instance.address
    )
    closing_sig_fake, addr = sign.check(closing_msg_hash_fake, tester.k3)

    # Wrong balance as an argument
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).cooperativeClose(
            receiver,
            open_block_number,
            balance + 1,
            balance_msg_sig,
            closing_sig
        )

    # Sender signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).cooperativeClose(
            receiver,
            open_block_number,
            balance,
            balance_msg_sig_fake,
            closing_sig
        )

    # Receiver signs wrong balance
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).cooperativeClose(
            receiver,
            open_block_number,
            balance,
            balance_msg_sig,
            closing_sig_fake
        )

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