Skip to content

Commit

Permalink
Add EVM serialization (kkrt-labs#995)
Browse files Browse the repository at this point in the history
Time spent on this PR: 0.5

## Pull request type

Please check the type of change your PR introduces:

- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

Not possible to return `model.EVM` from a test

Resolves kkrt-labs#940 

## What is the new behavior?

Serialize model.EVM + included struct + fix when returning struct and
not struct*

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/995)
<!-- Reviewable:end -->
  • Loading branch information
ClementWalter committed Feb 27, 2024
1 parent 5f2d942 commit b0c62ee
Show file tree
Hide file tree
Showing 41 changed files with 581 additions and 586 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ resources/*
*.pb.gz
profile*.png
logs

tests/ef_tests/test_data
!tests/ef_tests/test_data/.gitkeep
1 change: 1 addition & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ lint:
- logs*
- lib*
- resources*
- tests/ef_tests/test_data
actions:
disabled:
- trunk-announce
Expand Down
23 changes: 5 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
# The release tag of https://github.com/ethereum/tests to use for EF tests
EF_TESTS_TAG := v12.4
EF_TESTS_URL := https://github.com/ethereum/tests/archive/refs/tags/$(EF_TESTS_TAG).tar.gz
EF_TESTS_DIR := ./tests/ef_tests/test_data

# Downloads and unpacks Ethereum Foundation tests in the `$(EF_TESTS_DIR)` directory.
# Requires `wget` and `tar`
$(EF_TESTS_DIR):
mkdir -p $(EF_TESTS_DIR)
wget $(EF_TESTS_URL) -O ethereum-tests.tar.gz
tar -xzf ethereum-tests.tar.gz --strip-components=1 -C $(EF_TESTS_DIR)
rm ethereum-tests.tar.gz


.PHONY: build test coverage $(EF_TESTS_DIR)

# Include .env file to get GITHUB_TOKEN
ifneq ("$(wildcard .env)","")
include .env
Expand All @@ -32,6 +16,9 @@ build: check
check:
poetry check --lock

fetch-ef-tests:
poetry run python ./scripts/ef_tests/fetch.py

# This action fetches the latest Kakarot SSJ (Cairo compiler version >=2) artifacts
# from the main branch and unzips them into the build/ssj directory.
# This is required because Kakarot Zero (Cairo Zero, compiler version <1) uses some SSJ Cairo programs.
Expand All @@ -47,11 +34,11 @@ setup: fetch-ssj-artifacts
poetry install

test: build-sol deploy
poetry run pytest tests/src -m "not EFTests" --log-cli-level=INFO -n logical
poetry run pytest tests/src -m "not NoCI" --log-cli-level=INFO -n logical
poetry run pytest tests/end_to_end

test-unit:
poetry run pytest tests/src -n logical
poetry run pytest tests/src -m "not NoCI" -n logical

test-end-to-end: build-sol deploy
poetry run pytest tests/end_to_end
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ markers = [
"EFTests",
"SSTORE",
"SLOAD",
"NoCI",
]
env = [
"PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION = python",
Expand Down
22 changes: 9 additions & 13 deletions scripts/ef-tests/debug.py → scripts/ef_tests/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from eth.vm.forks.shanghai.blocks import ShanghaiBlock
from web3 import Web3

from scripts.ef_tests.fetch import EF_TESTS_PARSED_DIR

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand All @@ -22,7 +24,7 @@
TEST_NAME = os.getenv("TEST_NAME")
if TEST_NAME is None:
raise ValueError("Please set TEST_NAME")
TEST_PARENT_FOLDER = os.getenv("TEST_PARENT_FOLDER")
TEST_PARENT_FOLDER = os.getenv("TEST_PARENT_FOLDER", "")
RPC_ENDPOINT = "http://127.0.0.1:8545"


Expand Down Expand Up @@ -59,29 +61,23 @@ def signal_handler(self, signum, _):

def get_test_file():
tests = [
(content, str(file_path))
for file_path in TESTS_PATH.glob("**/*.json")
for name, content in json.load(open(file_path)).items()
if TEST_NAME in name and content["network"] == "Shanghai"
file_name
for file_name in os.listdir(EF_TESTS_PARSED_DIR)
if TEST_NAME in file_name and TEST_PARENT_FOLDER in file_name
]

if len(tests) == 0:
raise ValueError(f"Test {TEST_NAME} not found")

if len(tests) > 1:
if TEST_PARENT_FOLDER is None:
if TEST_PARENT_FOLDER == "":
raise ValueError(
f"Test {TEST_NAME} is ambiguous, please set TEST_PARENT_FOLDER to test file folder"
)

test = [content for (content, path) in tests if TEST_PARENT_FOLDER in path]
if len(test) == 0:
raise ValueError(f"Test {TEST_NAME} not found")

return test[0]
raise ValueError(f"Test {TEST_NAME} not found")

else:
return tests[0][0]
return json.loads((EF_TESTS_PARSED_DIR / tests[0]).read_text())


def connect_anvil():
Expand Down
52 changes: 52 additions & 0 deletions scripts/ef_tests/fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import io
import json
import logging
import os
import shutil
import tarfile
from pathlib import Path

import requests

EF_TESTS_TAG = "v12.4"
EF_TESTS_URL = (
f"https://github.com/ethereum/tests/archive/refs/tags/{EF_TESTS_TAG}.tar.gz"
)
EF_TESTS_DIR = Path("tests") / "ef_tests" / "test_data" / EF_TESTS_TAG
EF_TESTS_PARSED_DIR = Path("tests") / "ef_tests" / "test_data" / "parsed"

DEFAULT_NETWORK = "Shanghai"

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def generate_tests():
if not EF_TESTS_DIR.exists():
response = requests.get(EF_TESTS_URL)
with tarfile.open(fileobj=io.BytesIO(response.content), mode="r:gz") as tar:
tar.extractall(EF_TESTS_DIR)

test_cases = {
f"{os.path.basename(root)}_{name}": content
for (root, _, files) in os.walk(EF_TESTS_DIR)
for file in files
if file.endswith(".json") and "BlockchainTests/GeneralStateTests" in root
for name, content in json.loads((Path(root) / file).read_text()).items()
if content.get("network") == DEFAULT_NETWORK
}

shutil.rmtree(EF_TESTS_PARSED_DIR, ignore_errors=True)
EF_TESTS_PARSED_DIR.mkdir(parents=True, exist_ok=True)

for test_name, test_case in test_cases.items():
json.dump(
test_case,
open(EF_TESTS_PARSED_DIR / f"{test_name}.json", "w"),
indent=4,
)


if __name__ == "__main__":
generate_tests()
File renamed without changes.
3 changes: 3 additions & 0 deletions scripts/utils/kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def get_contract(
bytecode=target_compilation_output["bytecode"]["object"],
),
)
contract.bytecode_runtime = HexBytes(
target_compilation_output["deployedBytecode"]["object"]
)

try:
for fun in contract.functions:
Expand Down
1 change: 0 additions & 1 deletion tests/ef_tests/.gitignore

This file was deleted.

174 changes: 0 additions & 174 deletions tests/ef_tests/conftest.py

This file was deleted.

Empty file.
Loading

0 comments on commit b0c62ee

Please sign in to comment.