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 feature-gated Serde implementations #47

Merged
merged 1 commit into from
Feb 19, 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
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ revm = { version = "6.0", default-features = false, features = ["std"] }
anstyle = "1.0"
colorchoice = "1.0"

# js-tracing-inspector
# serde
serde = { version = "1", optional = true, features = ["derive"] }

# js-tracer
boa_engine = { version = "0.17", optional = true }
boa_gc = { version = "0.17", optional = true }

serde = { version = "1", features = ["derive"] }
thiserror = { version = "1", optional = true }
serde_json = { version = "1", optional = true }

[dev-dependencies]
expect-test = "1.4"

[features]
js-tracer = ["boa_engine", "boa_gc", "thiserror", "serde_json"]
serde = ["dep:serde", "revm/serde"]
js-tracer = ["dep:boa_engine", "dep:boa_gc", "dep:thiserror", "dep:serde_json"]
5 changes: 3 additions & 2 deletions src/tracing/arena.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::tracing::types::{CallTrace, CallTraceNode, LogCallOrder};
use super::types::{CallTrace, CallTraceNode, LogCallOrder};

/// An arena of recorded traces.
///
/// This type will be populated via the [TracingInspector](crate::tracing::TracingInspector).
/// This type will be populated via the [TracingInspector](super::TracingInspector).
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallTraceArena {
/// The arena of recorded trace nodes
pub(crate) arena: Vec<CallTraceNode>,
Expand Down
36 changes: 33 additions & 3 deletions src/tracing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use alloy_rpc_trace_types::{
},
};
use revm::interpreter::{opcode, CallContext, CallScheme, CreateScheme, InstructionResult, OpCode};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// A trace of a call.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallTrace {
/// The depth of the call
pub depth: usize,
Expand Down Expand Up @@ -101,6 +101,7 @@ impl CallTrace {

/// A node in the arena
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallTraceNode {
/// Parent node index in the arena
pub parent: Option<usize>,
Expand Down Expand Up @@ -333,8 +334,9 @@ impl CallTraceNode {
}

/// A unified representation of a call.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "UPPERCASE"))]
pub enum CallKind {
/// Represents a regular call.
#[default]
Expand Down Expand Up @@ -452,6 +454,7 @@ pub(crate) struct CallTraceStepStackItem<'a> {

/// Ordering enum for calls and logs
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LogCallOrder {
/// Contains the index of the corresponding log
Log(usize),
Expand All @@ -461,13 +464,15 @@ pub enum LogCallOrder {

/// Represents a tracked call step during execution
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallTraceStep {
// Fields filled in `step`
/// Call depth
pub depth: u64,
/// Program counter before step execution
pub pc: usize,
/// Opcode to be executed
#[cfg_attr(feature = "serde", serde(with = "opcode_serde"))]
pub op: OpCode,
/// Current contract address
pub contract: Address,
Expand Down Expand Up @@ -572,6 +577,7 @@ impl CallTraceStep {
/// from an SSTORE or SLOAD instruction.
#[allow(clippy::upper_case_acronyms)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum StorageChangeReason {
/// SLOAD opcode
SLOAD,
Expand All @@ -587,6 +593,7 @@ pub enum StorageChangeReason {
/// It is used to track both storage change and warm load of a storage slot. For warm load in regard
/// to EIP-2929 AccessList had_value will be None.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StorageChange {
/// key of the storage slot
pub key: U256,
Expand All @@ -602,6 +609,7 @@ pub struct StorageChange {
///
/// This is a wrapper around the [SharedMemory](revm::interpreter::SharedMemory) context memory.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RecordedMemory(pub(crate) Vec<u8>);

impl RecordedMemory {
Expand Down Expand Up @@ -645,3 +653,25 @@ impl AsRef<[u8]> for RecordedMemory {
self.as_bytes()
}
}

#[cfg(feature = "serde")]
mod opcode_serde {
use super::OpCode;
use serde::{Deserialize, Deserializer, Serializer};

pub(super) fn serialize<S>(op: &OpCode, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u8(op.get())
}

pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<OpCode, D::Error>
where
D: Deserializer<'de>,
{
let op = u8::deserialize(deserializer)?;
Ok(OpCode::new(op)
.unwrap_or_else(|| OpCode::new(revm::interpreter::opcode::INVALID).unwrap()))
}
}
Loading