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

update cairo native to use gas consumed #1102

Merged
merged 9 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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 = "db1c3f6b044a4a6a3de3ab33e472c2e2263d81ac", optional = true }
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "03cd09ba3e51852da2234fb32a74056787abba8e", optional = true }
cairo-vm = { workspace = true, features = ["cairo-1-hints"] }
flate2 = "1.0.25"
getset = "0.1.2"
Expand Down
44 changes: 31 additions & 13 deletions src/execution/execution_entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,14 @@ impl ExecutionEntryPoint {
tx_execution_context: &TransactionExecutionContext,
block_context: &BlockContext,
) -> Result<CallInfo, TransactionError> {
use cairo_lang_sierra::{
extensions::core::{CoreLibfunc, CoreType, CoreTypeConcrete},
program_registry::ProgramRegistry,
};
use serde_json::json;

use crate::syscalls::business_logic_syscall_handler::SYSCALL_BASE;

let entry_point = match self.entry_point_type {
EntryPointType::External => contract_class
.entry_points_by_type
Expand All @@ -664,6 +670,8 @@ impl ExecutionEntryPoint {
};

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

let native_context = NativeContext::new();
let mut native_program = native_context.compile(&sierra_program).unwrap();
Expand All @@ -672,16 +680,15 @@ impl ExecutionEntryPoint {

let syscall_handler = NativeSyscallHandler {
starknet_storage_state: contract_storage_state,
n_emitted_events: 0,
events: Vec::new(),
l2_to_l1_messages: Vec::new(),
n_sent_messages: 0,
contract_address: self.contract_address.clone(),
internal_calls: Vec::new(),
caller_address: self.caller_address.clone(),
entry_point_selector: self.entry_point_selector.clone(),
tx_execution_context: tx_execution_context.clone(),
block_context: block_context.clone(),
resources_manager: Default::default(),
};

native_program
Expand All @@ -694,14 +701,20 @@ impl ExecutionEntryPoint {
.as_ptr()
.as_ptr() as *const () as usize;

let fn_id = &sierra_program
let entry_point_fn = &sierra_program
.funcs
.iter()
.find(|x| x.id.id == (entry_point.function_idx as u64))
.unwrap()
.id;
.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_program.get_required_init_gas(fn_id);
let required_init_gas = native_program.get_required_init_gas(entry_point_id);

let calldata: Vec<_> = self
.calldata
Expand All @@ -719,13 +732,13 @@ impl ExecutionEntryPoint {
*/

let wrapped_calldata = vec![calldata];
let params: Vec<Value> = sierra_program.funcs[fn_id.id as usize]
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 as u64)
json!(self.initial_gas)
}
"Pedersen" | "SegmentArena" | "RangeCheck" | "Bitwise" | "Poseidon" => {
json!(null)
Expand All @@ -748,11 +761,14 @@ impl ExecutionEntryPoint {
let native_executor = NativeExecutor::new(native_program);

native_executor
.execute(fn_id, json!(params), returns, required_init_gas)
.execute(entry_point_id, json!(params), returns, required_init_gas)
.map_err(|e| TransactionError::CustomError(format!("cairo-native error: {:?}", e)))?;

let result: String = String::from_utf8(writer).unwrap();
let value = serde_json::from_str::<NativeExecutionResult>(&result).unwrap();
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(),
Expand All @@ -773,8 +789,10 @@ impl ExecutionEntryPoint {
failure_flag: value.failure_flag,
l2_to_l1_messages: syscall_handler.l2_to_l1_messages,
internal_calls: syscall_handler.internal_calls,
// TODO: check it's correct
gas_consumed: self.initial_gas - u128::from(value.gas_builtin.unwrap_or(0)),
gas_consumed: self
.initial_gas
.saturating_sub(SYSCALL_BASE)
.saturating_sub(value.remaining_gas),
})
}
}
9 changes: 5 additions & 4 deletions src/syscalls/business_logic_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ use lazy_static::lazy_static;
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
use num_traits::{One, ToPrimitive, Zero};

const STEP: u128 = 100;
const SYSCALL_BASE: u128 = 100 * STEP;
const KECCAK_ROUND_COST: u128 = 180000;
pub(crate) const STEP: u128 = 100;
pub(crate) const SYSCALL_BASE: u128 = 100 * STEP;
pub(crate) const KECCAK_ROUND_COST: u128 = 180000;

lazy_static! {
/// Felt->syscall map that was extracted from new_syscalls.json (Cairo 1.0 syscalls)
static ref SELECTOR_TO_SYSCALL: HashMap<Felt252, &'static str> = {
Expand Down Expand Up @@ -91,7 +92,7 @@ lazy_static! {
// Taken from starkware/starknet/constants.py in cairo-lang
// See further documentation on cairo_programs/constants.cairo
/// Maps syscall name to gas costs
static ref SYSCALL_GAS_COST: HashMap<&'static str, u128> = {
pub(crate) static ref SYSCALL_GAS_COST: HashMap<&'static str, u128> = {
let mut map = HashMap::new();

map.insert("initial", 100_000_000 * STEP);
Expand Down
Loading