Skip to content

Commit

Permalink
build(blockifier_reexecution): convert declare starknet api to declar…
Browse files Browse the repository at this point in the history
…e blockifier
  • Loading branch information
AvivYossef-starkware committed Oct 31, 2024
1 parent 2956dc1 commit 50739a6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 26 deletions.
3 changes: 3 additions & 0 deletions crates/blockifier_reexecution/src/state_reader/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use blockifier::execution::errors::ContractClassError;
use blockifier::state::errors::StateError;
use blockifier::transaction::errors::TransactionExecutionError;
use blockifier::versioned_constants::VersionedConstantsError;
Expand All @@ -21,4 +22,6 @@ pub enum ReexecutionError {
TransactionExecutionError(#[from] TransactionExecutionError),
#[error(transparent)]
VersionedConstants(#[from] VersionedConstantsError),
#[error(transparent)]
ContractClassError(#[from] ContractClassError),
}
11 changes: 11 additions & 0 deletions crates/blockifier_reexecution/src/state_reader/rpc_https_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,14 @@ pub fn test_get_declare_tx_by_hash(
pub fn test_get_statediff_rpc(test_state_reader: TestStateReader) {
assert!(test_state_reader.get_state_diff().is_ok());
}

#[rstest]
#[case(EXAMPLE_DECLARE_V1_BLOCK_NUMBER)]
#[case(EXAMPLE_DECLARE_V2_BLOCK_NUMBER)]
#[case(EXAMPLE_DECLARE_V3_BLOCK_NUMBER)]
pub fn test_get_all_blockifier_tx_in_block(#[case] block_number: u64) {
let state_reader = TestStateReader::new_for_testing(BlockNumber(block_number));
state_reader
.from_api_txs_to_blockifier_txs(state_reader.get_all_txs_in_block().unwrap())
.unwrap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use blockifier::blockifier::config::TransactionExecutorConfig;
use blockifier::blockifier::transaction_executor::TransactionExecutor;
use blockifier::bouncer::BouncerConfig;
use blockifier::context::BlockContext;
use blockifier::execution::contract_class::ContractClass as BlockifierContractClass;
use blockifier::execution::contract_class::{ClassInfo, ContractClass as BlockifierContractClass};
use blockifier::state::cached_state::{CachedState, CommitmentStateDiff};
use blockifier::state::errors::StateError;
use blockifier::state::state_api::{StateReader, StateResult};
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
use blockifier::versioned_constants::VersionedConstants;
use papyrus_execution::DEPRECATED_CONTRACT_SIERRA_SIZE;
use serde_json::{json, to_value};
use starknet_api::block::{BlockNumber, StarknetVersion};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_core::types::ContractClass as StarknetContractClass;
use starknet_core::types::ContractClass::{Legacy, Sierra};
use starknet_gateway::config::RpcStateReaderConfig;
use starknet_gateway::errors::serde_err_to_state_err;
use starknet_gateway::rpc_objects::{BlockHeader, GetBlockWithTxHashesParams, ResourcePrice};
Expand All @@ -31,7 +31,6 @@ use crate::state_reader::serde_utils::{
};
use crate::state_reader::utils::{
disjoint_hashmap_union,
from_api_txs_to_blockifier_txs,
get_chain_info,
get_rpc_state_reader_config,
};
Expand Down Expand Up @@ -64,8 +63,8 @@ impl StateReader for TestStateReader {
class_hash: ClassHash,
) -> StateResult<BlockifierContractClass> {
match self.get_contract_class(&class_hash)? {
Sierra(sierra) => sierra_to_contact_class_v1(sierra),
Legacy(legacy) => legacy_to_contract_class_v0(legacy),
StarknetContractClass::Sierra(sierra) => sierra_to_contact_class_v1(sierra),
StarknetContractClass::Legacy(legacy) => legacy_to_contract_class_v0(legacy),
}
}

Expand Down Expand Up @@ -229,6 +228,56 @@ impl TestStateReader {
class_hash_to_compiled_class_hash: declared_classes,
})
}

pub fn get_class_info(&self, class_hash: ClassHash) -> ReexecutionResult<ClassInfo> {
match self.get_contract_class(&class_hash)? {
StarknetContractClass::Sierra(sierra) => {
let abi_length = sierra.abi.len();
let sierra_length = sierra.sierra_program.len();
Ok(ClassInfo::new(&sierra_to_contact_class_v1(sierra)?, sierra_length, abi_length)?)
}
StarknetContractClass::Legacy(legacy) => {
let abi_length =
legacy.abi.clone().expect("legendary contract should have abi").len();
Ok(ClassInfo::new(
&legacy_to_contract_class_v0(legacy)?,
DEPRECATED_CONTRACT_SIERRA_SIZE,
abi_length,
)?)
}
}
}

// TODO(Aner): extend/refactor to accomodate all types of transactions.
pub(crate) fn from_api_txs_to_blockifier_txs(
self: &TestStateReader,
txs_and_hashes: Vec<(Transaction, TransactionHash)>,
) -> ReexecutionResult<Vec<BlockifierTransaction>> {
txs_and_hashes
.into_iter()
.map(|(tx, tx_hash)| match tx {
Transaction::Invoke(_) | Transaction::DeployAccount(_) => {
BlockifierTransaction::from_api(tx, tx_hash, None, None, None, false)
.map_err(ReexecutionError::from)
}
Transaction::Declare(ref declare_tx) => {
let class_info = self
.get_class_info(declare_tx.class_hash())
.map_err(ReexecutionError::from)?;
BlockifierTransaction::from_api(
tx,
tx_hash,
Some(class_info),
None,
None,
false,
)
.map_err(ReexecutionError::from)
}
_ => unimplemented!("unimplemented transaction type: {:?}", tx),
})
.collect::<Result<Vec<_>, _>>()
}
}

/// Trait of the functions \ queries required for reexecution.
Expand Down Expand Up @@ -276,7 +325,8 @@ impl ConsecutiveStateReaders<TestStateReader> for ConsecutiveTestStateReaders {
}

fn get_next_block_txs(&self) -> ReexecutionResult<Vec<BlockifierTransaction>> {
from_api_txs_to_blockifier_txs(self.next_block_state_reader.get_all_txs_in_block()?)
self.next_block_state_reader
.from_api_txs_to_blockifier_txs(self.next_block_state_reader.get_all_txs_in_block()?)
}

fn get_next_block_state_diff(&self) -> ReexecutionResult<CommitmentStateDiff> {
Expand Down
19 changes: 0 additions & 19 deletions crates/blockifier_reexecution/src/state_reader/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use blockifier::context::{ChainInfo, FeeTokenAddresses};
use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction;
use indexmap::IndexMap;
use papyrus_execution::{eth_fee_contract_address, strk_fee_contract_address};
use starknet_api::core::ChainId;
use starknet_api::transaction::{Transaction, TransactionHash};
use starknet_gateway::config::RpcStateReaderConfig;

use crate::state_reader::test_state_reader::ReexecutionResult;

pub const RPC_NODE_URL: &str = "https://free-rpc.nethermind.io/mainnet-juno/";
pub const JSON_RPC_VERSION: &str = "2.0";

Expand All @@ -32,21 +28,6 @@ pub fn get_chain_info() -> ChainInfo {
ChainInfo { chain_id: ChainId::Mainnet, fee_token_addresses: get_fee_token_addresses() }
}

// TODO(Aner): extend/refactor to accomodate all types of transactions.
#[allow(dead_code)]
pub(crate) fn from_api_txs_to_blockifier_txs(
txs_and_hashes: Vec<(Transaction, TransactionHash)>,
) -> ReexecutionResult<Vec<BlockifierTransaction>> {
Ok(txs_and_hashes
.into_iter()
.map(|(tx, tx_hash)| match tx {
Transaction::Invoke(_) => {
BlockifierTransaction::from_api(tx, tx_hash, None, None, None, false)
}
_ => unimplemented!(),
})
.collect::<Result<_, _>>()?)
}
// TODO(Aner): import the following functions instead, to reduce code duplication.
pub(crate) fn disjoint_hashmap_union<K: std::hash::Hash + std::cmp::Eq, V>(
map1: IndexMap<K, V>,
Expand Down
3 changes: 2 additions & 1 deletion crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ pub type AbiSize = usize;
/// The size of the sierra program.
pub type SierraSize = usize;

const DEPRECATED_CONTRACT_SIERRA_SIZE: SierraSize = 0;
/// The size of the sierra program for deprecated contracts.
pub const DEPRECATED_CONTRACT_SIERRA_SIZE: SierraSize = 0;

/// The transaction input to be executed.
// TODO(yair): This should use broadcasted transactions instead of regular transactions, but the
Expand Down

0 comments on commit 50739a6

Please sign in to comment.