From c5f93a5b6e8f7533d591a847d27e7ab4f9606fe5 Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Wed, 25 Oct 2023 22:30:31 +0200 Subject: [PATCH 1/4] Seperate Async from Sync Cached services Moved StateServices to new package --- .../merkletree/InMemoryMerkleTreeStorage.ts | 2 +- .../src/utils/merkletree/MerkleTreeStore.ts | 10 ----- packages/sequencer/src/index.ts | 16 ++++--- .../production/BlockProducerModule.ts | 10 ++--- .../production/TransactionTraceService.ts | 12 +++-- .../production/tasks/BlockProvingTask.ts | 2 +- .../production/tasks/RuntimeProvingTask.ts | 2 +- .../production/tasks/StateTransitionTask.ts | 2 +- .../MerkleStoreWitnessProvider.ts | 0 .../src/state/async/AsyncMerkleTreeStore.ts | 9 ++++ .../async}/AsyncStateService.ts | 5 +++ .../merkle}/CachedMerkleTreeStore.ts | 42 +---------------- .../state/merkle/SyncCachedMerkleTreeStore.ts | 35 +++++++++++++++ .../prefilled}/PreFilledStateService.ts | 0 .../prefilled}/PreFilledWitnessProvider.ts | 0 .../state}/CachedStateService.ts | 12 ++++- .../state}/DummyStateService.ts | 0 .../src/state/state/SyncCachedStateService.ts | 45 +++++++++++++++++++ .../storage/MockStorageDependencyFactory.ts | 6 +-- .../src/storage/StorageDependencyFactory.ts | 5 +-- 20 files changed, 135 insertions(+), 80 deletions(-) rename packages/sequencer/src/{protocol/production/execution => state}/MerkleStoreWitnessProvider.ts (100%) create mode 100644 packages/sequencer/src/state/async/AsyncMerkleTreeStore.ts rename packages/sequencer/src/{protocol/production/state => state/async}/AsyncStateService.ts (88%) rename packages/sequencer/src/{protocol/production/execution => state/merkle}/CachedMerkleTreeStore.ts (76%) create mode 100644 packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts rename packages/sequencer/src/{protocol/production/tasks/providers => state/prefilled}/PreFilledStateService.ts (100%) rename packages/sequencer/src/{protocol/production/tasks/providers => state/prefilled}/PreFilledWitnessProvider.ts (100%) rename packages/sequencer/src/{protocol/production/execution => state/state}/CachedStateService.ts (91%) rename packages/sequencer/src/{protocol/production/execution => state/state}/DummyStateService.ts (100%) create mode 100644 packages/sequencer/src/state/state/SyncCachedStateService.ts diff --git a/packages/protocol/src/utils/merkletree/InMemoryMerkleTreeStorage.ts b/packages/protocol/src/utils/merkletree/InMemoryMerkleTreeStorage.ts index 1fa83ad7..390b87fc 100644 --- a/packages/protocol/src/utils/merkletree/InMemoryMerkleTreeStorage.ts +++ b/packages/protocol/src/utils/merkletree/InMemoryMerkleTreeStorage.ts @@ -1,7 +1,7 @@ import { MerkleTreeStore } from "./MerkleTreeStore"; export class InMemoryMerkleTreeStorage implements MerkleTreeStore { - protected readonly nodes: { + protected nodes: { [key: number]: { [key: string]: bigint; }; diff --git a/packages/protocol/src/utils/merkletree/MerkleTreeStore.ts b/packages/protocol/src/utils/merkletree/MerkleTreeStore.ts index a03c4923..595c9065 100644 --- a/packages/protocol/src/utils/merkletree/MerkleTreeStore.ts +++ b/packages/protocol/src/utils/merkletree/MerkleTreeStore.ts @@ -1,13 +1,3 @@ -export interface AsyncMerkleTreeStore { - openTransaction: () => void; - - commit: () => void; - - setNodeAsync: (key: bigint, level: number, value: bigint) => Promise; - - getNodeAsync: (key: bigint, level: number) => Promise; -} - export interface MerkleTreeStore { setNode: (key: bigint, level: number, value: bigint) => void; diff --git a/packages/sequencer/src/index.ts b/packages/sequencer/src/index.ts index f15d8f1a..b63dd784 100644 --- a/packages/sequencer/src/index.ts +++ b/packages/sequencer/src/index.ts @@ -19,12 +19,6 @@ export * from "./worker/worker/FlowTaskWorker"; export * from "./worker/worker/LocalTaskWorkerModule"; export * from "./protocol/baselayer/BaseLayer"; export * from "./protocol/baselayer/NoopBaseLayer"; -export * from "./protocol/production/execution/CachedStateService"; -export * from "./protocol/production/execution/DummyStateService"; -export * from "./protocol/production/execution/MerkleStoreWitnessProvider"; -export * from "./protocol/production/state/AsyncStateService"; -export * from "./protocol/production/tasks/providers/PreFilledStateService"; -export * from "./protocol/production/tasks/providers/PreFilledWitnessProvider"; export * from "./protocol/production/tasks/BlockProvingTask"; export * from "./protocol/production/tasks/CompileRegistry"; export * from "./protocol/production/tasks/RuntimeProvingTask"; @@ -45,3 +39,13 @@ export * from "./storage/StorageDependencyFactory"; export * from "./helpers/query/QueryTransportModule"; export * from "./helpers/query/QueryBuilderFactory"; export * from "./helpers/query/NetworkStateQuery"; +export * from "./state/prefilled/PreFilledStateService"; +export * from "./state/prefilled/PreFilledWitnessProvider"; +export * from "./state/async/AsyncMerkleTreeStore"; +export * from "./state/async/AsyncStateService"; +export * from "./state/merkle/CachedMerkleTreeStore"; +export * from "./state/merkle/SyncCachedMerkleTreeStore"; +export * from "./state/state/DummyStateService"; +export * from "./state/state/SyncCachedStateService"; +export * from "./state/state/CachedStateService"; +export * from "./state/MerkleStoreWitnessProvider"; diff --git a/packages/sequencer/src/protocol/production/BlockProducerModule.ts b/packages/sequencer/src/protocol/production/BlockProducerModule.ts index fbf95469..7ed12f49 100644 --- a/packages/sequencer/src/protocol/production/BlockProducerModule.ts +++ b/packages/sequencer/src/protocol/production/BlockProducerModule.ts @@ -1,6 +1,5 @@ import { inject } from "tsyringe"; import { - AsyncMerkleTreeStore, BlockProverPublicInput, BlockProverPublicOutput, DefaultProvableHashList, @@ -22,15 +21,16 @@ import { ComputedBlock, ComputedBlockTransaction, } from "../../storage/model/Block"; +import { CachedStateService } from "../../state/state/CachedStateService"; +import { CachedMerkleTreeStore } from "../../state/merkle/CachedMerkleTreeStore"; +import { AsyncStateService } from "../../state/async/AsyncStateService"; +import { AsyncMerkleTreeStore } from "../../state/async/AsyncMerkleTreeStore"; -import { AsyncStateService } from "./state/AsyncStateService"; -import { CachedStateService } from "./execution/CachedStateService"; +import { BlockProverParameters } from "./tasks/BlockProvingTask"; import { StateTransitionProofParameters } from "./tasks/StateTransitionTaskParameters"; import { RuntimeProofParameters } from "./tasks/RuntimeTaskParameters"; import { TransactionTraceService } from "./TransactionTraceService"; import { BlockTaskFlowService } from "./BlockTaskFlowService"; -import { BlockProverParameters } from "./tasks/BlockProvingTask"; -import { CachedMerkleTreeStore } from "./execution/CachedMerkleTreeStore"; export interface StateRecord { [key: string]: Field[] | undefined; diff --git a/packages/sequencer/src/protocol/production/TransactionTraceService.ts b/packages/sequencer/src/protocol/production/TransactionTraceService.ts index 276642dc..09cb4316 100644 --- a/packages/sequencer/src/protocol/production/TransactionTraceService.ts +++ b/packages/sequencer/src/protocol/production/TransactionTraceService.ts @@ -33,15 +33,13 @@ import { PendingTransaction } from "../../mempool/PendingTransaction"; import { distinctByString } from "../../helpers/utils"; import { ComputedBlockTransaction } from "../../storage/model/Block"; -import { CachedStateService } from "./execution/CachedStateService"; import type { StateRecord, TransactionTrace } from "./BlockProducerModule"; -import { DummyStateService } from "./execution/DummyStateService"; import { StateTransitionProofParameters } from "./tasks/StateTransitionTaskParameters"; -import { AsyncStateService } from "./state/AsyncStateService"; -import { - CachedMerkleTreeStore, - SyncCachedMerkleTreeStore, -} from "./execution/CachedMerkleTreeStore"; +import { DummyStateService } from "../../state/state/DummyStateService"; +import { CachedMerkleTreeStore } from "../../state/merkle/CachedMerkleTreeStore"; +import { CachedStateService } from "../../state/state/CachedStateService"; +import { SyncCachedMerkleTreeStore } from "../../state/merkle/SyncCachedMerkleTreeStore"; +import { AsyncStateService } from "../../state/async/AsyncStateService"; const errors = { methodIdNotFound: (methodId: string) => diff --git a/packages/sequencer/src/protocol/production/tasks/BlockProvingTask.ts b/packages/sequencer/src/protocol/production/tasks/BlockProvingTask.ts index f32c29a6..94c4dcfc 100644 --- a/packages/sequencer/src/protocol/production/tasks/BlockProvingTask.ts +++ b/packages/sequencer/src/protocol/production/tasks/BlockProvingTask.ts @@ -27,7 +27,7 @@ import { Task } from "../../../worker/flow/Task"; import { CompileRegistry } from "./CompileRegistry"; import { DecodedState, JSONEncodableState } from "./RuntimeTaskParameters"; -import { PreFilledStateService } from "./providers/PreFilledStateService"; +import { PreFilledStateService } from "../../../state/prefilled/PreFilledStateService"; type RuntimeProof = Proof; type BlockProof = Proof; diff --git a/packages/sequencer/src/protocol/production/tasks/RuntimeProvingTask.ts b/packages/sequencer/src/protocol/production/tasks/RuntimeProvingTask.ts index 6ae989a0..1b14ff38 100644 --- a/packages/sequencer/src/protocol/production/tasks/RuntimeProvingTask.ts +++ b/packages/sequencer/src/protocol/production/tasks/RuntimeProvingTask.ts @@ -19,7 +19,7 @@ import { RuntimeProofParameters, RuntimeProofParametersSerializer, } from "./RuntimeTaskParameters"; -import { PreFilledStateService } from "./providers/PreFilledStateService"; +import { PreFilledStateService } from "../../../state/prefilled/PreFilledStateService"; type RuntimeProof = Proof; diff --git a/packages/sequencer/src/protocol/production/tasks/StateTransitionTask.ts b/packages/sequencer/src/protocol/production/tasks/StateTransitionTask.ts index d74801c2..42ee610a 100644 --- a/packages/sequencer/src/protocol/production/tasks/StateTransitionTask.ts +++ b/packages/sequencer/src/protocol/production/tasks/StateTransitionTask.ts @@ -25,7 +25,7 @@ import { StateTransitionParametersSerializer, StateTransitionProofParameters, } from "./StateTransitionTaskParameters"; -import { PreFilledWitnessProvider } from "./providers/PreFilledWitnessProvider"; +import { PreFilledWitnessProvider } from "../../../state/prefilled/PreFilledWitnessProvider"; import { CompileRegistry } from "./CompileRegistry"; @injectable() diff --git a/packages/sequencer/src/protocol/production/execution/MerkleStoreWitnessProvider.ts b/packages/sequencer/src/state/MerkleStoreWitnessProvider.ts similarity index 100% rename from packages/sequencer/src/protocol/production/execution/MerkleStoreWitnessProvider.ts rename to packages/sequencer/src/state/MerkleStoreWitnessProvider.ts diff --git a/packages/sequencer/src/state/async/AsyncMerkleTreeStore.ts b/packages/sequencer/src/state/async/AsyncMerkleTreeStore.ts new file mode 100644 index 00000000..30e0b8cd --- /dev/null +++ b/packages/sequencer/src/state/async/AsyncMerkleTreeStore.ts @@ -0,0 +1,9 @@ +export interface AsyncMerkleTreeStore { + openTransaction: () => void; + + commit: () => void; + + setNodeAsync: (key: bigint, level: number, value: bigint) => Promise; + + getNodeAsync: (key: bigint, level: number) => Promise; +} \ No newline at end of file diff --git a/packages/sequencer/src/protocol/production/state/AsyncStateService.ts b/packages/sequencer/src/state/async/AsyncStateService.ts similarity index 88% rename from packages/sequencer/src/protocol/production/state/AsyncStateService.ts rename to packages/sequencer/src/state/async/AsyncStateService.ts index 9b00eefe..fe12fe84 100644 --- a/packages/sequencer/src/protocol/production/state/AsyncStateService.ts +++ b/packages/sequencer/src/state/async/AsyncStateService.ts @@ -6,6 +6,11 @@ import { Field } from "o1js"; * CachedStateService to preload keys for In-Circuit usage. */ export interface AsyncStateService { + openTransaction: () => void; + + commit: () => void; + setAsync: (key: Field, value: Field[] | undefined) => Promise; + getAsync: (key: Field) => Promise; } diff --git a/packages/sequencer/src/protocol/production/execution/CachedMerkleTreeStore.ts b/packages/sequencer/src/state/merkle/CachedMerkleTreeStore.ts similarity index 76% rename from packages/sequencer/src/protocol/production/execution/CachedMerkleTreeStore.ts rename to packages/sequencer/src/state/merkle/CachedMerkleTreeStore.ts index 72102b62..dcdabcbe 100644 --- a/packages/sequencer/src/protocol/production/execution/CachedMerkleTreeStore.ts +++ b/packages/sequencer/src/state/merkle/CachedMerkleTreeStore.ts @@ -1,9 +1,9 @@ import { - AsyncMerkleTreeStore, - InMemoryMerkleTreeStorage, MerkleTreeStore, + InMemoryMerkleTreeStorage, RollupMerkleTree } from "@proto-kit/protocol"; import { log, noop } from "@proto-kit/common"; +import { AsyncMerkleTreeStore } from "../async/AsyncMerkleTreeStore"; export class CachedMerkleTreeStore extends InMemoryMerkleTreeStorage @@ -123,42 +123,4 @@ export class CachedMerkleTreeStore this.getNode(key, level) ?? (await this.parent.getNodeAsync(key, level)) ); } -} - -export class SyncCachedMerkleTreeStore extends InMemoryMerkleTreeStorage { - private writeCache: { - [key: number]: { - [key: string]: bigint; - }; - } = {}; - - public constructor(private readonly parent: MerkleTreeStore) { - 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); - (this.writeCache[level] ??= {})[key.toString()] = value; - } - - public mergeIntoParent() { - if (Object.keys(this.writeCache).length === 0) { - return; - } - - const { height } = RollupMerkleTree; - const nodes = this.writeCache - - Array.from({ length: height }).forEach((ignored, level) => - Object.entries(nodes[level]).forEach((entry) => { - this.parent.setNode(BigInt(entry[0]), level, entry[1]); - }) - ); - - this.writeCache = {} - } } \ No newline at end of file diff --git a/packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts b/packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts new file mode 100644 index 00000000..db17bd08 --- /dev/null +++ b/packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts @@ -0,0 +1,35 @@ +import { + InMemoryMerkleTreeStorage, MerkleTreeStore, + RollupMerkleTree +} from "@proto-kit/protocol"; + +export class SyncCachedMerkleTreeStore extends InMemoryMerkleTreeStorage { + public constructor(private readonly parent: MerkleTreeStore) { + 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 mergeIntoParent() { + if (Object.keys(this.nodes).length === 0) { + return; + } + + const { height } = RollupMerkleTree; + const { nodes } = this; + + Array.from({ length: height }).forEach((ignored, level) => + Object.entries(nodes[level]).forEach((entry) => { + this.parent.setNode(BigInt(entry[0]), level, entry[1]); + }) + ); + + this.nodes = {} + } +} \ No newline at end of file diff --git a/packages/sequencer/src/protocol/production/tasks/providers/PreFilledStateService.ts b/packages/sequencer/src/state/prefilled/PreFilledStateService.ts similarity index 100% rename from packages/sequencer/src/protocol/production/tasks/providers/PreFilledStateService.ts rename to packages/sequencer/src/state/prefilled/PreFilledStateService.ts diff --git a/packages/sequencer/src/protocol/production/tasks/providers/PreFilledWitnessProvider.ts b/packages/sequencer/src/state/prefilled/PreFilledWitnessProvider.ts similarity index 100% rename from packages/sequencer/src/protocol/production/tasks/providers/PreFilledWitnessProvider.ts rename to packages/sequencer/src/state/prefilled/PreFilledWitnessProvider.ts diff --git a/packages/sequencer/src/protocol/production/execution/CachedStateService.ts b/packages/sequencer/src/state/state/CachedStateService.ts similarity index 91% rename from packages/sequencer/src/protocol/production/execution/CachedStateService.ts rename to packages/sequencer/src/state/state/CachedStateService.ts index 24d558aa..84d9097d 100644 --- a/packages/sequencer/src/protocol/production/execution/CachedStateService.ts +++ b/packages/sequencer/src/state/state/CachedStateService.ts @@ -1,8 +1,8 @@ import { Field } from "o1js"; -import { log } from "@proto-kit/common"; +import { log, noop } from "@proto-kit/common"; import { InMemoryStateService } from "@proto-kit/module"; -import { AsyncStateService } from "../state/AsyncStateService"; +import { AsyncStateService } from "../async/AsyncStateService"; const errors = { parentIsUndefined: () => new Error("Parent StateService is undefined"), @@ -28,6 +28,14 @@ export class CachedStateService } } + public commit(): void { + noop(); + } + + public openTransaction(): void { + noop(); + } + public async preloadKey(key: Field) { // Only preload it if it hasn't been preloaded previously if (this.parent !== undefined && this.get(key) === undefined) { diff --git a/packages/sequencer/src/protocol/production/execution/DummyStateService.ts b/packages/sequencer/src/state/state/DummyStateService.ts similarity index 100% rename from packages/sequencer/src/protocol/production/execution/DummyStateService.ts rename to packages/sequencer/src/state/state/DummyStateService.ts diff --git a/packages/sequencer/src/state/state/SyncCachedStateService.ts b/packages/sequencer/src/state/state/SyncCachedStateService.ts new file mode 100644 index 00000000..f3ec443a --- /dev/null +++ b/packages/sequencer/src/state/state/SyncCachedStateService.ts @@ -0,0 +1,45 @@ +import { InMemoryStateService } from "@proto-kit/module"; +import { Field } from "o1js"; +import { StateService } from "@proto-kit/protocol"; + +const errors = { + parentIsUndefined: () => new Error("Parent StateService is undefined"), +}; + +export class SyncCachedStateService + extends InMemoryStateService + implements StateService +{ + public constructor(private readonly parent: StateService | undefined) { + super(); + } + + public get(key: Field): Field[] | undefined { + return super.get(key) ?? this.parent?.get(key); + } + + private assertParentNotNull( + parent: StateService | undefined + ): asserts parent is StateService { + if (parent === undefined) { + throw errors.parentIsUndefined(); + } + } + + /** + * Merges all caches set() operation into the parent and + * resets this instance to the parent's state (by clearing the cache and + * defaulting to the parent) + */ + public async mergeIntoParent() { + const { parent, values } = this; + this.assertParentNotNull(parent); + + // Set all cached values on parent + Object.entries(values).map((value) => { + parent.set(Field(value[0]), value[1]); + }); + // Clear cache + this.values = {}; + } +} diff --git a/packages/sequencer/src/storage/MockStorageDependencyFactory.ts b/packages/sequencer/src/storage/MockStorageDependencyFactory.ts index 82118bfd..c6011bbe 100644 --- a/packages/sequencer/src/storage/MockStorageDependencyFactory.ts +++ b/packages/sequencer/src/storage/MockStorageDependencyFactory.ts @@ -1,5 +1,4 @@ import { - AsyncMerkleTreeStore, InMemoryMerkleTreeStorage, StateServiceProvider, StateTransitionWitnessProviderReference, @@ -11,8 +10,9 @@ import { noop, } from "@proto-kit/common"; -import { AsyncStateService } from "../protocol/production/state/AsyncStateService"; -import { CachedStateService } from "../protocol/production/execution/CachedStateService"; +import { AsyncMerkleTreeStore } from "../state/async/AsyncMerkleTreeStore"; +import { CachedStateService } from "../state/state/CachedStateService"; +import { AsyncStateService } from "../state/async/AsyncStateService"; import { StorageDependencyFactory } from "./StorageDependencyFactory"; import { diff --git a/packages/sequencer/src/storage/StorageDependencyFactory.ts b/packages/sequencer/src/storage/StorageDependencyFactory.ts index 15c5d205..9a58c3eb 100644 --- a/packages/sequencer/src/storage/StorageDependencyFactory.ts +++ b/packages/sequencer/src/storage/StorageDependencyFactory.ts @@ -1,8 +1,8 @@ import { inject } from "tsyringe"; -import { AsyncMerkleTreeStore } from "@proto-kit/protocol"; import { DependencyFactory, dependencyFactory } from "@proto-kit/common"; -import { AsyncStateService } from "../protocol/production/state/AsyncStateService"; +import { AsyncStateService } from "../state/async/AsyncStateService"; +import { AsyncMerkleTreeStore } from "../state/async/AsyncMerkleTreeStore"; import { Database } from "./Database"; import { BlockStorage } from "./repositories/BlockStorage"; @@ -14,7 +14,6 @@ export interface StorageDependencyFactory { } @dependencyFactory() -// eslint-disable-next-line import/no-unused-modules export class DatabaseStorageDependencyFactory extends DependencyFactory { public constructor(@inject("Database") private readonly database: Database) { super(); From 296b0733b12df7666c900758ca7e3e414d9e0bd0 Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Wed, 25 Oct 2023 22:30:41 +0200 Subject: [PATCH 2/4] Added docs for state services --- packages/sequencer/src/state/StateServices.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/sequencer/src/state/StateServices.md diff --git a/packages/sequencer/src/state/StateServices.md b/packages/sequencer/src/state/StateServices.md new file mode 100644 index 00000000..9802dd64 --- /dev/null +++ b/packages/sequencer/src/state/StateServices.md @@ -0,0 +1,32 @@ +## State Service Architecture + +The state services are built using a abstraction stack, where every service that is below the root is based on the parent. +All of this services have the ability to base off one another to allow for diff-based changes. +You can think of it a bit like git, where every commit is only the diff between the parents state and the next state. + +*But*, in this architecture there is no such thing as branching, it is strictly linear and behaves almost like a traditional stack. +That means, for example, that you shouldn't change state in a service that has one or more children, as these changes might not get reflected instantly in the children's perception of state. + +Now I will go over all the different kinds of stateservices + +Btw all of this also applies to to merkle tree stores + +### AsyncStateService + +This is always the base for the whole stack and is meant to be implemented by the actual persistence layer. +That means primarily the DB (but is also implemented by the in-memory state service). +It's functions are async-based in order to enable external DB APIs + +### CachedStateService + +It receives a AsyncStateService as a parent and can build on top of it. +It "caches" in the sense that it can preload state entries from the parent (asynchronously) and then lets circuits among others perform synchronous operations on them. +It additionally caches write operations that can then later be merged back into the parent (or thrown away). + +### SyncCachedStateService + +These are the same as CachedStateService, with the difference that it accepts a CachedStateService and requires no preloading. + +### PreFilledStateService + +Pre-filled with only a part of the data needed. This is mostly used in Task implementation where all state needed is passed as args. \ No newline at end of file From 59eaf9d71ffdb9eb7f301a2e468ac0d6f8855659 Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Fri, 27 Oct 2023 13:46:22 +0200 Subject: [PATCH 3/4] Fixed Block Proving bug --- .../src/blockmodules/AccountStateModule.ts | 2 + .../production/tasks/BlockProvingTask.ts | 48 ++++++++++++------- .../test/integration/BlockProduction.test.ts | 27 ++++++++--- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/packages/protocol/src/blockmodules/AccountStateModule.ts b/packages/protocol/src/blockmodules/AccountStateModule.ts index 19117706..6fe16605 100644 --- a/packages/protocol/src/blockmodules/AccountStateModule.ts +++ b/packages/protocol/src/blockmodules/AccountStateModule.ts @@ -23,6 +23,8 @@ export class AccountStateModule extends ProvableTransactionHook( + startingState: DecodedState, + callback: () => Promise + ): Promise { + const prefilledStateService = new PreFilledStateService(startingState); + this.protocol.stateServiceProvider.setCurrentStateService( + prefilledStateService + ); + + const returnValue = await callback(); + + this.protocol.stateServiceProvider.popCurrentStateService(); + + return returnValue; + } + public async compute( input: PairingDerivedInput< StateTransitionProof, @@ -202,23 +217,24 @@ export class BlockProvingTask const stateTransitionProof = input.input1; const runtimeProof = input.input2; - const prefilledStateService = new PreFilledStateService( - input.params.startingState - ); - this.protocol.stateServiceProvider.setCurrentStateService( - prefilledStateService + await this.executeWithPrefilledStateService( + input.params.startingState, + // eslint-disable-next-line putout/putout + async () => { + this.blockProver.proveTransaction( + input.params.publicInput, + stateTransitionProof, + runtimeProof, + input.params.executionData + ); + } ); - this.blockProver.proveTransaction( - input.params.publicInput, - stateTransitionProof, - runtimeProof, - input.params.executionData + return await this.executeWithPrefilledStateService( + input.params.startingState, + async () => + await this.executionContext.current().result.prove() ); - - this.protocol.stateServiceProvider.popCurrentStateService(); - - return await this.executionContext.current().result.prove(); } // eslint-disable-next-line sonarjs/no-identical-functions diff --git a/packages/sequencer/test/integration/BlockProduction.test.ts b/packages/sequencer/test/integration/BlockProduction.test.ts index ef8e94b1..28577d74 100644 --- a/packages/sequencer/test/integration/BlockProduction.test.ts +++ b/packages/sequencer/test/integration/BlockProduction.test.ts @@ -5,11 +5,7 @@ import "reflect-metadata"; // TODO this is actually a big issue // eslint-disable-next-line import/no-extraneous-dependencies import { AppChain } from "@proto-kit/sdk"; -import { - Fieldable, - Runtime, - MethodIdResolver, -} from "@proto-kit/module"; +import { Fieldable, Runtime, MethodIdResolver } from "@proto-kit/module"; import { AccountState, AccountStateModule, @@ -93,7 +89,7 @@ describe("block production", () => { const protocolClass = VanillaProtocol.from( { AccountStateModule }, - { AccountStateModule: {}, StateTransitionProver: {}, BlockProver: {} } + { StateTransitionProver: {}, BlockProver: {}, AccountStateModule: {} } ); const app = AppChain.from({ @@ -103,6 +99,25 @@ describe("block production", () => { modules: {}, }); + app.configure({ + Sequencer: { + BlockTrigger: {}, + Mempool: {}, + BlockProducerModule: {}, + LocalTaskWorkerModule: {}, + BaseLayer: {}, + TaskQueue: {}, + }, + Runtime: { + Balance: {}, + }, + Protocol: { + AccountStateModule: {}, + BlockProver: {}, + StateTransitionProver: {}, + }, + }); + // Start AppChain await app.start(); From 1abd365dca4bfa2d2ccc582ea29614b2e71b3c41 Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Fri, 27 Oct 2023 14:03:04 +0200 Subject: [PATCH 4/4] Removed log --- packages/protocol/src/blockmodules/AccountStateModule.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/protocol/src/blockmodules/AccountStateModule.ts b/packages/protocol/src/blockmodules/AccountStateModule.ts index 6fe16605..19117706 100644 --- a/packages/protocol/src/blockmodules/AccountStateModule.ts +++ b/packages/protocol/src/blockmodules/AccountStateModule.ts @@ -23,8 +23,6 @@ export class AccountStateModule extends ProvableTransactionHook