Skip to content

Commit

Permalink
Merge pull request #541 from crytic/dev
Browse files Browse the repository at this point in the history
sync master<>dev
  • Loading branch information
0xalpharush authored Jan 16, 2024
2 parents 3a4b0de + 2bbe730 commit 62b16c3
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v4

- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
25 changes: 17 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
fail-fast: false
matrix:
os: ["ubuntu-latest", "windows-2022"]
python: ${{ (github.event_name == 'pull_request' && fromJSON('["3.8", "3.12"]')) || fromJSON('["3.8", "3.9", "3.10", "3.11", "3.12"]') }}
type: ["brownie", "buidler", "dapp", "embark", "hardhat", "solc", "truffle", "waffle", "foundry", "standard", "vyper", "solc_multi_file", "hardhat_multi_file"]
exclude:
# Currently broken, tries to pull git:// which is blocked by GH
Expand All @@ -32,6 +33,15 @@ jobs:
# Explore foundry support in windows
- os: windows-2022
type: foundry
# brownie does not install correctly with Python 3.10
- python: 3.10
type: brownie
# brownie does not install correctly with Python 3.11
- python: 3.11
type: brownie
# brownie does not install correctly with Python 3.12
- python: 3.12
type: brownie
steps:
- uses: actions/checkout@v4
- name: Set up shell
Expand All @@ -40,24 +50,23 @@ jobs:
echo 'C:\msys64\mingw64\bin' >> "$GITHUB_PATH"
echo 'C:\msys64\usr\bin' >> "$GITHUB_PATH"
- name: Set up Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.15
- name: Set up Python 3.8
uses: actions/setup-python@v4
- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v5
with:
python-version: 3.8
python-version: ${{ matrix.python }}
- name: Install dependencies
run: |
pip install "solc-select>=v1.0.0b1"
solc-select use 0.5.7 --always-install
pip install .
solc-select use 0.5.7 --always-install
- name: Set up nix
if: matrix.type == 'dapp'
uses: cachix/install-nix-action@v23
uses: cachix/install-nix-action@v24
- name: Set up cachix
if: matrix.type == 'dapp'
uses: cachix/cachix-action@v12
uses: cachix/cachix-action@v13
with:
name: dapp
- name: Install Foundry
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/darglint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Run Tests
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v3
- uses: actions/setup-python@v4
uses: actions/configure-pages@v4
- uses: actions/setup-python@v5
with:
python-version: '3.8'
- run: pip install -e ".[doc]"
- run: pdoc -o html/ crytic_compile
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
uses: actions/upload-pages-artifact@v3
with:
# Upload the doc
path: './html/'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
uses: actions/deploy-pages@v4
2 changes: 1 addition & 1 deletion .github/workflows/etherscan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
echo 'C:\msys64\mingw64\bin' >> "$GITHUB_PATH"
echo 'C:\msys64\usr\bin' >> "$GITHUB_PATH"
- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v4

- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v4

- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.x'

Expand All @@ -24,7 +24,7 @@ jobs:
python -m build
- name: Upload distributions
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: crytic-compile-dists
path: dist/
Expand All @@ -39,16 +39,16 @@ jobs:
- build-release
steps:
- name: fetch dists
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: crytic-compile-dists
path: dist/

- name: publish
uses: pypa/[email protected].10
uses: pypa/[email protected].11

- name: sign
uses: sigstore/[email protected].0
uses: sigstore/[email protected].1
with:
inputs: ./dist/*.tar.gz ./dist/*.whl
release-signing-artifacts: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v4

- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
5 changes: 2 additions & 3 deletions crytic_compile/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
This is the Slither cli script
"""
import argparse
from importlib.metadata import version
import json
import logging
import os
import sys
from typing import TYPE_CHECKING, Any, Optional

from pkg_resources import require

from crytic_compile.crytic_compile import compile_all, get_platforms
from crytic_compile.cryticparser import DEFAULTS_FLAG_IN_CONFIG, cryticparser
from crytic_compile.platform import InvalidCompilation
Expand Down Expand Up @@ -109,7 +108,7 @@ def parse_args() -> argparse.Namespace:
parser.add_argument(
"--version",
help="displays the current version",
version=require("crytic-compile")[0].version,
version=version("crytic-compile"),
action="version",
)

Expand Down
5 changes: 2 additions & 3 deletions crytic_compile/crytic_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def compile_all(target: str, **kwargs: str) -> List[CryticCompile]:
**kwargs: optional arguments. Used: "solc_standard_json"
Raises:
NotImplementedError: If the target could not be compiled
ValueError: If the target could not be compiled
Returns:
List[CryticCompile]: Returns a list of CryticCompile instances for all compilations which occurred.
Expand Down Expand Up @@ -737,12 +737,11 @@ def compile_all(target: str, **kwargs: str) -> List[CryticCompile]:
vyper_standard_json.add_source_files(vyper_filenames)
compilations.append(CryticCompile(vyper_standard_json, **kwargs))
else:
raise NotImplementedError()
# TODO split glob into language
# # Attempt to perform glob expansion of target/filename
# globbed_targets = glob.glob(target, recursive=True)
# print(globbed_targets)

# raise ValueError(f"{str(target)} is not a file or directory.")
raise ValueError(f"{str(target)} is not a file or directory.")

return compilations
9 changes: 9 additions & 0 deletions crytic_compile/platform/etherscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,19 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
working_dir: Optional[str] = None
remappings: Optional[List[str]] = None

dict_source_code: Optional[Dict] = None
try:
# etherscan might return an object with two curly braces, {{ content }}
dict_source_code = json.loads(source_code[1:-1])
assert isinstance(dict_source_code, dict)
filenames, working_dir, remappings = _handle_multiple_files(
dict_source_code, addr, prefix, contract_name, export_dir
)
except JSONDecodeError:
try:
# or etherscan might return an object with single curly braces, { content }
dict_source_code = json.loads(source_code)
assert isinstance(dict_source_code, dict)
filenames, working_dir, remappings = _handle_multiple_files(
dict_source_code, addr, prefix, contract_name, export_dir
)
Expand All @@ -390,6 +393,11 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
_handle_single_file(source_code, addr, prefix, contract_name, export_dir)
]

# viaIR is not exposed on the top level JSON offered by etherscan, so we need to inspect the settings
via_ir_enabled: Optional[bool] = None
if isinstance(dict_source_code, dict):
via_ir_enabled = dict_source_code.get("settings", {}).get("viaIR", None)

compilation_unit = CompilationUnit(crytic_compile, contract_name)

compilation_unit.compiler_version = CompilerVersion(
Expand All @@ -413,6 +421,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
working_dir=working_dir,
remappings=remappings,
evm_version=evm_version,
via_ir=via_ir_enabled,
)

def clean(self, **_kwargs: str) -> None:
Expand Down
80 changes: 25 additions & 55 deletions crytic_compile/platform/foundry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import os
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING, List, Optional, Dict, TypeVar
from typing import TYPE_CHECKING, List, Optional, TypeVar

import toml
import json

from crytic_compile.platform.abstract_platform import AbstractPlatform, PlatformConfig
from crytic_compile.platform.types import Type
Expand Down Expand Up @@ -63,7 +63,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
compile_all = kwargs.get("foundry_compile_all", False)

if not compile_all:
foundry_config = self.config(str(crytic_compile.working_dir.absolute()))
foundry_config = self.config(self._target)
if foundry_config:
compilation_command += [
"--skip",
Expand Down Expand Up @@ -122,64 +122,34 @@ def config(working_dir: str) -> Optional[PlatformConfig]:
"""Return configuration data that should be passed to solc, such as remappings.
Args:
working_dir (str): path to the working directory
working_dir (str): path to the working_dir
Returns:
Optional[PlatformConfig]: Platform configuration data such as optimization, remappings...
"""
result = PlatformConfig()
result.remappings = (
subprocess.run(["forge", "remappings"], stdout=subprocess.PIPE, check=True)
.stdout.decode("utf-8")
.replace("\n", " ")
.strip()
LOGGER.info("'forge config --json' running")
json_config = json.loads(
subprocess.run(
["forge", "config", "--json"], cwd=working_dir, stdout=subprocess.PIPE, check=True
).stdout
)
with open("foundry.toml", "r", encoding="utf-8") as f:
foundry_toml = toml.loads(f.read())
default_profile = foundry_toml["profile"]["default"]

def lookup_by_keys(keys: List[str], dictionary: Dict[str, T]) -> Optional[T]:
for key in keys:
if key in dictionary:
return dictionary[key]
return None

# Foundry supports snake and kebab case.
result.solc_version = lookup_by_keys(
["solc", "solc_version", "solc-version"], default_profile
)
via_ir = lookup_by_keys(["via_ir", "via-ir"], default_profile)
if via_ir:
result.via_ir = via_ir
result.allow_paths = lookup_by_keys(["allow_paths", "allow-paths"], default_profile)

if "offline" in default_profile:
result.offline = default_profile["offline"]
if "optimizer" in default_profile:
result.optimizer = default_profile["optimizer"]
else:
# Default to true
result.optimizer = True
optimizer_runs = lookup_by_keys(["optimizer_runs", "optimizer-runs"], default_profile)
if optimizer_runs is None:
# Default to 200
result.optimizer_runs = 200
else:
result.optimizer_runs = optimizer_runs
evm_version = lookup_by_keys(["evm_version", "evm-version"], default_profile)
if evm_version is None:
result.evm_version = evm_version
else:
# Default to london
result.evm_version = "london"
if "src" in default_profile:
result.src_path = default_profile["src"]
if "test" in default_profile:
result.tests_path = default_profile["test"]
if "libs" in default_profile:
result.libs_path = default_profile["libs"]
if "script" in default_profile:
result.scripts_path = default_profile["script"]

# Solc configurations
result.solc_version = json_config.get("solc")
result.via_ir = json_config.get("via_ir")
result.allow_paths = json_config.get("allow_paths")
result.offline = json_config.get("offline")
result.evm_version = json_config.get("evm_version")
result.optimizer = json_config.get("optimizer")
result.optimizer_runs = json_config.get("optimizer_runs")
result.remappings = json_config.get("remappings")

# Foundry project configurations
result.src_path = json_config.get("src")
result.tests_path = json_config.get("test")
result.libs_path = json_config.get("libs")
result.scripts_path = json_config.get("script")

return result

Expand Down
Loading

0 comments on commit 62b16c3

Please sign in to comment.