Skip to content

Commit

Permalink
upgrade blockifier
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop committed Nov 15, 2023
1 parent 619d9f3 commit f0157bd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
8 changes: 4 additions & 4 deletions vm/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ edition = "2021"
[dependencies]
serde = "1.0.171"
serde_json = { version = "1.0.96", features = ["raw_value"] }
blockifier = {git = "https://github.com/starkware-libs/blockifier", rev = "v0.3.0-rc1"}
starknet_api = { git = "https://github.com/starkware-libs/starknet-api", rev = "8f620bc" }
blockifier = "0.4.0-rc6"
starknet_api = "0.6.0-rc2"
cairo-vm = "0.8.2"
cairo-lang-casm = "2.1.0"
cairo-lang-starknet = "2.1.0"
cairo-lang-casm = "2.4.0-rc2"
cairo-lang-starknet = "2.4.0-rc2"
indexmap = "1.9.2"
starknet = { rev = "starknet-core/v0.4.0", git = "https://github.com/xJonathanLEI/starknet-rs" }
cached = "0.44.0"
Expand Down
12 changes: 7 additions & 5 deletions vm/rust/src/jsonrpc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use blockifier;
use blockifier::execution::entry_point::{CallType, OrderedL2ToL1Message};
use blockifier::execution::entry_point::CallType;
use blockifier::execution::call_info::OrderedL2ToL1Message;
use cairo_vm::vm::runners::builtin_runner::{
BITWISE_BUILTIN_NAME, EC_OP_BUILTIN_NAME, HASH_BUILTIN_NAME,
POSEIDON_BUILTIN_NAME, RANGE_CHECK_BUILTIN_NAME, SIGNATURE_BUILTIN_NAME, KECCAK_BUILTIN_NAME,
Expand All @@ -9,10 +10,10 @@ use blockifier::state::cached_state::TransactionalState;
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{State, StateReader};
use serde::Serialize;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, PatriciaKey};
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, PatriciaKey, EthAddress};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::{Calldata, EthAddress, EventContent, L2ToL1Payload};
use starknet_api::transaction::{Calldata, EventContent, L2ToL1Payload};
use starknet_api::transaction::{DeclareTransaction, Transaction as StarknetApiTransaction};

use crate::juno_state_reader::JunoStateReader;
Expand Down Expand Up @@ -140,6 +141,7 @@ pub fn new_transaction_trace(
DeclareTransaction::V0(_) => Some(declare_txn.class_hash()),
DeclareTransaction::V1(_) => Some(declare_txn.class_hash()),
DeclareTransaction::V2(_) => None,
DeclareTransaction::V3(_) => None,
}
} else {
None
Expand All @@ -163,7 +165,7 @@ pub struct OrderedEvent {
pub event: EventContent,
}

type BlockifierOrderedEvent = blockifier::execution::entry_point::OrderedEvent;
use blockifier::execution::call_info::OrderedEvent as BlockifierOrderedEvent;
impl From<BlockifierOrderedEvent> for OrderedEvent {
fn from(val: BlockifierOrderedEvent) -> Self {
OrderedEvent {
Expand Down Expand Up @@ -235,7 +237,7 @@ pub struct FunctionInvocation {
pub execution_resources: ExecutionResources,
}

type BlockifierCallInfo = blockifier::execution::entry_point::CallInfo;
use blockifier::execution::call_info::CallInfo as BlockifierCallInfo;
impl From<BlockifierCallInfo> for FunctionInvocation {
fn from(val: BlockifierCallInfo) -> Self {
FunctionInvocation {
Expand Down
75 changes: 44 additions & 31 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ use std::{

use blockifier::{
abi::constants::{INITIAL_GAS_COST, N_STEPS_RESOURCE},
block_context::BlockContext,
block_context::{BlockContext, GasPrices, FeeTokenAddresses},
execution::{
contract_class::{ContractClass, ContractClassV1},
entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext, ExecutionResources},
common_hints::ExecutionMode,
},
fee::fee_utils::calculate_tx_fee,
state::cached_state::CachedState,
state::cached_state::{CachedState, GlobalContractCache},
transaction::{
objects::AccountTransactionContext, transaction_execution::Transaction,
objects::{AccountTransactionContext, DeprecatedAccountTransactionContext, FeeType},
transaction_execution::Transaction,
transactions::ExecutableTransaction,
},
};
Expand All @@ -33,7 +34,7 @@ use cairo_vm::vm::runners::builtin_runner::{
};
use juno_state_reader::{contract_class_from_json_str, felt_to_byte_array};
use serde::Deserialize;
use starknet_api::transaction::{Calldata, Transaction as StarknetApiTransaction};
use starknet_api::transaction::{Calldata, Transaction as StarknetApiTransaction, TransactionHash};
use starknet_api::{
block::{BlockNumber, BlockTimestamp},
deprecated_contract_class::EntryPointType,
Expand Down Expand Up @@ -92,22 +93,29 @@ pub extern "C" fn cairoVMCall(
initial_gas: INITIAL_GAS_COST,
};

const GAS_PRICE: u128 = 1;
let mut state = CachedState::new(reader);
const GAS_PRICES: GasPrices = GasPrices {
eth_l1_gas_price: 1,
strk_l1_gas_price: 0,
};
let mut state = CachedState::new(reader, GlobalContractCache::default());
let mut resources = ExecutionResources::default();
let mut context = EntryPointExecutionContext::new(
build_block_context(
let context = EntryPointExecutionContext::new(
&build_block_context(
chain_id_str,
block_number,
block_timestamp,
StarkFelt::default(),
GAS_PRICE,
GAS_PRICES,
),
AccountTransactionContext::default(),
4_000_000,
&AccountTransactionContext::Deprecated(DeprecatedAccountTransactionContext::default()),
ExecutionMode::Execute,
false,
);
let call_info = entry_point.execute(&mut state, &mut resources, &mut context);
if let Err(e) = context {
report_error(reader_handle, e.to_string().as_str());
return;
}
let call_info = entry_point.execute(&mut state, &mut resources, &mut context.unwrap());

match call_info {
Err(e) => report_error(reader_handle, e.to_string().as_str()),
Expand All @@ -124,6 +132,7 @@ pub extern "C" fn cairoVMCall(
#[derive(Deserialize)]
pub struct TxnAndQueryBit {
pub txn: StarknetApiTransaction,
pub txn_hash: TransactionHash,
pub query_bit: bool,
}

Expand Down Expand Up @@ -181,9 +190,12 @@ pub extern "C" fn cairoVMExecute(
block_number,
block_timestamp,
sequencer_address_felt,
felt_to_u128(gas_price_felt),
GasPrices {
eth_l1_gas_price: felt_to_u128(gas_price_felt),
strk_l1_gas_price: 0,
},
);
let mut state = CachedState::new(reader);
let mut state = CachedState::new(reader, GlobalContractCache::default());
let charge_fee = skip_charge_fee == 0;

let mut trace_buffer = Vec::with_capacity(10_000);
Expand Down Expand Up @@ -223,7 +235,7 @@ pub extern "C" fn cairoVMExecute(
_ => None,
};

let txn = transaction_from_api(txn_and_query_bit.txn.clone(), contract_class, paid_fee_on_l1, txn_and_query_bit.query_bit);
let txn = transaction_from_api(txn_and_query_bit.txn.clone(), txn_and_query_bit.txn_hash, contract_class, paid_fee_on_l1, txn_and_query_bit.query_bit);
if let Err(e) = txn {
report_error(reader_handle, e.to_string().as_str());
return;
Expand All @@ -245,7 +257,7 @@ pub extern "C" fn cairoVMExecute(
reader_handle,
format!(
"failed txn {:?} reason:{:?}",
txn_and_query_bit.txn.transaction_hash(),
txn_and_query_bit.txn_hash,
e
)
.as_str(),
Expand All @@ -255,7 +267,7 @@ pub extern "C" fn cairoVMExecute(
Ok(mut t) => {
// we are estimating fee, override actual fee calculation
if !charge_fee {
t.actual_fee = calculate_tx_fee(&t.actual_resources, &block_context).unwrap();
t.actual_fee = calculate_tx_fee(&t.actual_resources, &block_context, &FeeType::Eth).unwrap();
}

let actual_fee = t.actual_fee.0.into();
Expand Down Expand Up @@ -293,27 +305,28 @@ fn felt_to_u128(felt: StarkFelt) -> u128 {

fn transaction_from_api(
tx: StarknetApiTransaction,
tx_hash: TransactionHash,
contract_class: Option<ContractClass>,
paid_fee_on_l1: Option<Fee>,
query_bit: bool,
) -> Result<Transaction, String> {
match tx {
StarknetApiTransaction::Deploy(deploy) => {
StarknetApiTransaction::Deploy(_) => {
return Err(format!(
"Unsupported deploy transaction in the traced block (transaction_hash={})",
deploy.transaction_hash
tx_hash,
))
}
StarknetApiTransaction::Declare(declare) if contract_class.is_none() => {
StarknetApiTransaction::Declare(_) if contract_class.is_none() => {
return Err(format!(
"Declare transaction must be created with a ContractClass (transaction_hash={})",
declare.transaction_hash()
tx_hash,
))
}
_ => {} // all ok
};

Transaction::from_api(tx, contract_class, paid_fee_on_l1, query_bit)
Transaction::from_api(tx, tx_hash, contract_class, paid_fee_on_l1, None, query_bit)
.map_err(|err| format!("failed to create transaction from api: {:?}", err))
}

Expand Down Expand Up @@ -345,7 +358,7 @@ fn build_block_context(
block_number: c_ulonglong,
block_timestamp: c_ulonglong,
sequencer_address: StarkFelt,
gas_price: u128,
gas_prices: GasPrices,
) -> BlockContext {
BlockContext {
chain_id: ChainId(chain_id_str.into()),
Expand All @@ -355,14 +368,14 @@ fn build_block_context(
sequencer_address: ContractAddress::try_from(sequencer_address).unwrap(),
// https://github.com/starknet-io/starknet-addresses/blob/df19b17d2c83f11c30e65e2373e8a0c65446f17c/bridged_tokens/mainnet.json
// fee_token_address is the same for all networks
fee_token_address: ContractAddress::try_from(
StarkHash::try_from(
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
)
.unwrap(),
)
.unwrap(),
gas_price, // fixed gas price, so that we can return "consumed gas" to Go side
fee_token_addresses: FeeTokenAddresses {
eth_fee_token_address: ContractAddress::try_from(
StarkHash::try_from(
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
).unwrap()).unwrap(),
strk_fee_token_address: ContractAddress::try_from(StarkHash::try_from("0x0").unwrap()).unwrap(), // TODO what is the real STRK address?
},
gas_prices, // fixed gas price, so that we can return "consumed gas" to Go side
vm_resource_fee_cost: HashMap::from([
(N_STEPS_RESOURCE.to_string(), N_STEPS_FEE_WEIGHT),
(OUTPUT_BUILTIN_NAME.to_string(), 0.0),
Expand Down

0 comments on commit f0157bd

Please sign in to comment.