Skip to content

Commit

Permalink
initial fix of not logging function calls in hardhat
Browse files Browse the repository at this point in the history
  • Loading branch information
anishnaik committed Jul 11, 2022
1 parent 2f66a9f commit 6fb5725
Show file tree
Hide file tree
Showing 31 changed files with 22,758 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ build
dist
venv/
*egg*
init.json
init.json
tests/drizzle/node_modules/
3 changes: 2 additions & 1 deletion etheno/etheno.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def post(self, data):
else:
if method == 'eth_getTransactionReceipt':
# for eth_getTransactionReceipt, make sure we block until all clients have mined the transaction
print(data['params'][0])
ret = self.master_client.wait_for_transaction(data['params'][0])
if 'id' in data and 'id' in ret:
ret['id'] = data['id']
Expand All @@ -209,7 +210,7 @@ def post(self, data):
except JSONRPCError as e:
self.logger.error(e)
ret = e

self.rpc_client_result = ret
self.logger.debug(f"Result from the master client ({self.master_client}): {ret}")

Expand Down
40 changes: 36 additions & 4 deletions etheno/jsonrpc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
from typing import Dict, TextIO, Union

from pyrsistent import m

from .etheno import EthenoPlugin
from .utils import format_hex_address
from .client import JSONRPCError
Expand Down Expand Up @@ -57,6 +59,14 @@ def decode_raw_tx(raw_tx: str):
'v': tx.v
}

def get_transaction_by_receipt_object(tx_hash: int) -> Dict:
tx_hash = hex(tx_hash)
return {
'id': 1,
'jsonrpc': '2.0',
'method': 'eth_getTransactionReceipt',
'params': [tx_hash]
}

class JSONExporter:
def __init__(self, out_stream: Union[str, TextIO]):
Expand Down Expand Up @@ -114,6 +124,14 @@ def handle_contract_created(self, creator_address: str, contract_address: str, g
def handle_function_call(self, from_address: str, to_address: str, gas_used: str, gas_price: str, data: str, value: str):
self.logger.info(f'Function call with {value} wei from {from_address} to {to_address} with {(len(data)-2)//2} bytes of data for {gas_used} gas with a gas price of {gas_price}')

def handle_unlogged_transactions(self):
unlogged_transactions = dict(filter(lambda txn: txn[1]["is_logged"] == False, self._transactions.items()))
for (tx_hash, txn) in unlogged_transactions.items():
# might not need first conditional
post_data = get_transaction_by_receipt_object(tx_hash)
self._etheno.post(post_data)


def after_post(self, post_data, result):
if len(result):
result = result[0]
Expand All @@ -128,11 +146,19 @@ def after_post(self, post_data, result):
try:
transaction_hash = int(result['result'], 16)
except ValueError:
self.logger.error(f'Unable to parse transaction with the following post data: {post_data} and result: {result}')
return
# Add a boolean to check at shutdown whether everything has been logged.
if post_data['method'] == 'eth_sendRawTransaction':
self._transactions[transaction_hash] = decode_raw_tx(post_data['params'][0])
self._transactions[transaction_hash] = {
"transaction": decode_raw_tx(post_data['params'][0]),
"is_logged": False
}
else:
self._transactions[transaction_hash] = post_data['params'][0]
self._transactions[transaction_hash] = {
"transaction": post_data['params'][0],
"is_logged": False
}
elif post_data['method'] == 'evm_mine':
self.handle_increase_block_number()
elif post_data['method'] == 'evm_increaseTime':
Expand All @@ -142,7 +168,11 @@ def after_post(self, post_data, result):
if transaction_hash not in self._transactions:
self.logger.error(f'Received transaction receipt {result} for unknown transaction hash {post_data["params"][0]}')
return
original_transaction = self._transactions[transaction_hash]
(original_transaction, is_logged) = self._transactions[transaction_hash]["transaction"], self._transactions[transaction_hash]["is_logged"]
# Check if it was logged already
if is_logged:
self.logger.debug(f"Transaction hash {transaction_hash} has already been logged. This should not happen.")
return
if 'value' not in original_transaction or original_transaction['value'] is None:
value = '0x0'
else:
Expand All @@ -155,7 +185,8 @@ def after_post(self, post_data, result):
self.handle_contract_created(original_transaction['from'], contract_address, result['result']['gasUsed'], result['result']['effectiveGasPrice'], original_transaction['data'], value)
else:
self.handle_function_call(original_transaction['from'], original_transaction['to'], result['result']['gasUsed'], result['result']['effectiveGasPrice'], original_transaction['data'] if 'data' in original_transaction else '0x', value)

# Transaction has been logged successfully
self._transactions[transaction_hash]["is_logged"] = True

class EventSummaryExportPlugin(EventSummaryPlugin):
def __init__(self, out_stream: Union[str, TextIO]):
Expand Down Expand Up @@ -209,6 +240,7 @@ def handle_function_call(self, from_address: str, to_address: str, gas_used: str
super().handle_function_call(from_address, to_address, gas_used, gas_price, data, value)

def finalize(self):
super().handle_unlogged_transactions()
self._exporter.finalize()
if hasattr(self._exporter.output, 'name'):
self.logger.info(f'Event summary JSON saved to {self._exporter.output.name}')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/5ae0f1ecc55f7f1fa914a3ef06d7b005.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "Context",
"sourceName": "@openzeppelin/contracts/GSN/Context.sol",
"abi": [
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../build-info/5ae0f1ecc55f7f1fa914a3ef06d7b005.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "SafeMath",
"sourceName": "@openzeppelin/contracts/math/SafeMath.sol",
"abi": [],
"bytecode": "0x604c602c600b82828239805160001a60731460008114601c57601e565bfe5b5030600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea165627a7a7230582099b1b7946a12fd64ef5e2070fe576a16463c6e13672e3bfd5bc4749e45c8164d0029",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea165627a7a7230582099b1b7946a12fd64ef5e2070fe576a16463c6e13672e3bfd5bc4749e45c8164d0029",
"linkReferences": {},
"deployedLinkReferences": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/5ae0f1ecc55f7f1fa914a3ef06d7b005.json"
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../../../../build-info/5ae0f1ecc55f7f1fa914a3ef06d7b005.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "IERC20",
"sourceName": "@openzeppelin/contracts/token/ERC20/IERC20.sol",
"abi": [
{
"constant": false,
"inputs": [
{
"name": "spender",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "sender",
"type": "address"
},
{
"name": "recipient",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "recipient",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "owner",
"type": "address"
},
{
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}
Loading

0 comments on commit 6fb5725

Please sign in to comment.