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: support block number as string #6157

Merged
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
34 changes: 34 additions & 0 deletions crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,26 @@ mod tests {

#[test]
fn test_custom_reset() {
let s = r#"{"method": "anvil_reset", "params": [{"forking": {"jsonRpcUrl": "https://ethereumpublicnode.com",
"blockNumber": "18441649"
}
}]}"#;
let value: serde_json::Value = serde_json::from_str(s).unwrap();
let req = serde_json::from_value::<EthRequest>(value).unwrap();
match req {
EthRequest::Reset(forking) => {
let forking = forking.and_then(|f| f.params);
assert_eq!(
forking,
Some(Forking {
json_rpc_url: Some("https://ethereumpublicnode.com".into()),
block_number: Some(18441649)
})
)
}
_ => unreachable!(),
}

let s = r#"{"method": "anvil_reset", "params": [ { "forking": {
"jsonRpcUrl": "https://eth-mainnet.alchemyapi.io/v2/<key>",
"blockNumber": 11095000
Expand Down Expand Up @@ -959,6 +979,20 @@ mod tests {
_ => unreachable!(),
}

let s = r#"{"method":"anvil_reset","params":[{ "blockNumber": "14000000"}]}"#;
let value: serde_json::Value = serde_json::from_str(s).unwrap();
let req = serde_json::from_value::<EthRequest>(value).unwrap();
match req {
EthRequest::Reset(forking) => {
let forking = forking.and_then(|f| f.params);
assert_eq!(
forking,
Some(Forking { json_rpc_url: None, block_number: Some(14000000) })
)
}
_ => unreachable!(),
}

let s = r#"{"method":"anvil_reset","params":[{"jsonRpcUrl": "http://localhost:8545"}]}"#;
let value: serde_json::Value = serde_json::from_str(s).unwrap();
let req = serde_json::from_value::<EthRequest>(value).unwrap();
Expand Down
25 changes: 25 additions & 0 deletions crates/anvil/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ impl<'de> serde::Deserialize<'de> for Forking {
#[serde(rename_all = "camelCase")]
struct ForkOpts {
pub json_rpc_url: Option<String>,
#[serde(
default,
deserialize_with = "ethers_core::types::serde_helpers::deserialize_stringified_u64_opt"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated q/comment: we should probably move over some serde helpers from reth once we finish alloyfying foundry, right?

Copy link
Member Author

@mattsse mattsse Oct 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to alloy, yes

)]
pub block_number: Option<u64>,
}

Expand Down Expand Up @@ -199,3 +203,24 @@ pub struct NodeForkConfig {
pub fork_block_number: Option<u64>,
pub fork_retry_backoff: Option<u128>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn serde_forking() {
let s = r#"{"forking": {"jsonRpcUrl": "https://ethereumpublicnode.com",
"blockNumber": "18441649"
}
}"#;
let f: Forking = serde_json::from_str(s).unwrap();
assert_eq!(
f,
Forking {
json_rpc_url: Some("https://ethereumpublicnode.com".into()),
block_number: Some(18441649)
}
);
}
}