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

feat: add anvil_setRpcUrl #451

Merged
merged 1 commit into from
Nov 29, 2024
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
1 change: 1 addition & 0 deletions SUPPORTED_APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The `status` options are:

| Namespace | API | <div style="width:130px">Status</div> | Description |
| --- | --- | --- | --- |
| `ANVIL` | `anvil_setRpcUrl` | `SUPPORTED` | Sets the fork RPC url. Assumes the underlying chain is the same as before |
| `ANVIL` | `anvil_setNextBlockBaseFeePerGas` | `SUPPORTED` | Sets the base fee of the next block |
| `ANVIL` | `anvil_dropTransaction` | `SUPPORTED` | Removes a transaction from the pool |
| `ANVIL` | `anvil_dropAllTransactions` | `SUPPORTED` | Remove all transactions from the pool |
Expand Down
5 changes: 5 additions & 0 deletions src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@ impl ForkDetails {
}
}
}

/// Sets fork's internal URL. Assumes the underlying chain is the same as before.
pub fn set_rpc_url(&mut self, url: String) {
self.fork_source = Box::new(HttpForkSource::new(url, self.cache_config.clone()));
}
}

#[cfg(test)]
Expand Down
8 changes: 8 additions & 0 deletions src/namespaces/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ use crate::utils::Numeric;

#[rpc]
pub trait AnvilNamespaceT {
/// Sets the fork RPC url. Assumes the underlying chain is the same as before.
///
/// # Arguments
///
/// * `url` - Fork's new URL
#[rpc(name = "anvil_setRpcUrl")]
fn set_rpc_url(&self, url: String) -> RpcResult<()>;

/// Sets the base fee of the next block.
///
/// # Arguments
Expand Down
9 changes: 9 additions & 0 deletions src/node/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ use crate::{
impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> AnvilNamespaceT
for InMemoryNode<S>
{
fn set_rpc_url(&self, url: String) -> RpcResult<()> {
self.set_rpc_url(url)
.map_err(|err| {
tracing::error!("failed setting RPC url: {:?}", err);
into_jsrpc_error(Web3Error::InternalError(err))
})
.into_boxed_future()
}

fn set_next_block_base_fee_per_gas(&self, base_fee: U256) -> RpcResult<()> {
self.set_next_block_base_fee_per_gas(base_fee)
.map_err(|err| {
Expand Down
25 changes: 25 additions & 0 deletions src/node/in_memory_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,31 @@ impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> InMemoryNo
.set_base_fee(base_fee.as_u64());
Ok(())
}

pub fn set_rpc_url(&self, url: String) -> Result<()> {
let inner = self
.inner
.read()
.map_err(|err| anyhow!("failed acquiring lock: {:?}", err))?;
let mut fork_storage = inner
.fork_storage
.inner
.write()
.map_err(|err| anyhow!("failed acquiring lock: {:?}", err))?;
if let Some(fork) = &mut fork_storage.fork {
let old_url = fork.fork_source.get_fork_url().map_err(|e| {
anyhow::anyhow!(
"failed to resolve current fork's RPC URL: {}",
e.to_string()
)
})?;
fork.set_rpc_url(url.clone());
tracing::info!("Updated fork rpc from \"{}\" to \"{}\"", old_url, url);
} else {
tracing::info!("Non-forking node tried to switch RPC URL to '{url}'. Call `anvil_reset` instead if you wish to switch to forking mode");
}
Ok(())
}
}

#[cfg(test)]
Expand Down