Skip to content

Commit 6449f7d

Browse files
authored
Merge pull request #260 from proto-kit/feature/st-prover-3
STProver v3
2 parents 7f233d3 + 885679c commit 6449f7d

File tree

98 files changed

+5152
-2730
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+5152
-2730
lines changed

.github/workflows/pull-request-develop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
- uses: actions/checkout@v3
8181
- uses: actions/setup-node@v3
8282
with:
83-
node-version: 22.9.0
83+
node-version: 18
8484
cache: npm
8585

8686
- name: "Install dependencies"

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/common/src/index.ts

Lines changed: 1 addition & 1 deletion
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";
@@ -22,4 +23,3 @@ export * from "./compiling/AtomicCompileHelper";
2223
export * from "./compiling/CompileRegistry";
2324
export * from "./compiling/CompilableModule";
2425
export * from "./compiling/services/ChildVerificationKeyService";
25-
export * from "./config/injectAlias";

packages/common/src/trees/RollupMerkleTree.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export function createMerkleTree(height: number): AbstractMerkleTreeClass {
200200

201201
public static dummy() {
202202
return new RollupMerkleWitness({
203-
isLeft: Array<Bool>(height - 1).fill(Bool(false)),
203+
isLeft: Array<Bool>(height - 1).fill(Bool(true)),
204204
path: Array<Field>(height - 1).fill(Field(0)),
205205
});
206206
}
@@ -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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
DynamicProof,
66
Proof,
77
} from "o1js";
8+
import _ from "lodash";
89

910
import { TypedClass } from "./types";
1011

@@ -72,6 +73,26 @@ export function reduceSequential<T, U>(
7273
);
7374
}
7475

76+
export function yieldSequential<Source, State, Target>(
77+
array: Source[],
78+
callbackfn: (
79+
previousValue: State,
80+
currentValue: Source,
81+
currentIndex: number,
82+
array: Source[]
83+
) => Promise<[State, Target]>,
84+
initialValue: State
85+
): Promise<[State, Target[]]> {
86+
return reduceSequential<Source, [State, Target[]]>(
87+
array,
88+
async ([state, collectedTargets], curr, index, arr) => {
89+
const [newState, addition] = await callbackfn(state, curr, index, arr);
90+
return [newState, collectedTargets.concat(addition)];
91+
},
92+
[initialValue, []]
93+
);
94+
}
95+
7596
export function mapSequential<T, R>(
7697
array: T[],
7798
f: (element: T, index: number, array: T[]) => Promise<R>
@@ -198,3 +219,43 @@ export function safeParseJson<T>(json: string) {
198219
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
199220
return JSON.parse(json) as T;
200221
}
222+
223+
export type Nullable<T> = {
224+
[Key in keyof T]: T[Key] | undefined;
225+
};
226+
227+
export function isFull<T>(t: Nullable<T>): t is T {
228+
return Object.values(t).findIndex((v) => v === undefined) === -1;
229+
}
230+
231+
// TODO Restructure utils into separate package and multiple files
232+
233+
export function padArray<T>(
234+
array: T[],
235+
batchSize: number,
236+
generator: (index: number) => T
237+
): T[] {
238+
const slice = array.slice();
239+
const dummies = range(0, batchSize - (array.length % batchSize)).map((i) =>
240+
generator(i + array.length)
241+
);
242+
slice.push(...dummies);
243+
return slice;
244+
}
245+
246+
export function batch<T>(
247+
arr: T[],
248+
batchSize: number,
249+
dummy: (index: number) => T
250+
): T[][] {
251+
const padded = padArray(arr, batchSize, dummy);
252+
253+
const partitioned = _.groupBy(
254+
padded.map((v, i) => [v, i] as const),
255+
([v, i]) => Math.floor(i / batchSize)
256+
);
257+
258+
const numBatches = Math.ceil(arr.length / batchSize);
259+
260+
return range(0, numBatches).map((i) => partitioned[i].map((x) => x[0]));
261+
}

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/deployment/src/queue/BullQueue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class BullQueue
5151
await this.activePromise;
5252
}
5353
let resOutside: () => void = () => {};
54+
// TODO Use Promise.withResolvers() for that
5455
const promise = new Promise<void>((res) => {
5556
resOutside = res;
5657
});

packages/library/src/hooks/TransactionFeeHook.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {
66
} from "@proto-kit/module";
77
import { inject, injectable } from "tsyringe";
88
import {
9+
BeforeTransactionHookArguments,
910
ProvableTransactionHook,
10-
BlockProverExecutionData,
1111
PublicKeyOption,
1212
} from "@proto-kit/protocol";
1313
import { Field, Provable, PublicKey } from "o1js";
14+
import { noop } from "@proto-kit/common";
1415

1516
import { UInt64 } from "../math/UInt64";
1617
import { Balance, TokenId } from "../runtime/Balances";
@@ -122,8 +123,8 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
122123
*
123124
* @param executionData
124125
*/
125-
public async onTransaction(
126-
executionData: BlockProverExecutionData
126+
public async beforeTransaction(
127+
executionData: BeforeTransactionHookArguments
127128
): Promise<void> {
128129
const feeConfig = Provable.witness(MethodFeeConfigData, () =>
129130
this.feeAnalyzer.getFeeConfig(
@@ -154,4 +155,8 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
154155
UInt64.Unsafe.fromField(fee.value)
155156
);
156157
}
158+
159+
public async afterTransaction(): Promise<void> {
160+
noop();
161+
}
157162
}

packages/module/src/method/runtimeMethod.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Bool, Field, FlexibleProvablePure, Poseidon } from "o1js";
22
import { container } from "tsyringe";
33
import {
4-
StateTransition,
54
ProvableStateTransition,
65
MethodPublicOutput,
76
RuntimeMethodExecutionContext,
@@ -40,7 +39,7 @@ const errors = {
4039
};
4140

4241
export function toStateTransitionsHash(
43-
stateTransitions: StateTransition<any>[]
42+
stateTransitions: { toProvable: () => ProvableStateTransition }[]
4443
) {
4544
const stateTransitionsHashList = new StateTransitionReductionList(
4645
ProvableStateTransition
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `blockStateTransitions` on the `BlockResult` table. All the data in the column will be lost.
5+
- You are about to drop the column `protocolTransitions` on the `TransactionExecutionResult` table. All the data in the column will be lost.
6+
- Added the required column `beforeBlockStateTransitions` to the `Block` table without a default value. This is not possible if the table is not empty.
7+
- Added the required column `fromStateRoot` to the `Block` table without a default value. This is not possible if the table is not empty.
8+
- Added the required column `afterBlockStateTransitions` to the `BlockResult` table without a default value. This is not possible if the table is not empty.
9+
10+
*/
11+
-- AlterTable
12+
ALTER TABLE "Block" ADD COLUMN "beforeBlockStateTransitions" JSON NOT NULL,
13+
ADD COLUMN "fromStateRoot" TEXT NOT NULL;
14+
15+
-- AlterTable
16+
ALTER TABLE "BlockResult" DROP COLUMN "blockStateTransitions",
17+
ADD COLUMN "afterBlockStateTransitions" JSON NOT NULL,
18+
ADD COLUMN "witnessedRoots" TEXT[];
19+
20+
-- AlterTable
21+
ALTER TABLE "TransactionExecutionResult" DROP COLUMN "protocolTransitions";

0 commit comments

Comments
 (0)