Skip to content

Commit

Permalink
Chore: bump cairo-lang, blockifier and cairo-vm (#220)
Browse files Browse the repository at this point in the history
Now that `cairo-vm` supports `cairo-lang` 2.6.x, we can upgrade a few
dependencies.

* Created a new fork of blockifier to use latest stable (0.5.0) + a few
  modifications because we want `Clone` implementations. PRed the fork
  to upstream blockifier (starkware-libs/blockifier#1950).
* Bumped cairo-vm to latest main
* Removed the constraint to 2.5.4 for cairo-lang-* crates.
  • Loading branch information
Olivier Desenfans authored Jun 4, 2024
1 parent ab4d2ac commit 9aa35e2
Show file tree
Hide file tree
Showing 27 changed files with 603 additions and 571 deletions.
857 changes: 443 additions & 414 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 7 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ assert_matches = "1.5.0"
async-stream = "0.3.5"
base64 = "0.21.3"
bitvec = { version = "1.0.1", features = ["serde"] }
blockifier = { git = "https://github.com/keep-starknet-strange/blockifier", branch = "snos/callinfo-clone", features = ["clone", "testing"] }
blockifier = { git = "https://github.com/Moonsong-Labs/blockifier", branch = "msl/derive-clone", features = ["testing"] }
cairo-lang-starknet = { version = "2.6.3" }
cairo-lang-starknet-classes = { version = "2.6.3" }
cairo-lang-casm = { version = "2.6.3" }
cairo-type-derive = { version = "0.1.0", path = "cairo-type-derive" }
cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", features = ["extensive_hints", "cairo-1-hints"] }
env_logger = "0.11.3"
futures = "0.3.30"
futures-util = "0.3.30"
heck = "0.4.1"
Expand All @@ -33,24 +37,14 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = { version = "1.0.105", features = ["arbitrary_precision"] }
serde_with = "3.3.0"
serde_yaml = "0.9.25"
starknet-crypto = "0.6.0"
starknet_api = { version = "=0.7.0-dev.0", features = ["testing"] }
starknet-crypto = "0.6.2"
starknet_api = { version = "=0.10", features = ["testing"] }
thiserror = "1.0.48"
tokio = { version = "1.37.0", features = ["rt-multi-thread"] }
tokio-stream = "0.1.14"
uuid = { version = "1.4.0", features = ["v4", "serde"] }
zip = { version = "0.6.6", features = ["deflate-zlib"] }

# These dependencies pin cairo-lang requirement versions because cairo-vm does not do it
cairo-lang-starknet = { version = "~2.5.4" }
cairo-lang-casm = { version = "~2.5.4" }
cairo-lang-defs = { version = "~2.5.4" }
cairo-lang-diagnostics = { version = "~2.5.4" }
cairo-lang-filesystem = { version = "~2.5.4" }
cairo-lang-semantic = { version = "~2.5.4" }
cairo-lang-sierra = { version = "~2.5.4" }
cairo-lang-syntax = { version = "~2.5.4" }
env_logger = "0.11.3"

[dev-dependencies]
getset = "0.1.2"
Expand Down
4 changes: 2 additions & 2 deletions bin/hint_tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ name = "examples"
version = "0.1.0"

[dependencies]
blockifier = { git = "https://github.com/keep-starknet-strange/blockifier", branch = "snos/callinfo-clone", features = ["clone", "testing"] }
blockifier = { git = "https://github.com/Moonsong-Labs/blockifier", branch = "msl/derive-clone", features = ["testing"] }
cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", features = ["extensive_hints", "cairo-1-hints"] }
clap = { version = "4.5.4", features = ["derive"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = { version = "1.0.105", features = ["arbitrary_precision"] }
snos = { path = "../../" }
snos = { path = "../.." }

[[bin]]
name = "hint_tool"
Expand Down
65 changes: 43 additions & 22 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fs::File;
use std::path::PathBuf;

use blockifier::abi::constants::MAX_STEPS_PER_TX;
use blockifier::block_context::{BlockContext, FeeTokenAddresses, GasPrices};
use blockifier::block::{BlockInfo, GasPrices};
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use blockifier::transaction::objects::FeeType;
use blockifier::versioned_constants::VersionedConstants;
use cairo_vm::types::layout_name::LayoutName;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
Expand All @@ -20,6 +21,9 @@ pub fn default_layout() -> LayoutName {
LayoutName::starknet_with_keccak
}

// https://github.com/starkware-libs/blockifier/blob/8da582b285bfbc7d4c21178609bbd43f80a69240/crates/native_blockifier/src/py_block_executor.rs#L44
const MAX_STEPS_PER_TX: u32 = 4_000_000;

const DEFAULT_CONFIG_PATH: &str = "cairo-lang/src/starkware/starknet/definitions/general_config.yml";
pub const STORED_BLOCK_HASH_BUFFER: u64 = 10;
pub const BLOCK_HASH_CONTRACT_ADDRESS: u64 = 1;
Expand Down Expand Up @@ -100,8 +104,8 @@ impl Default for StarknetGeneralConfig {
min_wei_l1_data_gas_price: 100000,
min_wei_l1_gas_price: 10000000000,
},
invoke_tx_max_n_steps: MAX_STEPS_PER_TX as u32,
validate_max_n_steps: MAX_STEPS_PER_TX as u32,
invoke_tx_max_n_steps: MAX_STEPS_PER_TX,
validate_max_n_steps: MAX_STEPS_PER_TX,
default_eth_price_in_fri: 1_000_000_000_000_000_000_000,
constant_gas_price: false,
sequencer_address: contract_address!(SEQUENCER_ADDR_0_13_0),
Expand All @@ -120,25 +124,36 @@ impl StarknetGeneralConfig {
pub fn from_default_file() -> Result<StarknetGeneralConfig, SnOsError> {
StarknetGeneralConfig::from_file(PathBuf::from(DEFAULT_CONFIG_PATH))
}

pub fn empty_block_context(&self) -> BlockContext {
BlockContext {
chain_id: self.starknet_os_config.chain_id.clone(),
let mut versioned_constants = VersionedConstants::default();
versioned_constants.invoke_tx_max_n_steps = self.invoke_tx_max_n_steps;
versioned_constants.validate_max_n_steps = self.validate_max_n_steps;
versioned_constants.max_recursion_depth = 50;

let block_info = BlockInfo {
block_number: BlockNumber(0),
block_timestamp: BlockTimestamp(0),

sequencer_address: self.sequencer_address,
gas_prices: GasPrices {
eth_l1_gas_price: 1u128.try_into().unwrap(), // TODO: update with 4844
strk_l1_gas_price: 1u128.try_into().unwrap(),
eth_l1_data_gas_price: 1u128.try_into().unwrap(),
strk_l1_data_gas_price: 1u128.try_into().unwrap(),
},
use_kzg_da: false,
};

let chain_info = ChainInfo {
chain_id: self.starknet_os_config.chain_id.clone(),
fee_token_addresses: FeeTokenAddresses {
eth_fee_token_address: self.starknet_os_config.fee_token_address,
strk_fee_token_address: contract_address!("0x0"),
},
vm_resource_fee_cost: Default::default(),
gas_prices: GasPrices {
eth_l1_gas_price: 1, // TODO: update with 4844
strk_l1_gas_price: 1,
},
invoke_tx_max_n_steps: self.invoke_tx_max_n_steps,
validate_max_n_steps: self.validate_max_n_steps,
max_recursion_depth: 50,
}
};

BlockContext::new_unchecked(&block_info, &chain_info, &versioned_constants)
}
}

Expand All @@ -148,11 +163,14 @@ impl TryFrom<BlockContext> for StarknetGeneralConfig {
fn try_from(block_context: BlockContext) -> Result<Self, SnOsError> {
Ok(Self {
starknet_os_config: StarknetOsConfig {
chain_id: block_context.chain_id,
fee_token_address: block_context.fee_token_addresses.get_by_fee_type(&FeeType::Eth),
deprecated_fee_token_address: block_context.fee_token_addresses.get_by_fee_type(&FeeType::Strk),
chain_id: block_context.chain_info().chain_id.clone(),
fee_token_address: block_context.chain_info().fee_token_addresses.get_by_fee_type(&FeeType::Eth),
deprecated_fee_token_address: block_context
.chain_info()
.fee_token_addresses
.get_by_fee_type(&FeeType::Strk),
},
sequencer_address: block_context.sequencer_address,
sequencer_address: block_context.block_info().sequencer_address,
..Default::default()
})
}
Expand Down Expand Up @@ -183,8 +201,11 @@ mod tests {
let conf = StarknetGeneralConfig::default();
let ctx: BlockContext = conf.empty_block_context();

assert_eq!(conf.starknet_os_config.chain_id, ctx.chain_id);
assert_eq!(conf.starknet_os_config.fee_token_address, ctx.fee_token_addresses.get_by_fee_type(&FeeType::Eth));
assert_eq!(conf.sequencer_address, ctx.sequencer_address);
assert_eq!(conf.starknet_os_config.chain_id, ctx.chain_info().chain_id);
assert_eq!(
conf.starknet_os_config.fee_token_address,
ctx.chain_info().fee_token_addresses.get_by_fee_type(&FeeType::Eth)
);
assert_eq!(conf.sequencer_address, ctx.block_info().sequencer_address);
}
}
26 changes: 10 additions & 16 deletions src/execution/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ pub const ENTRY_POINT_INITIAL_BUDGET: u64 = 100 * STEP_GAS_COST;
pub const SYSCALL_BASE_GAS_COST: u64 = 100 * STEP_GAS_COST;
// OS gas costs.
#[allow(unused)]
pub const ENTRY_POINT_GAS_COST: u64 = blockifier::abi::constants::ENTRY_POINT_INITIAL_BUDGET + 500 * STEP_GAS_COST;
pub const ENTRY_POINT_GAS_COST: u64 = ENTRY_POINT_INITIAL_BUDGET + 500 * STEP_GAS_COST;
#[allow(unused)]
pub const FEE_TRANSFER_GAS_COST: u64 = ENTRY_POINT_GAS_COST + 100 * STEP_GAS_COST;
#[allow(unused)]
pub const TRANSACTION_GAS_COST: u64 =
(2 * ENTRY_POINT_GAS_COST) + blockifier::abi::constants::FEE_TRANSFER_GAS_COST + (100 * STEP_GAS_COST);
pub const TRANSACTION_GAS_COST: u64 = (2 * ENTRY_POINT_GAS_COST) + FEE_TRANSFER_GAS_COST + (100 * STEP_GAS_COST);
// The required gas for each syscall.
#[allow(unused)]
pub const CALL_CONTRACT_GAS_COST: u64 = SYSCALL_BASE_GAS_COST + 10 * STEP_GAS_COST + ENTRY_POINT_GAS_COST;
Expand All @@ -52,30 +51,25 @@ pub const REPLACE_CLASS_GAS_COST: u64 = SYSCALL_BASE_GAS_COST + 50 * STEP_GAS_CO
pub const SECP256K1_ADD_GAS_COST: u64 = 406 * STEP_GAS_COST + 29 * RANGE_CHECK_GAS_COST;
#[allow(unused)]
pub const SECP256K1_GET_POINT_FROM_X_GAS_COST: u64 =
391 * STEP_GAS_COST + 30 * RANGE_CHECK_GAS_COST + 20 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
391 * STEP_GAS_COST + 30 * RANGE_CHECK_GAS_COST + 20 * MEMORY_HOLE_GAS_COST;
#[allow(unused)]
pub const SECP256K1_GET_XY_GAS_COST: u64 =
239 * STEP_GAS_COST + 11 * RANGE_CHECK_GAS_COST + 40 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
pub const SECP256K1_GET_XY_GAS_COST: u64 = 239 * STEP_GAS_COST + 11 * RANGE_CHECK_GAS_COST + 40 * MEMORY_HOLE_GAS_COST;
#[allow(unused)]
pub const SECP256K1_MUL_GAS_COST: u64 =
76501 * STEP_GAS_COST + 7045 * RANGE_CHECK_GAS_COST + 2 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
pub const SECP256K1_MUL_GAS_COST: u64 = 76501 * STEP_GAS_COST + 7045 * RANGE_CHECK_GAS_COST + 2 * MEMORY_HOLE_GAS_COST;
#[allow(unused)]
pub const SECP256K1_NEW_GAS_COST: u64 =
475 * STEP_GAS_COST + 35 * RANGE_CHECK_GAS_COST + 40 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
pub const SECP256K1_NEW_GAS_COST: u64 = 475 * STEP_GAS_COST + 35 * RANGE_CHECK_GAS_COST + 40 * MEMORY_HOLE_GAS_COST;
#[allow(unused)]
pub const SECP256R1_ADD_GAS_COST: u64 = 589 * STEP_GAS_COST + 57 * RANGE_CHECK_GAS_COST;
#[allow(unused)]
pub const SECP256R1_GET_POINT_FROM_X_GAS_COST: u64 =
510 * STEP_GAS_COST + 44 * RANGE_CHECK_GAS_COST + 20 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
510 * STEP_GAS_COST + 44 * RANGE_CHECK_GAS_COST + 20 * MEMORY_HOLE_GAS_COST;
#[allow(unused)]
pub const SECP256R1_GET_XY_GAS_COST: u64 =
241 * STEP_GAS_COST + 11 * RANGE_CHECK_GAS_COST + 40 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
pub const SECP256R1_GET_XY_GAS_COST: u64 = 241 * STEP_GAS_COST + 11 * RANGE_CHECK_GAS_COST + 40 * MEMORY_HOLE_GAS_COST;
#[allow(unused)]
pub const SECP256R1_MUL_GAS_COST: u64 =
125340 * STEP_GAS_COST + 13961 * RANGE_CHECK_GAS_COST + 2 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
125340 * STEP_GAS_COST + 13961 * RANGE_CHECK_GAS_COST + 2 * MEMORY_HOLE_GAS_COST;
#[allow(unused)]
pub const SECP256R1_NEW_GAS_COST: u64 =
594 * STEP_GAS_COST + 49 * RANGE_CHECK_GAS_COST + 40 * blockifier::abi::constants::MEMORY_HOLE_GAS_COST;
pub const SECP256R1_NEW_GAS_COST: u64 = 594 * STEP_GAS_COST + 49 * RANGE_CHECK_GAS_COST + 40 * MEMORY_HOLE_GAS_COST;

#[allow(unused)]
pub const SEND_MESSAGE_TO_L1_GAS_COST: u64 = SYSCALL_BASE_GAS_COST + 50 * STEP_GAS_COST;
Expand Down
35 changes: 21 additions & 14 deletions src/execution/deprecated_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ impl DeprecatedOsSyscallHandlerWrapper {
#[cfg(test)]
mod test {
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;

use blockifier::block_context::{BlockContext, FeeTokenAddresses, GasPrices};
use blockifier::block::{BlockInfo, GasPrices};
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses};
use blockifier::execution::call_info::Retdata;
use blockifier::execution::entry_point_execution::CallResult;
use blockifier::versioned_constants::VersionedConstants;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::{MaybeRelocatable, Relocatable};
use cairo_vm::vm::vm_core::VirtualMachine;
Expand All @@ -176,26 +176,33 @@ mod test {

#[fixture]
fn block_context() -> BlockContext {
BlockContext {
let chain_info = ChainInfo {
chain_id: ChainId("SN_GOERLI".to_string()),
fee_token_addresses: FeeTokenAddresses {
strk_fee_token_address: contract_address!("0x1"),
eth_fee_token_address: contract_address!("0x2"),
},
};

let block_info = BlockInfo {
block_number: BlockNumber(1_000_000),
block_timestamp: BlockTimestamp(1_704_067_200),
sequencer_address: contract_address!("0x0"),
fee_token_addresses: FeeTokenAddresses {
eth_fee_token_address: contract_address!("0x1"),
strk_fee_token_address: contract_address!("0x2"),
gas_prices: GasPrices {
eth_l1_gas_price: 1u128.try_into().unwrap(),
strk_l1_gas_price: 1u128.try_into().unwrap(),
eth_l1_data_gas_price: 1u128.try_into().unwrap(),
strk_l1_data_gas_price: 1u128.try_into().unwrap(),
},
vm_resource_fee_cost: Arc::new(HashMap::new()),
gas_prices: GasPrices { eth_l1_gas_price: 1, strk_l1_gas_price: 1 },
invoke_tx_max_n_steps: 1,
validate_max_n_steps: 1,
max_recursion_depth: 50,
}
use_kzg_da: false,
};

BlockContext::new_unchecked(&block_info, &chain_info, &VersionedConstants::latest_constants())
}

#[fixture]
fn old_block_number_and_hash(block_context: BlockContext) -> (Felt252, Felt252) {
(Felt252::from(block_context.block_number.0 - STORED_BLOCK_HASH_BUFFER), Felt252::from(66_u64))
(Felt252::from(block_context.block_info().block_number.0 - STORED_BLOCK_HASH_BUFFER), Felt252::from(66_u64))
}

#[rstest]
Expand Down
10 changes: 7 additions & 3 deletions src/execution/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Deref;
use std::rc::Rc;
use std::vec::IntoIter;

use blockifier::block_context::BlockContext;
use blockifier::context::BlockContext;
use blockifier::execution::call_info::CallInfo;
use blockifier::execution::entry_point_execution::CallResult;
use blockifier::transaction::objects::TransactionExecutionInfo;
Expand Down Expand Up @@ -70,8 +70,12 @@ impl ExecutionHelperWrapper {
) -> Self {
// Block number and block hash (current_block_number - buffer) block buffer=STORED_BLOCK_HASH_BUFFER
// Hash that is going to be written by this OS run
let prev_block_context =
block_context.block_number.0.checked_sub(STORED_BLOCK_HASH_BUFFER).map(|_| block_context.clone());
let prev_block_context = block_context
.block_info()
.block_number
.0
.checked_sub(STORED_BLOCK_HASH_BUFFER)
.map(|_| block_context.clone());

Self {
execution_helper: Rc::new(RwLock::new(ExecutionHelper {
Expand Down
4 changes: 2 additions & 2 deletions src/execution/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::execution::syscall_handler_utils::{
};
use crate::utils::felt_api2vm;

/// DeprecatedSyscallHandlerimplementation for execution of system calls in the StarkNet OS
/// DeprecatedSyscallHandler implementation for execution of system calls in the StarkNet OS
#[derive(Debug)]
pub struct OsSyscallHandler {
pub exec_wrapper: ExecutionHelperWrapper,
Expand All @@ -28,7 +28,7 @@ pub struct OsSyscallHandler {
}

/// OsSyscallHandler is wrapped in Rc<RefCell<_>> in order
/// to clone the refrence when entering and exiting vm scopes
/// to clone the reference when entering and exiting vm scopes
#[derive(Clone, Debug)]
pub struct OsSyscallHandlerWrapper {
pub syscall_handler: Rc<RwLock<OsSyscallHandler>>,
Expand Down
10 changes: 5 additions & 5 deletions src/hints/block_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::any::Any;
use std::collections::hash_map::IntoIter;
use std::collections::HashMap;

use blockifier::block_context::BlockContext;
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use blockifier::context::BlockContext;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_vm::hint_processor::builtin_hint_processor::dict_manager::Dictionary;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_ptr_from_var_name, get_relocatable_from_var_name, insert_value_from_var_name, insert_value_into_ap,
Expand Down Expand Up @@ -177,7 +177,7 @@ pub fn block_number(
) -> Result<(), HintError> {
// TODO: replace w/ block context from syscall handler
let block_context = exec_scopes.get_ref::<BlockContext>("block_context")?;
insert_value_into_ap(vm, Felt252::from(block_context.block_number.0))
insert_value_into_ap(vm, Felt252::from(block_context.block_info().block_number.0))
}

pub const BLOCK_TIMESTAMP: &str = "memory[ap] = to_felt_or_relocatable(syscall_handler.block_info.block_timestamp)";
Expand All @@ -189,7 +189,7 @@ pub fn block_timestamp(
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let block_context = exec_scopes.get_ref::<BlockContext>("block_context")?;
insert_value_into_ap(vm, Felt252::from(block_context.block_timestamp.0))
insert_value_into_ap(vm, Felt252::from(block_context.block_info().block_timestamp.0))
}

pub const CHAIN_ID: &str = "memory[ap] = to_felt_or_relocatable(os_input.general_config.chain_id.value)";
Expand Down Expand Up @@ -244,7 +244,7 @@ pub fn sequencer_address(
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let block_context = exec_scopes.get_ref::<BlockContext>("block_context")?;
insert_value_into_ap(vm, felt_api2vm(*block_context.sequencer_address.0.key()))
insert_value_into_ap(vm, felt_api2vm(*block_context.block_info().sequencer_address.0.key()))
}

pub const GET_BLOCK_MAPPING: &str = indoc! {r#"
Expand Down
7 changes: 3 additions & 4 deletions src/hints/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1808,12 +1808,11 @@ mod tests {
use std::cell::RefCell;
use std::rc::Rc;

use blockifier::block_context::BlockContext;
use blockifier::context::BlockContext;
use cairo_vm::hint_processor::builtin_hint_processor::dict_manager::DictManager;
use cairo_vm::types::relocatable::Relocatable;
use num_bigint::BigUint;
use rstest::{fixture, rstest};
use starknet_api::block::BlockNumber;

use super::*;
use crate::config::STORED_BLOCK_HASH_BUFFER;
Expand All @@ -1829,12 +1828,12 @@ mod tests {

#[fixture]
pub fn block_context() -> BlockContext {
BlockContext { block_number: BlockNumber(10), ..BlockContext::create_for_account_testing() }
BlockContext::create_for_account_testing()
}

#[fixture]
fn old_block_number_and_hash(block_context: BlockContext) -> (Felt252, Felt252) {
(Felt252::from(block_context.block_number.0 - STORED_BLOCK_HASH_BUFFER), Felt252::from(66_u64))
(Felt252::from(block_context.block_info().block_number.0 - STORED_BLOCK_HASH_BUFFER), Felt252::from(66_u64))
}

#[fixture]
Expand Down
Loading

0 comments on commit 9aa35e2

Please sign in to comment.