Skip to content

Commit

Permalink
refactor: pass epoch data with EboActor constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xyaco committed Aug 8, 2024
1 parent be6f331 commit e6068bd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
27 changes: 16 additions & 11 deletions packages/automated-dispute/src/eboActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}

Expand All @@ -25,8 +33,8 @@ export class EboActor {
* @param event `RequestCreated` event
*/
public async onRequestCreated(event: EboEvent<"RequestCreated">): Promise<void> {
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(
Expand All @@ -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;
Expand All @@ -55,7 +63,7 @@ export class EboActor {

try {
await this.protocolProvider.proposeResponse(
this.requestId,
this.actorRequest.id,
response.epoch,
response.chainId,
response.block,
Expand All @@ -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;
Expand Down Expand Up @@ -122,16 +130,13 @@ export class EboActor {
* @returns a response body
*/
private async buildResponse(chainId: Caip2ChainId): Promise<Response["response"]> {
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,
};
Expand Down
51 changes: 29 additions & 22 deletions packages/automated-dispute/tests/eboActor/onRequestCreated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
);

Expand All @@ -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,
);
Expand All @@ -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,
);

Expand Down Expand Up @@ -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,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
});
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit e6068bd

Please sign in to comment.