Skip to content

Commit

Permalink
fix: use actor request to validate if event should be handled
Browse files Browse the repository at this point in the history
  • Loading branch information
0xyaco committed Aug 8, 2024
1 parent e6068bd commit 82b982f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 4 additions & 6 deletions packages/automated-dispute/src/eboActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class EboActor {
* @returns void
*/
public async onResponseProposed(event: EboEvent<"ResponseProposed">): Promise<void> {
this.validateRequestPresent(event.metadata.requestId);
this.shouldHandleRequest(event.metadata.requestId);

this.registry.addResponse(event.metadata.responseId, event.metadata.response);

Expand Down Expand Up @@ -185,14 +185,12 @@ export class EboActor {
}

/**
* Validate if the actor is handling the request.
* Validate that the actor should handle the request by its ID.
*
* @param requestId request ID
*/
private validateRequestPresent(requestId: string) {
const request = this.registry.getRequest(requestId);

if (!request) {
private shouldHandleRequest(requestId: string) {
if (this.actorRequest.id.toLowerCase() !== requestId.toLowerCase()) {
this.logger.error(`The request ${requestId} is not handled by this actor.`);

// We want to fail the actor as receiving events from other requests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("onResponseProposed", () => {
});

it("throws if the response's request is not handled by actor", () => {
const { actor, registry } = mockEboActor({
const { actor } = mockEboActor({
requestId,
indexedChainId,
mockActorResponse: {
Expand All @@ -75,11 +75,15 @@ describe("onResponseProposed", () => {
},
});

vi.spyOn(registry, "getRequest").mockReturnValue(undefined);
const otherRequestEvent = {
...responseProposedEvent,
metadata: {
...responseProposedEvent.metadata,
requestId: responseProposedEvent.metadata.requestId + "123",
},
};

expect(actor.onResponseProposed(responseProposedEvent)).rejects.toThrowError(
InvalidActorState,
);
expect(actor.onResponseProposed(otherRequestEvent)).rejects.toThrowError(InvalidActorState);
});

it("does not dispute the response if seems valid", async () => {
Expand Down

0 comments on commit 82b982f

Please sign in to comment.