-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
193 changed files
with
7,587 additions
and
1,689 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,12 @@ FROM ciimage/python:3.7 | |
RUN apt update | ||
RUN apt install -y cmake libgmp3-dev g++ python3-pip python3.7-dev python3.7-venv npm | ||
|
||
# Install solc and ganache | ||
RUN curl https://binaries.soliditylang.org/linux-amd64/solc-linux-amd64-v0.6.12+commit.27d51765 -o /usr/local/bin/solc-0.6.12 | ||
RUN echo 'f6cb519b01dabc61cab4c184a3db11aa591d18151e362fcae850e42cffdfb09a /usr/local/bin/solc-0.6.12' | sha256sum --check | ||
RUN chmod +x /usr/local/bin/solc-0.6.12 | ||
RUN npm install -g --unsafe-perm [email protected] | ||
|
||
COPY . /app/ | ||
|
||
# Build. | ||
|
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(cmake_utils) | ||
add_subdirectory(services) | ||
add_subdirectory(starkware) |
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,17 @@ | ||
python_lib(gen_solidity_lib | ||
FILES | ||
gen_solidity_env.py | ||
LIBS | ||
pip_mypy_extensions | ||
) | ||
|
||
python_venv(gen_solidity_venv | ||
PYTHON python3.7 | ||
LIBS | ||
gen_solidity_lib | ||
) | ||
|
||
python_exe(gen_solidity_exe | ||
VENV gen_solidity_venv | ||
MODULE gen_solidity_env | ||
) |
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,133 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
A helper for solidity_rules.cmake. | ||
Generates an EVM contract environment from python_lib targets. | ||
""" | ||
|
||
import json | ||
import os | ||
import shutil | ||
import subprocess | ||
from argparse import ArgumentParser | ||
from typing import Dict, List | ||
|
||
|
||
def find_dependency_libraries(libs: List[str], info_dir: str) -> Dict[str, dict]: | ||
""" | ||
Finds all transitively closed dependency libraries for given libraries. | ||
Returns a dictionary from library name to the info dict generated by gen_py_lib.py. | ||
""" | ||
found_libraries = {} | ||
library_queue = libs.copy() | ||
while library_queue: | ||
lib = library_queue.pop() | ||
if lib in found_libraries: | ||
continue | ||
filename = os.path.join(info_dir, f"{lib}.info") | ||
with open(filename, "r") as fp: | ||
found_libraries[lib] = json.load(fp) | ||
library_queue += found_libraries[lib]["lib_deps"] | ||
|
||
return found_libraries | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser(description="Generates an EVM contract environment.") | ||
parser.add_argument( | ||
"--name", type=str, help="The name of the EVM contract environment", required=True | ||
) | ||
parser.add_argument("--libs", type=str, nargs="*", help="Library list", required=True) | ||
parser.add_argument( | ||
"--env_dir", help="EVM contract environment output directory", type=str, required=True | ||
) | ||
parser.add_argument( | ||
"--info_dir", help="Directory for all libraries info files", type=str, required=True | ||
) | ||
args = parser.parse_args() | ||
|
||
# Clean directories. | ||
shutil.rmtree(args.env_dir, ignore_errors=True) | ||
contracts_dir = os.path.join(args.env_dir, "contracts") | ||
artifacts_dir = os.path.join(args.env_dir, "artifacts") | ||
os.makedirs(contracts_dir) | ||
os.makedirs(artifacts_dir) | ||
|
||
# Find all libraries. | ||
found_libraries = find_dependency_libraries(args.libs, args.info_dir) | ||
|
||
# Populate project contracts directory. | ||
filenames = [] | ||
for lib_name, lib_info in found_libraries.items(): | ||
lib_dirs = lib_info["lib_dir"] | ||
assert len(lib_dirs) == 1, f"Library {lib_name} has {len(lib_dirs)} library directories." | ||
for filename in lib_info["files"]: | ||
src = os.path.join(lib_dirs[0], filename) | ||
dst = os.path.join(contracts_dir, filename) | ||
filenames.append(dst) | ||
os.makedirs(os.path.dirname(dst), exist_ok=True) | ||
assert not os.path.exists(dst), f"Multiple entries for {filename} in site dir." | ||
# Create a hardlink. | ||
os.link(src, dst) | ||
|
||
# Compile. | ||
subprocess.check_call( | ||
[ | ||
"solc-0.6.12", | ||
"--optimize", | ||
"--optimize-runs", | ||
"200", | ||
"--combined-json", | ||
"abi,bin", | ||
"-o", | ||
"artifacts/", | ||
*filenames, | ||
], | ||
cwd=args.env_dir, | ||
) | ||
|
||
# Extract artifacts. | ||
extract_artifacts( | ||
artifacts_dir=artifacts_dir, | ||
combined_json_filename=os.path.join(artifacts_dir, "combined.json"), | ||
) | ||
|
||
# Generate info file. | ||
with open(os.path.join(args.info_dir, f"{args.name}.info"), "w") as fp: | ||
json.dump( | ||
{ | ||
"env_dir": args.env_dir, | ||
}, | ||
fp, | ||
indent=4, | ||
) | ||
fp.write("\n") | ||
|
||
|
||
def extract_artifacts(artifacts_dir, combined_json_filename): | ||
with open(combined_json_filename) as fp: | ||
combined_json = json.load(fp) | ||
|
||
for full_name, val in combined_json["contracts"].items(): | ||
_, name = full_name.split(":") | ||
|
||
# 1. We cannot put "0x" in case of empty bin, as this would not prevent | ||
# loading an empty (virtual) contract. (We want it to fail) | ||
# 2. Note that we can't put an assert len(val['bin']) > 0 here, because some contracts | ||
# are pure virtual and others lack external and public functions. | ||
bytecode = None | ||
if len(val["bin"]) > 0: | ||
bytecode = "0x" + val["bin"] | ||
|
||
artifact = { | ||
"contractName": name, | ||
"abi": json.loads(val["abi"]), | ||
"bytecode": bytecode, | ||
} | ||
destination_filename = os.path.join(artifacts_dir, f"{name}.json") | ||
with open(destination_filename, "w") as fp: | ||
json.dump(artifact, fp, indent=4) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,50 @@ | ||
set(GEN_SOLIDITY_ENV_EXE ${CMAKE_BINARY_DIR}/src/cmake_utils/gen_solidity_exe CACHE INTERNAL "") | ||
|
||
# Creates a solidity environment target. | ||
# Usage: solidity_env(venv_name LIBS lib0 lib1 ...) | ||
function(solidity_env ENV_NAME) | ||
# Parse arguments. | ||
set(options) | ||
set(oneValueArgs) | ||
set(multiValueArgs CONTRACTS LIBS) | ||
cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) | ||
|
||
# A directory with symlinks to files of other libraries. | ||
set(ENV_DIR ${CMAKE_CURRENT_BINARY_DIR}/${ENV_NAME}) | ||
get_lib_info_file(ENV_INFO_FILE ${ENV_NAME}) | ||
|
||
set(DEP_INFO) | ||
foreach(DEP_LIB ${ARGS_LIBS}) | ||
get_lib_info_file(DEP_INFO_FILE ${DEP_LIB}) | ||
set(DEP_INFO ${DEP_INFO} ${DEP_INFO_FILE}) | ||
endforeach() | ||
|
||
add_custom_command( | ||
OUTPUT ${ENV_INFO_FILE} | ||
COMMAND ${GEN_SOLIDITY_ENV_EXE} | ||
--name ${ENV_NAME} | ||
--libs ${ARGS_LIBS} | ||
--env_dir ${ENV_DIR} | ||
--info_dir ${PY_LIB_INFO_GLOBAL_DIR} | ||
DEPENDS gen_solidity_exe ${GEN_SOLIDITY_ENV_EXE} ${DEP_INFO} ${ARGS_LIBS} | ||
) | ||
|
||
# Add contract file targets. | ||
foreach(CONTRACT ${ARGS_CONTRACTS}) | ||
set(OUTPUT_FILENAME ${CMAKE_CURRENT_BINARY_DIR}/${CONTRACT}.json) | ||
add_custom_command( | ||
OUTPUT ${OUTPUT_FILENAME} | ||
COMMAND ${CMAKE_COMMAND} -E copy | ||
${ENV_DIR}/artifacts/${CONTRACT}.json | ||
${OUTPUT_FILENAME} | ||
DEPENDS ${ENV_INFO_FILE} | ||
COMMENT "Copying contract ${CONTRACT}" | ||
) | ||
set(OUTPUT_FILES ${OUTPUT_FILES} ${OUTPUT_FILENAME}) | ||
endforeach(CONTRACT) | ||
|
||
# Create target. | ||
add_custom_target(${ENV_NAME} ALL | ||
DEPENDS ${ENV_INFO_FILE} ${OUTPUT_FILES} | ||
) | ||
endfunction() |
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
Oops, something went wrong.