-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(object): linearize pair semantic (#476)
- Loading branch information
1 parent
23abdfd
commit 8ffdcec
Showing
2 changed files
with
263 additions
and
28 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
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,240 @@ | ||
import { newVertex } from "@ts-drp/object/src/index.js"; | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { ActionType, HashGraph, type Vertex, SemanticsType } from "../../src/hashgraph/index.js"; | ||
import { DrpType } from "../../src/index.js"; | ||
import { linearizeMultipleSemantics } from "../../src/linearize/multipleSemantics.js"; | ||
import { ObjectSet } from "../../src/utils/objectSet.js"; | ||
|
||
describe("linearizeMultipleSemantics", () => { | ||
test("should linearize operations in a simple sequence", () => { | ||
const hashGraph = new HashGraph( | ||
"", | ||
(_vertices: Vertex[]) => ({ | ||
action: ActionType.Nop, | ||
}), | ||
() => ({ | ||
action: ActionType.Nop, | ||
}), | ||
SemanticsType.multiple | ||
); | ||
const origin = HashGraph.rootHash; | ||
|
||
// Add vertices to the graph | ||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [1], | ||
drpType: DrpType.DRP, | ||
}, | ||
hashGraph.getFrontier(), | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [2], | ||
drpType: DrpType.DRP, | ||
}, | ||
hashGraph.getFrontier(), | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
const subgraph = new ObjectSet<string>(); | ||
hashGraph.getAllVertices().forEach((vertex) => subgraph.add(vertex.hash)); | ||
|
||
const result = linearizeMultipleSemantics(hashGraph, origin, subgraph); | ||
expect(result.map((op) => op.value)).toEqual([[1], [2]]); | ||
}); | ||
|
||
test("should handle concurrent operations with conflict resolution", () => { | ||
const hashGraph = new HashGraph( | ||
"", | ||
(_vertices: Vertex[]) => ({ | ||
action: ActionType.Drop, | ||
vertices: _vertices.filter((_, index) => index !== 0).map((vertex) => vertex.hash), | ||
}), | ||
(_vertices: Vertex[]) => ({ | ||
action: ActionType.Drop, | ||
vertices: _vertices.filter((_, index) => index !== 0).map((vertex) => vertex.hash), | ||
}), | ||
SemanticsType.multiple | ||
); | ||
const origin = HashGraph.rootHash; | ||
let frontier = hashGraph.getFrontier(); | ||
|
||
// Add concurrent vertices | ||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [1], | ||
drpType: DrpType.DRP, | ||
}, | ||
frontier, | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [2], | ||
drpType: DrpType.DRP, | ||
}, | ||
frontier, | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [3], | ||
drpType: DrpType.DRP, | ||
}, | ||
frontier, | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
// Get the frontier | ||
frontier = hashGraph.getFrontier(); | ||
|
||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [4], | ||
drpType: DrpType.DRP, | ||
}, | ||
frontier, | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
console.log(`frontier: ${frontier}`); | ||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [5], | ||
drpType: DrpType.DRP, | ||
}, | ||
frontier.filter((_, idx) => idx !== 0), | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
frontier = hashGraph.getFrontier(); | ||
|
||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [6], | ||
drpType: DrpType.DRP, | ||
}, | ||
frontier, | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
const subgraph = new ObjectSet<string>(); | ||
hashGraph.getAllVertices().forEach((vertex) => subgraph.add(vertex.hash)); | ||
|
||
const result = linearizeMultipleSemantics(hashGraph, origin, subgraph); | ||
expect(result.map((op) => op.value)).toEqual([[1], [4], [6]]); | ||
Check failure on line 169 in packages/object/tests/linearize/multi.test.ts
|
||
}); | ||
|
||
test("should handle operations with null values", () => { | ||
const hashGraph = new HashGraph( | ||
"", | ||
() => ({ | ||
action: ActionType.Nop, | ||
}), | ||
() => ({ | ||
action: ActionType.Nop, | ||
}), | ||
SemanticsType.multiple | ||
); | ||
const origin = HashGraph.rootHash; | ||
|
||
// Add vertices to the graph | ||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: null, | ||
drpType: DrpType.DRP, | ||
}, | ||
hashGraph.getFrontier(), | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
hashGraph.addVertex( | ||
newVertex( | ||
"", | ||
{ | ||
opType: "set", | ||
value: [2], | ||
drpType: DrpType.DRP, | ||
}, | ||
hashGraph.getFrontier(), | ||
Date.now(), | ||
new Uint8Array() | ||
) | ||
); | ||
|
||
const subgraph = new ObjectSet<string>(); | ||
hashGraph.getAllVertices().forEach((vertex) => subgraph.add(vertex.hash)); | ||
|
||
const result = linearizeMultipleSemantics(hashGraph, origin, subgraph); | ||
expect(result.map((op) => op.value)).toEqual([[2]]); | ||
}); | ||
|
||
test("should handle empty subgraph", () => { | ||
const hashGraph = new HashGraph( | ||
"", | ||
() => ({ | ||
action: ActionType.Nop, | ||
}), | ||
() => ({ | ||
action: ActionType.Nop, | ||
}), | ||
SemanticsType.multiple | ||
); | ||
const origin = HashGraph.rootHash; | ||
|
||
const subgraph = new ObjectSet<string>(); | ||
subgraph.add(origin); | ||
|
||
const result = linearizeMultipleSemantics(hashGraph, origin, subgraph); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |