From 383ed57eeb6a4f4c12e8fe8a27e2b1471d67013b Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Mon, 24 Jun 2024 07:59:07 +0200 Subject: [PATCH 1/3] dryrun syscall handler init --- .../dryrun_syscall_handler.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 packages/contract_bootloader/dryrun_syscall_handler.py diff --git a/packages/contract_bootloader/dryrun_syscall_handler.py b/packages/contract_bootloader/dryrun_syscall_handler.py new file mode 100644 index 00000000..c4f20070 --- /dev/null +++ b/packages/contract_bootloader/dryrun_syscall_handler.py @@ -0,0 +1,56 @@ +from rlp import decode +from typing import ( + Dict, + Iterable, +) +from tools.py.utils import little_8_bytes_chunks_to_bytes +from starkware.cairo.lang.vm.relocatable import RelocatableValue, MaybeRelocatable +from starkware.cairo.lang.vm.memory_segments import MemorySegmentManager +from contract_bootloader.syscall_handler_base import SyscallHandlerBase +from starkware.cairo.common.dict import DictManager +from starkware.cairo.common.structs import CairoStructProxy +from tools.py.block_header import BlockHeaderDencun as Block +from starkware.cairo.lang.vm.crypto import poseidon_hash_many +from starkware.starknet.business_logic.execution.objects import ( + CallResult, +) + + +class SyscallHandler(SyscallHandlerBase): + """ + A handler for system calls; used by the BusinessLogic entry point execution. + """ + + def __init__( + self, + dict_manager: DictManager, + segments: MemorySegmentManager, + ): + super().__init__(segments=segments, initial_syscall_ptr=None) + self.syscall_counter: Dict[str, int] = {} + self.dict_manager = dict_manager + + def set_syscall_ptr(self, syscall_ptr: RelocatableValue): + assert self._syscall_ptr is None, "syscall_ptr is already set." + self._syscall_ptr = syscall_ptr + + def allocate_segment(self, data: Iterable[MaybeRelocatable]) -> RelocatableValue: + segment_start = self.segments.add() + self.segments.write_arg(ptr=segment_start, arg=data) + return segment_start + + def _allocate_segment_for_retdata(self, retdata: Iterable[int]) -> RelocatableValue: + return self.allocate_segment(data=retdata) + + def _call_contract_helper( + self, request: CairoStructProxy, syscall_name: str + ) -> CallResult: + calldata = self._get_felt_range( + start_addr=request.calldata_start, end_addr=request.calldata_end + ) + + return CallResult( + gas_consumed=0, + failure_flag=0, + retdata=[], + ) From 2154cd117d5ffe2058c096f941ed6f13dabae560 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Mon, 24 Jun 2024 11:51:08 +0200 Subject: [PATCH 2/3] cleanup --- compile.sh | 12 ------------ .../contract_bootloader/dryrun_syscall_handler.py | 2 ++ tools/make/build_cairo1.sh | 2 +- 3 files changed, 3 insertions(+), 13 deletions(-) delete mode 100644 compile.sh diff --git a/compile.sh b/compile.sh deleted file mode 100644 index aaee60fd..00000000 --- a/compile.sh +++ /dev/null @@ -1,12 +0,0 @@ -cairo-compile --cairo_path=packages packages/contract_bootloader/contract_bootloader.cairo --output bootloader.json --proof_mode -cairo-run \ - --program=bootloader.json \ - --layout=starknet_with_keccak \ - --program_input=bootloader_input.json \ - --air_public_input=bootloader_public_input.json \ - --air_private_input=bootloader_private_input.json \ - --trace_file=bootloader.trace \ - --memory_file=bootloader.memory \ - --print_output \ - --proof_mode \ - --print_info \ No newline at end of file diff --git a/packages/contract_bootloader/dryrun_syscall_handler.py b/packages/contract_bootloader/dryrun_syscall_handler.py index c4f20070..3572d4d1 100644 --- a/packages/contract_bootloader/dryrun_syscall_handler.py +++ b/packages/contract_bootloader/dryrun_syscall_handler.py @@ -49,6 +49,8 @@ def _call_contract_helper( start_addr=request.calldata_start, end_addr=request.calldata_end ) + + return CallResult( gas_consumed=0, failure_flag=0, diff --git a/tools/make/build_cairo1.sh b/tools/make/build_cairo1.sh index 2cc3eb7b..19828a69 100755 --- a/tools/make/build_cairo1.sh +++ b/tools/make/build_cairo1.sh @@ -28,7 +28,7 @@ export -f process_scarb_project mkdir -p build/compiled_cairo_files # Find Scarb projects and execute process_scarb_project in each -find ./src/cairo1 ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 process_scarb_project {} +find ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 process_scarb_project {} # Capture the exit status of parallel exit_status=$? From f4ef14522064ceb64eb73953113d99b26693a62e Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Mon, 24 Jun 2024 13:49:53 +0200 Subject: [PATCH 3/3] cairo1 hdp_cairo package introduction --- .gitignore | 4 ++ Makefile | 3 -- .../Scarb.lock => Scarb.lock | 5 +++ Scarb.toml | 12 ++++++ cairo/Scarb.toml | 9 +++++ .../hdp_context.cairo => cairo/src/lib.cairo | 2 +- cairo/src/memorizer.cairo | 1 + .../src/memorizer}/header_memorizer.cairo | 3 +- .../dryrun_syscall_handler.py | 2 - .../snfoundry.toml => snfoundry.toml | 0 .../simple_linear_regression/.gitignore | 2 - .../simple_linear_regression/Scarb.toml | 1 + .../simple_linear_regression/src/lib.cairo | 3 -- tools/make/build.sh | 7 +++- tools/make/build_cairo1.sh | 38 ------------------- tools/make/cairo_format_check.sh | 6 +-- .../make/contract_casm_compile.sh | 0 tools/make/format_cairo_files.sh | 6 +-- 18 files changed, 45 insertions(+), 59 deletions(-) rename src/contracts/simple_linear_regression/Scarb.lock => Scarb.lock (83%) create mode 100644 Scarb.toml create mode 100644 cairo/Scarb.toml rename src/contracts/simple_linear_regression/src/slr/hdp_context.cairo => cairo/src/lib.cairo (92%) create mode 100644 cairo/src/memorizer.cairo rename {src/contracts/simple_linear_regression/src/slr/hdp_context => cairo/src/memorizer}/header_memorizer.cairo (95%) rename src/contracts/simple_linear_regression/snfoundry.toml => snfoundry.toml (100%) delete mode 100644 src/contracts/simple_linear_regression/.gitignore delete mode 100755 tools/make/build_cairo1.sh rename src/contracts/sierra_compile.sh => tools/make/contract_casm_compile.sh (100%) diff --git a/.gitignore b/.gitignore index 2e22a513..e32a4097 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,7 @@ src/hdp_input.json !tools/js/package.json !*_contract.json !packages/cairo-lang-0.13.1.zip + +# Scarb & snfoundry +target +.snfoundry_cache/ diff --git a/Makefile b/Makefile index 7a6f247b..1a013bc4 100644 --- a/Makefile +++ b/Makefile @@ -9,14 +9,11 @@ CAIRO_FILES := $(shell find ./tests/cairo_programs -name "*.cairo") build: clean @echo "Building project..." ./tools/make/build.sh - ./tools/make/build_cairo1.sh @echo "Build complete." # Setup environment setup: @echo "Setting up the environment..." - ./tools/make/install_cairo1_run.sh - ./tools/make/build_cairo1.sh ./tools/make/setup.sh $(VENV_PATH) @echo "Setup complete." diff --git a/src/contracts/simple_linear_regression/Scarb.lock b/Scarb.lock similarity index 83% rename from src/contracts/simple_linear_regression/Scarb.lock rename to Scarb.lock index d0da7f02..89b9d78f 100644 --- a/src/contracts/simple_linear_regression/Scarb.lock +++ b/Scarb.lock @@ -1,10 +1,15 @@ # Code generated by scarb DO NOT EDIT. version = 1 +[[package]] +name = "hdp_cairo" +version = "0.1.0" + [[package]] name = "simple_linear_regression" version = "0.1.0" dependencies = [ + "hdp_cairo", "snforge_std", ] diff --git a/Scarb.toml b/Scarb.toml new file mode 100644 index 00000000..cf7f2f02 --- /dev/null +++ b/Scarb.toml @@ -0,0 +1,12 @@ +[workspace] +name = "hdp_cairo" +version = "0.1.0" +edition = "2023_11" + +members = [ + "cairo", + "src/contracts/simple_linear_regression" +] + +[workspace.dependencies] +hdp_cairo = { path = "cairo" } \ No newline at end of file diff --git a/cairo/Scarb.toml b/cairo/Scarb.toml new file mode 100644 index 00000000..711fe2e8 --- /dev/null +++ b/cairo/Scarb.toml @@ -0,0 +1,9 @@ +[package] +name = "hdp_cairo" +version = "0.1.0" +edition = "2023_11" + +# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html + +[dependencies] +starknet = "2.6.3" \ No newline at end of file diff --git a/src/contracts/simple_linear_regression/src/slr/hdp_context.cairo b/cairo/src/lib.cairo similarity index 92% rename from src/contracts/simple_linear_regression/src/slr/hdp_context.cairo rename to cairo/src/lib.cairo index 4e95d588..c8da34c4 100644 --- a/src/contracts/simple_linear_regression/src/slr/hdp_context.cairo +++ b/cairo/src/lib.cairo @@ -1,4 +1,4 @@ -pub mod header_memorizer; +pub mod memorizer; #[derive(Serde, Drop)] pub struct HDP { diff --git a/cairo/src/memorizer.cairo b/cairo/src/memorizer.cairo new file mode 100644 index 00000000..b4e1bb77 --- /dev/null +++ b/cairo/src/memorizer.cairo @@ -0,0 +1 @@ +pub mod header_memorizer; diff --git a/src/contracts/simple_linear_regression/src/slr/hdp_context/header_memorizer.cairo b/cairo/src/memorizer/header_memorizer.cairo similarity index 95% rename from src/contracts/simple_linear_regression/src/slr/hdp_context/header_memorizer.cairo rename to cairo/src/memorizer/header_memorizer.cairo index aea90799..85c4bd7e 100644 --- a/src/contracts/simple_linear_regression/src/slr/hdp_context/header_memorizer.cairo +++ b/cairo/src/memorizer/header_memorizer.cairo @@ -1,5 +1,4 @@ -use core::option::OptionTrait; -use super::{Memorizer}; +use hdp_cairo::Memorizer; use starknet::syscalls::call_contract_syscall; use starknet::{SyscallResult, SyscallResultTrait}; use core::poseidon::poseidon_hash_span; diff --git a/packages/contract_bootloader/dryrun_syscall_handler.py b/packages/contract_bootloader/dryrun_syscall_handler.py index 3572d4d1..c4f20070 100644 --- a/packages/contract_bootloader/dryrun_syscall_handler.py +++ b/packages/contract_bootloader/dryrun_syscall_handler.py @@ -49,8 +49,6 @@ def _call_contract_helper( start_addr=request.calldata_start, end_addr=request.calldata_end ) - - return CallResult( gas_consumed=0, failure_flag=0, diff --git a/src/contracts/simple_linear_regression/snfoundry.toml b/snfoundry.toml similarity index 100% rename from src/contracts/simple_linear_regression/snfoundry.toml rename to snfoundry.toml diff --git a/src/contracts/simple_linear_regression/.gitignore b/src/contracts/simple_linear_regression/.gitignore deleted file mode 100644 index 73aa31e6..00000000 --- a/src/contracts/simple_linear_regression/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target -.snfoundry_cache/ diff --git a/src/contracts/simple_linear_regression/Scarb.toml b/src/contracts/simple_linear_regression/Scarb.toml index 6c6b4300..56287203 100644 --- a/src/contracts/simple_linear_regression/Scarb.toml +++ b/src/contracts/simple_linear_regression/Scarb.toml @@ -7,6 +7,7 @@ edition = "2023_11" [dependencies] starknet = "2.6.3" +hdp_cairo = { workspace = true } [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.24.0" } diff --git a/src/contracts/simple_linear_regression/src/lib.cairo b/src/contracts/simple_linear_regression/src/lib.cairo index b00c2cf6..21366984 100644 --- a/src/contracts/simple_linear_regression/src/lib.cairo +++ b/src/contracts/simple_linear_regression/src/lib.cairo @@ -1,8 +1,5 @@ #[starknet::contract] mod slr { - pub mod hdp_context; - use hdp_context::HDP; - pub mod fraction; use fraction::fraction::{Fraction, FractionImpl, FractionTrait, Sign}; diff --git a/tools/make/build.sh b/tools/make/build.sh index 38de0fda..2fcdfe15 100755 --- a/tools/make/build.sh +++ b/tools/make/build.sh @@ -23,11 +23,14 @@ export -f process_cairo_file mkdir -p build/compiled_cairo_files # Find Cairo files and process them in parallel -find ./src ./tests/cairo_programs ./packages/contract_bootloader ./packages/hdp_bootloader -name "*.cairo" ! -path "./src/cairo1/*" ! -path "./src/contracts/*" | parallel --halt now,fail=1 process_cairo_file {} +find ./src ./tests/cairo_programs ./packages/contract_bootloader -name "*.cairo" ! -path "./src/cairo1/*" ! -path "./src/contracts/*" | parallel --halt now,fail=1 process_cairo_file {} # Capture the exit status of parallel exit_status=$? +# Build Cairo1 workspace +scarb build + # Exit with the captured status echo "Parallel execution exited with status: $exit_status" -exit $exit_status +exit $exit_status \ No newline at end of file diff --git a/tools/make/build_cairo1.sh b/tools/make/build_cairo1.sh deleted file mode 100755 index 19828a69..00000000 --- a/tools/make/build_cairo1.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -process_scarb_project() { - local project_dir="$1" - local build_dir="build/compiled_cairo_files" - - echo "Building Scarb project in $project_dir" - - # Change to the project directory and build the project - if (cd "$project_dir" && scarb build); then - echo "Copying built files from $project_dir/target/dev to $build_dir" - if cp "$project_dir/target/dev/"* "$build_dir/"; then - echo "$(date '+%Y-%m-%d %H:%M:%S') - Successfully built and copied files for Scarb project in $project_dir" - else - echo "$(date '+%Y-%m-%d %H:%M:%S') - Built Scarb project in $project_dir, but failed to copy files" - return 1 - fi - else - echo "$(date '+%Y-%m-%d %H:%M:%S') - Failed to build Scarb project in $project_dir" - return 1 - fi -} - -# Export the function so it's available in subshells -export -f process_scarb_project - -# Ensure the build directory exists -mkdir -p build/compiled_cairo_files - -# Find Scarb projects and execute process_scarb_project in each -find ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 process_scarb_project {} - -# Capture the exit status of parallel -exit_status=$? - -# Exit with the captured status -echo "Parallel execution exited with status: $exit_status" -exit $exit_status diff --git a/tools/make/cairo_format_check.sh b/tools/make/cairo_format_check.sh index b2a056bf..072116b4 100755 --- a/tools/make/cairo_format_check.sh +++ b/tools/make/cairo_format_check.sh @@ -35,9 +35,9 @@ find ./src ./tests ./packages/contract_bootloader/ ./packages/hdp_bootloader/ -n # Capture the exit status of parallel for .cairo files exit_status_cairo_files=$? -# Find Scarb projects and format them in parallel -echo "Finding and formatting Scarb projects..." -find ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 format_scarb_project {} +# Format Scarb workspace +echo "Formatting Scarb workspace..." +scarb fmt # Capture the exit status of parallel for Scarb projects exit_status_scarb_projects=$? diff --git a/src/contracts/sierra_compile.sh b/tools/make/contract_casm_compile.sh similarity index 100% rename from src/contracts/sierra_compile.sh rename to tools/make/contract_casm_compile.sh diff --git a/tools/make/format_cairo_files.sh b/tools/make/format_cairo_files.sh index 2cb4f3b2..beca4ce5 100755 --- a/tools/make/format_cairo_files.sh +++ b/tools/make/format_cairo_files.sh @@ -39,9 +39,9 @@ find ./src ./tests ./packages/contract_bootloader/ ./packages/hdp_bootloader/ -n # Capture the exit status of parallel for .cairo files exit_status_cairo_files=$? -# Find Scarb projects and execute format_scarb_project in each -echo "Formatting Scarb projects..." -find ./src/contracts -mindepth 1 -maxdepth 1 -type d | parallel --halt now,fail=1 format_scarb_project {} +# Format Scarb workspace +echo "Formatting Scarb workspace..." +scarb fmt --check # Capture the exit status of parallel for Scarb projects exit_status_scarb_projects=$?