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
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_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_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
def get(uraiden=None, token=None, deposit=None, sender=None, receiver=None, contract_type=None): deposit = deposit or channel_params['deposit'] contract_type = contract_type or channel_params['type'] balance = channel_params['balance'] uraiden = uraiden or uraiden_instance token = token or token_instance if not sender: (sender, receiver) = get_accounts(2) # Supply accounts with tokens token.transact({"from": owner}).transfer(sender, deposit + 500) token.transact({"from": owner}).transfer(receiver, 100) # Create channel (ERC20 or ERC223 logic) if contract_type == '20': token.transact({"from": sender}).approve( uraiden.address, deposit ) txn_hash = uraiden.transact({"from": sender}).createChannelERC20( receiver, deposit ) else: txdata = bytes.fromhex(receiver[2:].zfill(40)) txn_hash = token.transact({"from": sender}).transfer( uraiden.address, deposit, txdata ) open_block_number = get_block(txn_hash) 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) closing_msg_hash = closing_message_hash( sender, open_block_number, balance, uraiden_instance.address ) closing_sig, addr = sign.check(closing_msg_hash, tester.k3) return (sender, receiver, open_block_number, balance_msg_sig, closing_sig)
def get(uraiden=None, token=None, deposit=None, sender=None, receiver=None, contract_type=None): deposit = deposit or channel_params['deposit'] contract_type = contract_type or channel_params['type'] balance = channel_params['balance'] uraiden = uraiden or uraiden_instance token = token or token_instance if not sender: (sender, receiver) = get_accounts(2) # Supply accounts with tokens token.transact({"from": owner}).transfer(sender, deposit + 500) token.transact({"from": owner}).transfer(receiver, 100) # Create channel (ERC20 or ERC223 logic) 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 ) open_block_number = get_block(txn_hash) 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) closing_msg_hash = closing_message_hash( sender, open_block_number, balance, uraiden_instance.address ) closing_sig, addr = sign.check(closing_msg_hash, tester.k3) return (sender, receiver, open_block_number, balance_msg_sig, closing_sig)
def test_uncooperative_close_state(contract_params, channel_params, uraiden_instance, get_channel, get_block, print_gas): (sender, receiver, open_block_number) = get_channel()[:3] balance = channel_params['balance'] txn_hash = uraiden_instance.transact({ "from": sender }).uncooperativeClose(receiver, open_block_number, balance) channel_info = uraiden_instance.call().getChannelInfo( sender, receiver, open_block_number) # settle_block_number assert channel_info[2] == get_block( txn_hash) + contract_params['challenge_period'] # closing_balance assert channel_info[3] == balance print_gas(txn_hash, 'uncooperativeClose')
def test_channel_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_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_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_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 get(uraiden_instance, token_instance, deposit, sender=None, receiver=None): contract_type = request.param ev_handler = event_handler(uraiden_instance) gas_used_create = 0 if not sender: (sender, receiver) = get_accounts(2) # Supply accounts with tokens token_instance.transact({"from": owner}).transfer(sender, deposit + 500) token_instance.transact({"from": owner}).transfer(receiver, 100) # Memorize balances for tests uraiden_pre_balance = token_instance.call().balanceOf(uraiden_instance.address) sender_pre_balance = token_instance.call().balanceOf(sender) receiver_pre_balance = token_instance.call().balanceOf(receiver) # Create channel (ERC20 or ERC223 logic) if contract_type == '20': txn_hash = token_instance.transact({"from": sender}).approve( uraiden_instance.address, deposit ) gas_used_create += txn_gas(txn_hash) txn_hash = uraiden_instance.transact({"from": sender}).createChannelERC20( receiver, deposit ) message = 'test_channel_20_create' else: txdata = receiver[2:].zfill(40) txdata = bytes.fromhex(txdata) txn_hash = token_instance.transact({"from": sender}).transfer( uraiden_instance.address, deposit, txdata ) message = 'test_channel_223_create' # Check token balances post channel creation uraiden_balance = uraiden_pre_balance + deposit assert token_instance.call().balanceOf(uraiden_instance.address) == uraiden_balance assert token_instance.call().balanceOf(sender) == sender_pre_balance - deposit assert token_instance.call().balanceOf(receiver) == receiver_pre_balance # Check creation event ev_handler.add( txn_hash, uraiden_events['created'], checkCreatedEvent(sender, receiver, deposit) ) ev_handler.check() open_block_number = get_block(txn_hash) channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number) assert channel_data[0] == uraiden_instance.call().getKey( sender, receiver, open_block_number ) assert channel_data[1] == deposit assert channel_data[2] == 0 assert channel_data[3] == 0 print_gas(txn_hash, message, gas_used_create) return (sender, receiver, open_block_number)