Beispiel #1
0
def test_get_channel_info(web3, get_accounts, uraiden_instance, token_instance,
                          get_channel):
    (A, B, C, D) = get_accounts(4)
    channel = get_channel(uraiden_instance, token_instance, 100, C, D)[:3]
    (sender, receiver, open_block_number) = channel

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.call().getChannelInfo(sender, receiver,
                                               open_block_number - 2)
    web3.testing.mine(2)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.call().getChannelInfo(A, receiver, open_block_number)
    web3.testing.mine(2)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.call().getChannelInfo(sender, A, open_block_number)

    web3.testing.mine(2)
    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] == 100
    assert channel_data[2] == 0
    assert channel_data[3] == 0
    assert channel_data[4] == 0
Beispiel #2
0
def test_extract_closing_signature(get_accounts, token_instance,
                                   uraiden_instance):
    (A, B) = get_accounts(2)
    token = token_instance
    uraiden = uraiden_instance

    sender = '0x5601ea8445a5d96eeebf89a67c4199fbb7a43fbb'
    block = 4804175
    balance = 22000000000000000000

    message_hash = closing_message_hash(sender, block, balance,
                                        uraiden.address)
    balance_msg_sig, signer = sign.check(message_hash, tester.k2)
    assert signer == A

    signature_address = uraiden.call().extractClosingSignature(
        sender, block, balance, balance_msg_sig)
    assert signature_address == signer

    # Wrong sender
    signature_address = uraiden.call().extractClosingSignature(
        B, block, balance, balance_msg_sig)
    assert signature_address != signer

    # Wrong block
    signature_address = uraiden.call().extractClosingSignature(
        sender, 10, balance, balance_msg_sig)
    assert signature_address != signer

    # Wrong balance
    signature_address = uraiden.call().extractClosingSignature(
        sender, block, 20, balance_msg_sig)
    assert signature_address != signer
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)
Beispiel #4
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)
Beispiel #5
0
def test_create_token_fallback_uint_conversion(
    contract_params,
    owner,
    get_accounts,
    uraiden_instance,
    token_instance):
    token = token_instance
    (sender, receiver) = get_accounts(2)

    # Make sure you have a fixture with a supply > 2 ** 192 + 100
    deposit = contract_params['supply'] - 100
    txdata = bytes.fromhex(receiver[2:].zfill(40))

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, deposit)
    assert token.call().balanceOf(sender) == deposit

    # Open a channel with tokenFallback
    if deposit > 2 ** 192:
        with pytest.raises(tester.TransactionFailed):
            txn_hash = token_instance.transact({"from": sender}).transfer(
                uraiden_instance.address,
                deposit,
                txdata
            )
Beispiel #6
0
def test_channel_20_create(owner, get_accounts, uraiden_instance, token_instance):
    token = token_instance
    (sender, receiver) = get_accounts(2)
    deposit = 1000

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

    # Cannot create a channel if tokens were not approved
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).createChannelERC20(receiver, deposit)

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

    # Can create a channel with deposit 0
    uraiden_instance.transact({"from": sender}).createChannelERC20(receiver, 0)

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

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

    # Create channel
    uraiden_instance.transact({"from": sender}).createChannelERC20(receiver, deposit)
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)
Beispiel #8
0
def test_channel_223_create_bounty_limit(get_block, owner, get_accounts, uraiden_instance, token_instance):
    token = token_instance
    (sender, receiver, C, D) = get_accounts(4)
    txdata = bytes.fromhex(receiver[2:].zfill(40))

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

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

    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            channel_deposit_bugbounty_limit + 1,
            txdata
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            channel_deposit_bugbounty_limit + 10,
            txdata
        )

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

    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
Beispiel #9
0
def test_verifyBalanceProof(get_accounts, token_instance, uraiden_instance):
    (A, B) = get_accounts(2)
    token = token_instance
    uraiden = uraiden_instance

    receiver = '0x5601ea8445a5d96eeebf89a67c4199fbb7a43fbb'
    block = 4804175
    balance = 22000000000000000000

    message_hash = sign.eth_signed_typed_data_message(
        ('address', ('uint', 32), ('uint', 192), 'address'),
        ('receiver', 'block_created', 'balance', 'contract'),
        (receiver, block, balance, uraiden.address))
    balance_msg_sig, signer = sign.check(message_hash, tester.k2)
    assert signer == A

    signature_address = uraiden.call().verifyBalanceProof(
        receiver, block, balance, balance_msg_sig)
    assert signature_address == signer

    # Wrong receiver
    signature_address = uraiden.call().verifyBalanceProof(
        B, block, balance, balance_msg_sig)
    assert signature_address != signer

    # Wrong block
    signature_address = uraiden.call().verifyBalanceProof(
        receiver, 10, balance, balance_msg_sig)
    assert signature_address != signer

    # Wrong balance
    signature_address = uraiden.call().verifyBalanceProof(
        receiver, block, 20, balance_msg_sig)
    assert signature_address != signer
Beispiel #10
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_create_token_fallback_uint_conversion(
        owner,
        get_accounts,
        uraiden_contract,
        get_token_contract):
    token = get_token_contract([MAX_UINT192 + 100, 'CustomToken', 'TKN', 18])
    uraiden = uraiden_contract(token)
    (sender, receiver) = get_accounts(2)
    txdata = bytes.fromhex(sender[2:] + receiver[2:])

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, MAX_UINT192 + 5)
    assert token.call().balanceOf(sender) == MAX_UINT192 + 5

    # Open a channel with tokenFallback
    # uint192 deposit = uint192(_deposit), where _deposit is uint256
    with pytest.raises(tester.TransactionFailed):
        token.transact({"from": sender}).transfer(
            uraiden.address,
            MAX_UINT192 + 1,
            txdata
        )
    with pytest.raises(tester.TransactionFailed):
        token.transact({"from": sender}).transfer(
            uraiden.address,
            MAX_UINT192 + 4,
            txdata
        )
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')
Beispiel #13
0
def test_trusted_contracts_constructor(owner, get_accounts,
                                       get_uraiden_contract, uraiden_contract,
                                       token_instance, delegate_contract,
                                       contract_params):
    trusted_contract = delegate_contract()
    trusted_contract2 = delegate_contract()
    other_contract = delegate_contract()
    simple_account = get_accounts(1)[0]
    uraiden = uraiden_contract(token_instance, [trusted_contract.address])

    assert uraiden.call().trusted_contracts(trusted_contract.address)
    assert not uraiden.call().trusted_contracts(other_contract.address)

    with pytest.raises(TypeError):
        get_uraiden_contract([token_instance.address, challenge_period_min])
    with pytest.raises(TypeError):
        get_uraiden_contract(
            [token_instance.address, challenge_period_min, [fake_address]])

    uraiden2 = get_uraiden_contract([
        token_instance.address, challenge_period_min,
        [trusted_contract2.address, empty_address, simple_account]
    ])
    assert uraiden2.call().trusted_contracts(trusted_contract2.address)
    assert not uraiden2.call().trusted_contracts(empty_address)
    assert not uraiden2.call().trusted_contracts(simple_account)
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')
Beispiel #15
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
    )
Beispiel #16
0
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
        )
Beispiel #17
0
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_channel_erc20_create_delegate(
        owner,
        get_accounts,
        uraiden_instance,
        token_instance,
        delegate_instance,
        get_block):
    (sender, receiver) = get_accounts(2)
    deposit = 1000

    # Delegate contract is a trusted contract
    assert uraiden_instance.call().trusted_contracts(delegate_instance.address)

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

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

    # Make sure the channel was created between sender and receiver
    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
def test_channel_erc20_create(owner, get_accounts, uraiden_instance, token_instance):
    token = token_instance
    (sender, receiver) = get_accounts(2)
    deposit = 1000

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

    # Cannot create a channel if tokens were not approved
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({"from": sender}).createChannel(receiver, deposit)

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

    # Can create a channel with deposit 0
    uraiden_instance.transact({"from": sender}).createChannel(receiver, 0)

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

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

    # Create channel
    uraiden_instance.transact({"from": sender}).createChannel(receiver, deposit)
Beispiel #20
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)
def test_trusted_contracts_constructor(
        owner,
        get_accounts,
        get_uraiden_contract,
        uraiden_contract,
        token_instance,
        delegate_contract,
        contract_params):
    trusted_contract = delegate_contract()
    trusted_contract2 = delegate_contract()
    other_contract = delegate_contract()
    simple_account = get_accounts(1)[0]
    uraiden = uraiden_contract(token_instance, [trusted_contract.address])

    assert uraiden.call().trusted_contracts(trusted_contract.address)
    assert not uraiden.call().trusted_contracts(other_contract.address)

    with pytest.raises(TypeError):
        get_uraiden_contract([token_instance.address, challenge_period_min])
    with pytest.raises(TypeError):
        get_uraiden_contract([token_instance.address, challenge_period_min, [fake_address]])

    uraiden2 = get_uraiden_contract([
        token_instance.address,
        challenge_period_min,
        [trusted_contract2.address, empty_address, simple_account]
    ])
    assert uraiden2.call().trusted_contracts(trusted_contract2.address)
    assert not uraiden2.call().trusted_contracts(empty_address)
    assert not uraiden2.call().trusted_contracts(simple_account)
Beispiel #22
0
def test_channel_erc20_create_delegate(owner, get_accounts, uraiden_instance,
                                       token_instance, delegate_instance,
                                       get_block):
    (sender, receiver) = get_accounts(2)
    deposit = 1000

    # Delegate contract is a trusted contract
    assert uraiden_instance.call().trusted_contracts(delegate_instance.address)

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

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

    # Make sure the channel was created between sender and receiver
    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
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)
Beispiel #24
0
def test_version(web3, owner, get_accounts, get_uraiden_contract,
                 uraiden_instance, token_instance):
    (A, B) = get_accounts(2)
    token = token_instance
    other_contract = get_uraiden_contract(
        [token.address, challenge_period_min], {'from': A})

    assert uraiden_instance.call().version() == uraiden_contract_version
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)
def test_add_trusted_contracts_call(owner, get_accounts, uraiden_instance, delegate_contract):
    (A, B) = get_accounts(2)
    trusted_contract = delegate_contract()

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

    uraiden_instance.transact({'from': owner}).addTrustedContracts([])
    uraiden_instance.transact({'from': owner}).addTrustedContracts([empty_address])
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()
def test_channel_create_state(
        owner,
        channel_params,
        get_accounts,
        uraiden_instance,
        token_instance,
        get_block):
    token = token_instance
    uraiden = uraiden_instance
    (sender, receiver) = get_accounts(2)
    deposit = channel_params['deposit']
    contract_type = channel_params['type']

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

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

    if contract_type == '20':
        token.transact({"from": sender}).approve(
            uraiden.address,
            deposit
        )
        txn_hash = uraiden.transact({"from": sender}).createChannel(
            receiver,
            deposit
        )
    else:
        txdata = bytes.fromhex(sender[2:] + receiver[2:])
        txn_hash = token.transact({"from": sender}).transfer(
            uraiden.address,
            deposit,
            txdata
        )

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

    open_block_number = get_block(txn_hash)
    channel_data = uraiden.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[0] == uraiden.call().getKey(
        sender,
        receiver,
        open_block_number
    )
    assert channel_data[1] == deposit
    assert channel_data[2] == 0
    assert channel_data[3] == 0
Beispiel #30
0
def test_function_access(
    owner,
    get_accounts,
    uraiden_contract,
    uraiden_instance,
    token_instance,
    get_channel):
    (A, B, C, D) = get_accounts(4)
    uraiden_instance2 = uraiden_contract()
    channel = get_channel(uraiden_instance, token_instance, 100, A, B)[:3]
    (sender, receiver, open_block_number) = channel

    uraiden_instance.call().getKey(*channel)
    uraiden_instance.call().getChannelInfo(*channel)

    # even if TransactionFailed , this means the function is public / external
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().extractBalanceProofSignature(
            receiver,
            open_block_number,
            10,
            encode_hex(bytearray(65))
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().tokenFallback(sender, 10, encode_hex(bytearray(20)))
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': C}).createChannel(D, 10)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().topUp(receiver, open_block_number, 10)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().uncooperativeClose(receiver, open_block_number, 10)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().cooperativeClose(
            receiver,
            open_block_number,
            10,
            encode_hex(bytearray(65)),
            encode_hex(bytearray(65))
        )
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().settle(receiver, open_block_number)

    # Test functions are private
    # raise ValueError("No matching functions found")
    with pytest.raises(ValueError):
        uraiden_instance.transact().createChannelPrivate(*channel)
    with pytest.raises(ValueError):
        uraiden_instance.transact().topUpPrivate(*channel, 10)
    with pytest.raises(ValueError):
        uraiden_instance.transact().initChallengePeriod(receiver, open_block_number, 10)
    with pytest.raises(ValueError):
        uraiden_instance.transact().settleChannel(*channel, 10)
Beispiel #31
0
def test_add_trusted_contracts_event(owner, get_accounts, uraiden_instance,
                                     delegate_contract, event_handler):
    (A, B) = get_accounts(2)
    ev_handler = event_handler(uraiden_instance)
    trusted_contract = delegate_contract()

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

    ev_handler.add(txn_hash, uraiden_events['trusted'],
                   checkTrustedEvent(trusted_contract.address, True))
    ev_handler.check()
Beispiel #32
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)
Beispiel #33
0
def test_add_trusted_contracts_call(owner, get_accounts, uraiden_instance,
                                    delegate_contract):
    (A, B) = get_accounts(2)
    trusted_contract = delegate_contract()

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

    uraiden_instance.transact({'from': owner}).addTrustedContracts([])
    uraiden_instance.transact({
        'from': owner
    }).addTrustedContracts([empty_address])
Beispiel #34
0
def test_version(
    web3,
    owner,
    get_accounts,
    get_uraiden_contract,
    uraiden_instance,
    token_instance):
    (A, B) = get_accounts(2)
    token = token_instance
    other_contract = get_uraiden_contract(
        [token.address, challenge_period_min, []],
        {'from': A}
    )

    assert uraiden_instance.call().version() == uraiden_contract_version
def test_add_trusted_contracts_event(owner, get_accounts, uraiden_instance, delegate_contract, event_handler):
    (A, B) = get_accounts(2)
    ev_handler = event_handler(uraiden_instance)
    trusted_contract = delegate_contract()

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

    ev_handler.add(
        txn_hash,
        uraiden_events['trusted'],
        checkTrustedEvent(trusted_contract.address, True)
    )
    ev_handler.check()
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)
def test_channel_topup_223_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

    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(8)
    top_up_data = bytes.fromhex(top_up_data)

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

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

    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            added_deposit + 1,
            top_up_data
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            added_deposit + 20,
            top_up_data
        )

    token_instance.transact({"from": sender}).transfer(
        uraiden_instance.address,
        added_deposit,
        top_up_data
    )

    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_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_extract_closing_signature(get_accounts, token_instance, uraiden_instance):
    (A, B) = get_accounts(2)
    token = token_instance
    uraiden = uraiden_instance

    sender = '0x5601Ea8445A5d96EEeBF89A67C4199FbB7a43Fbb'
    block = 4804175
    balance = 22000000000000000000

    message_hash = closing_message_hash(sender, block, balance, uraiden.address)
    balance_msg_sig, signer = sign.check(message_hash, tester.k2)
    assert is_same_address(signer, A)

    signature_address = uraiden.call().extractClosingSignature(
        sender,
        block,
        balance,
        balance_msg_sig
    )
    assert is_same_address(signature_address, signer)

    # Wrong sender
    signature_address = uraiden.call().extractClosingSignature(
        B,
        block,
        balance,
        balance_msg_sig
    )
    assert not is_same_address(signature_address, signer)

    # Wrong block
    signature_address = uraiden.call().extractClosingSignature(
        sender,
        10,
        balance,
        balance_msg_sig
    )
    assert not is_same_address(signature_address, signer)

    # Wrong balance
    signature_address = uraiden.call().extractClosingSignature(
        sender,
        block,
        20,
        balance_msg_sig
    )
    assert not is_same_address(signature_address, signer)
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
        )
Beispiel #41
0
def test_function_access(owner, get_accounts, uraiden_contract,
                         uraiden_instance, token_instance, get_channel):
    (A, B, C, D) = get_accounts(4)
    uraiden_instance2 = uraiden_contract()
    channel = get_channel(uraiden_instance, token_instance, 100, A, B)
    (sender, receiver, open_block_number) = channel

    uraiden_instance.call().getKey(*channel)
    uraiden_instance.call().getChannelInfo(*channel)

    # even if TransactionFailed , this means the function is public / external
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().verifyBalanceProof(receiver,
                                                       open_block_number, 10,
                                                       bytearray(65))
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().tokenFallback(sender, 10, bytearray(20))
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact({'from': C}).createChannelERC20(D, 10)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().topUpERC20(receiver, open_block_number, 10)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().uncooperativeClose(receiver,
                                                       open_block_number, 10,
                                                       bytearray(65))
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().cooperativeClose(receiver,
                                                     open_block_number, 10,
                                                     bytearray(65),
                                                     bytearray(65))
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.transact().settle(receiver, open_block_number)

    # Test functions are private
    # raise ValueError("No matching functions found")
    with pytest.raises(ValueError):
        uraiden_instance.transact().createChannelPrivate(*channel)
    with pytest.raises(ValueError):
        uraiden_instance.transact().topUpPrivate(*channel, 10)
    with pytest.raises(ValueError):
        uraiden_instance.transact().initChallengePeriod(
            receiver, open_block_number, 10)
    with pytest.raises(ValueError):
        uraiden_instance.transact().settleChannel(*channel, 10)
Beispiel #42
0
def test_uraiden_init(
    web3,
    owner,
    get_accounts,
    get_uraiden_contract,
    token_contract,
    uraiden_contract):
    token = token_contract()
    fake_token = uraiden_contract()
    (A, B) = get_accounts(2)

    with pytest.raises(TypeError):
        get_uraiden_contract([token.address])
    with pytest.raises(TypeError):
        get_uraiden_contract([token.address, 500])
    with pytest.raises(TypeError):
        get_uraiden_contract([fake_address, challenge_period_min, []])
    with pytest.raises(TypeError):
        get_uraiden_contract([token.address, -2, []])
    with pytest.raises(TypeError):
        get_uraiden_contract([token.address, 2 ** 32, []])
    with pytest.raises(TypeError):
        get_uraiden_contract([0x0, challenge_period_min, []])
    with pytest.raises(tester.TransactionFailed):
        get_uraiden_contract([empty_address, challenge_period_min, []])
    with pytest.raises(tester.TransactionFailed):
        get_uraiden_contract([A, challenge_period_min, []])
    with pytest.raises(tester.TransactionFailed):
        get_uraiden_contract([token.address, 0, []])
    with pytest.raises(tester.TransactionFailed):
        get_uraiden_contract([token.address, challenge_period_min - 1, []])
    with pytest.raises(tester.TransactionFailed):
        get_uraiden_contract([fake_token.address, challenge_period_min, []])

    uraiden = get_uraiden_contract([token.address, 2 ** 32 - 1, []])
    assert is_same_address(uraiden.call().owner_address(), owner)
    assert is_same_address(uraiden.call().token(), token.address)
    assert uraiden.call().challenge_period() == 2 ** 32 - 1
    assert token.call().balanceOf(uraiden.address) == 0
    assert web3.eth.getBalance(uraiden.address) == 0

    # Temporary limit for the bug bounty release
    assert uraiden.call().channel_deposit_bugbounty_limit() == channel_deposit_bugbounty_limit
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()
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_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_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_topup_223_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

    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(
        8)
    top_up_data = bytes.fromhex(top_up_data)

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

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

    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, added_deposit + 1, top_up_data)
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({
            "from": sender
        }).transfer(uraiden_instance.address, added_deposit + 20, top_up_data)

    token_instance.transact({
        "from": sender
    }).transfer(uraiden_instance.address, added_deposit, top_up_data)

    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
Beispiel #48
0
def test_token_mint(web3, token_contract, contract_params, get_accounts):
    decimals = contract_params['decimals']
    multiplier = 10**(decimals)
    supply = 10000 * multiplier
    (A, B) = get_accounts(2)
    token = token_contract()
    supply = token.call().totalSupply()
    token_pre_balance = web3.eth.getBalance(token.address)

    with pytest.raises(TypeError):
        token.transact({'from': A}).mint(-3)

    with pytest.raises(tester.TransactionFailed):
        token.transact({'from': A}).mint()

    wei_value = 10**17 + 21000
    tokens = 50 * multiplier;
    token.transact({'from': A, 'value': wei_value}).mint()
    assert token.call().balanceOf(A) == tokens
    assert token.call().totalSupply() == supply + tokens
    assert web3.eth.getBalance(token.address) == token_pre_balance + wei_value
Beispiel #49
0
def test_get_channel_info(web3, get_accounts, uraiden_instance, token_instance, get_channel):
    (A, B, C, D) = get_accounts(4)
    channel = get_channel(uraiden_instance, token_instance, 100, C, D)[:3]
    (sender, receiver, open_block_number) = channel

    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number-2)
    web3.testing.mine(2)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.call().getChannelInfo(A, receiver, open_block_number)
    web3.testing.mine(2)
    with pytest.raises(tester.TransactionFailed):
        uraiden_instance.call().getChannelInfo(sender, A, open_block_number)

    web3.testing.mine(2)
    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] == 100
    assert channel_data[2] == 0
    assert channel_data[3] == 0
    assert channel_data[4] == 0
def test_channel_erc223_create_bounty_limit(
        get_block,
        owner,
        get_accounts,
        uraiden_instance,
        token_instance):
    token = token_instance
    (sender, receiver, C, D) = get_accounts(4)
    txdata = bytes.fromhex(sender[2:] + receiver[2:])

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

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

    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            channel_deposit_bugbounty_limit + 1,
            txdata
        )
    with pytest.raises(tester.TransactionFailed):
        token_instance.transact({"from": sender}).transfer(
            uraiden_instance.address,
            channel_deposit_bugbounty_limit + 10,
            txdata
        )

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

    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_topup_token_fallback_uint_conversion(
    contract_params,
    owner,
    get_accounts,
    uraiden_instance,
    token_instance,
    get_block):
    token = token_instance
    (sender, receiver) = get_accounts(2)

    # Make sure you have a fixture with a supply > 2 ** 192
    supply = contract_params['supply']
    deposit = 100
    top_up_deposit = supply - 100

    txdata = bytes.fromhex(sender[2:] + receiver[2:])

    # Fund accounts with tokens
    token.transact({"from": owner}).transfer(sender, supply)
    assert token.call().balanceOf(sender) == supply

    # Open a channel with tokenFallback
    txn_hash = token_instance.transact({"from": sender}).transfer(uraiden_instance.address, deposit, txdata)
    open_block_number = get_block(txn_hash)

    assert token.call().balanceOf(uraiden_instance.address) == deposit
    channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number)
    assert channel_data[1] == deposit

    top_up_data = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(8)
    top_up_data = bytes.fromhex(top_up_data)

    # TopUp a channel with tokenFallback
    if deposit > 2 ** 192:
        with pytest.raises(tester.TransactionFailed):
            txn_hash = token_instance.transact({"from": sender}).transfer(
                uraiden_instance.address,
                top_up_deposit,
                top_up_data
            )
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)