Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
update and use new native jit api
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Nov 13, 2023
1 parent c5c04f5 commit 778fa79
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 84 deletions.
33 changes: 29 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cairo-lang-runner = { workspace = true }
cairo-lang-sierra = { workspace = true }
cairo-lang-starknet = { workspace = true }
cairo-lang-utils = { workspace = true }
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "4012a10b97530e208b76d42169aa9608a6a9d8fd", optional = true }
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "efaeb4f719fe23afc0d0699442a7046fa371d7d0", optional = true }
cairo-vm = { workspace = true }
flate2 = "1.0.25"
getset = "0.1.2"
Expand Down
88 changes: 9 additions & 79 deletions src/execution/execution_entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ use std::{cell::RefCell, rc::Rc};
#[cfg(feature = "cairo-native")]
use {
crate::syscalls::native_syscall_handler::NativeSyscallHandler,
cairo_native::{
context::NativeContext, execution_result::NativeExecutionResult,
metadata::syscall_handler::SyscallHandlerMeta, utils::felt252_bigint,
},
serde_json::Value,
cairo_native::{context::NativeContext, metadata::syscall_handler::SyscallHandlerMeta},
};

#[derive(Debug, Default)]
Expand Down Expand Up @@ -772,11 +768,7 @@ impl ExecutionEntryPoint {
class_hash: &[u8; 32],
program_cache: Rc<RefCell<ProgramCache<'_, ClassHash>>>,
) -> Result<CallInfo, TransactionError> {
use cairo_lang_sierra::{
extensions::core::{CoreLibfunc, CoreType, CoreTypeConcrete},
program_registry::ProgramRegistry,
};
use serde_json::json;
use cairo_native::values::JITValue;

use crate::syscalls::business_logic_syscall_handler::SYSCALL_BASE;
let sierra_program = &sierra_program_and_entrypoints.0;
Expand All @@ -800,9 +792,6 @@ impl ExecutionEntryPoint {
.unwrap(),
};

let program_registry: ProgramRegistry<CoreType, CoreLibfunc> =
ProgramRegistry::new(sierra_program).unwrap();

let native_executor = {
let mut cache = program_cache.borrow_mut();
if let Some(executor) = cache.get(*class_hash) {
Expand All @@ -815,7 +804,7 @@ impl ExecutionEntryPoint {
let contract_storage_state =
ContractStorageState::new(state, self.contract_address.clone());

let syscall_handler = NativeSyscallHandler {
let mut syscall_handler = NativeSyscallHandler {
starknet_storage_state: contract_storage_state,
events: Vec::new(),
l2_to_l1_messages: Vec::new(),
Expand All @@ -836,87 +825,28 @@ impl ExecutionEntryPoint {
native_executor
.borrow_mut()
.get_module_mut()
.insert_metadata(SyscallHandlerMeta::new(&syscall_handler));

let syscall_addr = native_executor
.borrow()
.get_module()
.get_metadata::<SyscallHandlerMeta>()
.unwrap()
.as_ptr()
.as_ptr() as *const () as usize;
.insert_metadata(SyscallHandlerMeta::new(&mut syscall_handler));

let entry_point_fn = &sierra_program
.funcs
.iter()
.find(|x| x.id.id == (entry_point.function_idx as u64))
.unwrap();
let ret_types: Vec<&CoreTypeConcrete> = entry_point_fn
.signature
.ret_types
.iter()
.map(|x| program_registry.get_type(x).unwrap())
.collect();
let entry_point_id = &entry_point_fn.id;

let required_init_gas = native_executor
.borrow()
.get_module()
.get_required_init_gas(entry_point_id);
let entry_point_id = &entry_point_fn.id;

let calldata: Vec<_> = self
.calldata
.iter()
.map(|felt| felt252_bigint(felt.to_bigint()))
.collect();

/*
Below we construct `params`, the Serde value that MLIR expects. It consists of the following:
- One `null` value for each builtin that is going to be used.
- The maximum amout of gas allowed by the call.
- `syscall_addr`, the address of the syscall handler.
- `calldata`, an array of Felt arguments to the method being called.
*/

let wrapped_calldata = vec![calldata];
let params: Vec<Value> = sierra_program.funcs[entry_point_id.id as usize]
.params
.iter()
.map(|param| {
match param.ty.debug_name.as_ref().unwrap().as_str() {
"GasBuiltin" => {
json!(self.initial_gas)
}
"Pedersen" | "SegmentArena" | "RangeCheck" | "Bitwise" | "Poseidon" => {
json!(null)
}
"System" => {
json!(syscall_addr)
}
// calldata
"core::array::Span::<core::felt252>" => json!(wrapped_calldata),
x => {
unimplemented!("unhandled param type: {:?}", x);
}
}
})
.cloned()
.map(JITValue::Felt252)
.collect();

let mut writer: Vec<u8> = Vec::new();
let returns = &mut serde_json::Serializer::new(&mut writer);

native_executor
let value = native_executor
.borrow()
.execute(entry_point_id, json!(params), returns, required_init_gas)
.execute_contract(entry_point_id, &calldata, self.initial_gas)
.map_err(|e| TransactionError::CustomError(format!("cairo-native error: {:?}", e)))?;

let value = NativeExecutionResult::deserialize_from_ret_types(
&mut serde_json::Deserializer::from_slice(&writer),
&ret_types,
)
.expect("failed to serialize starknet execution result");

Ok(CallInfo {
caller_address: self.caller_address.clone(),
call_type: Some(self.call_type.clone()),
Expand Down

0 comments on commit 778fa79

Please sign in to comment.