diff --git a/packages/node/src/handlers.ts b/packages/node/src/handlers.ts index d93508e8..a89526ee 100644 --- a/packages/node/src/handlers.ts +++ b/packages/node/src/handlers.ts @@ -92,44 +92,9 @@ function updateHandler(node: TopologyNode, data: Uint8Array) { }), async (croId: string, nodeId: string) => { await node.syncObject(croId, nodeId); - } + }, ); - // const _merge = () => { - // object.merge( - // updateMessage.vertices.map((v) => { - // return { - // hash: v.hash, - // nodeId: v.nodeId, - // operation: { - // type: v.operation?.type ?? "", - // value: v.operation?.value, - // }, - // dependencies: v.dependencies, - // }; - // }), - // ); - // }; - - // try { - // _merge(); - // } catch (err) { - // if ((err as Error).name === "InvalidDependencyError") { - // if (updateMessage.vertices.length === 0) return false; - // const peerId = updateMessage.vertices[0].nodeId; - // node.syncObject(object.id, peerId).then(() => { - // _merge(); - // }); - // } else { - // console.error( - // "topology::node::updateHandler", - // "Error merging vertices", - // err, - // ); - // return false; - // } - // } - node.objectStore.put(object.id, object); return true; diff --git a/packages/object/src/hashgraph/index.ts b/packages/object/src/hashgraph/index.ts index dcf69ea9..afc18718 100644 --- a/packages/object/src/hashgraph/index.ts +++ b/packages/object/src/hashgraph/index.ts @@ -118,7 +118,11 @@ export class HashGraph { return vertex; } - checkVertexDependency(operation: Operation, deps: Hash[], nodeId: string): boolean { + checkVertexDependency( + operation: Operation, + deps: Hash[], + nodeId: string, + ): boolean { const hash = computeHash(nodeId, operation, deps); if (this.vertices.has(hash)) { return true; @@ -128,15 +132,13 @@ export class HashGraph { if ( !deps.every((dep) => this.forwardEdges.has(dep) || this.vertices.has(dep)) ) { - console.log("THE DEPENDENCIES ARE NOT OKAY"); return false; } return true; - } - addVertex(operation: Operation, deps: Hash[], nodeId: string): Hash | boolean { + addVertex(operation: Operation, deps: Hash[], nodeId: string): Hash { const hash = computeHash(nodeId, operation, deps); if (this.vertices.has(hash)) { return hash; // Vertex already exists diff --git a/packages/object/src/index.ts b/packages/object/src/index.ts index dcd6f045..2ae6f877 100644 --- a/packages/object/src/index.ts +++ b/packages/object/src/index.ts @@ -99,7 +99,7 @@ export class TopologyObject implements ITopologyObject { async merge( vertices: Vertex[], - synchronizeVertices: (croId: string, nodeId: string) => void, + synchronizeVertices?: (croId: string, nodeId: string) => void, ) { for (const vertex of vertices) { // Check to avoid manually crafted `undefined` operations @@ -107,13 +107,14 @@ export class TopologyObject implements ITopologyObject { continue; } if ( - this.hashGraph.checkVertexDependency( + !this.hashGraph.checkVertexDependency( vertex.operation, vertex.dependencies, vertex.nodeId, ) ) { - await synchronizeVertices(this.id, vertex.nodeId); + if (synchronizeVertices) + await synchronizeVertices(this.id, vertex.nodeId); } this.hashGraph.addVertex(