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(provider): Prevent panic from having 0 keys when calling on_anvil_with_wallet_and_config #1055

Merged
merged 8 commits into from
Jul 16, 2024

Conversation

moricho
Copy link
Contributor

@moricho moricho commented Jul 15, 2024

Motivation

Closes #889.

on_anvil_with_wallet_and_config can panic if the available key in an anvil instance is 0.

Solution

Adds the new error NoKeysAvailable to AnvilError in alloy-node-bindings and uses it as the error of on_anvil_with_wallet_and_config

pub enum AnvilError {
  ...
  /// No keys available in anvil instance.
  #[error("no keys available in anvil instance")]
  NoKeysAvailable,
}
pub fn on_anvil_with_wallet_and_config(
        self,
        f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
    ) -> Result<
        ...,
        AnvilError,
    >

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Comment on lines 422 to 425
pub fn on_anvil_with_wallet_and_config(
self,
f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
) -> <JoinFill<F, WalletFiller<alloy_network::EthereumWallet>> as ProviderLayer<
L::Provider,
alloy_transport_http::Http<reqwest::Client>,
>>::Provider
) -> Result<
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to keep this function and not return a result, but we can add fn try_on_anvil... for this and on_anvil calls this and unwraps

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed: bd30ae1

Comment on lines +349 to +352
type JoinedEthereumWalletFiller<F> = JoinFill<F, WalletFiller<alloy_network::EthereumWallet>>;

#[cfg(any(test, feature = "anvil-node"))]
type AnvilProviderResult<T> = Result<T, alloy_node_bindings::anvil::AnvilError>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added these type aliases to avoid clippy error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mattsse By the way, should we create a type alias for Http<reqwest::Client>? It appears so many times in this repository and it can cause clippy warnings very complex type used. Consider factoring parts into type definitions when a type is complex, as in this case.

@moricho moricho requested a review from mattsse July 15, 2024 15:57
@mattsse mattsse merged commit 0950b03 into alloy-rs:main Jul 16, 2024
22 checks passed
@moricho moricho deleted the anvil-provider-panic branch July 16, 2024 13:59
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
…l_with_wallet_and_config` (alloy-rs#1055)

* node-bindings: Add AnvilError::NoKeysAvailable

* prevent panic from having 0 keys

* fix the error handlings for `on_anvil_with_wallet`

* fix clippy

* fix(clippy): Add type alias

* Add `try_on_anvil_with_wallet_and_config` and call it in `on_anvil_with_wallet_and_config`

* Revert test changes

* Fix docs
j75689 pushed a commit to bnb-chain/alloy that referenced this pull request Aug 1, 2024
…l_with_wallet_and_config` (alloy-rs#1055)

* node-bindings: Add AnvilError::NoKeysAvailable

* prevent panic from having 0 keys

* fix the error handlings for `on_anvil_with_wallet`

* fix clippy

* fix(clippy): Add type alias

* Add `try_on_anvil_with_wallet_and_config` and call it in `on_anvil_with_wallet_and_config`

* Revert test changes

* Fix docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prevent panic from having 0 keys when calling on_anvil_with_wallet_and_config
2 participants