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

[FR] Add internal cache for selected RPC calls #100

Open
phroi opened this issue Dec 16, 2024 · 1 comment
Open

[FR] Add internal cache for selected RPC calls #100

phroi opened this issue Dec 16, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@phroi
Copy link

phroi commented Dec 16, 2024

Hello CKB DevRel folks!!
I was wondering, have you considered adding an internal cache for RPC calls?

Many calls have no possiblity of being cached, like get_tip_header, then again many other are cache-able.

Let's take for example the Header of a block. The header is not returned by get_cells, but the header becomes part of the cell, especially in NervosDAO and iCKB calculations. To be able to get the header we either go with getHeaderByNumber (which is not reliable in case of a re-org) or we add another call per cell (get_transaction_proof) to get the hash of the containing block (which is not reliable in case of a re-org) and only then then we call getHeaderByHash (which can be cached safely).

All this means that every DApp using cell's header needs to jump thru hoops to get this info, which doesn't seem reasonable.

The solution I'd like to propose is simple and it tries to minimize the pain for the developers handling these headers, while also keeping implementation cost reasonable.

Idea

Same as RBG++, after a certain amount of time, those blocks can be consider final and not subject to re-org.

Implementation

The method getHeaderByNumber provided by CCC could have the following behavior:

  1. If the header is cached return the header
  2. If the header is not cached, get the value from RPC node
  3. If the header's block can be consider final by comparing the header timestamp vs current time, then add it to the cache by number and block hash (we could also add non-final headers with a couple seconds of life after which they get evicted, but that would complicate the cache requirements)
  4. Return header

Example

For example if we cache only blocks older than 1 hour, then only final blocks enter the cache as:

If one hour slack is not enough, we can double it until is enough.
If one hour slack is too much, we can shorten the slack until is just the right duration to be robust while minimizing cache misses.

Keep up the Great Work,
Phroi

@phroi
Copy link
Author

phroi commented Dec 16, 2024

For context, this is also related to: ickb/v1-interface#3

@Hanssen0 Hanssen0 added the enhancement New feature or request label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants