Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump blockifier to support Cairo 2.7 #2180

Merged
merged 27 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 25 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
1,003 changes: 477 additions & 526 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions bin/katana/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use clap_complete::{generate, Shell};
use console::Style;
use dojo_metrics::{metrics_process, prometheus_exporter};
use katana_core::constants::MAX_RECURSION_DEPTH;
use katana_core::env::get_default_vm_resource_fee_cost;
use katana_core::sequencer::KatanaSequencer;
use katana_executor::SimulationFlag;
use katana_primitives::class::ClassHash;
Expand Down Expand Up @@ -49,7 +48,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let cfg_env = CfgEnv {
chain_id: starknet_config.env.chain_id,
vm_resource_fee_cost: get_default_vm_resource_fee_cost(),
invoke_tx_max_n_steps: starknet_config.env.invoke_max_steps,
validate_max_n_steps: starknet_config.env.validate_max_steps,
max_recursion_depth: MAX_RECURSION_DEPTH,
Expand Down
2 changes: 0 additions & 2 deletions crates/dojo-test-utils/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::Arc;
use jsonrpsee::core::Error;
pub use katana_core::backend::config::{Environment, StarknetConfig};
use katana_core::constants::MAX_RECURSION_DEPTH;
use katana_core::env::get_default_vm_resource_fee_cost;
use katana_core::sequencer::KatanaSequencer;
pub use katana_core::sequencer::SequencerConfig;
use katana_executor::implementation::blockifier::BlockifierFactory;
Expand Down Expand Up @@ -40,7 +39,6 @@ impl TestSequencer {
pub async fn start(config: SequencerConfig, starknet_config: StarknetConfig) -> Self {
let cfg_env = CfgEnv {
chain_id: starknet_config.env.chain_id,
vm_resource_fee_cost: get_default_vm_resource_fee_cost(),
invoke_tx_max_n_steps: starknet_config.env.invoke_max_steps,
validate_max_n_steps: starknet_config.env.validate_max_steps,
max_recursion_depth: MAX_RECURSION_DEPTH,
Expand Down
17 changes: 9 additions & 8 deletions crates/katana/cairo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ version.workspace = true

# Use from git instead of crates.io registry so that the workspace patches aren't applied.
[dependencies]
cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo", tag = "v2.6.3" }
cairo-lang-runner = { git = "https://github.com/starkware-libs/cairo", tag = "v2.6.3" }
cairo-lang-sierra = { git = "https://github.com/starkware-libs/cairo", tag = "v2.6.3" }
cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo", tag = "v2.6.3" }
cairo-lang-starknet-classes = { git = "https://github.com/starkware-libs/cairo", tag = "v2.6.3" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", tag = "v2.6.3" }
cairo-vm = "0.9.2"
starknet_api = "0.11.0"
cairo-lang-casm = "2.7.0-rc.1"
cairo-lang-runner = "2.7.0-rc.1"
cairo-lang-sierra = "2.7.0-rc.1"
cairo-lang-sierra-to-casm = "2.7.0-rc.1"
cairo-lang-starknet = "2.7.0-rc.1"
cairo-lang-starknet-classes = "2.7.0-rc.1"
cairo-lang-utils = "2.7.0-rc.1"
cairo-vm = "1.0.0-rc3"
starknet_api = "0.13.0-dev.9"
12 changes: 7 additions & 5 deletions crates/katana/cairo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
//! Re-export of the Cairo language crates used throughout Katana.

pub mod lang {
pub use {
cairo_lang_casm as casm, cairo_lang_runner as runner, cairo_lang_sierra as sierra,
cairo_lang_starknet as starknet, cairo_lang_starknet_classes as starknet_classes,
cairo_lang_utils as utils,
};
pub extern crate cairo_lang_casm as casm;
pub extern crate cairo_lang_runner as runner;
pub extern crate cairo_lang_sierra as sierra;
pub extern crate cairo_lang_sierra_to_casm as sierra_to_casm;
pub extern crate cairo_lang_starknet as starknet;
pub extern crate cairo_lang_starknet_classes as starknet_classes;
pub extern crate cairo_lang_utils as utils;
}

pub use {cairo_vm, starknet_api};
1 change: 0 additions & 1 deletion crates/katana/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ katana-tasks.workspace = true

anyhow.workspace = true
async-trait = { workspace = true, optional = true }
cairo-vm.workspace = true
derive_more.workspace = true
dojo-metrics.workspace = true
futures.workspace = true
Expand Down
23 changes: 0 additions & 23 deletions crates/katana/core/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
use std::collections::HashMap;

use cairo_vm::vm::runners::builtin_runner::{
BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME,
OUTPUT_BUILTIN_NAME, POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME,
SEGMENT_ARENA_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME,
};

#[derive(Debug, Default)]
pub struct BlockContextGenerator {
pub block_timestamp_offset: i64,
pub next_block_start_time: u64,
}

pub fn get_default_vm_resource_fee_cost() -> HashMap<String, f64> {
HashMap::from([
(String::from("n_steps"), 1_f64),
(HASH_BUILTIN_NAME.to_string(), 1_f64),
(RANGE_CHECK_BUILTIN_NAME.to_string(), 1_f64),
(SIGNATURE_BUILTIN_NAME.to_string(), 1_f64),
(BITWISE_BUILTIN_NAME.to_string(), 1_f64),
(POSEIDON_BUILTIN_NAME.to_string(), 1_f64),
(OUTPUT_BUILTIN_NAME.to_string(), 1_f64),
(EC_OP_BUILTIN_NAME.to_string(), 1_f64),
(KECCAK_BUILTIN_NAME.to_string(), 1_f64),
(SEGMENT_ARENA_BUILTIN_NAME.to_string(), 1_f64),
])
}
4 changes: 1 addition & 3 deletions crates/katana/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ version.workspace = true
katana-primitives.workspace = true
katana-provider.workspace = true

convert_case.workspace = true
parking_lot.workspace = true
starknet = { workspace = true, optional = true }
thiserror.workspace = true
tracing.workspace = true

# blockifier deps
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "57c11586", features = [ "testing" ], optional = true }
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "24029ba4", features = [ "testing" ], optional = true }
kariy marked this conversation as resolved.
Show resolved Hide resolved
katana-cairo = { workspace = true, optional = true }

# Disable SIR for now until they support Cairo 2.6.3
Expand Down
7 changes: 2 additions & 5 deletions crates/katana/executor/benches/execution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use blockifier::state::cached_state::{CachedState, GlobalContractCache};
use blockifier::state::cached_state::CachedState;
use criterion::measurement::WallTime;
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkGroup, Criterion};
use katana_executor::{SimulationFlag, StateProviderDb};
Expand Down Expand Up @@ -45,10 +45,7 @@ fn blockifier(
|| {
// setup state
let state = provider.latest().expect("failed to get latest state");

// setup blockifier cached state
let contract_cache = GlobalContractCache::new(100);
let state = CachedState::new(StateProviderDb::from(state), contract_cache);
let state = CachedState::new(StateProviderDb::from(state));

(state, &block_context, execution_flags, tx.clone())
},
Expand Down
23 changes: 0 additions & 23 deletions crates/katana/executor/benches/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
use std::collections::HashMap;

use katana_cairo::cairo_vm::vm::runners::builtin_runner::{
BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME, KECCAK_BUILTIN_NAME,
OUTPUT_BUILTIN_NAME, POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME,
SEGMENT_ARENA_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME,
};
use katana_primitives::block::GasPrices;
use katana_primitives::env::{BlockEnv, CfgEnv, FeeTokenAddressses};
use katana_primitives::genesis::constant::DEFAULT_FEE_TOKEN_ADDRESS;
Expand Down Expand Up @@ -40,7 +33,6 @@ pub fn envs() -> (BlockEnv, CfgEnv) {
max_recursion_depth: 100,
validate_max_n_steps: 4_000_000,
invoke_tx_max_n_steps: 4_000_000,
vm_resource_fee_cost: vm_resource_fee_cost(),
fee_token_addresses: FeeTokenAddressses {
eth: DEFAULT_FEE_TOKEN_ADDRESS,
strk: DEFAULT_FEE_TOKEN_ADDRESS,
Expand All @@ -50,18 +42,3 @@ pub fn envs() -> (BlockEnv, CfgEnv) {

(block, cfg)
}

fn vm_resource_fee_cost() -> HashMap<String, f64> {
HashMap::from([
(String::from("n_steps"), 1_f64),
(HASH_BUILTIN_NAME.to_string(), 1_f64),
(RANGE_CHECK_BUILTIN_NAME.to_string(), 1_f64),
(SIGNATURE_BUILTIN_NAME.to_string(), 1_f64),
(BITWISE_BUILTIN_NAME.to_string(), 1_f64),
(POSEIDON_BUILTIN_NAME.to_string(), 1_f64),
(OUTPUT_BUILTIN_NAME.to_string(), 1_f64),
(EC_OP_BUILTIN_NAME.to_string(), 1_f64),
(KECCAK_BUILTIN_NAME.to_string(), 1_f64),
(SEGMENT_ARENA_BUILTIN_NAME.to_string(), 1_f64),
])
}
8 changes: 4 additions & 4 deletions crates/katana/executor/src/abstraction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub enum ExecutorError {}
/// Errors that can occur during the transaction execution.
#[derive(Debug, Clone, thiserror::Error)]
pub enum ExecutionError {
#[error("contract constructor execution error: {0}")]
ConstructorExecutionFailed(Box<ExecutionError>),
#[error("contract constructor execution error: {reason}")]
ConstructorExecutionFailed { reason: String },

#[error("class with hash {0:#x} is already declared")]
ClassAlreadyDeclared(ClassHash),
Expand Down Expand Up @@ -51,8 +51,8 @@ pub enum ExecutionError {
#[error("entry point execution error: {reason}")]
ExecutionFailed { reason: String },

#[error("transaction validation error: {0}")]
TransactionValidationFailed(Box<ExecutionError>),
#[error("transaction validation error: {reason}")]
TransactionValidationFailed { reason: String },

#[error("transaction reverted: {revert_error}")]
TransactionReverted { revert_error: String },
Expand Down
23 changes: 9 additions & 14 deletions crates/katana/executor/src/implementation/blockifier/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ use blockifier::transaction::errors::{
TransactionExecutionError, TransactionFeeError, TransactionPreValidationError,
};

use crate::implementation::blockifier::utils::{to_address, to_felt};
use crate::implementation::blockifier::utils::to_address;
use crate::ExecutionError;

impl From<TransactionExecutionError> for ExecutionError {
fn from(error: TransactionExecutionError) -> Self {
match error {
TransactionExecutionError::DeclareTransactionError { class_hash } => {
Self::ClassAlreadyDeclared(to_felt(class_hash.0))
Self::ClassAlreadyDeclared(class_hash.0)
}
TransactionExecutionError::ValidateTransactionError(e) => {
Self::TransactionValidationFailed(Box::new(Self::from(e)))
TransactionExecutionError::ValidateTransactionError { error, .. } => {
Self::TransactionValidationFailed { reason: error.to_string() }
}
TransactionExecutionError::StateError(e) => Self::from(e),
TransactionExecutionError::TransactionPreValidationError(e) => Self::from(e),
TransactionExecutionError::TransactionFeeError(e) => Self::from(e),
TransactionExecutionError::ExecutionError(e) => Self::from(e),
TransactionExecutionError::ExecutionError { error, .. } => Self::from(error),
TransactionExecutionError::ContractConstructorExecutionFailed(e) => {
Self::ConstructorExecutionFailed(Box::new(Self::from(e)))
Self::ConstructorExecutionFailed { reason: e.to_string() }
}
e => Self::Other(e.to_string()),
}
Expand All @@ -49,9 +49,7 @@ impl From<EntryPointExecutionError> for ExecutionError {
impl From<PreExecutionError> for ExecutionError {
fn from(error: PreExecutionError) -> Self {
match error {
PreExecutionError::EntryPointNotFound(selector) => {
Self::EntryPointNotFound(to_felt(selector.0))
}
PreExecutionError::EntryPointNotFound(selector) => Self::EntryPointNotFound(selector.0),
PreExecutionError::UninitializedStorageAddress(address) => {
Self::ContractNotDeployed(to_address(address))
}
Expand All @@ -68,10 +66,7 @@ impl From<TransactionPreValidationError> for ExecutionError {
account_nonce,
incoming_tx_nonce,
..
} => Self::InvalidNonce {
actual: to_felt(incoming_tx_nonce.0),
expected: to_felt(account_nonce.0),
},
} => Self::InvalidNonce { actual: incoming_tx_nonce.0, expected: account_nonce.0 },
TransactionPreValidationError::TransactionFeeError(e) => Self::from(e),
TransactionPreValidationError::StateError(e) => Self::from(e),
}
Expand All @@ -96,7 +91,7 @@ impl From<TransactionFeeError> for ExecutionError {
impl From<StateError> for ExecutionError {
fn from(error: StateError) -> Self {
match error {
StateError::UndeclaredClassHash(hash) => Self::UndeclaredClass(to_felt(hash.0)),
StateError::UndeclaredClassHash(hash) => Self::UndeclaredClass(hash.0),
e => Self::Other(e.to_string()),
}
}
Expand Down
30 changes: 11 additions & 19 deletions crates/katana/executor/src/implementation/blockifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub mod utils;

use std::num::NonZeroU128;

use blockifier::block::{BlockInfo, GasPrices};
use blockifier::blockifier::block::{BlockInfo, GasPrices};
use blockifier::context::BlockContext;
use blockifier::state::cached_state::{self, GlobalContractCache, MutRefState};
use blockifier::state::cached_state::{self, MutRefState};
use blockifier::state::state_api::StateReader;
use katana_cairo::starknet_api::block::{BlockNumber, BlockTimestamp};
use katana_primitives::block::{ExecutableBlock, GasPrices as KatanaGasPrices, PartialHeader};
Expand All @@ -26,12 +26,6 @@ use crate::{

pub(crate) const LOG_TARGET: &str = "katana::executor::blockifier";

// TODO: @kariy Which value should be considered here? I took the default
// value from the previous implementation.
// Previous: https://github.com/dojoengine/blockifier/blob/7459891173b64b148a7ce870c0b1d5907af15b8d/crates/blockifier/src/state/cached_state.rs#L731
// New code: https://github.com/starkware-libs/blockifier/blob/a6200402ab635d8a8e175f7f135be5914c960007/crates/blockifier/src/state/global_cache.rs#L17C11-L17C46
pub(crate) const CACHE_SIZE: usize = 100;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, is this CACHE_SIZE something that we want to customize at some point, or we shouldn't care more about it?

Copy link
Member Author

@kariy kariy Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure but they have removed the GlobalContractCache when creating a new CachedState which what we used CACHE_SIZE before.

this was before:

https://github.com/dojoengine/blockifier/blob/57c115864b5d2e9876efe289bd3dfbf05744a76b/crates/blockifier/src/state/cached_state.rs#L40


#[derive(Debug)]
pub struct BlockifierFactory {
cfg: CfgEnv,
Expand Down Expand Up @@ -111,8 +105,8 @@ impl<'a> StarknetVMProcessor<'a> {
// TODO: @kariy, not sure here if we should add some functions to alter it
// instead of cloning. Or did I miss a function?
// https://github.com/starkware-libs/blockifier/blob/a6200402ab635d8a8e175f7f135be5914c960007/crates/blockifier/src/context.rs#L23
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let versioned_constants = self.block_context.versioned_constants();
let chain_info = self.block_context.chain_info();
let versioned_constants = self.block_context.versioned_constants().clone();
let chain_info = self.block_context.chain_info().clone();
let block_info = BlockInfo {
block_number: number,
block_timestamp: timestamp,
Expand All @@ -127,7 +121,7 @@ impl<'a> StarknetVMProcessor<'a> {
};

self.block_context =
BlockContext::new_unchecked(&block_info, chain_info, versioned_constants);
BlockContext::new(block_info, chain_info, versioned_constants, Default::default());
}

fn simulate_with<F, T>(
Expand All @@ -140,11 +134,8 @@ impl<'a> StarknetVMProcessor<'a> {
F: FnMut(&mut dyn StateReader, (TxWithHash, ExecutionResult)) -> T,
{
let block_context = &self.block_context;
let state = &mut self.state.0.write().inner;
let mut state = cached_state::CachedState::new(
MutRefState::new(state),
GlobalContractCache::new(CACHE_SIZE),
);
let state = &mut self.state.0.lock().inner;
let mut state = cached_state::CachedState::new(MutRefState::new(state));

let mut results = Vec::with_capacity(transactions.len());
for exec_tx in transactions {
Expand All @@ -170,7 +161,7 @@ impl<'a> BlockExecutor<'a> for StarknetVMProcessor<'a> {
) -> ExecutorResult<()> {
let block_context = &self.block_context;
let flags = &self.simulation_flags;
let mut state = self.state.write();
let mut state = self.state.0.lock();

for exec_tx in transactions {
// Collect class artifacts if its a declare tx
Expand All @@ -187,7 +178,8 @@ impl<'a> BlockExecutor<'a> for StarknetVMProcessor<'a> {
match &res {
ExecutionResult::Success { receipt, trace } => {
self.stats.l1_gas_used += receipt.fee().gas_consumed;
self.stats.cairo_steps_used += receipt.resources_used().steps as u128;
self.stats.cairo_steps_used +=
receipt.resources_used().vm_resources.n_steps as u128;

if let Some(reason) = receipt.revert_reason() {
info!(target: LOG_TARGET, %reason, "Transaction reverted.");
Expand Down Expand Up @@ -280,7 +272,7 @@ impl ExecutorExt for StarknetVMProcessor<'_> {

fn call(&self, call: EntryPointCall) -> Result<Vec<FieldElement>, ExecutionError> {
let block_context = &self.block_context;
let mut state = self.state.0.write();
let mut state = self.state.0.lock();
let state = MutRefState::new(&mut state.inner);
let retdata = utils::call(call, state, block_context, 1_000_000_000)?;
Ok(retdata)
Expand Down
Loading
Loading