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_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_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_deposit(self): # Deposit Test evm.set_current_account(test_account) balance = eth.get_balance(shared.eth_address) r = eosapi.transfer(test_account, main_account, 10.1, 'deposit') eth_balance = eth.get_balance(shared.eth_address) logger.info(('++++balance:', balance, eth_balance)) assert eth_balance == balance + 10*10000+1000
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_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_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_withdraw(self): ### Withdraw test evm.set_current_account(test_account) args = {'account': test_account, 'amount': '1.0000 SYS'} try: eos_balance = eosapi.get_balance(test_account) eth_balance = eth.get_balance(shared.eth_address) r = eosapi.push_action(main_account, 'withdraw', args, {test_account:'active'}) print('++++console:', r['processed']['action_traces'][0]['console']) print(r['processed']['elapsed']) assert eth_balance - 1.0 == eth.get_balance(shared.eth_address) assert eos_balance + 1.0 == eosapi.get_balance(test_account) except Exception as e: print(e)
def test_overdraw(self): #Overdraw test evm.set_current_account(test_account) eth_balance = eth.get_balance(shared.eth_address) logger.info(('++++eth_balance:', eth_balance)) args = {'account': test_account, 'amount': '%.4f SYS'%(eth_balance+0.1,)} logger.info(('++++args:', args)) try: r = eosapi.push_action(main_account, 'withdraw', args, {test_account:'active'}) print('++++console:', r['processed']['action_traces'][0]['console']) #should not go here assert 0 except Exception as e: assert eth_balance == eth.get_balance(shared.eth_address) e = json.loads(e.response) assert e['error']['details'][0]['message'] == "assertion failure with message: balance overdraw!"
def test_transfer_eth_to_not_created_address(self): evm.set_current_account('helloworld12') eosapi.transfer('helloworld12', 'helloworld11', 1.0, 'hello') transaction = { 'from':shared.eth_address, 'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', 'value': 1000, 'gas': 2000000, 'gasPrice': 0, 'nonce': 0, 'chainId': 1 } try: w3.eth.sendTransaction(transaction) except Exception as e: logger.info(e) e = json.loads(e.response) assert e['error']['details'][0]['message'] == "assertion failure with message: eth address does not exists!"
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_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 deploy_evm_contract(): logger.info(shared.contract_address) if shared.contract_address: return evm.set_current_account(test_account) nonce = eth.get_nonce(shared.eth_address) e = rlp.encode([bytes.fromhex(shared.eth_address), nonce]) h = keccak(e) expected_address = h[12:].hex() #test deploy evm contract logger.info(("++++++++++shared.eth_address:", shared.eth_address)) logs = Greeter.constructor().transact({'from': shared.eth_address}) shared.contract_address = logs[0].hex() logger.info((expected_address, shared.contract_address)) assert expected_address == shared.contract_address assert eth.get_nonce(shared.eth_address) == nonce + 1 #test get contract code code = eth.get_code(shared.contract_address) # logger.info(code) # logger.info( logs[1].hex()) assert code # == logs[1].hex() logs = Tester.constructor().transact({'from': shared.eth_address}) shared.tester_contract_address = logs[0].hex() logger.info(shared.tester_contract_address) logs = Callee.constructor().transact({'from': shared.eth_address}) shared.callee_contract_address = logs[0].hex() logger.info(shared.callee_contract_address)
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)
def __init__(self, testName, extra_args=[]): super(EVMTestCase, self).__init__(testName) self.extra_args = extra_args evm.set_current_account(test_account) evm.set_chain_id(1)
def deposit(self, account, amount): evm.set_current_account(test_account) r = eosapi.transfer(account, main_account, amount, 'deposit')