def test_overspend(self): self.set_up() self.test_create_channel() self.get_all_events() data = [] amount = 142871500000000000 # obviously much more that we've sent initially address = int(addresses[1], 0) data.append(D160 * amount + address) # Trying to close the channel with an overspend balance_msg = self.channel.call().getBalanceMessage( self.sender, self.open_block, data) balance_msg_sig, _ = sign.check( balance_msg, binascii.unhexlify(self.sender_key[2:])) tx_hash = self.channel.transact({ 'from': self.sender }).close(self.sender, self.open_block, data, balance_msg_sig) check_successful_tx(self.web3, tx_hash, txn_wait) try: self.logger.info('Channel info: {}'.format( self.channel.call().getChannelInfo(self.sender, self.open_block))) except: self.logger.warning('Channel info failed in overspend. ×') try: self.logger.warning('Closing request: {}'.format( self.channel.call().getClosingRequestInfo( self.sender, self.open_block))) except: self.logger.info('Closing request info failed in overspend. ✓') # Checking if we've overspent self.logger.info( self.channel.call({ 'from': self.sender }).checkOverspend(self.key, data)) # Reporting cheating right_data = [] amount = 1428715 address = int(addresses[1], 0) right_data.append(D160 * amount + address) right_balance_msg = self.channel.call().getBalanceMessage( self.sender, self.open_block, right_data) right_balance_msg_sig, _ = sign.check( right_balance_msg, binascii.unhexlify(self.sender_key[2:])) tx_hash = self.channel.transact({ 'from': self.web3.eth.accounts[1] }).reportCheating(self.sender, self.open_block, right_data, right_balance_msg_sig, data, balance_msg_sig) check_successful_tx(self.web3, tx_hash, txn_wait) try: self.logger.info('Closing request: {}'.format( self.channel.call().getClosingRequestInfo( self.sender, self.open_block))) except: self.logger.warning('Closing request info failed in close. ×')
def test_close_by_receiver(web3, chain, contract, channel): (sender, receiver, open_block_number) = channel (A) = web3.eth.accounts[3] channel_pre_close_tests(contract, channel) current_deposit = get_current_deposit(contract, channel) balance = current_deposit - 1 balance_msg = get_balance_message(receiver, open_block_number, balance) balance_msg_sig, addr = sign.check(balance_msg, tester.k1) balance_msg_sig_false, addr = sign.check(balance_msg, tester.k2) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': A }).close(receiver, open_block_number, balance, balance_msg_sig) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': receiver }).close(receiver, open_block_number + 1, balance, balance_msg_sig) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': receiver }).close(receiver, open_block_number, balance + 1, balance_msg_sig) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': receiver }).close(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(contract.address) print('--BALANCES', receiver_pre_balance, sender_pre_balance, contract_pre_balance) trxid = contract.transact({ 'from': receiver }).close(receiver, open_block_number, balance, balance_msg_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_receiver', 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 }).close(receiver, open_block_number, balance, balance_msg_sig) with pytest.raises(tester.TransactionFailed): contract.transact({ 'from': sender }).close(A, open_block_number, balance, balance_msg_sig) # Cannot close if arguments not correct with pytest.raises(ValueError): contract.transact({ 'from': sender }).close(receiver, open_block_number, balance) with pytest.raises(ValueError): contract.transact({ 'from': receiver }).close(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 }).close(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('----------------------------------')
def test_cashCheque(trustlines_contract, accounts, web3): (A, B) = accounts(2) balA = trustlines_contract.call().balanceOf(A) balB = trustlines_contract.call().balanceOf(B) mtime = trustlines_contract.call().calculateMtime() data = trustlines_contract.call().shaOfValue(A, B, 90, mtime + 1) sig, addr = sign.check(bytes(data, "raw_unicode_escape"), tester.k0) assert addr == A trustlines_contract.transact({"from": A}).prepareFrom(A, B, 100, [B]) assert (trustlines_contract.transact({ "from": A }).cashCheque(A, B, 90, mtime + 1, sig)) assert trustlines_contract.call().balanceOf(A) == balA - 90 assert trustlines_contract.call().balanceOf(B) == balB + 90
def cheating_template(self, case_n, good_data, bad_data): self.set_up() self.test_create_channel() self.logger.info('Case#: {}, Good data: {}'.format(case_n, good_data)) bad_balance_msg = self.channel.call().getBalanceMessage( self.sender, self.open_block, bad_data) bad_balance_msg_sig, _ = sign.check( bad_balance_msg, binascii.unhexlify(self.sender_key[2:])) right_balance_msg = self.channel.call().getBalanceMessage( self.sender, self.open_block, good_data) right_balance_msg_sig, _ = sign.check( right_balance_msg, binascii.unhexlify(self.sender_key[2:])) tx_hash = self.channel.transact({ 'from': self.web3.eth.accounts[1] }).reportCheating(self.sender, self.open_block, good_data, right_balance_msg_sig, bad_data, bad_balance_msg_sig) check_successful_tx(self.web3, tx_hash, txn_wait) try: self.logger.info('Closing request: {}'.format( self.channel.call().getClosingRequestInfo( self.sender, self.open_block))) except: self.logger.warning( 'Closing request info failed in cheating {}. ×'.format(case_n))
def test_right_sign(self): data = [] for i in range(SENDERS - 1): amount = 5491 * 10**18 address = int(addresses[i], 0) data.append(D160 * amount + address) for i in range(SENDERS - 1): # with self.subTest(address=addresses[i]): balance_msg = self.channel.call().getBalanceMessage( addresses[i], 1, data) balance_msg_sig, _ = sign.check( balance_msg, binascii.unhexlify(private_keys[i])) ec_recovered_addr = self.channel.call() \ .verifyBalanceProof(addresses[i], 1, data, balance_msg_sig) assert ec_recovered_addr == addresses[i] self.logger.info('Signs are right')
def test_sign(contract, channel): (sender, receiver, open_block_number) = channel balance = 23 msg = get_balance_message(receiver, open_block_number, balance) # "Receiver: " + receiver + "\nBalance: " + str(balance) + "\nChannel ID: " + str(open_block_number) # hex: 0x19457468657265756d205369676e6564204d6573736167653a0a373852656365697665723a203078646365636561663366633563306136336431393564363962316139303031316237623139363530640a42616c616e63653a2032330a4368616e6e656c2049443a2035 print('--- test_sign msg', msg) balance_msg_sig, addr = sign.check(msg, tester.k2) print('--balance_msg_sig', balance_msg_sig, addr) addr = contract.call().verifyBalanceProof(receiver, open_block_number, balance, balance_msg_sig) print('--verifyBalanceProof addr', addr) # with Timeout(20) as timeout: # timeout.sleep(2) assert addr == receiver
def test_send_new_transaction(self): data = [] amount = 1528715 address = int(self.web3.eth.accounts[1], 0) data.append(D160 * amount + address) balance_msg = self.channel.call().getBalanceMessage( self.sender, self.open_block, data) balance_msg_sig, _ = sign.check( balance_msg, binascii.unhexlify(self.sender_key[2:])) tx_hash = self.channel.transact({ 'from': self.sender }).submitLaterTransaction(self.sender, self.open_block, data, balance_msg_sig) check_successful_tx(self.web3, tx_hash, txn_wait) try: self.logger.info('Closing request: {}'.format( self.channel.call().getClosingRequestInfo( self.sender, self.open_block))) except: self.logger.warning( 'Closing request info failed in send_new_transaction ×')
def test_ten_payee_close(self): self.set_up() self.get_all_events() self.test_create_channel() self.logger.info('Waiting for channel lifetime to end') time.sleep(15 * (channel_lifetime + 1)) self.logger.info('Lifetime should have ended') data = generate_data([1000] * 10) balance_msg = self.channel.call().getBalanceMessage( self.sender, self.open_block, data) balance_msg_sig, _ = sign.check( balance_msg, binascii.unhexlify(self.sender_key[2:])) try: self.logger.info( self.channel.call({ 'from': self.sender }).close(self.sender, self.open_block, data, balance_msg_sig)) except: self.logger.warning('CLOSE REQUEST WILL FAIL') tx_hash = self.channel.transact({ 'from': self.sender }).close(self.sender, self.open_block, data, balance_msg_sig) check_successful_tx(self.web3, tx_hash, txn_wait) self.logger.info('BLOCK {}'.format( self.open_block)) # TODO write this to the data.json as well? try: self.logger.info('Channel info: {}'.format( self.channel.call().getChannelInfo(self.sender, self.open_block))) except: self.logger.warning('Channel info failed in close ×') try: self.logger.info('Closing request: {}'.format( self.channel.call().getClosingRequestInfo( self.sender, self.open_block))) except: self.logger.warning('Closing request info failed in close ×') self.test_settle_channel()
def test_close_channel(self): self.logger.info('Waiting for channel lifetime to end') time.sleep(15 * (channel_lifetime + 1)) self.logger.info('Lifetime should have ended') data = [] amount = 1428715 address = int(self.web3.eth.accounts[1], 0) data.append(D160 * amount + address) balance_msg = self.channel.call().getBalanceMessage( self.sender, self.open_block, data) balance_msg_sig, _ = sign.check( balance_msg, binascii.unhexlify(self.sender_key[2:])) try: self.logger.info( self.channel.call({ 'from': self.sender }).close(self.sender, self.open_block, data, balance_msg_sig)) except: self.logger.warning('CLOSE REQUEST WILL FAIL') tx_hash = self.channel.transact({ 'from': self.sender }).close(self.sender, self.open_block, data, balance_msg_sig) check_successful_tx(self.web3, tx_hash, txn_wait) self.logger.info('BLOCK {}'.format( self.open_block)) # TODO write this to the data.json as well? try: self.logger.info('Channel info: {}'.format( self.channel.call().getChannelInfo(self.sender, self.open_block))) except: self.logger.warning('Channel info failed in close ×') try: self.logger.info('Closing request: {}'.format( self.channel.call().getClosingRequestInfo( self.sender, self.open_block))) except: self.logger.warning('Closing request info failed in close ×')
def getTokens(**kwargs): project = Project() # print(kwargs) chain_name = kwargs['chain'] owner = kwargs['owner'] challenge_period = kwargs['challenge_period'] supply = kwargs['supply'] senders = kwargs['senders'] sender_addresses = kwargs['sender_addresses'].split(',') token_name = kwargs['token_name'] token_decimals = kwargs['token_decimals'] token_symbol = kwargs['token_symbol'] token_address = kwargs['token_address'] supply *= 10**(token_decimals) token_assign = int(supply / (len(sender_addresses) + senders)) txn_wait = 250 event_wait = 50 if chain_name == 'rinkeby': txn_wait = 500 event_wait = 500 print( "Make sure {} chain is running, you can connect to it and it is synced, or you'll get timeout" .format(chain_name)) with project.get_chain(chain_name) as chain: web3 = chain.web3 owner = owner or web3.eth.accounts[0] print('Web3 provider is', web3.currentProvider) if not token_address: token = chain.provider.get_contract_factory('ERC223Token') txhash = token.deploy( args=[supply, token_name, token_decimals, token_symbol], transaction={'from': owner}) receipt = check_succesful_tx(chain.web3, txhash, txn_wait) token_address = receipt['contractAddress'] print(token_name, ' address is', token_address) channel_factory = chain.provider.get_contract_factory( 'RaidenMicroTransferChannels') txhash = channel_factory.deploy(args=[token_address, challenge_period]) print('RaidenMicroTransferChannels arguments', token_address, challenge_period) padded_token_address = pad_left(remove_0x_prefix(token_address), 64, '0') print('RaidenMicroTransferChannels abi encoded arguments:', encode_hex(pack(padded_token_address, challenge_period))) receipt = check_succesful_tx(chain.web3, txhash, txn_wait) cf_address = receipt['contractAddress'] print('RaidenMicroTransferChannels address is', cf_address) priv_keys = [] addresses = [] # we cannot retrieve private keys from configured chains # therefore: create 5 wallets (sample addresses with private keys) # store in separate arrays for i in range(senders - 1): priv_key, address = createWallet() priv_keys.append(priv_key) addresses.append('0x' + address) # send tokens to each new wallet for sender in addresses: token(token_address).transact({ 'from': owner }).transfer(sender, token_assign) # also send tokens to sender addresses for sender in sender_addresses: token(token_address).transact({ 'from': owner }).transfer(sender, token_assign) print('Senders have each been issued', token_assign, ' tokens') # check if it works: # 1. get message balance hash for address[0] balance_msg = "Receiver: " + addresses[ 0] + ", Balance: 10000, Channel ID: 100" # 2. sign the hash with private key corresponding to address[0] balance_msg_sig, addr = sign.check(balance_msg, binascii.unhexlify(priv_keys[0])) # 3. check if ECVerify and ec_recovered address are equal ec_recovered_addr = channel_factory( cf_address).call().verifyBalanceProof(addresses[0], 100, 10000, balance_msg_sig) print('EC_RECOVERED_ADDR:', ec_recovered_addr) print('FIRST WALLET ADDR:', addresses[0]) assert ec_recovered_addr == addresses[0] print('Wait for confirmation...') transfer_filter = token.on('Transfer') wait(transfer_filter, event_wait) print('BALANCE:', token(token_address).call().balanceOf(addresses[0])) assert token(token_address).call().balanceOf(addresses[0]) > 0 # return arrays with generated wallets (private keys first, then addresses, so that priv_key[0] <-> address[0] return (priv_keys, addresses, token(token_address))