Skip to content

Commit 12aefff

Browse files
committed
First working e2e, fixed dummies in STProver, code cleanup
1 parent 7fd6fb3 commit 12aefff

File tree

59 files changed

+2806
-1376
lines changed

Some content is hidden

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

59 files changed

+2806
-1376
lines changed

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/trees/RollupMerkleTree.ts

Lines changed: 1 addition & 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
}

packages/common/src/utils.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
DynamicProof,
66
Proof,
77
} from "o1js";
8+
import _ from "lodash";
89

910
import { TypedClass } from "./types";
10-
import _ from "lodash";
1111

1212
export function requireTrue(
1313
condition: boolean,
@@ -216,20 +216,24 @@ export function safeParseJson<T>(json: string) {
216216
return JSON.parse(json) as T;
217217
}
218218

219-
export function isFull<T>(t: Partial<T>): t is T {
219+
export type Nullable<T> = {
220+
[Key in keyof T]: T[Key] | undefined;
221+
};
222+
223+
export function isFull<T>(t: Nullable<T>): t is T {
220224
return Object.values(t).findIndex((v) => v === undefined) === -1;
221225
}
222226

223227
// TODO Restructure utils into separate package and multiple files
224228

225229
export function padArray<T>(
226-
batch: T[],
230+
array: T[],
227231
batchSize: number,
228232
generator: (index: number) => T
229233
): T[] {
230-
const slice = batch.slice();
231-
const dummies = range(0, batchSize - (batch.length % batchSize)).map((i) =>
232-
generator(i + batch.length)
234+
const slice = array.slice();
235+
const dummies = range(0, batchSize - (array.length % batchSize)).map((i) =>
236+
generator(i + array.length)
233237
);
234238
slice.push(...dummies);
235239
return slice;
@@ -247,7 +251,7 @@ export function batch<T>(
247251
([v, i]) => Math.floor(i / batchSize)
248252
);
249253

250-
const numBatches = Math.floor(arr.length / batchSize);
254+
const numBatches = Math.ceil(arr.length / batchSize);
251255

252256
return range(0, numBatches).map((i) => partitioned[i].map((x) => x[0]));
253257
}

packages/library/src/hooks/TransactionFeeHook.ts

Lines changed: 7 additions & 2 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";
@@ -126,7 +127,7 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
126127
* @param executionData
127128
*/
128129
public async onTransaction(
129-
executionData: BlockProverExecutionData
130+
executionData: BeforeTransactionHookArguments
130131
): Promise<void> {
131132
const feeConfig = Provable.witness(MethodFeeConfigData, () =>
132133
this.feeAnalyzer.getFeeConfig(
@@ -157,4 +158,8 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
157158
UInt64.Unsafe.fromField(fee.value)
158159
);
159160
}
161+
162+
public async onAfterTransaction(): Promise<void> {
163+
noop();
164+
}
160165
}

packages/module/src/method/runtimeMethod.ts

Lines changed: 0 additions & 1 deletion
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,

packages/protocol/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from "./model/transaction/RuntimeTransaction";
1111
export * from "./model/transaction/ValueOption";
1212
export * from "./model/MethodPublicOutput";
1313
export * from "./model/RuntimeLike";
14+
export * from "./model/AppliedStateTransitionBatch";
1415
export * from "./utils/ProvableHashList";
1516
export * from "./utils/PrefixedProvableHashList";
1617
export * from "./utils/MinaPrefixedProvableHashList";

packages/protocol/src/model/StateTransition.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export class ProvableStateTransition extends Struct({
2323
to: Option.none().toProvable(),
2424
});
2525
}
26+
27+
public static isDummy(stateTransition: ProvableStateTransition) {
28+
return stateTransition.path
29+
.equals(0)
30+
.and(stateTransition.from.isSome.not());
31+
}
2632
}
2733

2834
/**

packages/protocol/src/model/StateTransitionProvableBatch.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export class StateTransitionType {
2020
*/
2121
export class ProvableStateTransitionType extends Struct({
2222
type: Field,
23-
// TODO Remove accumulate or remove the Bool array args
24-
accumulate: Bool,
2523
}) {
2624
public static get nothing(): ProvableStateTransitionType {
2725
return this.from(StateTransitionType.nothing);
@@ -35,10 +33,9 @@ export class ProvableStateTransitionType extends Struct({
3533
return this.from(StateTransitionType.closeAndThrowAway);
3634
}
3735

38-
private static from(constant: number, accumulate = false) {
36+
private static from(constant: number) {
3937
return new ProvableStateTransitionType({
4038
type: Field(constant),
41-
accumulate: Bool(accumulate),
4239
});
4340
}
4441

@@ -48,6 +45,10 @@ export class ProvableStateTransitionType extends Struct({
4845
// 0^2 == 0 && 1^2 == 1
4946
return type.mul(type).equals(type);
5047
}
48+
49+
public isNothing() {
50+
return this.type.equals(ProvableStateTransitionType.nothing.type);
51+
}
5152
}
5253

5354
export class MerkleWitnessBatch extends Struct({
@@ -57,17 +58,12 @@ export class MerkleWitnessBatch extends Struct({
5758
),
5859
}) {}
5960

60-
// export class STProverBoolArray extends Struct({
61-
// values: Provable.Array(Bool, constants.stateTransitionProverBatchSize),
62-
// }) {}
63-
64-
// TODO Name
65-
export class ProvableStateTransitionInformation extends Struct({
61+
export class ProvableStateTransitionEntry extends Struct({
6662
stateTransition: ProvableStateTransition,
6763
type: ProvableStateTransitionType,
6864
witnessRoot: Bool,
6965
}) {
70-
public static dummy(): ProvableStateTransitionInformation {
66+
public static dummy(): ProvableStateTransitionEntry {
7167
return {
7268
stateTransition: ProvableStateTransition.dummy(),
7369
type: ProvableStateTransitionType.nothing,
@@ -80,47 +76,53 @@ export class ProvableStateTransitionInformation extends Struct({
8076
* A Batch of StateTransitions to be consumed by the StateTransitionProver
8177
* to prove multiple STs at once
8278
*
83-
* bases: Describes the state root on which the ST will be applied on
84-
* If it is zero, this means that this ST should connect with the previous one
85-
* If it is one, this means that the batch should be closed
79+
* The batch is formed as an array fo ProvableSTEntries, which have a type and
80+
* witnessesRoot flag attached to them.
8681
*/
8782
export class StateTransitionProvableBatch extends Struct({
8883
batch: Provable.Array(
89-
ProvableStateTransitionInformation,
84+
ProvableStateTransitionEntry,
9085
constants.stateTransitionProverBatchSize
9186
),
9287
}) {
93-
// TODO Test
9488
public static fromBatches(
9589
batches: {
9690
stateTransitions: ProvableStateTransition[];
9791
applied: Bool;
9892
witnessRoot: Bool;
9993
}[]
10094
): StateTransitionProvableBatch[] {
101-
const flattened = batches.flatMap((stBatch, i) =>
102-
stBatch.stateTransitions.map<ProvableStateTransitionInformation>(
103-
(stateTransition, j, sts) => {
104-
return {
105-
stateTransition,
106-
type:
107-
// eslint-disable-next-line no-nested-ternary
108-
j === sts.length - 1
109-
? stBatch.applied.toBoolean()
110-
? ProvableStateTransitionType.closeAndApply
111-
: ProvableStateTransitionType.closeAndThrowAway
112-
: ProvableStateTransitionType.nothing,
113-
witnessRoot:
114-
j === sts.length - 1 ? stBatch.witnessRoot : Bool(false),
115-
};
116-
}
117-
)
118-
);
95+
const flattened: ProvableStateTransitionEntry[] = [];
96+
97+
for (const stBatch of batches) {
98+
const entries =
99+
stBatch.stateTransitions.map<ProvableStateTransitionEntry>(
100+
(stateTransition, j, sts) => {
101+
return {
102+
stateTransition,
103+
type:
104+
// eslint-disable-next-line no-nested-ternary
105+
j === sts.length - 1
106+
? stBatch.applied.toBoolean()
107+
? ProvableStateTransitionType.closeAndApply
108+
: ProvableStateTransitionType.closeAndThrowAway
109+
: ProvableStateTransitionType.nothing,
110+
witnessRoot: Bool(false),
111+
};
112+
}
113+
);
114+
115+
flattened.push(...entries);
116+
117+
if (stBatch.witnessRoot.toBoolean() && flattened.length > 0) {
118+
flattened.at(-1)!.witnessRoot = Bool(true);
119+
}
120+
}
119121

120122
const values = batch(
121123
flattened,
122124
constants.stateTransitionProverBatchSize,
123-
() => ProvableStateTransitionInformation.dummy()
125+
() => ProvableStateTransitionEntry.dummy()
124126
);
125127

126128
return values.map((stBatch) => {

packages/protocol/src/prover/accumulators/WitnessedRootHashList.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ export class WitnessedRootHashList extends DefaultProvableHashList<WitnessedRoot
2525
super(WitnessedRoot, commitment);
2626
}
2727

28-
// To be used by the STProver
29-
// public pushWitness() {}
30-
31-
// To be used by the BlockProver
32-
public validateWitnessedRoot(
28+
/**
29+
* To be used by the BlockProver or for tracing
30+
*
31+
* The main purpose of this method compared to the simple push methods
32+
* is for deduplicating witnessed roots. We need to do this because the
33+
* STProver can only witness once per batch, therefore if multiple witness
34+
* points fall back to the same ST (because any batches in between were empty),
35+
* this has to be detected and compensated for.
36+
* This function does this using the preimage of the current list state.
37+
*
38+
* @param preimage The preimage to the **current** state of the list.
39+
*/
40+
public witnessRoot(
3341
witnessedRoot: WitnessedRoot,
3442
preimage: Field,
3543
condition: Bool

0 commit comments

Comments
 (0)