Skip to content

Commit

Permalink
Refactoring of function 'execute_block'
Browse files Browse the repository at this point in the history
  • Loading branch information
Eagle941 committed Oct 1, 2024
1 parent 0bdeb2a commit aa44d60
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions starknet-replay/src/storage/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,46 @@ impl RpcStorage {
BlockifierTransaction::L1HandlerTransaction(_) => TransactionType::L1Handler,
}
}

fn preprocess_transactions(
&self,
work: &ReplayBlock,
) -> Result<Vec<BlockifierTransaction>, RunnerError> {
let mut transactions = Vec::with_capacity(work.transactions.len());
let block_number = BlockNumber::new(work.header.block_number.0);
for (transaction, receipt) in work.transactions.iter().zip(work.receipts.iter()) {
let tx = transaction;
let tx_hash = receipt.transaction_hash;
let class_info = class_info::generate_class_info(self, block_number, tx)?;

let paid_fee_on_l1 = match tx {
Transaction::L1Handler(_) => {
Some(starknet_api::transaction::Fee(1_000_000_000_000))
}
_ => None,
};

let deployed_contract_address = match &receipt.output {
starknet_api::transaction::TransactionOutput::DeployAccount(receipt) => {
Some(receipt.contract_address)
}
_ => None,
};

let only_query = false;
let transaction = BlockifierTransaction::from_api(
tx.to_owned(),
tx_hash,
class_info,
paid_fee_on_l1,
deployed_contract_address,
only_query,
)?;

transactions.push(transaction);
}
Ok(transactions)
}
}
impl ReplayStorage for RpcStorage {
fn get_most_recent_block_number(&self) -> Result<BlockNumber, DatabaseError> {
Expand Down Expand Up @@ -357,40 +397,6 @@ impl ReplayStorage for RpcStorage {
) -> Result<Vec<TransactionOutput>, RunnerError> {
let block_number = BlockNumber::new(work.header.block_number.0);

let mut transactions: Vec<BlockifierTransaction> =
Vec::with_capacity(work.transactions.len());
for (transaction, receipt) in work.transactions.iter().zip(work.receipts.iter()) {
let tx = transaction;
let tx_hash = receipt.transaction_hash;
let class_info = class_info::generate_class_info(self, block_number, tx)?;

let paid_fee_on_l1 = match tx {
Transaction::L1Handler(_) => {
Some(starknet_api::transaction::Fee(1_000_000_000_000))
}
_ => None,
};

let deployed_contract_address = match &receipt.output {
starknet_api::transaction::TransactionOutput::DeployAccount(receipt) => {
Some(receipt.contract_address)
}
_ => None,
};

let only_query = false;
let transaction = BlockifierTransaction::from_api(
tx.to_owned(),
tx_hash,
class_info,
paid_fee_on_l1,
deployed_contract_address,
only_query,
)?;

transactions.push(transaction);
}

// Transactions are replayed with the call to `ExecutableTransaction::execute`.
// When simulating transactions, the storage layer should match the data of the
// parent block (i.e. before the transaction is executed)
Expand Down Expand Up @@ -433,8 +439,8 @@ impl ReplayStorage for RpcStorage {
work.header.block_number,
)?;

let mut transaction_result: Vec<_> = Vec::with_capacity(transactions.len());
for (idx, transaction) in transactions.iter().enumerate() {
let mut transaction_result: Vec<_> = Vec::with_capacity(work.transactions.len());
for (idx, transaction) in self.preprocess_transactions(work)?.iter().enumerate() {
let tx_type = Self::transaction_type(transaction);
let transaction_declared_deprecated_class_hash =
Self::transaction_declared_deprecated_class(transaction);
Expand Down

0 comments on commit aa44d60

Please sign in to comment.