Skip to content
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

refactor: move peer id from drpObject to hashgraph #467

Merged
merged 7 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Vertex_Operation as Operation, Vertex } from "@ts-drp/types";
import { type Vertex_Operation as Operation, Vertex } from "@ts-drp/types";

import { log } from "../index.js";
import { BitSet } from "./bitset.js";
import { linearizeMultipleSemantics } from "../linearize/multipleSemantics.js";
import { linearizePairSemantics } from "../linearize/pairSemantics.js";
import { computeHash } from "../utils/computeHash.js";
import { ObjectSet } from "../utils/objectSet.js";

// Reexporting the Vertex and Operation types from the protobuf file
Expand Down Expand Up @@ -105,6 +106,16 @@ export class HashGraph {
: { action: ActionType.Nop };
}

createVertex(operation: Operation, dependencies: Hash[], timestamp: number): Vertex {
return Vertex.create({
hash: computeHash(this.peerId, operation, dependencies, timestamp),
peerId: this.peerId,
timestamp,
operation,
dependencies,
});
}

addToFrontier(vertex: Vertex) {
this.vertices.set(vertex.hash, vertex);
// Update forward edges
Expand Down
44 changes: 16 additions & 28 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { Logger, type LoggerOptions } from "@ts-drp/logger";
import { DRPObjectBase, DRPState, DRPStateEntry, ObjectPb } from "@ts-drp/types";
import {
DRPObjectBase,
DRPState,
DRPStateEntry,
type Vertex_Operation as Operation,
type Vertex,
} from "@ts-drp/types";
import { cloneDeep } from "es-toolkit";
import { deepEqual } from "fast-equals";
import * as crypto from "node:crypto";

import { ObjectACL } from "./acl/index.js";
import type { ACL } from "./acl/interface.js";
import { type FinalityConfig, FinalityStore } from "./finality/index.js";
import { type Hash, HashGraph, type Operation, type Vertex } from "./hashgraph/index.js";
import { type Hash, HashGraph } from "./hashgraph/index.js";
import {
type DRP,
type DRPObjectCallback,
type DRPPublicCredential,
DrpType,
type LcaAndOperations,
} from "./interface.js";
import { computeHash } from "./utils/computeHash.js";
import { ObjectSet } from "./utils/objectSet.js";

export * from "./utils/serializer.js";
Expand All @@ -33,7 +40,6 @@ export let log: Logger;

export class DRPObject implements DRPObjectBase {
id: string;
peerId: string;
vertices: Vertex[] = [];
acl?: ProxyHandler<ACL>;
drp?: ProxyHandler<DRP>;
Expand All @@ -59,7 +65,6 @@ export class DRPObject implements DRPObjectBase {
throw new Error("Either publicCredential or acl must be provided to create a DRPObject");
}

this.peerId = options.peerId;
log = new Logger("drp::object", options.config?.log_config);
this.id =
options.id ??
Expand All @@ -77,9 +82,9 @@ export class DRPObject implements DRPObjectBase {
});
this.acl = new Proxy(objAcl, this.proxyDRPHandler(DrpType.ACL));
if (options.drp) {
this._initLocalDrpInstance(options.drp, objAcl);
this._initLocalDrpInstance(options.peerId, options.drp, objAcl);
} else {
this._initNonLocalDrpInstance(objAcl);
this._initNonLocalDrpInstance(options.peerId, objAcl);
}

this.aclStates = new Map([[HashGraph.rootHash, DRPState.create()]]);
Expand All @@ -91,19 +96,19 @@ export class DRPObject implements DRPObjectBase {
this.originalDRP = cloneDeep(options.drp);
}

private _initLocalDrpInstance(drp: DRP, acl: DRP) {
private _initLocalDrpInstance(peerId: string, drp: DRP, acl: DRP) {
this.drp = new Proxy(drp, this.proxyDRPHandler(DrpType.DRP));
this.hashGraph = new HashGraph(
this.peerId,
peerId,
acl.resolveConflicts.bind(acl),
drp.resolveConflicts.bind(drp),
drp.semanticsType
);
this.vertices = this.hashGraph.getAllVertices();
}

private _initNonLocalDrpInstance(acl: DRP) {
this.hashGraph = new HashGraph(this.peerId, acl.resolveConflicts.bind(this.acl));
private _initNonLocalDrpInstance(peerId: string, acl: DRP) {
this.hashGraph = new HashGraph(peerId, acl.resolveConflicts.bind(this.acl));
this.vertices = this.hashGraph.getAllVertices();
}

Expand Down Expand Up @@ -192,13 +197,7 @@ export class DRPObject implements DRPObjectBase {
? [this._computeDRP(vertexDependencies, preComputeLca), clonedDRP as ACL]
: [clonedDRP as DRP, this._computeObjectACL(vertexDependencies, preComputeLca)];

const vertex = ObjectPb.Vertex.create({
hash: computeHash(this.peerId, vertexOperation, vertexDependencies, now),
peerId: this.peerId,
operation: vertexOperation,
dependencies: vertexDependencies,
timestamp: now,
});
const vertex = this.hashGraph.createVertex(vertexOperation, vertexDependencies, now);

this.hashGraph.addToFrontier(vertex);
this._setDRPState(vertex, preComputeLca, this._getDRPState(drp));
Expand Down Expand Up @@ -543,17 +542,6 @@ export class DRPObject implements DRPObjectBase {
}
}

function computeHash(
peerId: string,
operation: Operation | undefined,
deps: Hash[],
timestamp: number
): Hash {
const serialized = JSON.stringify({ operation, deps, peerId, timestamp });
const hash = crypto.createHash("sha256").update(serialized).digest("hex");
return hash;
}

export function newVertex(
peerId: string,
operation: Operation,
Expand Down
3 changes: 2 additions & 1 deletion packages/object/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ObjectPb } from "@ts-drp/types";
import { type Vertex_Operation as Operation, Vertex } from "@ts-drp/types";

import type { Operation, ResolveConflictsType, SemanticsType, Vertex } from "./hashgraph/index.js";
import type { ResolveConflictsType, SemanticsType } from "./hashgraph/index.js";
import type { DRPObject } from "./index.js";

export enum DrpType {
Expand Down
10 changes: 3 additions & 7 deletions packages/object/src/linearize/multipleSemantics.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
ActionType,
type Hash,
type HashGraph,
type Operation,
type Vertex,
} from "../hashgraph/index.js";
import { type Vertex_Operation as Operation, Vertex } from "@ts-drp/types";

import { ActionType, type Hash, type HashGraph } from "../hashgraph/index.js";
import type { ObjectSet } from "../utils/objectSet.js";

export function linearizeMultipleSemantics(
Expand Down
4 changes: 3 additions & 1 deletion packages/object/src/linearize/pairSemantics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ActionType, type Hash, type HashGraph, type Operation } from "../hashgraph/index.js";
import { type Vertex_Operation as Operation } from "@ts-drp/types";

import { ActionType, type Hash, type HashGraph } from "../hashgraph/index.js";
import type { ObjectSet } from "../utils/objectSet.js";

export function linearizePairSemantics(
Expand Down
15 changes: 15 additions & 0 deletions packages/object/src/utils/computeHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Vertex_Operation as Operation } from "@ts-drp/types";
import * as crypto from "node:crypto";

import type { Hash } from "../hashgraph/index.js";

export function computeHash(
peerId: string,
operation: Operation | undefined,
deps: Hash[],
timestamp: number
): Hash {
const serialized = JSON.stringify({ operation, deps, peerId, timestamp });
const hash = crypto.createHash("sha256").update(serialized).digest("hex");
return hash;
}
3 changes: 2 additions & 1 deletion packages/object/tests/hashgraph.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { MapConflictResolution, MapDRP } from "@ts-drp/blueprints/src/Map/index.js";
import { SetDRP } from "@ts-drp/blueprints/src/Set/index.js";
import { Vertex } from "@ts-drp/types";
import { beforeAll, beforeEach, describe, expect, test, vi } from "vitest";

import { ObjectACL } from "../src/acl/index.js";
import { ActionType, SemanticsType, Vertex } from "../src/hashgraph/index.js";
import { ActionType, SemanticsType } from "../src/hashgraph/index.js";
import {
ACLGroup,
DRP,
Expand Down
3 changes: 1 addition & 2 deletions packages/object/tests/linearize.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, expect, test, vi, beforeEach, afterEach } from "vitest";

import { ActionType } from "../dist/src/hashgraph/index.js";
import { SemanticsType } from "../dist/src/hashgraph/index.js";
import { SemanticsType, ActionType } from "../dist/src/hashgraph/index.js";
import { DrpType, HashGraph, newVertex, type Vertex } from "../src/index.js";
import { linearizeMultipleSemantics } from "../src/linearize/multipleSemantics.js";
import { linearizePairSemantics } from "../src/linearize/pairSemantics.js";
Expand Down
Loading