def test_close_by_sender(web3, chain, contract, channel): (sender, receiver, open_block_number) = channel (A) = web3.eth.accounts[3] current_deposit = get_current_deposit(contract, channel) balance = current_deposit - 1 balance_msg = get_balance_message(receiver, open_block_number, balance) balance_msg_false_receiver = get_balance_message(A, open_block_number, balance) balance_msg_sig, addr = sign.check(balance_msg, tester.k1) balance_msg_sig_false_signer, addr = sign.check(balance_msg, tester.k3) balance_msg_sig_false_receiver, addr = sign.check(balance_msg_false_receiver, tester.k3) closing_sig, addr = sign.check(balance_msg, tester.k2) closing_sig_false_signer, addr = sign.check(balance_msg, tester.k3) closing_sig_false_receiver, addr = sign.check(balance_msg_false_receiver, tester.k2) with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).close(A, open_block_number, balance, balance_msg_sig, closing_sig) with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).close(receiver, open_block_number - 3, balance, balance_msg_sig, closing_sig) with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).close(receiver, open_block_number, balance + 5, balance_msg_sig, closing_sig) with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).close(receiver, open_block_number, balance, balance_msg_sig_false_signer, closing_sig) with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).close(receiver, open_block_number, balance, balance_msg_sig_false_receiver, closing_sig) with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).close(receiver, open_block_number, balance, balance_msg_sig, closing_sig_false_signer) with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).close(receiver, open_block_number, balance, balance_msg_sig, closing_sig_false_receiver) receiver_pre_balance = token.call().balanceOf(receiver) sender_pre_balance = token.call().balanceOf(sender) contract_pre_balance = token.call().balanceOf(contract.address) channel_pre_close_tests(contract, channel) trxid = contract.transact({'from': sender}).close(receiver, open_block_number, balance, balance_msg_sig, closing_sig) receiver_post_balance = receiver_pre_balance + balance sender_post_balance = sender_pre_balance + (current_deposit - balance) contract_post_balance = contract_pre_balance - current_deposit assert token.call().balanceOf(receiver) == receiver_post_balance assert token.call().balanceOf(sender) == sender_post_balance assert token.call().balanceOf(contract.address) == contract_post_balance channel_settle_tests(contract, channel) print('----------------------------------') print('GAS USED test_close_by_sender', get_gas_used(chain, trxid)) print('----------------------------------')
def test_close_call(web3, chain, contract, channel): (sender, receiver, open_block_number) = channel (A) = web3.eth.accounts[3] balance = channel_deposit - 10 balance_msg = get_balance_message(receiver, open_block_number, balance) balance_msg_sig, addr = sign.check(balance_msg, tester.k1) # Cannot close what was not opened with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': A }).uncooperativeClose(receiver, open_block_number, balance, balance_msg_sig) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': sender }).uncooperativeClose(A, open_block_number, balance, balance_msg_sig) # Cannot close if arguments not correct with pytest.raises(ValueError): contract.transact({ 'from': sender }).initChallengePeriod(receiver, open_block_number, balance) with pytest.raises(ValueError): contract.transact({ 'from': receiver }).settleChannel(sender, receiver, open_block_number, balance)
def test_close_by_sender_challenge_settle_by_sender2(web3, chain, contract, channel): (sender, receiver, open_block_number) = channel (A) = web3.eth.accounts[3] current_deposit = get_current_deposit(contract, channel) balance = 0 balance_msg = get_balance_message(receiver, open_block_number, balance) balance_msg_sig, addr = sign.check(balance_msg, tester.k1) receiver_pre_balance = token.call().balanceOf(receiver) sender_pre_balance = token.call().balanceOf(sender) contract_pre_balance = token.call().balanceOf(contract.address) channel_pre_close_tests(contract, channel) trxid1 = contract.transact({ 'from': sender }).uncooperativeClose(receiver, open_block_number, balance, balance_msg_sig) channel_data = contract.call().getChannelInfo(sender, receiver, open_block_number) assert channel_data[2] != 0 # settle_block_number with pytest.raises(tester.TransactionFailed): contract.transact({'from': sender}).settle(receiver, open_block_number) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': receiver }).settle(receiver, open_block_number) web3.testing.mine(challenge_period + 1) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': receiver }).settle(receiver, open_block_number) trxid2 = contract.transact({ 'from': sender }).settle(receiver, open_block_number) receiver_post_balance = receiver_pre_balance + balance sender_post_balance = sender_pre_balance + (current_deposit - balance) contract_post_balance = contract_pre_balance - current_deposit assert token.call().balanceOf(receiver) == receiver_post_balance assert token.call().balanceOf(sender) == sender_post_balance assert token.call().balanceOf(contract.address) == contract_post_balance channel_settle_tests(contract, channel) print('----------------------------------') print('GAS USED test_close_by_sender_challenge_settle_by_sender2', get_gas_used(chain, trxid1) + get_gas_used(chain, trxid2)) print('----------------------------------')