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

Update blockfier #40

Merged
merged 11 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
993 changes: 599 additions & 394 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 @@ -8,6 +8,6 @@ resolver = "2"

[workspace.dependencies]
thiserror = "1.0.32"
starknet_api = "=0.12.0-dev.1"
blockifier = { git = "https://github.com/lambdaclass/blockifier", rev = "0e47bbe66ccd8003dcd0aa1380485daa07ea95e4" }
starknet_api = "0.13.0-rc.0"
blockifier = { git = "https://github.com/lambdaclass/blockifier", branch= "native2.7.x-adapt" }
tracing = "0.1"
9 changes: 4 additions & 5 deletions replay/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rpc_state_reader::{
};
use starknet_api::{
block::BlockNumber,
hash::StarkFelt,
hash::StarkHash,
transaction::{Transaction as SNTransaction, TransactionHash},
};
use tracing::{error, info, info_span};
Expand Down Expand Up @@ -50,9 +50,8 @@ pub fn fetch_block_range_data(
.unwrap()
.into_iter()
.map(|transaction_hash| {
let transaction_hash = TransactionHash(
StarkFelt::try_from(transaction_hash.strip_prefix("0x").unwrap()).unwrap(),
);
let transaction_hash =
TransactionHash(StarkHash::from_hex(&transaction_hash).unwrap());

// Fetch transaction
let transaction = rpc_state.get_transaction(&transaction_hash).unwrap();
Expand Down Expand Up @@ -151,7 +150,7 @@ impl<S: StateReader> StateReader for OptionalStateReader<S> {
&self,
contract_address: starknet_api::core::ContractAddress,
key: starknet_api::state::StorageKey,
) -> blockifier::state::state_api::StateResult<StarkFelt> {
) -> blockifier::state::state_api::StateResult<StarkHash> {
self.get_inner().get_storage_at(contract_address, key)
}

Expand Down
2 changes: 1 addition & 1 deletion replay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn show_execution_data(
);
}

let execution_gas = execution_info.actual_fee;
let execution_gas = execution_info.transaction_receipt.fee;
let rpc_gas = rpc_receipt.actual_fee;
debug!(?execution_gas, ?rpc_gas, "execution actual fee");
}
Expand Down
10 changes: 6 additions & 4 deletions rpc-state-reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ serde_json = { version = "1.0", features = [
"raw_value",
] }
starknet_api = {workspace = true}
cairo-lang-starknet = "=2.6.4"
cairo-lang-starknet-classes = "=2.6.4"
cairo-lang-utils = "=2.6.4"
cairo-lang-starknet = "=2.7.1"
cairo-lang-sierra = "=2.7.1"
cairo-lang-starknet-classes = "=2.7.1"
cairo-lang-utils = "=2.7.1"
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", branch = "cairo-lang2.7.0-rc.3" }
starknet = "0.7.0"
thiserror = { workspace = true }
flate2 = "1.0.25"
dotenv = "0.15.0"
cairo-vm = "0.9.2"
cairo-vm = "1.0.0-rc5"
blockifier = { workspace = true }
tracing = { workspace = true }

Expand Down
92 changes: 63 additions & 29 deletions rpc-state-reader/src/blockifier_state_reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use blockifier::{
blockifier::block::BlockInfo,
bouncer::BouncerConfig,
context::{BlockContext, ChainInfo, FeeTokenAddresses},
execution::contract_class::{ClassInfo, ContractClass, ContractClassV0, ContractClassV0Inner},
execution::contract_class::{
ClassInfo, ContractClass, ContractClassV0, ContractClassV0Inner, NativeContractClassV1,
},
state::{
cached_state::CachedState,
errors::StateError,
Expand All @@ -17,21 +20,21 @@ use blockifier::{
},
versioned_constants::VersionedConstants,
};

use cairo_vm::types::program::Program;
use starknet::core::types::ContractClass as SNContractClass;
use starknet_api::{
block::BlockNumber,
core::{calculate_contract_address, ClassHash, CompiledClassHash, ContractAddress, Nonce},
hash::StarkFelt,
stark_felt,
hash::StarkHash,
state::StorageKey,
transaction::{Transaction as SNTransaction, TransactionHash},
};
use std::sync::Arc;

use crate::{
rpc_state::{RpcBlockInfo, RpcChain, RpcState, RpcTransactionReceipt, TransactionTrace},
utils,
utils::{self, get_native_executor},
};

pub struct RpcStateReader(pub RpcState);
Expand All @@ -47,7 +50,7 @@ impl StateReader for RpcStateReader {
&self,
contract_address: starknet_api::core::ContractAddress,
key: StorageKey,
) -> StateResult<StarkFelt> {
) -> StateResult<StarkHash> {
Ok(self.0.get_storage_at(&contract_address, &key))
}

Expand Down Expand Up @@ -95,7 +98,12 @@ impl StateReader for RpcStateReader {
cairo_lang_starknet_classes::casm_contract_class::CasmContractClass::from_contract_class(sierra_cc, false, usize::MAX).unwrap();
ContractClass::V1(casm_cc.try_into().unwrap())
} else {
ContractClass::V1Sierra(sierra_cc.try_into().unwrap())
let program = sierra_cc.extract_sierra_program().unwrap();
let executor = get_native_executor(program, class_hash);

ContractClass::V1Native(
NativeContractClassV1::new(executor, sierra_cc).unwrap(),
)
}
}
None => {
Expand Down Expand Up @@ -141,7 +149,7 @@ pub fn execute_tx(
} = rpc_reader.0.get_block_info().unwrap();

// Get transaction before giving ownership of the reader
let tx_hash = TransactionHash(stark_felt!(tx_hash));
let tx_hash = TransactionHash(StarkHash::from_hex(tx_hash).unwrap());
let sn_api_tx = rpc_reader.0.get_transaction(&tx_hash);

let trace = rpc_reader.0.get_transaction_trace(&tx_hash).unwrap();
Expand Down Expand Up @@ -182,12 +190,16 @@ pub fn execute_tx(
chain_id,
fee_token_addresses: FeeTokenAddresses::default(),
};
let mut versioned_constants =
VersionedConstants::latest_constants_with_overrides(u32::MAX, usize::MAX);
versioned_constants.disable_cairo0_redeclaration = false;

// TODO: Check BlockContext::new_unchecked
let block_context = BlockContext::new_unchecked(
&block_info,
&chain_info,
&VersionedConstants::latest_constants_with_overrides(u32::MAX, usize::MAX),
let block_context = BlockContext::new(
block_info,
chain_info,
versioned_constants,
BouncerConfig::empty(),
);
// let block_context = BlockContext {
// chain_id,
Expand Down Expand Up @@ -281,7 +293,7 @@ fn calculate_class_info_for_testing(contract_class: ContractClass) -> ClassInfo
let sierra_program_length = match contract_class {
ContractClass::V0(_) => 0,
ContractClass::V1(_) => 100,
ContractClass::V1Sierra(_) => 100,
ContractClass::V1Native(_) => 100,
};
ClassInfo::new(&contract_class, sierra_program_length, 100).unwrap()
}
Expand All @@ -305,14 +317,18 @@ pub fn execute_tx_configurable_with_state(
let chain_id = state.state.0.get_chain_name();

let chain_info = ChainInfo {
chain_id,
chain_id: chain_id.clone(),
fee_token_addresses: FeeTokenAddresses::default(),
};
let mut versioned_constants =
VersionedConstants::latest_constants_with_overrides(u32::MAX, usize::MAX);
versioned_constants.disable_cairo0_redeclaration = false;

let block_context = BlockContext::new_unchecked(
&block_info,
&chain_info,
&VersionedConstants::latest_constants_with_overrides(u32::MAX, usize::MAX),
let block_context = BlockContext::new(
block_info,
chain_info,
versioned_constants,
BouncerConfig::empty(),
);

// Get transaction before giving ownership of the reader
Expand Down Expand Up @@ -341,11 +357,15 @@ pub fn execute_tx_configurable_with_state(
})
}
SNTransaction::Declare(tx) => {
let contract_class = state
.state
let block_number = block_context.block_info().block_number;
let network = parse_to_rpc_chain(&chain_id.to_string());
// we need to retrieve the next block in order to get the contract_class
let next_block_state_reader = RpcStateReader(
RpcState::new_rpc(network, (block_number.next()).unwrap().into()).unwrap(),
);
let contract_class = next_block_state_reader
.get_compiled_contract_class(tx.class_hash())
.unwrap();

let class_info = calculate_class_info_for_testing(contract_class);

let declare = DeclareTransaction::new(tx, *tx_hash, class_info).unwrap();
Expand Down Expand Up @@ -380,8 +400,7 @@ pub fn execute_tx_configurable(
TransactionTrace,
RpcTransactionReceipt,
)> {
let tx_hash =
TransactionHash(StarkFelt::try_from(tx_hash.strip_prefix("0x").unwrap()).unwrap());
let tx_hash = TransactionHash(StarkHash::from_hex(tx_hash).unwrap());
let tx = state.state.0.get_transaction(&tx_hash).unwrap();
let gas_price = state.state.0.get_gas_price(block_number.0).unwrap();
let RpcBlockInfo {
Expand Down Expand Up @@ -473,23 +492,36 @@ pub fn execute_tx_with_blockifier(
account_transaction.execute(state, &context, false, true)
}

fn parse_to_rpc_chain(network: &str) -> RpcChain {
match network {
"alpha-mainnet" => RpcChain::MainNet,
"alpha4" => RpcChain::TestNet,
"alpha4-2" => RpcChain::TestNet2,
_ => panic!("Invalid network name {}", network),
}
}

pub fn fetch_block_context(state: &RpcState, block_number: BlockNumber) -> BlockContext {
let rpc_block_info = state.get_block_info().unwrap();
let gas_price = state.get_gas_price(block_number.0).unwrap();
let mut versioned_constants =
VersionedConstants::latest_constants_with_overrides(u32::MAX, usize::MAX);
versioned_constants.disable_cairo0_redeclaration = false;

BlockContext::new_unchecked(
&BlockInfo {
BlockContext::new(
BlockInfo {
block_number,
block_timestamp: rpc_block_info.block_timestamp,
sequencer_address: rpc_block_info.sequencer_address,
gas_prices: gas_price,
use_kzg_da: false,
},
&ChainInfo {
ChainInfo {
chain_id: state.get_chain_name(),
fee_token_addresses: Default::default(),
},
&VersionedConstants::latest_constants_with_overrides(u32::MAX, usize::MAX),
versioned_constants,
BouncerConfig::empty(),
)
}

Expand Down Expand Up @@ -553,7 +585,7 @@ mod tests {
=> ignore
)]
#[test_case(
// Declare tx
// Declare tx (fails with "already declared")
"0x1088aa18785779e1e8eef406dc495654ad42a9729b57969ad0dbf2189c40bee",
271888,
RpcChain::MainNet
Expand Down Expand Up @@ -884,6 +916,7 @@ mod tests {
false
)]
#[test_case(
// fails with "already declared (is the same declare tx)"
"0x026c17728b9cd08a061b1f17f08034eb70df58c1a96421e73ee6738ad258a94c",
169929,
RpcChain::MainNet,
Expand Down Expand Up @@ -1102,7 +1135,8 @@ mod tests {
) {
let previous_block = BlockNumber(block_number - 1);
let (tx_info, _, _) = execute_tx(hash, chain, previous_block);
let starknet_resources = tx_info.actual_resources.starknet_resources;
let tx_receipt = tx_info.transaction_receipt;
let starknet_resources = tx_receipt.resources.starknet_resources;
let callinfo_iter = match tx_info.execute_call_info {
Some(c) => vec![c],
None => vec![CallInfo::default()], // there's no call info, so we take the default value to have all of it's atributes set to 0
Expand All @@ -1117,7 +1151,7 @@ mod tests {
);

assert_eq!(is_reverted, tx_info.revert_error.is_some());
assert_eq!(da_gas, tx_info.da_gas);
assert_eq!(da_gas, tx_receipt.da_gas);
assert_eq!(starknet_rsc, starknet_resources);
}

Expand Down
Loading