From 2f7493373200b82d5b066b33d27b3d5c9a9f602f Mon Sep 17 00:00:00 2001 From: Joseph Zhao Date: Wed, 1 Jan 2025 12:32:21 +0800 Subject: [PATCH 1/4] change type --- crates/provider/src/blocks.rs | 5 ++- crates/provider/src/ext/anvil.rs | 48 ++++++++++++++-------------- crates/provider/src/fillers/nonce.rs | 2 +- crates/rpc-types-anvil/src/lib.rs | 6 ++-- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/crates/provider/src/blocks.rs b/crates/provider/src/blocks.rs index 05d095fa7d3..5211d7fde0f 100644 --- a/crates/provider/src/blocks.rs +++ b/crates/provider/src/blocks.rs @@ -182,7 +182,6 @@ mod tests { use super::*; use crate::{ext::AnvilApi, Provider, ProviderBuilder}; use alloy_node_bindings::Anvil; - use alloy_primitives::U256; use std::{future::Future, time::Duration}; async fn timeout(future: T) -> T::Output { @@ -215,7 +214,7 @@ mod tests { } // We will also use provider to manipulate anvil instance via RPC. - provider.anvil_mine(Some(U256::from(1)), None).await.unwrap(); + provider.anvil_mine(Some(1), None).await.unwrap(); let block = timeout(stream.next()).await.expect("Block wasn't fetched"); assert_eq!(block.header.number, 1); @@ -246,7 +245,7 @@ mod tests { } // We will also use provider to manipulate anvil instance via RPC. - provider.anvil_mine(Some(U256::from(BLOCKS_TO_MINE)), None).await.unwrap(); + provider.anvil_mine(Some(BLOCKS_TO_MINE as u64), None).await.unwrap(); let blocks = timeout(stream.take(BLOCKS_TO_MINE).collect::>()).await; assert_eq!(blocks.len(), BLOCKS_TO_MINE); diff --git a/crates/provider/src/ext/anvil.rs b/crates/provider/src/ext/anvil.rs index dfb411331fd..545ad540e15 100644 --- a/crates/provider/src/ext/anvil.rs +++ b/crates/provider/src/ext/anvil.rs @@ -34,8 +34,8 @@ pub trait AnvilApi: Send + Sync { /// Mines a series of blocks. async fn anvil_mine( &self, - num_blocks: Option, - interval: Option, + num_blocks: Option, + interval: Option, ) -> TransportResult<()>; /// Sets the mining behavior to interval with the given interval (seconds). @@ -62,7 +62,7 @@ pub trait AnvilApi: Send + Sync { async fn anvil_set_code(&self, address: Address, code: Bytes) -> TransportResult<()>; /// Sets the nonce of an address. - async fn anvil_set_nonce(&self, address: Address, nonce: U256) -> TransportResult<()>; + async fn anvil_set_nonce(&self, address: Address, nonce: u64) -> TransportResult<()>; /// Writes a single slot of the account's storage. async fn anvil_set_storage_at( @@ -76,10 +76,10 @@ pub trait AnvilApi: Send + Sync { async fn anvil_set_logging(&self, enable: bool) -> TransportResult<()>; /// Set the minimum gas price for the node. - async fn anvil_set_min_gas_price(&self, gas: U256) -> TransportResult<()>; + async fn anvil_set_min_gas_price(&self, gas: u128) -> TransportResult<()>; /// Sets the base fee of the next block. - async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: U256) -> TransportResult<()>; + async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: u128) -> TransportResult<()>; /// Sets the coinbase address. async fn anvil_set_coinbase(&self, address: Address) -> TransportResult<()>; @@ -109,7 +109,7 @@ pub trait AnvilApi: Send + Sync { async fn anvil_revert(&self, id: U256) -> TransportResult; /// Jump forward in time by the given amount of time, in seconds. - async fn anvil_increase_time(&self, seconds: U256) -> TransportResult; + async fn anvil_increase_time(&self, seconds: u64) -> TransportResult; /// Similar to `evm_increaseTime` but takes the exact timestamp that you want in the next block. async fn anvil_set_next_block_timestamp(&self, timestamp: u64) -> TransportResult<()>; @@ -119,7 +119,7 @@ pub trait AnvilApi: Send + Sync { async fn anvil_set_time(&self, timestamp: u64) -> TransportResult; /// Set the next block gas limit. - async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> TransportResult; + async fn anvil_set_block_gas_limit(&self, gas_limit: u64) -> TransportResult; /// Sets an interval for the block timestamp. async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> TransportResult<()>; @@ -178,8 +178,8 @@ where async fn anvil_mine( &self, - num_blocks: Option, - interval: Option, + num_blocks: Option, + interval: Option, ) -> TransportResult<()> { self.client().request("anvil_mine", (num_blocks, interval)).await } @@ -212,7 +212,7 @@ where self.client().request("anvil_setCode", (address, code)).await } - async fn anvil_set_nonce(&self, address: Address, nonce: U256) -> TransportResult<()> { + async fn anvil_set_nonce(&self, address: Address, nonce: u64) -> TransportResult<()> { self.client().request("anvil_setNonce", (address, nonce)).await } @@ -229,11 +229,11 @@ where self.client().request("anvil_setLoggingEnabled", (enable,)).await } - async fn anvil_set_min_gas_price(&self, gas: U256) -> TransportResult<()> { + async fn anvil_set_min_gas_price(&self, gas: u128) -> TransportResult<()> { self.client().request("anvil_setMinGasPrice", (gas,)).await } - async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: U256) -> TransportResult<()> { + async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: u128) -> TransportResult<()> { self.client().request("anvil_setNextBlockBaseFeePerGas", (basefee,)).await } @@ -269,7 +269,7 @@ where self.client().request("evm_revert", (id,)).await } - async fn anvil_increase_time(&self, seconds: U256) -> TransportResult { + async fn anvil_increase_time(&self, seconds: u64) -> TransportResult { self.client().request("evm_increaseTime", (seconds,)).await } @@ -281,7 +281,7 @@ where self.client().request("evm_setTime", (timestamp,)).await } - async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> TransportResult { + async fn anvil_set_block_gas_limit(&self, gas_limit: u64) -> TransportResult { self.client().request("evm_setBlockGasLimit", (gas_limit,)).await } @@ -424,7 +424,7 @@ mod tests { let start_num = provider.get_block_number().await.unwrap(); - provider.anvil_mine(Some(U256::from(10)), None).await.unwrap(); + provider.anvil_mine(Some(10 as u64), None).await.unwrap(); let num = provider.get_block_number().await.unwrap(); @@ -566,11 +566,11 @@ mod tests { let provider = ProviderBuilder::new().on_anvil(); let address = Address::random(); - let nonce = U256::from(1337); + let nonce = 1337; provider.anvil_set_nonce(address, nonce).await.unwrap(); let new_nonce = provider.get_transaction_count(address).await.unwrap(); - assert_eq!(new_nonce, nonce.to::()); + assert_eq!(new_nonce, nonce); } #[tokio::test] @@ -599,7 +599,7 @@ mod tests { let gas = U256::from(1337); - if let Err(e) = provider.anvil_set_min_gas_price(gas).await { + if let Err(e) = provider.anvil_set_min_gas_price(gas.try_into().unwrap()).await { assert_eq!( e.to_string(), "server returned an error response: error code -32602: anvil_setMinGasPrice is not supported when EIP-1559 is active" @@ -611,7 +611,7 @@ mod tests { async fn test_anvil_set_next_block_base_fee_per_gas() { let provider = ProviderBuilder::new().on_anvil(); - let basefee = U256::from(1337); + let basefee = 1337; provider.anvil_set_next_block_base_fee_per_gas(basefee).await.unwrap(); provider.evm_mine(None).await.unwrap(); @@ -622,7 +622,7 @@ mod tests { .unwrap() .unwrap(); - assert_eq!(block.header.base_fee_per_gas, Some(basefee.to::())); + assert_eq!(block.header.base_fee_per_gas, Some(basefee as u64)); } #[tokio::test] @@ -762,7 +762,7 @@ mod tests { .header .timestamp; - let seconds = provider.anvil_increase_time(U256::from(1337)).await.unwrap(); + let seconds = provider.anvil_increase_time(1337).await.unwrap(); assert_eq!(timestamp as i64 + seconds, timestamp as i64 + 1337_i64); } @@ -806,7 +806,7 @@ mod tests { async fn test_anvil_set_block_gas_limit() { let provider = ProviderBuilder::new().on_anvil(); - let block_gas_limit = U256::from(1337); + let block_gas_limit = 1337; assert!(provider.anvil_set_block_gas_limit(block_gas_limit).await.unwrap()); provider.evm_mine(None).await.unwrap(); @@ -816,7 +816,7 @@ mod tests { .await .unwrap() .unwrap(); - assert_eq!(block_gas_limit.to::(), latest_block.header.gas_limit); + assert_eq!(block_gas_limit, latest_block.header.gas_limit); } #[tokio::test] @@ -952,7 +952,7 @@ mod tests { let provider = ProviderBuilder::new().on_anvil(); // Mine two blocks - provider.anvil_mine(Some(U256::from(2)), None).await.unwrap(); + provider.anvil_mine(Some(2 as u64), None).await.unwrap(); let reorged_block = provider .get_block_by_number(2.into(), BlockTransactionsKind::Hashes) diff --git a/crates/provider/src/fillers/nonce.rs b/crates/provider/src/fillers/nonce.rs index 2145bc35356..18239e5f1ac 100644 --- a/crates/provider/src/fillers/nonce.rs +++ b/crates/provider/src/fillers/nonce.rs @@ -204,7 +204,7 @@ mod tests { { use crate::ext::AnvilApi; filler.nonce_manager.nonces.clear(); - provider.anvil_set_nonce(address, U256::from(69)).await.unwrap(); + provider.anvil_set_nonce(address, 69).await.unwrap(); check_nonces(&filler, &provider, address, 69).await; } } diff --git a/crates/rpc-types-anvil/src/lib.rs b/crates/rpc-types-anvil/src/lib.rs index 8bbd90dcbb9..af0c6c22c85 100644 --- a/crates/rpc-types-anvil/src/lib.rs +++ b/crates/rpc-types-anvil/src/lib.rs @@ -85,13 +85,13 @@ pub struct NodeInfo { #[serde(rename_all = "camelCase")] pub struct NodeEnvironment { /// Base fee of the current block - pub base_fee: U256, + pub base_fee: u128, /// Chain id of the node. pub chain_id: ChainId, /// Configured block gas limit - pub gas_limit: U256, + pub gas_limit: u64, /// Configured gas price - pub gas_price: U256, + pub gas_price: u128, } /// The node's fork configuration. From 93823115dc0786efa41a11fa4b2851854b44fbba Mon Sep 17 00:00:00 2001 From: Joseph Zhao Date: Wed, 1 Jan 2025 13:46:09 +0800 Subject: [PATCH 2/4] fix clippy --- crates/provider/src/ext/anvil.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/provider/src/ext/anvil.rs b/crates/provider/src/ext/anvil.rs index 545ad540e15..fa2b959d16c 100644 --- a/crates/provider/src/ext/anvil.rs +++ b/crates/provider/src/ext/anvil.rs @@ -424,7 +424,7 @@ mod tests { let start_num = provider.get_block_number().await.unwrap(); - provider.anvil_mine(Some(10 as u64), None).await.unwrap(); + provider.anvil_mine(Some(10), None).await.unwrap(); let num = provider.get_block_number().await.unwrap(); @@ -952,7 +952,7 @@ mod tests { let provider = ProviderBuilder::new().on_anvil(); // Mine two blocks - provider.anvil_mine(Some(2 as u64), None).await.unwrap(); + provider.anvil_mine(Some(2), None).await.unwrap(); let reorged_block = provider .get_block_by_number(2.into(), BlockTransactionsKind::Hashes) From 3f381e8d9dfd99d7d320cf6c73b110728dbca5ab Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 7 Jan 2025 17:09:58 +0100 Subject: [PATCH 3/4] fix: keep serialization the same --- crates/provider/src/ext/anvil.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/provider/src/ext/anvil.rs b/crates/provider/src/ext/anvil.rs index 5fecc35d625..8668c2cf0d7 100644 --- a/crates/provider/src/ext/anvil.rs +++ b/crates/provider/src/ext/anvil.rs @@ -2,7 +2,7 @@ use crate::Provider; use alloy_network::Network; -use alloy_primitives::{Address, Bytes, TxHash, B256, U256}; +use alloy_primitives::{Address, Bytes, TxHash, B256, U128, U256, U64}; use alloy_rpc_types_anvil::{Forking, Metadata, MineOptions, NodeInfo, ReorgOptions}; use alloy_rpc_types_eth::Block; use alloy_transport::TransportResult; @@ -180,7 +180,9 @@ where num_blocks: Option, interval: Option, ) -> TransportResult<()> { - self.client().request("anvil_mine", (num_blocks, interval)).await + self.client() + .request("anvil_mine", (num_blocks.map(U64::from), interval.map(U64::from))) + .await } async fn anvil_set_interval_mining(&self, secs: u64) -> TransportResult<()> { @@ -212,7 +214,7 @@ where } async fn anvil_set_nonce(&self, address: Address, nonce: u64) -> TransportResult<()> { - self.client().request("anvil_setNonce", (address, nonce)).await + self.client().request("anvil_setNonce", (address, U64::from(nonce))).await } async fn anvil_set_storage_at( @@ -229,11 +231,11 @@ where } async fn anvil_set_min_gas_price(&self, gas: u128) -> TransportResult<()> { - self.client().request("anvil_setMinGasPrice", (gas,)).await + self.client().request("anvil_setMinGasPrice", (U128::from(gas),)).await } async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: u128) -> TransportResult<()> { - self.client().request("anvil_setNextBlockBaseFeePerGas", (basefee,)).await + self.client().request("anvil_setNextBlockBaseFeePerGas", (U128::from(basefee),)).await } async fn anvil_set_coinbase(&self, address: Address) -> TransportResult<()> { @@ -269,7 +271,7 @@ where } async fn anvil_increase_time(&self, seconds: u64) -> TransportResult { - self.client().request("evm_increaseTime", (seconds,)).await + self.client().request("evm_increaseTime", (U64::from(seconds),)).await } async fn anvil_set_next_block_timestamp(&self, seconds: u64) -> TransportResult<()> { @@ -281,7 +283,7 @@ where } async fn anvil_set_block_gas_limit(&self, gas_limit: u64) -> TransportResult { - self.client().request("evm_setBlockGasLimit", (gas_limit,)).await + self.client().request("evm_setBlockGasLimit", (U64::from(gas_limit),)).await } async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> TransportResult<()> { From e55c653168a443417f65143b1f968fba07bdec84 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 7 Jan 2025 17:09:58 +0100 Subject: [PATCH 4/4] fix: keep serialization the same --- crates/rpc-types-anvil/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/rpc-types-anvil/src/lib.rs b/crates/rpc-types-anvil/src/lib.rs index af0c6c22c85..59c9ed342de 100644 --- a/crates/rpc-types-anvil/src/lib.rs +++ b/crates/rpc-types-anvil/src/lib.rs @@ -85,12 +85,15 @@ pub struct NodeInfo { #[serde(rename_all = "camelCase")] pub struct NodeEnvironment { /// Base fee of the current block + #[serde(with = "alloy_serde::quantity")] pub base_fee: u128, /// Chain id of the node. pub chain_id: ChainId, /// Configured block gas limit + #[serde(with = "alloy_serde::quantity")] pub gas_limit: u64, /// Configured gas price + #[serde(with = "alloy_serde::quantity")] pub gas_price: u128, }