Skip to content

Commit

Permalink
Using callback
Browse files Browse the repository at this point in the history
  • Loading branch information
elielnfinic committed Nov 13, 2024
1 parent d7fbd3b commit 450eb0e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
37 changes: 1 addition & 36 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,22 @@ 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
if (!vertex.operation) {
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(
Expand Down

0 comments on commit 450eb0e

Please sign in to comment.