@proto-kit/protocol 0.1.1-develop.336 → 0.1.1-develop.455

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 (110) hide show
  1. package/dist/blockmodules/AccountStateModule.d.ts +1 -1
  2. package/dist/blockmodules/AccountStateModule.d.ts.map +1 -1
  3. package/dist/blockmodules/BlockHeightHook.d.ts +7 -0
  4. package/dist/blockmodules/BlockHeightHook.d.ts.map +1 -0
  5. package/dist/blockmodules/BlockHeightHook.js +16 -0
  6. package/dist/blockmodules/NoopBlockHook.d.ts +7 -0
  7. package/dist/blockmodules/NoopBlockHook.d.ts.map +1 -0
  8. package/dist/blockmodules/NoopBlockHook.js +9 -0
  9. package/dist/blockmodules/NoopTransactionHook.d.ts +1 -1
  10. package/dist/blockmodules/NoopTransactionHook.d.ts.map +1 -1
  11. package/dist/blockmodules/NoopTransactionHook.js +1 -2
  12. package/dist/hooks/AccountStateHook.d.ts +38 -0
  13. package/dist/hooks/AccountStateHook.d.ts.map +1 -0
  14. package/dist/hooks/AccountStateHook.js +36 -0
  15. package/dist/hooks/BlockHeightHook.d.ts +7 -0
  16. package/dist/hooks/BlockHeightHook.d.ts.map +1 -0
  17. package/dist/hooks/BlockHeightHook.js +14 -0
  18. package/dist/hooks/NoopBlockHook.d.ts +7 -0
  19. package/dist/hooks/NoopBlockHook.d.ts.map +1 -0
  20. package/dist/hooks/NoopBlockHook.js +9 -0
  21. package/dist/hooks/NoopTransactionHook.d.ts +6 -0
  22. package/dist/hooks/NoopTransactionHook.d.ts.map +1 -0
  23. package/dist/hooks/NoopTransactionHook.js +4 -0
  24. package/dist/hooks/TransactionFeeHook.d.ts +16 -0
  25. package/dist/hooks/TransactionFeeHook.d.ts.map +1 -0
  26. package/dist/hooks/TransactionFeeHook.js +39 -0
  27. package/dist/index.d.ts +4 -4
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +4 -4
  30. package/dist/model/Option.d.ts +29 -22
  31. package/dist/model/Option.d.ts.map +1 -1
  32. package/dist/model/Option.js +51 -45
  33. package/dist/model/StateTransition.d.ts +4 -2
  34. package/dist/model/StateTransition.d.ts.map +1 -1
  35. package/dist/model/StateTransition.js +2 -2
  36. package/dist/model/network/NetworkState.d.ts +1 -0
  37. package/dist/model/network/NetworkState.d.ts.map +1 -1
  38. package/dist/model/network/NetworkState.js +7 -0
  39. package/dist/protocol/Protocol.d.ts +4 -18
  40. package/dist/protocol/Protocol.d.ts.map +1 -1
  41. package/dist/protocol/Protocol.js +20 -9
  42. package/dist/protocol/ProtocolModule.d.ts +2 -2
  43. package/dist/protocol/ProtocolModule.d.ts.map +1 -1
  44. package/dist/protocol/ProtocolModule.js +1 -1
  45. package/dist/protocol/ProvableBlockHook.d.ts +16 -0
  46. package/dist/protocol/ProvableBlockHook.d.ts.map +1 -0
  47. package/dist/protocol/ProvableBlockHook.js +4 -0
  48. package/dist/protocol/ProvableTransactionHook.d.ts +2 -1
  49. package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -1
  50. package/dist/protocol/TransitioningProtocolModule.d.ts +4 -0
  51. package/dist/protocol/TransitioningProtocolModule.d.ts.map +1 -1
  52. package/dist/protocol/TransitioningProtocolModule.js +4 -0
  53. package/dist/prover/block/BlockProvable.d.ts +21 -0
  54. package/dist/prover/block/BlockProvable.d.ts.map +1 -1
  55. package/dist/prover/block/BlockProvable.js +3 -1
  56. package/dist/prover/block/BlockProver.d.ts +9 -4
  57. package/dist/prover/block/BlockProver.d.ts.map +1 -1
  58. package/dist/prover/block/BlockProver.js +66 -16
  59. package/dist/prover/block/BlockTransactionPosition.d.ts +36 -0
  60. package/dist/prover/block/BlockTransactionPosition.d.ts.map +1 -0
  61. package/dist/prover/block/BlockTransactionPosition.js +25 -0
  62. package/dist/prover/statetransition/StateTransitionProver.d.ts +1 -1
  63. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
  64. package/dist/prover/statetransition/StateTransitionProver.js +4 -5
  65. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts +3 -3
  66. package/dist/prover/statetransition/StateTransitionWitnessProvider.d.ts.map +1 -1
  67. package/dist/prover/statetransition/StateTransitionWitnessProvider.js +2 -2
  68. package/dist/state/State.d.ts +8 -1
  69. package/dist/state/State.d.ts.map +1 -1
  70. package/dist/state/State.js +11 -4
  71. package/dist/state/StateServiceProvider.d.ts +0 -2
  72. package/dist/state/StateServiceProvider.d.ts.map +1 -1
  73. package/dist/state/StateServiceProvider.js +5 -17
  74. package/package.json +3 -2
  75. package/src/blockmodules/AccountStateModule.ts +2 -2
  76. package/src/blockmodules/BlockHeightHook.ts +22 -0
  77. package/src/blockmodules/NoopBlockHook.ts +16 -0
  78. package/src/blockmodules/NoopTransactionHook.ts +3 -4
  79. package/src/index.ts +4 -4
  80. package/src/model/Option.ts +71 -56
  81. package/src/model/StateTransition.ts +2 -2
  82. package/src/model/network/NetworkState.ts +8 -0
  83. package/src/protocol/Protocol.ts +33 -32
  84. package/src/protocol/ProtocolModule.ts +3 -3
  85. package/src/protocol/ProvableBlockHook.ts +22 -0
  86. package/src/protocol/ProvableTransactionHook.ts +4 -1
  87. package/src/protocol/TransitioningProtocolModule.ts +4 -0
  88. package/src/prover/block/BlockProvable.ts +4 -2
  89. package/src/prover/block/BlockProver.ts +134 -21
  90. package/src/prover/block/BlockTransactionPosition.ts +34 -0
  91. package/src/prover/statetransition/StateTransitionProver.ts +6 -13
  92. package/src/prover/statetransition/StateTransitionWitnessProvider.ts +4 -5
  93. package/src/state/State.ts +11 -4
  94. package/src/state/StateServiceProvider.ts +2 -10
  95. package/test/Option.test.ts +64 -0
  96. package/test/StateTransition.test.ts +1 -1
  97. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts +0 -11
  98. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.d.ts.map +0 -1
  99. package/dist/utils/merkletree/InMemoryMerkleTreeStorage.js +0 -12
  100. package/dist/utils/merkletree/MerkleTreeStore.d.ts +0 -11
  101. package/dist/utils/merkletree/MerkleTreeStore.d.ts.map +0 -1
  102. package/dist/utils/merkletree/MerkleTreeStore.js +0 -1
  103. package/dist/utils/merkletree/RollupMerkleTree.d.ts +0 -132
  104. package/dist/utils/merkletree/RollupMerkleTree.d.ts.map +0 -1
  105. package/dist/utils/merkletree/RollupMerkleTree.js +0 -251
  106. package/src/utils/merkletree/InMemoryMerkleTreeStorage.ts +0 -17
  107. package/src/utils/merkletree/MerkleTreeStore.ts +0 -15
  108. package/src/utils/merkletree/RollupMerkleTree.ts +0 -275
  109. package/src/utils/merkletree/VirtualMerkleTreeStore.ts +0 -21
  110. package/test/utils/MerkleTree.test.ts +0 -82
@@ -22,15 +22,23 @@ import { ProvableTransactionHook } from "./ProvableTransactionHook";
22
22
  import { NoopTransactionHook } from "../blockmodules/NoopTransactionHook";
23
23
  import { ProtocolEnvironment } from "./ProtocolEnvironment";
24
24
  import { AccountStateModule } from "../blockmodules/AccountStateModule";
25
+ import { ProvableBlockHook } from "./ProvableBlockHook";
26
+ import { NoopBlockHook } from "../blockmodules/NoopBlockHook";
27
+ import { BlockHeightHook } from "../blockmodules/BlockHeightHook";
28
+
29
+ const PROTOCOL_INJECTION_TOKENS = {
30
+ ProvableTransactionHook: "ProvableTransactionHook",
31
+ ProvableBlockHook: "ProvableBlockHook",
32
+ };
25
33
 
26
34
  export type GenericProtocolModuleRecord = ModulesRecord<
27
35
  TypedClass<ProtocolModule<unknown>>
28
36
  >;
29
37
 
30
- interface BlockProverType extends ProtocolModule<object>, BlockProvable {}
38
+ interface BlockProverType extends ProtocolModule, BlockProvable {}
31
39
 
32
40
  interface StateTransitionProverType
33
- extends ProtocolModule<object>,
41
+ extends ProtocolModule,
34
42
  StateTransitionProvable {}
35
43
 
36
44
  export interface ProtocolCustomModulesRecord {
@@ -126,67 +134,60 @@ export class Protocol<Modules extends ProtocolModulesRecord>
126
134
  // Register the BlockModules seperately since we need to
127
135
  // inject them differently later
128
136
  let atLeastOneTransactionHookRegistered = false;
137
+ let atLeastOneBlockHookRegistered = false;
129
138
  Object.entries(this.definition.modules).forEach(([key, value]) => {
130
139
  if (Object.prototype.isPrototypeOf.call(ProvableTransactionHook, value)) {
131
140
  this.container.register(
132
- "ProvableTransactionHook",
141
+ PROTOCOL_INJECTION_TOKENS.ProvableTransactionHook,
133
142
  { useToken: key },
134
143
  { lifecycle: Lifecycle.ContainerScoped }
135
144
  );
136
145
  atLeastOneTransactionHookRegistered = true;
137
146
  }
147
+ if (Object.prototype.isPrototypeOf.call(ProvableBlockHook, value)) {
148
+ this.container.register(
149
+ PROTOCOL_INJECTION_TOKENS.ProvableBlockHook,
150
+ { useToken: key },
151
+ { lifecycle: Lifecycle.ContainerScoped }
152
+ );
153
+ atLeastOneBlockHookRegistered = true;
154
+ }
138
155
  });
139
156
 
140
157
  // We need this so that tsyringe doesn't throw when no hooks are registered
141
158
  if (!atLeastOneTransactionHookRegistered) {
142
159
  this.container.register(
143
- "ProvableTransactionHook",
160
+ PROTOCOL_INJECTION_TOKENS.ProvableTransactionHook,
144
161
  { useClass: NoopTransactionHook },
145
162
  { lifecycle: Lifecycle.ContainerScoped }
146
163
  );
147
164
  }
165
+ if (!atLeastOneBlockHookRegistered) {
166
+ this.container.register(
167
+ PROTOCOL_INJECTION_TOKENS.ProvableBlockHook,
168
+ { useClass: NoopBlockHook },
169
+ { lifecycle: Lifecycle.ContainerScoped }
170
+ );
171
+ }
148
172
  }
149
173
  }
150
174
 
151
175
  export const VanillaProtocol = {
152
176
  create() {
153
- return VanillaProtocol.from(
154
- {},
155
- {
156
- BlockProver: {},
157
- StateTransitionProver: {},
158
- AccountState: {},
159
- }
160
- );
177
+ return VanillaProtocol.from({});
161
178
  },
162
179
 
163
180
  from<AdditonalModules extends GenericProtocolModuleRecord>(
164
- additionalModules: AdditonalModules,
165
- config: ModulesConfig<
166
- AdditonalModules & {
167
- StateTransitionProver: typeof StateTransitionProver;
168
- BlockProver: typeof BlockProver;
169
- AccountState: typeof AccountStateModule;
170
- }
171
- >
172
- ): TypedClass<
173
- Protocol<
174
- AdditonalModules & {
175
- StateTransitionProver: typeof StateTransitionProver;
176
- BlockProver: typeof BlockProver;
177
- AccountState: typeof AccountStateModule;
178
- }
179
- >
180
- > {
181
- return Protocol.from({
181
+ additionalModules: AdditonalModules
182
+ ): TypedClass<Protocol<ProtocolModulesRecord>> {
183
+ return Protocol.from<ProtocolModulesRecord>({
182
184
  modules: {
183
185
  StateTransitionProver,
184
186
  BlockProver,
185
187
  AccountState: AccountStateModule,
188
+ BlockHeight: BlockHeightHook,
186
189
  ...additionalModules,
187
190
  },
188
-
189
- config,
190
191
  });
191
192
  },
192
193
  };
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  AreProofsEnabled,
3
3
  ChildContainerProvider,
4
- ConfigurableModule,
5
- noop,
4
+ ConfigurableModule, NoConfig,
5
+ noop
6
6
  } from "@proto-kit/common";
7
7
 
8
8
  import { ProtocolEnvironment } from "./ProtocolEnvironment";
9
9
 
10
10
  export abstract class ProtocolModule<
11
- Config
11
+ Config = NoConfig
12
12
  > extends ConfigurableModule<Config> {
13
13
  public protocol?: ProtocolEnvironment;
14
14
 
@@ -0,0 +1,22 @@
1
+ import type { BlockProverState } from "../prover/block/BlockProver";
2
+ import { NetworkState } from "../model/network/NetworkState";
3
+
4
+ import { TransitioningProtocolModule } from "./TransitioningProtocolModule";
5
+
6
+ export interface BeforeBlockParameters {
7
+ state: BlockProverState;
8
+ networkState: NetworkState;
9
+ }
10
+
11
+ export interface AfterBlockParameters {
12
+ state: BlockProverState;
13
+ networkState: NetworkState;
14
+ }
15
+
16
+ // Purpose is to validate transition from -> to network state
17
+ export abstract class ProvableBlockHook<
18
+ Config
19
+ > extends TransitioningProtocolModule<Config> {
20
+ public abstract beforeBlock(blockData: BeforeBlockParameters): NetworkState;
21
+ public abstract afterBlock(blockData: AfterBlockParameters): NetworkState;
22
+ }
@@ -1,7 +1,10 @@
1
1
  import { BlockProverExecutionData } from "../prover/block/BlockProvable";
2
2
 
3
3
  import { TransitioningProtocolModule } from "./TransitioningProtocolModule";
4
+ import { NoConfig } from "@proto-kit/common";
4
5
 
5
- export abstract class ProvableTransactionHook<Config> extends TransitioningProtocolModule<Config> {
6
+ export abstract class ProvableTransactionHook<
7
+ Config = NoConfig
8
+ > extends TransitioningProtocolModule<Config> {
6
9
  public abstract onTransaction(executionData: BlockProverExecutionData): void;
7
10
  }
@@ -1,5 +1,9 @@
1
1
  import { ProtocolModule } from "./ProtocolModule";
2
2
 
3
+ /**
4
+ * A Protocolmodule that enables it's implementing module to access to
5
+ * StateTransitions and state
6
+ */
3
7
  export abstract class TransitioningProtocolModule<
4
8
  Config
5
9
  > extends ProtocolModule<Config> {
@@ -1,10 +1,11 @@
1
- import { Field, Proof, Struct } from "o1js";
1
+ import { Bool, Field, Proof, Struct } from "o1js";
2
2
  import { WithZkProgrammable } from "@proto-kit/common";
3
3
 
4
4
  import { StateTransitionProof } from "../statetransition/StateTransitionProvable";
5
5
  import { MethodPublicOutput } from "../../model/MethodPublicOutput";
6
6
  import { ProtocolTransaction } from "../../model/transaction/ProtocolTransaction";
7
7
  import { NetworkState } from "../../model/network/NetworkState";
8
+ import { BlockTransactionPosition } from "./BlockTransactionPosition";
8
9
 
9
10
  export class BlockProverPublicInput extends Struct({
10
11
  transactionsHash: Field,
@@ -15,6 +16,7 @@ export class BlockProverPublicInput extends Struct({
15
16
  export class BlockProverPublicOutput extends Struct({
16
17
  transactionsHash: Field,
17
18
  stateRoot: Field,
19
+ networkStateHash: Field,
18
20
  }) {}
19
21
 
20
22
  export type BlockProverProof = Proof<
@@ -25,7 +27,7 @@ export type BlockProverProof = Proof<
25
27
  export class BlockProverExecutionData extends Struct({
26
28
  transaction: ProtocolTransaction,
27
29
  networkState: NetworkState,
28
- // accountstate
30
+ transactionPosition: BlockTransactionPosition,
29
31
  }) {}
30
32
 
31
33
  export interface BlockProvable
@@ -1,5 +1,13 @@
1
1
  /* eslint-disable max-lines */
2
- import { Experimental, Field, type Proof, Provable, SelfProof } from "o1js";
2
+ import {
3
+ Bool,
4
+ Experimental,
5
+ Field,
6
+ type Proof,
7
+ Provable,
8
+ SelfProof,
9
+ Struct,
10
+ } from "o1js";
3
11
  import { container, inject, injectable, injectAll } from "tsyringe";
4
12
  import {
5
13
  AreProofsEnabled,
@@ -29,7 +37,9 @@ import {
29
37
  import { ProvableStateTransition } from "../../model/StateTransition";
30
38
  import { ProvableTransactionHook } from "../../protocol/ProvableTransactionHook";
31
39
  import { RuntimeMethodExecutionContext } from "../../state/context/RuntimeMethodExecutionContext";
32
- import { Protocol, ProtocolModulesRecord } from "../../protocol/Protocol";
40
+ import { ProvableBlockHook } from "../../protocol/ProvableBlockHook";
41
+ import { NetworkState } from "../../model/network/NetworkState";
42
+ import { BlockTransactionPosition } from "./BlockTransactionPosition";
33
43
 
34
44
  const errors = {
35
45
  stateProofNotStartingAtZero: () =>
@@ -75,7 +85,8 @@ export class BlockProverProgrammable extends ZkProgrammable<
75
85
  StateTransitionProverPublicOutput
76
86
  >,
77
87
  public readonly runtime: ZkProgrammable<undefined, MethodPublicOutput>,
78
- private readonly blockModules: ProvableTransactionHook<unknown>[]
88
+ private readonly transactionHooks: ProvableTransactionHook<unknown>[],
89
+ private readonly blockHooks: ProvableBlockHook<unknown>[]
79
90
  ) {
80
91
  super();
81
92
  }
@@ -165,17 +176,6 @@ export class BlockProverProgrammable extends ZkProgrammable<
165
176
  "Network state provided to BlockProver does not match the publicInput"
166
177
  );
167
178
 
168
- // Append tx to transaction list
169
- const transactionList = new DefaultProvableHashList(
170
- Field,
171
- state.transactionsHash
172
- );
173
-
174
- const { transactionHash } = appProof.publicOutput;
175
- transactionList.push(transactionHash);
176
-
177
- stateTo.transactionsHash = transactionList.commitment;
178
-
179
179
  return stateTo;
180
180
  }
181
181
 
@@ -204,7 +204,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
204
204
  });
205
205
  executionContext.beforeMethod("", "", []);
206
206
 
207
- this.blockModules.forEach((module) => {
207
+ this.transactionHooks.forEach((module) => {
208
208
  module.onTransaction(executionData);
209
209
  });
210
210
 
@@ -234,6 +234,83 @@ export class BlockProverProgrammable extends ZkProgrammable<
234
234
  );
235
235
  }
236
236
 
237
+ private getBeforeBlockNetworkState(
238
+ state: BlockProverState,
239
+ networkState: NetworkState
240
+ ) {
241
+ return this.blockHooks.reduce<NetworkState>((networkState, blockHook) => {
242
+ return blockHook.beforeBlock({
243
+ state,
244
+ networkState,
245
+ });
246
+ }, networkState);
247
+ }
248
+
249
+ private getAfterBlockNetworkState(
250
+ state: BlockProverState,
251
+ networkState: NetworkState
252
+ ) {
253
+ return this.blockHooks.reduce<NetworkState>((networkState, blockHook) => {
254
+ return blockHook.afterBlock({
255
+ state,
256
+ networkState,
257
+ });
258
+ }, networkState);
259
+ }
260
+
261
+ private addTransactionToBundle(
262
+ state: BlockProverState,
263
+ stateTransitionProof: StateTransitionProof,
264
+ appProof: Proof<void, MethodPublicOutput>,
265
+ executionData: BlockProverExecutionData
266
+ ): {
267
+ state: BlockProverState;
268
+ networkState: NetworkState;
269
+ bundleOpened: Bool;
270
+ bundleClosed: Bool;
271
+ } {
272
+ const { transactionPosition, networkState } = executionData;
273
+ const stateTo = {
274
+ ...state,
275
+ };
276
+
277
+ // Execute beforeBlook hooks and apply if it is the first tx of the bundle
278
+ const beforeHookResult = this.getBeforeBlockNetworkState(
279
+ state,
280
+ networkState
281
+ );
282
+ const bundleOpened = transactionPosition.equals(
283
+ BlockTransactionPosition.fromPositionType("FIRST")
284
+ );
285
+ const resultingNetworkState = new NetworkState(
286
+ Provable.if(bundleOpened, NetworkState, beforeHookResult, networkState)
287
+ );
288
+ stateTo.networkStateHash = resultingNetworkState.hash();
289
+
290
+ // TODO Modify bundle merkle tree as per specs
291
+
292
+ // Append tx to transaction list
293
+ const transactionList = new DefaultProvableHashList(
294
+ Field,
295
+ state.transactionsHash
296
+ );
297
+
298
+ const { transactionHash } = appProof.publicOutput;
299
+ transactionList.push(transactionHash);
300
+
301
+ stateTo.transactionsHash = transactionList.commitment;
302
+
303
+ return {
304
+ state: stateTo,
305
+ networkState: resultingNetworkState,
306
+ bundleOpened,
307
+
308
+ bundleClosed: transactionPosition.equals(
309
+ BlockTransactionPosition.fromPositionType("LAST")
310
+ ),
311
+ };
312
+ }
313
+
237
314
  @provableMethod()
238
315
  public proveTransaction(
239
316
  publicInput: BlockProverPublicInput,
@@ -247,16 +324,46 @@ export class BlockProverProgrammable extends ZkProgrammable<
247
324
  networkStateHash: publicInput.networkStateHash,
248
325
  };
249
326
 
250
- const stateTo = this.applyTransaction(
327
+ const bundleInclusionResult = this.addTransactionToBundle(
251
328
  state,
252
329
  stateProof,
253
330
  appProof,
254
331
  executionData
255
332
  );
256
333
 
334
+ const stateTo = this.applyTransaction(
335
+ bundleInclusionResult.state,
336
+ stateProof,
337
+ appProof,
338
+ {
339
+ transaction: executionData.transaction,
340
+ transactionPosition: executionData.transactionPosition,
341
+ networkState: bundleInclusionResult.networkState,
342
+ }
343
+ );
344
+
345
+ // Apply afterBlock hooks
346
+ const afterBlockNetworkState = this.getAfterBlockNetworkState(
347
+ stateTo,
348
+ bundleInclusionResult.networkState
349
+ );
350
+ const bundleClosed = executionData.transactionPosition.equals(
351
+ BlockTransactionPosition.fromPositionType("LAST")
352
+ );
353
+
354
+ // We only need the hash here since this computed networkstate
355
+ // is only used as an input in the next bundle
356
+ const resultingNetworkStateHash = Provable.if(
357
+ bundleClosed,
358
+ afterBlockNetworkState.hash(),
359
+ stateTo.networkStateHash
360
+ );
361
+
257
362
  return new BlockProverPublicOutput({
258
363
  stateRoot: stateTo.stateRoot,
259
364
  transactionsHash: stateTo.transactionsHash,
365
+ // eslint-disable-next-line putout/putout
366
+ networkStateHash: resultingNetworkStateHash,
260
367
  });
261
368
  }
262
369
 
@@ -294,7 +401,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
294
401
  proof1.publicInput.networkStateHash,
295
402
  errors.transactionsHashNotMatching("publicInput.from -> proof1.from")
296
403
  );
297
- publicInput.networkStateHash.assertEquals(
404
+ proof1.publicOutput.networkStateHash.assertEquals(
298
405
  proof2.publicInput.networkStateHash,
299
406
  errors.transactionsHashNotMatching("proof1.to -> proof2.from")
300
407
  );
@@ -302,6 +409,7 @@ export class BlockProverProgrammable extends ZkProgrammable<
302
409
  return new BlockProverPublicOutput({
303
410
  stateRoot: proof2.publicOutput.stateRoot,
304
411
  transactionsHash: proof2.publicOutput.transactionsHash,
412
+ networkStateHash: proof2.publicOutput.networkStateHash,
305
413
  });
306
414
  }
307
415
 
@@ -387,7 +495,10 @@ export class BlockProverProgrammable extends ZkProgrammable<
387
495
  * then be merged to be committed to the base-layer contract
388
496
  */
389
497
  @injectable()
390
- export class BlockProver extends ProtocolModule<object> implements BlockProvable {
498
+ export class BlockProver
499
+ extends ProtocolModule
500
+ implements BlockProvable
501
+ {
391
502
  public zkProgrammable: BlockProverProgrammable;
392
503
 
393
504
  public constructor(
@@ -399,15 +510,17 @@ export class BlockProver extends ProtocolModule<object> implements BlockProvable
399
510
  @inject("Runtime")
400
511
  public readonly runtime: WithZkProgrammable<undefined, MethodPublicOutput>,
401
512
  @injectAll("ProvableTransactionHook")
402
- transactionHooks: ProvableTransactionHook<unknown>[]
513
+ transactionHooks: ProvableTransactionHook<unknown>[],
514
+ @injectAll("ProvableBlockHook")
515
+ blockHooks: ProvableBlockHook<unknown>[]
403
516
  ) {
404
517
  super();
405
518
  this.zkProgrammable = new BlockProverProgrammable(
406
519
  this,
407
520
  stateTransitionProver.zkProgrammable,
408
521
  runtime.zkProgrammable,
409
- transactionHooks
410
- // protocol.dependencyContainer.resolveAll("P rovableTransactionHook")
522
+ transactionHooks,
523
+ blockHooks
411
524
  );
412
525
  }
413
526
 
@@ -0,0 +1,34 @@
1
+ import { Bool, Field, Struct } from "o1js";
2
+ import { match } from "ts-pattern";
3
+
4
+ export type BlockTransactionPositionType = "FIRST" | "LAST" | "MIDDLE";
5
+
6
+ export class BlockTransactionPosition extends Struct({
7
+ type: Field,
8
+ }) {
9
+ private static readonly fieldMapping: Record<
10
+ BlockTransactionPositionType,
11
+ number
12
+ > = {
13
+ FIRST: 0,
14
+ MIDDLE: 1,
15
+ LAST: 2,
16
+ };
17
+
18
+ public static fromPositionType(type: BlockTransactionPositionType) {
19
+ return new BlockTransactionPosition({
20
+ type: Field(BlockTransactionPosition.fieldMapping[type]).toConstant(),
21
+ });
22
+ }
23
+
24
+ public static positionTypeFromIndex(index: number, bundleLength: number) {
25
+ return match<number, BlockTransactionPositionType>(index)
26
+ .with(0, () => "FIRST")
27
+ .with(bundleLength - 1, () => "LAST")
28
+ .otherwise(() => "MIDDLE");
29
+ }
30
+
31
+ public equals(type: BlockTransactionPosition): Bool {
32
+ return this.type.equals(type.type);
33
+ }
34
+ }
@@ -6,13 +6,10 @@ import {
6
6
  log,
7
7
  PlainZkProgram,
8
8
  provableMethod,
9
+ RollupMerkleTreeWitness,
9
10
  ZkProgrammable,
10
11
  } from "@proto-kit/common";
11
12
 
12
- import {
13
- MerkleTreeUtils,
14
- RollupMerkleWitness,
15
- } from "../../utils/merkletree/RollupMerkleTree.js";
16
13
  import {
17
14
  DefaultProvableHashList,
18
15
  ProvableHashList,
@@ -25,7 +22,7 @@ import {
25
22
  import { constants } from "../../Constants";
26
23
  import { ProtocolModule } from "../../protocol/ProtocolModule";
27
24
 
28
- import { StateTransitionWitnessProvider } from "./StateTransitionWitnessProvider.js";
25
+ import { StateTransitionWitnessProvider } from "./StateTransitionWitnessProvider";
29
26
  import {
30
27
  StateTransitionProvable,
31
28
  StateTransitionProverPublicInput,
@@ -192,12 +189,11 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
192
189
  type: ProvableStateTransitionType,
193
190
  index = 0
194
191
  ) {
195
- const treeWitness = Provable.witness(RollupMerkleWitness, () =>
192
+ const witness = Provable.witness(RollupMerkleTreeWitness, () =>
196
193
  this.witnessProvider.getWitness(transition.path)
197
194
  );
198
195
 
199
- const membershipValid = MerkleTreeUtils.checkMembership(
200
- treeWitness,
196
+ const membershipValid = witness.checkMembership(
201
197
  state.stateRoot,
202
198
  transition.path,
203
199
  transition.from.value
@@ -212,10 +208,7 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
212
208
  )
213
209
  );
214
210
 
215
- const newRoot = MerkleTreeUtils.computeRoot(
216
- treeWitness,
217
- transition.to.value
218
- );
211
+ const newRoot = witness.calculateRoot(transition.to.value);
219
212
 
220
213
  state.stateRoot = Provable.if(
221
214
  transition.to.isSome,
@@ -345,7 +338,7 @@ export class StateTransitionProverProgrammable extends ZkProgrammable<
345
338
 
346
339
  @injectable()
347
340
  export class StateTransitionProver
348
- extends ProtocolModule<object>
341
+ extends ProtocolModule
349
342
  implements StateTransitionProvable
350
343
  {
351
344
  public readonly zkProgrammable: StateTransitionProverProgrammable;
@@ -1,7 +1,6 @@
1
1
  import type { Field } from "o1js";
2
2
  import { injectable } from "tsyringe";
3
-
4
- import { RollupMerkleWitness } from "../../utils/merkletree/RollupMerkleTree.js";
3
+ import { RollupMerkleTreeWitness } from "@proto-kit/common";
5
4
 
6
5
  /**
7
6
  * Interface for providing merkle witnesses to the state-transition prover
@@ -11,14 +10,14 @@ export interface StateTransitionWitnessProvider {
11
10
  * Provides the merkle witness corresponding to the given key
12
11
  * @param key Merkle-tree key
13
12
  */
14
- getWitness: (key: Field) => RollupMerkleWitness;
13
+ getWitness: (key: Field) => RollupMerkleTreeWitness;
15
14
  }
16
15
 
17
16
  @injectable()
18
17
  export class NoOpStateTransitionWitnessProvider
19
18
  implements StateTransitionWitnessProvider
20
19
  {
21
- public getWitness(): RollupMerkleWitness {
22
- return new RollupMerkleWitness({ path: [], isLeft: [] });
20
+ public getWitness(): RollupMerkleTreeWitness {
21
+ return new RollupMerkleTreeWitness({ path: [], isLeft: [] });
23
22
  }
24
23
  }
@@ -54,6 +54,13 @@ export class State<Value> extends Mixin(WithPath, WithStateServiceProvider) {
54
54
  super();
55
55
  }
56
56
 
57
+ /**
58
+ * Returns the state that is currently the current state tree
59
+ * value: The value-fields, or if not state was found, dummy values
60
+ * isSome: Whether the values where found in the state or not
61
+ * (Basically, whether the value-fields are dummy values or actual values
62
+ * @private
63
+ */
57
64
  private getState(): { value: Value; isSome: Bool } {
58
65
  this.hasStateServiceOrFail();
59
66
  this.hasPathOrFail();
@@ -99,7 +106,7 @@ export class State<Value> extends Mixin(WithPath, WithStateServiceProvider) {
99
106
  *
100
107
  * @returns Optional value of the current state
101
108
  */
102
- private witnessState() {
109
+ private witnessFromState() {
103
110
  // get the value from storage, or return a dummy value instead
104
111
  const value = Provable.witness(this.valueType, () => this.getState().value);
105
112
 
@@ -116,7 +123,7 @@ export class State<Value> extends Mixin(WithPath, WithStateServiceProvider) {
116
123
  * @returns Option representation of the current state.
117
124
  */
118
125
  public get(): Option<Value> {
119
- const option = this.witnessState();
126
+ const option = this.witnessFromState();
120
127
 
121
128
  this.hasPathOrFail();
122
129
 
@@ -142,8 +149,8 @@ export class State<Value> extends Mixin(WithPath, WithStateServiceProvider) {
142
149
  */
143
150
  public set(value: Value) {
144
151
  // link the transition to the current state
145
- const fromOption = this.witnessState();
146
- const toOption = Option.from(Bool(true), value, this.valueType);
152
+ const fromOption = this.witnessFromState();
153
+ const toOption = Option.fromValue(value, this.valueType);
147
154
 
148
155
  this.hasPathOrFail();
149
156
 
@@ -14,14 +14,6 @@ const errors = {
14
14
  export class StateServiceProvider {
15
15
  private readonly stateServiceStack: StateService[] = [];
16
16
 
17
- public constructor(
18
- @inject("StateService") private readonly baseStateService?: StateService
19
- ) {
20
- if (baseStateService !== undefined) {
21
- this.stateServiceStack.push(baseStateService);
22
- }
23
- }
24
-
25
17
  public get stateService(): StateService {
26
18
  if (this.stateServiceStack.length === 0) {
27
19
  throw errors.stateServiceNotSet();
@@ -37,8 +29,8 @@ export class StateServiceProvider {
37
29
  }
38
30
 
39
31
  public popCurrentStateService() {
40
- if (this.stateServiceStack.length === 1) {
41
- log.trace("Trying to pop last available (root) stateservice");
32
+ if (this.stateServiceStack.length === 0) {
33
+ log.trace("Trying to pop from empty state-service stack");
42
34
  return;
43
35
  }
44
36
  this.stateServiceStack.pop();