@proto-kit/protocol 0.1.1-develop.1315 → 0.1.1-develop.1343

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/dist/hooks/AccountStateHook.d.ts +3 -3
  2. package/dist/hooks/AccountStateHook.d.ts.map +1 -1
  3. package/dist/hooks/AccountStateHook.js +6 -2
  4. package/dist/hooks/AccountStateHook.js.map +1 -1
  5. package/dist/hooks/LastStateRootBlockHook.d.ts +3 -4
  6. package/dist/hooks/LastStateRootBlockHook.d.ts.map +1 -1
  7. package/dist/hooks/LastStateRootBlockHook.js +4 -4
  8. package/dist/hooks/LastStateRootBlockHook.js.map +1 -1
  9. package/dist/hooks/NoopBlockHook.d.ts +3 -4
  10. package/dist/hooks/NoopBlockHook.d.ts.map +1 -1
  11. package/dist/hooks/NoopBlockHook.js +1 -1
  12. package/dist/hooks/NoopBlockHook.js.map +1 -1
  13. package/dist/hooks/NoopTransactionHook.d.ts +2 -2
  14. package/dist/hooks/NoopTransactionHook.d.ts.map +1 -1
  15. package/dist/hooks/NoopTransactionHook.js +4 -1
  16. package/dist/hooks/NoopTransactionHook.js.map +1 -1
  17. package/dist/index.d.ts +6 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +6 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/model/AppliedStateTransitionBatch.d.ts +114 -0
  22. package/dist/model/AppliedStateTransitionBatch.d.ts.map +1 -0
  23. package/dist/model/AppliedStateTransitionBatch.js +16 -0
  24. package/dist/model/AppliedStateTransitionBatch.js.map +1 -0
  25. package/dist/model/StateTransition.d.ts +1 -0
  26. package/dist/model/StateTransition.d.ts.map +1 -1
  27. package/dist/model/StateTransition.js +5 -0
  28. package/dist/model/StateTransition.js.map +1 -1
  29. package/dist/model/StateTransitionProvableBatch.d.ts +249 -97
  30. package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -1
  31. package/dist/model/StateTransitionProvableBatch.js +72 -65
  32. package/dist/model/StateTransitionProvableBatch.js.map +1 -1
  33. package/dist/model/transaction/RuntimeTransaction.d.ts +1 -0
  34. package/dist/model/transaction/RuntimeTransaction.d.ts.map +1 -1
  35. package/dist/model/transaction/RuntimeTransaction.js +3 -0
  36. package/dist/model/transaction/RuntimeTransaction.js.map +1 -1
  37. package/dist/protocol/ProvableBlockHook.d.ts +13 -3
  38. package/dist/protocol/ProvableBlockHook.d.ts.map +1 -1
  39. package/dist/protocol/ProvableBlockHook.js +16 -0
  40. package/dist/protocol/ProvableBlockHook.js.map +1 -1
  41. package/dist/protocol/ProvableTransactionHook.d.ts +29 -2
  42. package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -1
  43. package/dist/protocol/ProvableTransactionHook.js +9 -0
  44. package/dist/protocol/ProvableTransactionHook.js.map +1 -1
  45. package/dist/prover/accumulators/AppliedBatchHashList.d.ts +18 -0
  46. package/dist/prover/accumulators/AppliedBatchHashList.d.ts.map +1 -0
  47. package/dist/prover/accumulators/AppliedBatchHashList.js +25 -0
  48. package/dist/prover/accumulators/AppliedBatchHashList.js.map +1 -0
  49. package/dist/{utils → prover/accumulators}/StateTransitionReductionList.d.ts +3 -2
  50. package/dist/prover/accumulators/StateTransitionReductionList.d.ts.map +1 -0
  51. package/dist/{utils → prover/accumulators}/StateTransitionReductionList.js +6 -3
  52. package/dist/prover/accumulators/StateTransitionReductionList.js.map +1 -0
  53. package/dist/prover/accumulators/TransactionHashList.d.ts +6 -0
  54. package/dist/prover/accumulators/TransactionHashList.d.ts.map +1 -0
  55. package/dist/prover/accumulators/TransactionHashList.js +8 -0
  56. package/dist/prover/accumulators/TransactionHashList.js.map +1 -0
  57. package/dist/prover/accumulators/WitnessedRootHashList.d.ts +137 -0
  58. package/dist/prover/accumulators/WitnessedRootHashList.d.ts.map +1 -0
  59. package/dist/prover/accumulators/WitnessedRootHashList.js +50 -0
  60. package/dist/prover/accumulators/WitnessedRootHashList.js.map +1 -0
  61. package/dist/prover/block/BlockProvable.d.ts +579 -35
  62. package/dist/prover/block/BlockProvable.d.ts.map +1 -1
  63. package/dist/prover/block/BlockProvable.js +55 -3
  64. package/dist/prover/block/BlockProvable.js.map +1 -1
  65. package/dist/prover/block/BlockProver.d.ts +52 -50
  66. package/dist/prover/block/BlockProver.d.ts.map +1 -1
  67. package/dist/prover/block/BlockProver.js +289 -209
  68. package/dist/prover/block/BlockProver.js.map +1 -1
  69. package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts +55 -13
  70. package/dist/prover/block/accummulators/BlockHashMerkleTree.d.ts.map +1 -1
  71. package/dist/prover/block/accummulators/BlockHashMerkleTree.js +11 -3
  72. package/dist/prover/block/accummulators/BlockHashMerkleTree.js.map +1 -1
  73. package/dist/prover/statetransition/StateTransitionProvable.d.ts +107 -106
  74. package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -1
  75. package/dist/prover/statetransition/StateTransitionProvable.js +8 -8
  76. package/dist/prover/statetransition/StateTransitionProvable.js.map +1 -1
  77. package/dist/prover/statetransition/StateTransitionProver.d.ts +14 -11
  78. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
  79. package/dist/prover/statetransition/StateTransitionProver.js +116 -62
  80. package/dist/prover/statetransition/StateTransitionProver.js.map +1 -1
  81. package/dist/settlement/contracts/DispatchContractProtocolModule.d.ts +1 -1
  82. package/dist/settlement/contracts/DispatchSmartContract.d.ts +1 -1
  83. package/dist/settlement/contracts/SettlementSmartContract.d.ts.map +1 -1
  84. package/dist/settlement/contracts/SettlementSmartContract.js +1 -0
  85. package/dist/settlement/contracts/SettlementSmartContract.js.map +1 -1
  86. package/dist/state/assert/assert.d.ts.map +1 -1
  87. package/dist/state/assert/assert.js +5 -3
  88. package/dist/state/assert/assert.js.map +1 -1
  89. package/dist/state/context/RuntimeMethodExecutionContext.d.ts +3 -3
  90. package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -1
  91. package/dist/state/context/RuntimeMethodExecutionContext.js +7 -4
  92. package/dist/state/context/RuntimeMethodExecutionContext.js.map +1 -1
  93. package/dist/utils/FieldOptions.d.ts +62 -0
  94. package/dist/utils/FieldOptions.d.ts.map +1 -0
  95. package/dist/utils/FieldOptions.js +13 -0
  96. package/dist/utils/FieldOptions.js.map +1 -0
  97. package/dist/utils/ProvableHashList.d.ts +21 -2
  98. package/dist/utils/ProvableHashList.d.ts.map +1 -1
  99. package/dist/utils/ProvableHashList.js +37 -2
  100. package/dist/utils/ProvableHashList.js.map +1 -1
  101. package/dist/utils/ProvableReductionHashList.d.ts +7 -3
  102. package/dist/utils/ProvableReductionHashList.d.ts.map +1 -1
  103. package/dist/utils/ProvableReductionHashList.js +8 -5
  104. package/dist/utils/ProvableReductionHashList.js.map +1 -1
  105. package/dist/utils/utils.d.ts +10 -1
  106. package/dist/utils/utils.d.ts.map +1 -1
  107. package/dist/utils/utils.js +6 -0
  108. package/dist/utils/utils.js.map +1 -1
  109. package/package.json +2 -2
  110. package/src/hooks/AccountStateHook.ts +12 -3
  111. package/src/hooks/LastStateRootBlockHook.ts +7 -8
  112. package/src/hooks/NoopBlockHook.ts +7 -4
  113. package/src/hooks/NoopTransactionHook.ts +5 -2
  114. package/src/index.ts +6 -1
  115. package/src/model/AppliedStateTransitionBatch.ts +16 -0
  116. package/src/model/StateTransition.ts +6 -0
  117. package/src/model/StateTransitionProvableBatch.ts +94 -105
  118. package/src/model/transaction/RuntimeTransaction.ts +4 -0
  119. package/src/protocol/ProvableBlockHook.ts +51 -3
  120. package/src/protocol/ProvableTransactionHook.ts +67 -3
  121. package/src/prover/accumulators/AppliedBatchHashList.ts +32 -0
  122. package/src/{utils → prover/accumulators}/StateTransitionReductionList.ts +7 -4
  123. package/src/prover/accumulators/TransactionHashList.ts +9 -0
  124. package/src/prover/accumulators/WitnessedRootHashList.ts +61 -0
  125. package/src/prover/block/BlockProvable.ts +128 -9
  126. package/src/prover/block/BlockProver.ts +531 -383
  127. package/src/prover/block/accummulators/BlockHashMerkleTree.ts +11 -3
  128. package/src/prover/statetransition/StateTransitionProvable.ts +17 -11
  129. package/src/prover/statetransition/StateTransitionProver.ts +219 -144
  130. package/src/settlement/contracts/SettlementSmartContract.ts +4 -0
  131. package/src/state/assert/assert.ts +6 -3
  132. package/src/state/context/RuntimeMethodExecutionContext.ts +15 -7
  133. package/src/utils/FieldOptions.ts +13 -0
  134. package/src/utils/ProvableHashList.ts +77 -2
  135. package/src/utils/ProvableReductionHashList.ts +12 -3
  136. package/src/utils/utils.ts +18 -1
  137. package/test/BlockProver.test.ts +2 -0
  138. package/test/TestingProtocol.ts +5 -0
  139. package/test/model/StateTransitionProvableBatch.test.ts +137 -0
  140. package/test/prover/block/BlockProver.test.ts +18 -0
  141. package/test/prover/statetransition/StateTransitionProver.test.ts +240 -0
  142. package/test/utils/ProvableHashList.test.ts +44 -0
  143. package/test/utils/ProvableReductionHashList.test.ts +1 -1
  144. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts +0 -16
  145. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts.map +0 -1
  146. package/dist/prover/statetransition/StateTransitionWitnessProvider.js +0 -16
  147. package/dist/prover/statetransition/StateTransitionWitnessProviderReference.d.ts +0 -7
  148. package/dist/prover/statetransition/StateTransitionWitnessProviderReference.d.ts.map +0 -1
  149. package/dist/prover/statetransition/StateTransitionWitnessProviderReference.js +0 -19
  150. package/dist/state/context/ProtocolMethodExecutionContext.d.ts +0 -22
  151. package/dist/state/context/ProtocolMethodExecutionContext.d.ts.map +0 -1
  152. package/dist/state/context/ProtocolMethodExecutionContext.js +0 -28
  153. package/dist/utils/StateTransitionReductionList.d.ts.map +0 -1
  154. package/dist/utils/StateTransitionReductionList.js.map +0 -1
@@ -1,50 +1,74 @@
1
- import { Bool, Provable, Struct } from "o1js";
2
- import {
3
- InMemoryMerkleTreeStorage,
4
- range,
5
- RollupMerkleTree,
6
- RollupMerkleTreeWitness,
7
- } from "@proto-kit/common";
1
+ import { Bool, Field, Provable, Struct } from "o1js";
2
+ import { batch, RollupMerkleTreeWitness } from "@proto-kit/common";
8
3
 
9
4
  import { constants } from "../Constants";
10
5
 
11
6
  import { ProvableStateTransition } from "./StateTransition.js";
12
7
 
13
8
  export class StateTransitionType {
14
- public static readonly normal = true;
9
+ public static readonly nothing = 2;
15
10
 
16
- public static readonly protocol = false;
11
+ // The reason these are 0 and 1 is to efficiently check
12
+ // x in [inside, closing] in-circuit via the boolean trick
13
+ public static readonly closeAndApply = 1;
17
14
 
18
- public static isNormal(type: boolean) {
19
- return type === StateTransitionType.normal;
20
- }
21
-
22
- public static isProtocol(type: boolean) {
23
- return type === StateTransitionType.protocol;
24
- }
15
+ public static readonly closeAndThrowAway = 0;
25
16
  }
26
17
 
18
+ /**
19
+ * STType is encoding both the type and whether it should be accumulated or not in one field
20
+ */
27
21
  export class ProvableStateTransitionType extends Struct({
28
- type: Bool,
22
+ type: Field,
29
23
  }) {
30
- public static get normal(): ProvableStateTransitionType {
31
- return new ProvableStateTransitionType({
32
- type: Bool(StateTransitionType.normal),
33
- });
24
+ public static get nothing(): ProvableStateTransitionType {
25
+ return this.from(StateTransitionType.nothing);
26
+ }
27
+
28
+ public static get closeAndApply(): ProvableStateTransitionType {
29
+ return this.from(StateTransitionType.closeAndApply);
34
30
  }
35
31
 
36
- public static get protocol(): ProvableStateTransitionType {
32
+ public static get closeAndThrowAway(): ProvableStateTransitionType {
33
+ return this.from(StateTransitionType.closeAndThrowAway);
34
+ }
35
+
36
+ private static from(constant: number) {
37
37
  return new ProvableStateTransitionType({
38
- type: Bool(StateTransitionType.protocol),
38
+ type: Field(constant),
39
39
  });
40
40
  }
41
41
 
42
- public isNormal(): Bool {
43
- return this.type;
42
+ public isClosing() {
43
+ const { type } = this;
44
+ // check if base is 0 or 1
45
+ // 0^2 == 0 && 1^2 == 1
46
+ return type.mul(type).equals(type);
47
+ }
48
+
49
+ public isNothing() {
50
+ return this.type.equals(ProvableStateTransitionType.nothing.type);
44
51
  }
52
+ }
53
+
54
+ export class MerkleWitnessBatch extends Struct({
55
+ witnesses: Provable.Array(
56
+ RollupMerkleTreeWitness,
57
+ constants.stateTransitionProverBatchSize
58
+ ),
59
+ }) {}
45
60
 
46
- public isProtocol(): Bool {
47
- return this.type.not();
61
+ export class ProvableStateTransitionEntry extends Struct({
62
+ stateTransition: ProvableStateTransition,
63
+ type: ProvableStateTransitionType,
64
+ witnessRoot: Bool,
65
+ }) {
66
+ public static dummy(): ProvableStateTransitionEntry {
67
+ return {
68
+ stateTransition: ProvableStateTransition.dummy(),
69
+ type: ProvableStateTransitionType.nothing,
70
+ witnessRoot: Bool(false),
71
+ };
48
72
  }
49
73
  }
50
74
 
@@ -52,94 +76,59 @@ export class ProvableStateTransitionType extends Struct({
52
76
  * A Batch of StateTransitions to be consumed by the StateTransitionProver
53
77
  * to prove multiple STs at once
54
78
  *
55
- * transitionType:
56
- * true == normal ST, false == protocol ST
79
+ * The batch is formed as an array fo ProvableSTEntries, which have a type and
80
+ * witnessesRoot flag attached to them.
57
81
  */
58
82
  export class StateTransitionProvableBatch extends Struct({
59
83
  batch: Provable.Array(
60
- ProvableStateTransition,
61
- constants.stateTransitionProverBatchSize
62
- ),
63
-
64
- transitionTypes: Provable.Array(
65
- ProvableStateTransitionType,
66
- constants.stateTransitionProverBatchSize
67
- ),
68
-
69
- merkleWitnesses: Provable.Array(
70
- RollupMerkleTreeWitness,
84
+ ProvableStateTransitionEntry,
71
85
  constants.stateTransitionProverBatchSize
72
86
  ),
73
87
  }) {
74
- public static fromMappings(
75
- transitions: {
76
- transition: ProvableStateTransition;
77
- type: ProvableStateTransitionType;
78
- }[],
79
- merkleWitnesses: RollupMerkleTreeWitness[]
80
- ): StateTransitionProvableBatch {
81
- const batch = transitions.map((entry) => entry.transition);
82
- const transitionTypes = transitions.map((entry) => entry.type);
83
- const witnesses = merkleWitnesses.slice();
84
- // Check that order is correct
85
- let normalSTsStarted = false;
86
- transitionTypes.forEach((x) => {
87
- if (!normalSTsStarted && x.isNormal().toBoolean()) {
88
- normalSTsStarted = true;
88
+ public static fromBatches(
89
+ batches: {
90
+ stateTransitions: ProvableStateTransition[];
91
+ applied: Bool;
92
+ witnessRoot: Bool;
93
+ }[]
94
+ ): StateTransitionProvableBatch[] {
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);
89
119
  }
90
- if (normalSTsStarted && x.isProtocol().toBoolean()) {
91
- throw new Error("Order in initializing STBatch not correct");
92
- }
93
- });
94
-
95
- while (batch.length < constants.stateTransitionProverBatchSize) {
96
- batch.push(ProvableStateTransition.dummy());
97
- transitionTypes.push(ProvableStateTransitionType.normal);
98
- witnesses.push(
99
- new RollupMerkleTree(new InMemoryMerkleTreeStorage()).getWitness(
100
- BigInt(0)
101
- )
102
- );
103
120
  }
104
- return new StateTransitionProvableBatch({
105
- batch,
106
- transitionTypes,
107
- merkleWitnesses: witnesses,
108
- });
109
- }
110
121
 
111
- public static fromTransitions(
112
- transitions: ProvableStateTransition[],
113
- protocolTransitions: ProvableStateTransition[],
114
- merkleWitnesses: RollupMerkleTreeWitness[]
115
- ): StateTransitionProvableBatch {
116
- const array = transitions.slice().concat(protocolTransitions);
117
-
118
- const transitionTypes = range(0, transitions.length)
119
- .map(() => ProvableStateTransitionType.normal)
120
- .concat(
121
- range(0, protocolTransitions.length).map(
122
- () => ProvableStateTransitionType.protocol
123
- )
124
- );
125
-
126
- while (array.length < constants.stateTransitionProverBatchSize) {
127
- array.push(ProvableStateTransition.dummy());
128
- transitionTypes.push(ProvableStateTransitionType.normal);
129
- }
122
+ const values = batch(
123
+ flattened,
124
+ constants.stateTransitionProverBatchSize,
125
+ () => ProvableStateTransitionEntry.dummy()
126
+ );
130
127
 
131
- return new StateTransitionProvableBatch({
132
- batch: array,
133
- transitionTypes,
134
- merkleWitnesses,
128
+ return values.map((stBatch) => {
129
+ return new StateTransitionProvableBatch({
130
+ batch: stBatch,
131
+ });
135
132
  });
136
133
  }
137
-
138
- private constructor(object: {
139
- batch: ProvableStateTransition[];
140
- transitionTypes: ProvableStateTransitionType[];
141
- merkleWitnesses: RollupMerkleTreeWitness[];
142
- }) {
143
- super(object);
144
- }
145
134
  }
@@ -73,6 +73,10 @@ export class RuntimeTransaction extends Struct({
73
73
  .assertTrue("Transaction sender is not set to dummy");
74
74
  }
75
75
 
76
+ public isDummy(): Bool {
77
+ return this.methodId.equals(0);
78
+ }
79
+
76
80
  public hashData(): Field[] {
77
81
  return [
78
82
  this.methodId,
@@ -1,7 +1,55 @@
1
- import type { BlockProverState } from "../prover/block/BlockProver";
1
+ import { Field } from "o1js";
2
+
2
3
  import { NetworkState } from "../model/network/NetworkState";
4
+ import { MethodPublicOutput } from "../model/MethodPublicOutput";
5
+ import { BlockProverTransactionArguments } from "../prover/block/BlockProvable";
3
6
 
4
7
  import { TransitioningProtocolModule } from "./TransitioningProtocolModule";
8
+ import {
9
+ AfterTransactionHookArguments,
10
+ BeforeTransactionHookArguments,
11
+ ProvableHookBlockState,
12
+ toProvableHookBlockState,
13
+ } from "./ProvableTransactionHook";
14
+
15
+ export interface BeforeBlockHookArguments extends ProvableHookBlockState {}
16
+
17
+ export interface AfterBlockHookArguments extends BeforeBlockHookArguments {
18
+ stateRoot: Field;
19
+ }
20
+
21
+ export function toBeforeTransactionHookArgument(
22
+ executionData: Omit<
23
+ BlockProverTransactionArguments,
24
+ "verificationKeyAttestation"
25
+ >,
26
+ networkState: NetworkState,
27
+ state: Parameters<typeof toProvableHookBlockState>[0]
28
+ ): BeforeTransactionHookArguments {
29
+ const { transaction, signature } = executionData;
30
+
31
+ return {
32
+ networkState,
33
+ transaction,
34
+ signature,
35
+ prover: toProvableHookBlockState(state),
36
+ };
37
+ }
38
+
39
+ export function toAfterTransactionHookArgument(
40
+ executionData: Omit<
41
+ BlockProverTransactionArguments,
42
+ "verificationKeyAttestation"
43
+ >,
44
+ networkState: NetworkState,
45
+ state: Parameters<typeof toProvableHookBlockState>[0],
46
+ runtimeResult: MethodPublicOutput
47
+ ): AfterTransactionHookArguments {
48
+ return {
49
+ ...toBeforeTransactionHookArgument(executionData, networkState, state),
50
+ runtimeResult,
51
+ };
52
+ }
5
53
 
6
54
  // Purpose is to build transition from -> to network state
7
55
  export abstract class ProvableBlockHook<
@@ -9,11 +57,11 @@ export abstract class ProvableBlockHook<
9
57
  > extends TransitioningProtocolModule<Config> {
10
58
  public abstract beforeBlock(
11
59
  networkState: NetworkState,
12
- state: BlockProverState
60
+ state: BeforeBlockHookArguments
13
61
  ): Promise<NetworkState>;
14
62
 
15
63
  public abstract afterBlock(
16
64
  networkState: NetworkState,
17
- state: BlockProverState
65
+ state: AfterBlockHookArguments
18
66
  ): Promise<NetworkState>;
19
67
  }
@@ -1,13 +1,77 @@
1
1
  import { NoConfig } from "@proto-kit/common";
2
+ import { Signature } from "o1js";
2
3
 
3
- import { BlockProverExecutionData } from "../prover/block/BlockProvable";
4
+ import { RuntimeTransaction } from "../model/transaction/RuntimeTransaction";
5
+ import { NetworkState } from "../model/network/NetworkState";
6
+ import { MethodPublicOutput } from "../model/MethodPublicOutput";
7
+ import type {
8
+ BlockProverState,
9
+ BlockProverStateCommitments,
10
+ } from "../prover/block/BlockProvable";
4
11
 
5
12
  import { TransitioningProtocolModule } from "./TransitioningProtocolModule";
6
13
 
14
+ export type ProvableHookBlockState = Pick<
15
+ BlockProverStateCommitments,
16
+ | "transactionsHash"
17
+ | "eternalTransactionsHash"
18
+ | "incomingMessagesHash"
19
+ | "blockHashRoot"
20
+ >;
21
+
22
+ export function toProvableHookBlockState(
23
+ state: Pick<
24
+ BlockProverState,
25
+ | "transactionList"
26
+ | "eternalTransactionsList"
27
+ | "incomingMessages"
28
+ | "blockHashRoot"
29
+ >
30
+ ) {
31
+ const {
32
+ transactionList,
33
+ eternalTransactionsList,
34
+ incomingMessages,
35
+ blockHashRoot,
36
+ } = state;
37
+ return {
38
+ transactionsHash: transactionList.commitment,
39
+ eternalTransactionsHash: eternalTransactionsList.commitment,
40
+ incomingMessagesHash: incomingMessages.commitment,
41
+ blockHashRoot,
42
+ };
43
+ }
44
+
45
+ /**
46
+ * This type is a reduced set of the runtime method's public output.
47
+ * It omits internal commitments to data that is already present as data in
48
+ * the hook arguments
49
+ */
50
+ export type TransactionResult = Omit<
51
+ MethodPublicOutput,
52
+ "networkStateHash" | "transactionHash"
53
+ >;
54
+
55
+ export interface BeforeTransactionHookArguments {
56
+ transaction: RuntimeTransaction;
57
+ signature: Signature;
58
+ networkState: NetworkState;
59
+ prover: ProvableHookBlockState;
60
+ }
61
+
62
+ export interface AfterTransactionHookArguments
63
+ extends BeforeTransactionHookArguments {
64
+ runtimeResult: TransactionResult;
65
+ }
66
+
7
67
  export abstract class ProvableTransactionHook<
8
68
  Config = NoConfig,
9
69
  > extends TransitioningProtocolModule<Config> {
10
- public abstract onTransaction(
11
- executionData: BlockProverExecutionData
70
+ public abstract beforeTransaction(
71
+ executionData: BeforeTransactionHookArguments
72
+ ): Promise<void>;
73
+
74
+ public abstract afterTransaction(
75
+ execution: AfterTransactionHookArguments
12
76
  ): Promise<void>;
13
77
  }
@@ -0,0 +1,32 @@
1
+ import { Bool, Field } from "o1js";
2
+
3
+ import { DefaultProvableHashList } from "../../utils/ProvableHashList";
4
+ import { NonMethods } from "../../utils/utils";
5
+ import { AppliedStateTransitionBatch } from "../../model/AppliedStateTransitionBatch";
6
+
7
+ /**
8
+ * A HashList for AppliedSTBatches, that in addition to the default
9
+ * functionality, checks that the pushed batches are not empty.
10
+ * If they are, the pushing is skipped.
11
+ * Note that if the batch has applied: false, the batch still has to be appended
12
+ * if it has STs inside
13
+ */
14
+ export class AppliedBatchHashList extends DefaultProvableHashList<
15
+ NonMethods<AppliedStateTransitionBatch>
16
+ > {
17
+ public constructor(commitment: Field = Field(0)) {
18
+ super(AppliedStateTransitionBatch, commitment);
19
+ }
20
+
21
+ private isNotEmpty(value: AppliedStateTransitionBatch): Bool {
22
+ return value.batchHash.equals(Field(0)).not();
23
+ }
24
+
25
+ public push(value: AppliedStateTransitionBatch) {
26
+ return super.pushIf(value, this.isNotEmpty(value));
27
+ }
28
+
29
+ public pushIf(value: AppliedStateTransitionBatch, condition: Bool) {
30
+ return super.pushIf(value, condition.and(this.isNotEmpty(value)));
31
+ }
32
+ }
@@ -1,12 +1,11 @@
1
1
  import { Provable } from "o1js";
2
2
 
3
- import { ProvableOption } from "../model/Option";
3
+ import { ProvableOption } from "../../model/Option";
4
4
  import {
5
5
  ProvableStateTransition,
6
6
  StateTransition,
7
- } from "../model/StateTransition";
8
-
9
- import { ProvableReductionHashList } from "./ProvableReductionHashList";
7
+ } from "../../model/StateTransition";
8
+ import { ProvableReductionHashList } from "../../utils/ProvableReductionHashList";
10
9
 
11
10
  export class StateTransitionReductionList extends ProvableReductionHashList<ProvableStateTransition> {
12
11
  public push(value: ProvableStateTransition) {
@@ -56,6 +55,10 @@ export class StateTransitionReductionList extends ProvableReductionHashList<Prov
56
55
  return [transition, pathsMatch];
57
56
  });
58
57
  }
58
+
59
+ public toField() {
60
+ return this.commitment;
61
+ }
59
62
  }
60
63
 
61
64
  export function reduceStateTransitions(
@@ -0,0 +1,9 @@
1
+ import { Field } from "o1js";
2
+
3
+ import { DefaultProvableHashList } from "../../utils/ProvableHashList";
4
+
5
+ export class TransactionHashList extends DefaultProvableHashList<Field> {
6
+ public constructor(commitment: Field = Field(0)) {
7
+ super(Field, commitment);
8
+ }
9
+ }
@@ -0,0 +1,61 @@
1
+ import { Bool, Field, Struct } from "o1js";
2
+
3
+ import { DefaultProvableHashList } from "../../utils/ProvableHashList";
4
+
5
+ /**
6
+ * Link between a certain applied batch stack and a given root hash that the
7
+ * stack has to evaluate to at that given point in time
8
+ */
9
+ export class WitnessedRoot extends Struct({
10
+ appliedBatchListState: Field,
11
+ root: Field,
12
+ }) {}
13
+
14
+ export class WitnessedRootWitness extends Struct({
15
+ witnessedRoot: Field,
16
+ preimage: Field,
17
+ }) {}
18
+
19
+ /**
20
+ * Accumulator as of section "Intermediary state roots" of the STProver v2 spec
21
+ */
22
+
23
+ export class WitnessedRootHashList extends DefaultProvableHashList<WitnessedRoot> {
24
+ public constructor(commitment: Field = Field(0)) {
25
+ super(WitnessedRoot, commitment);
26
+ }
27
+
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(
41
+ witnessedRoot: WitnessedRoot,
42
+ preimage: Field,
43
+ condition: Bool
44
+ ) {
45
+ // Note, we don't have to validate the preimage here because of the following
46
+ // 1. If the sequencer doesn't provide the correct witness, the BlockProver's
47
+ // equality check will fail
48
+ // 2. If the list is empty, no preimage exists, therefore condition (2) doesn't
49
+ // apply, which is the same outcome when the sequencer provides an arbitrary witness
50
+ const preimageCheckList = new WitnessedRootHashList(preimage).push(
51
+ witnessedRoot
52
+ );
53
+
54
+ // Conditions:
55
+ // (1) don't append if witnessedRoot == finalizedRoot -> Already covered in BlockProver
56
+ // (2) don't append if preimage.push({ finalizedRoot, pendingSTBatchesHash }) == this.commitment
57
+ const skipPush = preimageCheckList.commitment.equals(this.commitment);
58
+
59
+ return this.pushIf(witnessedRoot, condition.and(skipPush.not()));
60
+ }
61
+ }