Fix stale action invalidation in global state manager - #313
Conversation
| and dispatchTask = (self, task: S.task) => { | ||
| let stateId = self.state->S.getId | ||
| Js.Global.setTimeout(() => { | ||
| S.taskReducer(self.state, task, ~dispatchAction=action => | ||
| dispatchAction(~stateId=self.state->S.getId, self, action) | ||
| dispatchAction(~stateId, self, action) | ||
| )->ignore | ||
| }, 0)->ignore | ||
| } |
There was a problem hiding this comment.
This was a bug, the state id should be retrieved at the time dispatchTask gets called, not as dispatchAction gets called.
Since S.getId was being called only at dispatchAction it would always pass the latest stateId and there would be no way to invalidate the action against it's state id
d102d13 to
0244ba5
Compare
| } else { | ||
| (state, []) | ||
| } | ||
| (freshState->incrementId, [NextQuery(CheckAllChains)]) |
There was a problem hiding this comment.
Here the new state increments the state id so that any actions being dispatched from previous invalid state will be invalidated.
| //Protect against handling this action twice. It should only be handled once | ||
| //when the pre registration is done | ||
| if ChainManager.isPreRegisteringDynamicContracts(chainManager) { |
There was a problem hiding this comment.
Removed this if check since it's using the state id invalidator.
| } else { | ||
| (state, []) | ||
| } | ||
| (freshState->incrementId, [NextQuery(CheckAllChains)]) |
| switch (state, action) { | ||
| | ({rollbackState: RollingBack(_)}, EventBatchProcessed(_)) => ( | ||
| {...state, currentlyProcessingBatch: false}, | ||
| [Rollback], | ||
| ) |
There was a problem hiding this comment.
Could you explain the logic? I can't completely comprehend what's going on here
There was a problem hiding this comment.
Yeah sure, it's a bit difficult to follow how the actions/tasks react to each other.
So Rollback action will first get dispatched after block range response when a reorg gets detected here:
Then on the Rollback task, if it only starts if a batch is not currently processing.
hyperindex/codegenerator/cli/templates/static/codegen/src/globalState/GlobalState.res
Lines 1050 to 1054 in d476b6e
When the EventBatchProcessed action gets dispatched, it will be an invalid action and all we want it to do is set currently processing to false and then dispatch the rollback task again.
I just added an extra check here that rollback state will be RollingBack so that we don't match on this case if it were produced say by this preRegistration feature.
d476b6e to
e206024
Compare
Closes #311
The bug was being caused by a stale action being dispatched (from before the restart after pre registration)
This PR: