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

Cache RPC responses #19

Merged
merged 4 commits into from
Jan 9, 2023
Merged

Cache RPC responses #19

merged 4 commits into from
Jan 9, 2023

Conversation

PaulRBerg
Copy link
Contributor

@PaulRBerg PaulRBerg commented Jan 7, 2023

As discussed here and here, this PR upstreams my RPC caching method in foundry-toolchain itself.

I explained how the caching works in the README, but I will paste it here as well:

This action matches Forge's behavior and caches all RPC responses in the ~/.foundry/cache/rpc directory. This is done
to speed up the tests and avoid hitting the rate limit of your RPC provider.

The logic of the caching is as follows:

  • Always load the latest valid cache, and always create a new one with the updated cache.
  • When there are no changes to the fork tests, the cache does not change but the key does, since the key is > based on the commit hash.
  • When the fork tests are changed, both the cache and the key are updated.

Side note: this PR also installs Prettier and adds a basic config file.

I tested this in my private repo that has fork tests, and both restoring the cache and saving it worked as expected:

restore-cache

save-cache

Feel free to test in your repos, too:

- name: "Install Foundry"
  uses: "paulrberg/foundry-toolchain@caching"

chore: use double quotes in "action.yml"
docs: add brief description about Foundry in README
docs: fix typos in README
docs: remove optional input "version" from example script
refactor: set "nightly" as default value for "version"
build: add "@actions/cache" and "@actions/github" as deps
build: add "prettier" as dev dep
build: regenerate "dist" files
chore: add Prettier configuration and Prettier ignore file
docs: change author to "Foundry"
feat: run "save" function on post
style: format JavaScript files with Prettier
README.md Outdated

- Always load the latest valid cache, and always create a new one with the updated cache.
- When there are no changes to the fork tests, the cache does not change but the restore key does, since the key is based on the commit hash.
- When the fork tests are changed, both the cache and the store key are updated.
Copy link

Choose a reason for hiding this comment

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

What does this bullet mean / what is the store key and restore key? More generally I think I'm unclear on why the logic isn't simpler to just "always cache all responses, clear cache if user changes the cache key". Basically just want to sanity check that the cache isn't deleted if I add/remove new fork blocks or networks to tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can't be that simple, unfortunately.

As Twitter user ultrasecreth explained here, we want a mechanism whereby if the fork tests change, the cache is updated too.

If you don't specify the git commit in the cache key, then the cache will never update, even if you completely redesign your tests. Quote from the README:

If the provided key matches an existing cache, a new cache is not created.

Copy link

@mds1 mds1 Jan 9, 2023

Choose a reason for hiding this comment

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

Got it, I see. And is "store key" the same as the "restore keys" in the actions/cache repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh my goodness, I have completely butchered the terminology in the proposed README. I'm sorry, I must have been off-caffeine on the day when I opened the PR.

I have pushed a commit to fix the wording, you can see the diff here:

607b0c5

Basically, I should have just said "key", because the "restore keys" never change on the same OS platform. It's the "key" (simple key, no restore) that changes all the time because it's dependent upon the commit hash.

Copy link

Choose a reason for hiding this comment

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

Heh got it, thanks! Deferring to @gakonst and co. for review/merge 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @mds1 for the review thus far.

Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

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

Sick - ty

const github = require("@actions/github");
const os = require("os");

const CACHE_PATHS = ["~/.foundry/cache/rpc"];
Copy link
Member

Choose a reason for hiding this comment

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

Does this also cache any etherscan queries? Should we add support for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I didn't think about Etherscan. Opened an issue for tracking:

#20

Comment on lines +3 to +5
async function save() {
await saveCache();
}
Copy link
Member

Choose a reason for hiding this comment

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

Nit - redundant? Can export saveCache as alias?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not redundant, I'm afraid.

The save function needs to kept separate because we have to run it at the end of the GA workflow execution - see the runs.post field I added in action.yml. There is no other way to run the save cache step at the end of the workflow execution.

Related - this is also how the official GitHub cache does this.

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.

3 participants