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

Add simple cache on eth_call #880

Merged
merged 5 commits into from
Feb 7, 2023
Merged

Add simple cache on eth_call #880

merged 5 commits into from
Feb 7, 2023

Conversation

georgi-l95
Copy link
Collaborator

@georgi-l95 georgi-l95 commented Feb 7, 2023

Signed-off-by: georgi-l95 glazarov95@gmail.com

Description:
Adds a cache on eth_call request, based on to and data parameters for 200ms.

Related issue(s):

Fixes #877

Notes for reviewer:

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: georgi-l95 <glazarov95@gmail.com>
@georgi-l95 georgi-l95 self-assigned this Feb 7, 2023
Signed-off-by: georgi-l95 <glazarov95@gmail.com>
@georgi-l95 georgi-l95 marked this pull request as ready for review February 7, 2023 13:07
@codecov-commenter
Copy link

codecov-commenter commented Feb 7, 2023

Codecov Report

Base: 73.88% // Head: 74.16% // Increases project coverage by +0.28% 🎉

Coverage data is based on head (3af5d1a) compared to base (213d7cb).
Patch coverage: 100.00% of modified lines in pull request are covered.

❗ Current head 3af5d1a differs from pull request most recent head 024588c. Consider uploading reports for the commit 024588c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #880      +/-   ##
==========================================
+ Coverage   73.88%   74.16%   +0.28%     
==========================================
  Files          29       29              
  Lines        1815     1827      +12     
  Branches      336      339       +3     
==========================================
+ Hits         1341     1355      +14     
+ Misses        375      374       -1     
+ Partials       99       98       -1     
Impacted Files Coverage Δ
packages/relay/src/lib/eth.ts 82.84% <100.00%> (+0.71%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@Nana-EC Nana-EC left a comment

Choose a reason for hiding this comment

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

LG
Some suggestions

@@ -1017,8 +1017,17 @@ export class EthImpl implements Eth {
}
}

let cachedResponse = this.cache.get(`eth_call: ${call.data} from ${call.to}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: add const for label. Maybe shorten key also

Suggested change
let cachedResponse = this.cache.get(`eth_call: ${call.data} from ${call.to}`);
const cacheKey = `eth_call:.${call.to}.${call.data}`;
let cachedResponse = this.cache.get(cacheKey);

return EthImpl.prepend0x(Buffer.from(contractCallResponse.asBytes()).toString('hex'));
const formattedCallReponse = EthImpl.prepend0x(Buffer.from(contractCallResponse.asBytes()).toString('hex'));

this.cache.set(`eth_call: ${call.data} from ${call.to}`, formattedCallReponse, { ttl: 200 });
Copy link
Collaborator

Choose a reason for hiding this comment

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

use cacheKey here

@@ -1017,8 +1017,17 @@ export class EthImpl implements Eth {
}
}

let cachedResponse = this.cache.get(`eth_call: ${call.data} from ${call.to}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm concerned the call.data can get quite long.
In my exploration I went down the path of taking a hashing of the data to reduce it's length.

Maybe since the cache is so short lived it's not a biggie but let's make sure the key itself doesn't have length limits.
Maybe a simple approach of taking the first n characters also works if the hash approach is costly

Copy link
Collaborator

Choose a reason for hiding this comment

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

I just wanna avoid any potential error cases from a long data. maybe add a test case with a long data field if you stay with this approach.

Copy link
Collaborator

@Nana-EC Nana-EC left a comment

Choose a reason for hiding this comment

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

Let's make the ttl configureable

return EthImpl.prepend0x(Buffer.from(contractCallResponse.asBytes()).toString('hex'));
const formattedCallReponse = EthImpl.prepend0x(Buffer.from(contractCallResponse.asBytes()).toString('hex'));

this.cache.set(`eth_call: ${call.data} from ${call.to}`, formattedCallReponse, { ttl: 200 });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make the ttl value a config to allow for on demand tuning

Signed-off-by: georgi-l95 <glazarov95@gmail.com>
Signed-off-by: georgi-l95 <glazarov95@gmail.com>
Signed-off-by: georgi-l95 <glazarov95@gmail.com>
@sonarcloud
Copy link

sonarcloud bot commented Feb 7, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Ivo-Yankov
Copy link
Collaborator

Ivo-Yankov commented Feb 7, 2023

I think this cache could cause issues in the case where the result of a method from a contract depends not on its own state, but on the state of another contract.

Such is the case with a Multicaller contract, where it only aggregates and returns results from multiple other contracts, but it doesn't update its own state.

@georgi-l95
Copy link
Collaborator Author

Hmm, I think you're right, but we have a ttl of 200ms, which is not so long, because those multicalls might take some time. Also this is a temporary solution and it's configurable if there is indeed problem with multicall queries.

Copy link
Collaborator

@Nana-EC Nana-EC left a comment

Choose a reason for hiding this comment

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

LG

@Nana-EC Nana-EC added enhancement New feature or request limechain P1 labels Feb 7, 2023
@Nana-EC Nana-EC added this to the 0.18.0 milestone Feb 7, 2023
@georgi-l95 georgi-l95 merged commit 1f9b860 into main Feb 7, 2023
@georgi-l95 georgi-l95 deleted the add-cache-on-eth-call branch February 7, 2023 16:43
data = crypto.createHash('sha1').update(call.data).digest('hex'); // NOSONAR
}

const cacheKey = `eth_call:.${call.to}.${data}`;
Copy link
Member

@lukelee-sl lukelee-sl Feb 7, 2023

Choose a reason for hiding this comment

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

what happens if data is null/undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No problems there we continue with the query as normal, I just didn’t want to calculate hash for a undefined, now that I think, I should’ve added a or call.data.length is bigger than 64(for example).

lukelee-sl pushed a commit that referenced this pull request Feb 7, 2023
Adds a cache on eth_call request, based on `to` and `data` parameters for 200ms.

Signed-off-by: georgi-l95 <glazarov95@gmail.com>
lukelee-sl pushed a commit that referenced this pull request Feb 7, 2023
Adds a cache on eth_call request, based on `to` and `data` parameters for 200ms.

Signed-off-by: georgi-l95 <glazarov95@gmail.com>
lukelee-sl added a commit that referenced this pull request Feb 8, 2023
* Add simple cache on `eth_call` (#880)

Adds a cache on eth_call request, based on `to` and `data` parameters for 200ms.

Signed-off-by: georgi-l95 <glazarov95@gmail.com>

* retrigger checks

---------

Signed-off-by: georgi-l95 <glazarov95@gmail.com>
Co-authored-by: Georgi Lazarov <glazarov95@gmail.com>
@lukelee-sl lukelee-sl modified the milestones: 0.18.0, 0.17.1 Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request limechain P1
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Add a simple eth_call cache
5 participants