From 53cb34db8634ed4dbc7a1694da117350b9f90b59 Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 15 Jul 2024 12:59:20 +0100 Subject: [PATCH 1/3] refactor: replace `U64` with `u64` in rpc-types-(eth|trace) --- crates/rpc-types-eth/src/account.rs | 6 ++-- crates/rpc-types-eth/src/state.rs | 8 ++--- .../rpc-types-eth/src/transaction/optimism.rs | 10 +++--- crates/rpc-types-trace/src/parity.rs | 31 +++++++++++-------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/crates/rpc-types-eth/src/account.rs b/crates/rpc-types-eth/src/account.rs index 26f6c9497ea..10d6e9e3b16 100644 --- a/crates/rpc-types-eth/src/account.rs +++ b/crates/rpc-types-eth/src/account.rs @@ -1,7 +1,6 @@ -use alloy_primitives::{Address, Bytes, B256, B512, U256, U64}; +use alloy_primitives::{Address, Bytes, B256, B512, U256}; use alloy_serde::storage::JsonStorageKey; use serde::{Deserialize, Serialize}; - /// Account information. #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct AccountInfo { @@ -32,7 +31,8 @@ pub struct EIP1186AccountProofResponse { /// The hash of the code of the account. pub code_hash: B256, /// The account nonce. - pub nonce: U64, + #[serde(with = "alloy_serde::quantity")] + pub nonce: u64, /// The hash of the storage of the account. pub storage_hash: B256, /// The account proof. diff --git a/crates/rpc-types-eth/src/state.rs b/crates/rpc-types-eth/src/state.rs index 89a7fbf7671..4746e5368da 100644 --- a/crates/rpc-types-eth/src/state.rs +++ b/crates/rpc-types-eth/src/state.rs @@ -1,7 +1,7 @@ //! bindings for state overrides in eth_call use crate::BlockOverrides; -use alloy_primitives::{Address, Bytes, B256, U256, U64}; +use alloy_primitives::{Address, Bytes, B256, U256}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -16,8 +16,8 @@ pub struct AccountOverride { #[serde(default, skip_serializing_if = "Option::is_none")] pub balance: Option, /// Fake nonce to set for the account before executing the call. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub nonce: Option, + #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")] + pub nonce: Option, /// Fake EVM bytecode to inject into the account before executing the call. #[serde(default, skip_serializing_if = "Option::is_none")] pub code: Option, @@ -113,7 +113,7 @@ mod tests { let acc = state_override.get(&address!("1234567890123456789012345678901234567890")).unwrap(); assert_eq!(acc.balance, Some(U256::MAX)); - assert_eq!(acc.nonce, Some(U64::MAX)); + assert_eq!(acc.nonce, Some(u64::MAX)); } #[test] diff --git a/crates/rpc-types-eth/src/transaction/optimism.rs b/crates/rpc-types-eth/src/transaction/optimism.rs index eac57944fdd..6b40a3a4b6b 100644 --- a/crates/rpc-types-eth/src/transaction/optimism.rs +++ b/crates/rpc-types-eth/src/transaction/optimism.rs @@ -1,6 +1,6 @@ //! Misc Optimism-specific types. -use alloy_primitives::{B256, U128, U64}; +use alloy_primitives::{B256, U128}; use alloy_serde::OtherFields; use serde::{Deserialize, Serialize}; @@ -27,11 +27,11 @@ pub struct OptimismTransactionFields { #[doc(alias = "OptimismTxReceiptFields")] pub struct OptimismTransactionReceiptFields { /// Deposit nonce for deposit transactions post-regolith - #[serde(skip_serializing_if = "Option::is_none")] - pub deposit_nonce: Option, + #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")] + pub deposit_nonce: Option, /// Deposit receipt version for deposit transactions post-canyon - #[serde(skip_serializing_if = "Option::is_none")] - pub deposit_receipt_version: Option, + #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")] + pub deposit_receipt_version: Option, /// L1 fee for the transaction #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")] pub l1_fee: Option, diff --git a/crates/rpc-types-trace/src/parity.rs b/crates/rpc-types-trace/src/parity.rs index e3c1ef4d539..21abea91bb2 100644 --- a/crates/rpc-types-trace/src/parity.rs +++ b/crates/rpc-types-trace/src/parity.rs @@ -133,6 +133,7 @@ pub struct AccountDiff { /// How the code changed, if at all pub code: Delta, /// How the nonce changed, if at all + // TODO: Change type to `u64` and write custom serde for `Delta` pub nonce: Delta, /// All touched/changed storage values pub storage: BTreeMap>, @@ -288,7 +289,8 @@ pub struct CallAction { /// The type of the call. pub call_type: CallType, /// The gas available for executing the call. - pub gas: U64, + #[serde(with = "alloy_serde::quantity")] + pub gas: u64, /// The input data provided to the call. pub input: Bytes, /// Address of the destination/target account. @@ -304,7 +306,8 @@ pub struct CreateAction { /// The address of the creator. pub from: Address, /// The gas available for the creation init code. - pub gas: U64, + #[serde(with = "alloy_serde::quantity")] + pub gas: u64, /// The init code. pub init: Bytes, /// The value with which the new account is endowed. @@ -350,7 +353,8 @@ pub struct SelfdestructAction { #[serde(rename_all = "camelCase")] pub struct CallOutput { /// Gas used by the call. - pub gas_used: U64, + #[serde(with = "alloy_serde::quantity")] + pub gas_used: u64, /// The output data of the call. pub output: Bytes, } @@ -364,7 +368,8 @@ pub struct CreateOutput { /// Contract code. pub code: Bytes, /// Gas used by the call. - pub gas_used: U64, + #[serde(with = "alloy_serde::quantity")] + pub gas_used: u64, } /// Represents the output of a trace. @@ -395,18 +400,18 @@ impl TraceOutput { } /// Returns the gas used by this trace. - pub fn gas_used(&self) -> u64 { + pub const fn gas_used(&self) -> u64 { match self { - Self::Call(call) => call.gas_used.to(), - Self::Create(create) => create.gas_used.to(), + Self::Call(call) => call.gas_used, + Self::Create(create) => create.gas_used, } } /// Sets the gas used by this trace. pub fn set_gas_used(&mut self, gas_used: u64) { match self { - Self::Call(call) => call.gas_used = U64::from(gas_used), - Self::Create(create) => create.gas_used = U64::from(gas_used), + Self::Call(call) => call.gas_used = gas_used, + Self::Create(create) => create.gas_used = gas_used, } } } @@ -660,13 +665,13 @@ mod tests { action: Action::Call(CallAction { from: "0x4f4495243837681061c4743b74b3eedf548d56a5".parse::
().unwrap(), call_type: CallType::DelegateCall, - gas: U64::from(3148955), + gas: 3148955, input: Bytes::from_str("0x585a9fd40000000000000000000000000000000000000000000000000000000000000040a47c5ad9a4af285720eae6cc174a9c75c5bbaf973b00f1a0c191327445b6581000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000ce16f69375520ab01377ce7b88f5ba8c48f8d666f61490331372e432315cd97447e3bc452d6c73a6e0536260a88ddab46f85c88d00000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000aab8cf0fbfb038751339cb61161fa11789b41a78f1b7b0e12cf8e467d403590b7a5f26f0000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000646616e746f6d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a3078636531364636393337353532306162303133373763653742383866354241384334384638443636360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000045553444300000000000000000000000000000000000000000000000000000000").unwrap(), to: "0x99b5fa03a5ea4315725c43346e55a6a6fbd94098".parse::
().unwrap(), value: U256::from(0), }), error: None, - result: Some(TraceOutput::Call(CallOutput { gas_used: U64::from(32364), output: Bytes::new() })), + result: Some(TraceOutput::Call(CallOutput { gas_used: 32364, output: Bytes::new() })), subtraces: 0, trace_address: vec![0, 10, 0], }, @@ -706,12 +711,12 @@ mod tests { trace: TransactionTrace { action: Action::Create(CreateAction{ from: "0x4f4495243837681061c4743b74b3eedf548d56a5".parse::
().unwrap(), - gas: U64::from(3438907), + gas: 3438907, init: Bytes::from_str("0x6080604052600160005534801561001557600080fd5b50610324806100256000396000f3fe608060405234801561001057600080fd5b50600436106100355760003560e01c8062f55d9d1461003a5780631cff79cd1461004f575b600080fd5b61004d6100483660046101da565b610079565b005b61006261005d3660046101fc565b6100bb565b60405161007092919061027f565b60405180910390f35b6002600054141561009d5760405163caa30f5560e01b815260040160405180910390fd5b600260005573ffffffffffffffffffffffffffffffffffffffff8116ff5b60006060600260005414156100e35760405163caa30f5560e01b815260040160405180910390fd5b600260005573ffffffffffffffffffffffffffffffffffffffff85163b610136576040517f6f7c43f100000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8473ffffffffffffffffffffffffffffffffffffffff16848460405161015d9291906102de565b6000604051808303816000865af19150503d806000811461019a576040519150601f19603f3d011682016040523d82523d6000602084013e61019f565b606091505b50600160005590969095509350505050565b803573ffffffffffffffffffffffffffffffffffffffff811681146101d557600080fd5b919050565b6000602082840312156101ec57600080fd5b6101f5826101b1565b9392505050565b60008060006040848603121561021157600080fd5b61021a846101b1565b9250602084013567ffffffffffffffff8082111561023757600080fd5b818601915086601f83011261024b57600080fd5b81358181111561025a57600080fd5b87602082850101111561026c57600080fd5b6020830194508093505050509250925092565b821515815260006020604081840152835180604085015260005b818110156102b557858101830151858201606001528201610299565b818111156102c7576000606083870101525b50601f01601f191692909201606001949350505050565b818382376000910190815291905056fea264697066735822122032cb5e746816b7fac95205c068b30da37bd40119a57265be331c162cae74712464736f6c63430008090033").unwrap(), value: U256::from(0), }), error: None, - result: Some(TraceOutput::Create(CreateOutput { gas_used: U64::from(183114), address: "0x7eb6c6c1db08c0b9459a68cfdcedab64f319c138".parse::
().unwrap(), code: Bytes::from_str("0x608060405234801561001057600080fd5b50600436106100355760003560e01c8062f55d9d1461003a5780631cff79cd1461004f575b600080fd5b61004d6100483660046101da565b610079565b005b61006261005d3660046101fc565b6100bb565b60405161007092919061027f565b60405180910390f35b6002600054141561009d5760405163caa30f5560e01b815260040160405180910390fd5b600260005573ffffffffffffffffffffffffffffffffffffffff8116ff5b60006060600260005414156100e35760405163caa30f5560e01b815260040160405180910390fd5b600260005573ffffffffffffffffffffffffffffffffffffffff85163b610136576040517f6f7c43f100000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8473ffffffffffffffffffffffffffffffffffffffff16848460405161015d9291906102de565b6000604051808303816000865af19150503d806000811461019a576040519150601f19603f3d011682016040523d82523d6000602084013e61019f565b606091505b50600160005590969095509350505050565b803573ffffffffffffffffffffffffffffffffffffffff811681146101d557600080fd5b919050565b6000602082840312156101ec57600080fd5b6101f5826101b1565b9392505050565b60008060006040848603121561021157600080fd5b61021a846101b1565b9250602084013567ffffffffffffffff8082111561023757600080fd5b818601915086601f83011261024b57600080fd5b81358181111561025a57600080fd5b87602082850101111561026c57600080fd5b6020830194508093505050509250925092565b821515815260006020604081840152835180604085015260005b818110156102b557858101830151858201606001528201610299565b818111156102c7576000606083870101525b50601f01601f191692909201606001949350505050565b818382376000910190815291905056fea264697066735822122032cb5e746816b7fac95205c068b30da37bd40119a57265be331c162cae74712464736f6c63430008090033").unwrap() })), + result: Some(TraceOutput::Create(CreateOutput { gas_used: 183114, address: "0x7eb6c6c1db08c0b9459a68cfdcedab64f319c138".parse::
().unwrap(), code: Bytes::from_str("0x608060405234801561001057600080fd5b50600436106100355760003560e01c8062f55d9d1461003a5780631cff79cd1461004f575b600080fd5b61004d6100483660046101da565b610079565b005b61006261005d3660046101fc565b6100bb565b60405161007092919061027f565b60405180910390f35b6002600054141561009d5760405163caa30f5560e01b815260040160405180910390fd5b600260005573ffffffffffffffffffffffffffffffffffffffff8116ff5b60006060600260005414156100e35760405163caa30f5560e01b815260040160405180910390fd5b600260005573ffffffffffffffffffffffffffffffffffffffff85163b610136576040517f6f7c43f100000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b8473ffffffffffffffffffffffffffffffffffffffff16848460405161015d9291906102de565b6000604051808303816000865af19150503d806000811461019a576040519150601f19603f3d011682016040523d82523d6000602084013e61019f565b606091505b50600160005590969095509350505050565b803573ffffffffffffffffffffffffffffffffffffffff811681146101d557600080fd5b919050565b6000602082840312156101ec57600080fd5b6101f5826101b1565b9392505050565b60008060006040848603121561021157600080fd5b61021a846101b1565b9250602084013567ffffffffffffffff8082111561023757600080fd5b818601915086601f83011261024b57600080fd5b81358181111561025a57600080fd5b87602082850101111561026c57600080fd5b6020830194508093505050509250925092565b821515815260006020604081840152835180604085015260005b818110156102b557858101830151858201606001528201610299565b818111156102c7576000606083870101525b50601f01601f191692909201606001949350505050565b818382376000910190815291905056fea264697066735822122032cb5e746816b7fac95205c068b30da37bd40119a57265be331c162cae74712464736f6c63430008090033").unwrap() })), subtraces: 0, trace_address: vec![0, 7, 0, 0], }, From cd197844fb0392bec4843f7cd078963b3a697faf Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 15 Jul 2024 13:36:46 +0100 Subject: [PATCH 2/3] replace `U64` in rpc-types-(eth|engine) --- crates/rpc-types-engine/src/transition.rs | 5 +++-- crates/rpc-types-eth/src/block.rs | 15 ++++++++++----- crates/rpc-types-eth/src/call.rs | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/rpc-types-engine/src/transition.rs b/crates/rpc-types-engine/src/transition.rs index 070ab9243a9..a63cdf3f596 100644 --- a/crates/rpc-types-engine/src/transition.rs +++ b/crates/rpc-types-engine/src/transition.rs @@ -1,4 +1,4 @@ -use alloy_primitives::{B256, U256, U64}; +use alloy_primitives::{B256, U256}; use serde::{Deserialize, Serialize}; /// This structure contains configurable settings of the transition process. @@ -11,5 +11,6 @@ pub struct TransitionConfiguration { /// Maps on TERMINAL_BLOCK_HASH parameter of EIP-3675 pub terminal_block_hash: B256, /// Maps on TERMINAL_BLOCK_NUMBER parameter of EIP-3675 - pub terminal_block_number: U64, + #[serde(with = "alloy_serde::quantity")] + pub terminal_block_number: u64, } diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index c2ff60ade05..3a549f55c82 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -1,7 +1,7 @@ //! Block RPC types. use crate::{ConversionError, Transaction, Withdrawal}; -use alloy_primitives::{Address, BlockHash, Bloom, Bytes, B256, B64, U256, U64}; +use alloy_primitives::{Address, BlockHash, Bloom, Bytes, B256, B64, U256}; use alloy_serde::OtherFields; use serde::{ser::Error, Deserialize, Serialize, Serializer}; use std::{collections::BTreeMap, ops::Deref}; @@ -608,11 +608,16 @@ pub struct BlockOverrides { pub difficulty: Option, /// Overrides the timestamp of the block. // Note: geth uses `time`, erigon uses `timestamp` - #[serde(default, skip_serializing_if = "Option::is_none", alias = "timestamp")] - pub time: Option, + #[serde( + default, + skip_serializing_if = "Option::is_none", + alias = "timestamp", + with = "alloy_serde::quantity::opt" + )] + pub time: Option, /// Overrides the gas limit of the block. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub gas_limit: Option, + #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::quantity::opt")] + pub gas_limit: Option, /// Overrides the coinbase address of the block. #[serde(default, skip_serializing_if = "Option::is_none")] pub coinbase: Option
, diff --git a/crates/rpc-types-eth/src/call.rs b/crates/rpc-types-eth/src/call.rs index 934b637e1eb..09f47660e5b 100644 --- a/crates/rpc-types-eth/src/call.rs +++ b/crates/rpc-types-eth/src/call.rs @@ -145,7 +145,7 @@ mod tests { let s = r#"{"transactions":[{"data":"0x70a08231000000000000000000000000000000dbc80bf780c6dc0ca16ed071b1f00cc000","to":"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"}],"blockOverride":{"timestamp":1711546233}}"#; let bundle = serde_json::from_str::(s).unwrap(); assert_eq!(bundle.transactions.len(), 1); - assert_eq!(bundle.block_override.unwrap().time.unwrap().to::(), 1711546233); + assert_eq!(bundle.block_override.unwrap().time.unwrap(), 1711546233); } #[test] From 3c73808e23e2ad95c1a81b0b573bc6dd9d3709db Mon Sep 17 00:00:00 2001 From: Yash Atreya <44857776+yash-atreya@users.noreply.github.com> Date: Mon, 15 Jul 2024 13:39:36 +0100 Subject: [PATCH 3/3] use u128 in OptimismTransactionFields.mint --- crates/rpc-types-eth/src/transaction/optimism.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/rpc-types-eth/src/transaction/optimism.rs b/crates/rpc-types-eth/src/transaction/optimism.rs index 6b40a3a4b6b..835806ce414 100644 --- a/crates/rpc-types-eth/src/transaction/optimism.rs +++ b/crates/rpc-types-eth/src/transaction/optimism.rs @@ -1,6 +1,6 @@ //! Misc Optimism-specific types. -use alloy_primitives::{B256, U128}; +use alloy_primitives::B256; use alloy_serde::OtherFields; use serde::{Deserialize, Serialize}; @@ -12,8 +12,13 @@ pub struct OptimismTransactionFields { #[serde(rename = "sourceHash", skip_serializing_if = "Option::is_none")] pub source_hash: Option, /// The ETH value to mint on L2 - #[serde(rename = "mint", skip_serializing_if = "Option::is_none")] - pub mint: Option, + #[serde( + default, + rename = "mint", + skip_serializing_if = "Option::is_none", + with = "alloy_serde::quantity::opt" + )] + pub mint: Option, /// Field indicating whether the transaction is a system transaction, and therefore /// exempt from the L2 gas limit. #[serde(rename = "isSystemTx", skip_serializing_if = "Option::is_none")]