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

enha: ensure all primitives implement DebugAsJson #1570

Merged
merged 2 commits into from
Jul 30, 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
3 changes: 2 additions & 1 deletion src/eth/primitives/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/block_header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use display_json::DebugAsJson;
use ethereum_types::H160;
use ethereum_types::H256;
use ethereum_types::H64;
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +21,7 @@ use crate::eth::primitives::Hash;
use crate::gen_newtype_from;

#[derive(
Debug,
DebugAsJson,
derive_more::Display,
Clone,
Copy,
Expand Down
10 changes: 2 additions & 8 deletions src/eth/primitives/bytes.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<u8>);

impl Display for Bytes {
Expand All @@ -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
// -----------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion src/eth/primitives/call_input.rs
Original file line number Diff line number Diff line change
@@ -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<Address>,
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/chain_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::anyhow;
use display_json::DebugAsJson;
use ethereum_types::U256;
use ethereum_types::U64;
use fake::Dummy;
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/code_hash.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use display_json::DebugAsJson;
use ethereum_types::H256;
use ethers_core::utils::keccak256;
use fake::Dummy;
Expand All @@ -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<Faker> for CodeHash {
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/difficulty.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/eth/primitives/execution_result.rs
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
90 changes: 52 additions & 38 deletions src/eth/primitives/execution_value_change.rs
Original file line number Diff line number Diff line change
@@ -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<T>
where
T: PartialEq,
T: PartialEq + serde::Serialize,
{
original: ValueState<T>,
modified: ValueState<T>,
}

impl<T: PartialEq> Default for ExecutionValueChange<T> {
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<T> {
Set(T),
NotSet,
}

impl<T> ValueState<T> {
pub fn is_set(&self) -> bool {
matches!(self, Self::Set(_))
}

pub fn take(self) -> Option<T> {
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<T> Debug for ExecutionValueChange<T>
where
T: PartialEq + serde::Serialize,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&to_json_string(self))
}
}

impl<T> ExecutionValueChange<T>
where
T: PartialEq,
T: PartialEq + serde::Serialize,
{
/// Creates a new [`ExecutionValueChange`] only with original value.
pub fn from_original(value: T) -> Self {
Expand Down Expand Up @@ -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<T> {
Set(T),
NotSet,
}

impl<T> ValueState<T> {
pub fn is_set(&self) -> bool {
matches!(self, Self::Set(_))
}

pub fn take(self) -> Option<T> {
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
}
}
}
3 changes: 2 additions & 1 deletion src/eth/primitives/gas.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::anyhow;
use display_json::DebugAsJson;
use ethereum_types::U256;
use ethereum_types::U64;
use fake::Dummy;
Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::num::TryFromIntError;

use display_json::DebugAsJson;
use ethereum_types::U256;
use ethereum_types::U64;

Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/eth/primitives/log.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use display_json::DebugAsJson;

use crate::alias::EthersLog;
use crate::alias::RevmLog;
use crate::eth::primitives::Address;
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,
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/log_mined.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use display_json::DebugAsJson;
use itertools::Itertools;
use jsonrpsee::SubscriptionMessage;

Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/log_topic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Display;

use display_json::DebugAsJson;
use ethereum_types::H256;
use fake::Dummy;
use fake::Faker;
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/eth/primitives/miner_nonce.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use display_json::DebugAsJson;
use ethereum_types::H64;
use fake::Dummy;
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 {
Expand Down
11 changes: 2 additions & 9 deletions src/eth/primitives/nonce.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
Loading