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

fix(anvil): anvil_metadata return types #6360

Merged
merged 1 commit into from
Nov 19, 2023
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
10 changes: 5 additions & 5 deletions crates/anvil/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub struct NodeInfo {
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct NodeEnvironment {
pub base_fee: U256,
pub chain_id: U256,
pub chain_id: u64,
pub gas_limit: U256,
pub gas_price: U256,
}
Expand All @@ -212,9 +212,9 @@ pub struct NodeForkConfig {
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct AnvilMetadata {
pub client_version: &'static str,
pub chain_id: U256,
pub chain_id: u64,
pub instance_id: H256,
pub latest_block_number: U64,
pub latest_block_number: u64,
pub latest_block_hash: H256,
pub forked_network: Option<ForkedNetwork>,
}
Expand All @@ -225,8 +225,8 @@ pub struct AnvilMetadata {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct ForkedNetwork {
pub chain_id: U256,
pub fork_block_number: U64,
pub chain_id: u64,
pub fork_block_number: u64,
pub fork_block_hash: TxHash,
}

Expand Down
17 changes: 8 additions & 9 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,15 @@ impl EthApi {
/// Handler for ETH RPC call: `eth_chainId`
pub fn eth_chain_id(&self) -> Result<Option<U64>> {
node_info!("eth_chainId");
Ok(Some(self.backend.chain_id().as_u64().into()))
Ok(Some(self.backend.chain_id().into()))
}

/// Returns the same as `chain_id`
///
/// Handler for ETH RPC call: `eth_networkId`
pub fn network_id(&self) -> Result<Option<String>> {
node_info!("eth_networkId");
let chain_id = self.backend.chain_id().as_u64();
let chain_id = self.backend.chain_id();
Ok(Some(format!("{chain_id}")))
}

Expand Down Expand Up @@ -1708,17 +1708,16 @@ impl EthApi {
pub async fn anvil_metadata(&self) -> Result<AnvilMetadata> {
node_info!("anvil_metadata");
let fork_config = self.backend.get_fork();
let chain_id_uint = U256::from(self.backend.chain_id().low_u64());
let latest_block_number_uint = U64::from(self.backend.best_number().low_u64());

Ok(AnvilMetadata {
client_version: CLIENT_VERSION,
chain_id: chain_id_uint,
chain_id: self.backend.chain_id().try_into().unwrap_or(u64::MAX),
latest_block_hash: self.backend.best_hash(),
latest_block_number: latest_block_number_uint,
latest_block_number: self.backend.best_number().as_u64(),
instance_id: *self.instance_id.read(),
forked_network: fork_config.map(|cfg| ForkedNetwork {
chain_id: U256::from(cfg.chain_id()),
fork_block_number: U64::from(cfg.block_number()),
chain_id: cfg.chain_id().into(),
fork_block_number: cfg.block_number().into(),
fork_block_hash: cfg.block_hash(),
}),
})
Expand Down Expand Up @@ -2302,7 +2301,7 @@ impl EthApi {

/// Returns the chain ID used for transaction
pub fn chain_id(&self) -> u64 {
self.backend.chain_id().as_u64()
self.backend.chain_id()
}

pub fn get_fork(&self) -> Option<ClientFork> {
Expand Down
12 changes: 5 additions & 7 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ impl Backend {

/// Returns the current best number of the chain
pub fn best_number(&self) -> U64 {
let num: u64 = self.env.read().block.number.try_into().unwrap_or(u64::MAX);
num.into()
self.env.read().block.number.saturating_to::<u64>().into()
}

/// Sets the block number
Expand All @@ -486,8 +485,8 @@ impl Backend {
}

/// Returns the client coinbase address.
pub fn chain_id(&self) -> U256 {
self.env.read().cfg.chain_id.into()
pub fn chain_id(&self) -> u64 {
self.env.read().cfg.chain_id
}

pub fn set_chain_id(&self, chain_id: u64) {
Expand Down Expand Up @@ -2202,11 +2201,10 @@ impl TransactionValidator for Backend {

if let Some(tx_chain_id) = tx.chain_id() {
let chain_id = self.chain_id();
if chain_id != tx_chain_id.into() {
if chain_id != tx_chain_id {
if let Some(legacy) = tx.as_legacy() {
// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md>
if env.cfg.spec_id >= SpecId::SPURIOUS_DRAGON &&
!legacy.meets_eip155(chain_id.as_u64())
if env.cfg.spec_id >= SpecId::SPURIOUS_DRAGON && !legacy.meets_eip155(chain_id)
{
warn!(target: "backend", ?chain_id, ?tx_chain_id, "incompatible EIP155-based V");
return Err(InvalidTransactionError::IncompatibleEIP155)
Expand Down
10 changes: 5 additions & 5 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ async fn can_get_node_info() {
transaction_order: "fees".to_owned(),
environment: NodeEnvironment {
base_fee: U256::from_str("0x3b9aca00").unwrap(),
chain_id: U256::from_str("0x7a69").unwrap(),
chain_id: 0x7a69,
gas_limit: U256::from_str("0x1c9c380").unwrap(),
gas_price: U256::from_str("0x77359400").unwrap(),
},
Expand All @@ -462,8 +462,8 @@ async fn can_get_metadata() {

let provider = handle.http_provider();

let block_number = provider.get_block_number().await.unwrap();
let chain_id = provider.get_chainid().await.unwrap();
let block_number = provider.get_block_number().await.unwrap().as_u64();
let chain_id = provider.get_chainid().await.unwrap().as_u64();
let block = provider.get_block(block_number).await.unwrap().unwrap();

let expected_metadata = AnvilMetadata {
Expand All @@ -486,8 +486,8 @@ async fn can_get_metadata_on_fork() {

let metadata = api.anvil_metadata().await.unwrap();

let block_number = provider.get_block_number().await.unwrap();
let chain_id = provider.get_chainid().await.unwrap();
let block_number = provider.get_block_number().await.unwrap().as_u64();
let chain_id = provider.get_chainid().await.unwrap().as_u64();
let block = provider.get_block(block_number).await.unwrap().unwrap();

let expected_metadata = AnvilMetadata {
Expand Down
Loading