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

Commit d2651a2

Browse files
edg-ljuanbono
authored andcommitted
update cairo native to use gas consumed (#1102)
* update cairo native to use gas consumed * gas consumed * update native rev * fix gas consumed * remove comments * fixes --------- Co-authored-by: Juan Bono <juanbono94@gmail.com>
1 parent c73ef20 commit d2651a2

File tree

6 files changed

+155
-63
lines changed

6 files changed

+155
-63
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ cairo-lang-runner = { workspace = true }
3434
cairo-lang-sierra = { workspace = true }
3535
cairo-lang-starknet = { workspace = true }
3636
cairo-lang-utils = { workspace = true }
37-
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "db1c3f6b044a4a6a3de3ab33e472c2e2263d81ac", optional = true }
37+
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "03cd09ba3e51852da2234fb32a74056787abba8e", optional = true }
3838
cairo-vm = { workspace = true, features = ["cairo-1-hints"] }
3939
flate2 = "1.0.25"
4040
getset = "0.1.2"

src/execution/execution_entry_point.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,14 @@ impl ExecutionEntryPoint {
640640
tx_execution_context: &TransactionExecutionContext,
641641
block_context: &BlockContext,
642642
) -> Result<CallInfo, TransactionError> {
643+
use cairo_lang_sierra::{
644+
extensions::core::{CoreLibfunc, CoreType, CoreTypeConcrete},
645+
program_registry::ProgramRegistry,
646+
};
643647
use serde_json::json;
644648

649+
use crate::syscalls::business_logic_syscall_handler::SYSCALL_BASE;
650+
645651
let entry_point = match self.entry_point_type {
646652
EntryPointType::External => contract_class
647653
.entry_points_by_type
@@ -664,6 +670,8 @@ impl ExecutionEntryPoint {
664670
};
665671

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

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

673681
let syscall_handler = NativeSyscallHandler {
674682
starknet_storage_state: contract_storage_state,
675-
n_emitted_events: 0,
676683
events: Vec::new(),
677684
l2_to_l1_messages: Vec::new(),
678-
n_sent_messages: 0,
679685
contract_address: self.contract_address.clone(),
680686
internal_calls: Vec::new(),
681687
caller_address: self.caller_address.clone(),
682688
entry_point_selector: self.entry_point_selector.clone(),
683689
tx_execution_context: tx_execution_context.clone(),
684690
block_context: block_context.clone(),
691+
resources_manager: Default::default(),
685692
};
686693

687694
native_program
@@ -694,14 +701,20 @@ impl ExecutionEntryPoint {
694701
.as_ptr()
695702
.as_ptr() as *const () as usize;
696703

697-
let fn_id = &sierra_program
704+
let entry_point_fn = &sierra_program
698705
.funcs
699706
.iter()
700707
.find(|x| x.id.id == (entry_point.function_idx as u64))
701-
.unwrap()
702-
.id;
708+
.unwrap();
709+
let ret_types: Vec<&CoreTypeConcrete> = entry_point_fn
710+
.signature
711+
.ret_types
712+
.iter()
713+
.map(|x| program_registry.get_type(x).unwrap())
714+
.collect();
715+
let entry_point_id = &entry_point_fn.id;
703716

704-
let required_init_gas = native_program.get_required_init_gas(fn_id);
717+
let required_init_gas = native_program.get_required_init_gas(entry_point_id);
705718

706719
let calldata: Vec<_> = self
707720
.calldata
@@ -719,13 +732,13 @@ impl ExecutionEntryPoint {
719732
*/
720733

721734
let wrapped_calldata = vec![calldata];
722-
let params: Vec<Value> = sierra_program.funcs[fn_id.id as usize]
735+
let params: Vec<Value> = sierra_program.funcs[entry_point_id.id as usize]
723736
.params
724737
.iter()
725738
.map(|param| {
726739
match param.ty.debug_name.as_ref().unwrap().as_str() {
727740
"GasBuiltin" => {
728-
json!(self.initial_gas as u64)
741+
json!(self.initial_gas)
729742
}
730743
"Pedersen" | "SegmentArena" | "RangeCheck" | "Bitwise" | "Poseidon" => {
731744
json!(null)
@@ -748,11 +761,14 @@ impl ExecutionEntryPoint {
748761
let native_executor = NativeExecutor::new(native_program);
749762

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

754-
let result: String = String::from_utf8(writer).unwrap();
755-
let value = serde_json::from_str::<NativeExecutionResult>(&result).unwrap();
767+
let value = NativeExecutionResult::deserialize_from_ret_types(
768+
&mut serde_json::Deserializer::from_slice(&writer),
769+
&ret_types,
770+
)
771+
.expect("failed to serialize starknet execution result");
756772

757773
Ok(CallInfo {
758774
caller_address: self.caller_address.clone(),
@@ -773,8 +789,10 @@ impl ExecutionEntryPoint {
773789
failure_flag: value.failure_flag,
774790
l2_to_l1_messages: syscall_handler.l2_to_l1_messages,
775791
internal_calls: syscall_handler.internal_calls,
776-
// TODO: check it's correct
777-
gas_consumed: self.initial_gas - u128::from(value.gas_builtin.unwrap_or(0)),
792+
gas_consumed: self
793+
.initial_gas
794+
.saturating_sub(SYSCALL_BASE)
795+
.saturating_sub(value.remaining_gas),
778796
})
779797
}
780798
}

src/syscalls/business_logic_syscall_handler.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ use lazy_static::lazy_static;
5757
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
5858
use num_traits::{One, ToPrimitive, Zero};
5959

60-
const STEP: u128 = 100;
61-
const SYSCALL_BASE: u128 = 100 * STEP;
62-
const KECCAK_ROUND_COST: u128 = 180000;
60+
pub(crate) const STEP: u128 = 100;
61+
pub(crate) const SYSCALL_BASE: u128 = 100 * STEP;
62+
pub(crate) const KECCAK_ROUND_COST: u128 = 180000;
63+
6364
lazy_static! {
6465
/// Felt->syscall map that was extracted from new_syscalls.json (Cairo 1.0 syscalls)
6566
static ref SELECTOR_TO_SYSCALL: HashMap<Felt252, &'static str> = {
@@ -91,7 +92,7 @@ lazy_static! {
9192
// Taken from starkware/starknet/constants.py in cairo-lang
9293
// See further documentation on cairo_programs/constants.cairo
9394
/// Maps syscall name to gas costs
94-
static ref SYSCALL_GAS_COST: HashMap<&'static str, u128> = {
95+
pub(crate) static ref SYSCALL_GAS_COST: HashMap<&'static str, u128> = {
9596
let mut map = HashMap::new();
9697

9798
map.insert("initial", 100_000_000 * STEP);

0 commit comments

Comments
 (0)