Skip to content

Commit

Permalink
fix: move and fix execution engine state update checks for online sta…
Browse files Browse the repository at this point in the history
…te (#5862)

* fix: move and fix execution engine state update checks for online state

* Fix the state change

---------

Co-authored-by: Nazar Hussain <[email protected]>
  • Loading branch information
g11tech and nazarhussain authored Aug 8, 2023
1 parent df2f4d3 commit a8ad567
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/beacon-node/src/execution/engine/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class ExecutionEngineHttp implements IExecutionEngine {
protected async fetchWithRetries<R, P = IJson[]>(payload: RpcPayload<P>, opts?: ReqOpts): Promise<R> {
try {
const res = await this.rpc.fetchWithRetries<R, P>(payload, opts);
this.updateEngineState(ExecutionEngineState.ONLINE);
this.updateEngineState(getExecutionEngineState({targetState: ExecutionEngineState.ONLINE, oldState: this.state}));
return res;
} catch (err) {
this.updateEngineState(getExecutionEngineState({payloadError: err, oldState: this.state}));
Expand Down
15 changes: 12 additions & 3 deletions packages/beacon-node/src/execution/engine/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,21 @@ function getExecutionEngineStateForPayloadError(
export function getExecutionEngineState<S extends ExecutionPayloadStatus | undefined, E extends unknown | undefined>({
payloadError,
payloadStatus,
targetState,
oldState,
}:
| {payloadStatus: S; payloadError?: never; oldState: ExecutionEngineState}
| {payloadStatus?: never; payloadError: E; oldState: ExecutionEngineState}): ExecutionEngineState {
| {payloadStatus: S; payloadError?: never; targetState?: never; oldState: ExecutionEngineState}
| {payloadStatus?: never; payloadError: E; targetState?: never; oldState: ExecutionEngineState}
| {
payloadStatus?: never;
payloadError?: never;
targetState: ExecutionEngineState;
oldState: ExecutionEngineState;
}): ExecutionEngineState {
const newState =
payloadStatus === undefined
targetState !== undefined
? targetState
: payloadStatus === undefined
? getExecutionEngineStateForPayloadError(payloadError, oldState)
: getExecutionEngineStateForPayloadStatus(payloadStatus);

Expand Down
51 changes: 51 additions & 0 deletions packages/beacon-node/test/unit/execution/engine/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,49 @@ describe("execution / engine / utils", () => {
],
];

const testCasesTargetState: Record<
ExecutionEngineState,
[oldState: ExecutionEngineState, newState: ExecutionEngineState][]
> = {
[ExecutionEngineState.ONLINE]: [
[ExecutionEngineState.ONLINE, ExecutionEngineState.ONLINE],
[ExecutionEngineState.AUTH_FAILED, ExecutionEngineState.ONLINE],
[ExecutionEngineState.OFFLINE, ExecutionEngineState.ONLINE],
// Once start syncing it should not go back to online state
// Online is an initial state when execution engine is created
[ExecutionEngineState.SYNCED, ExecutionEngineState.SYNCED],
[ExecutionEngineState.SYNCING, ExecutionEngineState.SYNCING],
],
[ExecutionEngineState.AUTH_FAILED]: [
[ExecutionEngineState.ONLINE, ExecutionEngineState.AUTH_FAILED],
[ExecutionEngineState.AUTH_FAILED, ExecutionEngineState.AUTH_FAILED],
[ExecutionEngineState.OFFLINE, ExecutionEngineState.AUTH_FAILED],
[ExecutionEngineState.SYNCED, ExecutionEngineState.AUTH_FAILED],
[ExecutionEngineState.SYNCING, ExecutionEngineState.AUTH_FAILED],
],
[ExecutionEngineState.OFFLINE]: [
[ExecutionEngineState.ONLINE, ExecutionEngineState.OFFLINE],
[ExecutionEngineState.AUTH_FAILED, ExecutionEngineState.OFFLINE],
[ExecutionEngineState.OFFLINE, ExecutionEngineState.OFFLINE],
[ExecutionEngineState.SYNCED, ExecutionEngineState.OFFLINE],
[ExecutionEngineState.SYNCING, ExecutionEngineState.OFFLINE],
],
[ExecutionEngineState.SYNCED]: [
[ExecutionEngineState.ONLINE, ExecutionEngineState.SYNCED],
[ExecutionEngineState.AUTH_FAILED, ExecutionEngineState.SYNCED],
[ExecutionEngineState.OFFLINE, ExecutionEngineState.SYNCED],
[ExecutionEngineState.SYNCED, ExecutionEngineState.SYNCED],
[ExecutionEngineState.SYNCING, ExecutionEngineState.SYNCED],
],
[ExecutionEngineState.SYNCING]: [
[ExecutionEngineState.ONLINE, ExecutionEngineState.SYNCING],
[ExecutionEngineState.AUTH_FAILED, ExecutionEngineState.SYNCING],
[ExecutionEngineState.OFFLINE, ExecutionEngineState.SYNCING],
[ExecutionEngineState.SYNCED, ExecutionEngineState.SYNCING],
[ExecutionEngineState.SYNCING, ExecutionEngineState.SYNCING],
],
};

for (const payloadStatus of Object.keys(testCasesPayload) as ExecutionPayloadStatus[]) {
for (const [oldState, newState] of testCasesPayload[payloadStatus]) {
it(`should transition from "${oldState}" to "${newState}" on payload status "${payloadStatus}"`, () => {
Expand All @@ -188,5 +231,13 @@ describe("execution / engine / utils", () => {
});
}
}

for (const targetState of Object.keys(testCasesTargetState) as ExecutionEngineState[]) {
for (const [oldState, newState] of testCasesTargetState[targetState]) {
it(`should transition from "${oldState}" to "${newState}" on when targeting "${targetState}"`, () => {
expect(getExecutionEngineState({targetState, oldState})).to.equal(newState);
});
}
}
});
});

0 comments on commit a8ad567

Please sign in to comment.