def test_eth_call(rpc_client, accounts): txn_hash = rpc_client( method="eth_sendTransaction", params=[{ "from": accounts[0], "data": CONTRACT_BIN, "value": 1234, }], ) txn_receipt = rpc_client( method="eth_getTransactionReceipt", params=[txn_hash], ) contract_address = txn_receipt['contractAddress'] assert contract_address function_sig = encode_data(sha3("return13()")[:4]) should_be_13 = rpc_client( method="eth_call", params=[{ "from": accounts[0], "to": contract_address, "data": function_sig, }], ) result = big_endian_to_int(decode_hex(should_be_13[2:])) assert result == 13
def test_eth_call(rpc_client, accounts): txn_hash = rpc_client( method="eth_sendTransaction", params=[{ "from": accounts[0], "data": CONTRACT_BIN, "value": 1234, }], ) txn_receipt = rpc_client( method="eth_getTransactionReceipt", params=[txn_hash], ) contract_address = txn_receipt['contractAddress'] assert contract_address function_sig = encode_data(sha3("return13()")[:4]) should_be_13 = rpc_client( method="eth_call", params=[{ "from": accounts[0], "to": contract_address, "data": function_sig, }], ) result = big_endian_to_int(decode_hex(should_be_13[2:])) assert result == 13
def test_eth_sendRawTransaction(accounts, rpc_client): tx = Transaction(0, tester.gas_price, tester.gas_limit, accounts[1], 1234, '') tx.sign(tester.keys[0]) raw_tx = rlp.encode(tx) raw_tx_hex = encode_data(raw_tx) result = rpc_client('eth_sendRawTransaction', params=[raw_tx_hex]) assert len(result) == 66
def test_eth_sendRawTransaction(accounts, rpc_client): tx = Transaction(0, tester.gas_price, tester.gas_limit, accounts[1], 1234, '') tx.sign(tester.keys[0]) raw_tx = rlp.encode(tx) raw_tx_hex = encode_data(raw_tx) result = rpc_client('eth_sendRawTransaction', params=[raw_tx_hex]) assert len(result) == 66
def runTest(contract, optimize, verbose=False): client = EthTesterClient() client.reset_evm() a0 = sorted(client.get_accounts())[0] try: bin = list(compile_source(contract, optimize=optimize).values())[0]['bin'] except SolcError as e: if "Compiler error: Stack too deep" in str(e): return ("STACK TOO DEEP", None, None) if "Internal compiler error during compilation" in str(e): print(contract) print("INTERNAL COMPILER ERROR WITH optimize =", optimize) print(e) assert False if verbose: print(e) return ("COMPILATION FAILED", None, None) try: txnHash = client.send_transaction(_from=a0, data=bytes(bin), value=0) except TransactionFailed as e: if verbose: print(e) return ("SEND CONTRACT TRANSACTION FAILED", bytes(bin), None) txnReceipt = client.get_transaction_receipt(txnHash) contractAddress = txnReceipt['contractAddress'] origBalances = {} for a in client.get_accounts(): origBalances[a] = client.get_balance(a) sendVal = 9999999 fsig = encode_data(sha3("f()")[:4]) try: val = client.call(_from=a0, to=contractAddress, data=fsig, value=sendVal) except TransactionFailed as e: balances = {} for a in client.get_accounts(): balances[a] = client.get_balance(a) - origBalances[a] if verbose: print("CALL FAILURE:") print(e) return ("CALL FAILED", bytes(bin), balances) balances = {} for a in client.get_accounts(): balances[a] = client.get_balance(a) - origBalances[a] if verbose: print(balances) return (big_endian_to_int(decode_hex(val)), bytes(bin), balances)
def test_eth_sendRawTransaction(hex_accounts, client): tx = Transaction(0, tester.gas_price, tester.gas_limit, tester.accounts[1], 1234, '') tx.sign(tester.keys[0]) raw_tx = rlp.encode(tx) raw_tx_hex = encode_data(raw_tx) tx_hash = client.send_raw_transaction(raw_tx_hex) assert tx_hash tx_data = client.get_transaction_by_hash(tx_hash) assert tx_data['hash'] == tx_hash assert tx_data['from'] == hex_accounts[0] assert tx_data['to'] == hex_accounts[1]
def _client_call_emitter(method_signature, arguments=None): if arguments is None: arguments = [] function_sig = encode_data(sha3(method_signature)[:4]) data = function_sig + b''.join( (strip_0x(encode_number(arg, 32)) for arg in arguments)) assert len(data) == 2 + 8 + 64 * len(arguments) txn_hash = client.send_transaction( _from=accounts[0], to=emitter_contract_address, data=data, gas=200000, ) return txn_hash
def _rpc_call_emitter(method_signature, arguments=None): if arguments is None: arguments = [] function_sig = encode_data(sha3(method_signature)[:4]) data = function_sig + b''.join( (strip_0x(encode_number(arg, 32)) for arg in arguments)) assert len(data) == 2 + 8 + 64 * len(arguments) txn_hash = rpc_client( method="eth_sendTransaction", params=[{ '_from': accounts[0], 'to': rpc_emitter_contract_address, 'data': data, 'gas': 200000, }], ) return txn_hash
def test_eth_call(client, hex_accounts): txn_hash = client.send_transaction( _from=hex_accounts[0], data=CONTRACT_BIN, value=1234, ) txn_receipt = client.get_transaction_receipt(txn_hash) contract_address = txn_receipt['contractAddress'] assert contract_address function_sig = encode_data(sha3("return13()")[:4]) should_be_13 = client.call( _from=hex_accounts[0], to=contract_address, data=function_sig, ) result = big_endian_to_int(decode_hex(should_be_13[2:])) assert result == 13
def test_eth_sendRawTransaction(web3, wait_for_transaction, extra_accounts): private_key = mk_random_privkey() address = encode_address(privtoaddr(private_key)) funding_txn_hash = web3.eth.sendTransaction({ "from": web3.eth.coinbase, "to": address, "value": 10000000000000000, }) wait_for_transaction(web3, funding_txn_hash) # ethereum-tester-client doesn't quite implement the # `sendRawTransaction` correctly because of how the underlying tester # evm works. It needs to know about the address for this to work. web3.personal.importRawKey(private_key, "password") web3.personal.unlockAccount(address, "password") initial_balance = web3.eth.getBalance(extra_accounts[1]) tx = Transaction( web3.eth.getTransactionCount(address), web3.eth.gasPrice, 100000, extra_accounts[1], 1234, '', ) tx.sign(private_key) raw_tx = rlp.encode(tx) raw_tx_hex = encode_data(raw_tx) txn_hash = web3.eth.sendRawTransaction(raw_tx_hex) wait_for_transaction(web3, txn_hash) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) after_balance = web3.eth.getBalance(extra_accounts[1]) assert after_balance - initial_balance == 1234
def make_log_entry(block_number=1, type=b"mined", address=b"0xd3cda913deb6f67967b99d67acdfa1712c293601", topics=None, data=b""): if topics is None: topics = [] log_entry = { "type": type, "logIndex": "0x0", "transactionIndex": "0x0", "transactionHash": "0xebb0f76aa6a6bb8d178bc2b54ae8fd7ca778d703bf47d135c188ca2b6d25f2e4", "blockHash": "0xd2f44ad2d3702136acccacb5098829585e63b5e1e264b0e54c4d5af2edb87368", "blockNumber": encode_number(block_number), "address": address, "data": encode_data(data), "topics": topics, } return log_entry
def event_topic(event_signature): return encode_data(sha3(force_bytes(event_signature)))