Skip to content

Commit

Permalink
rust any_type syscall support
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Feb 4, 2025
1 parent c5b02db commit 1a4f78d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crates/dry_hint_processor/src/syscall_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use hints::vars;
use serde::{Deserialize, Serialize};
use starknet::CallContractHandler as StarknetCallContractHandler;
use syscall_handler::{
call_contract, call_contract::debug::DebugCallContractHandler, felt_from_ptr, keccak::KeccakHandler, run_handler, traits,
SyscallExecutionError, SyscallResult, SyscallSelector, WriteResponseResult,
call_contract::{self, any_type::AnyTypeCallContractHandler, debug::DebugCallContractHandler},
felt_from_ptr,
keccak::KeccakHandler,
run_handler, traits, SyscallExecutionError, SyscallResult, SyscallSelector, WriteResponseResult,
};
use tokio::{sync::RwLock, task};
use types::{
Expand Down Expand Up @@ -83,6 +85,7 @@ pub struct CallContractHandlerRelay {
pub starknet_call_contract_handler: StarknetCallContractHandler,
#[serde(skip)]
pub debug_call_contract_handler: DebugCallContractHandler,
pub any_type_call_contract_handler: AnyTypeCallContractHandler,
}

impl traits::SyscallHandler for CallContractHandlerRelay {
Expand All @@ -98,6 +101,7 @@ impl traits::SyscallHandler for CallContractHandlerRelay {
async fn execute(&mut self, request: Self::Request, vm: &mut VirtualMachine) -> SyscallResult<Self::Response> {
match request.contract_address {
v if v == call_contract::debug::CONTRACT_ADDRESS => self.debug_call_contract_handler.execute(request, vm).await,
v if v == call_contract::any_type::CONTRACT_ADDRESS => self.any_type_call_contract_handler.execute(request, vm).await,
_ => {
let chain_id = <Felt252 as TryInto<u128>>::try_into(*vm.get_integer((request.calldata_start + 2)?)?)
.map_err(|e| SyscallExecutionError::InternalError(e.to_string().into()))?;
Expand Down
97 changes: 97 additions & 0 deletions crates/syscall_handler/src/call_contract/any_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use cairo_vm::{
types::relocatable::{MaybeRelocatable, Relocatable},
vm::vm_core::VirtualMachine,
Felt252,
};
use serde::{Deserialize, Serialize};
use types::cairo::{
new_syscalls::{CallContractRequest, CallContractResponse},
traits::CairoType,
FELT_10,
};

use crate::{traits, SyscallResult, WriteResponseResult};

pub const CONTRACT_ADDRESS: Felt252 = Felt252::from_hex_unchecked("0x616e795f74797065"); // 'any_type' in hex

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct AnyTypeCallContractHandler;

impl traits::SyscallHandler for AnyTypeCallContractHandler {
type Request = CallContractRequest;
type Response = CallContractResponse;

fn read_request(&mut self, vm: &VirtualMachine, ptr: &mut Relocatable) -> SyscallResult<Self::Request> {
let ret = Self::Request::from_memory(vm, *ptr)?;
*ptr = (*ptr + Self::Request::cairo_size())?;
Ok(ret)
}

async fn execute(&mut self, request: Self::Request, vm: &mut VirtualMachine) -> SyscallResult<Self::Response> {
let field_len = (request.calldata_end - request.calldata_start)?;
let fields = vm
.get_integer_range(request.calldata_start, field_len)?
.into_iter()
.map(|f| (*f.as_ref()))
.collect::<Vec<Felt252>>();

// we need here macro to deserialize the cairo1 way the CairoType derived structure
let input = AnyTypeInput {
item_a: fields[0],
item_b: fields[1],
item_c: fields[2],
item_d: fields[3],
};

// we need here macro to serialize the cairo1 way the CairoType derived structure
let output = AnyTypeOutput {
item_a: input.item_a,
item_b: input.item_b,
item_c: input.item_c,
item_d: input.item_d,
item_e: FELT_10,
item_f: FELT_10,
};

let retdata_start = vm.add_memory_segment();
let retdata_end = vm.load_data(
retdata_start,
&[
output.item_a,
output.item_b,
output.item_c,
output.item_d,
output.item_e,
output.item_f,
]
.map(MaybeRelocatable::from),
)?;

Ok(Self::Response {
retdata_start,
retdata_end,
})
}

fn write_response(&mut self, response: Self::Response, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
response.to_memory(vm, *ptr)?;
*ptr = (*ptr + Self::Response::cairo_size())?;
Ok(())
}
}

struct AnyTypeInput {
pub item_a: Felt252,
pub item_b: Felt252,
pub item_c: Felt252,
pub item_d: Felt252,
}

struct AnyTypeOutput {
pub item_a: Felt252,
pub item_b: Felt252,
pub item_c: Felt252,
pub item_d: Felt252,
pub item_e: Felt252,
pub item_f: Felt252,
}
1 change: 1 addition & 0 deletions crates/syscall_handler/src/call_contract/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod any_type;
pub mod debug;

0 comments on commit 1a4f78d

Please sign in to comment.