Skip to content

Commit 48fa9e0

Browse files
committed
Merge branch 'develop' into feature/worker-readiness
# Conflicts: # packages/common/src/utils.ts # packages/protocol/src/prover/block/BlockProver.ts # packages/protocol/src/prover/statetransition/StateTransitionProvable.ts # packages/sequencer/src/protocol/production/tasks/StateTransitionTask.ts
2 parents ee0d5b2 + e56a02c commit 48fa9e0

25 files changed

+473
-212
lines changed

packages/common/src/config/ModuleContainer.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,6 @@ export class ModuleContainer<
454454
);
455455
}
456456

457-
protected createSuperClassAliases(module: any) {}
458-
459457
/**
460458
* This is a placeholder for individual modules to override.
461459
* This method will be called whenever the underlying container fully

packages/common/src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export function requireTrue(
1919
}
2020
}
2121

22+
/**
23+
* Utility function to split an array of type T into a record <K, T[]> based on a
24+
* function T => K that determines the key of each record
25+
*/
2226
export function splitArray<T, K extends string | number>(
2327
arr: T[],
2428
split: (t: T) => K
@@ -167,6 +171,8 @@ type NonMethodKeys<Type> = {
167171
}[keyof Type];
168172
export type NonMethods<Type> = Pick<Type, NonMethodKeys<Type>>;
169173

174+
export const MAX_FIELD = Field(Field.ORDER - 1n);
175+
170176
/**
171177
* Returns a boolean indicating whether a given class is a subclass of another class,
172178
* indicated by the name parameter.

packages/library/src/hooks/RuntimeFeeAnalyzerService.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class RuntimeFeeAnalyzerService extends ConfigurableModule<RuntimeFeeAnal
8181
});
8282

8383
container.resolve(RuntimeMethodExecutionContext).clear();
84-
84+
let methodCounter = 0;
8585
const [values, indexes] =
8686
await this.runtime.zkProgrammable.zkProgram.reduce<
8787
Promise<[FeeTreeValues, FeeIndexes]>
@@ -93,7 +93,7 @@ export class RuntimeFeeAnalyzerService extends ConfigurableModule<RuntimeFeeAnal
9393
[FeeTreeValues, FeeIndexes]
9494
>(
9595
// eslint-disable-next-line @typescript-eslint/no-shadow
96-
([values, indexes], combinedMethodName, index) => {
96+
([values, indexes], combinedMethodName) => {
9797
const { rows } = analyzedMethods[combinedMethodName];
9898
// const rows = 1000;
9999
const [moduleName, methodName] = combinedMethodName.split(".");
@@ -128,7 +128,8 @@ export class RuntimeFeeAnalyzerService extends ConfigurableModule<RuntimeFeeAnal
128128
},
129129
{
130130
...indexes,
131-
[methodId.toString()]: BigInt(index),
131+
// eslint-disable-next-line no-plusplus
132+
[methodId.toString()]: BigInt(methodCounter++),
132133
},
133134
];
134135
},

packages/protocol/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export * from "./prover/block/accummulators/BlockHashMerkleTree";
2424
export * from "./prover/block/services/RuntimeVerificationKeyRootService";
2525
export * from "./prover/statetransition/StateTransitionProver";
2626
export * from "./prover/statetransition/StateTransitionProvable";
27-
export * from "./prover/statetransition/StateTransitionWitnessProvider";
28-
export * from "./prover/statetransition/StateTransitionWitnessProviderReference";
2927
export * from "./protocol/Protocol";
3028
export * from "./protocol/ProtocolModule";
3129
export * from "./protocol/ProtocolEnvironment";

packages/protocol/src/model/StateTransitionProvableBatch.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Bool, Provable, Struct } from "o1js";
2-
import { range } from "@proto-kit/common";
2+
import {
3+
InMemoryMerkleTreeStorage,
4+
range,
5+
RollupMerkleTree,
6+
RollupMerkleTreeWitness,
7+
} from "@proto-kit/common";
38

49
import { constants } from "../Constants";
510

@@ -60,16 +65,22 @@ export class StateTransitionProvableBatch extends Struct({
6065
ProvableStateTransitionType,
6166
constants.stateTransitionProverBatchSize
6267
),
68+
69+
merkleWitnesses: Provable.Array(
70+
RollupMerkleTreeWitness,
71+
constants.stateTransitionProverBatchSize
72+
),
6373
}) {
6474
public static fromMappings(
6575
transitions: {
6676
transition: ProvableStateTransition;
6777
type: ProvableStateTransitionType;
68-
}[]
78+
}[],
79+
merkleWitnesses: RollupMerkleTreeWitness[]
6980
): StateTransitionProvableBatch {
7081
const batch = transitions.map((entry) => entry.transition);
7182
const transitionTypes = transitions.map((entry) => entry.type);
72-
83+
const witnesses = merkleWitnesses.slice();
7384
// Check that order is correct
7485
let normalSTsStarted = false;
7586
transitionTypes.forEach((x) => {
@@ -84,16 +95,23 @@ export class StateTransitionProvableBatch extends Struct({
8495
while (batch.length < constants.stateTransitionProverBatchSize) {
8596
batch.push(ProvableStateTransition.dummy());
8697
transitionTypes.push(ProvableStateTransitionType.normal);
98+
witnesses.push(
99+
new RollupMerkleTree(new InMemoryMerkleTreeStorage()).getWitness(
100+
BigInt(0)
101+
)
102+
);
87103
}
88104
return new StateTransitionProvableBatch({
89105
batch,
90106
transitionTypes,
107+
merkleWitnesses: witnesses,
91108
});
92109
}
93110

94111
public static fromTransitions(
95112
transitions: ProvableStateTransition[],
96-
protocolTransitions: ProvableStateTransition[]
113+
protocolTransitions: ProvableStateTransition[],
114+
merkleWitnesses: RollupMerkleTreeWitness[]
97115
): StateTransitionProvableBatch {
98116
const array = transitions.slice().concat(protocolTransitions);
99117

@@ -113,12 +131,14 @@ export class StateTransitionProvableBatch extends Struct({
113131
return new StateTransitionProvableBatch({
114132
batch: array,
115133
transitionTypes,
134+
merkleWitnesses,
116135
});
117136
}
118137

119138
private constructor(object: {
120139
batch: ProvableStateTransition[];
121140
transitionTypes: ProvableStateTransitionType[];
141+
merkleWitnesses: RollupMerkleTreeWitness[];
122142
}) {
123143
super(object);
124144
}

packages/protocol/src/prover/block/BlockProvable.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class BlockProverPublicInput extends Struct({
2424
blockHashRoot: Field,
2525
eternalTransactionsHash: Field,
2626
incomingMessagesHash: Field,
27+
blockNumber: Field,
2728
}) {}
2829

2930
export class BlockProverPublicOutput extends Struct({
@@ -36,15 +37,10 @@ export class BlockProverPublicOutput extends Struct({
3637
closed: Bool,
3738
blockNumber: Field,
3839
}) {
39-
public equals(
40-
input: BlockProverPublicInput,
41-
closed: Bool,
42-
blockNumber: Field
43-
): Bool {
40+
public equals(input: BlockProverPublicInput, closed: Bool): Bool {
4441
const output2 = BlockProverPublicOutput.toFields({
4542
...input,
4643
closed,
47-
blockNumber,
4844
});
4945
const output1 = BlockProverPublicOutput.toFields(this);
5046
return output1

packages/protocol/src/prover/block/BlockProver.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
CompilableModule,
1616
CompileArtifact,
1717
CompileRegistry,
18+
MAX_FIELD,
1819
PlainZkProgram,
1920
provableMethod,
2021
WithZkProgrammable,
@@ -128,10 +129,6 @@ export interface BlockProverState {
128129
incomingMessagesHash: Field;
129130
}
130131

131-
function maxField() {
132-
return Field(Field.ORDER - 1n);
133-
}
134-
135132
export type BlockProof = Proof<BlockProverPublicInput, BlockProverPublicOutput>;
136133
export type RuntimeProof = Proof<void, MethodPublicOutput>;
137134

@@ -422,6 +419,11 @@ export class BlockProverProgrammable extends ZkProgrammable<
422419
"ExecutionData Networkstate doesn't equal public input hash"
423420
);
424421

422+
publicInput.blockNumber.assertEquals(
423+
MAX_FIELD,
424+
"blockNumber has to be MAX for transaction proofs"
425+
);
426+
425427
// Verify the [methodId, vk] tuple against the baked-in vk tree root
426428
const { verificationKey, witness: verificationKeyTreeWitness } =
427429
verificationKeyWitness;
@@ -451,7 +453,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
451453

452454
return new BlockProverPublicOutput({
453455
...stateTo,
454-
blockNumber: maxField(),
456+
blockNumber: publicInput.blockNumber,
455457
closed: Bool(false),
456458
});
457459
}
@@ -534,14 +536,14 @@ export class BlockProverProgrammable extends ZkProgrammable<
534536
// stateTransitionProof.verifyIf(Bool(false));
535537
// stateTransitionProof.verifyIf(stsEmitted);
536538

537-
// Verify Transaction proof if it has at least 1 tx
539+
// Verify Transaction proof if it has at least 1 tx - i.e. the
540+
// input and output doesn't match fully
538541
// We have to compare the whole input and output because we can make no
539542
// assumptions about the values, since it can be an arbitrary dummy-proof
540543
const txProofOutput = transactionProof.publicOutput;
541544
const isEmptyTransition = txProofOutput.equals(
542545
transactionProof.publicInput,
543-
txProofOutput.closed,
544-
txProofOutput.blockNumber
546+
txProofOutput.closed
545547
);
546548
Provable.log("VerifyIf 2", isEmptyTransition.not());
547549
transactionProof.verifyIf(isEmptyTransition.not());
@@ -626,6 +628,8 @@ export class BlockProverProgrammable extends ZkProgrammable<
626628
// Calculate the new block index
627629
const blockIndex = blockWitness.calculateIndex();
628630

631+
blockIndex.assertEquals(publicInput.blockNumber);
632+
629633
blockWitness
630634
.calculateRoot(Field(0))
631635
.assertEquals(
@@ -643,7 +647,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
643647

644648
return new BlockProverPublicOutput({
645649
...state,
646-
blockNumber: blockIndex,
650+
blockNumber: blockIndex.add(1),
647651
closed: Bool(true),
648652
});
649653
}
@@ -750,19 +754,25 @@ export class BlockProverProgrammable extends ZkProgrammable<
750754
// assert proof1.height == proof2.height
751755
// }
752756

753-
const proof1Height = proof1.publicOutput.blockNumber;
754757
const proof1Closed = proof1.publicOutput.closed;
755-
const proof2Height = proof2.publicOutput.blockNumber;
756758
const proof2Closed = proof2.publicOutput.closed;
757759

758-
const isValidTransactionMerge = proof1Height
759-
.equals(maxField())
760-
.and(proof2Height.equals(proof1Height))
760+
const blockNumberProgressionValid = publicInput.blockNumber
761+
.equals(proof1.publicInput.blockNumber)
762+
.and(
763+
proof1.publicOutput.blockNumber.equals(proof2.publicInput.blockNumber)
764+
);
765+
766+
// For tx proofs, we check that the progression starts and end with MAX
767+
// in addition to that both proofs are non-closed
768+
const isValidTransactionMerge = publicInput.blockNumber
769+
.equals(MAX_FIELD)
770+
.and(blockNumberProgressionValid)
761771
.and(proof1Closed.or(proof2Closed).not());
762772

763773
const isValidClosedMerge = proof1Closed
764774
.and(proof2Closed)
765-
.and(proof1Height.add(1).equals(proof2Height));
775+
.and(blockNumberProgressionValid);
766776

767777
isValidTransactionMerge
768778
.or(isValidClosedMerge)
@@ -775,9 +785,8 @@ export class BlockProverProgrammable extends ZkProgrammable<
775785
blockHashRoot: proof2.publicOutput.blockHashRoot,
776786
eternalTransactionsHash: proof2.publicOutput.eternalTransactionsHash,
777787
incomingMessagesHash: proof2.publicOutput.incomingMessagesHash,
778-
// Provable.if(isValidClosedMerge, Bool(true), Bool(false));
779788
closed: isValidClosedMerge,
780-
blockNumber: proof2Height,
789+
blockNumber: proof2.publicOutput.blockNumber,
781790
});
782791
}
783792

packages/protocol/src/prover/statetransition/StateTransitionProvable.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { WithZkProgrammable, CompilableModule } from "@proto-kit/common";
33

44
import { StateTransitionProvableBatch } from "../../model/StateTransitionProvableBatch";
55

6-
import { StateTransitionWitnessProviderReference } from "./StateTransitionWitnessProviderReference";
7-
86
export class StateTransitionProverPublicInput extends Struct({
97
stateTransitionsHash: Field,
108
protocolTransitionsHash: Field,
@@ -30,8 +28,6 @@ export interface StateTransitionProvable
3028
StateTransitionProverPublicOutput
3129
>,
3230
CompilableModule {
33-
witnessProviderReference: StateTransitionWitnessProviderReference;
34-
3531
runBatch: (
3632
publicInput: StateTransitionProverPublicInput,
3733
batch: StateTransitionProvableBatch

0 commit comments

Comments
 (0)