From fad21f87b291315520402d4f323f4ce402f4c9fc Mon Sep 17 00:00:00 2001 From: 0xyaco Date: Fri, 23 Aug 2024 16:16:38 -0300 Subject: [PATCH] feat: base ebo processor (#26) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 🤖 Linear Closes GRT-57 ## Description * Scaffolds the base of `EboProcessor` class * Adds unit tests as specs for this class * Creates a much needed `type RequestId` --- .../automated-dispute/src/exceptions/index.ts | 4 +- ...smatch.ts => requestMismatch.exception.ts} | 0 ...s => responseAlreadyProposed.exception.ts} | 0 .../automated-dispute/src/protocolProvider.ts | 4 ++ .../src/services/eboProcessor.ts | 40 +++++++++++++++++++ .../automated-dispute/src/services/index.ts | 1 + .../automated-dispute/src/types/events.ts | 1 + .../automated-dispute/src/types/prophet.ts | 8 ++-- .../tests/services/eboProcessor.spec.ts | 13 ++++++ 9 files changed, 66 insertions(+), 5 deletions(-) rename packages/automated-dispute/src/exceptions/{requestMismatch.ts => requestMismatch.exception.ts} (100%) rename packages/automated-dispute/src/exceptions/{responseAlreadyProposed.ts => responseAlreadyProposed.exception.ts} (100%) create mode 100644 packages/automated-dispute/src/services/eboProcessor.ts create mode 100644 packages/automated-dispute/src/services/index.ts create mode 100644 packages/automated-dispute/tests/services/eboProcessor.spec.ts diff --git a/packages/automated-dispute/src/exceptions/index.ts b/packages/automated-dispute/src/exceptions/index.ts index 252c940..840498a 100644 --- a/packages/automated-dispute/src/exceptions/index.ts +++ b/packages/automated-dispute/src/exceptions/index.ts @@ -1,6 +1,6 @@ export * from "./invalidActorState.exception.js"; export * from "./invalidDisputeStatus.exception.js"; export * from "./requestAlreadyHandled.exception.js"; -export * from "./requestMismatch.js"; -export * from "./responseAlreadyProposed.js"; +export * from "./requestMismatch.exception.js"; +export * from "./responseAlreadyProposed.exception.js"; export * from "./rpcUrlsEmpty.exception.js"; diff --git a/packages/automated-dispute/src/exceptions/requestMismatch.ts b/packages/automated-dispute/src/exceptions/requestMismatch.exception.ts similarity index 100% rename from packages/automated-dispute/src/exceptions/requestMismatch.ts rename to packages/automated-dispute/src/exceptions/requestMismatch.exception.ts diff --git a/packages/automated-dispute/src/exceptions/responseAlreadyProposed.ts b/packages/automated-dispute/src/exceptions/responseAlreadyProposed.exception.ts similarity index 100% rename from packages/automated-dispute/src/exceptions/responseAlreadyProposed.ts rename to packages/automated-dispute/src/exceptions/responseAlreadyProposed.exception.ts diff --git a/packages/automated-dispute/src/protocolProvider.ts b/packages/automated-dispute/src/protocolProvider.ts index 76a178d..7d35550 100644 --- a/packages/automated-dispute/src/protocolProvider.ts +++ b/packages/automated-dispute/src/protocolProvider.ts @@ -90,6 +90,7 @@ export class ProtocolProvider { name: "RequestCreated", blockNumber: 1n, logIndex: 1, + requestId: "0x01", metadata: { requestId: "0x01", chainId: "eip155:1", @@ -111,6 +112,7 @@ export class ProtocolProvider { name: "ResponseProposed", blockNumber: 2n, logIndex: 1, + requestId: "0x01", metadata: { requestId: "0x01", responseId: "0x02", @@ -129,6 +131,7 @@ export class ProtocolProvider { name: "ResponseDisputed", blockNumber: 3n, logIndex: 1, + requestId: "0x01", metadata: { requestId: "0x01", responseId: "0x02", @@ -145,6 +148,7 @@ export class ProtocolProvider { name: "DisputeStatusChanged", blockNumber: 4n, logIndex: 20, + requestId: "0x01", metadata: { disputeId: "0x03", status: "Won", blockNumber: 4n }, } as EboEvent<"DisputeStatusChanged">, ]; diff --git a/packages/automated-dispute/src/services/eboProcessor.ts b/packages/automated-dispute/src/services/eboProcessor.ts new file mode 100644 index 0000000..da0e6a3 --- /dev/null +++ b/packages/automated-dispute/src/services/eboProcessor.ts @@ -0,0 +1,40 @@ +import { ILogger } from "@ebo-agent/shared"; + +import { EboActor } from "../eboActor.js"; +import { EboActorsManager } from "../eboActorsManager.js"; +import { ProtocolProvider } from "../protocolProvider.js"; + +const DEFAULT_MS_BETWEEN_CHECKS = 10 * 60 * 1000; // 10 minutes + +export class EboProcessor { + private eventsInterval?: NodeJS.Timeout; + private lastCheckedBlock?: bigint; + + constructor( + private readonly protocolProvider: ProtocolProvider, + private readonly actorsManager: EboActorsManager, + private readonly logger: ILogger, + ) {} + + public async start(msBetweenChecks: number = DEFAULT_MS_BETWEEN_CHECKS) { + this.bootstrap(); // Bootstrapping + + this.eventsInterval = setInterval(this.eventLoop, msBetweenChecks); + } + + private async bootstrap() { + // TODO + } + + private async eventLoop() { + // TODO + } + + private async onActorError(_actor: EboActor, _error: Error) { + // TODO + } + + private async notifyError(_error: Error) { + // TODO + } +} diff --git a/packages/automated-dispute/src/services/index.ts b/packages/automated-dispute/src/services/index.ts new file mode 100644 index 0000000..448cab7 --- /dev/null +++ b/packages/automated-dispute/src/services/index.ts @@ -0,0 +1 @@ +export * from "./eboProcessor.js"; diff --git a/packages/automated-dispute/src/types/events.ts b/packages/automated-dispute/src/types/events.ts index 72b4c5e..62df660 100644 --- a/packages/automated-dispute/src/types/events.ts +++ b/packages/automated-dispute/src/types/events.ts @@ -69,5 +69,6 @@ export type EboEvent = { blockNumber: bigint; logIndex: number; rawLog?: Log; + requestId: string; // Field to use to route events to actors metadata: EboEventData; }; diff --git a/packages/automated-dispute/src/types/prophet.ts b/packages/automated-dispute/src/types/prophet.ts index 97195ac..d8564ff 100644 --- a/packages/automated-dispute/src/types/prophet.ts +++ b/packages/automated-dispute/src/types/prophet.ts @@ -2,8 +2,10 @@ import { Caip2ChainId } from "@ebo-agent/blocknumber/dist/types.js"; import { Timestamp } from "@ebo-agent/shared"; import { Address } from "viem"; +export type RequestId = string; + export interface Request { - id: string; + id: RequestId; chainId: Caip2ChainId; epoch: bigint; epochTimestamp: Timestamp; @@ -25,7 +27,7 @@ export interface Response { prophetData: Readonly<{ proposer: Address; - requestId: string; + requestId: RequestId; // To be byte-encode when sending it to Prophet response: { @@ -48,6 +50,6 @@ export interface Dispute { disputer: Address; proposer: Address; responseId: string; - requestId: string; + requestId: RequestId; }; } diff --git a/packages/automated-dispute/tests/services/eboProcessor.spec.ts b/packages/automated-dispute/tests/services/eboProcessor.spec.ts new file mode 100644 index 0000000..b64b0fd --- /dev/null +++ b/packages/automated-dispute/tests/services/eboProcessor.spec.ts @@ -0,0 +1,13 @@ +import { describe, it } from "vitest"; + +describe("EboProcessor", () => { + describe.skip("start", () => { + it.skip("bootstraps actors with onchain active requests when starting"); + it.skip("fetches events since epoch start when starting"); + it.skip("fetches events since last block checked after first events fetch"); + it.skip("registers new actor when a new request id is detected on events"); + it.skip("forwards events to corresponding actors"); + it.skip("notifies if an actor throws while handling events"); + it.skip("removes the actor when processing onFinalizeRequest event"); + }); +});