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: usage for mocking blockchain scenarios in Rust tests #6974

Closed
chris13524 opened this issue Jan 31, 2024 · 5 comments
Closed

feat: usage for mocking blockchain scenarios in Rust tests #6974

chris13524 opened this issue Jan 31, 2024 · 5 comments
Labels
A-extensions Area: extensions C-anvil Command: anvil T-feature Type: feature
Milestone

Comments

@chris13524
Copy link

Component

Forge

Describe the feature you would like

I'm implementing EIP-1271 using alloy in a Rust project and I'd like to write an automated test for my implementation. My idea is to write a tokio test which starts up an anvil blockchain, and deploys a mock EIP-1271 contract (perhaps the one from the spec) to the chain, and then executes the code under test using a mocked blockchain. Several test cases would check more scenarios, such as invalid signatures or invalid address.

However, I ran into some problems with this approach, namely:

  • Using anvil as a library does not appear to be stable. When using it with anvil = { git = "https://github.com/foundry-rs/foundry.git" } and providing the same [patch.crates-io] I get compile errors (and different ones from yesterday).
  • Using forge from Rust code is not possible because the main logic is inside the bin folder: https://github.com/foundry-rs/foundry/blob/master/crates/forge/bin/cmd/create.rs

Alternatively I could install the anvil and forge binaries and execute them from Rust, but this significantly more effort to pass configuration around (e.g. private keys and deployment addresses) and requires the developer to install dependencies.

Additional context

No response

@mattsse
Copy link
Member

mattsse commented Jan 31, 2024

something like this you mean?

#[tokio::test(flavor = "multi_thread")]
async fn can_set_empty_code() {
let (api, _handle) = spawn(NodeConfig::test()).await;
let addr = Address::random();
api.anvil_set_code(addr.to_alloy(), Vec::new().into()).await.unwrap();
let code = api.get_code(addr.to_alloy(), None).await.unwrap();
assert!(code.as_ref().is_empty());
}

@DaniPopes
Copy link
Member

DaniPopes commented Jan 31, 2024

Since this is just for internal testing, you should just require the anvil binary and call it through alloy-node-bindings. We don't provide any stability guarantees to any crate at the moment.

@insipx
Copy link

insipx commented Feb 2, 2024

Note that the ethers crate provides an interface to anvil: https://docs.rs/ethers/latest/ethers/core/utils/struct.Anvil.html

I know that alloy is supposed to replace it and ethers is being deprecated, but until there is a better solution, maybe this is an acceptable workaround?

We do something very similar to what you describe here with anvil from ethers: https://github.com/xmtp/didethresolver/blob/a30b4da8b436cdbef6f93a17538612bc4d512e81/lib/tests/integration_util/mod.rs#L52

It seems alloy-node-bindings is this same/similar interface, so that would do the trick

@chris13524
Copy link
Author

Thanks guys for the ideas! Ideally I'd like to do what @mattsse mentioned; i.e. call into the Anvil directly and spawn it. However, I had gotten compiler errors when trying to do this.

We don't provide any stability guarantees to any crate at the moment.

That's fine, I don't mind if the API is unstable and changes frequently. Would be nice if I could get it to compile though!

Seems like alloy-node-bindings is the best way forward for now. However I'd still request that this feature be added in the future: ability to spawn a node and deploy a contract from Rust w/o binary installation.

@zerosnacks zerosnacks changed the title Usage for mocking blockchain scenarios in Rust tests feat: ability to spawn an Anvil node and deploy a contract from Rust w/o binary installation Jul 10, 2024
@zerosnacks zerosnacks added C-anvil Command: anvil A-releases Area: releases/packaging A-extensions Area: extensions and removed A-releases Area: releases/packaging labels Jul 10, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@zerosnacks zerosnacks changed the title feat: ability to spawn an Anvil node and deploy a contract from Rust w/o binary installation feat: usage for mocking blockchain scenarios in Rust tests Oct 3, 2024
@zerosnacks
Copy link
Member

In the future we possibly have a better way of using Anvil as a library but for now using alloy-node-bindings is the recommended way. Marking as resolved.

@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-extensions Area: extensions C-anvil Command: anvil T-feature Type: feature
Projects
Archived in project
Development

No branches or pull requests

5 participants