-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: pending registry commands #32
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure why are we skiping this validation.