Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing vertex dependencies issue #247

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function topologyMessagesHandler(

switch (message.type) {
case NetworkPb.Message_MessageType.UPDATE:
updateHandler(node, message.data);
updateHandler(node, message.data, message.sender);
break;
case NetworkPb.Message_MessageType.SYNC:
if (!stream) {
Expand Down Expand Up @@ -71,15 +71,19 @@ export async function topologyMessagesHandler(
data: { id: string, operations: {nonce: string, fn: string, args: string[] }[] }
operations array doesn't contain the full remote operations array
*/
function updateHandler(node: TopologyNode, data: Uint8Array) {
async function updateHandler(
node: TopologyNode,
data: Uint8Array,
sender: string,
) {
const updateMessage = NetworkPb.Update.decode(data);
d-roak marked this conversation as resolved.
Show resolved Hide resolved
const object = node.objectStore.get(updateMessage.objectId);
if (!object) {
console.error("topology::node::updateHandler", "Object not found");
return false;
}

object.merge(
const [merged, _] = object.merge(
updateMessage.vertices.map((v) => {
return {
hash: v.hash,
Expand All @@ -92,6 +96,11 @@ function updateHandler(node: TopologyNode, data: Uint8Array) {
};
}),
);

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

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

return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = "0.3.0";
export const VERSION = "0.3.1";
8 changes: 5 additions & 3 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,20 @@ 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)) {
return hash; // Vertex already exists
}

// 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.error("Invalid dependency detected.");
return "";
throw new Error("Invalid dependency detected.");
elielnfinic marked this conversation as resolved.
Show resolved Hide resolved
}

const vertex: Vertex = {
Expand Down
23 changes: 17 additions & 6 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,36 @@ 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
44 changes: 22 additions & 22 deletions pnpm-lock.yaml

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