Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat: cached provider PoC #949

Closed
wants to merge 2 commits into from
Closed

feat: cached provider PoC #949

wants to merge 2 commits into from

Conversation

gakonst
Copy link
Owner

@gakonst gakonst commented Feb 22, 2022

Problem

We want to avoid repeating requests which we expect to have the same response. This is useful when used together with Forge's mainnet forking tests.

Solution

Add a Cache which stores all request/responses pairs in-memory, which then proceeds to store them in disk when it gets Dropped

@gakonst gakonst force-pushed the feat/cached-provider branch from 84c99a2 to a4405f8 Compare February 24, 2022 21:21
@gakonst gakonst force-pushed the feat/cached-provider branch from a4405f8 to 2e05148 Compare February 24, 2022 21:27
assert_eq!(res, 100.into());

assert!(!provider.cache().unwrap().requests.is_empty());
dbg!(&provider.cache());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
dbg!(&provider.cache());


impl Drop for Cache {
fn drop(&mut self) {
let file = match File::options().write(true).read(true).create(true).open(&self.path) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should be File::create? which

Opens a file in write-only mode. reate a file if it does not exist, and will truncate it if it does.

pub struct Cache {
path: PathBuf,
// serialized request / response pair
requests: DashMap<String, String>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this be <String, serde_json::Value>?

@gakonst
Copy link
Owner Author

gakonst commented Feb 24, 2022

@brockelmore curious what you think on this approach?

@tynes
Copy link
Contributor

tynes commented Mar 2, 2022

Depending on how forge implements its caching strategy, it might be useful to have a way to clear the cache

@gakonst
Copy link
Owner Author

gakonst commented Mar 3, 2022

Closing - will make it part of the Forking provider on Foundry foundry-rs/foundry#835

@gakonst gakonst closed this Mar 3, 2022
@gakonst gakonst deleted the feat/cached-provider branch March 3, 2022 16:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants