Skip to content

Commit 658bcbe

Browse files
starknet_os_runner: simulate tx
1 parent 0304289 commit 658bcbe

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/starknet_os_runner/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ license-file.workspace = true
77
description = "Runs transactions through the Starknet OS and returns Cairo PIE and OS output."
88

99
[dependencies]
10+
blockifier.workspace = true
11+
blockifier_reexecution = { path = "../blockifier_reexecution" }
12+
starknet_api.workspace = true
13+
thiserror.workspace = true
1014

1115
[lints]
1216
workspace = true
13-
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use blockifier_reexecution::errors::ReexecutionError;
2+
use thiserror::Error;
3+
4+
#[derive(Debug, Error)]
5+
pub enum SimulationError {
6+
#[error(transparent)]
7+
ReexecutionError(#[from] ReexecutionError),
8+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
//! Starknet OS Runner - executes transactions through the OS and returns Cairo PIE and output.
1+
pub mod errors;
2+
pub mod simulator;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use blockifier::blockifier::transaction_executor::TransactionExecutionOutput;
2+
use blockifier::context::BlockContext;
3+
use blockifier::state::cached_state::StateMaps;
4+
use blockifier_reexecution::execute_single_transaction;
5+
use starknet_api::block::BlockNumber;
6+
use starknet_api::core::ChainId;
7+
use starknet_api::transaction::Transaction;
8+
9+
use crate::errors::SimulationError;
10+
11+
pub struct SimulationOutput {
12+
pub execution_output: TransactionExecutionOutput,
13+
pub block_context: BlockContext,
14+
pub initial_reads: StateMaps,
15+
}
16+
17+
pub trait TxSimulator {
18+
fn simulate_tx(
19+
&self,
20+
block_number: BlockNumber,
21+
tx: Transaction,
22+
) -> Result<SimulationOutput, SimulationError>;
23+
}
24+
25+
pub struct RpcBlockifierTxSimulator {
26+
pub rpc_url: String,
27+
pub chain_id: ChainId,
28+
}
29+
30+
impl TxSimulator for RpcBlockifierTxSimulator {
31+
fn simulate_tx(
32+
&self,
33+
block_number: BlockNumber,
34+
tx: Transaction,
35+
) -> Result<SimulationOutput, SimulationError> {
36+
let (execution_output, initial_reads, block_context) = execute_single_transaction(
37+
block_number,
38+
self.rpc_url.clone(),
39+
self.chain_id.clone(),
40+
tx,
41+
)?;
42+
43+
Ok(SimulationOutput { execution_output, block_context, initial_reads })
44+
}
45+
}

0 commit comments

Comments
 (0)