From d3dba9d11c8bae69d2c58fba1258c93372292718 Mon Sep 17 00:00:00 2001 From: Yaco 0x Date: Wed, 7 Aug 2024 16:20:42 -0300 Subject: [PATCH] feat: handle invalid state for duplicate request --- packages/automated-dispute/src/eboActor.ts | 9 +++++++++ packages/automated-dispute/src/exceptions/index.ts | 1 + .../src/exceptions/invalidActorState.exception.ts | 8 ++++++++ packages/automated-dispute/src/interfaces/eboRegistry.ts | 7 +++++++ 4 files changed, 25 insertions(+) create mode 100644 packages/automated-dispute/src/exceptions/invalidActorState.exception.ts diff --git a/packages/automated-dispute/src/eboActor.ts b/packages/automated-dispute/src/eboActor.ts index e734fc8..ffc91c3 100644 --- a/packages/automated-dispute/src/eboActor.ts +++ b/packages/automated-dispute/src/eboActor.ts @@ -3,6 +3,7 @@ import { Caip2ChainId } from "@ebo-agent/blocknumber/dist/types.js"; import { ILogger } from "@ebo-agent/shared"; import { ContractFunctionRevertedError } from "viem"; +import { InvalidActorState } from "./exceptions/invalidActorState.exception.js"; import { RequestMismatch } from "./exceptions/requestMismatch.js"; import { EboRegistry } from "./interfaces/eboRegistry.js"; import { ProtocolProvider } from "./protocolProvider.js"; @@ -27,6 +28,14 @@ export class EboActor { if (event.metadata.requestId != this.requestId) throw new RequestMismatch(this.requestId, event.metadata.requestId); + if (this.registry.getRequest(event.metadata.requestId)) { + this.logger.error( + `The request ${event.metadata.requestId} was already being handled by an actor.`, + ); + + throw new InvalidActorState(); + } + this.registry.addRequest(event.metadata.requestId, event.metadata.request); if (this.anyActiveProposal()) { diff --git a/packages/automated-dispute/src/exceptions/index.ts b/packages/automated-dispute/src/exceptions/index.ts index 7e1aa47..4050512 100644 --- a/packages/automated-dispute/src/exceptions/index.ts +++ b/packages/automated-dispute/src/exceptions/index.ts @@ -1 +1,2 @@ export * from "./rpcUrlsEmpty.exception.js"; +export * from "./invalidActorState.exception.js"; diff --git a/packages/automated-dispute/src/exceptions/invalidActorState.exception.ts b/packages/automated-dispute/src/exceptions/invalidActorState.exception.ts new file mode 100644 index 0000000..74f8d47 --- /dev/null +++ b/packages/automated-dispute/src/exceptions/invalidActorState.exception.ts @@ -0,0 +1,8 @@ +export class InvalidActorState extends Error { + constructor() { + // TODO: we'll want to dump the Actor state into stderr at this point + super("The actor is in an invalid state."); + + this.name = "InvalidActorState"; + } +} diff --git a/packages/automated-dispute/src/interfaces/eboRegistry.ts b/packages/automated-dispute/src/interfaces/eboRegistry.ts index 0a987c7..411b024 100644 --- a/packages/automated-dispute/src/interfaces/eboRegistry.ts +++ b/packages/automated-dispute/src/interfaces/eboRegistry.ts @@ -10,6 +10,13 @@ export interface EboRegistry { */ addRequest(requestId: string, request: Request): void; + /** + * Get a `Request` by ID. + * + * @param requestId request ID + */ + getRequest(requestId: string): Request; + /** * Return all responses *