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

Commit ee8507d

Browse files
authored
Add global support for program caches. (#1140)
* Add global support for program caches. * Update cairo-native dependency and fix stuff. * Fix compilation errors.
1 parent e6d0e14 commit ee8507d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1366
-282
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 = "4012a10b97530e208b76d42169aa9608a6a9d8fd", optional = true }
37+
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "f668096bd6382066392cd563873dda5e7885a388", optional = true }
3838
cairo-vm = { workspace = true }
3939
flate2 = "1.0.25"
4040
getset = "0.1.2"

bench/internals.rs

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![deny(warnings)]
22

3+
use cairo_native::cache::ProgramCache;
34
use cairo_vm::felt;
45
use felt::{felt_str, Felt252};
56
use lazy_static::lazy_static;
@@ -21,6 +22,12 @@ use starknet_in_rust::{
2122
};
2223
use std::{collections::HashMap, hint::black_box, sync::Arc};
2324

25+
#[cfg(feature = "cairo-native")]
26+
use {
27+
starknet_in_rust::utils::ClassHash,
28+
std::{cell::RefCell, rc::Rc},
29+
};
30+
2431
lazy_static! {
2532
// include_str! doesn't seem to work in CI
2633
static ref CONTRACT_CLASS: ContractClass = ContractClass::from_path(
@@ -48,18 +55,25 @@ fn scope<T>(f: impl FnOnce() -> T) -> T {
4855
// We don't use the cargo test harness because it uses
4956
// FnOnce calls for each test, that are merged in the flamegraph.
5057
fn main() {
51-
deploy_account();
52-
declare();
53-
deploy();
54-
invoke();
58+
#[cfg(feature = "cairo-native")]
59+
let program_cache = Rc::new(RefCell::new(ProgramCache::new(
60+
starknet_in_rust::utils::get_native_context(),
61+
)));
62+
63+
deploy_account(program_cache.clone());
64+
declare(program_cache.clone());
65+
deploy(program_cache.clone());
66+
invoke(program_cache.clone());
5567

5668
// The black_box ensures there's no tail-call optimization.
5769
// If not, the flamegraph ends up less nice.
5870
black_box(());
5971
}
6072

6173
#[inline(never)]
62-
fn deploy_account() {
74+
fn deploy_account(
75+
#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCache<ClassHash>>>,
76+
) {
6377
const RUNS: usize = 500;
6478

6579
let state_reader = Arc::new(InMemoryStateReader::default());
@@ -91,14 +105,19 @@ fn deploy_account() {
91105
StarknetChainId::TestNet.to_felt(),
92106
)
93107
.unwrap();
94-
internal_deploy_account.execute(&mut state_copy, block_context)
108+
internal_deploy_account.execute(
109+
&mut state_copy,
110+
block_context,
111+
#[cfg(feature = "cairo-native")]
112+
Some(program_cache.clone()),
113+
)
95114
})
96115
.unwrap();
97116
}
98117
}
99118

100119
#[inline(never)]
101-
fn declare() {
120+
fn declare(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCache<ClassHash>>>) {
102121
const RUNS: usize = 5;
103122

104123
let state_reader = Arc::new(InMemoryStateReader::default());
@@ -123,14 +142,19 @@ fn declare() {
123142
)
124143
.expect("couldn't create transaction");
125144

126-
declare_tx.execute(&mut cloned_state, block_context)
145+
declare_tx.execute(
146+
&mut cloned_state,
147+
block_context,
148+
#[cfg(feature = "cairo-native")]
149+
Some(program_cache.clone()),
150+
)
127151
})
128152
.unwrap();
129153
}
130154
}
131155

132156
#[inline(never)]
133-
fn deploy() {
157+
fn deploy(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCache<ClassHash>>>) {
134158
const RUNS: usize = 8;
135159

136160
let state_reader = Arc::new(InMemoryStateReader::default());
@@ -161,14 +185,19 @@ fn deploy() {
161185
0.into(),
162186
)
163187
.unwrap();
164-
internal_deploy.execute(&mut state_copy, block_context)
188+
internal_deploy.execute(
189+
&mut state_copy,
190+
block_context,
191+
#[cfg(feature = "cairo-native")]
192+
Some(program_cache.clone()),
193+
)
165194
})
166195
.unwrap();
167196
}
168197
}
169198

170199
#[inline(never)]
171-
fn invoke() {
200+
fn invoke(#[cfg(feature = "cairo-native")] program_cache: Rc<RefCell<ProgramCache<ClassHash>>>) {
172201
const RUNS: usize = 100;
173202

174203
let state_reader = Arc::new(InMemoryStateReader::default());
@@ -195,7 +224,14 @@ fn invoke() {
195224
)
196225
.unwrap();
197226

198-
let _deploy_exec_info = deploy.execute(&mut state, block_context).unwrap();
227+
let _deploy_exec_info = deploy
228+
.execute(
229+
&mut state,
230+
block_context,
231+
#[cfg(feature = "cairo-native")]
232+
Some(program_cache.clone()),
233+
)
234+
.unwrap();
199235

200236
for _ in 0..RUNS {
201237
let mut state_copy = state.clone();
@@ -216,7 +252,13 @@ fn invoke() {
216252
Some(Felt252::zero()),
217253
)
218254
.unwrap();
219-
internal_invoke.execute(&mut state_copy, block_context, 2_000_000)
255+
internal_invoke.execute(
256+
&mut state_copy,
257+
block_context,
258+
2_000_000,
259+
#[cfg(feature = "cairo-native")]
260+
Some(program_cache.clone()),
261+
)
220262
})
221263
.unwrap();
222264
}

bench/native_bench.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77
// $ native_bench <n_executions> native <fibo|fact>
88
// where fibo executes a fibonacci function and fact a factorial n times.
99

10-
#![cfg(feature = "cairo-native")]
11-
12-
#[cfg(not(feature = "cairo-native"))]
13-
fn main() {
14-
unimplemented!("This program should be compiled with the cairo-native feature");
15-
}
16-
1710
use cairo_native::cache::ProgramCache;
1811
use cairo_native::context::NativeContext;
1912
use cairo_vm::felt::felt_str;
@@ -26,6 +19,7 @@ use starknet_in_rust::services::api::contract_classes::compiled_class::CompiledC
2619
use starknet_in_rust::state::state_api::State;
2720
use starknet_in_rust::transaction::DeployAccount;
2821
use starknet_in_rust::utils::calculate_sn_keccak;
22+
use starknet_in_rust::utils::get_native_context;
2923
use starknet_in_rust::CasmContractClass;
3024
use starknet_in_rust::EntryPointType;
3125
use starknet_in_rust::{
@@ -244,6 +238,7 @@ fn bench_erc20(executions: usize, native: bool) {
244238
static ref ERC20_DEPLOYMENT_CALLER_ADDRESS: Address = Address(0000.into());
245239
}
246240

241+
let program_cache = Rc::new(RefCell::new(ProgramCache::new(get_native_context())));
247242
let (erc20_address, mut state): (Address, CachedState<InMemoryStateReader>) = match native {
248243
true => {
249244
let erc20_sierra_class = include_bytes!("../starknet_programs/cairo2/erc20.sierra");
@@ -317,6 +312,7 @@ fn bench_erc20(executions: usize, native: bool) {
317312
&mut tx_execution_context,
318313
false,
319314
block_context.invoke_tx_max_n_steps(),
315+
Some(program_cache.clone()),
320316
)
321317
.unwrap();
322318

@@ -395,6 +391,7 @@ fn bench_erc20(executions: usize, native: bool) {
395391
&mut tx_execution_context,
396392
false,
397393
block_context.invoke_tx_max_n_steps(),
394+
Some(program_cache.clone()),
398395
)
399396
.unwrap();
400397

@@ -454,7 +451,7 @@ fn bench_erc20(executions: usize, native: bool) {
454451
// this will create the account and after that,
455452
// we can extract its address.
456453
let account1_address = account1_deploy_tx
457-
.execute(&mut state, &Default::default())
454+
.execute(&mut state, &Default::default(), Some(program_cache.clone()))
458455
.expect("failed to execute the deployment of account 1")
459456
.validate_info
460457
.expect("validate_info missing")
@@ -482,7 +479,7 @@ fn bench_erc20(executions: usize, native: bool) {
482479

483480
// execute the deploy_account transaction and retrieve the deployed account address.
484481
let _account2_address = account2_deploy_tx
485-
.execute(&mut state, &Default::default())
482+
.execute(&mut state, &Default::default(), Some(program_cache.clone()))
486483
.expect("failed to execute the deployment of account 2")
487484
.validate_info
488485
.expect("validate_info missing")
@@ -494,9 +491,6 @@ fn bench_erc20(executions: usize, native: bool) {
494491
// calldata for transfering 123 tokens from account1 to account2
495492
let calldata = vec![Felt252::from(12), Felt252::from(123)];
496493

497-
let native_ctx = NativeContext::new();
498-
let program_cache = Rc::new(RefCell::new(ProgramCache::new(&native_ctx)));
499-
500494
for _ in 0..executions {
501495
let result = execute(
502496
&mut state.clone(),
@@ -550,14 +544,14 @@ fn execute(
550544
let mut resources_manager = ExecutionResourcesManager::default();
551545

552546
exec_entry_point
553-
.execute_with_native_cache(
547+
.execute(
554548
state,
555549
&block_context,
556550
&mut resources_manager,
557551
&mut tx_execution_context,
558552
false,
559553
block_context.invoke_tx_max_n_steps(),
560-
program_cache,
554+
Some(program_cache),
561555
)
562556
.unwrap()
563557
.call_info

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[features]
77
default = ["with_mimalloc"]
8+
cairo-native = ["starknet_in_rust/cairo-native"]
89
with_mimalloc = ["dep:mimalloc"]
910

1011
[dependencies]

cli/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ fn invoke_parser(
201201
transaction_hash.unwrap(),
202202
)?;
203203
let mut transactional_state = cached_state.create_transactional();
204-
let _tx_info = internal_invoke.apply(&mut transactional_state, &BlockContext::default(), 0)?;
204+
let _tx_info = internal_invoke.apply(
205+
&mut transactional_state,
206+
&BlockContext::default(),
207+
0,
208+
#[cfg(feature = "cairo-native")]
209+
None,
210+
)?;
205211
cached_state.apply_state_update(&StateDiff::from_cached_state(transactional_state.cache())?)?;
206212

207213
let tx_hash = calculate_transaction_hash_common(
@@ -267,6 +273,8 @@ fn call_parser(
267273
&mut TransactionExecutionContext::default(),
268274
false,
269275
block_context.invoke_tx_max_n_steps(),
276+
#[cfg(feature = "cairo-native")]
277+
None,
270278
)?;
271279

272280
let call_info = call_info.ok_or(TransactionError::CallInfoIsNone)?;

examples/contract_execution/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name = "contract_execution"
33
version = "0.4.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
6+
[features]
7+
cairo-native = ["starknet_in_rust/cairo-native"]
78

89
[dependencies]
910
cairo-vm = { workspace = true }

examples/contract_execution/src/main.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ fn test_contract(
119119
.unwrap();
120120

121121
let account_contract_address = internal_deploy
122-
.execute(&mut state, &block_context)
122+
.execute(
123+
&mut state,
124+
&block_context,
125+
#[cfg(feature = "cairo-native")]
126+
None,
127+
)
123128
.expect("Account Deploy Failed")
124129
.call_info
125130
.unwrap()
@@ -155,7 +160,12 @@ fn test_contract(
155160
.expect("couldn't create declare transaction");
156161

157162
declare_tx
158-
.execute(&mut state, &block_context)
163+
.execute(
164+
&mut state,
165+
&block_context,
166+
#[cfg(feature = "cairo-native")]
167+
None,
168+
)
159169
.expect("could not declare the contract class");
160170

161171
//* ----------------------------------------------------------
@@ -175,7 +185,13 @@ fn test_contract(
175185
.unwrap();
176186

177187
let contract_address = deploy
178-
.execute(&mut state, &block_context, 0)
188+
.execute(
189+
&mut state,
190+
&block_context,
191+
0,
192+
#[cfg(feature = "cairo-native")]
193+
None,
194+
)
179195
.expect("could not deploy contract")
180196
.call_info
181197
.unwrap()
@@ -218,7 +234,15 @@ fn test_contract(
218234
)
219235
.unwrap();
220236

221-
let tx_exec_info = invoke_tx.execute(&mut state, &block_context, 0).unwrap();
237+
let tx_exec_info = invoke_tx
238+
.execute(
239+
&mut state,
240+
&block_context,
241+
0,
242+
#[cfg(feature = "cairo-native")]
243+
None,
244+
)
245+
.unwrap();
222246

223247
//* --------------------------------------------
224248
//* Extract return values

fuzzer/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name = "fuzzer"
33
version = "0.4.0"
44
edition = "2021"
55

6-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
6+
[features]
7+
cairo-native = ["starknet_in_rust/cairo-native"]
78

89
[dependencies]
910
honggfuzz = "0.5.55"

fuzzer/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ fn main() {
194194
&mut tx_execution_context,
195195
false,
196196
block_context.invoke_tx_max_n_steps(),
197+
#[cfg(feature = "cairo-native")]
198+
None,
197199
)
198200
.unwrap();
199201
assert_eq!(call_info.unwrap(), expected_call_info);

0 commit comments

Comments
 (0)