def test_block_info(self): evm.set_current_account(test_account) _from = w3.toChecksumAddress(shared.eth_address) _to = w3.toChecksumAddress(shared.contract_address) args = {'from': _from, 'to': _to} logs = Greeter.functions.testBlockInfo().transact(args)
def test_ecrecover_with_eos_key(self): evm.set_current_account(test_account) pub_key = 'EOS7ent7keWbVgvptfYaMYeF2cenMBiwYKcwEuc11uCbStsFKsrmV' eth_address = evm.gen_eth_address_from_eos_public_key(pub_key) _from = w3.toChecksumAddress(shared.eth_address) _to = w3.toChecksumAddress(shared.contract_address) args = {'from': _from, 'to': _to} from eth_keys import keys from eth_utils import keccak, to_bytes h = keccak(b'a message') base58_sign = wallet.sign_digest(h, pub_key) sign = base58.b58decode(base58_sign[7:]) print(sign) v = sign[0] sign = sign[1:-4] # v = chain_id + (sign[0]<<24)+0x800000 print('+++v:', v) r = sign[:32] s = sign[32:32+32] logs = Greeter.functions.ecrecoverTest(h, v, r, s).transact(args) recover_pub_key = eosapi.recover_key(h.hex(), base58_sign) logger.info(recover_pub_key) recover_address = evm.gen_eth_address_from_eos_public_key(recover_pub_key) logger.info(recover_address) logger.info(logs[1][12:].hex()) assert logs[1][12:].hex() == recover_address
def test_origin(self): evm.set_current_account(test_account) origin = _from = w3.toChecksumAddress(shared.eth_address) _to = w3.toChecksumAddress(shared.contract_address) args = {'from': _from, 'to': _to} logs = Greeter.functions.testOrigin(origin).transact(args)
def test_call_other_contract(self): _from = w3.toChecksumAddress(shared.eth_address) _to = w3.toChecksumAddress(shared.tester_contract_address) callee_address = w3.toChecksumAddress(shared.callee_contract_address) args = {'from': _from, 'to': _to} value = 2 logs = Tester.functions.testCall(callee_address, value).transact(args) ret_value = int.from_bytes(logs[1], 'big') assert ret_value == value + 1
def test_sha256(self): evm.set_current_account(test_account) _from = w3.toChecksumAddress(shared.eth_address) _to = w3.toChecksumAddress(shared.contract_address) args = {'from': _from, 'to': _to} logs = Greeter.functions.sha256Test(b'another message').transact(args) logger.info(logs) import hashlib h = hashlib.sha256() h.update(b'another message') digest = h.digest() logger.info((digest)) assert logs[1] == digest
def test_transfer_eth(self): evm.set_current_account('helloworld12') eosapi.transfer('helloworld12', main_account, 1.0) balance1 = eth.get_balance(shared.eth_address) balance2 = eth.get_balance(shared.main_eth_address) ram_usage_main_account = eosapi.get_account(main_account)['ram_usage'] ram_usage_test_account = eosapi.get_account(test_account)['ram_usage'] transaction = { 'from':shared.eth_address, 'to': w3.toChecksumAddress(shared.main_eth_address), 'value': 1000, 'gas': 2000000, 'gasPrice': 234567897654321, 'nonce': 0, 'chainId': 1 } w3.eth.sendTransaction(transaction) logger.info((balance1, eth.get_balance(shared.eth_address))) assert balance1 == eth.get_balance(shared.eth_address)+1000 assert balance2+1000 == eth.get_balance(shared.main_eth_address) assert ram_usage_main_account == eosapi.get_account(main_account)['ram_usage'] assert ram_usage_test_account == eosapi.get_account(test_account)['ram_usage']
def test_suicide(self): _from = w3.toChecksumAddress(shared.eth_address) _to = w3.toChecksumAddress(shared.tester_contract_address) args = {'from': _from, 'to': _to, 'value': 10000} logs = Tester.functions.transfer().transact(args) logger.info(logs) balance11 = eth.get_balance(shared.eth_address) balance21 = eth.get_balance(shared.tester_contract_address) logger.info((balance11, balance21)) args = {'from': _from, 'to': _to} logs = Tester.functions.testSuicide().transact(args) balance12 = eth.get_balance(shared.eth_address) balance22 = eth.get_balance(shared.tester_contract_address) logger.info((balance12, balance22)) assert balance22 == 0 assert balance12 == balance11 + balance21 assert not eth.get_code(shared.tester_contract_address)
def test_authorization(self): checksum_contract_address = w3.toChecksumAddress(shared.contract_address) logger.info(f'{bcolors.OKGREEN}++++++++++test call evm contract with wrong authorization{bcolors.ENDC}') try: evm.set_current_account(main_account) args = {'from': shared.eth_address,'to': checksum_contract_address} ret = Greeter.functions.setValue(0xaabbccddee).transact(args) assert 0 except Exception as e: e = json.loads(e.response) assert e['error']['details'][0]['message'] == 'missing authority of helloworld12'
def test_check_chain_id(self): # evm.set_chain_id(2) args = {'chainid': 2} r = eosapi.push_action(main_account, 'setchainid', args, {main_account:'active'}) print('++++console:', r['processed']['action_traces'][0]['console']) print(r['processed']['elapsed']) transaction = { 'from':shared.eth_address, 'to': w3.toChecksumAddress(shared.main_eth_address), 'value': 1000, 'gas': 2000000, 'gasPrice': 1, 'nonce': 0, 'chainId': 1 } try: w3.eth.sendTransaction(transaction) except Exception as e: e = json.loads(e.response) logger.info(e['error']['details'][0]['message']) assert e['error']['details'][0]['message'] == "assertion failure with message: bad chain id!" time.sleep(0.5) evm.set_chain_id(1) args = {'chainid': 1} r = eosapi.push_action(main_account, 'setchainid', args, {main_account:'active'}) print('++++console:', r['processed']['action_traces'][0]['console']) print(r['processed']['elapsed']) logger.info(shared.main_eth_address) transaction = { 'from':shared.eth_address, 'to': w3.toChecksumAddress(shared.main_eth_address), 'value': 1, 'gas': 2000000, 'gasPrice': 2, 'nonce': 0, 'chainId': 1 } w3.eth.sendTransaction(transaction)
def test_ecrecover(self): evm.set_current_account(test_account) _from = w3.toChecksumAddress(shared.eth_address) _to = w3.toChecksumAddress(shared.contract_address) args = {'from': _from, 'to': _to} from eth_keys import keys from eth_utils import keccak, to_bytes h = keccak(b'a message') pk = keys.PrivateKey(b'\x01' * 32) sign = pk.sign_msg_hash(h) print(h, sign.v, sign.r, sign.s) r = to_bytes(sign.r) s = to_bytes(sign.s) logs = Greeter.functions.ecrecoverTest(h, sign.v+27, r, s).transact(args) logger.info(logs) pub_key = sign.recover_public_key_from_msg(b'a message') address = pub_key.to_canonical_address() logger.info(pub_key) logger.info(address) assert logs[1][12:] == address
def test_set_value(self): evm.set_current_account(test_account) checksum_contract_address = w3.toChecksumAddress(shared.contract_address) #test storage args = {'from': shared.eth_address,'to': checksum_contract_address} logs = Greeter.functions.setValue(0xaabbccddee).transact(args) logger.info((logs, keccak(b'onSetValue(uint256)'))) logger.info(logs[2][0]) assert logs[2][0][1][0] == keccak(b'onSetValue(uint256)') evm.format_log(logs) logger.info(logs)
def test_check_balance(self): evm.set_current_account(test_account) checksum_contract_address = w3.toChecksumAddress(shared.contract_address) self.transfer_eth(shared.eth_address, checksum_contract_address, 10000) args = {'from': shared.eth_address,'to': checksum_contract_address} balance = eth.get_balance(shared.eth_address) balance_contract = eth.get_balance(shared.contract_address) logger.info((balance, balance_contract)) logs = Greeter.functions.checkBalance(balance).transact(args) evm.format_log(logs) print(logs)
def test_get_value(self): evm.set_current_account(test_account) checksum_contract_address = w3.toChecksumAddress(shared.contract_address) #test storage args = {'from': shared.eth_address,'to': checksum_contract_address} logs = Greeter.functions.getValue().transact(args) logger.info(logs) # [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', # b'', # [b'\xf9\x8a\xea\xf5\xdb\xa7\x92gk\xdc\xec#\xe3a\xfc\x1b0\x1e\x95e', # [ # b'\xf4\x8a\x1d\xc5~\xef\xa3\xdeD\x06\xe6_\xc0\xfc7{%\xd1\x00\xbd\x06B\x92\x01\xa0\x1a\x9e\x8e\x0e\x1d\x07s' # ], # b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xaa\xbb\xcc\xdd\xee' # ] values = eth.get_all_values(shared.contract_address) logger.info(values)
def test_transfer_back(self): evm.set_current_account(test_account) checksum_contract_address = w3.toChecksumAddress(shared.contract_address) self.deposit(test_account, 1.0) logger.info((shared.eth_address, "balance", eth.get_balance(shared.eth_address))) self.transfer_eth(shared.eth_address, shared.contract_address, 1000) balance1 = eth.get_balance(shared.eth_address) balance2 = eth.get_balance(shared.contract_address) logger.info((balance1, balance2)) args = {'from': shared.eth_address,'to': checksum_contract_address} logs = Greeter.functions.transferBack(1000).transact(args) balance1 = eth.get_balance(shared.eth_address) balance2 = eth.get_balance(shared.contract_address) logger.info((balance1, balance2)) evm.format_log(logs) logger.info(logs)
def transfer_eth(self, _from, _to, _value): evm.set_current_account('helloworld12') args = {'from': w3.toChecksumAddress(_from),'to': w3.toChecksumAddress(_to), 'value':_value} ret = Greeter.functions.transfer().transact(args)