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

Update cairo-native without the SyscallHandlerMeta API. #1254

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
556 changes: 260 additions & 296 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cairo-lang-casm = { 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 = "baf57d2dde0036ac4848fc40c672826fb7ffcde4", optional = true }
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "8f7602cb2a13682345547867a6c91b085a344da6", optional = true }
k256 = "0.13.3"
p256 = "0.13.2"
sec1 = "0.7.3"
Expand Down
2 changes: 1 addition & 1 deletion fuzzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn main() {
.entry_points_by_type()
.get(&EntryPointType::External)
.unwrap()
.get(0)
.first()
.unwrap()
.selector();

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.74.1"
channel = "1.75.0"
components = ["rustfmt", "clippy"]
profile = "minimal"
2 changes: 1 addition & 1 deletion src/bin/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() {
let fib_entrypoint_selector = *entry_points_by_type
.get(&EntryPointType::External)
.unwrap()
.get(0)
.first()
.unwrap()
.selector();

Expand Down
16 changes: 8 additions & 8 deletions src/bin/native_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn bench_fibo(executions: usize, bench_type: BenchType) {
let (contract_class, constructor_selector) = match bench_type {
BenchType::VM => {
let entrypoints = casm_contract_class.clone().entry_points_by_type;
let constructor_selector = entrypoints.external.get(0).unwrap().selector.clone();
let constructor_selector = entrypoints.external.first().unwrap().selector.clone();

(
CompiledClass::Casm {
Expand All @@ -113,7 +113,7 @@ fn bench_fibo(executions: usize, bench_type: BenchType) {
serde_json::from_slice(sierra_data).unwrap();

let entrypoints = sierra_contract_class.clone().entry_points_by_type;
let constructor_selector = entrypoints.external.get(0).unwrap().selector.clone();
let constructor_selector = entrypoints.external.first().unwrap().selector.clone();
let sierra_program = sierra_contract_class.extract_sierra_program().unwrap();
let entrypoints = sierra_contract_class.entry_points_by_type;
(
Expand Down Expand Up @@ -183,7 +183,7 @@ fn bench_fact(executions: usize, bench_type: BenchType) {
let (contract_class, constructor_selector) = match bench_type {
BenchType::VM => {
let entrypoints = casm_contract_class.clone().entry_points_by_type;
let constructor_selector = entrypoints.external.get(0).unwrap().selector.clone();
let constructor_selector = entrypoints.external.first().unwrap().selector.clone();

(
CompiledClass::Casm {
Expand All @@ -199,7 +199,7 @@ fn bench_fact(executions: usize, bench_type: BenchType) {
serde_json::from_slice(sierra_data).unwrap();

let entrypoints = sierra_contract_class.clone().entry_points_by_type;
let constructor_selector = entrypoints.external.get(0).unwrap().selector.clone();
let constructor_selector = entrypoints.external.first().unwrap().selector.clone();
let sierra_program = sierra_contract_class.extract_sierra_program().unwrap();
let entrypoints = sierra_contract_class.entry_points_by_type;
(
Expand Down Expand Up @@ -318,7 +318,7 @@ fn bench_erc20(executions: usize, bench_type: BenchType) {
};

let entrypoints = erc20_deployer_class.clone().entry_points_by_type;
let deploy_entrypoint_selector = &entrypoints.external.get(0).unwrap().selector;
let deploy_entrypoint_selector = &entrypoints.external.first().unwrap().selector;

// insert deployer and erc20 classes into the cache.
contract_class_cache.set_contract_class(
Expand Down Expand Up @@ -383,7 +383,7 @@ fn bench_erc20(executions: usize, bench_type: BenchType) {
.unwrap();

// obtain the address of the deployed erc20 contract
let erc20_address = *call_info.call_info.unwrap().retdata.get(0).unwrap();
let erc20_address = *call_info.call_info.unwrap().retdata.first().unwrap();

(Address(erc20_address), state)
}
Expand All @@ -399,7 +399,7 @@ fn bench_erc20(executions: usize, bench_type: BenchType) {
};

let entrypoints = erc20_deployer_class.clone().entry_points_by_type;
let deploy_entrypoint_selector = &entrypoints.external.get(0).unwrap().selector;
let deploy_entrypoint_selector = &entrypoints.external.first().unwrap().selector;

// insert deployer and erc20 classes into the cache.
contract_class_cache.set_contract_class(
Expand Down Expand Up @@ -464,7 +464,7 @@ fn bench_erc20(executions: usize, bench_type: BenchType) {
.unwrap();

// obtain the address of the deployed erc20 contract
let erc20_address = *call_info.call_info.unwrap().retdata.get(0).unwrap();
let erc20_address = *call_info.call_info.unwrap().retdata.first().unwrap();

(Address(erc20_address), state)
}
Expand Down
58 changes: 29 additions & 29 deletions src/core/contract_address/sierra_contract_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,35 +139,6 @@ fn get_contract_entry_points(
Ok(entry_points)
}

#[cfg(test)]
mod tests {
use crate::core::contract_address::compute_sierra_class_hash;
use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass;
use cairo_vm::Felt252;
use std::{fs::File, io::BufReader};

/// Test the correctness of the compute_sierra_class_hash function for a specific testnet contract.
#[test]
fn test_declare_tx_from_testnet() {
let file = File::open("starknet_programs/raw_contract_classes/0x113bf26d112a164297e04381212c9bd7409f07591f0a04f539bdf56693eaaf3.sierra").unwrap();
// 0x113bf26d112a164297e04381212c9bd7409f07591f0a04f539bdf56693eaaf3
let reader = BufReader::new(file);

let sierra_contract_class: SierraContractClass = serde_json::from_reader(reader).unwrap();

// this is the class_hash from: https://alpha4.starknet.io/feeder_gateway/get_transaction?transactionHash=0x01b852f1fe2b13db21a44f8884bc4b7760dc277bb3820b970dba929860275617
let expected_result = Felt252::from_dec_str(
"487202222862199115032202787294865701687663153957776561394399544814644144883",
)
.unwrap();

assert_eq!(
compute_sierra_class_hash(&sierra_contract_class).unwrap(),
expected_result
)
}
}

struct PythonJsonFormatter;

impl Formatter for PythonJsonFormatter {
Expand Down Expand Up @@ -220,3 +191,32 @@ impl Formatter for PythonJsonFormatter {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::core::contract_address::compute_sierra_class_hash;
use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass;
use cairo_vm::Felt252;
use std::{fs::File, io::BufReader};

/// Test the correctness of the compute_sierra_class_hash function for a specific testnet contract.
#[test]
fn test_declare_tx_from_testnet() {
let file = File::open("starknet_programs/raw_contract_classes/0x113bf26d112a164297e04381212c9bd7409f07591f0a04f539bdf56693eaaf3.sierra").unwrap();
// 0x113bf26d112a164297e04381212c9bd7409f07591f0a04f539bdf56693eaaf3
let reader = BufReader::new(file);

let sierra_contract_class: SierraContractClass = serde_json::from_reader(reader).unwrap();

// this is the class_hash from: https://alpha4.starknet.io/feeder_gateway/get_transaction?transactionHash=0x01b852f1fe2b13db21a44f8884bc4b7760dc277bb3820b970dba929860275617
let expected_result = Felt252::from_dec_str(
"487202222862199115032202787294865701687663153957776561394399544814644144883",
)
.unwrap();

assert_eq!(
compute_sierra_class_hash(&sierra_contract_class).unwrap(),
expected_result
)
}
}
13 changes: 4 additions & 9 deletions src/execution/execution_entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl ExecutionEntryPoint {
);

// fetch syscall_ptr
let initial_syscall_ptr: Relocatable = match os_context.get(0) {
let initial_syscall_ptr: Relocatable = match os_context.first() {
Some(MaybeRelocatable::RelocatableValue(ptr)) => ptr.to_owned(),
_ => return Err(TransactionError::NotARelocatableValue),
};
Expand Down Expand Up @@ -687,17 +687,14 @@ impl ExecutionEntryPoint {
class_hash: &ClassHash,
program_cache: Rc<RefCell<ProgramCache<'_, ClassHash>>>,
) -> Result<CallInfo, TransactionError> {
use cairo_native::{
executor::NativeExecutor, metadata::syscall_handler::SyscallHandlerMeta,
};

use crate::{
syscalls::{
business_logic_syscall_handler::SYSCALL_BASE,
native_syscall_handler::NativeSyscallHandler,
},
utils::NATIVE_CONTEXT,
};
use cairo_native::executor::NativeExecutor;

// Ensure we're using the global context, if initialized.
if let Some(native_context) = NATIVE_CONTEXT.get() {
Expand Down Expand Up @@ -757,8 +754,6 @@ impl ExecutionEntryPoint {
resources_manager: Default::default(),
};

let syscall_meta = SyscallHandlerMeta::new(&mut syscall_handler);

let entry_point_fn = &sierra_program
.funcs
.iter()
Expand All @@ -772,13 +767,13 @@ impl ExecutionEntryPoint {
entry_point_id,
&self.calldata,
Some(self.initial_gas),
Some(&syscall_meta),
&mut syscall_handler,
),
NativeExecutor::Jit(executor) => executor.invoke_contract_dynamic(
entry_point_id,
&self.calldata,
Some(self.initial_gas),
Some(&syscall_meta),
&mut syscall_handler,
),
}
.map_err(|e| TransactionError::CustomError(format!("cairo-native error: {:?}", e)))?;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ mod test {

let contract_class: CasmContractClass = serde_json::from_slice(program_data).unwrap();
let entrypoints = contract_class.clone().entry_points_by_type;
let entrypoint_selector = Felt252::from(&entrypoints.external.get(0).unwrap().selector);
let entrypoint_selector = Felt252::from(&entrypoints.external.first().unwrap().selector);

let contract_class_cache = PermanentContractClassCache::default();

Expand Down Expand Up @@ -460,7 +460,7 @@ mod test {
let program_data = include_bytes!("../starknet_programs/cairo1/fibonacci.casm");
let contract_class: CasmContractClass = serde_json::from_slice(program_data).unwrap();
let entrypoints = contract_class.clone().entry_points_by_type;
let entrypoint_selector = Felt252::from(&entrypoints.external.get(0).unwrap().selector);
let entrypoint_selector = Felt252::from(&entrypoints.external.first().unwrap().selector);

let contract_class_cache = PermanentContractClassCache::default();

Expand Down
26 changes: 13 additions & 13 deletions src/syscalls/native_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use cairo_native::{
cache::ProgramCache,
starknet::{
BlockInfo, ExecutionInfo, ExecutionInfoV2, ResourceBounds, Secp256k1Point, Secp256r1Point,
StarkNetSyscallHandler, SyscallResult, TxInfo, TxV2Info, U256,
StarknetSyscallHandler, SyscallResult, TxInfo, TxV2Info, U256,
},
};
use cairo_vm::Felt252;
Expand Down Expand Up @@ -85,8 +85,8 @@ impl<'a, 'cache, S: StateReader, C: ContractClassCache> NativeSyscallHandler<'a,
}
}

impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarkNetSyscallHandler
for NativeSyscallHandler<'a, 'cache, S, C>
impl<'a, 'cache, S: StateReader, C: ContractClassCache> StarknetSyscallHandler
for &mut NativeSyscallHandler<'a, 'cache, S, C>
{
fn get_block_hash(
&mut self,
Expand Down Expand Up @@ -1278,7 +1278,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256k1_new(
U256 {
hi: 0,
Expand All @@ -1304,7 +1304,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256k1_add(
Secp256k1Point {
x: U256 {
Expand Down Expand Up @@ -1342,7 +1342,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256k1_mul(
Secp256k1Point {
x: U256 {
Expand Down Expand Up @@ -1374,7 +1374,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256k1_get_point_from_x(
U256 {
hi: 0,
Expand All @@ -1397,7 +1397,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let (x, y) = syscall_handler
let (x, y) = (&mut syscall_handler)
.secp256k1_get_xy(
Secp256k1Point {
x: U256 {
Expand All @@ -1424,7 +1424,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256r1_new(
U256 {
hi: 0,
Expand All @@ -1450,7 +1450,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256r1_add(
Secp256r1Point {
x: U256 {
Expand Down Expand Up @@ -1488,7 +1488,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256r1_mul(
Secp256r1Point {
x: U256 {
Expand Down Expand Up @@ -1520,7 +1520,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let p = syscall_handler
let p = (&mut syscall_handler)
.secp256r1_get_point_from_x(
U256 {
hi: 0,
Expand All @@ -1543,7 +1543,7 @@ mod test {
let mut syscall_handler = test_ctx.new_syscall_handler();
let mut gas = 0;

let (x, y) = syscall_handler
let (x, y) = (&mut syscall_handler)
.secp256r1_get_xy(
Secp256r1Point {
x: U256 {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/account_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn account_panic() {
let selector_contract = &contract_class
.entry_points_by_type
.external
.get(0)
.first()
.unwrap()
.selector;
// calldata of contract_a is 1 value.
Expand Down
Loading
Loading