Skip to content

Commit

Permalink
Fixed Block Proving bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanic committed Oct 27, 2023
1 parent 0e2e1e5 commit 2506070
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/protocol/src/blockmodules/AccountStateModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class AccountStateModule extends ProvableTransactionHook<{}> {

const currentNonce = accountState.nonce;

console.log(`Current Nonce ${currentNonce.toString()}, tx: ${transaction.nonce.toString()}`);

assert(currentNonce.equals(transaction.nonce), "Nonce not matching");

this.accountState.set(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import {
MethodPublicOutput,
Protocol,
ProtocolModulesRecord,
// eslint-disable-next-line @typescript-eslint/no-shadow
ReturnType,
StateTransitionProof,
StateTransitionProvable,
} from "@proto-kit/protocol";
import { Field, Proof, Provable } from "o1js";
import { Field, Proof } from "o1js";
import { Runtime } from "@proto-kit/module";
import { inject, injectable, Lifecycle, scoped } from "tsyringe";
import { ProvableMethodExecutionContext } from "@proto-kit/common";
Expand Down Expand Up @@ -192,6 +191,22 @@ export class BlockProvingTask
);
}

private async executeWithPrefilledStateService<Return>(
startingState: DecodedState,
callback: () => Promise<Return>
): Promise<Return> {
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,
Expand All @@ -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<BlockProof>()
);

this.protocol.stateServiceProvider.popCurrentStateService();

return await this.executionContext.current().result.prove<BlockProof>();
}

// eslint-disable-next-line sonarjs/no-identical-functions
Expand Down
27 changes: 21 additions & 6 deletions packages/sequencer/test/integration/BlockProduction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -93,7 +89,7 @@ describe("block production", () => {

const protocolClass = VanillaProtocol.from(
{ AccountStateModule },
{ AccountStateModule: {}, StateTransitionProver: {}, BlockProver: {} }
{ StateTransitionProver: {}, BlockProver: {}, AccountStateModule: {} }
);

const app = AppChain.from({
Expand All @@ -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();

Expand Down

0 comments on commit 2506070

Please sign in to comment.