Skip to content

Commit 7fd6fb3

Browse files
committed
New STProver: Protocol side
1 parent 21f2f34 commit 7fd6fb3

31 files changed

+1241
-570
lines changed

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from "./config/ModuleContainer";
22
export * from "./config/ConfigurableModule";
33
export * from "./config/ChildContainerProvider";
44
export * from "./config/ChildContainerCreatable";
5+
export * from "./config/injectAlias";
56
export * from "./types";
67
export * from "./zkProgrammable/ZkProgrammable";
78
export * from "./zkProgrammable/ProvableMethodExecutionContext";

packages/common/src/trees/RollupMerkleTree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export function createMerkleTree(height: number): AbstractMerkleTreeClass {
222222
public static WITNESS = RollupMerkleWitness;
223223

224224
// private in interface
225+
// TODO Cache this in some static variable so that we don't recompute it every time
225226
readonly zeroes: bigint[];
226227

227228
readonly store: MerkleTreeStore;

packages/common/src/utils.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "o1js";
88

99
import { TypedClass } from "./types";
10+
import _ from "lodash";
1011

1112
export function requireTrue(
1213
condition: boolean,
@@ -68,6 +69,26 @@ export function reduceSequential<T, U>(
6869
);
6970
}
7071

72+
export function yieldSequential<Source, State, Target>(
73+
array: Source[],
74+
callbackfn: (
75+
previousValue: State,
76+
currentValue: Source,
77+
currentIndex: number,
78+
array: Source[]
79+
) => Promise<[State, Target]>,
80+
initialValue: State
81+
): Promise<[State, Target[]]> {
82+
return reduceSequential<Source, [State, Target[]]>(
83+
array,
84+
async ([state, collectedTargets], curr, index, arr) => {
85+
const [newState, addition] = await callbackfn(state, curr, index, arr);
86+
return [newState, collectedTargets.concat(addition)];
87+
},
88+
[initialValue, []]
89+
);
90+
}
91+
7192
export function mapSequential<T, R>(
7293
array: T[],
7394
f: (element: T, index: number, array: T[]) => Promise<R>
@@ -194,3 +215,39 @@ export function safeParseJson<T>(json: string) {
194215
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
195216
return JSON.parse(json) as T;
196217
}
218+
219+
export function isFull<T>(t: Partial<T>): t is T {
220+
return Object.values(t).findIndex((v) => v === undefined) === -1;
221+
}
222+
223+
// TODO Restructure utils into separate package and multiple files
224+
225+
export function padArray<T>(
226+
batch: T[],
227+
batchSize: number,
228+
generator: (index: number) => T
229+
): T[] {
230+
const slice = batch.slice();
231+
const dummies = range(0, batchSize - (batch.length % batchSize)).map((i) =>
232+
generator(i + batch.length)
233+
);
234+
slice.push(...dummies);
235+
return slice;
236+
}
237+
238+
export function batch<T>(
239+
arr: T[],
240+
batchSize: number,
241+
dummy: (index: number) => T
242+
): T[][] {
243+
const padded = padArray(arr, batchSize, dummy);
244+
245+
const partitioned = _.groupBy(
246+
padded.map((v, i) => [v, i] as const),
247+
([v, i]) => Math.floor(i / batchSize)
248+
);
249+
250+
const numBatches = Math.floor(arr.length / batchSize);
251+
252+
return range(0, numBatches).map((i) => partitioned[i].map((x) => x[0]));
253+
}

packages/common/src/zkProgrammable/provableMethod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export function toProver(
3131
);
3232

3333
if (zkProgram === undefined) {
34-
throw new Error("Correct ZkProgram not found");
34+
throw new Error(
35+
`Correct ZkProgram not found (searching for method ${methodName})`
36+
);
3537
}
3638

3739
if (areProofsEnabled) {

packages/module/src/method/runtimeMethod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const errors = {
3737
};
3838

3939
export function toStateTransitionsHash(
40-
stateTransitions: StateTransition<any>[]
40+
stateTransitions: { toProvable: () => ProvableStateTransition }[]
4141
) {
4242
const stateTransitionsHashList = new StateTransitionReductionList(
4343
ProvableStateTransition

packages/protocol/src/hooks/AccountStateHook.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { PublicKey, Struct, UInt64 } from "o1js";
22
import { injectable } from "tsyringe";
3+
import { noop } from "@proto-kit/common";
34

4-
import { BlockProverExecutionData } from "../prover/block/BlockProvable";
55
import { StateMap } from "../state/StateMap";
66
import { protocolState } from "../state/protocol/ProtocolState";
7-
import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
7+
import {
8+
ProvableTransactionHook,
9+
BeforeTransactionHookArguments,
10+
} from "../protocol/ProvableTransactionHook";
811
import { assert } from "../state/assert/assert";
912

1013
export class AccountState extends Struct({
@@ -18,7 +21,7 @@ export class AccountStateHook extends ProvableTransactionHook {
1821
AccountState
1922
);
2023

21-
public async onTransaction({ transaction }: BlockProverExecutionData) {
24+
public async onTransaction({ transaction }: BeforeTransactionHookArguments) {
2225
const sender = transaction.sender.value;
2326

2427
const aso = await this.accountState.get(sender);
@@ -45,4 +48,8 @@ export class AccountStateHook extends ProvableTransactionHook {
4548

4649
await this.accountState.set(sender, new AccountState({ nonce: newNonce }));
4750
}
51+
52+
public async onAfterTransaction() {
53+
noop();
54+
}
4855
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
import { ProvableBlockHook } from "../protocol/ProvableBlockHook";
1+
import {
2+
AfterBlockHookArguments,
3+
ProvableBlockHook,
4+
} from "../protocol/ProvableBlockHook";
25
import { NetworkState } from "../model/network/NetworkState";
3-
import { BlockProverState } from "../prover/block/BlockProver";
46

57
export class LastStateRootBlockHook extends ProvableBlockHook<
68
Record<string, never>
79
> {
810
public async afterBlock(
911
networkState: NetworkState,
10-
state: BlockProverState
12+
{ stateRoot }: AfterBlockHookArguments
1113
): Promise<NetworkState> {
1214
return new NetworkState({
1315
block: networkState.block,
1416
previous: {
15-
rootHash: state.stateRoot,
17+
rootHash: stateRoot,
1618
},
1719
});
1820
}
1921

20-
public async beforeBlock(
21-
networkState: NetworkState,
22-
state: BlockProverState
23-
): Promise<NetworkState> {
22+
public async beforeBlock(networkState: NetworkState): Promise<NetworkState> {
2423
return networkState;
2524
}
2625
}

packages/protocol/src/hooks/NoopBlockHook.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { NoConfig } from "@proto-kit/common";
22

3-
import { ProvableBlockHook } from "../protocol/ProvableBlockHook";
3+
import {
4+
AfterBlockHookArguments,
5+
BeforeBlockHookArguments,
6+
ProvableBlockHook,
7+
} from "../protocol/ProvableBlockHook";
48
import { NetworkState } from "../model/network/NetworkState";
5-
import { BlockProverState } from "../prover/block/BlockProver";
69

710
export class NoopBlockHook extends ProvableBlockHook<NoConfig> {
811
public async afterBlock(
912
networkState: NetworkState,
10-
state: BlockProverState
13+
state: AfterBlockHookArguments
1114
): Promise<NetworkState> {
1215
return networkState;
1316
}
1417

1518
public async beforeBlock(
1619
networkState: NetworkState,
17-
state: BlockProverState
20+
state: BeforeBlockHookArguments
1821
): Promise<NetworkState> {
1922
return networkState;
2023
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { noop } from "@proto-kit/common";
22

33
import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
4-
import { BlockProverExecutionData } from "../prover/block/BlockProvable";
54

65
export class NoopTransactionHook extends ProvableTransactionHook {
7-
public async onTransaction(executionData: BlockProverExecutionData) {
6+
public async onTransaction() {
7+
noop();
8+
}
9+
10+
public async onAfterTransaction() {
811
noop();
912
}
1013
}

packages/protocol/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ export * from "./utils/ProvableHashList";
1515
export * from "./utils/PrefixedProvableHashList";
1616
export * from "./utils/MinaPrefixedProvableHashList";
1717
export * from "./utils/ProvableReductionHashList";
18-
export * from "./utils/StateTransitionReductionList";
1918
export * from "./utils/utils";
19+
export * from "./utils/FieldOptions";
20+
export * from "./prover/accumulators/StateTransitionReductionList";
21+
export * from "./prover/accumulators/AppliedBatchHashList";
22+
export * from "./prover/accumulators/WitnessedRootHashList";
23+
export * from "./prover/accumulators/TransactionHashList";
2024
export * from "./prover/block/BlockProver";
2125
export * from "./prover/block/BlockProvable";
2226
export * from "./prover/block/accummulators/RuntimeVerificationKeyTree";

0 commit comments

Comments
 (0)