Skip to content

Commit

Permalink
Cairo v0.7.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Jan 17, 2022
1 parent fc97bdd commit 64a7f6a
Show file tree
Hide file tree
Showing 193 changed files with 7,587 additions and 1,689 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ find_program(PYTHON "python3")

include("src/cmake_utils/cmake_rules.cmake")
include("src/starkware/cairo/lang/cairo_cmake_rules.cmake")
include("src/starkware/starknet/compiler/starknet_cmake_rules.cmake")

# Repos needs to be first as it defines macros that are needed by src.
add_subdirectory(src)
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ganache-cli@6.12.2

COPY . /app/

# Build.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Once the docker image is built, you can fetch the python package zip file using:

```bash
> container_id=$(docker create cairo)
> docker cp ${container_id}:/app/cairo-lang-0.6.2.zip .
> docker cp ${container_id}:/app/cairo-lang-0.7.0.zip .
> docker rm -v ${container_id}
```

1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(cmake_utils)
add_subdirectory(services)
add_subdirectory(starkware)
17 changes: 17 additions & 0 deletions src/cmake_utils/CMakeLists.txt
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
)
1 change: 1 addition & 0 deletions src/cmake_utils/cmake_rules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include("${CMAKE_SOURCE_DIR}/src/cmake_utils/exe_rules.cmake")
include("${CMAKE_SOURCE_DIR}/src/cmake_utils/copy_rules.cmake")
include("${CMAKE_SOURCE_DIR}/src/cmake_utils/python_rules.cmake")
include("${CMAKE_SOURCE_DIR}/src/cmake_utils/pip_rules.cmake")
include("${CMAKE_SOURCE_DIR}/src/cmake_utils/solidity_rules.cmake")
python_get_pip_deps(main_reqs
python3.7:${CMAKE_SOURCE_DIR}/scripts/requirements-deps.json
${ADDITIONAL_PIP_DEPS}
Expand Down
133 changes: 133 additions & 0 deletions src/cmake_utils/gen_solidity_env.py
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()
2 changes: 1 addition & 1 deletion src/cmake_utils/pip_rules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function(python_pip TARGET)
COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_DIR}
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOWNLOAD_DIR}
COMMAND
${INTERPRETER} -m pip wheel --no-deps -w ${DOWNLOAD_DIR}/ ${REQ} ${PIP_INSTALL_ARGS}
${INTERPRETER} -m pip wheel --no-deps -w ${DOWNLOAD_DIR}/ ${REQ} ${PIP_INSTALL_ARGS_${INTERPRETER}}
# Extract wheel.
COMMAND cd ${LIB_DIR} && ${CMAKE_COMMAND} -E tar xzf ${DOWNLOAD_DIR}/*.whl
# Some wheels may put their files at /{name}-{version}.data/(pure|plat)lib/, instead of under
Expand Down
50 changes: 50 additions & 0 deletions src/cmake_utils/solidity_rules.cmake
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()
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class EverestFeederGatewayClient(BaseClient):

prefix: ClassVar[str] = "/feeder_gateway"

async def is_alive(self) -> str:
return await self._send_request(send_method="GET", uri="/is_alive")

async def get_last_batch_id(self) -> int:
raw_response = await self._send_request(send_method="GET", uri="/get_last_batch_id")
return json.loads(raw_response)
3 changes: 0 additions & 3 deletions src/services/everest/api/gateway/gateway_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class EverestGatewayClient(BaseClient):

prefix: ClassVar[str] = "/gateway"

async def is_alive(self) -> str:
return await self._send_request(send_method="GET", uri="/is_alive")

async def add_transaction_request(
self, add_tx_request: EverestAddTransactionRequest
) -> Dict[str, str]:
Expand Down
1 change: 1 addition & 0 deletions src/services/everest/business_logic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ python_lib(everest_business_logic_lib

LIBS
starkware_config_utils_lib
starkware_dataclasses_utils_lib
starkware_python_utils_lib
starkware_storage_lib
starkware_utils_lib
Expand Down
Loading

0 comments on commit 64a7f6a

Please sign in to comment.