Skip to content

Commit

Permalink
fix: serde for eth_simulateV1 (#1273)
Browse files Browse the repository at this point in the history
* fix: serde for eth_simulateV1

* clippy + fmt
  • Loading branch information
klkvr authored Sep 11, 2024
1 parent 043dc20 commit 5354223
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
2 changes: 2 additions & 0 deletions crates/provider/src/fillers/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl NonceManager for SimpleNonceManager {
}
}

/// Cached nonce manager
///
/// This [`NonceManager`] implementation will fetch the transaction count for any new account it
/// sees, store it locally and increment the locally stored nonce as transactions are sent via
/// [`Provider::send_transaction`].
Expand Down
63 changes: 26 additions & 37 deletions crates/rpc-types-eth/src/simulate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! 'eth_simulateV1' Request / Response types: <https://github.com/ethereum/execution-apis/pull/484>

use alloy_primitives::{Address, Bytes, Log, B256};
use alloy_primitives::Bytes;
use serde::{Deserialize, Serialize};

use crate::{state::StateOverride, BlockOverrides, TransactionRequest};
use crate::{state::StateOverride, Block, BlockOverrides, Log, TransactionRequest};

/// The maximum number of blocks that can be simulated in a single request,
pub const MAX_SIMULATE_BLOCKS: u64 = 256;
Expand All @@ -15,47 +15,26 @@ pub const MAX_SIMULATE_BLOCKS: u64 = 256;
#[serde(rename_all = "camelCase")]
pub struct SimBlock {
/// Modifications to the default block characteristics.
pub block_overrides: BlockOverrides,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub block_overrides: Option<BlockOverrides>,
/// State modifications to apply before executing the transactions.
pub state_overrides: StateOverride,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub state_overrides: Option<StateOverride>,
/// A vector of transactions to be simulated.
pub calls: Vec<TransactionRequest>,
}

/// Represents the result of simulating a block.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SimulatedBlock {
/// The number of the block.
#[serde(with = "alloy_serde::quantity")]
pub number: u64,
/// The hash of the block.
pub hash: B256,
/// The timestamp of the block.
#[serde(with = "alloy_serde::quantity")]
pub timestamp: u64,
/// The gas limit of the block.
#[serde(with = "alloy_serde::quantity")]
pub gas_limit: u64,
/// The amount of gas used in the block.
#[serde(with = "alloy_serde::quantity")]
pub gas_used: u64,
/// The recipient of the block's fees.
pub fee_recipient: Address,
/// The base fee per gas unit for the block.
#[serde(with = "alloy_serde::quantity")]
pub base_fee_per_gas: u64,
/// The previous RANDAO value of the block.
pub prev_randao: B256,
pub struct SimulatedBlock<B = Block> {
/// The simulated block.
#[serde(flatten)]
pub inner: B,
/// A vector of results for each call in the block.
pub calls: Vec<SimCallResult>,
}
/// The response type for the eth_simulateV1 method.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SimulateV1Response {
/// Simulated blocks vector.
pub simulated_blocks: Vec<SimulatedBlock>,
}

/// Captures the outcome of a transaction simulation.
/// It includes the return value, logs produced, gas used, and the status of the transaction.
#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -71,7 +50,7 @@ pub struct SimCallResult {
pub gas_used: u64,
/// The final status of the transaction, typically indicating success or failure.
#[serde(with = "alloy_serde::quantity")]
pub status: u64,
pub status: bool,
/// Error in case the call failed
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<SimulateError>,
Expand Down Expand Up @@ -168,11 +147,21 @@ mod tests {
assert_eq!(sim_opts.block_state_calls.len(), 2);

let block_state_call_1 = &sim_opts.block_state_calls[0];
assert!(block_state_call_1.state_overrides.contains_key(&address_1));
assert_eq!(block_state_call_1.state_overrides.get(&address_1).unwrap().nonce.unwrap(), 5);
assert!(block_state_call_1.state_overrides.as_ref().unwrap().contains_key(&address_1));
assert_eq!(
block_state_call_1
.state_overrides
.as_ref()
.unwrap()
.get(&address_1)
.unwrap()
.nonce
.unwrap(),
5
);

let block_state_call_2 = &sim_opts.block_state_calls[1];
assert!(block_state_call_2.state_overrides.contains_key(&address_1));
assert!(block_state_call_2.state_overrides.as_ref().unwrap().contains_key(&address_1));

assert_eq!(block_state_call_2.calls.len(), 2);
assert_eq!(block_state_call_2.calls[0].from.unwrap(), address_1);
Expand Down
5 changes: 5 additions & 0 deletions crates/rpc-types-eth/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub struct AccountOverride {
/// the call.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub state_diff: Option<HashMap<B256, B256>>,
/// Moves addresses precompile into the specified address. This move is done before the 'code'
/// override is set. When the specified address is not a precompile, the behaviour is undefined
/// and different clients might behave differently.
#[serde(default, skip_serializing_if = "Option::is_none", rename = "movePrecompileToAddress")]
pub move_precompile_to: Option<Address>,
}

/// Helper type that bundles various overrides for EVM Execution.
Expand Down

0 comments on commit 5354223

Please sign in to comment.