Skip to content

Commit

Permalink
Merge branch 'feat/in-block-simulation' of git-cs:ape-dev-cs/transact…
Browse files Browse the repository at this point in the history
…ion-simulator into feat/in-block-simulation
  • Loading branch information
ape-dev-cs committed Apr 2, 2024
2 parents 291af55 + 35c7460 commit cc29e87
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ pub struct EvmError(pub Report);

impl Reject for EvmError {}

#[derive(Debug)]
pub struct InvalidRpcError();

impl Reject for InvalidRpcError {}

#[derive(Debug)]
pub struct RpcError();

impl Reject for RpcError {}

#[derive(Debug)]
pub struct InvalidIndexError();

impl Reject for InvalidIndexError {}

pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
let code;
let message: String;
Expand Down Expand Up @@ -78,6 +93,9 @@ pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible>
} else if let Some(_e) = err.find::<OverrideError>() {
code = StatusCode::INTERNAL_SERVER_ERROR;
message = "OVERRIDE_ERROR".to_string();
} else if let Some(_e) = err.find::<InvalidIndexError>() {
code = StatusCode::BAD_REQUEST;
message = "INVALID_TRANSACTION_INDEX".to_string();
} else if let Some(_e) = err.find::<EvmError>() {
if _e.0.to_string().contains("CallGasCostMoreThanGasLimit") {
code = StatusCode::BAD_REQUEST;
Expand All @@ -95,6 +113,12 @@ pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible>
None => "BAD_REQUEST".to_string(),
};
code = StatusCode::BAD_REQUEST;
} else if let Some(_e) = err.find::<InvalidRpcError>() {
code = StatusCode::BAD_REQUEST;
message = "INVALID_RPC".to_string();
} else if let Some(_e) = err.find::<RpcError>() {
code = StatusCode::INTERNAL_SERVER_ERROR;
message = "RPC_ERROR".to_string();
} else if err.find::<warp::reject::MethodNotAllowed>().is_some() {
// We can handle a specific error, here METHOD_NOT_ALLOWED,
// and render it however we want
Expand Down
33 changes: 24 additions & 9 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use warp::Rejection;

use crate::errors::{
IncorrectChainIdError, InvalidBlockNumbersError, MultipleChainIdsError, NoURLForChainIdError,
StateNotFound,
StateNotFound, RpcError
};
use crate::evm::StorageOverride;
use crate::SharedSimulationState;
Expand Down Expand Up @@ -336,18 +336,33 @@ async fn apply_block_transactions(
response: &mut Vec<SimulationResponse>,
) -> Result<(), Rejection> {
let provider = Provider::<Http>::try_from(fork_url);
let pre_transactions = provider

if provider.is_err() {
return Err(warp::reject::custom(NoURLForChainIdError));
}

let block_transactions = provider
.unwrap()
.get_block_with_txs(
transaction
.clone()
.block_number
.expect("Transaction has no block number"),
)
.await
.unwrap()
.unwrap();
let relevant_transactions: Vec<_> = pre_transactions
.await;
if block_transactions.is_err() {
return Err(warp::reject::custom(RpcError()));
}
let block_transactions = block_transactions.unwrap();

if block_transactions.is_none() {
response.push(run(evm, transaction.clone(), true).await?);
return Ok(());
}

let block_transactions = block_transactions.unwrap();

let simulation_transactions: Vec<_> = block_transactions
.transactions
.iter()
.map(|x| SimulationRequest {
Expand All @@ -365,12 +380,12 @@ async fn apply_block_transactions(
state_overrides: None,
})
.collect();
let transaction_block_index = transaction.clone().transaction_block_index.unwrap();
let transactions_before_index = relevant_transactions
let transaction_block_index = transaction.transaction_block_index.unwrap();
let transactions_before_index = simulation_transactions
.iter()
.take(transaction_block_index.as_usize())
.collect::<Vec<_>>();
let transactions_after_index = relevant_transactions
let transactions_after_index = simulation_transactions
.iter()
.skip(transaction_block_index.as_usize());
for before_tx in transactions_before_index {
Expand Down

0 comments on commit cc29e87

Please sign in to comment.