diff --git a/src/eth/primitives/address.rs b/src/eth/primitives/address.rs index 670d70b78..fe1cbebcf 100644 --- a/src/eth/primitives/address.rs +++ b/src/eth/primitives/address.rs @@ -3,6 +3,7 @@ use std::ops::Deref; use std::str::FromStr; use anyhow::anyhow; +use display_json::DebugAsJson; use ethabi::Token; use ethereum_types::H160; use ethers_core::types::NameOrAddress; @@ -20,7 +21,7 @@ use crate::alias::RevmAddress; use crate::gen_newtype_from; /// Address of an Ethereum account (wallet or contract). -#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] pub struct Address(pub H160); impl Address { diff --git a/src/eth/primitives/block_header.rs b/src/eth/primitives/block_header.rs index b5570192a..ded3cb577 100644 --- a/src/eth/primitives/block_header.rs +++ b/src/eth/primitives/block_header.rs @@ -1,3 +1,4 @@ +use display_json::DebugAsJson; use ethereum_types::H160; use ethereum_types::H256; use ethereum_types::H64; @@ -32,7 +33,7 @@ const HASH_EMPTY_UNCLES: Hash = Hash::new(hex!("1dcc4de8dec75d7aab85b567b6ccd41a /// Special hash used in block mining to indicate no transaction root and no receipts root. const HASH_EMPTY_TRIE: Hash = Hash::new(hex!("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")); -#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct BlockHeader { pub number: BlockNumber, pub hash: Hash, diff --git a/src/eth/primitives/block_number.rs b/src/eth/primitives/block_number.rs index 23d591318..a181196e2 100644 --- a/src/eth/primitives/block_number.rs +++ b/src/eth/primitives/block_number.rs @@ -4,6 +4,7 @@ use std::ops::AddAssign; use std::str::FromStr; use anyhow::anyhow; +use display_json::DebugAsJson; use ethereum_types::U64; use ethers_core::utils::keccak256; use fake::Dummy; @@ -20,7 +21,7 @@ use crate::eth::primitives::Hash; use crate::gen_newtype_from; #[derive( - Debug, + DebugAsJson, derive_more::Display, Clone, Copy, diff --git a/src/eth/primitives/bytes.rs b/src/eth/primitives/bytes.rs index 8c78f4147..1eef58810 100644 --- a/src/eth/primitives/bytes.rs +++ b/src/eth/primitives/bytes.rs @@ -1,8 +1,8 @@ -use std::fmt::Debug; use std::fmt::Display; use std::ops::Deref; use std::ops::DerefMut; +use display_json::DebugAsJson; use revm::interpreter::analysis::to_analysed; use crate::alias::EthersBytes; @@ -11,7 +11,7 @@ use crate::alias::RevmBytes; use crate::alias::RevmOutput; use crate::gen_newtype_from; -#[derive(Clone, Default, Eq, PartialEq, fake::Dummy)] +#[derive(DebugAsJson, Clone, Default, Eq, PartialEq, fake::Dummy)] pub struct Bytes(pub Vec); impl Display for Bytes { @@ -24,12 +24,6 @@ impl Display for Bytes { } } -impl Debug for Bytes { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_tuple("Bytes").field(&self.to_string()).finish() - } -} - // ----------------------------------------------------------------------------- // Serialization / Deserialization // ----------------------------------------------------------------------------- diff --git a/src/eth/primitives/call_input.rs b/src/eth/primitives/call_input.rs index ad383735d..727bf4e35 100644 --- a/src/eth/primitives/call_input.rs +++ b/src/eth/primitives/call_input.rs @@ -1,8 +1,10 @@ +use display_json::DebugAsJson; + use crate::eth::primitives::Address; use crate::eth::primitives::Bytes; use crate::eth::primitives::Wei; -#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] pub struct CallInput { #[serde(rename = "from")] pub from: Option
, diff --git a/src/eth/primitives/chain_id.rs b/src/eth/primitives/chain_id.rs index 0f3d8c95a..8167d0783 100644 --- a/src/eth/primitives/chain_id.rs +++ b/src/eth/primitives/chain_id.rs @@ -1,4 +1,5 @@ use anyhow::anyhow; +use display_json::DebugAsJson; use ethereum_types::U256; use ethereum_types::U64; use fake::Dummy; @@ -7,7 +8,7 @@ use fake::Faker; use crate::gen_newtype_from; use crate::gen_newtype_try_from; -#[derive(Debug, derive_more::Display, Clone, Copy, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)] pub struct ChainId(pub U64); impl ChainId { diff --git a/src/eth/primitives/code_hash.rs b/src/eth/primitives/code_hash.rs index 82b49b477..95537616d 100644 --- a/src/eth/primitives/code_hash.rs +++ b/src/eth/primitives/code_hash.rs @@ -1,3 +1,4 @@ +use display_json::DebugAsJson; use ethereum_types::H256; use ethers_core::utils::keccak256; use fake::Dummy; @@ -11,7 +12,7 @@ use crate::gen_newtype_from; /// Digest of the bytecode of a contract. /// In the case of an externally-owned account (EOA), bytecode is null /// and the code hash is fixed as the keccak256 hash of an empty string -#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct CodeHash(pub H256); impl Dummy for CodeHash { diff --git a/src/eth/primitives/difficulty.rs b/src/eth/primitives/difficulty.rs index e6c071ed8..fd5db9357 100644 --- a/src/eth/primitives/difficulty.rs +++ b/src/eth/primitives/difficulty.rs @@ -1,10 +1,11 @@ +use display_json::DebugAsJson; use ethereum_types::U256; use fake::Dummy; use fake::Faker; use crate::gen_newtype_from; -#[derive(Debug, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(transparent)] pub struct Difficulty(U256); diff --git a/src/eth/primitives/execution_result.rs b/src/eth/primitives/execution_result.rs index e1387c528..ecca03352 100644 --- a/src/eth/primitives/execution_result.rs +++ b/src/eth/primitives/execution_result.rs @@ -1,5 +1,7 @@ +use display_json::DebugAsJson; + /// Indicates how a transaction execution was finished. -#[derive(Debug, strum::Display, Clone, PartialEq, Eq, fake::Dummy, derive_new::new, serde::Serialize, serde::Deserialize, strum::EnumString)] +#[derive(DebugAsJson, strum::Display, Clone, PartialEq, Eq, fake::Dummy, derive_new::new, serde::Serialize, serde::Deserialize, strum::EnumString)] #[serde(rename_all = "snake_case")] pub enum ExecutionResult { /// Finished normally (RETURN opcode). diff --git a/src/eth/primitives/execution_value_change.rs b/src/eth/primitives/execution_value_change.rs index a3485e741..71cd03ff8 100644 --- a/src/eth/primitives/execution_value_change.rs +++ b/src/eth/primitives/execution_value_change.rs @@ -1,54 +1,35 @@ +// ----------------------------------------------------------------------------- +// Value Change +// ----------------------------------------------------------------------------- + +use std::fmt::Debug; + +use display_json::DebugAsJson; + +use crate::ext::to_json_string; + /// Changes that happened to an account value during a transaction. -#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] +#[derive(Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] pub struct ExecutionValueChange where - T: PartialEq, + T: PartialEq + serde::Serialize, { original: ValueState, modified: ValueState, } -impl Default for ExecutionValueChange { - fn default() -> Self { - Self { - original: ValueState::NotSet, - modified: ValueState::NotSet, - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum ValueState { - Set(T), - NotSet, -} - -impl ValueState { - pub fn is_set(&self) -> bool { - matches!(self, Self::Set(_)) - } - - pub fn take(self) -> Option { - if let Self::Set(value) = self { - Some(value) - } else { - None - } - } - - pub fn take_ref(&self) -> Option<&T> { - if let Self::Set(value) = self { - Some(value) - } else { - None - } +impl Debug for ExecutionValueChange +where + T: PartialEq + serde::Serialize, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&to_json_string(self)) } } impl ExecutionValueChange where - T: PartialEq, + T: PartialEq + serde::Serialize, { /// Creates a new [`ExecutionValueChange`] only with original value. pub fn from_original(value: T) -> Self { @@ -111,3 +92,36 @@ where self.modified.is_set() && (self.original != self.modified) } } + +// ----------------------------------------------------------------------------- +// Value State +// ----------------------------------------------------------------------------- + +#[derive(DebugAsJson, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ValueState { + Set(T), + NotSet, +} + +impl ValueState { + pub fn is_set(&self) -> bool { + matches!(self, Self::Set(_)) + } + + pub fn take(self) -> Option { + if let Self::Set(value) = self { + Some(value) + } else { + None + } + } + + pub fn take_ref(&self) -> Option<&T> { + if let Self::Set(value) = self { + Some(value) + } else { + None + } + } +} diff --git a/src/eth/primitives/gas.rs b/src/eth/primitives/gas.rs index 5c57bdbac..2e477c7f0 100644 --- a/src/eth/primitives/gas.rs +++ b/src/eth/primitives/gas.rs @@ -1,4 +1,5 @@ use anyhow::anyhow; +use display_json::DebugAsJson; use ethereum_types::U256; use ethereum_types::U64; use fake::Dummy; @@ -7,7 +8,7 @@ use fake::Faker; use crate::gen_newtype_from; use crate::gen_newtype_try_from; -#[derive(Debug, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(transparent)] pub struct Gas(U64); diff --git a/src/eth/primitives/hash.rs b/src/eth/primitives/hash.rs index 481aa2989..37e70c2a5 100644 --- a/src/eth/primitives/hash.rs +++ b/src/eth/primitives/hash.rs @@ -2,6 +2,7 @@ use std::fmt::Display; use std::str::FromStr; use anyhow::anyhow; +use display_json::DebugAsJson; use ethereum_types::H256; use fake::Dummy; use fake::Faker; @@ -11,7 +12,7 @@ use sqlx::postgres::PgHasArrayType; use crate::gen_newtype_from; -#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] #[serde(transparent)] pub struct Hash(pub H256); diff --git a/src/eth/primitives/index.rs b/src/eth/primitives/index.rs index ac060b91e..8151f2379 100644 --- a/src/eth/primitives/index.rs +++ b/src/eth/primitives/index.rs @@ -1,5 +1,6 @@ use std::num::TryFromIntError; +use display_json::DebugAsJson; use ethereum_types::U256; use ethereum_types::U64; @@ -8,7 +9,7 @@ use crate::gen_newtype_try_from; /// Represents a transaction index or log index. #[derive( - Debug, derive_more::Display, Clone, Copy, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize, derive_more::Add, Hash, PartialOrd, Ord, + DebugAsJson, derive_more::Display, Clone, Copy, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize, derive_more::Add, Hash, PartialOrd, Ord, )] pub struct Index(pub u64); diff --git a/src/eth/primitives/log.rs b/src/eth/primitives/log.rs index be121b958..74d9e7878 100644 --- a/src/eth/primitives/log.rs +++ b/src/eth/primitives/log.rs @@ -1,3 +1,5 @@ +use display_json::DebugAsJson; + use crate::alias::EthersLog; use crate::alias::RevmLog; use crate::eth::primitives::Address; @@ -5,7 +7,7 @@ use crate::eth::primitives::Bytes; use crate::eth::primitives::LogTopic; /// Log is an event emitted by the EVM during contract execution. -#[derive(Debug, Clone, Default, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, Default, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] pub struct Log { /// Address that emitted the log. pub address: Address, diff --git a/src/eth/primitives/log_mined.rs b/src/eth/primitives/log_mined.rs index 2c2dee60f..8704edd34 100644 --- a/src/eth/primitives/log_mined.rs +++ b/src/eth/primitives/log_mined.rs @@ -1,3 +1,4 @@ +use display_json::DebugAsJson; use itertools::Itertools; use jsonrpsee::SubscriptionMessage; @@ -12,7 +13,7 @@ use crate::eth::primitives::LogTopic; use crate::ext::to_json_value; /// Log that was emitted by the EVM and added to a block. -#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)] pub struct LogMined { /// Original log emitted by the EVM. pub log: Log, diff --git a/src/eth/primitives/log_topic.rs b/src/eth/primitives/log_topic.rs index 041e94785..f9ae99db0 100644 --- a/src/eth/primitives/log_topic.rs +++ b/src/eth/primitives/log_topic.rs @@ -1,5 +1,6 @@ use std::fmt::Display; +use display_json::DebugAsJson; use ethereum_types::H256; use fake::Dummy; use fake::Faker; @@ -8,7 +9,7 @@ use crate::alias::RevmB256; use crate::gen_newtype_from; /// Topic is part of a [`Log`](super::Log) emitted by the EVM during contract execution. -#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default, Hash)] +#[derive(DebugAsJson, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default, Hash)] pub struct LogTopic(pub H256); impl LogTopic { diff --git a/src/eth/primitives/miner_nonce.rs b/src/eth/primitives/miner_nonce.rs index 0d4633abe..f113736eb 100644 --- a/src/eth/primitives/miner_nonce.rs +++ b/src/eth/primitives/miner_nonce.rs @@ -1,3 +1,4 @@ +use display_json::DebugAsJson; use ethereum_types::H64; use fake::Dummy; use fake::Faker; @@ -5,7 +6,7 @@ use fake::Faker; use crate::gen_newtype_from; /// The nonce of an Ethereum block. -#[derive(Debug, derive_more::Display, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)] pub struct MinerNonce(H64); impl MinerNonce { diff --git a/src/eth/primitives/nonce.rs b/src/eth/primitives/nonce.rs index df8871501..de2d943bd 100644 --- a/src/eth/primitives/nonce.rs +++ b/src/eth/primitives/nonce.rs @@ -1,14 +1,7 @@ -//! Nonce Module -//! -//! Manages nonces in Ethereum, which are crucial for preventing transaction -//! replay attacks. A nonce is a unique number assigned to each transaction sent -//! by an account, ensuring each transaction is processed once. This module -//! offers functionalities to create, manage, and convert nonces, maintaining -//! the integrity and uniqueness of transactions in the network. - use std::str::FromStr; use anyhow::anyhow; +use display_json::DebugAsJson; use ethereum_types::U256; use ethereum_types::U64; use fake::Dummy; @@ -17,7 +10,7 @@ use fake::Faker; use crate::gen_newtype_from; use crate::gen_newtype_try_from; -#[derive(Debug, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Nonce(U64); impl Nonce { diff --git a/src/eth/primitives/size.rs b/src/eth/primitives/size.rs index 9a8065ddc..9a01b596d 100644 --- a/src/eth/primitives/size.rs +++ b/src/eth/primitives/size.rs @@ -1,4 +1,5 @@ use anyhow::anyhow; +use display_json::DebugAsJson; use ethereum_types::U256; use ethereum_types::U64; use fake::Dummy; @@ -6,7 +7,7 @@ use fake::Faker; use crate::gen_newtype_from; -#[derive(Debug, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(transparent)] pub struct Size(U64); diff --git a/src/eth/primitives/slot_index.rs b/src/eth/primitives/slot_index.rs index 64b55828d..758003f20 100644 --- a/src/eth/primitives/slot_index.rs +++ b/src/eth/primitives/slot_index.rs @@ -1,8 +1,8 @@ -use std::fmt::Debug; use std::fmt::Display; use std::io::Read; use std::str::FromStr; +use display_json::DebugAsJson; use ethereum_types::U256; use ethers_core::utils::keccak256; use fake::Dummy; @@ -17,7 +17,7 @@ use sqlx::Decode; use crate::alias::RevmU256; use crate::gen_newtype_from; -#[derive(Clone, Copy, Default, Hash, Eq, PartialEq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, Copy, Default, Hash, Eq, PartialEq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)] pub struct SlotIndex(U256); impl SlotIndex { @@ -61,12 +61,6 @@ impl Display for SlotIndex { } } -impl Debug for SlotIndex { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "SlotIndex({:#x})", self.0) - } -} - // ----------------------------------------------------------------------------- // Conversions: Other -> Self // ----------------------------------------------------------------------------- diff --git a/src/eth/primitives/slot_value.rs b/src/eth/primitives/slot_value.rs index aa035326b..35049c090 100644 --- a/src/eth/primitives/slot_value.rs +++ b/src/eth/primitives/slot_value.rs @@ -1,6 +1,7 @@ use std::fmt::Display; use std::str::FromStr; +use display_json::DebugAsJson; use ethereum_types::U256; use fake::Dummy; use fake::Faker; @@ -14,7 +15,7 @@ use sqlx::Decode; use crate::alias::RevmU256; use crate::gen_newtype_from; -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct SlotValue(U256); impl SlotValue { diff --git a/src/eth/primitives/unix_time.rs b/src/eth/primitives/unix_time.rs index 7dc471721..7840daef6 100644 --- a/src/eth/primitives/unix_time.rs +++ b/src/eth/primitives/unix_time.rs @@ -3,13 +3,14 @@ use std::ops::Deref; use std::str::FromStr; use chrono::Utc; +use display_json::DebugAsJson; use ethereum_types::U256; use fake::Dummy; use fake::Faker; use crate::alias::RevmU256; -#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[derive(DebugAsJson, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct UnixTime(u64); impl UnixTime { diff --git a/src/eth/primitives/wei.rs b/src/eth/primitives/wei.rs index 7bdc6d287..4df614a64 100644 --- a/src/eth/primitives/wei.rs +++ b/src/eth/primitives/wei.rs @@ -1,5 +1,6 @@ use std::str::FromStr; +use display_json::DebugAsJson; use ethabi::Token; use ethereum_types::U256; use fake::Dummy; @@ -17,7 +18,19 @@ use crate::gen_newtype_from; /// Native token amount in wei. #[derive( - Debug, derive_more::Display, Clone, Copy, Default, PartialOrd, Ord, PartialEq, Eq, derive_more::Add, derive_more::Sub, serde::Serialize, serde::Deserialize, + DebugAsJson, + derive_more::Display, + Clone, + Copy, + Default, + PartialOrd, + Ord, + PartialEq, + Eq, + derive_more::Add, + derive_more::Sub, + serde::Serialize, + serde::Deserialize, )] pub struct Wei(pub U256); diff --git a/src/ext.rs b/src/ext.rs index 4abcbb761..ab4a6454c 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -304,6 +304,14 @@ pub fn from_json_str(s: &str) -> T { macro_rules! gen_test_serde { ($type:ty) => { paste::paste! { + #[test] + pub fn []() { + let original = ::fake::<$type>(&fake::Faker); + let encoded_json = serde_json::to_string(&original).unwrap(); + let encoded_debug = format!("{:?}", original); + assert_eq!(encoded_json, encoded_debug); + } + #[test] pub fn []() { // encode