Skip to content

Commit

Permalink
feat: add timestamp to vertex (topology-foundation#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
magnified103 authored Dec 29, 2024
1 parent 4220e72 commit b2a0f38
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 66 deletions.
34 changes: 31 additions & 3 deletions packages/network/src/proto/drp/object/v1/object_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export async function verifyIncomingVertices(
value: vertex.operation?.value,
},
dependencies: vertex.dependencies,
timestamp: vertex.timestamp,
signature: vertex.signature,
};
});
Expand Down
40 changes: 29 additions & 11 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ export class HashGraph {
/*
computeHash(
"",
{ type: OperationType.NOP },
{ type: OperationType.NOP, value: null },
[],
)
-1,
);
*/
static readonly rootHash: Hash =
"a65c9cbd875fd3d602adb69a90adb98c4e2c3f26bdf3a2bf597f3548971f2c93";
"425d2b1f5243dbf23c685078034b06fbfa71dc31dcce30f614e28023f140ff13";
private arePredecessorsFresh = false;
private reachablePredecessors: Map<Hash, BitSet> = new Map();
private topoSortedIndex: Map<Hash, number> = new Map();
Expand All @@ -89,6 +90,7 @@ export class HashGraph {
value: null,
},
dependencies: [],
timestamp: -1,
signature: "",
};
this.vertices.set(HashGraph.rootHash, rootVertex);
Expand All @@ -101,13 +103,15 @@ export class HashGraph {

addToFrontier(operation: Operation): Vertex {
const deps = this.getFrontier();
const hash = computeHash(this.peerId, operation, deps);
const currentTimestamp = Date.now();
const hash = computeHash(this.peerId, operation, deps, currentTimestamp);

const vertex: Vertex = {
hash,
peerId: this.peerId,
operation: operation ?? { type: OperationType.NOP },
dependencies: deps,
timestamp: currentTimestamp,
signature: "",
};

Expand Down Expand Up @@ -151,24 +155,37 @@ export class HashGraph {
operation: Operation,
deps: Hash[],
peerId: string,
timestamp: number,
signature: string,
): Hash {
const hash = computeHash(peerId, operation, deps);
const hash = computeHash(peerId, operation, deps, timestamp);
if (this.vertices.has(hash)) {
return hash; // Vertex already exists
}

if (
!deps.every((dep) => this.forwardEdges.has(dep) || this.vertices.has(dep))
) {
throw new Error("Invalid dependency detected.");
for (const dep of deps) {
const vertex = this.vertices.get(dep);
if (vertex === undefined) {
throw new Error("Invalid dependency detected.");
}
if (vertex.timestamp > timestamp) {
// Vertex's timestamp must not be less than any of its dependencies' timestamps
throw new Error("Invalid timestamp detected.");
}
}

const currentTimestamp = Date.now();
if (timestamp > currentTimestamp) {
// Vertex created in the future is invalid
throw new Error("Invalid timestamp detected.");
}

const vertex: Vertex = {
hash,
peerId,
operation,
dependencies: deps,
timestamp,
signature,
};
this.vertices.set(hash, vertex);
Expand Down Expand Up @@ -502,12 +519,13 @@ export class HashGraph {
}
}

function computeHash<T>(
function computeHash(
peerId: string,
operation: Operation,
deps: Hash[],
timestamp: number,
): Hash {
const serialized = JSON.stringify({ operation, deps, peerId });
const serialized = JSON.stringify({ operation, deps, peerId, timestamp });
const hash = crypto.createHash("sha256").update(serialized).digest("hex");
return hash;
}
2 changes: 2 additions & 0 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class DRPObject implements IDRPObject {
peerId: vertex.peerId,
operation: vertex.operation,
dependencies: vertex.dependencies,
timestamp: vertex.timestamp,
});
this.vertices.push(serializedVertex);
this._notify("callFn", [serializedVertex]);
Expand All @@ -155,6 +156,7 @@ export class DRPObject implements IDRPObject {
vertex.operation,
vertex.dependencies,
vertex.peerId,
vertex.timestamp,
vertex.signature,
);

Expand Down
3 changes: 2 additions & 1 deletion packages/object/src/proto/drp/object/v1/object.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ message Vertex {
string peer_id = 2;
Operation operation = 3;
repeated string dependencies = 4;
string signature = 5;
int64 timestamp = 5;
string signature = 6;
}

message DRPObjectBase {
Expand Down
34 changes: 31 additions & 3 deletions packages/object/src/proto/drp/object/v1/object_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b2a0f38

Please sign in to comment.