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

feat: add types for flat call tracer #1292

Merged
merged 1 commit into from
Sep 17, 2024
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
33 changes: 33 additions & 0 deletions crates/rpc-types-trace/src/geth/call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Geth call tracer types.

use crate::parity::LocalizedTransactionTrace;
use alloy_primitives::{Address, Bytes, B256, U256};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -88,6 +89,38 @@ impl CallConfig {
}
}

/// The response object for `debug_traceTransaction` with `"tracer": "flatCallTracer"`.
///
/// That is equivalent to parity's [`LocalizedTransactionTrace`]
/// <https://github.com/ethereum/go-ethereum/blob/0dd173a727dd2d2409b8e401b22e85d20c25b71f/eth/tracers/native/call_flat.go#L62-L62>
pub type FlatCallFrame = LocalizedTransactionTrace;

/// The configuration for the flat call tracer.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FlatCallConfig {
/// If true, call tracer converts errors to parity format
#[serde(default, skip_serializing_if = "Option::is_none")]
pub convert_parity_errors: Option<bool>,
/// If true, call tracer includes calls to precompiled contracts
#[serde(default, skip_serializing_if = "Option::is_none")]
pub include_precompiles: Option<bool>,
}

impl FlatCallConfig {
/// Converts errors to parity format.
pub const fn parity_errors(mut self) -> Self {
self.convert_parity_errors = Some(true);
self
}

/// Include calls to precompiled contracts.
pub const fn with_precompiles(mut self) -> Self {
self.include_precompiles = Some(true);
self
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
36 changes: 33 additions & 3 deletions crates/rpc-types-trace/src/geth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! Geth tracing types.

use crate::geth::mux::{MuxConfig, MuxFrame};
use crate::geth::{
call::FlatCallFrame,
mux::{MuxConfig, MuxFrame},
};
use alloy_primitives::{Bytes, B256, U256};
use alloy_rpc_types_eth::{state::StateOverride, BlockOverrides};
use serde::{de::DeserializeOwned, ser::SerializeMap, Deserialize, Serialize, Serializer};
use std::{collections::BTreeMap, time::Duration};

// re-exports
pub use self::{
call::{CallConfig, CallFrame, CallLogFrame},
call::{CallConfig, CallFrame, CallLogFrame, FlatCallConfig},
four_byte::FourByteFrame,
noop::NoopFrame,
pre_state::{
Expand Down Expand Up @@ -118,6 +120,8 @@ pub enum GethTrace {
Default(DefaultFrame),
/// The response for call tracer
CallTracer(CallFrame),
/// The response for the flat call tracer
FlatCallTracer(FlatCallFrame),
/// The response for four byte tracer
FourByteTracer(FourByteFrame),
/// The response for pre-state byte tracer
Expand Down Expand Up @@ -147,6 +151,14 @@ impl GethTrace {
}
}

/// Try to convert the inner tracer to [FlatCallFrame]
pub fn try_into_flat_call_frame(self) -> Result<FlatCallFrame, UnexpectedTracerError> {
match self {
Self::FlatCallTracer(inner) => Ok(inner),
_ => Err(UnexpectedTracerError(self)),
}
}

/// Try to convert the inner tracer to [FourByteFrame]
pub fn try_into_four_byte_frame(self) -> Result<FourByteFrame, UnexpectedTracerError> {
match self {
Expand Down Expand Up @@ -212,6 +224,12 @@ impl From<CallFrame> for GethTrace {
}
}

impl From<FlatCallFrame> for GethTrace {
fn from(value: FlatCallFrame) -> Self {
Self::FlatCallTracer(value)
}
}

impl From<PreStateFrame> for GethTrace {
fn from(value: PreStateFrame) -> Self {
Self::PreStateTracer(value)
Expand Down Expand Up @@ -246,6 +264,10 @@ pub enum GethDebugBuiltInTracerType {
/// with the top-level call at root and sub-calls as children of the higher levels.
#[serde(rename = "callTracer")]
CallTracer,
/// Tracks all call frames of a transaction and returns them in a flat format, i.e. as opposed
/// to the nested format of `callTracer`.
#[serde(rename = "flatCallTracer")]
FlatCallTracer,
/// The prestate tracer has two modes: prestate and diff. The prestate mode returns the
/// accounts necessary to execute a given transaction. diff mode returns the differences
/// between the transaction's pre and post-state (i.e. what changed because the transaction
Expand Down Expand Up @@ -311,6 +333,14 @@ impl GethDebugTracerConfig {
self.from_value()
}

/// Returns the [FlatCallConfig] if it is a call config.
pub fn into_flat_call_config(self) -> Result<FlatCallConfig, serde_json::Error> {
if self.0.is_null() {
return Ok(Default::default());
}
self.from_value()
}

/// Returns the raw json value
pub fn into_json(self) -> serde_json::Value {
self.0
Expand Down