-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/sequencer/src/state/merkle/SyncCachedLinkedMerkleTreeStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
InMemoryLinkedMerkleTreeStorage, | ||
LinkedLeaf, | ||
LinkedMerkleTree, | ||
LinkedMerkleTreeStore, | ||
} from "@proto-kit/common"; | ||
|
||
export class SyncCachedLinkedMerkleTreeStore extends InMemoryLinkedMerkleTreeStorage { | ||
public constructor(private readonly parent: LinkedMerkleTreeStore) { | ||
super(); | ||
} | ||
|
||
public getNode(key: bigint, level: number): bigint | undefined { | ||
return super.getNode(key, level) ?? this.parent.getNode(key, level); | ||
} | ||
|
||
public setNode(key: bigint, level: number, value: bigint) { | ||
super.setNode(key, level, value); | ||
} | ||
|
||
public getLeaf(index: bigint): LinkedLeaf | undefined { | ||
return super.getLeaf(index) ?? this.parent.getLeaf(index); | ||
} | ||
|
||
public setLeaf(index: bigint, value: LinkedLeaf) { | ||
super.setLeaf(index, value); | ||
} | ||
|
||
public mergeIntoParent() { | ||
if (Object.keys(this.leaves).length === 0) { | ||
return; | ||
} | ||
|
||
const { nodes, leaves } = this; | ||
Object.entries(leaves).forEach(([key, leaf]) => | ||
this.setLeaf(BigInt(key), leaf) | ||
); | ||
Array.from({ length: LinkedMerkleTree.HEIGHT }).forEach((ignored, level) => | ||
Object.entries(nodes[level]).forEach((entry) => { | ||
this.parent.setNode(BigInt(entry[0]), level, entry[1]); | ||
}) | ||
); | ||
|
||
this.leaves = {}; | ||
this.nodes = {}; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters