Skip to content

Commit

Permalink
Merge pull request #40 from HerodotusDev/dryrun_syscall_handler
Browse files Browse the repository at this point in the history
dryrun syscall handler & hdp_cairo scarb package
  • Loading branch information
Okm165 authored Jun 26, 2024
2 parents 77eba52 + f4ef145 commit 0fc10b9
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down
5 changes: 5 additions & 0 deletions ...racts/simple_linear_regression/Scarb.lock → Scarb.lock
Original file line number Diff line number Diff line change
@@ -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",
]

Expand Down
12 changes: 12 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -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" }
9 changes: 9 additions & 0 deletions cairo/Scarb.toml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod header_memorizer;
pub mod memorizer;

#[derive(Serde, Drop)]
pub struct HDP {
Expand Down
1 change: 1 addition & 0 deletions cairo/src/memorizer.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod header_memorizer;
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
12 changes: 0 additions & 12 deletions compile.sh

This file was deleted.

56 changes: 56 additions & 0 deletions packages/contract_bootloader/dryrun_syscall_handler.py
Original file line number Diff line number Diff line change
@@ -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=[],
)
File renamed without changes.
2 changes: 0 additions & 2 deletions src/contracts/simple_linear_regression/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions src/contracts/simple_linear_regression/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 0 additions & 3 deletions src/contracts/simple_linear_regression/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
7 changes: 5 additions & 2 deletions tools/make/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 0 additions & 38 deletions tools/make/build_cairo1.sh

This file was deleted.

6 changes: 3 additions & 3 deletions tools/make/cairo_format_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions tools/make/format_cairo_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down

0 comments on commit 0fc10b9

Please sign in to comment.