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

remove mp_transactions::UserOrL1HandlerTransactions #36

Merged
Merged
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
15 changes: 10 additions & 5 deletions crates/client/rpc/src/methods/read/estimate_fee.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use blockifier::transaction::account_transaction::AccountTransaction;
use deoxys_runtime::opaque::DBlockT;
use jsonrpsee::core::RpcResult;
use mc_genesis_data_provider::GenesisProvider;
use mp_hashers::HasherT;
use mp_simulations::convert_flags;
use mp_transactions::UserTransaction;
use mp_transactions::from_broadcasted_transactions::ToAccountTransaction;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::backend::{Backend, StorageProvider};
use sc_client_api::BlockBackend;
Expand Down Expand Up @@ -49,13 +50,17 @@ where
StarknetRpcApiError::BlockNotFound
})?;

let transactions =
request.into_iter().map(|tx| tx.try_into()).collect::<Result<Vec<UserTransaction>, _>>().map_err(|e| {
log::error!("Failed to convert BroadcastedTransaction to UserTransaction: {e}");
let transactions = request
.into_iter()
.map(|tx| tx.to_account_transaction())
.collect::<Result<Vec<AccountTransaction>, _>>()
.map_err(|e| {
log::error!("Failed to convert BroadcastedTransaction to AccountTransaction: {e}");
StarknetRpcApiError::InternalServerError
})?;

let account_transactions: Vec<UserTransaction> = transactions.into_iter().map(UserTransaction::from).collect();
let account_transactions: Vec<AccountTransaction> =
transactions.into_iter().map(AccountTransaction::from).collect();

let simulation_flags = convert_flags(simulation_flags);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use mp_felt::Felt252Wrapper;
use mp_hashers::pedersen::PedersenHasher;
use mp_hashers::HasherT;
use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::tx_into_user_or_l1_vec;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::backend::{Backend, StorageProvider};
use sc_client_api::BlockBackend;
Expand Down Expand Up @@ -586,12 +585,7 @@ where
let execution_infos = client
.client
.runtime_api()
.re_execute_transactions(
previous_block_hash,
tx_into_user_or_l1_vec(prev),
tx_into_user_or_l1_vec(last),
block_context,
)
.re_execute_transactions(previous_block_hash, prev, last, block_context)
.map_err(|e| {
log::error!("Failed to execute runtime API call: {e}");
StarknetRpcApiError::InternalServerError
Expand Down
9 changes: 4 additions & 5 deletions crates/client/rpc/src/methods/trace/simulate_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use mc_genesis_data_provider::GenesisProvider;
use mc_storage::StorageOverride;
use mp_hashers::HasherT;
use mp_simulations::{PlaceHolderErrorTypeForFailedStarknetExecution, SimulationFlags};
use mp_transactions::from_broadcasted_transactions::ToAccountTransaction;
use mp_transactions::TxType;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::{Backend, BlockBackend, StorageProvider};
Expand Down Expand Up @@ -43,11 +44,9 @@ where
starknet.substrate_block_hash_from_starknet_block(block_id).map_err(|_e| StarknetRpcApiError::BlockNotFound)?;

let tx_type_and_tx_iterator = transactions.into_iter().map(|tx| match tx {
BroadcastedTransaction::Invoke(invoke_tx) => invoke_tx.try_into().map(|tx| (TxType::Invoke, tx)),
BroadcastedTransaction::Declare(declare_tx) => declare_tx.try_into().map(|tx| (TxType::Declare, tx)),
BroadcastedTransaction::DeployAccount(deploy_account_tx) => {
deploy_account_tx.try_into().map(|tx| (TxType::DeployAccount, tx))
}
BroadcastedTransaction::Invoke(_) => tx.to_account_transaction().map(|tx| (TxType::Invoke, tx)),
BroadcastedTransaction::Declare(_) => tx.to_account_transaction().map(|tx| (TxType::Declare, tx)),
BroadcastedTransaction::DeployAccount(_) => tx.to_account_transaction().map(|tx| (TxType::DeployAccount, tx)),
});
let (tx_types, user_transactions) =
itertools::process_results(tx_type_and_tx_iterator, |iter| iter.unzip::<_, _, Vec<_>, Vec<_>>()).map_err(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use jsonrpsee::core::RpcResult;
use mc_genesis_data_provider::GenesisProvider;
use mp_felt::Felt252Wrapper;
use mp_hashers::HasherT;
use mp_transactions::getters::{Getters, Hash};
use mp_transactions::TxType;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::{Backend, BlockBackend, StorageProvider};
Expand Down Expand Up @@ -94,7 +95,7 @@ where
&**storage_override,
substrate_block_hash,
// Safe to unwrap coz re_execute returns exactly one ExecutionInfo for each tx
TxType::from(block_transactions.get(tx_idx).unwrap().tx_type()),
TxType::from(block_transactions.get(tx_idx).unwrap()),
&tx_exec_info,
)
.map(|trace_root| TransactionTraceWithHash {
Expand Down
3 changes: 2 additions & 1 deletion crates/client/rpc/src/methods/trace/trace_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use mc_db::DeoxysBackend;
use mc_genesis_data_provider::GenesisProvider;
use mp_felt::Felt252Wrapper;
use mp_hashers::HasherT;
use mp_transactions::TxType;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::{Backend, BlockBackend, StorageProvider};
use sc_transaction_pool::ChainApi;
Expand Down Expand Up @@ -96,7 +97,7 @@ where
let trace = tx_execution_infos_to_tx_trace(
&**storage_override,
substrate_block_hash,
tx_to_trace.get(0).unwrap().tx_type(),
TxType::from(tx_to_trace.get(0).unwrap()),
&execution_infos[0],
)
.unwrap();
Expand Down
17 changes: 9 additions & 8 deletions crates/client/rpc/src/methods/trace/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use blockifier::execution::contract_class::{ClassInfo, ContractClass, ContractCl
use blockifier::transaction as btx;
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier::transaction::transaction_execution::Transaction;
use blockifier::transaction::transactions::L1HandlerTransaction;
use deoxys_runtime::opaque::{DBlockT, DHashT};
use mc_db::DeoxysBackend;
Expand All @@ -13,7 +14,7 @@ use mp_block::DeoxysBlock;
use mp_felt::Felt252Wrapper;
use mp_hashers::HasherT;
use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::{TxType, UserOrL1HandlerTransaction};
use mp_transactions::TxType;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::{Backend, BlockBackend, StorageProvider};
use sc_transaction_pool::ChainApi;
Expand Down Expand Up @@ -263,7 +264,7 @@ pub fn map_transaction_to_user_transaction<A, BE, G, C, P, H>(
substrate_block_hash: DHashT,
chain_id: Felt252Wrapper,
target_transaction_hash: Option<Felt252Wrapper>,
) -> Result<(Vec<UserOrL1HandlerTransaction>, Vec<UserOrL1HandlerTransaction>), StarknetRpcApiError>
) -> Result<(Vec<Transaction>, Vec<Transaction>), StarknetRpcApiError>
where
A: ChainApi<Block = DBlockT> + 'static,
C: HeaderBackend<DBlockT> + BlockBackend<DBlockT> + StorageProvider<DBlockT, BE> + 'static,
Expand Down Expand Up @@ -296,7 +297,7 @@ fn convert_transaction<A, BE, G, C, P, H>(
substrate_block_hash: DHashT,
chain_id: Felt252Wrapper,
block_number: u64,
) -> Result<UserOrL1HandlerTransaction, StarknetRpcApiError>
) -> Result<Transaction, StarknetRpcApiError>
where
A: ChainApi<Block = DBlockT> + 'static,
C: HeaderBackend<DBlockT> + BlockBackend<DBlockT> + StorageProvider<DBlockT, BE> + 'static,
Expand All @@ -311,7 +312,7 @@ where
// TODO: Check if this is correct
only_query: false,
};
Ok(UserOrL1HandlerTransaction::User(AccountTransaction::Invoke(tx)))
Ok(Transaction::AccountTransaction(AccountTransaction::Invoke(tx)))
}
stx::Transaction::DeployAccount(deploy_account_tx) => {
let tx = btx::transactions::DeployAccountTransaction {
Expand All @@ -322,7 +323,7 @@ where
// TODO: Check if this is correct
only_query: false,
};
Ok(UserOrL1HandlerTransaction::User(AccountTransaction::DeployAccount(tx)))
Ok(Transaction::AccountTransaction(AccountTransaction::DeployAccount(tx)))
}
stx::Transaction::Declare(declare_tx) => {
let class_hash = ClassHash::from(Felt252Wrapper::from(*declare_tx.class_hash()));
Expand All @@ -348,7 +349,7 @@ where
)
.unwrap();

Ok(UserOrL1HandlerTransaction::User(AccountTransaction::Declare(tx)))
Ok(Transaction::AccountTransaction(AccountTransaction::Declare(tx)))
}
stx::DeclareTransaction::V2(_tx) => {
let contract_class = DeoxysBackend::sierra_classes()
Expand Down Expand Up @@ -383,7 +384,7 @@ where
)
.unwrap();

Ok(UserOrL1HandlerTransaction::User(AccountTransaction::Declare(tx)))
Ok(Transaction::AccountTransaction(AccountTransaction::Declare(tx)))
}
stx::DeclareTransaction::V3(_) => todo!(),
}
Expand All @@ -397,7 +398,7 @@ where
StarknetRpcApiError::InternalServerError
})?;

Ok(UserOrL1HandlerTransaction::L1Handler(L1HandlerTransaction {
Ok(Transaction::L1HandlerTransaction(L1HandlerTransaction {
tx: handle_l1_message_tx.clone(),
tx_hash,
paid_fee_on_l1,
Expand Down
9 changes: 5 additions & 4 deletions crates/pallets/starknet/runtime_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

use blockifier::context::{BlockContext, FeeTokenAddresses};
use blockifier::execution::contract_class::ContractClass;
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier::transaction::transaction_execution::Transaction;
use blockifier::transaction::transactions::L1HandlerTransaction;
use mp_felt::Felt252Wrapper;
use mp_transactions::{UserOrL1HandlerTransaction, UserTransaction};
use sp_api::BlockT;
pub extern crate alloc;
use alloc::vec::Vec;
Expand Down Expand Up @@ -58,13 +59,13 @@ sp_api::decl_runtime_apis! {
/// Returns the fee token address.
fn fee_token_addresses() -> FeeTokenAddresses;
/// Returns fee estimate
fn estimate_fee(transactions: Vec<UserTransaction>, simulation_flags: Vec<SimulationFlagForEstimateFee>,) -> Result<Vec<FeeEstimate>, DispatchError>;
fn estimate_fee(transactions: Vec<AccountTransaction>, simulation_flags: Vec<SimulationFlagForEstimateFee>,) -> Result<Vec<FeeEstimate>, DispatchError>;
/// Returns message fee estimate
fn estimate_message_fee(message: L1HandlerTransaction) -> Result<(u128, u128, u128), DispatchError>;
/// Simulates single L1 Message and returns its trace
fn simulate_message(message: L1HandlerTransaction, simulation_flags: SimulationFlags) -> Result<Result<TransactionExecutionInfo, PlaceHolderErrorTypeForFailedStarknetExecution>, DispatchError>;
/// Simulates transactions and returns their trace
fn simulate_transactions(transactions: Vec<UserTransaction>, simulation_flags: SimulationFlags) -> Result<Vec<Result<TransactionExecutionInfo, PlaceHolderErrorTypeForFailedStarknetExecution>>, DispatchError>;
fn simulate_transactions(transactions: Vec<AccountTransaction>, simulation_flags: SimulationFlags) -> Result<Vec<Result<TransactionExecutionInfo, PlaceHolderErrorTypeForFailedStarknetExecution>>, DispatchError>;

/// Filters extrinsic transactions to return only Starknet transactions
///
Expand All @@ -86,7 +87,7 @@ sp_api::decl_runtime_apis! {
///
/// Idealy, the execution traces of all of `transactions_to_trace`.
/// If any of the transactions (from both arguments) fails, an error is returned.
fn re_execute_transactions(transactions_before: Vec<UserOrL1HandlerTransaction>, transactions_to_trace: Vec<UserOrL1HandlerTransaction>, block_context: &BlockContext) -> Result<Result<Vec<TransactionExecutionInfo>, PlaceHolderErrorTypeForFailedStarknetExecution>, DispatchError>;
fn re_execute_transactions(transactions_before: Vec<Transaction>, transactions_to_trace: Vec<Transaction>, block_context: &BlockContext) -> Result<Result<Vec<TransactionExecutionInfo>, PlaceHolderErrorTypeForFailedStarknetExecution>, DispatchError>;

fn get_events_for_tx_by_hash(tx_hash: TransactionHash) -> Vec<StarknetEvent>;
// fn get_index_and_tx_for_tx_hash(xts: Vec<<Block as BlockT>::Extrinsic>, chain_id: Felt252Wrapper, tx_hash: TransactionHash) -> Option<(u32, Transaction)>;
Expand Down
88 changes: 34 additions & 54 deletions crates/pallets/starknet/src/simulations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use blockifier::transaction::transaction_execution::Transaction;
use blockifier::transaction::transactions::{ExecutableTransaction, L1HandlerTransaction};
use frame_support::storage;
use mp_simulations::{PlaceHolderErrorTypeForFailedStarknetExecution, SimulationFlagForEstimateFee, SimulationFlags};
use mp_transactions::{user_or_l1_into_tx_vec, UserOrL1HandlerTransaction, UserTransaction};
use sp_core::Get;
use sp_runtime::DispatchError;
use starknet_api::core::{ContractAddress, EntryPointSelector};
Expand All @@ -22,7 +21,7 @@ use crate::{Config, Error, Pallet};

impl<T: Config> Pallet<T> {
pub fn estimate_fee(
transactions: Vec<UserTransaction>,
transactions: Vec<AccountTransaction>,
simulation_flags: &Vec<SimulationFlagForEstimateFee>,
) -> Result<Vec<FeeEstimate>, DispatchError> {
storage::transactional::with_transaction(|| {
Expand All @@ -35,7 +34,7 @@ impl<T: Config> Pallet<T> {
}

fn estimate_fee_inner(
transactions: Vec<UserTransaction>,
transactions: Vec<AccountTransaction>,
simulation_flags: &Vec<SimulationFlagForEstimateFee>,
) -> Result<Vec<FeeEstimate>, DispatchError> {
let transactions_len = transactions.len();
Expand All @@ -60,7 +59,7 @@ impl<T: Config> Pallet<T> {
}

pub fn simulate_transactions(
transactions: Vec<UserTransaction>,
transactions: Vec<AccountTransaction>,
simulation_flags: &SimulationFlags,
) -> Result<Vec<Result<TransactionExecutionInfo, PlaceHolderErrorTypeForFailedStarknetExecution>>, DispatchError>
{
Expand All @@ -74,7 +73,7 @@ impl<T: Config> Pallet<T> {
}

fn simulate_transactions_inner(
transactions: Vec<UserTransaction>,
transactions: Vec<AccountTransaction>,
simulation_flags: &SimulationFlags,
) -> Result<Vec<Result<TransactionExecutionInfo, PlaceHolderErrorTypeForFailedStarknetExecution>>, DispatchError>
{
Expand All @@ -83,7 +82,13 @@ impl<T: Config> Pallet<T> {
let tx_execution_results = transactions
.into_iter()
.map(|tx| {
Self::execute_account_transaction(tx.into(), &block_context, simulation_flags).map_err(|e| {
tx.execute(
&mut Self::init_cached_state(),
&block_context,
simulation_flags.charge_fee,
simulation_flags.validate,
)
.map_err(|e| {
log::error!("Transaction execution failed during simulation: {e}");
PlaceHolderErrorTypeForFailedStarknetExecution
})
Expand Down Expand Up @@ -159,8 +164,8 @@ impl<T: Config> Pallet<T> {
}

pub fn re_execute_transactions(
transactions_before: Vec<UserOrL1HandlerTransaction>,
transactions_to_trace: Vec<UserOrL1HandlerTransaction>,
transactions_before: Vec<Transaction>,
transactions_to_trace: Vec<Transaction>,
block_context: &BlockContext,
) -> Result<Result<Vec<TransactionExecutionInfo>, PlaceHolderErrorTypeForFailedStarknetExecution>, DispatchError>
{
Expand All @@ -175,39 +180,33 @@ impl<T: Config> Pallet<T> {
}

fn re_execute_transactions_inner(
transactions_before: Vec<UserOrL1HandlerTransaction>,
transactions_to_trace: Vec<UserOrL1HandlerTransaction>,
transactions_before: Vec<Transaction>,
transactions_to_trace: Vec<Transaction>,
block_context: &BlockContext,
) -> Result<Result<Vec<TransactionExecutionInfo>, PlaceHolderErrorTypeForFailedStarknetExecution>, DispatchError>
{
// desactivate fee charging for the first blocks
let simulation_flags = SimulationFlags {
charge_fee: if block_context.block_info().gas_prices.eth_l1_gas_price.get() == 1 { false } else { true },
validate: false,
};

let _transactions_before_exec_infos = Self::execute_account_or_l1_handler_transactions(
user_or_l1_into_tx_vec(transactions_before),
&block_context,
&simulation_flags,
);
let transactions_exec_infos = Self::execute_account_or_l1_handler_transactions(
user_or_l1_into_tx_vec(transactions_to_trace),
&block_context,
&simulation_flags,
);
let charge_fee = if block_context.block_info().gas_prices.eth_l1_gas_price.get() == 1 { false } else { true };
let mut cached_state = Self::init_cached_state();

Ok(transactions_exec_infos)
}
transactions_before
.into_iter()
.map(|tx| tx.execute(&mut cached_state, block_context, charge_fee, false))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| {
log::error!("Transaction execution failed during re-execution: {e}");
Error::<T>::FailedToCreateATransactionalStorageExecution
})?;

fn execute_account_transaction(
transaction: AccountTransaction,
block_context: &BlockContext,
simulation_flags: &SimulationFlags,
) -> Result<TransactionExecutionInfo, TransactionExecutionError> {
let mut cached_state = Self::init_cached_state();
let transactions_exec_infos = transactions_to_trace
.into_iter()
.map(|tx| tx.execute(&mut cached_state, block_context, charge_fee, false))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| {
log::error!("Transaction execution failed during re-execution: {e}");
Error::<T>::FailedToCreateATransactionalStorageExecution
})?;

transaction.execute(&mut cached_state, block_context, simulation_flags.charge_fee, simulation_flags.validate)
Ok(Ok(transactions_exec_infos))
}

fn execute_fee_transaction(
Expand Down Expand Up @@ -284,25 +283,6 @@ impl<T: Config> Pallet<T> {

transaction.execute(&mut cached_state, block_context, simulation_flags.charge_fee, simulation_flags.validate)
}

fn execute_account_or_l1_handler_transactions(
transactions: Vec<Transaction>,
block_context: &BlockContext,
simulation_flags: &SimulationFlags,
) -> Result<Vec<TransactionExecutionInfo>, PlaceHolderErrorTypeForFailedStarknetExecution> {
let mut cached_state = Self::init_cached_state();

transactions
.into_iter()
.map(|user_or_l1_tx| match user_or_l1_tx {
Transaction::AccountTransaction(tx) => {
Self::execute_account_transaction(tx, block_context, simulation_flags)
}
Transaction::L1HandlerTransaction(tx) => Self::execute_message(tx, block_context, simulation_flags),
})
.collect::<Result<Vec<_>, _>>()
.map_err(|_| PlaceHolderErrorTypeForFailedStarknetExecution)
}
}

pub fn from_tx_info_and_gas_price(
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/transactions/src/compute_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use starknet_core::utils::starknet_keccak;
use starknet_crypto::FieldElement;

use super::SIMULATE_TX_VERSION_OFFSET;
use crate::{UserOrL1HandlerTransaction, LEGACY_BLOCK_NUMBER};
use crate::LEGACY_BLOCK_NUMBER;

const DECLARE_PREFIX: &[u8] = b"declare";
const DEPLOY_ACCOUNT_PREFIX: &[u8] = b"deploy_account";
Expand Down
Loading
Loading