Skip to content

Commit

Permalink
cleaning, deleting commented segments and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
JanLewDev committed Aug 27, 2024
1 parent ad3012f commit 276ed98
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 90 deletions.
17 changes: 1 addition & 16 deletions packages/crdt/src/cros/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "@topology-foundation/object";

/// AddWinsSet with support for state and op changes
export class AddWinsSet<T extends number> {
export class AddWinsSet<T> {
state: Map<T, number>;
hashGraph: HashGraph<T>;

Expand Down Expand Up @@ -54,19 +54,4 @@ export class AddWinsSet<T extends number> {
this.state.set(value, Math.max(this.getValue(value), count));
}
}

// read(): T[] {
// const operations = this.hashGraph.linearizeOps();
// const tempCounter = new AddWinsSet<T>("");

// for (const op of operations) {
// if (op.type === OperationType.Add) {
// tempCounter.add(op.value);
// } else {
// tempCounter.remove(op.value);
// }
// }

// return tempCounter.values();
// }
}
6 changes: 0 additions & 6 deletions packages/crdt/src/cros/ReduceActionType/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class ReduceActionType<T> {
resolveConflicts(ops: ReductionType<T>[]): ResolvedConflict {
ops.sort((a, b) => (a.hash < b.hash ? -1 : 1));
const seed: string = ops.map((op) => op.hash).join("");
console.log("Hashing...", seed, compute_hash(seed));
const rnd = new Smush32(compute_hash(seed));
const chosen = rnd.int() % ops.length;
const indices = ops.map((op) => op.index);
Expand All @@ -59,7 +58,6 @@ export class ReduceActionType<T> {
const moving = order[j];

if (!this.hashGraph.areCausallyRelated(anchor, moving)) {
console.log("Start the magic", i, j);
const concurrentOps: ReductionType<T>[] = [];
concurrentOps.push({
hash: anchor,
Expand Down Expand Up @@ -89,9 +87,7 @@ export class ReduceActionType<T> {
});
}
}
console.log("Concurrent ops: ", concurrentOps);
const resolved = this.resolveConflicts(concurrentOps);
console.log("Resolved: ", resolved);

switch (resolved.action) {
case ActionType.Reduce:
Expand All @@ -100,8 +96,6 @@ export class ReduceActionType<T> {
for (const idx of resolved.indices) {
if (idx === i) shouldIncrementI = false;
order.splice(idx, 1);
console.log("Spliced at", idx);
console.log("After splicing: ", order);
}
if (!shouldIncrementI) j = order.length; // Break out of inner loop
break;
Expand Down
12 changes: 0 additions & 12 deletions packages/crdt/tests/ReduceActionType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,38 @@ describe("Reduce Action Type tests", () => {
const op1: Operation<number> = new Operation(OperationType.Add, 1);
const deps1: string[] = [vertexHash0];
const vertexHash1 = cro.hashGraph.addVertex(op1, deps1, peerId);
console.log("vertex1: ", vertexHash1);
// Add second vertex
const op2: Operation<number> = new Operation(OperationType.Add, 1);
const deps2: string[] = [vertexHash1];
const vertexHash2 = cro.hashGraph.addVertex(op2, deps2, peerId);
console.log("vertex2: ", vertexHash2);
// Add the third vertex V3 with dependency on V2
const op3: Operation<number> = new Operation(OperationType.Remove, 2);
const deps3: string[] = [vertexHash2];
const vertexHash3 = cro.hashGraph.addVertex(op3, deps3, peerId);
console.log("vertex3: ", vertexHash3);
// Add the vertex V4 -> [V1]
const op4: Operation<number> = new Operation(OperationType.Remove, 2);
const deps4: string[] = [vertexHash1];
const vertexHash4 = cro.hashGraph.addVertex(op4, deps4, peerId);
console.log("vertex4: ", vertexHash4);
// Add the vertex V5 -> [V4]
const op5: Operation<number> = new Operation(OperationType.Add, 2);
const deps5: string[] = [vertexHash4];
const vertexHash5 = cro.hashGraph.addVertex(op5, deps5, peerId);
console.log("vertex5: ", vertexHash5);
// Add the vertex V6 ->[V3]
const op6: Operation<number> = new Operation(OperationType.Add, 3);
const deps6: string[] = [vertexHash3];
const vertexHash6 = cro.hashGraph.addVertex(op6, deps6, peerId);
console.log("vertex6: ", vertexHash6);
// Add the vertex V7 -> [V3]
const op7: Operation<number> = new Operation(OperationType.Remove, 1);
const deps7: string[] = [vertexHash3];
const vertexHash7 = cro.hashGraph.addVertex(op7, deps7, peerId);
console.log("vertex7: ", vertexHash7);
// Add the vertex V8 -> [V7, V5]
const op8: Operation<number> = new Operation(OperationType.Remove, 3);
const deps8: string[] = [vertexHash7, vertexHash5];
const vertexHash8 = cro.hashGraph.addVertex(op8, deps8, peerId);
console.log("vertex8: ", vertexHash8);
// Add the vertex V9 -> [V5]
const op9: Operation<number> = new Operation(OperationType.Remove, 1);
const deps9: string[] = [vertexHash5];
const vertexHash9 = cro.hashGraph.addVertex(op9, deps9, peerId);
console.log("vertex9: ", vertexHash9);

const sortedOrder = cro.hashGraph.topologicalSort();
expect([
Expand All @@ -91,9 +82,7 @@ describe("Reduce Action Type tests", () => {
vertexHash6,
],
]).toContainEqual(sortedOrder);
console.log("Sorted", sortedOrder);
const linearOps = cro.linearizeOps();
console.log("Linear order", linearOps);
expect([[op1, op5, op8]]).toContainEqual(linearOps);
});

Expand Down Expand Up @@ -126,7 +115,6 @@ describe("Reduce Action Type tests", () => {
const vertexHash5 = cro.hashGraph.addVertex(op5, deps5, peerId);

const linearOps = cro.linearizeOps();
console.log("Linear order", linearOps);
expect(linearOps).toEqual([op1]);
});
});
57 changes: 1 addition & 56 deletions packages/object/src/hashgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as crypto from "node:crypto";

export type Hash = string;

class Vertex<T> {
export class Vertex<T> {
constructor(
readonly hash: Hash,
readonly operation: Operation<T>,
Expand Down Expand Up @@ -138,61 +138,6 @@ export class HashGraph<T> {
return result;
}

// linearizeOps(): Operation<T>[] {
// const order = this.topologicalSort();
// const result: Operation<T>[] = [];
// let i = 0;

// while (i < order.length) {
// const anchor = order[i];
// let j = i + 1;
// let shouldIncrementI = true;

// while (j < order.length) {
// const moving = order[j];

// if (!this.areCausallyRelated(anchor, moving)) {
// const op1 = this.vertices.get(anchor)?.operation;
// const op2 = this.vertices.get(moving)?.operation;
// let action: ActionType;
// if (!op1 || !op2) {
// action = ActionType.Nop;
// } else {
// action = this.resolveConflicts(op1, op2);
// }

// switch (action) {
// case ActionType.DropLeft:
// order.splice(i, 1);
// j = order.length; // Break out of inner loop
// shouldIncrementI = false;
// continue; // Continue outer loop without incrementing i
// case ActionType.DropRight:
// order.splice(j, 1);
// continue; // Continue with the same j
// case ActionType.Swap:
// [order[i], order[j]] = [order[j], order[i]];
// j = order.length; // Break out of inner loop
// break;
// case ActionType.Nop:
// j++;
// break;
// }
// } else {
// j++;
// }
// }

// if (shouldIncrementI) {
// const op = this.vertices.get(order[i])?.operation;
// if (op) result.push();
// i++;
// }
// }

// return result;
// }

// Time complexity: O(V), Space complexity: O(V)
areCausallyRelated(hash1: Hash, hash2: Hash): boolean {
const visited = new Set<Hash>();
Expand Down

0 comments on commit 276ed98

Please sign in to comment.