-
Notifications
You must be signed in to change notification settings - Fork 68
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
feat: send epoch number on each request #380
feat: send epoch number on each request #380
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good! can you cache the epoch number? like i know we have the event listener that listens to the staking contract events, so i think that should set it in the cache when it changes? if there is a cache miss, then you should fallback to the getCurrentEpochNumber function.
maybe like, set it inside litNodeClient.connect() with getCurrentEpochNumber. and then update it with the event listener when the epoch changes. but remember - we only want to use a new one if it's more than 30s old. so you could put a setTimeout() into the event listener when the epoch changes?
and also, on first page load, in theory you do need to check that it's more than 30s old. you can do this with just the epoch object though. it returns epochLength and endTime in addition to number. you can do (endTime - epochLength) to find the time where the epoch transitioned. if it happened less than 30s ago, you should actually use "epochNumber - 1" and put in a setTimeout to use the new one (aka, epochNumber) 30s after the time when the epoch transitioned.
* feat: make capacityTokenId optional * chore: add test * fix: Conditionally include nft_id * fix: replace nx executor to @nrwl/js:tsc * fix: replace nx executor * feat: migrated nx to 15.9.0 * feat: migration nx from 15.9.0 -> 16.10.0 * feat: migrate nx to 17.3.0 * fix: yarn:test:unit command * chore: log cc nft token info * fix: undefined typings in package.json
…385) * remove old v0 errors from js sdk. provide error details to the user * building now seems to consistently work * refactored error handling to make it cleaner * Remove unused function * okay also added requestId in a few places * ran the linter * fix: replace nx executor * feat: add gitignore --------- Co-authored-by: Ansonhkg <[email protected]>
* feat: make capacityTokenId optional * chore: add test * fix: Conditionally include nft_id
…number-on-each-request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful. this is very close - just a note about the max cache age. great job understanding the requirements re: the 30s delay and implementing that.
packages/core/src/lib/lit-core.ts
Outdated
|
||
if ( | ||
this.epochCache.number !== null && | ||
cacheAge <= DELAY_BEFORE_NEXT_EPOCH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this should be separate from DELAY_BEFORE_NEXT_EPOCH and have a different max cache age. like the DELAY_BEFORE_NEXT_EPOCH is 30s because we want to give the nodes 30s to see the new epoch. but we can technically cache the current epoch for much longer than that. the epoch time is 60 mins, and the epoch shouldn't change during that time.
and since we have the event listener for when the change happens, i'm not sure we actually even need a max cache age here? what do you think?
i suppose the purpose of the max cache age is, in case we somehow miss an epoch change event? but i don't think that should happen often - even if the event polling fails it should pick up the event on the next poll, and that's like a 5s polling interval. i kinda think we can remove max cache age and handle this corner case in a future PR described below:
we should have error handling in a future PR, to handle the state where the nodes go "hey this epoch is too old!" and the SDK goes "ok let me get and cache the current epoch and retry". but you can't really write that code until we have the error coming from the Node, because you won't know how to handle it. so we can save that for a future PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm if we remove the check cacheAge <= MAX_CACHE_AGE
here and leaving only this.epochCache.number !== null
, we will have a stale state relying on the epoch that we initially set.
If we remove the checks altogether, I'm not sure if we still need to have the whole caching mechanism at all? as we are only setting them and not using them.
I think what we can do is, try to getCurrentEpochNumber()
from the staking contract, if it fails, we will use the cache value instead.
☝️should I do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but the event listener will fire and set this.epochCache.number when the epoch changes? so it won't be stale, it will always be up to date?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it won't for the first time because this.epochCache.number
is null, then it will get the epoch number from the contract and set the cache, then the second time the event listener will just return this.epochCache.number
because it's not null anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if this change works? 53bacb9 @glitch003
let's merge this, but i think we should do a follow up next that adds one more feature. when we initially pull the epoch number, we should also pull the timestamp of when that epoch transitioned. and then, if that's less than 30s ago, we minus 1 (unless the epoch is 1), and then setTimeout so that when it has been 30s from the epoch transition, we start using the new one. |
…number-on-each-request
Description
Send epoch number on each request.
eg. output:
Type of change
How Has This Been Tested?
Observing behaviour changes on calling the
getCurrentEpochNumber
function every second.Screen.Recording.2024-02-28.at.08.43.56.mov
Checklist: