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 a66d8bd commit d7fbd3b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 49 deletions.
100 changes: 53 additions & 47 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,57 @@ function updateHandler(node: TopologyNode, data: Uint8Array) {
console.error("topology::node::updateHandler", "Object not found");
return false;
}
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);
_merge();
} else {
console.error(
"topology::node::updateHandler",
"Error merging vertices",
err,
);
return false;
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,
};
}),
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);

Expand Down Expand Up @@ -193,21 +211,9 @@ function syncAcceptHandler(
});

if (vertices.length !== 0) {
try {
object.merge(vertices);
} catch (err) {
if ((err as Error).name === "InvalidDependencyError") {
node.syncObject(object.id, sender);
object.merge(vertices);
} else {
console.error(
"topology::node::syncAcceptHandler",
"Error merging vertices",
err,
);
return;
}
}
object.merge(vertices, async (croId: string, nodeId: string) => {
await node.syncObject(croId, nodeId);
});
node.objectStore.put(object.id, object);
}

Expand Down
20 changes: 19 additions & 1 deletion packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,25 @@ export class HashGraph {
return vertex;
}

addVertex(operation: Operation, deps: Hash[], nodeId: string): Hash {
checkVertexDependency(operation: Operation, deps: Hash[], nodeId: string): boolean {
const hash = computeHash(nodeId, operation, deps);
if (this.vertices.has(hash)) {
return true;
}

// Temporary fix: don't add the vertex if the dependencies are not present in the local HG.
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 {
const hash = computeHash(nodeId, operation, deps);
if (this.vertices.has(hash)) {
return hash; // Vertex already exists
Expand Down
14 changes: 13 additions & 1 deletion packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,24 @@ export class TopologyObject implements ITopologyObject {
this._notify("callFn", [serializedVertex]);
}

merge(vertices: Vertex[]) {
async merge(
vertices: Vertex[],
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(
vertex.operation,
vertex.dependencies,
vertex.nodeId,
)
) {
await synchronizeVertices(this.id, vertex.nodeId);

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:26:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:107:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:111:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:112:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:135:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:139:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:140:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:165:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:171:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 116 in packages/object/src/index.ts

View workflow job for this annotation

GitHub Actions / tests

Unhandled error

TypeError: synchronizeVertices is not a function ❯ TopologyObject.merge packages/object/src/index.ts:116:11 ❯ packages/object/tests/hashgraph.test.ts:172:8 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:146:14 ❯ node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:533:11 ❯ runWithTimeout node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:39:7 ❯ runTest node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1048:17 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runSuite node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1204:15 ❯ runFiles node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/index.js:1261:5 This error originated in "packages/object/tests/hashgraph.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "packages/object/tests/hashgraph.test.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
}

this.hashGraph.addVertex(
vertex.operation,
Expand Down

0 comments on commit d7fbd3b

Please sign in to comment.