Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ def __add__(self, other: "Storage") -> "Storage":
"""Return a new storage that is the sum of two storages."""
return Storage({**self.root, **other.root})

def __len__(self) -> int:
"""
Return the number of keys that have been explicitly set in the storage.
"""
return len(self.root)

def keys(self) -> set[StorageKeyValueType]:
"""Return the keys of the storage."""
return set(self.root.keys())
Expand Down
37 changes: 21 additions & 16 deletions packages/testing/src/execution_testing/cli/benchmark_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

import argparse
import ast
import json
import sys
from pathlib import Path

from execution_testing.cli.pytest_commands.plugins.shared.benchmarking import (
OpcodeCountsConfig,
)


def get_repo_root() -> Path:
"""Get the repository root directory."""
Expand Down Expand Up @@ -227,16 +230,11 @@ def scan_benchmark_tests(
return config, pattern_sources


def load_existing_config(config_file: Path) -> dict[str, list[int]]:
def load_existing_config(config_file: Path) -> OpcodeCountsConfig:
"""Load existing config from .fixed_opcode_counts.json."""
if not config_file.exists():
return {}

try:
data = json.loads(config_file.read_text())
return data.get("scenario_configs", {})
except (json.JSONDecodeError, KeyError):
return {}
return OpcodeCountsConfig()
return OpcodeCountsConfig.model_validate_json(config_file.read_bytes())


def categorize_patterns(
Expand Down Expand Up @@ -270,7 +268,8 @@ def categorize_patterns(
def generate_config_json(
config: dict[str, list[int]],
pattern_sources: dict[str, Path],
) -> str:
default_counts: list[int],
) -> OpcodeCountsConfig:
"""Generate the JSON config file content."""
categories = categorize_patterns(config, pattern_sources)

Expand All @@ -279,9 +278,10 @@ def generate_config_json(
for pattern in patterns:
scenario_configs[pattern] = config[pattern]

output = {"scenario_configs": scenario_configs}

return json.dumps(output, indent=2) + "\n"
return OpcodeCountsConfig(
scenario_configs=scenario_configs,
default_counts=default_counts,
)


def main() -> int:
Expand All @@ -307,7 +307,8 @@ def main() -> int:
detected, pattern_sources = scan_benchmark_tests(benchmark_dir)
print(f"Detected {len(detected)} opcode patterns")

existing = load_existing_config(config_file)
existing_file = load_existing_config(config_file)
existing = existing_file.scenario_configs
print(f"Loaded {len(existing)} existing entries")

detected_keys = set(detected.keys())
Expand Down Expand Up @@ -354,8 +355,12 @@ def main() -> int:
if pattern in merged:
del merged[pattern]

content = generate_config_json(merged, pattern_sources)
config_file.write_text(content)
content = generate_config_json(
merged, pattern_sources, existing_file.default_counts
)
config_file.write_text(
content.model_dump_json(exclude_defaults=True, indent=2)
)
print(f"\nUpdated {config_file}")
return 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def fund_eoa(
self,
amount: NumberConvertible | None = None,
label: str | None = None,
storage: Storage | None = None,
storage: Storage | StorageRootType | None = None,
delegation: Address | Literal["Self"] | None = None,
nonce: NumberConvertible | None = None,
) -> EOA:
Expand All @@ -432,14 +432,16 @@ def fund_eoa(
fund_tx: PendingTransaction | None = None
if delegation is not None or storage is not None:
if storage is not None:
if not isinstance(storage, Storage):
storage = Storage.model_validate(storage)
logger.debug(
f"Deploying storage contract for EOA {eoa} with {len(storage.root)} storage slots"
f"Deploying storage contract for EOA {eoa} with {len(storage)} storage slots"
)
sstore_address = self.deploy_contract(
code=(
sum(
Op.SSTORE(key, value)
for key, value in storage.root.items()
for key, value in storage.items()
)
+ Op.STOP
)
Expand All @@ -451,6 +453,7 @@ def fund_eoa(
set_storage_tx = PendingTransaction(
sender=self._sender,
to=eoa,
value=0,
authorization_list=[
AuthorizationTuple(
chain_id=self._chain_id,
Expand Down Expand Up @@ -498,7 +501,7 @@ def fund_eoa(
fund_tx = PendingTransaction(
sender=self._sender,
to=eoa,
value=amount,
value=amount if amount is not None else 0,
authorization_list=[
AuthorizationTuple(
chain_id=self._chain_id,
Expand Down
Loading
Loading