-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: kakarot file architecture (#343)
- Loading branch information
1 parent
2ff5841
commit 16f0a41
Showing
80 changed files
with
407 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +0,0 @@ | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
|
||
class Object: | ||
pass | ||
|
||
|
||
def update(ref: dict, new: dict) -> dict: | ||
"""Update ref recursively with new.""" | ||
for key, value in new.items(): | ||
if isinstance(value, dict): | ||
ref[key] = update(ref.get(key, {}), value) | ||
else: | ||
ref[key] = value | ||
return ref | ||
|
||
|
||
def objectify(parent: object, attributes: dict) -> object: | ||
"""Set attributes recursively to parent object.""" | ||
for key, value in attributes.items(): | ||
if isinstance(value, dict): | ||
value = objectify(Object(), value) | ||
setattr(parent, key, value) | ||
return parent | ||
|
||
|
||
def load(path: str, context: object): | ||
"""Read config files and setup context.""" | ||
# load config | ||
path = Path(path) | ||
config = {} | ||
for parent in reversed(path.parents): | ||
config_path = parent / path.name | ||
if not config_path.exists(): | ||
continue | ||
with open(config_path, "r") as file_instance: | ||
update(config, yaml.safe_load(file_instance)) | ||
|
||
# set up context | ||
context = objectify(context, config) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import logging | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
from typing import AsyncGenerator | ||
|
||
import pandas as pd | ||
import pytest_asyncio | ||
from cairo_coverage import cairo_coverage | ||
from starkware.starknet.business_logic.execution.execute_entry_point import ( | ||
ExecuteEntryPoint, | ||
) | ||
from starkware.starknet.business_logic.state.state_api_objects import BlockInfo | ||
from starkware.starknet.testing.starknet import Starknet | ||
|
||
from tests.utils.reporting import dump_coverage, dump_reports, timeit, traceit | ||
|
||
pd.set_option("display.max_rows", 500) | ||
pd.set_option("display.max_columns", 500) | ||
pd.set_option("display.width", 1000) | ||
|
||
logging.getLogger("asyncio").setLevel(logging.ERROR) | ||
logger = logging.getLogger() | ||
|
||
|
||
@pytest_asyncio.fixture(scope="session") | ||
async def starknet(worker_id, request) -> AsyncGenerator[Starknet, None]: | ||
starknet = await Starknet.empty() | ||
starknet.state.state.update_block_info( | ||
BlockInfo.create_for_testing(block_number=1, block_timestamp=1) | ||
) | ||
|
||
starknet.deploy = traceit.trace_all(timeit(starknet.deploy)) | ||
starknet.declare = timeit(starknet.declare) | ||
if request.config.getoption("trace_run"): | ||
logger.info("trace-run option enabled") | ||
ExecuteEntryPoint._run = traceit.trace_run(ExecuteEntryPoint._run) | ||
else: | ||
logger.info("trace-run option disabled") | ||
output_dir = Path("coverage") | ||
shutil.rmtree(output_dir, ignore_errors=True) | ||
|
||
yield starknet | ||
|
||
output_dir.mkdir(exist_ok=True, parents=True) | ||
files = cairo_coverage.report_runs(excluded_file={"site-packages", "tests"}) | ||
total_covered = [] | ||
for file in files: | ||
if file.pct_covered < 80: | ||
logger.warning(f"{file.name} only {file.pct_covered:.2f}% covered") | ||
total_covered.append(file.pct_covered) | ||
if files and (val := not sum(total_covered) / len(files)) >= 80: | ||
logger.warning(f"Project is not covered enough {val:.2f})") | ||
|
||
if worker_id == "master": | ||
dump_reports(output_dir) | ||
dump_coverage(output_dir, files) | ||
else: | ||
dump_reports(output_dir / worker_id) | ||
dump_coverage(output_dir / worker_id, files) | ||
if len(os.listdir(output_dir)) == int(os.environ["PYTEST_XDIST_WORKER_COUNT"]): | ||
# This is the last teardown of the testsuite, merge the files | ||
resources = pd.concat( | ||
[pd.read_csv(f) for f in output_dir.glob("**/resources.csv")], | ||
) | ||
if not resources.empty: | ||
resources.sort_values(["n_steps"], ascending=False).to_csv( | ||
output_dir / "resources.csv", index=False | ||
) | ||
times = pd.concat( | ||
[pd.read_csv(f) for f in output_dir.glob("**/times.csv")], | ||
ignore_index=True, | ||
) | ||
if not times.empty: | ||
times.sort_values(["duration"], ascending=False).to_csv( | ||
output_dir / "times.csv", index=False | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import pytest_asyncio | ||
from starkware.starknet.testing.contract import DeclaredClass, StarknetContract | ||
from starkware.starknet.testing.starknet import Starknet | ||
|
||
|
||
@pytest_asyncio.fixture(scope="session") | ||
async def eth(starknet: Starknet): | ||
return await starknet.deploy( | ||
source="./tests/fixtures/ERC20.cairo", | ||
constructor_calldata=[2] * 6, | ||
# Uint256(2, 2) tokens to 2 | ||
) | ||
|
||
|
||
@pytest_asyncio.fixture(scope="session") | ||
async def contract_account_class(starknet: Starknet): | ||
return await starknet.declare( | ||
source="./src/kakarot/accounts/contract/contract_account.cairo", | ||
cairo_path=["src"], | ||
disable_hint_validation=True, | ||
) | ||
|
||
|
||
@pytest_asyncio.fixture(scope="package") | ||
async def kakarot( | ||
starknet: Starknet, eth: StarknetContract, contract_account_class: DeclaredClass | ||
) -> StarknetContract: | ||
return await starknet.deploy( | ||
source="./src/kakarot/kakarot.cairo", | ||
cairo_path=["src"], | ||
disable_hint_validation=True, | ||
constructor_calldata=[ | ||
1, | ||
eth.contract_address, | ||
contract_account_class.class_hash, | ||
], | ||
) |
Oops, something went wrong.