diff --git a/packages/sequencer/src/protocol/production/TransactionTraceService.ts b/packages/sequencer/src/protocol/production/TransactionTraceService.ts index 1a717fa3..06121170 100644 --- a/packages/sequencer/src/protocol/production/TransactionTraceService.ts +++ b/packages/sequencer/src/protocol/production/TransactionTraceService.ts @@ -16,7 +16,6 @@ import { LinkedMerkleTree } from "@proto-kit/common/dist/trees/LinkedMerkleTree" import { distinctByString } from "../../helpers/utils"; import { CachedStateService } from "../../state/state/CachedStateService"; -import { SyncCachedMerkleTreeStore } from "../../state/merkle/SyncCachedMerkleTreeStore"; import type { TransactionExecutionResult, BlockWithResult, @@ -24,6 +23,7 @@ import type { import { AsyncMerkleTreeStore } from "../../state/async/AsyncMerkleTreeStore"; import { VerificationKeyService } from "../runtime/RuntimeVerificationKeyService"; import { CachedLinkedMerkleTreeStore } from "../../state/merkle/CachedLinkedMerkleTreeStore"; +import { SyncCachedLinkedMerkleTreeStore } from "../../state/merkle/SyncCachedLinkedMerkleTreeStore"; import type { TransactionTrace, BlockTrace } from "./BatchProducerModule"; import { StateTransitionProofParameters } from "./tasks/StateTransitionTaskParameters"; @@ -266,7 +266,7 @@ export class TransactionTraceService { }> { const keys = this.allKeys(protocolTransitions.concat(stateTransitions)); - const runtimeSimulationMerkleStore = new SyncCachedMerkleTreeStore( + const runtimeSimulationMerkleStore = new SyncCachedLinkedMerkleTreeStore( merkleStore ); diff --git a/packages/sequencer/src/state/merkle/SyncCachedLinkedMerkleTreeStore.ts b/packages/sequencer/src/state/merkle/SyncCachedLinkedMerkleTreeStore.ts new file mode 100644 index 00000000..fb01e542 --- /dev/null +++ b/packages/sequencer/src/state/merkle/SyncCachedLinkedMerkleTreeStore.ts @@ -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 = {}; + } +} diff --git a/packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts b/packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts index a028006e..a2e122bc 100644 --- a/packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts +++ b/packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts @@ -1,10 +1,10 @@ import { - InMemoryLinkedMerkleTreeStorage, + InMemoryMerkleTreeStorage, MerkleTreeStore, RollupMerkleTree, } from "@proto-kit/common"; -export class SyncCachedMerkleTreeStore extends InMemoryLinkedMerkleTreeStorage { +export class SyncCachedMerkleTreeStore extends InMemoryMerkleTreeStorage { public constructor(private readonly parent: MerkleTreeStore) { super(); }