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

Update blockifier mainnet #1163

Closed
wants to merge 8 commits into from
Closed
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
268 changes: 136 additions & 132 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ cairo-lang-runner = "2.3.1"
cairo-lang-sierra = "2.3.1"
cairo-lang-starknet = "2.3.1"
cairo-lang-utils = "2.3.1"
cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] }
cairo-vm = { version = "0.8.2", features = ["cairo-1-hints"] }
num-traits = "0.2.15"
starknet = "0.5.0"
starknet_api = "0.4.1"
starknet_api = { git = "https://github.com/starkware-libs/starknet-api", rev = "8f620bc" }
thiserror = "1.0.32"

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions rpc_state_reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ starknet_in_rust = []
cairo-native = ["starknet_in_rust/cairo-native"]

[dependencies]
cairo-vm = { workspace = true }
ureq = { version = "2.7.1", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = [
"arbitrary_precision",
"raw_value",
] }
starknet_api = "0.4.1"
starknet_api = { workspace = true }
cairo-lang-starknet = { workspace = true }
cairo-lang-utils = { workspace = true }
starknet = { workspace = true }
thiserror = { workspace = true }
flate2 = "1.0.25"
serde_with = "3.0.0"
dotenv = "0.15.0"
cairo-vm = { workspace = true }
blockifier = "=0.2.0-rc0"
blockifier = { git = "https://github.com/starkware-libs/blockifier", rev = "256e2a6bb600963c26c43bf64c53f5b91985a290" }
starknet_in_rust = { path = "../", version = "0.4.0" }

[dev-dependencies]
Expand Down
7 changes: 6 additions & 1 deletion rpc_state_reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub use sir_state_reader::{
mod tests {
use pretty_assertions_sorted::{assert_eq, assert_eq_sorted};
use starknet_api::{
class_hash,
core::{ClassHash, ContractAddress, PatriciaKey},
hash::{StarkFelt, StarkHash},
patricia_key, stark_felt,
Expand All @@ -38,6 +37,12 @@ mod tests {
};
}

macro_rules! class_hash {
($s:expr) => {
ClassHash(stark_felt!($s))
};
}

#[test]
fn test_get_contract_class_cairo1() {
let rpc_state = RpcState::new_rpc(RpcChain::MainNet, BlockTag::Latest.into()).unwrap();
Expand Down
27 changes: 21 additions & 6 deletions rpc_state_reader/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
borrow::BorrowMut,
collections::HashMap,
io::{self, Read},
};
Expand All @@ -8,7 +9,7 @@ use cairo_lang_utils::bigint::BigUintAsHex;
use serde::Deserialize;
use starknet::core::types::{LegacyContractEntryPoint, LegacyEntryPointsByType};
use starknet_api::{
core::EntryPointSelector,
core::{calculate_contract_address, ContractAddress, EntryPointSelector},
deprecated_contract_class::{EntryPoint, EntryPointOffset, EntryPointType},
hash::{StarkFelt, StarkHash},
transaction::{DeclareTransaction, InvokeTransaction, Transaction},
Expand Down Expand Up @@ -65,11 +66,10 @@ pub fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {

/// Freestanding deserialize method to avoid a new type.
pub fn deserialize_transaction_json(
transaction: serde_json::Value,
mut transaction: serde_json::Value,
) -> serde_json::Result<Transaction> {
let tx_type: String = serde_json::from_value(transaction["type"].clone())?;
let tx_version: String = serde_json::from_value(transaction["version"].clone())?;

match tx_type.as_str() {
"INVOKE" => match tx_version.as_str() {
"0x0" => Ok(Transaction::Invoke(InvokeTransaction::V0(
Expand All @@ -82,9 +82,24 @@ pub fn deserialize_transaction_json(
"unimplemented invoke version: {x}"
))),
},
"DEPLOY_ACCOUNT" => Ok(Transaction::DeployAccount(serde_json::from_value(
transaction,
)?)),
"DEPLOY_ACCOUNT" => {
let salt = serde_json::from_value(transaction["contract_address_salt"].clone())?;
let class_hash = serde_json::from_value(transaction["class_hash"].clone())?;
let constructor_calldata =
&serde_json::from_value(transaction["constructor_calldata"].clone())?;
let contract_address = calculate_contract_address(
salt,
class_hash,
constructor_calldata,
ContractAddress::default(),
)
.unwrap();
let contract_address = serde_json::to_value(contract_address)?;
transaction.borrow_mut()["contract_address"] = contract_address;
Ok(Transaction::DeployAccount(serde_json::from_value(
transaction,
)?))
}
"DECLARE" => match tx_version.as_str() {
"0x0" => Ok(Transaction::Declare(DeclareTransaction::V0(
serde_json::from_value(transaction)?,
Expand Down
64 changes: 20 additions & 44 deletions rpc_state_reader/tests/blockifier_tests.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
use blockifier::execution::contract_class::{
ContractClass as BlockifierContractClass, ContractClassV0, ContractClassV0Inner,
};
use blockifier::{
block_context::BlockContext,
execution::{contract_class::ContractClass, entry_point::CallInfo},
state::{
cached_state::{CachedState, GlobalContractCache},
cached_state::CachedState,
errors::StateError,
state_api::{StateReader, StateResult},
},
transaction::{
account_transaction::AccountTransaction,
objects::TransactionExecutionInfo,
transactions::{
DeclareTransaction, DeployAccountTransaction, ExecutableTransaction,
L1HandlerTransaction,
},
transactions::{DeclareTransaction, ExecutableTransaction, L1HandlerTransaction},
},
};
use blockifier::{
execution::contract_class::{
ContractClass as BlockifierContractClass, ContractClassV0, ContractClassV0Inner,
},
transaction::transactions::InvokeTransaction,
};
use cairo_lang_starknet::{
casm_contract_class::CasmContractClass, contract_class::ContractClass as SierraContractClass,
};
Expand All @@ -29,15 +23,13 @@ use pretty_assertions_sorted::{assert_eq, assert_eq_sorted};
use rpc_state_reader::rpc_state::*;
use rpc_state_reader::utils;
use starknet::core::types::ContractClass as SNContractClass;
use starknet_api::core::PatriciaKey;
use starknet_api::hash::StarkHash;
use starknet_api::{
block::BlockNumber,
contract_address,
core::{
calculate_contract_address, ClassHash, CompiledClassHash, ContractAddress, Nonce,
PatriciaKey,
},
hash::{StarkFelt, StarkHash},
patricia_key, stark_felt,
core::{ClassHash, CompiledClassHash, ContractAddress, Nonce},
hash::StarkFelt,
stark_felt,
state::StorageKey,
transaction::{Transaction as SNTransaction, TransactionHash},
};
Expand Down Expand Up @@ -143,11 +135,12 @@ pub fn execute_tx(
let receipt = rpc_reader.0.get_transaction_receipt(&tx_hash).unwrap();

// Create state from RPC reader
let global_cache = GlobalContractCache::default();
let mut state = CachedState::new(rpc_reader, global_cache);
let mut state = CachedState::new(rpc_reader);

let fee_token_address =
contract_address!("049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7");
let stark_hash =
StarkHash::try_from("049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
.unwrap();
let fee_token_address = ContractAddress(PatriciaKey::try_from(stark_hash).unwrap());

const N_STEPS_FEE_WEIGHT: f64 = 0.01;
let vm_resource_fee_cost = Arc::new(HashMap::from([
Expand Down Expand Up @@ -181,24 +174,8 @@ pub fn execute_tx(

// Map starknet_api transaction to blockifier's
let blockifier_tx = match sn_api_tx.unwrap() {
SNTransaction::Invoke(tx) => {
let invoke = InvokeTransaction { tx, tx_hash };
AccountTransaction::Invoke(invoke)
}
SNTransaction::DeployAccount(tx) => {
let contract_address = calculate_contract_address(
tx.contract_address_salt,
tx.class_hash,
&tx.constructor_calldata,
ContractAddress::default(),
)
.unwrap();
AccountTransaction::DeployAccount(DeployAccountTransaction {
tx,
tx_hash,
contract_address,
})
}
SNTransaction::Invoke(tx) => AccountTransaction::Invoke(tx),
SNTransaction::DeployAccount(tx) => AccountTransaction::DeployAccount(tx),
SNTransaction::Declare(tx) => {
// Fetch the contract_class from the next block (as we don't have it in the previous one)
let mut next_block_state_reader =
Expand All @@ -207,19 +184,18 @@ pub fn execute_tx(
.get_compiled_contract_class(&tx.class_hash())
.unwrap();

let declare = DeclareTransaction::new(tx, tx_hash, contract_class).unwrap();
let declare = DeclareTransaction::new(tx, contract_class).unwrap();
AccountTransaction::Declare(declare)
}
SNTransaction::L1Handler(tx) => {
// As L1Hanlder is not an account transaction we execute it here and return the result
let blockifier_tx = L1HandlerTransaction {
tx,
tx_hash,
paid_fee_on_l1: starknet_api::transaction::Fee(u128::MAX),
};
return (
blockifier_tx
.execute(&mut state, &block_context, true, true)
.execute(&mut state, &block_context, true)
.unwrap(),
trace,
receipt,
Expand All @@ -230,7 +206,7 @@ pub fn execute_tx(

(
blockifier_tx
.execute(&mut state, &block_context, true, true)
.execute(&mut state, &block_context, true)
.unwrap(),
trace,
receipt,
Expand Down
50 changes: 28 additions & 22 deletions src/services/api/contract_classes/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ mod tests {
felt::{felt_str, PRIME_STR},
serde::deserialize_program::BuiltinName,
};
use starknet_api::deprecated_contract_class::{FunctionAbiEntry, TypedParameter};
use starknet_api::deprecated_contract_class::{
FunctionAbiEntry, FunctionAbiEntryType, FunctionAbiEntryWithType, TypedParameter,
};

#[test]
fn deserialize_contract_class() {
Expand Down Expand Up @@ -331,28 +333,32 @@ mod tests {
// This specific contract compiles with --no_debug_info
let res = ContractClass::from_path("starknet_programs/fibonacci.json");
let contract_class = res.expect("should be able to read file");
let expected_abi = Some(vec![ContractClassAbiEntry::Function(FunctionAbiEntry {
name: "fib".to_string(),
inputs: vec![
TypedParameter {
name: "first_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "second_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "n".to_string(),
r#type: "felt".to_string(),
let expected_abi = Some(vec![ContractClassAbiEntry::Function(
FunctionAbiEntryWithType {
entry: FunctionAbiEntry {
name: "fib".to_string(),
inputs: vec![
TypedParameter {
name: "first_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "second_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "n".to_string(),
r#type: "felt".to_string(),
},
],
outputs: vec![TypedParameter {
name: "res".to_string(),
r#type: "felt".to_string(),
}],
},
],
outputs: vec![TypedParameter {
name: "res".to_string(),
r#type: "felt".to_string(),
}],
state_mutability: None,
})]);
r#type: FunctionAbiEntryType::Function,
},
)]);
assert_eq!(contract_class.abi, expected_abi);
}

Expand Down
23 changes: 10 additions & 13 deletions src/transaction/invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,7 @@ fn convert_invoke_v0(
value: starknet_api::transaction::InvokeTransactionV0,
chain_id: StarknetChainId,
) -> Result<InvokeFunction, TransactionError> {
let contract_address = Address(Felt252::from_bytes_be(
value.contract_address.0.key().bytes(),
));
let contract_address = Address(Felt252::from_bytes_be(value.sender_address.0.key().bytes()));
let max_fee = value.max_fee.0;
let entry_point_selector = Felt252::from_bytes_be(value.entry_point_selector.0.bytes());
let nonce = None;
Expand Down Expand Up @@ -685,24 +683,23 @@ mod tests {
use starknet_api::{
core::{ContractAddress, Nonce, PatriciaKey},
hash::{StarkFelt, StarkHash},
transaction::{Fee, InvokeTransaction, InvokeTransactionV1, TransactionSignature},
transaction::{
Fee, InvokeTransaction, InvokeTransactionV1, TransactionHash, TransactionSignature,
},
};
use std::sync::Arc;

#[test]
fn test_from_invoke_transaction() {
// https://starkscan.co/tx/0x05b6cf416d56e7c7c519b44e6d06a41657ff6c6a3f2629044fac395e6d200ac4
// result 0x05b6cf416d56e7c7c519b44e6d06a41657ff6c6a3f2629044fac395e6d200ac4
let tx_hash = StarkHash::try_from(
"0x00c4658311841a69ce121543af332622bc243cf5593fc4aaf822481c7b7f183d",
)
.unwrap();
let tx = InvokeTransaction::V1(InvokeTransactionV1 {
sender_address: ContractAddress(
PatriciaKey::try_from(
StarkHash::try_from(
"0x00c4658311841a69ce121543af332622bc243cf5593fc4aaf822481c7b7f183d",
)
.unwrap(),
)
.unwrap(),
),
sender_address: ContractAddress(PatriciaKey::try_from(tx_hash).unwrap()),
transaction_hash: TransactionHash(tx_hash),
max_fee: Fee(49000000000000),
signature: TransactionSignature(vec![
StarkFelt::try_from(
Expand Down