Skip to content

Commit

Permalink
add throw on add vertex, change return type to detect if it was merge…
Browse files Browse the repository at this point in the history
…d/missing, do sync on missing deps
  • Loading branch information
d-roak committed Nov 14, 2024
1 parent 0866e32 commit 6424c71
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
11 changes: 6 additions & 5 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function topologyMessagesHandler(
async function updateHandler(
node: TopologyNode,
data: Uint8Array,
senderPeerId: string,
sender: string,
) {
const updateMessage = NetworkPb.Update.decode(data);
const object = node.objectStore.get(updateMessage.objectId);
Expand All @@ -83,9 +83,7 @@ async function updateHandler(
return false;
}

await node.syncObject(updateMessage.objectId, senderPeerId);

object.merge(
const [merged, _] = object.merge(
updateMessage.vertices.map((v) => {
return {
hash: v.hash,
Expand All @@ -99,6 +97,10 @@ async function updateHandler(
}),
);

if (!merged) {
await node.syncObject(updateMessage.objectId, sender);
}

node.objectStore.put(object.id, object);

return true;
Expand Down Expand Up @@ -180,7 +182,6 @@ async function syncAcceptHandler(
});

if (vertices.length !== 0) {
await node.syncObject(object.id, sender);
object.merge(vertices);
node.objectStore.put(object.id, object);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export class HashGraph {
return vertex;
}

/* Add a vertex to the hashgraph with the given operation and dependencies.
* If the vertex already exists, return the hash of the existing vertex.
* Throws an error if any of the dependencies are not present in the hashgraph.
*/
addVertex(operation: Operation, deps: Hash[], nodeId: string): Hash {
const hash = computeHash(nodeId, operation, deps);
if (this.vertices.has(hash)) {
Expand All @@ -128,8 +132,7 @@ export class HashGraph {
if (
!deps.every((dep) => this.forwardEdges.has(dep) || this.vertices.has(dep))
) {
console.error("Invalid dependency detected.");
return "";
throw new Error("Invalid dependency detected.");
}

const vertex: Vertex = {
Expand Down
24 changes: 18 additions & 6 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,37 @@ export class TopologyObject implements ITopologyObject {
this._notify("callFn", [serializedVertex]);
}

merge(vertices: Vertex[]) {
/*
* Merges the vertices into the hashgraph
* Returns a tuple with a boolean indicating if there were
* missing vertices and an array with the missing vertices
*/
merge(vertices: Vertex[]): [merged: boolean, missing: string[]] {
const missing = [];
for (const vertex of vertices) {
// Check to avoid manually crafted `undefined` operations
if (!vertex.operation) {
continue;
}

this.hashGraph.addVertex(
vertex.operation,
vertex.dependencies,
vertex.nodeId,
);
try {
this.hashGraph.addVertex(
vertex.operation,
vertex.dependencies,
vertex.nodeId,
);
} catch (e) {
missing.push(vertex.hash);
}
}

const operations = this.hashGraph.linearizeOperations();
this.vertices = this.hashGraph.getAllVertices();

(this.cro as CRO).mergeCallback(operations);
this._notify("merge", this.vertices);

return [missing.length === 0, missing];
}

subscribe(callback: TopologyObjectCallback) {
Expand Down

0 comments on commit 6424c71

Please sign in to comment.