-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce action type for the hashgraph (#132)
- Loading branch information
Showing
13 changed files
with
727 additions
and
409 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
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
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,90 @@ | ||
import { Smush32 } from "@thi.ng/random"; | ||
import { | ||
ActionType, | ||
type CRO, | ||
type Hash, | ||
type Operation, | ||
type ResolveConflictsType, | ||
SemanticsType, | ||
type Vertex, | ||
} from "@topology-foundation/object"; | ||
|
||
const MOD = 1e9 + 9; | ||
|
||
function computeHash(s: string): number { | ||
let hash = 0; | ||
for (let i = 0; i < s.length; i++) { | ||
// Same as hash = hash * 31 + s.charCodeAt(i); | ||
hash = (hash << 5) - hash + s.charCodeAt(i); | ||
hash %= MOD; | ||
} | ||
return hash; | ||
} | ||
|
||
/* | ||
Example implementation of multi-vertex semantics that uses the reduce action type. | ||
An arbitrary number of concurrent operations can be reduced to a single operation. | ||
The winning operation is chosen using a pseudo-random number generator. | ||
*/ | ||
export class PseudoRandomWinsSet<T> implements CRO { | ||
operations: string[] = ["add", "remove"]; | ||
state: Map<T, boolean>; | ||
semanticsType = SemanticsType.multiple; | ||
|
||
constructor() { | ||
this.state = new Map<T, boolean>(); | ||
} | ||
|
||
private _add(value: T): void { | ||
if (!this.state.get(value)) this.state.set(value, true); | ||
} | ||
|
||
add(value: T): void { | ||
this._add(value); | ||
} | ||
|
||
private _remove(value: T): void { | ||
if (this.state.get(value)) this.state.set(value, false); | ||
} | ||
|
||
remove(value: T): void { | ||
this._remove(value); | ||
} | ||
|
||
contains(value: T): boolean { | ||
return this.state.get(value) === true; | ||
} | ||
|
||
values(): T[] { | ||
return Array.from(this.state.entries()) | ||
.filter(([_, exists]) => exists) | ||
.map(([value, _]) => value); | ||
} | ||
|
||
resolveConflicts(vertices: Vertex[]): ResolveConflictsType { | ||
vertices.sort((a, b) => (a.hash < b.hash ? -1 : 1)); | ||
const seed: string = vertices.map((vertex) => vertex.hash).join(""); | ||
const rnd = new Smush32(computeHash(seed)); | ||
const chosen = rnd.int() % vertices.length; | ||
const hashes: Hash[] = vertices.map((vertex) => vertex.hash); | ||
hashes.splice(chosen, 1); | ||
return { action: ActionType.Drop, vertices: hashes }; | ||
} | ||
|
||
// merged at HG level and called as a callback | ||
mergeCallback(operations: Operation[]): void { | ||
this.state = new Map<T, boolean>(); | ||
for (const op of operations) { | ||
switch (op.type) { | ||
case "add": | ||
if (op.value !== null) this._add(op.value); | ||
break; | ||
case "remove": | ||
if (op.value !== null) this._remove(op.value); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} |
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,35 @@ | ||
import { beforeEach, describe, expect, test } from "vitest"; | ||
import { PseudoRandomWinsSet } from "../src/cros/PseudoRandomWinsSet/index.js"; | ||
|
||
describe("HashGraph for PseudoRandomWinsSet tests", () => { | ||
let cro: PseudoRandomWinsSet<number>; | ||
|
||
beforeEach(() => { | ||
cro = new PseudoRandomWinsSet(); | ||
}); | ||
|
||
test("Test: Add", () => { | ||
cro.add(1); | ||
let set = cro.values(); | ||
expect(set).toEqual([1]); | ||
|
||
cro.add(2); | ||
set = cro.values(); | ||
expect(set).toEqual([1, 2]); | ||
}); | ||
|
||
test("Test: Add and Remove", () => { | ||
cro.add(1); | ||
let set = cro.values(); | ||
expect(set).toEqual([1]); | ||
|
||
cro.add(2); | ||
set = cro.values(); | ||
expect(set).toEqual([1, 2]); | ||
|
||
cro.remove(1); | ||
set = cro.values(); | ||
expect(cro.contains(1)).toBe(false); | ||
expect(set).toEqual([2]); | ||
}); | ||
}); |
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 +1 @@ | ||
export const VERSION = "0.1.1-3"; | ||
export const VERSION = "0.1.1"; |
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
Oops, something went wrong.