|
8 | 8 |
|
9 | 9 | from eth_abi.tools._strategies import get_abi_strategy |
10 | 10 | from hypothesis import strategies |
11 | | -from pyk.utils import run_process |
| 11 | +from kontrol.solc_to_k import Contract as EVMContract |
| 12 | +from kontrol.solc_to_k import contract_name_with_path, method_sig_from_abi |
| 13 | +from pyk.kast.inner import KSort |
| 14 | +from pyk.utils import run_process, single |
12 | 15 |
|
13 | | -from .simulation import call_data |
| 16 | +from skribe.simulation import call_data |
14 | 17 |
|
15 | 18 | if TYPE_CHECKING: |
16 | 19 |
|
17 | 20 | from hypothesis.strategies import SearchStrategy |
18 | 21 |
|
19 | 22 |
|
20 | | -@dataclass(frozen=True) |
21 | | -class ContractBinding: |
22 | | - """Represents one of the function bindings for a Stylus contract.""" |
| 23 | +type Method = EVMContract.Method |
23 | 24 |
|
24 | | - name: str |
25 | | - inputs: tuple[str, ...] |
26 | | - outputs: tuple[str, ...] |
27 | 25 |
|
28 | | - @staticmethod |
29 | | - def from_dict(d: dict[str, Any]) -> ContractBinding: |
30 | | - name = d['name'] |
31 | | - inputs = tuple(inp['type'] for inp in d['inputs']) |
32 | | - outputs = tuple(out['type'] for out in d['outputs']) |
33 | | - return ContractBinding(name, inputs, outputs) |
| 26 | +@dataclass |
| 27 | +class StylusContract: |
| 28 | + contract_path: Path |
| 29 | + _cargo_bin: Path |
34 | 30 |
|
35 | | - @cached_property |
36 | | - def strategy(self) -> SearchStrategy[bytes]: |
37 | | - input_strategies = (get_abi_strategy(arg) for arg in self.inputs) |
38 | | - tuple_strategy = strategies.tuples(*input_strategies) |
39 | | - |
40 | | - encoder = partial(call_data, self.name, self.inputs) |
41 | | - return tuple_strategy.map(encoder) |
| 31 | + def __init__(self, cargo_bin: Path, contract_dir: Path): |
| 32 | + self.contract_path = contract_dir.resolve() |
| 33 | + self._cargo_bin = cargo_bin |
42 | 34 |
|
43 | 35 | @cached_property |
44 | | - def is_test(self) -> bool: |
45 | | - return self.name.startswith('test') |
| 36 | + def name_with_path(self) -> str: |
| 37 | + return contract_name_with_path(str(self.contract_path), self._name) |
46 | 38 |
|
47 | 39 | @cached_property |
48 | | - def arity(self) -> int: |
49 | | - return len(self.inputs) |
50 | | - |
| 40 | + def manifest_path(self) -> Path: |
| 41 | + return self.contract_path / 'Cargo.toml' |
51 | 42 |
|
52 | | -@dataclass(frozen=True) |
53 | | -class ContractMetadata: |
54 | | - manifest_path: Path |
55 | | - name: str |
56 | | - bindings: tuple[ContractBinding, ...] |
57 | | - target_dir: Path |
| 43 | + @cached_property |
| 44 | + def manifest(self) -> dict[str, Any]: |
| 45 | + return json.loads( |
| 46 | + run_process( |
| 47 | + [ |
| 48 | + str(self._cargo_bin), |
| 49 | + 'metadata', |
| 50 | + '--no-deps', |
| 51 | + '--manifest-path', |
| 52 | + str(self.manifest_path), |
| 53 | + '--format-version', |
| 54 | + '1', |
| 55 | + ], |
| 56 | + check=True, |
| 57 | + ).stdout |
| 58 | + ) |
58 | 59 |
|
59 | 60 | @cached_property |
60 | | - def wasm_target_dir(self) -> Path: |
61 | | - return (self.target_dir / 'wasm32-unknown-unknown' / 'release').resolve() |
| 61 | + def contract_package(self) -> dict[str, Any]: |
| 62 | + return single(p for p in self.manifest['packages'] if Path(p['manifest_path']) == self.manifest_path) |
62 | 63 |
|
63 | 64 | @cached_property |
64 | | - def wasm_path(self) -> Path: |
65 | | - wasm_file_name = self.name.replace('-', '_') + '.wasm' |
66 | | - wasm_path = self.wasm_target_dir / wasm_file_name |
67 | | - return wasm_path.resolve() |
| 65 | + def _name(self) -> str: |
| 66 | + return self.contract_package['name'] |
68 | 67 |
|
69 | 68 | @cached_property |
70 | | - def init_func(self) -> ContractBinding | None: |
71 | | - for b in self.bindings: |
72 | | - if b.name == 'init': |
73 | | - return b |
74 | | - return None |
| 69 | + def abi(self) -> list[dict[str, Any]]: |
| 70 | + proc_res = run_process( |
| 71 | + [str(self._cargo_bin), 'stylus', 'export-abi', '--json'], |
| 72 | + cwd=self.contract_path, |
| 73 | + check=True, |
| 74 | + ) |
| 75 | + json_output = proc_res.stdout.split('\n', 3)[3] # remove the headers |
| 76 | + return json.loads(json_output) |
75 | 77 |
|
76 | 78 | @cached_property |
77 | | - def has_init(self) -> bool: |
78 | | - return self.init_func is not None |
| 79 | + def methods(self) -> tuple[Method, ...]: |
| 80 | + return tuple( |
| 81 | + EVMContract.Method( |
| 82 | + msig=method_sig_from_abi(method_abi, True), |
| 83 | + id=0, |
| 84 | + abi=method_abi, |
| 85 | + ast=None, |
| 86 | + contract_name_with_path='', |
| 87 | + contract_digest='', |
| 88 | + contract_storage_digest='', |
| 89 | + sort=KSort(f'{EVMContract.escaped(self.name_with_path, "S2K")}Method'), |
| 90 | + devdoc=None, |
| 91 | + function_calls=None, |
| 92 | + ) |
| 93 | + for method_abi in self.abi |
| 94 | + if method_abi['type'] == 'function' |
| 95 | + ) |
79 | 96 |
|
80 | 97 | @cached_property |
81 | | - def test_functions(self) -> tuple[ContractBinding, ...]: |
82 | | - return tuple(b for b in self.bindings if b.is_test) |
83 | | - |
84 | | - def typecheck(self) -> None: |
85 | | - for b in self.bindings: |
86 | | - if b.name == 'init': |
87 | | - no_output = not b.outputs |
88 | | - only_address = all(i == 'address' for i in b.inputs) |
89 | | - if no_output and only_address: |
90 | | - continue |
91 | | - if not b.is_test: |
92 | | - continue |
93 | | - if not b.outputs: |
94 | | - continue |
95 | | - if b.outputs == ('bool',): |
96 | | - continue |
97 | | - |
98 | | - raise TypeError(f'Invalid type: {b.name}{b.inputs} -> {b.outputs}') |
99 | | - |
100 | | - |
101 | | -def read_contract_bindings(cargo_bin: Path, contract_dir: Path) -> tuple[ContractBinding, ...]: |
102 | | - """Reads a stylus wasm contract, and returns a list of the function bindings for it.""" |
103 | | - proc_res = run_process( |
104 | | - [str(cargo_bin), 'stylus', 'export-abi', '--json'], |
105 | | - cwd=contract_dir, |
106 | | - check=True, |
107 | | - ) |
108 | | - json_output = proc_res.stdout.split('\n', 3)[3] # remove the headers |
109 | | - bindings_list = json.loads(json_output) |
110 | | - |
111 | | - return tuple( |
112 | | - ContractBinding.from_dict(binding_dict) for binding_dict in bindings_list if binding_dict['type'] == 'function' |
113 | | - ) |
114 | | - |
115 | | - |
116 | | -def read_contract_metadata(cargo_bin: Path, contract_dir: Path) -> ContractMetadata: |
117 | | - manifest_path = (contract_dir / 'Cargo.toml').resolve() |
118 | | - proc_res = run_process( |
119 | | - [str(cargo_bin), 'metadata', '--no-deps', '--manifest-path', str(manifest_path), '--format-version', '1'], |
120 | | - check=True, |
121 | | - ) |
122 | | - manifest = json.loads(proc_res.stdout) |
123 | | - |
124 | | - # filter out other packages in the workspace and get the one that matches the contract |
125 | | - contract_package = [p for p in manifest['packages'] if Path(p['manifest_path']) == manifest_path][0] |
126 | | - name = contract_package['name'] |
127 | | - target_dir = Path(manifest['target_directory']).resolve() |
128 | | - |
129 | | - bindings = read_contract_bindings(cargo_bin, contract_dir) |
130 | | - |
131 | | - res = ContractMetadata( |
132 | | - manifest_path=manifest_path, |
133 | | - name=name, |
134 | | - bindings=bindings, |
135 | | - target_dir=target_dir, |
136 | | - ) |
137 | | - res.typecheck() |
138 | | - |
139 | | - return res |
| 98 | + def deployed_bytecode(self) -> bytes: |
| 99 | + wasm_file_name = self._name.replace('-', '_') + '.wasm' |
| 100 | + wasm_path = Path(self.manifest['target_directory']) / 'wasm32-unknown-unknown' / 'release' / wasm_file_name |
| 101 | + return wasm_path.read_bytes() |
| 102 | + |
| 103 | + |
| 104 | +type ArbitrumContract = EVMContract | StylusContract |
| 105 | + |
| 106 | + |
| 107 | +def setup_method(c: ArbitrumContract) -> Method | None: |
| 108 | + for m in c.methods: |
| 109 | + if m.name == 'setUp': |
| 110 | + return m |
| 111 | + return None |
| 112 | + |
| 113 | + |
| 114 | +def is_foundry_test(ctr: EVMContract) -> bool: |
| 115 | + if ctr.is_test_contract: |
| 116 | + for m in ctr.methods: |
| 117 | + if m.is_test: |
| 118 | + return True |
| 119 | + return False |
| 120 | + |
| 121 | + |
| 122 | +def argument_strategy(m: Method) -> SearchStrategy[bytes]: |
| 123 | + input_strategies = (get_abi_strategy(arg) for arg in m.arg_types) |
| 124 | + tuple_strategy = strategies.tuples(*input_strategies) |
| 125 | + encoder = partial(call_data, m.name, m.arg_types) |
| 126 | + return tuple_strategy.map(encoder) |
0 commit comments