From e6068bde21840a0fd7196d42f2622d1d37d1774a Mon Sep 17 00:00:00 2001 From: Yaco 0x Date: Thu, 8 Aug 2024 15:38:57 -0300 Subject: [PATCH] refactor: pass epoch data with EboActor constructor --- packages/automated-dispute/src/eboActor.ts | 27 ++++++---- .../tests/eboActor/onRequestCreated.spec.ts | 51 +++++++++++-------- .../tests/eboActor/onResponseProposed.spec.ts | 22 +++++--- 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/packages/automated-dispute/src/eboActor.ts b/packages/automated-dispute/src/eboActor.ts index 702cf50..68edfd2 100644 --- a/packages/automated-dispute/src/eboActor.ts +++ b/packages/automated-dispute/src/eboActor.ts @@ -10,12 +10,20 @@ import { ProtocolProvider } from "./protocolProvider.js"; import { EboEvent } from "./types/events.js"; import { Dispute, Response } from "./types/prophet.js"; +/** + * Actor that handles a singular Prophet's request asking for the block number that corresponds + * to an instant on an indexed chain. + */ export class EboActor { constructor( + private readonly actorRequest: { + id: string; + epoch: bigint; + epochTimestamp: bigint; + }, private readonly protocolProvider: ProtocolProvider, private readonly blockNumberService: BlockNumberService, private readonly registry: EboRegistry, - private readonly requestId: string, private readonly logger: ILogger, ) {} @@ -25,8 +33,8 @@ export class EboActor { * @param event `RequestCreated` event */ public async onRequestCreated(event: EboEvent<"RequestCreated">): Promise { - if (event.metadata.requestId != this.requestId) - throw new RequestMismatch(this.requestId, event.metadata.requestId); + if (event.metadata.requestId != this.actorRequest.id) + throw new RequestMismatch(this.actorRequest.id, event.metadata.requestId); if (this.registry.getRequest(event.metadata.requestId)) { this.logger.error( @@ -42,7 +50,7 @@ export class EboActor { // Skipping new proposal until the actor receives a ResponseDisputed event; // at that moment, it will be possible to re-propose again. this.logger.info( - `There is an active proposal for request ${this.requestId}. Skipping...`, + `There is an active proposal for request ${this.actorRequest.id}. Skipping...`, ); return; @@ -55,7 +63,7 @@ export class EboActor { try { await this.protocolProvider.proposeResponse( - this.requestId, + this.actorRequest.id, response.epoch, response.chainId, response.block, @@ -68,7 +76,7 @@ export class EboActor { ); } else { this.logger.error( - `Actor handling request ${this.requestId} is not able to continue.`, + `Actor handling request ${this.actorRequest.id} is not able to continue.`, ); throw err; @@ -122,16 +130,13 @@ export class EboActor { * @returns a response body */ private async buildResponse(chainId: Caip2ChainId): Promise { - const { currentEpoch, currentEpochTimestamp } = - await this.protocolProvider.getCurrentEpoch(); - const epochBlockNumber = await this.blockNumberService.getEpochBlockNumber( - currentEpochTimestamp, + this.actorRequest.epochTimestamp, chainId, ); return { - epoch: currentEpoch, + epoch: this.actorRequest.epoch, chainId: chainId, block: epochBlockNumber, }; diff --git a/packages/automated-dispute/tests/eboActor/onRequestCreated.spec.ts b/packages/automated-dispute/tests/eboActor/onRequestCreated.spec.ts index f666289..ff87f1e 100644 --- a/packages/automated-dispute/tests/eboActor/onRequestCreated.spec.ts +++ b/packages/automated-dispute/tests/eboActor/onRequestCreated.spec.ts @@ -62,7 +62,6 @@ describe("EboActor", () => { it("proposes a response", async () => { const indexedEpochBlockNumber = 48n; - vi.spyOn(protocolProvider, "getCurrentEpoch").mockResolvedValue(protocolEpoch); vi.spyOn(blockNumberService, "getEpochBlockNumber").mockResolvedValue( indexedEpochBlockNumber, ); @@ -78,11 +77,17 @@ describe("EboActor", () => { ) => Promise.resolve(), ); + const requestConfig = { + id: requestId, + epoch: protocolEpoch.currentEpoch, + epochTimestamp: protocolEpoch.currentEpochTimestamp, + }; + const actor = new EboActor( + requestConfig, protocolProvider, blockNumberService, registry, - requestId, logger, ); @@ -99,7 +104,6 @@ describe("EboActor", () => { it("does not propose when already proposed the same block", async () => { const indexedEpochBlockNumber = 48n; - vi.spyOn(protocolProvider, "getCurrentEpoch").mockResolvedValue(protocolEpoch); vi.spyOn(blockNumberService, "getEpochBlockNumber").mockResolvedValue( indexedEpochBlockNumber, ); @@ -115,11 +119,17 @@ describe("EboActor", () => { ) => Promise.resolve(), ); + const requestConfig = { + id: requestId, + epoch: protocolEpoch.currentEpoch, + epochTimestamp: protocolEpoch.currentEpochTimestamp, + }; + const actor = new EboActor( + requestConfig, protocolProvider, blockNumberService, registry, - requestId, logger, ); @@ -154,42 +164,39 @@ describe("EboActor", () => { }, }; + const requestConfig = { + id: requestId, + epoch: protocolEpoch.currentEpoch, + epochTimestamp: protocolEpoch.currentEpochTimestamp, + }; + const actor = new EboActor( + requestConfig, protocolProvider, blockNumberService, registry, - requestId, logger, ); - expect(actor.onRequestCreated(noMatchRequestCreatedEvent)).rejects.toBeInstanceOf( + expect(actor.onRequestCreated(noMatchRequestCreatedEvent)).rejects.toThrowError( RequestMismatch, ); }); - it("throws if current epoch cannot be fetched", () => { - vi.spyOn(protocolProvider, "getCurrentEpoch").mockRejectedValue(new Error()); - - const actor = new EboActor( - protocolProvider, - blockNumberService, - registry, - requestId, - logger, - ); - - expect(actor.onRequestCreated(requestCreatedEvent)).rejects.toBeDefined(); - }); - it("throws if the indexed chain block number cannot be fetched", () => { - vi.spyOn(protocolProvider, "getCurrentEpoch").mockResolvedValue(protocolEpoch); vi.spyOn(blockNumberService, "getEpochBlockNumber").mockRejectedValue(new Error()); + const requestConfig = { + id: requestId, + epoch: protocolEpoch.currentEpoch, + epochTimestamp: protocolEpoch.currentEpochTimestamp, + }; + const actor = new EboActor( + requestConfig, protocolProvider, blockNumberService, registry, - requestId, logger, ); diff --git a/packages/automated-dispute/tests/eboActor/onResponseProposed.spec.ts b/packages/automated-dispute/tests/eboActor/onResponseProposed.spec.ts index 916463d..308f857 100644 --- a/packages/automated-dispute/tests/eboActor/onResponseProposed.spec.ts +++ b/packages/automated-dispute/tests/eboActor/onResponseProposed.spec.ts @@ -77,7 +77,7 @@ describe("onResponseProposed", () => { vi.spyOn(registry, "getRequest").mockReturnValue(undefined); - expect(actor.onResponseProposed(responseProposedEvent)).rejects.toBeInstanceOf( + expect(actor.onResponseProposed(responseProposedEvent)).rejects.toThrowError( InvalidActorState, ); }); @@ -141,15 +141,21 @@ function mockEboActor(mockedValues: { const blockNumberService = new BlockNumberService(chainRpcUrls, logger); const registry = new EboMemoryRegistry(); - const actor = new EboActor(protocolProvider, blockNumberService, registry, requestId, logger); + const requestConfig = { + id: requestId, + epoch: mockEpoch, + epochTimestamp: BigInt(Date.UTC(2024, 1, 1, 0, 0, 0, 0)), + }; - vi.spyOn(registry, "getRequest").mockReturnValue(DEFAULT_MOCKED_PROPHET_REQUEST); + const actor = new EboActor( + requestConfig, + protocolProvider, + blockNumberService, + registry, + logger, + ); - vi.spyOn(protocolProvider, "getCurrentEpoch").mockResolvedValue({ - currentEpochBlockNumber: 0n, - currentEpoch: mockEpoch, - currentEpochTimestamp: BigInt(Date.UTC(2024, 1, 1, 0, 0, 0, 0)), - }); + vi.spyOn(registry, "getRequest").mockReturnValue(DEFAULT_MOCKED_PROPHET_REQUEST); vi.spyOn(blockNumberService, "getEpochBlockNumber").mockResolvedValue(mockBlockNumber);