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

refactor: extract cache logic from getVp #938

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

wa0x6e
Copy link
Contributor

@wa0x6e wa0x6e commented Oct 30, 2023

🧿 Current issues / What's wrong ?

The cache engine is tightly coupled to the getVp function, making it harder to test and adding too much unrelated lines unrelated to the core function (almost half of the function's lines are related to cache set and get)

💊 Fixes / Solution

Extract the cache function from the getVp function.

The goal here is:

  • to have a more simpler getVp function by removing the cache logic
  • instrument the cache engine (in later PR), by tracking how many requests are hitting/missing/skipping the cache
  • move the getScores to this cache engine in later PR, to benefit from instrumentation

🚧 Changes

🛠️ Tests

  • Current tests are untouched, and pass
  • run yarn dev
  • run
 curl 'http://localhost:3003/' \                                                              
  -H 'accept: application/json' \
  -H 'content-type: application/json' \
  --data-raw '{"jsonrpc":"2.0","method":"get_vp","params":{"address":"0x91FD2c8d24767db4Ece7069AA27832ffaf8590f3","network":"42161","strategies":[{"name":"erc20-votes","network":"42161","params":{"symbol":"ARB","address":"0x912CE59144191C1204E64559FE8253a0e49E6548","decimals":18}}],"snapshot":143863052,"space":"arbitrumfoundation.eth","delegation":false}}' \
  --compressed
  • it should return result with cache: false on first run
  • it should return result with cache: true on subsequent run

@wa0x6e wa0x6e requested a review from ChaituVR October 30, 2023 05:17
@wa0x6e wa0x6e added this to the Add more metrics milestone Oct 30, 2023
src/methods.ts Outdated
Comment on lines 37 to 49
return await cachedVp(
sha256(JSON.stringify(params)),
async () => {
return await snapshot.utils.getVp(
params.address,
params.network,
params.strategies,
params.snapshot,
params.space,
params.delegation
);
},
params.snapshot !== 'latest' && !disableCachingForSpaces.includes(params.space)
Copy link
Member

Choose a reason for hiding this comment

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

I have never seen our team using callbacks 🙈 especially less

should that be fine?

Copy link
Contributor Author

@wa0x6e wa0x6e Oct 30, 2023

Choose a reason for hiding this comment

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

It's the most elegant way to do this thing.

We use it in the requestDeduplicator

@wa0x6e wa0x6e changed the title refactor: extract cache engine from getVp refactor: extract cache logic from getVp Oct 30, 2023
@codecov
Copy link

codecov bot commented Oct 30, 2023

Codecov Report

Attention: 8 lines in your changes are missing coverage. Please review.

Comparison is base (ddec4db) 72.33% compared to head (037eca7) 75.97%.

Files Patch % Lines
src/redis.ts 11.11% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #938      +/-   ##
==========================================
+ Coverage   72.33%   75.97%   +3.64%     
==========================================
  Files          13       14       +1     
  Lines         665      691      +26     
  Branches      106      116      +10     
==========================================
+ Hits          481      525      +44     
+ Misses        180      163      -17     
+ Partials        4        3       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wa0x6e wa0x6e requested a review from ChaituVR November 28, 2023 12:20
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.

2 participants