-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add missing ebo registry commands
- Loading branch information
Showing
12 changed files
with
337 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
packages/automated-dispute/src/exceptions/invalidActorState.exception.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/automated-dispute/src/services/eboRegistry/commands/addDispute.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { CommandAlreadyRun, CommandNotRun } from "../../../exceptions/index.js"; | ||
import { EboRegistry, EboRegistryCommand } from "../../../interfaces/index.js"; | ||
import { Dispute, EboEvent } from "../../../types/index.js"; | ||
|
||
export class AddDispute implements EboRegistryCommand { | ||
private wasRun: boolean = false; | ||
|
||
private constructor( | ||
private readonly registry: EboRegistry, | ||
private readonly dispute: Dispute, | ||
) {} | ||
|
||
public static buildFromEvent( | ||
event: EboEvent<"ResponseDisputed">, | ||
registry: EboRegistry, | ||
): AddDispute { | ||
const dispute: Dispute = { | ||
id: event.metadata.disputeId, | ||
createdAt: event.blockNumber, | ||
status: "Active", | ||
prophetData: event.metadata.dispute, | ||
}; | ||
|
||
return new AddDispute(registry, dispute); | ||
} | ||
|
||
run(): void { | ||
if (this.wasRun) throw new CommandAlreadyRun(AddDispute.name); | ||
|
||
this.registry.addDispute(this.dispute); | ||
this.wasRun = true; | ||
} | ||
|
||
undo(): void { | ||
if (!this.wasRun) throw new CommandNotRun(AddDispute.name); | ||
|
||
this.registry.removeDispute(this.dispute.id); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
packages/automated-dispute/src/services/eboRegistry/commands/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./addRequest.js"; | ||
export * from "./addResponse.js"; | ||
|
||
// TODO: add the rest of the commands | ||
export * from "./addDispute.js"; | ||
export * from "./noop.js"; | ||
export * from "./updateDisputeStatus.js"; |
22 changes: 22 additions & 0 deletions
22
packages/automated-dispute/src/services/eboRegistry/commands/noop.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { CommandAlreadyRun, CommandNotRun } from "../../../exceptions/index.js"; | ||
import { EboRegistryCommand } from "../../../interfaces/index.js"; | ||
|
||
export class Noop implements EboRegistryCommand { | ||
private wasRun: boolean = false; | ||
|
||
private constructor() {} | ||
|
||
public static buildFromEvent(): Noop { | ||
return new Noop(); | ||
} | ||
|
||
run(): void { | ||
if (this.wasRun) throw new CommandAlreadyRun(Noop.name); | ||
|
||
this.wasRun = true; | ||
} | ||
|
||
undo(): void { | ||
if (!this.wasRun) throw new CommandNotRun(Noop.name); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/automated-dispute/src/services/eboRegistry/commands/updateDisputeStatus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { CommandAlreadyRun, CommandNotRun, DisputeNotFound } from "../../../exceptions/index.js"; | ||
import { EboRegistry, EboRegistryCommand } from "../../../interfaces/index.js"; | ||
import { DisputeStatus, EboEvent } from "../../../types/index.js"; | ||
|
||
export class UpdateDisputeStatus implements EboRegistryCommand { | ||
private wasRun: boolean = false; | ||
private previousStatus?: DisputeStatus; | ||
|
||
private constructor( | ||
private readonly registry: EboRegistry, | ||
private readonly disputeId: string, | ||
private readonly status: DisputeStatus, | ||
) {} | ||
|
||
public static buildFromEvent( | ||
event: EboEvent<"DisputeStatusChanged">, | ||
registry: EboRegistry, | ||
): UpdateDisputeStatus { | ||
const disputeId = event.metadata.disputeId; | ||
const status = event.metadata.status; | ||
|
||
return new UpdateDisputeStatus(registry, disputeId, status); | ||
} | ||
|
||
run(): void { | ||
if (this.wasRun) throw new CommandAlreadyRun(UpdateDisputeStatus.name); | ||
|
||
const dispute = this.registry.getDispute(this.disputeId); | ||
|
||
if (!dispute) throw new DisputeNotFound(this.disputeId); | ||
|
||
this.previousStatus = dispute.status; | ||
|
||
this.registry.updateDisputeStatus(this.disputeId, this.status); | ||
|
||
this.wasRun = true; | ||
} | ||
|
||
undo(): void { | ||
if (!this.wasRun || !this.previousStatus) throw new CommandNotRun(UpdateDisputeStatus.name); | ||
|
||
this.registry.updateDisputeStatus(this.disputeId, this.previousStatus); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/automated-dispute/tests/services/eboMemoryRegistry/commands/addDispute.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { beforeEach, describe, expect, it, Mock, vi } from "vitest"; | ||
|
||
import { CommandAlreadyRun, CommandNotRun } from "../../../../src/exceptions/index.js"; | ||
import { EboRegistry } from "../../../../src/interfaces/index.js"; | ||
import { AddDispute } from "../../../../src/services/index.js"; | ||
import { EboEvent } from "../../../../src/types/index.js"; | ||
import { DEFAULT_MOCKED_REQUEST_CREATED_DATA } from "../../../eboActor/fixtures.js"; | ||
import mocks from "../../../mocks/index.js"; | ||
|
||
describe("AddDispute", () => { | ||
let registry: EboRegistry; | ||
|
||
const request = DEFAULT_MOCKED_REQUEST_CREATED_DATA; | ||
const response = mocks.buildResponse(request); | ||
const dispute = mocks.buildDispute(request, response); | ||
|
||
const event: EboEvent<"ResponseDisputed"> = { | ||
name: "ResponseDisputed", | ||
blockNumber: 1n, | ||
logIndex: 1, | ||
requestId: request.id, | ||
metadata: { | ||
dispute: dispute.prophetData, | ||
disputeId: dispute.id, | ||
responseId: response.id, | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
registry = { | ||
addDispute: vi.fn(), | ||
removeDispute: vi.fn(), | ||
} as unknown as EboRegistry; | ||
}); | ||
|
||
describe("run", () => { | ||
it("adds the dispute to the registry", () => { | ||
const command = AddDispute.buildFromEvent(event, registry); | ||
|
||
command.run(); | ||
|
||
expect(registry.addDispute).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
id: dispute.id, | ||
}), | ||
); | ||
}); | ||
|
||
it("throws if the command was already run", () => { | ||
const command = AddDispute.buildFromEvent(event, registry); | ||
|
||
command.run(); | ||
|
||
expect(() => command.run()).toThrow(CommandAlreadyRun); | ||
}); | ||
}); | ||
|
||
describe("undo", () => { | ||
it("removes the added request", () => { | ||
const command = AddDispute.buildFromEvent(event, registry); | ||
|
||
const mockRemoveDispute = registry.removeDispute as Mock; | ||
|
||
command.run(); | ||
command.undo(); | ||
|
||
expect(mockRemoveDispute).toHaveBeenCalledWith(request.id); | ||
}); | ||
|
||
it("throws if undoing the command before being run", () => { | ||
const command = AddDispute.buildFromEvent(event, registry); | ||
|
||
expect(() => command.undo()).toThrow(CommandNotRun); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.