diff --git a/packages/object/src/hashgraph/index.ts b/packages/object/src/hashgraph/index.ts index bfa329cf..45518b07 100644 --- a/packages/object/src/hashgraph/index.ts +++ b/packages/object/src/hashgraph/index.ts @@ -164,7 +164,7 @@ export class HashGraph { throw new Error("Invalid dependency detected."); } if (vertex.timestamp > timestamp) { - // Vertex's timestamp must not be less than its dependencies + // Vertex's timestamp must not be less than any of its dependencies' timestamps throw new Error("Invalid timestamp detected."); } } diff --git a/packages/object/tests/hashgraph.test.ts b/packages/object/tests/hashgraph.test.ts index a906b9ab..9f2fa9d6 100644 --- a/packages/object/tests/hashgraph.test.ts +++ b/packages/object/tests/hashgraph.test.ts @@ -619,3 +619,66 @@ describe("Vertex state tests", () => { expect(drpStateV8?.state.get("state").get(3)).toBe(undefined); }); }); + +describe("Vertex timestamp tests", () => { + let obj1: DRPObject; + let obj2: DRPObject; + let obj3: DRPObject; + + beforeEach(async () => { + obj1 = new DRPObject("peer1", new AddWinsSet()); + obj2 = new DRPObject("peer1", new AddWinsSet()); + obj3 = new DRPObject("peer1", new AddWinsSet()); + }); + + test("Test: Vertex created in the future is invalid", () => { + const drp1 = obj1.drp as AddWinsSet; + + drp1.add(1); + + expect(() => + obj1.hashGraph.addVertex( + { + type: "add", + value: 1, + }, + obj1.hashGraph.getFrontier(), + "", + Number.POSITIVE_INFINITY, + ), + ).toThrowError("Invalid timestamp detected."); + }); + + test("Test: Vertex's timestamp must not be less than any of its dependencies' timestamps", () => { + /* + __ V1:ADD(1) __ + / \ + ROOT -- V2:ADD(2) ---- V1:ADD(4) (invalid) + \ / + -- V3:ADD(3) -- + */ + + const drp1 = obj1.drp as AddWinsSet; + const drp2 = obj2.drp as AddWinsSet; + const drp3 = obj2.drp as AddWinsSet; + + drp1.add(1); + drp2.add(2); + drp3.add(3); + + obj1.merge(obj2.hashGraph.getAllVertices()); + obj1.merge(obj3.hashGraph.getAllVertices()); + + expect(() => + obj1.hashGraph.addVertex( + { + type: "add", + value: 1, + }, + obj1.hashGraph.getFrontier(), + "", + 1, + ), + ).toThrowError("Invalid timestamp detected."); + }); +});