Skip to content

Commit

Permalink
[Feature] Keep Anvil in Provider have same types as the rest of the p…
Browse files Browse the repository at this point in the history
…roject (#1876)

* change type

* fix clippy

* fix: keep serialization the same

* fix: keep serialization the same

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
  • Loading branch information
programskillforverification and DaniPopes authored Jan 7, 2025
1 parent 0eead4a commit 1b0bb9a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
5 changes: 2 additions & 3 deletions crates/provider/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Future>(future: T) -> T::Output {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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::<Vec<_>>()).await;
assert_eq!(blocks.len(), BLOCKS_TO_MINE);
Expand Down
64 changes: 33 additions & 31 deletions crates/provider/src/ext/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,8 +34,8 @@ pub trait AnvilApi<N: Network>: Send + Sync {
/// Mines a series of blocks.
async fn anvil_mine(
&self,
num_blocks: Option<U256>,
interval: Option<U256>,
num_blocks: Option<u64>,
interval: Option<u64>,
) -> TransportResult<()>;

/// Sets the mining behavior to interval with the given interval (seconds).
Expand All @@ -62,7 +62,7 @@ pub trait AnvilApi<N: Network>: 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(
Expand All @@ -76,10 +76,10 @@ pub trait AnvilApi<N: Network>: 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<()>;
Expand Down Expand Up @@ -109,7 +109,7 @@ pub trait AnvilApi<N: Network>: Send + Sync {
async fn anvil_revert(&self, id: U256) -> TransportResult<bool>;

/// Jump forward in time by the given amount of time, in seconds.
async fn anvil_increase_time(&self, seconds: U256) -> TransportResult<i64>;
async fn anvil_increase_time(&self, seconds: u64) -> TransportResult<i64>;

/// 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<()>;
Expand All @@ -119,7 +119,7 @@ pub trait AnvilApi<N: Network>: Send + Sync {
async fn anvil_set_time(&self, timestamp: u64) -> TransportResult<u64>;

/// Set the next block gas limit.
async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> TransportResult<bool>;
async fn anvil_set_block_gas_limit(&self, gas_limit: u64) -> TransportResult<bool>;

/// Sets an interval for the block timestamp.
async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> TransportResult<()>;
Expand Down Expand Up @@ -177,10 +177,12 @@ where

async fn anvil_mine(
&self,
num_blocks: Option<U256>,
interval: Option<U256>,
num_blocks: Option<u64>,
interval: Option<u64>,
) -> 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<()> {
Expand Down Expand Up @@ -211,8 +213,8 @@ where
self.client().request("anvil_setCode", (address, code)).await
}

async fn anvil_set_nonce(&self, address: Address, nonce: U256) -> TransportResult<()> {
self.client().request("anvil_setNonce", (address, nonce)).await
async fn anvil_set_nonce(&self, address: Address, nonce: u64) -> TransportResult<()> {
self.client().request("anvil_setNonce", (address, U64::from(nonce))).await
}

async fn anvil_set_storage_at(
Expand All @@ -228,12 +230,12 @@ where
self.client().request("anvil_setLoggingEnabled", (enable,)).await
}

async fn anvil_set_min_gas_price(&self, gas: U256) -> TransportResult<()> {
self.client().request("anvil_setMinGasPrice", (gas,)).await
async fn anvil_set_min_gas_price(&self, gas: u128) -> TransportResult<()> {
self.client().request("anvil_setMinGasPrice", (U128::from(gas),)).await
}

async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: U256) -> TransportResult<()> {
self.client().request("anvil_setNextBlockBaseFeePerGas", (basefee,)).await
async fn anvil_set_next_block_base_fee_per_gas(&self, basefee: u128) -> TransportResult<()> {
self.client().request("anvil_setNextBlockBaseFeePerGas", (U128::from(basefee),)).await
}

async fn anvil_set_coinbase(&self, address: Address) -> TransportResult<()> {
Expand Down Expand Up @@ -268,8 +270,8 @@ where
self.client().request("evm_revert", (id,)).await
}

async fn anvil_increase_time(&self, seconds: U256) -> TransportResult<i64> {
self.client().request("evm_increaseTime", (seconds,)).await
async fn anvil_increase_time(&self, seconds: u64) -> TransportResult<i64> {
self.client().request("evm_increaseTime", (U64::from(seconds),)).await
}

async fn anvil_set_next_block_timestamp(&self, seconds: u64) -> TransportResult<()> {
Expand All @@ -280,8 +282,8 @@ where
self.client().request("evm_setTime", (timestamp,)).await
}

async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> TransportResult<bool> {
self.client().request("evm_setBlockGasLimit", (gas_limit,)).await
async fn anvil_set_block_gas_limit(&self, gas_limit: u64) -> TransportResult<bool> {
self.client().request("evm_setBlockGasLimit", (U64::from(gas_limit),)).await
}

async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> TransportResult<()> {
Expand Down Expand Up @@ -423,7 +425,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), None).await.unwrap();

let num = provider.get_block_number().await.unwrap();

Expand Down Expand Up @@ -565,11 +567,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::<u64>());
assert_eq!(new_nonce, nonce);
}

#[tokio::test]
Expand Down Expand Up @@ -598,7 +600,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"
Expand All @@ -610,7 +612,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();
Expand All @@ -621,7 +623,7 @@ mod tests {
.unwrap()
.unwrap();

assert_eq!(block.header.base_fee_per_gas, Some(basefee.to::<u64>()));
assert_eq!(block.header.base_fee_per_gas, Some(basefee as u64));
}

#[tokio::test]
Expand Down Expand Up @@ -761,7 +763,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);
}
Expand Down Expand Up @@ -805,7 +807,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();
Expand All @@ -815,7 +817,7 @@ mod tests {
.await
.unwrap()
.unwrap();
assert_eq!(block_gas_limit.to::<u64>(), latest_block.header.gas_limit);
assert_eq!(block_gas_limit, latest_block.header.gas_limit);
}

#[tokio::test]
Expand Down Expand Up @@ -951,7 +953,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), None).await.unwrap();

let reorged_block = provider
.get_block_by_number(2.into(), BlockTransactionsKind::Hashes)
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,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;
}
}
Expand Down
9 changes: 6 additions & 3 deletions crates/rpc-types-anvil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ pub struct NodeInfo {
#[serde(rename_all = "camelCase")]
pub struct NodeEnvironment {
/// Base fee of the current block
pub base_fee: U256,
#[serde(with = "alloy_serde::quantity")]
pub base_fee: u128,
/// Chain id of the node.
pub chain_id: ChainId,
/// Configured block gas limit
pub gas_limit: U256,
#[serde(with = "alloy_serde::quantity")]
pub gas_limit: u64,
/// Configured gas price
pub gas_price: U256,
#[serde(with = "alloy_serde::quantity")]
pub gas_price: u128,
}

/// The node's fork configuration.
Expand Down

0 comments on commit 1b0bb9a

Please sign in to comment.