Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0cc3cc

Browse files
authored
fix: remove unreachable modified detection in MerkleDAG.compare() (#294)
Remove unreachable modified detection in MerkleDAG.compare() — node IDs are SHA-256 of data, so same ID guarantees same data. Simplify return type to { added, removed }.
1 parent 1ebda84 commit c0cc3cc

2 files changed

Lines changed: 9 additions & 18 deletions

File tree

packages/core/src/sync/merkle.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,13 @@ export class MerkleDAG {
7878
return dag;
7979
}
8080

81-
public static compare(dag1: MerkleDAG, dag2: MerkleDAG): { added: string[], removed: string[], modified: string[] } {
82-
const nodes1 = new Map(Array.from(dag1.getAllNodes()).map(n => [n.id, n]));
83-
const nodes2 = new Map(Array.from(dag2.getAllNodes()).map(n => [n.id, n]));
84-
85-
const added = Array.from(nodes2.keys()).filter(k => !nodes1.has(k));
86-
const removed = Array.from(nodes1.keys()).filter(k => !nodes2.has(k));
87-
88-
// For modified, we'll check if the data has changed for nodes that exist in both
89-
const modified: string[] = [];
90-
for (const [id, node1] of Array.from(nodes1.entries())) {
91-
const node2 = nodes2.get(id);
92-
if (node2 && node1.data !== node2.data) {
93-
modified.push(id);
94-
}
95-
}
81+
public static compare(dag1: MerkleDAG, dag2: MerkleDAG): { added: string[], removed: string[] } {
82+
const nodes1 = new Set(Array.from(dag1.getAllNodes()).map(n => n.id));
83+
const nodes2 = new Set(Array.from(dag2.getAllNodes()).map(n => n.id));
84+
85+
const added = Array.from(nodes2).filter(k => !nodes1.has(k));
86+
const removed = Array.from(nodes1).filter(k => !nodes2.has(k));
9687

97-
return { added, removed, modified };
88+
return { added, removed };
9889
}
9990
}

packages/core/src/sync/synchronizer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ export class FileSynchronizer {
236236
// Compare the DAGs
237237
const changes = MerkleDAG.compare(this.merkleDAG, newMerkleDAG);
238238

239-
// If there are any changes in the DAG, we should also do a file-level comparison
240-
if (changes.added.length > 0 || changes.removed.length > 0 || changes.modified.length > 0) {
239+
// If there are any changes in the DAG, do a file-level comparison
240+
if (changes.added.length > 0 || changes.removed.length > 0) {
241241
console.log('[Synchronizer] Merkle DAG has changed. Comparing file states...');
242242
const fileChanges = this.compareStates(this.fileHashes, newFileHashes);
243243

0 commit comments

Comments
 (0)