def test_add_trusted_contracts_state(owner, get_accounts, uraiden_instance, delegate_contract, print_gas): (A, B) = get_accounts(2) trusted_contract1 = delegate_contract() trusted_contract2 = delegate_contract() trusted_contract3 = delegate_contract() trusted_contract4 = delegate_contract() assert not uraiden_instance.call().trusted_contracts(trusted_contract1.address) assert not uraiden_instance.call().trusted_contracts(trusted_contract2.address) assert not uraiden_instance.call().trusted_contracts(trusted_contract3.address) assert not uraiden_instance.call().trusted_contracts(trusted_contract4.address) uraiden_instance.transact({'from': owner}).addTrustedContracts([A]) assert not uraiden_instance.call().trusted_contracts(A) txn_hash = uraiden_instance.transact({'from': owner}).addTrustedContracts([trusted_contract1.address]) assert uraiden_instance.call().trusted_contracts(trusted_contract1.address) print_gas(txn_hash, 'add 1 trusted contract') txn_hash = uraiden_instance.transact({'from': owner}).addTrustedContracts([ trusted_contract2.address, trusted_contract3.address, A, trusted_contract4.address ]) assert uraiden_instance.call().trusted_contracts(trusted_contract2.address) assert uraiden_instance.call().trusted_contracts(trusted_contract3.address) assert uraiden_instance.call().trusted_contracts(trusted_contract4.address) assert not uraiden_instance.call().trusted_contracts(A) print_gas(txn_hash, 'add 3 trusted contracts')
def test_channel_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_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_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_topup_delegate( owner, uraiden_instance, token_instance, delegate_instance, get_channel): deposit = 1000 deposit_topup = 200 (sender, receiver, open_block_number) = get_channel(uraiden_instance, token_instance, deposit)[:3] # 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_topup) # Top up channel through delegate delegate_instance.transact({"from": sender}).topUpERC20( sender, receiver, open_block_number, deposit_topup ) # Check channel deposit channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number) assert channel_data[1] == deposit + deposit_topup
def test_channel_erc223_topup_delegate(owner, uraiden_instance, token_instance, delegate_instance, get_channel): deposit = 1000 deposit_topup = 200 (sender, receiver, open_block_number) = get_channel(uraiden_instance, token_instance, deposit)[:3] txdata = sender[2:] + receiver[2:] + hex(open_block_number)[2:].zfill(8) txdata = bytes.fromhex(txdata) # 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_topup + 100) # Top up channel through delegate delegate_instance.transact({ "from": sender }).topUpERC223(deposit_topup, txdata) # Check channel deposit channel_data = uraiden_instance.call().getChannelInfo( sender, receiver, open_block_number) assert channel_data[1] == deposit + deposit_topup
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 )
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)
def test_settle_state( web3, channel_params, contract_params, uraiden_instance, token_instance, get_channel, print_gas): (sender, receiver, open_block_number) = get_channel()[:3] balance = channel_params['balance'] deposit = channel_params['deposit'] # Trigger a challenge period uraiden_instance.transact({"from": sender}).uncooperativeClose( receiver, open_block_number, balance ) web3.testing.mine(contract_params['challenge_period'] + 1) # Keep track of pre closing balances receiver_pre_balance = token_instance.call().balanceOf(receiver) sender_pre_balance = token_instance.call().balanceOf(sender) contract_pre_balance = token_instance.call().balanceOf(uraiden_instance.address) txn_hash = uraiden_instance.transact({"from": sender}).settle(receiver, open_block_number) # Check post closing balances receiver_post_balance = receiver_pre_balance + balance sender_post_balance = sender_pre_balance + (deposit - balance) contract_post_balance = contract_pre_balance - deposit assert token_instance.call().balanceOf(receiver) == receiver_post_balance assert token_instance.call().balanceOf(sender) == sender_post_balance assert token_instance.call().balanceOf(uraiden_instance.address) == contract_post_balance channel_settle_tests(uraiden_instance, token_instance, (sender, receiver, open_block_number) ) # Channel does not exist anymore, so this will fail with pytest.raises(tester.TransactionFailed): uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number) web3.testing.mine(30) # Cannot be called another time with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({"from": sender}).settle(receiver, open_block_number) print_gas(txn_hash, 'settle')
def test_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_remove_trusted_contracts_only_owner(owner, get_accounts, uraiden_instance, delegate_contract): (A, B) = get_accounts(2) trusted_contract = delegate_contract() uraiden_instance.transact({'from': owner}).addTrustedContracts([trusted_contract.address]) assert uraiden_instance.call().trusted_contracts(trusted_contract.address) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({'from': A}).removeTrustedContracts([trusted_contract.address]) uraiden_instance.transact({'from': owner}).removeTrustedContracts( [trusted_contract.address] ) assert not uraiden_instance.call().trusted_contracts(trusted_contract.address)
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_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_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_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_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)
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)
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_remove_trusted_contracts_only_owner(owner, get_accounts, uraiden_instance, delegate_contract): (A, B) = get_accounts(2) trusted_contract = delegate_contract() uraiden_instance.transact({ 'from': owner }).addTrustedContracts([trusted_contract.address]) assert uraiden_instance.call().trusted_contracts(trusted_contract.address) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': A }).removeTrustedContracts([trusted_contract.address]) uraiden_instance.transact({ 'from': owner }).removeTrustedContracts([trusted_contract.address]) assert not uraiden_instance.call().trusted_contracts( trusted_contract.address)
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_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_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_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_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_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_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_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_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_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_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_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_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_channel_topup_20( get_accounts, uraiden_instance, token_instance, get_channel, txn_gas, event_handler, print_gas): token = token_instance ev_handler = event_handler(uraiden_instance) (sender, receiver, A) = get_accounts(3) channel_deposit = 999 channel = get_channel(uraiden_instance, token_instance, channel_deposit, sender, receiver)[:3] (sender, receiver, open_block_number) = channel top_up_deposit = 14 with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({'from': sender}).topUp( receiver, open_block_number, top_up_deposit ) # Approve token allowance txn_hash = token.transact({"from": sender}).approve(uraiden_instance.address, top_up_deposit) gas_used_approve = txn_gas(txn_hash) with pytest.raises(TypeError): uraiden_instance.transact({"from": sender}).topUp(0x0, top_up_deposit) with pytest.raises(TypeError): uraiden_instance.transact({"from": sender}).topUp('0x0', top_up_deposit) with pytest.raises(TypeError): uraiden_instance.transact({"from": sender}).topUp(fake_address, top_up_deposit) with pytest.raises(TypeError): uraiden_instance.transact({"from": sender}).topUp(receiver, -3) with pytest.raises(TypeError): uraiden_instance.transact({"from": sender}).topUp(receiver, MAX_UINT192 + 1) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({'from': A}).topUp(receiver, open_block_number, top_up_deposit) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({'from': sender}).topUp(A, open_block_number, top_up_deposit) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({'from': sender}).topUp(receiver, open_block_number, 0) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({'from': sender}).topUp(receiver, 0, top_up_deposit) txn_hash = uraiden_instance.transact({'from': sender}).topUp( receiver, open_block_number, top_up_deposit ) channel_data = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number) assert channel_data[1] == channel_deposit + top_up_deposit # deposit print_gas(txn_hash, 'test_channel_topup_20', gas_used_approve) # Check topup event ev_handler.add(txn_hash, uraiden_events['topup'], checkToppedUpEvent( sender, receiver, open_block_number, top_up_deposit, channel_deposit + top_up_deposit) ) ev_handler.check()
def test_withdraw_state( contract_params, channel_params, uraiden_instance, token_instance, get_channel, get_block, print_gas): (sender, receiver, open_block_number) = get_channel()[:3] deposit = channel_params['deposit'] balance1 = 20 balance2 = deposit balance_message_hash = balance_proof_hash( receiver, open_block_number, balance1, uraiden_instance.address ) balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2) assert is_same_address(addr, sender) # Memorize balances for tests uraiden_pre_balance = token_instance.call().balanceOf(uraiden_instance.address) sender_pre_balance = token_instance.call().balanceOf(sender) receiver_pre_balance = token_instance.call().balanceOf(receiver) txn_hash = uraiden_instance.transact({"from": receiver}).withdraw( open_block_number, balance1, balance_msg_sig ) # Check channel info channel_info = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number) # deposit assert channel_info[1] == deposit assert channel_info[2] == 0 assert channel_info[3] == 0 assert channel_info[4] == balance1 # Check token balances post withrawal uraiden_balance = uraiden_pre_balance - balance1 assert token_instance.call().balanceOf(uraiden_instance.address) == uraiden_balance assert token_instance.call().balanceOf(sender) == sender_pre_balance assert token_instance.call().balanceOf(receiver) == receiver_pre_balance + balance1 print_gas(txn_hash, 'withdraw') balance_message_hash = balance_proof_hash( receiver, open_block_number, balance2, uraiden_instance.address ) balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2) assert is_same_address(addr, sender) txn_hash = uraiden_instance.transact({"from": receiver}).withdraw( open_block_number, balance2, balance_msg_sig ) channel_info = uraiden_instance.call().getChannelInfo(sender, receiver, open_block_number) # deposit assert channel_info[1] == deposit assert channel_info[2] == 0 assert channel_info[3] == 0 assert channel_info[4] == balance2 # Check token balances post withrawal uraiden_balance = uraiden_pre_balance - deposit assert token_instance.call().balanceOf(uraiden_instance.address) == uraiden_balance assert token_instance.call().balanceOf(sender) == sender_pre_balance assert token_instance.call().balanceOf(receiver) == receiver_pre_balance + deposit print_gas(txn_hash, 'withdraw')
def test_channel_topup_20(get_accounts, uraiden_instance, token_instance, get_channel, txn_gas, event_handler, print_gas): token = token_instance ev_handler = event_handler(uraiden_instance) (sender, receiver, A) = get_accounts(3) channel_deposit = 999 channel = get_channel(uraiden_instance, token_instance, channel_deposit, sender, receiver)[:3] (sender, receiver, open_block_number) = channel top_up_deposit = 14 with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': sender }).topUp(receiver, open_block_number, top_up_deposit) # Approve token allowance txn_hash = token.transact({ "from": sender }).approve(uraiden_instance.address, top_up_deposit) gas_used_approve = txn_gas(txn_hash) with pytest.raises(TypeError): uraiden_instance.transact({"from": sender}).topUp(0x0, top_up_deposit) with pytest.raises(TypeError): uraiden_instance.transact({ "from": sender }).topUp('0x0', top_up_deposit) with pytest.raises(TypeError): uraiden_instance.transact({ "from": sender }).topUp(fake_address, top_up_deposit) with pytest.raises(TypeError): uraiden_instance.transact({"from": sender}).topUp(receiver, -3) with pytest.raises(TypeError): uraiden_instance.transact({ "from": sender }).topUp(receiver, MAX_UINT192 + 1) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': A }).topUp(receiver, open_block_number, top_up_deposit) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': sender }).topUp(A, open_block_number, top_up_deposit) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': sender }).topUp(receiver, open_block_number, 0) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': sender }).topUp(receiver, 0, top_up_deposit) txn_hash = uraiden_instance.transact({ 'from': sender }).topUp(receiver, open_block_number, top_up_deposit) channel_data = uraiden_instance.call().getChannelInfo( sender, receiver, open_block_number) assert channel_data[1] == channel_deposit + top_up_deposit # deposit print_gas(txn_hash, 'test_channel_topup_20', gas_used_approve) # Check topup event ev_handler.add( txn_hash, uraiden_events['topup'], checkToppedUpEvent(sender, receiver, open_block_number, top_up_deposit, channel_deposit + top_up_deposit)) ev_handler.check()
def test_close_by_sender_challenge_settle_by_sender2( web3, contract_params, get_accounts, uraiden_instance, token_instance, get_channel, print_gas, txn_gas, event_handler): token = token_instance ev_handler = event_handler(uraiden_instance) channel_deposit = 800 top_up_deposit = 14 channel = get_channel(uraiden_instance, token_instance, channel_deposit) (sender, receiver, open_block_number) = channel balance = 0 balance_message_hash = balance_proof_hash(receiver, open_block_number, balance, uraiden_instance.address) balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2) channel_pre_close_tests(uraiden_instance, token_instance, channel, top_up_deposit) txn_hash1 = uraiden_instance.transact({ 'from': sender }).uncooperativeClose(receiver, open_block_number, balance, balance_msg_sig) channel_data = uraiden_instance.call().getChannelInfo( sender, receiver, open_block_number) assert channel_data[2] != 0 # settle_block_number ev_handler.add( txn_hash1, uraiden_events['closed'], checkClosedEvent(sender, receiver, open_block_number, balance)) ev_handler.check() with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': sender }).settle(receiver, open_block_number) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': receiver }).settle(receiver, open_block_number) web3.testing.mine(contract_params['challenge_period'] + 1) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': receiver }).settle(receiver, open_block_number) receiver_pre_balance = token.call().balanceOf(receiver) sender_pre_balance = token.call().balanceOf(sender) contract_pre_balance = token.call().balanceOf(uraiden_instance.address) txn_hash2 = uraiden_instance.transact({ 'from': sender }).settle(receiver, open_block_number) channel_deposit += top_up_deposit receiver_post_balance = receiver_pre_balance + balance sender_post_balance = sender_pre_balance + (channel_deposit - balance) contract_post_balance = contract_pre_balance - channel_deposit assert token.call().balanceOf(receiver) == receiver_post_balance assert token.call().balanceOf(sender) == sender_post_balance assert token.call().balanceOf( uraiden_instance.address) == contract_post_balance channel_settle_tests(uraiden_instance, token_instance, channel) ev_handler.add( txn_hash2, uraiden_events['settled'], checkSettledEvent(sender, receiver, open_block_number, balance)) ev_handler.check() print_gas(txn_hash1, 'test_close_by_sender_challenge_settle_by_sender2', txn_gas(txn_hash2))
def test_close_by_receiver(get_accounts, uraiden_instance, token_instance, get_channel, print_gas, event_handler): token = token_instance (sender, receiver, A) = get_accounts(3) ev_handler = event_handler(uraiden_instance) channel_deposit = 800 top_up_deposit = 14 channel = get_channel(uraiden_instance, token_instance, channel_deposit, sender, receiver) (sender, receiver, open_block_number) = channel channel_pre_close_tests(uraiden_instance, token_instance, channel, top_up_deposit) balance = channel_deposit - 1 balance_message_hash = balance_proof_hash(receiver, open_block_number, balance, uraiden_instance.address) balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2) balance_msg_sig_false, addr2 = sign.check(balance_message_hash, tester.k4) assert addr == sender contract_verified_address = uraiden_instance.call().verifyBalanceProof( receiver, open_block_number, balance, balance_msg_sig) assert contract_verified_address == sender with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': A }).uncooperativeClose(receiver, open_block_number, balance, balance_msg_sig) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': receiver }).uncooperativeClose(receiver, open_block_number + 1, balance, balance_msg_sig) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': receiver }).uncooperativeClose(receiver, open_block_number, balance + 1, balance_msg_sig) with pytest.raises(tester.TransactionFailed): uraiden_instance.transact({ 'from': receiver }).uncooperativeClose(receiver, open_block_number, balance, balance_msg_sig_false) receiver_pre_balance = token.call().balanceOf(receiver) sender_pre_balance = token.call().balanceOf(sender) contract_pre_balance = token.call().balanceOf(uraiden_instance.address) txn_hash = uraiden_instance.transact({ 'from': receiver }).uncooperativeClose(receiver, open_block_number, balance, balance_msg_sig) channel_deposit += top_up_deposit receiver_post_balance = receiver_pre_balance + balance sender_post_balance = sender_pre_balance + (channel_deposit - balance) contract_post_balance = contract_pre_balance - channel_deposit assert token.call().balanceOf(receiver) == receiver_post_balance assert token.call().balanceOf(sender) == sender_post_balance assert token.call().balanceOf( uraiden_instance.address) == contract_post_balance channel_settle_tests(uraiden_instance, token_instance, channel) # TODO: # with pytest.raises(Exception): # ev_handler.add(txn_hash, uraiden_events['closed'], checkClosedEvent(sender, receiver, open_block_number, balance)) ev_handler.add( txn_hash, uraiden_events['settled'], checkSettledEvent(sender, receiver, open_block_number, balance)) ev_handler.check() print_gas(txn_hash, 'test_close_by_receiver')
def test_withdraw_state(contract_params, channel_params, uraiden_instance, token_instance, get_channel, get_block, print_gas): (sender, receiver, open_block_number) = get_channel()[:3] deposit = channel_params['deposit'] balance1 = 20 balance2 = deposit balance_message_hash = balance_proof_hash(receiver, open_block_number, balance1, uraiden_instance.address) balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2) assert is_same_address(addr, sender) # Memorize balances for tests uraiden_pre_balance = token_instance.call().balanceOf( uraiden_instance.address) sender_pre_balance = token_instance.call().balanceOf(sender) receiver_pre_balance = token_instance.call().balanceOf(receiver) txn_hash = uraiden_instance.transact({ "from": receiver }).withdraw(open_block_number, balance1, balance_msg_sig) # Check channel info channel_info = uraiden_instance.call().getChannelInfo( sender, receiver, open_block_number) # deposit assert channel_info[1] == deposit assert channel_info[2] == 0 assert channel_info[3] == 0 assert channel_info[4] == balance1 # Check token balances post withrawal uraiden_balance = uraiden_pre_balance - balance1 assert token_instance.call().balanceOf( uraiden_instance.address) == uraiden_balance assert token_instance.call().balanceOf(sender) == sender_pre_balance assert token_instance.call().balanceOf( receiver) == receiver_pre_balance + balance1 print_gas(txn_hash, 'withdraw') balance_message_hash = balance_proof_hash(receiver, open_block_number, balance2, uraiden_instance.address) balance_msg_sig, addr = sign.check(balance_message_hash, tester.k2) assert is_same_address(addr, sender) txn_hash = uraiden_instance.transact({ "from": receiver }).withdraw(open_block_number, balance2, balance_msg_sig) channel_info = uraiden_instance.call().getChannelInfo( sender, receiver, open_block_number) # deposit assert channel_info[1] == deposit assert channel_info[2] == 0 assert channel_info[3] == 0 assert channel_info[4] == balance2 # Check token balances post withrawal uraiden_balance = uraiden_pre_balance - deposit assert token_instance.call().balanceOf( uraiden_instance.address) == uraiden_balance assert token_instance.call().balanceOf(sender) == sender_pre_balance assert token_instance.call().balanceOf( receiver) == receiver_pre_balance + deposit print_gas(txn_hash, 'withdraw')