Skip to content

Commit

Permalink
feat: get_call_tree implementation (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jun 17, 2022
1 parent 18be1bc commit 84d8761
Show file tree
Hide file tree
Showing 73 changed files with 595 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
run: npm install --save-dev hardhat

- name: Run Tests
run: pytest -n auto -m "not fuzzing" -m "not fork"
run: pytest -n auto -m "not fork and not manual and not fuzzing"

- name: Run Fork Tests
run: pytest -m fork
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.960
rev: v0.961
hooks:
- id: mypy
additional_dependencies: [types-requests]
Expand Down
26 changes: 15 additions & 11 deletions ape_hardhat/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ape.utils import cached_property
from ape_test import Config as TestConfig
from eth_utils import is_0x_prefixed, to_hex
from evm_trace import TraceFrame
from evm_trace import CallTreeNode, CallType, TraceFrame, get_calltree_from_geth_trace
from web3 import HTTPProvider, Web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.middleware import geth_poa_middleware
Expand Down Expand Up @@ -271,7 +271,7 @@ def _start(self):
port = random.randint(EPHEMERAL_PORTS_START, EPHEMERAL_PORTS_END)
attempts += 1
if attempts == max_attempts:
ports_str = ", ".join(self.attempted_ports)
ports_str = ", ".join([str(p) for p in self.attempted_ports])
raise HardhatProviderError(
f"Unable to find an available port. Ports tried: {ports_str}"
)
Expand Down Expand Up @@ -361,19 +361,23 @@ def send_transaction(self, txn: TransactionAPI) -> ReceiptAPI:
return receipt

def get_transaction_trace(self, txn_hash: str) -> Iterator[TraceFrame]:
"""
Provide a detailed description of opcodes.
Args:
txn_hash (str): The hash of a transaction to trace.
Returns:
Iterator(TraceFrame): Transaction execution trace object.
"""
frames = self._make_request("debug_traceTransaction", [txn_hash]).structLogs
for frame in frames:
yield TraceFrame(**frame)

def get_call_tree(self, txn_hash: str) -> CallTreeNode:
receipt = self.get_transaction(txn_hash)
root_node_kwargs = {
"gas_cost": receipt.gas_used,
"gas_limit": receipt.gas_limit,
"address": receipt.receiver,
"calldata": receipt.data,
"value": receipt.value,
"call_type": CallType.CALL,
"failed": receipt.failed,
}
return get_calltree_from_geth_trace(receipt.trace, **root_node_kwargs)

def set_balance(self, account: AddressType, amount: Union[int, float, str, bytes]):
is_str = isinstance(amount, str)
is_hex = False if not is_str else is_0x_prefixed(str(amount))
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
requires = ["setuptools>=51.1.1", "wheel", "setuptools_scm[toml]>=5.0"]

[tool.mypy]
exclude = "build/"
exclude = """
build/
tests/functional/data
"""

[tool.setuptools_scm]
write_to = "ape_hardhat/version.py"
Expand Down Expand Up @@ -31,6 +34,7 @@ testpaths = "tests"
markers = """
fuzzing: Run Hypothesis fuzz test suite
fork: Run forked-network tests using Alchemy as the upstream provider
manual: Tests you want to avoid running in automated CI/CD because of expense
"""

[tool.isort]
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ exclude =
venv*
docs
build
per-file-ignores =
# The traces have to be formatted this way for the tests.
tests/expected_traces.py: E501
10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
extras_require = {
"test": [ # `test` GitHub Action jobs uses this
"pytest>=6.0,<7.0", # Core testing package
"pytest-mock", # For patching and mocking
"pytest-xdist", # multi-process runner
"pytest-cov", # Coverage analyzer plugin
"hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
"ape-alchemy",
"ape-alchemy>=0.3.0", # Needed for forked-network tests
"rich", # Needed for trace tests
],
"lint": [
"black>=22.3.0,<23.0", # auto-formatter and linter
Expand Down Expand Up @@ -68,12 +70,12 @@
url="https://github.com/ApeWorX/ape-hardhat",
include_package_data=True,
install_requires=[
"eth-ape>=0.2.8,<0.3.0",
"eth-ape>=0.3.0,<0.4.0",
"importlib-metadata ; python_version<'3.8'",
"evm-trace>=0.1.0.a3",
"evm-trace>=0.1.0.a6",
"hexbytes", # Use same as eth-ape
"web3", # Use same as eth-ape
], # NOTE: Add 3rd party libraries here
],
python_requires=">=3.7.2,<4",
extras_require=extras_require,
py_modules=["ape_hardhat"],
Expand Down
11 changes: 0 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,3 @@ def method(port: int = 9001, network: str = "mainnet"):
@pytest.fixture(scope="session")
def mainnet_fork_network_api(networks):
return networks.ecosystems["ethereum"]["mainnet-fork"]


@pytest.fixture(scope="session")
def connected_mainnet_fork_provider(networks):
with networks.parse_network_choice("ethereum:mainnet-fork:hardhat") as provider:
yield provider


@pytest.fixture(scope="module")
def fork_contract_instance(owner, contract_container, connected_mainnet_fork_provider):
return owner.deploy(contract_container)
1 change: 1 addition & 0 deletions tests/data/contracts/ethereum/local/contract_a.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/data/contracts/ethereum/local/contract_b.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/data/contracts/ethereum/local/contract_c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"abi":[{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"addressToValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"addresses","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getSomeList","outputs":[{"internalType":"uint128[3]","name":"","type":"uint128[3]"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"string","name":"windows95","type":"string"},{"internalType":"uint256","name":"jamaica","type":"uint256"},{"internalType":"address","name":"cardinal","type":"address"}],"name":"methodC1","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"methodC2","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"paperwork","outputs":[{"internalType":"string","name":"os","type":"string"},{"internalType":"uint256","name":"country","type":"uint256"},{"internalType":"address","name":"wings","type":"address"}],"stateMutability":"view","type":"function"}],"contractName":"ContractC","deploymentBytecode":{"bytecode":"0x608060405234801561001057600080fd5b50610cf1806100206000396000f3fe6080604052600436106100555760003560e01c80637007cbe81461005a578063878fb7011461008557806390bb7141146100a1578063bff2e095146100ab578063e5e1d93f146100e8578063edf26d9b14610127575b600080fd5b34801561006657600080fd5b5061006f610164565b60405161007c919061077e565b60405180910390f35b61009f600480360381019061009a9190610987565b6101e9565b005b6100a96103cb565b005b3480156100b757600080fd5b506100d260048036038101906100cd91906109f6565b6104c9565b6040516100df9190610a32565b60405180910390f35b3480156100f457600080fd5b5061010f600480360381019061010a91906109f6565b6104e1565b60405161011e93929190610ae4565b60405180910390f35b34801561013357600080fd5b5061014e60048036038101906101499190610b22565b6105b3565b60405161015b9190610b4f565b60405180910390f35b61016c6105f2565b60405180606001604052806f0293b0e3558d33b8a4c483e40e2b8db96fffffffffffffffffffffffffffffffff16815260200167018b932eebcc7eb96fffffffffffffffffffffffffffffffff1681526020016ebf550935e92f79f09e3530df8660c56fffffffffffffffffffffffffffffffff16815250905090565b600034111561022d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161022490610bb6565b60405180910390fd5b346000808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461027b9190610c05565b925050819055506001819080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060405180606001604052808481526020018381526020018273ffffffffffffffffffffffffffffffffffffffff16815250600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000820151816000019080519060200190610371929190610614565b506020820151816001015560408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550905050505050565b600034111561040f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161040690610bb6565b60405180910390fd5b346000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461045d9190610c05565b925050819055506001339080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b60006020528060005260406000206000915090505481565b600260205280600052604060002060009150905080600001805461050490610c8a565b80601f016020809104026020016040519081016040528092919081815260200182805461053090610c8a565b801561057d5780601f106105525761010080835404028352916020019161057d565b820191906000526020600020905b81548152906001019060200180831161056057829003601f168201915b5050505050908060010154908060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905083565b600181815481106105c357600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6040518060600160405280600390602082028036833780820191505090505090565b82805461062090610c8a565b90600052602060002090601f0160209004810192826106425760008555610689565b82601f1061065b57805160ff1916838001178555610689565b82800160010185558215610689579182015b8281111561068857825182559160200191906001019061066d565b5b509050610696919061069a565b5090565b5b808211156106b357600081600090555060010161069b565b5090565b600060039050919050565b600081905092915050565b6000819050919050565b60006fffffffffffffffffffffffffffffffff82169050919050565b6106fc816106d7565b82525050565b600061070e83836106f3565b60208301905092915050565b6000602082019050919050565b610730816106b7565b61073a81846106c2565b9250610745826106cd565b8060005b8381101561077657815161075d8782610702565b96506107688361071a565b925050600181019050610749565b505050505050565b60006060820190506107936000830184610727565b92915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610800826107b7565b810181811067ffffffffffffffff8211171561081f5761081e6107c8565b5b80604052505050565b6000610832610799565b905061083e82826107f7565b919050565b600067ffffffffffffffff82111561085e5761085d6107c8565b5b610867826107b7565b9050602081019050919050565b82818337600083830152505050565b600061089661089184610843565b610828565b9050828152602081018484840111156108b2576108b16107b2565b5b6108bd848285610874565b509392505050565b600082601f8301126108da576108d96107ad565b5b81356108ea848260208601610883565b91505092915050565b6000819050919050565b610906816108f3565b811461091157600080fd5b50565b600081359050610923816108fd565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061095482610929565b9050919050565b61096481610949565b811461096f57600080fd5b50565b6000813590506109818161095b565b92915050565b6000806000606084860312156109a05761099f6107a3565b5b600084013567ffffffffffffffff8111156109be576109bd6107a8565b5b6109ca868287016108c5565b93505060206109db86828701610914565b92505060406109ec86828701610972565b9150509250925092565b600060208284031215610a0c57610a0b6107a3565b5b6000610a1a84828501610972565b91505092915050565b610a2c816108f3565b82525050565b6000602082019050610a476000830184610a23565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610a87578082015181840152602081019050610a6c565b83811115610a96576000848401525b50505050565b6000610aa782610a4d565b610ab18185610a58565b9350610ac1818560208601610a69565b610aca816107b7565b840191505092915050565b610ade81610949565b82525050565b60006060820190508181036000830152610afe8186610a9c565b9050610b0d6020830185610a23565b610b1a6040830184610ad5565b949350505050565b600060208284031215610b3857610b376107a3565b5b6000610b4684828501610914565b91505092915050565b6000602082019050610b646000830184610ad5565b92915050565b7f216d6f6e65790000000000000000000000000000000000000000000000000000600082015250565b6000610ba0600683610a58565b9150610bab82610b6a565b602082019050919050565b60006020820190508181036000830152610bcf81610b93565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610c10826108f3565b9150610c1b836108f3565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610c5057610c4f610bd6565b5b828201905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610ca257607f821691505b602082108103610cb557610cb4610c5b565b5b5091905056fea26469706673582212207548d14efaf6ba85f19c50fbdf4dcc7f15ee0bc04066d87d04d3b6a1107feed864736f6c634300080e0033"},"devdoc":{"kind":"dev","methods":{},"version":1},"runtimeBytecode":{"bytecode":"0x608060405234801561001057600080fd5b50610cf1806100206000396000f3fe6080604052600436106100555760003560e01c80637007cbe81461005a578063878fb7011461008557806390bb7141146100a1578063bff2e095146100ab578063e5e1d93f146100e8578063edf26d9b14610127575b600080fd5b34801561006657600080fd5b5061006f610164565b60405161007c919061077e565b60405180910390f35b61009f600480360381019061009a9190610987565b6101e9565b005b6100a96103cb565b005b3480156100b757600080fd5b506100d260048036038101906100cd91906109f6565b6104c9565b6040516100df9190610a32565b60405180910390f35b3480156100f457600080fd5b5061010f600480360381019061010a91906109f6565b6104e1565b60405161011e93929190610ae4565b60405180910390f35b34801561013357600080fd5b5061014e60048036038101906101499190610b22565b6105b3565b60405161015b9190610b4f565b60405180910390f35b61016c6105f2565b60405180606001604052806f0293b0e3558d33b8a4c483e40e2b8db96fffffffffffffffffffffffffffffffff16815260200167018b932eebcc7eb96fffffffffffffffffffffffffffffffff1681526020016ebf550935e92f79f09e3530df8660c56fffffffffffffffffffffffffffffffff16815250905090565b600034111561022d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161022490610bb6565b60405180910390fd5b346000808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461027b9190610c05565b925050819055506001819080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060405180606001604052808481526020018381526020018273ffffffffffffffffffffffffffffffffffffffff16815250600260008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000820151816000019080519060200190610371929190610614565b506020820151816001015560408201518160020160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550905050505050565b600034111561040f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161040690610bb6565b60405180910390fd5b346000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461045d9190610c05565b925050819055506001339080600181540180825580915050600190039060005260206000200160009091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b60006020528060005260406000206000915090505481565b600260205280600052604060002060009150905080600001805461050490610c8a565b80601f016020809104026020016040519081016040528092919081815260200182805461053090610c8a565b801561057d5780601f106105525761010080835404028352916020019161057d565b820191906000526020600020905b81548152906001019060200180831161056057829003601f168201915b5050505050908060010154908060020160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905083565b600181815481106105c357600080fd5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6040518060600160405280600390602082028036833780820191505090505090565b82805461062090610c8a565b90600052602060002090601f0160209004810192826106425760008555610689565b82601f1061065b57805160ff1916838001178555610689565b82800160010185558215610689579182015b8281111561068857825182559160200191906001019061066d565b5b509050610696919061069a565b5090565b5b808211156106b357600081600090555060010161069b565b5090565b600060039050919050565b600081905092915050565b6000819050919050565b60006fffffffffffffffffffffffffffffffff82169050919050565b6106fc816106d7565b82525050565b600061070e83836106f3565b60208301905092915050565b6000602082019050919050565b610730816106b7565b61073a81846106c2565b9250610745826106cd565b8060005b8381101561077657815161075d8782610702565b96506107688361071a565b925050600181019050610749565b505050505050565b60006060820190506107936000830184610727565b92915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b610800826107b7565b810181811067ffffffffffffffff8211171561081f5761081e6107c8565b5b80604052505050565b6000610832610799565b905061083e82826107f7565b919050565b600067ffffffffffffffff82111561085e5761085d6107c8565b5b610867826107b7565b9050602081019050919050565b82818337600083830152505050565b600061089661089184610843565b610828565b9050828152602081018484840111156108b2576108b16107b2565b5b6108bd848285610874565b509392505050565b600082601f8301126108da576108d96107ad565b5b81356108ea848260208601610883565b91505092915050565b6000819050919050565b610906816108f3565b811461091157600080fd5b50565b600081359050610923816108fd565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061095482610929565b9050919050565b61096481610949565b811461096f57600080fd5b50565b6000813590506109818161095b565b92915050565b6000806000606084860312156109a05761099f6107a3565b5b600084013567ffffffffffffffff8111156109be576109bd6107a8565b5b6109ca868287016108c5565b93505060206109db86828701610914565b92505060406109ec86828701610972565b9150509250925092565b600060208284031215610a0c57610a0b6107a3565b5b6000610a1a84828501610972565b91505092915050565b610a2c816108f3565b82525050565b6000602082019050610a476000830184610a23565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610a87578082015181840152602081019050610a6c565b83811115610a96576000848401525b50505050565b6000610aa782610a4d565b610ab18185610a58565b9350610ac1818560208601610a69565b610aca816107b7565b840191505092915050565b610ade81610949565b82525050565b60006060820190508181036000830152610afe8186610a9c565b9050610b0d6020830185610a23565b610b1a6040830184610ad5565b949350505050565b600060208284031215610b3857610b376107a3565b5b6000610b4684828501610914565b91505092915050565b6000602082019050610b646000830184610ad5565b92915050565b7f216d6f6e65790000000000000000000000000000000000000000000000000000600082015250565b6000610ba0600683610a58565b9150610bab82610b6a565b602082019050919050565b60006020820190508181036000830152610bcf81610b93565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000610c10826108f3565b9150610c1b836108f3565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115610c5057610c4f610bd6565b5b828201905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680610ca257607f821691505b602082108103610cb557610cb4610c5b565b5b5091905056fea26469706673582212207548d14efaf6ba85f19c50fbdf4dcc7f15ee0bc04066d87d04d3b6a1107feed864736f6c634300080e0033"},"sourceId":"ContractC.sol","userdoc":{"kind":"user","methods":{},"version":1}}
Loading

0 comments on commit 84d8761

Please sign in to comment.