@proto-kit/protocol 0.1.1-develop.191 → 0.1.1-develop.239

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 (97) hide show
  1. package/dist/blockmodules/AccountStateModule.d.ts +38 -0
  2. package/dist/blockmodules/AccountStateModule.d.ts.map +1 -0
  3. package/dist/blockmodules/AccountStateModule.js +38 -0
  4. package/dist/blockmodules/NoopTransactionHook.d.ts +6 -0
  5. package/dist/blockmodules/NoopTransactionHook.d.ts.map +1 -0
  6. package/dist/blockmodules/NoopTransactionHook.js +5 -0
  7. package/dist/index.d.ts +12 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +12 -0
  10. package/dist/model/Option.d.ts +8 -8
  11. package/dist/model/Option.d.ts.map +1 -1
  12. package/dist/model/Option.js +7 -15
  13. package/dist/model/StateTransition.d.ts.map +1 -1
  14. package/dist/model/StateTransitionProvableBatch.d.ts +59 -1
  15. package/dist/model/StateTransitionProvableBatch.d.ts.map +1 -1
  16. package/dist/model/StateTransitionProvableBatch.js +58 -4
  17. package/dist/protocol/Protocol.d.ts +21 -6
  18. package/dist/protocol/Protocol.d.ts.map +1 -1
  19. package/dist/protocol/Protocol.js +44 -1
  20. package/dist/protocol/ProtocolModule.d.ts +2 -3
  21. package/dist/protocol/ProtocolModule.d.ts.map +1 -1
  22. package/dist/protocol/ProtocolModule.js +1 -3
  23. package/dist/protocol/ProvableTransactionHook.d.ts +6 -0
  24. package/dist/protocol/ProvableTransactionHook.d.ts.map +1 -0
  25. package/dist/protocol/ProvableTransactionHook.js +3 -0
  26. package/dist/protocol/TransitioningProtocolModule.d.ts +5 -0
  27. package/dist/protocol/TransitioningProtocolModule.d.ts.map +1 -0
  28. package/dist/protocol/TransitioningProtocolModule.js +3 -0
  29. package/dist/prover/block/BlockProvable.d.ts +2 -2
  30. package/dist/prover/block/BlockProvable.d.ts.map +1 -1
  31. package/dist/prover/block/BlockProver.d.ts +25 -11
  32. package/dist/prover/block/BlockProver.d.ts.map +1 -1
  33. package/dist/prover/block/BlockProver.js +74 -17
  34. package/dist/prover/statetransition/StateTransitionProvable.d.ts +18 -2
  35. package/dist/prover/statetransition/StateTransitionProvable.d.ts.map +1 -1
  36. package/dist/prover/statetransition/StateTransitionProvable.js +2 -0
  37. package/dist/prover/statetransition/StateTransitionProver.d.ts +16 -6
  38. package/dist/prover/statetransition/StateTransitionProver.d.ts.map +1 -1
  39. package/dist/prover/statetransition/StateTransitionProver.js +45 -13
  40. package/dist/state/State.d.ts +60 -0
  41. package/dist/state/State.d.ts.map +1 -0
  42. package/dist/state/State.js +116 -0
  43. package/dist/state/StateMap.d.ts +37 -0
  44. package/dist/state/StateMap.d.ts.map +1 -0
  45. package/dist/state/StateMap.js +56 -0
  46. package/dist/state/StateService.d.ts +6 -0
  47. package/dist/state/StateService.d.ts.map +1 -0
  48. package/dist/state/StateService.js +1 -0
  49. package/dist/state/StateServiceProvider.d.ts +10 -0
  50. package/dist/state/StateServiceProvider.d.ts.map +1 -0
  51. package/dist/state/StateServiceProvider.js +42 -0
  52. package/dist/state/assert/assert.d.ts +12 -0
  53. package/dist/state/assert/assert.d.ts.map +1 -0
  54. package/dist/state/assert/assert.js +23 -0
  55. package/dist/state/context/ProtocolMethodExecutionContext.d.ts +22 -0
  56. package/dist/state/context/ProtocolMethodExecutionContext.d.ts.map +1 -0
  57. package/dist/state/context/ProtocolMethodExecutionContext.js +28 -0
  58. package/dist/state/context/RuntimeMethodExecutionContext.d.ts +60 -0
  59. package/dist/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -0
  60. package/dist/state/context/RuntimeMethodExecutionContext.js +105 -0
  61. package/dist/state/context/TransitionMethodExecutionContext.d.ts +23 -0
  62. package/dist/state/context/TransitionMethodExecutionContext.d.ts.map +1 -0
  63. package/dist/state/context/TransitionMethodExecutionContext.js +6 -0
  64. package/dist/state/protocol/ProtocolState.d.ts +7 -0
  65. package/dist/state/protocol/ProtocolState.d.ts.map +1 -0
  66. package/dist/state/protocol/ProtocolState.js +42 -0
  67. package/dist/utils/merkletree/MemoryMerkleTreeStorage.d.ts +26 -0
  68. package/dist/utils/merkletree/MemoryMerkleTreeStorage.d.ts.map +1 -0
  69. package/dist/utils/merkletree/MemoryMerkleTreeStorage.js +79 -0
  70. package/package.json +5 -3
  71. package/src/blockmodules/AccountStateModule.ts +31 -0
  72. package/src/blockmodules/NoopTransactionHook.ts +7 -0
  73. package/src/index.ts +12 -0
  74. package/src/model/Option.ts +18 -22
  75. package/src/model/StateTransition.ts +1 -1
  76. package/src/model/StateTransitionProvableBatch.ts +86 -5
  77. package/src/protocol/Protocol.ts +85 -23
  78. package/src/protocol/ProtocolModule.ts +1 -7
  79. package/src/protocol/ProvableTransactionHook.ts +7 -0
  80. package/src/protocol/TransitioningProtocolModule.ts +5 -0
  81. package/src/prover/block/BlockProvable.ts +2 -2
  82. package/src/prover/block/BlockProver.ts +139 -21
  83. package/src/prover/statetransition/StateTransitionProvable.ts +4 -2
  84. package/src/prover/statetransition/StateTransitionProver.ts +93 -13
  85. package/src/state/State.ts +160 -0
  86. package/src/state/StateMap.ts +74 -0
  87. package/src/state/StateService.ts +6 -0
  88. package/src/state/StateServiceProvider.ts +37 -0
  89. package/src/state/assert/assert.test.ts +49 -0
  90. package/src/state/assert/assert.ts +28 -0
  91. package/src/state/context/ProtocolMethodExecutionContext.ts +36 -0
  92. package/src/state/context/RuntimeMethodExecutionContext.ts +124 -0
  93. package/src/state/context/TransitionMethodExecutionContext.ts +27 -0
  94. package/src/state/protocol/ProtocolState.ts +63 -0
  95. package/test/BlockProver.test.ts +139 -36
  96. package/test/Protocol.test.ts +22 -5
  97. package/test/StateTransition.test.ts +1 -1
@@ -0,0 +1,7 @@
1
+ import { TransitioningProtocolModule } from "../../protocol/TransitioningProtocolModule";
2
+ /**
3
+ * Decorates a runtime module property as state, passing down some
4
+ * underlying values to improve developer experience.
5
+ */
6
+ export declare function protocolState(): <TargetTransitioningModule extends TransitioningProtocolModule>(target: TargetTransitioningModule, propertyKey: string) => void;
7
+ //# sourceMappingURL=ProtocolState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProtocolState.d.ts","sourceRoot":"","sources":["../../../src/state/protocol/ProtocolState.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,4CAA4C,CAAC;AAgBzF;;;GAGG;AACH,wBAAgB,aAAa,oHAGZ,MAAM,UAoCtB"}
@@ -0,0 +1,42 @@
1
+ import { Path } from "../../model/Path";
2
+ const errors = {
3
+ missingName: (className) => new Error(`Unable to provide a unique identifier for state, ${className} is missing a name.
4
+ Did you forget to extend your block module with 'extends ...Hook'?`),
5
+ missingProtocol: (className) => new Error(`Unable to provide 'procotol' for state, ${className} is missing a name.
6
+ Did you forget to extend your block module with 'extends ...Hook'?`),
7
+ };
8
+ /**
9
+ * Decorates a runtime module property as state, passing down some
10
+ * underlying values to improve developer experience.
11
+ */
12
+ export function protocolState() {
13
+ return (target, propertyKey) => {
14
+ // eslint-disable-next-line @typescript-eslint/init-declarations
15
+ let value;
16
+ Object.defineProperty(target, propertyKey, {
17
+ enumerable: true,
18
+ get: function get() {
19
+ // eslint-disable-next-line max-len
20
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
21
+ const self = this;
22
+ if (self.name === undefined) {
23
+ throw errors.missingName(self.constructor.name);
24
+ }
25
+ if (!self.protocol) {
26
+ throw errors.missingProtocol(self.constructor.name);
27
+ }
28
+ // eslint-disable-next-line no-warning-comments
29
+ // TODO Add Prefix?
30
+ const path = Path.fromProperty(self.name, propertyKey);
31
+ if (value) {
32
+ value.path = path;
33
+ value.stateServiceProvider = self.protocol.stateServiceProvider;
34
+ }
35
+ return value;
36
+ },
37
+ set: (newValue) => {
38
+ value = newValue;
39
+ },
40
+ });
41
+ };
42
+ }
@@ -0,0 +1,26 @@
1
+ import { type MerkleTreeStore, type SyncMerkleTreeStore } from "./RollupMerkleTree.js";
2
+ export declare class NoOpMerkleTreeStorage implements SyncMerkleTreeStore {
3
+ parent: MerkleTreeStore;
4
+ openTransaction(): void;
5
+ commit(): void;
6
+ getNode(): bigint | undefined;
7
+ setNode(): void;
8
+ virtualize(): MerkleTreeStore;
9
+ getNodeAsync(): Promise<bigint | undefined>;
10
+ setNodeAsync(): Promise<void>;
11
+ }
12
+ export declare class MemoryMerkleTreeStorage implements SyncMerkleTreeStore {
13
+ private readonly nodes;
14
+ private readonly cache;
15
+ parent: SyncMerkleTreeStore;
16
+ constructor(parent?: SyncMerkleTreeStore | undefined);
17
+ openTransaction(): void;
18
+ commit(): void;
19
+ getNode(key: bigint, level: number): bigint | undefined;
20
+ setNode(key: bigint, level: number, value: bigint): void;
21
+ virtualize(): MerkleTreeStore;
22
+ getNodeAsync(key: bigint, level: number): Promise<bigint | undefined>;
23
+ setNodeAsync(key: bigint, level: number, value: bigint): Promise<void>;
24
+ cacheFromParent(index: bigint): Promise<void>;
25
+ }
26
+ //# sourceMappingURL=MemoryMerkleTreeStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MemoryMerkleTreeStorage.d.ts","sourceRoot":"","sources":["../../../src/utils/merkletree/MemoryMerkleTreeStorage.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,eAAe,EAEpB,KAAK,mBAAmB,EACzB,MAAM,uBAAuB,CAAC;AAE/B,qBAAa,qBAAsB,YAAW,mBAAmB;IACxD,MAAM,EAAE,eAAe,CAAQ;IAE/B,eAAe,IAAI,IAAI;IAIvB,MAAM,IAAI,IAAI;IAId,OAAO,IAAI,MAAM,GAAG,SAAS;IAI7B,OAAO,IAAI,IAAI;IAIf,UAAU,IAAI,eAAe;IAIvB,YAAY,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAI3C,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAG3C;AAED,qBAAa,uBAAwB,YAAW,mBAAmB;IACjE,OAAO,CAAC,QAAQ,CAAC,KAAK,CAGf;IAEP,OAAO,CAAC,QAAQ,CAAC,KAAK,CAGf;IAEA,MAAM,EAAE,mBAAmB,CAAC;gBAEhB,MAAM,GAAE,mBAAmB,GAAG,SAAqB;IAI/D,eAAe,IAAI,IAAI;IAIvB,MAAM,IAAI,IAAI;IAId,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAQvD,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAIxD,UAAU,IAAI,eAAe;IAIvB,YAAY,CACvB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAIjB,YAAY,CACvB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC;IAKH,eAAe,CAAC,KAAK,EAAE,MAAM;CAoB3C"}
@@ -0,0 +1,79 @@
1
+ /* eslint-disable @typescript-eslint/no-magic-numbers */
2
+ import noop from "lodash/noop";
3
+ import { RollupMerkleTree, } from "./RollupMerkleTree.js";
4
+ export class NoOpMerkleTreeStorage {
5
+ constructor() {
6
+ this.parent = this;
7
+ }
8
+ openTransaction() {
9
+ noop();
10
+ }
11
+ commit() {
12
+ noop();
13
+ }
14
+ getNode() {
15
+ return undefined;
16
+ }
17
+ setNode() {
18
+ noop();
19
+ }
20
+ virtualize() {
21
+ return this;
22
+ }
23
+ async getNodeAsync() {
24
+ return undefined;
25
+ }
26
+ async setNodeAsync() {
27
+ return undefined;
28
+ }
29
+ }
30
+ export class MemoryMerkleTreeStorage {
31
+ constructor(parent = undefined) {
32
+ this.nodes = {};
33
+ this.cache = {};
34
+ this.parent = parent ?? new NoOpMerkleTreeStorage();
35
+ }
36
+ openTransaction() {
37
+ noop();
38
+ }
39
+ commit() {
40
+ noop();
41
+ }
42
+ getNode(key, level) {
43
+ return (this.nodes[level]?.[key.toString()] ??
44
+ this.cache[level]?.[key.toString()] ??
45
+ this.parent.getNode(key, level));
46
+ }
47
+ setNode(key, level, value) {
48
+ var _a;
49
+ ((_a = this.nodes)[level] ?? (_a[level] = {}))[key.toString()] = value;
50
+ }
51
+ virtualize() {
52
+ return new MemoryMerkleTreeStorage(this);
53
+ }
54
+ async getNodeAsync(key, level) {
55
+ return this.getNode(key, level);
56
+ }
57
+ async setNodeAsync(key, level, value) {
58
+ this.setNode(key, level, value);
59
+ return undefined;
60
+ }
61
+ async cacheFromParent(index) {
62
+ var _a;
63
+ // Algo from RollupMerkleTree.getWitness()
64
+ const { leafCount, height } = RollupMerkleTree;
65
+ if (index >= leafCount) {
66
+ index %= leafCount;
67
+ }
68
+ for (let level = 0; level < height - 1; level++) {
69
+ const isLeft = index % 2n === 0n;
70
+ const key = isLeft ? index + 1n : index - 1n;
71
+ // eslint-disable-next-line no-await-in-loop
72
+ const value = await this.parent.getNodeAsync(key, level);
73
+ if (value !== undefined) {
74
+ ((_a = this.nodes)[level] ?? (_a[level] = {}))[key.toString()] = value;
75
+ }
76
+ index /= 2n;
77
+ }
78
+ }
79
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "license": "MIT",
4
4
  "private": false,
5
5
  "type": "module",
6
- "version": "0.1.1-develop.191+554ba10",
6
+ "version": "0.1.1-develop.239+0bf6910",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
9
9
  "dev": "tsc -p tsconfig.json --watch",
@@ -20,7 +20,9 @@
20
20
  "@proto-kit/common": "*",
21
21
  "lodash": "^4.17.21",
22
22
  "loglevel": "^1.8.1",
23
- "reflect-metadata": "^0.1.13"
23
+ "reflect-metadata": "^0.1.13",
24
+ "ts-mixer": "^6.0.3",
25
+ "tsyringe": "^4.7.0"
24
26
  },
25
27
  "peerDependencies": {
26
28
  "snarkyjs": "0.12.0",
@@ -30,5 +32,5 @@
30
32
  "@jest/globals": "^29.5.0",
31
33
  "@types/lodash": "^4.14.194"
32
34
  },
33
- "gitHead": "554ba102c48f41a62409ff01354582567ef4124a"
35
+ "gitHead": "0bf6910a100b9ae24fb00a253c9f58b5fd73ca55"
34
36
  }
@@ -0,0 +1,31 @@
1
+ import { Provable, PublicKey, Struct, UInt64 } from "snarkyjs";
2
+
3
+ import { BlockProverExecutionData } from "../prover/block/BlockProvable";
4
+ import { StateMap } from "../state/StateMap";
5
+ import { protocolState } from "../state/protocol/ProtocolState";
6
+ import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
7
+ import { assert } from "../state/assert/assert";
8
+
9
+ export class AccountState extends Struct({
10
+ nonce: UInt64,
11
+ }) {}
12
+
13
+ export class AccountStateModule extends ProvableTransactionHook {
14
+ @protocolState() public accountState = StateMap.from<PublicKey, AccountState>(PublicKey, AccountState);
15
+
16
+ public onTransaction({ transaction }: BlockProverExecutionData): void {
17
+ const accountState = this.accountState
18
+ .get(transaction.sender)
19
+ .orElse(new AccountState({ nonce: UInt64.zero }));
20
+
21
+ const currentNonce = accountState.nonce;
22
+ Provable.log("Current Nonce", currentNonce);
23
+ Provable.log("Tx Nonce", transaction.nonce);
24
+ assert(currentNonce.equals(transaction.nonce), "Nonce not matching");
25
+
26
+ this.accountState.set(
27
+ transaction.sender,
28
+ new AccountState({ nonce: currentNonce.add(1) })
29
+ );
30
+ }
31
+ }
@@ -0,0 +1,7 @@
1
+ import { ProvableTransactionHook } from "../protocol/ProvableTransactionHook";
2
+ import { BlockProverExecutionData } from "../prover/block/BlockProvable";
3
+
4
+ export class NoopTransactionHook extends ProvableTransactionHook {
5
+ public onTransaction(executionData: BlockProverExecutionData): void {
6
+ }
7
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./blockmodules/AccountStateModule";
1
2
  export * from "./utils/ProvableHashList";
2
3
  export * from "./model/StateTransition";
3
4
  export * from "./model/StateTransitionProvableBatch";
@@ -20,4 +21,15 @@ export * from "./model/MethodPublicOutput";
20
21
  export * from "./prover/statetransition/StateTransitionWitnessProviderReference";
21
22
  export * from "./protocol/Protocol";
22
23
  export * from "./protocol/ProtocolModule";
24
+ export * from "./protocol/TransitioningProtocolModule";
25
+ export * from "./protocol/ProvableTransactionHook";
26
+ export * from "./state/context/ProtocolMethodExecutionContext";
27
+ export * from "./state/context/TransitionMethodExecutionContext";
28
+ export * from "./state/context/RuntimeMethodExecutionContext";
29
+ export * from "./state/protocol/ProtocolState";
30
+ export * from "./state/State";
31
+ export * from "./state/StateMap";
32
+ export * from "./state/StateService";
33
+ export * from "./state/StateServiceProvider";
34
+ export * from "./state/assert/assert";
23
35
  export { constants as ProtocolConstants } from "./Constants";
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  Bool,
3
3
  Field,
4
+ FlexibleProvable,
4
5
  type FlexibleProvablePure,
5
6
  Poseidon,
6
7
  Provable,
@@ -17,6 +18,10 @@ export class ProvableOption extends Struct({
17
18
  }
18
19
  }
19
20
 
21
+ export interface ToFieldable {
22
+ toFields: () => Field[];
23
+ }
24
+
20
25
  /**
21
26
  * Option facilitating in-circuit values that may or may not exist.
22
27
  */
@@ -58,28 +63,6 @@ export class Option<Value> {
58
63
  return new Option(Bool(false), Field(0), Field);
59
64
  }
60
65
 
61
- public static dummyValueFields<Value>(
62
- valueType: FlexibleProvablePure<Value>
63
- ): Field[] {
64
- const length = valueType.sizeInFields();
65
- return Array.from({ length }, () => Field(0));
66
- }
67
-
68
- /**
69
- * Computes a dummy value for the given value type.
70
- *
71
- * @param valueType - Value type to generate the dummy value for
72
- * @returns Dummy value for the given value type
73
- */
74
- public static dummyValue<Value>(
75
- valueType: FlexibleProvablePure<Value>
76
- ): Value {
77
- const fields = Option.dummyValueFields(valueType);
78
-
79
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
80
- return valueType.fromFields(fields) as Value;
81
- }
82
-
83
66
  public isForcedSome = Bool(false);
84
67
 
85
68
  public constructor(
@@ -131,6 +114,19 @@ export class Option<Value> {
131
114
  });
132
115
  }
133
116
 
117
+ /**
118
+ * @returns Returns the value of this option if it isSome,
119
+ * otherwise returns the given defaultValue
120
+ */
121
+ public orElse(defaultValue: Value): Value {
122
+ return Provable.if<Value>(
123
+ this.isSome,
124
+ this.valueType,
125
+ this.value,
126
+ defaultValue
127
+ );
128
+ }
129
+
134
130
  public toJSON() {
135
131
  const valueContent = this.valueType
136
132
  .toFields(this.value)
@@ -1,6 +1,6 @@
1
1
  import { Field, Struct } from "snarkyjs";
2
2
 
3
- import { Option, ProvableOption } from "./Option.js";
3
+ import { Option, ProvableOption, ToFieldable } from "./Option.js";
4
4
 
5
5
  /**
6
6
  * Provable representation of a State Transition, used to
@@ -1,32 +1,113 @@
1
- import { Provable, Struct } from "snarkyjs";
1
+ import { Bool, Provable, Struct } from "snarkyjs";
2
2
 
3
3
  import { constants } from "../Constants";
4
4
 
5
5
  import { ProvableStateTransition } from "./StateTransition.js";
6
+ import { range } from "@proto-kit/common";
7
+
8
+ export class StateTransitionType {
9
+ public static readonly normal = true;
10
+ public static readonly protocol = false;
11
+
12
+ public static isNormal(type: boolean) {
13
+ return type === StateTransitionType.normal;
14
+ }
15
+
16
+ public static isProtocol(type: boolean) {
17
+ return type === StateTransitionType.protocol;
18
+ }
19
+ }
20
+
21
+ export class ProvableStateTransitionType extends Struct({
22
+ type: Bool,
23
+ }) {
24
+ public static get normal(): ProvableStateTransitionType {
25
+ return new ProvableStateTransitionType({
26
+ type: Bool(StateTransitionType.normal),
27
+ });
28
+ }
29
+
30
+ public static get protocol(): ProvableStateTransitionType {
31
+ return new ProvableStateTransitionType({
32
+ type: Bool(StateTransitionType.protocol),
33
+ });
34
+ }
35
+
36
+ public isNormal(): Bool {
37
+ return this.type;
38
+ }
39
+
40
+ public isProtocol(): Bool {
41
+ return this.type.not();
42
+ }
43
+ }
6
44
 
7
45
  /**
8
46
  * A Batch of StateTransitions to be consumed by the StateTransitionProver
9
47
  * to prove multiple STs at once
48
+ *
49
+ * transitionType:
50
+ * true == normal ST, false == protocol ST
10
51
  */
11
52
  export class StateTransitionProvableBatch extends Struct({
12
53
  batch: Provable.Array(
13
54
  ProvableStateTransition,
14
55
  constants.stateTransitionProverBatchSize
15
56
  ),
57
+
58
+ transitionTypes: Provable.Array(
59
+ ProvableStateTransitionType,
60
+ constants.stateTransitionProverBatchSize
61
+ ),
16
62
  }) {
63
+ public static fromMappings(
64
+ transitions: {
65
+ transition: ProvableStateTransition;
66
+ type: ProvableStateTransitionType;
67
+ }[]
68
+ ): StateTransitionProvableBatch {
69
+ const batch = transitions.map((entry) => entry.transition);
70
+ const transitionTypes = transitions.map((entry) => entry.type);
71
+
72
+ while (batch.length < constants.stateTransitionProverBatchSize) {
73
+ batch.push(ProvableStateTransition.dummy());
74
+ transitionTypes.push(ProvableStateTransitionType.normal);
75
+ }
76
+ return new StateTransitionProvableBatch({
77
+ batch,
78
+ transitionTypes,
79
+ });
80
+ }
81
+
17
82
  public static fromTransitions(
18
- transitions: ProvableStateTransition[]
83
+ transitions: ProvableStateTransition[],
84
+ protocolTransitions: ProvableStateTransition[]
19
85
  ): StateTransitionProvableBatch {
20
- const array = transitions.slice();
86
+ const array = transitions.slice().concat(protocolTransitions);
87
+
88
+ const transitionTypes = range(0, transitions.length)
89
+ .map(() => ProvableStateTransitionType.normal)
90
+ .concat(
91
+ range(0, protocolTransitions.length).map(
92
+ () => ProvableStateTransitionType.protocol
93
+ )
94
+ );
21
95
 
22
96
  while (array.length < constants.stateTransitionProverBatchSize) {
23
97
  array.push(ProvableStateTransition.dummy());
98
+ transitionTypes.push(ProvableStateTransitionType.normal);
24
99
  }
25
100
 
26
- return new StateTransitionProvableBatch({ batch: array });
101
+ return new StateTransitionProvableBatch({
102
+ batch: array,
103
+ transitionTypes,
104
+ });
27
105
  }
28
106
 
29
- private constructor(object: { batch: ProvableStateTransition[] }) {
107
+ private constructor(object: {
108
+ batch: ProvableStateTransition[];
109
+ transitionTypes: ProvableStateTransitionType[];
110
+ }) {
30
111
  super(object);
31
112
  }
32
113
  }
@@ -6,36 +6,27 @@ import {
6
6
  StringKeyOf,
7
7
  TypedClass,
8
8
  } from "@proto-kit/common";
9
- import { DependencyContainer } from "tsyringe";
9
+ import { DependencyContainer, Lifecycle } from "tsyringe";
10
10
 
11
- import {
12
- BlockProvable,
13
- BlockProverPublicInput,
14
- BlockProverPublicOutput,
15
- } from "../prover/block/BlockProvable";
11
+ import { BlockProvable } from "../prover/block/BlockProvable";
16
12
  import { StateTransitionProver } from "../prover/statetransition/StateTransitionProver";
17
- import {
18
- StateTransitionProvable,
19
- StateTransitionProverPublicInput,
20
- StateTransitionProverPublicOutput,
21
- } from "../prover/statetransition/StateTransitionProvable";
13
+ import { StateTransitionProvable } from "../prover/statetransition/StateTransitionProvable";
22
14
  import { BlockProver } from "../prover/block/BlockProver";
15
+ import { StateServiceProvider } from "../state/StateServiceProvider";
16
+ import { StateService } from "../state/StateService";
23
17
 
24
18
  import { ProtocolModule } from "./ProtocolModule";
19
+ import { ProvableTransactionHook } from "./ProvableTransactionHook";
20
+ import { NoopTransactionHook } from "../blockmodules/NoopTransactionHook";
25
21
 
26
22
  export type GenericProtocolModuleRecord = ModulesRecord<
27
- TypedClass<ProtocolModule<any, any>>
23
+ TypedClass<ProtocolModule>
28
24
  >;
29
25
 
30
- interface BlockProverType
31
- extends ProtocolModule<BlockProverPublicInput, BlockProverPublicOutput>,
32
- BlockProvable {}
26
+ interface BlockProverType extends ProtocolModule, BlockProvable {}
33
27
 
34
28
  interface StateTransitionProverType
35
- extends ProtocolModule<
36
- StateTransitionProverPublicInput,
37
- StateTransitionProverPublicOutput
38
- >,
29
+ extends ProtocolModule,
39
30
  StateTransitionProvable {}
40
31
 
41
32
  export interface ProtocolCustomModulesRecord {
@@ -49,6 +40,11 @@ export interface ProtocolModulesRecord
49
40
 
50
41
  export interface ProtocolDefinition<Modules extends ProtocolModulesRecord> {
51
42
  modules: Modules;
43
+
44
+ /**
45
+ * @deprecated
46
+ */
47
+ state?: StateService;
52
48
  // config: ModulesConfig<Modules>
53
49
  }
54
50
 
@@ -79,6 +75,52 @@ export class Protocol<
79
75
  return protocol;
80
76
  }
81
77
 
78
+ public definition: ProtocolDefinition<Modules>;
79
+
80
+ private readonly stateServiceProviderInstance = new StateServiceProvider(
81
+ // eslint-disable-next-line etc/no-deprecated
82
+ this.definition.state
83
+ );
84
+
85
+ public constructor(definition: ProtocolDefinition<Modules>) {
86
+ super(definition);
87
+ this.definition = definition;
88
+
89
+ // Register the BlockModules seperately since we need to
90
+ // inject them differently later
91
+ let atLeastOneTransactionHookRegistered = false;
92
+ Object.entries(definition.modules).forEach(([key, value]) => {
93
+ if (Object.prototype.isPrototypeOf.call(ProvableTransactionHook, value)) {
94
+ this.container.register(
95
+ "ProvableTransactionHook",
96
+ { useToken: key },
97
+ { lifecycle: Lifecycle.ContainerScoped }
98
+ );
99
+ atLeastOneTransactionHookRegistered = true;
100
+ }
101
+ });
102
+
103
+ // We need this so that tsyringe doesn't throw when no hooks are registered
104
+ if (!atLeastOneTransactionHookRegistered) {
105
+ this.container.register(
106
+ "ProvableTransactionHook",
107
+ { useClass: NoopTransactionHook },
108
+ { lifecycle: Lifecycle.ContainerScoped }
109
+ );
110
+ }
111
+ // this.container.afterResolution<ProvableTransactionHook>("ProvableTransactionHook", (token, result) => {
112
+ // if ()
113
+ // })
114
+ }
115
+
116
+ public get stateService(): StateService {
117
+ return this.stateServiceProviderInstance.stateService;
118
+ }
119
+
120
+ public get stateServiceProvider(): StateServiceProvider {
121
+ return this.stateServiceProviderInstance;
122
+ }
123
+
82
124
  public decorateModule(
83
125
  moduleName: StringKeyOf<Modules>,
84
126
  containedModule: InstanceType<Modules[StringKeyOf<Modules>]>
@@ -86,6 +128,15 @@ export class Protocol<
86
128
  log.debug(`Decorated ${moduleName}`);
87
129
  containedModule.protocol = this;
88
130
 
131
+ log.debug(
132
+ "Is instanceof:",
133
+ containedModule instanceof ProvableTransactionHook
134
+ );
135
+ if (containedModule instanceof ProvableTransactionHook) {
136
+ console.log(`Setting name to ${moduleName}`);
137
+ containedModule.name = moduleName;
138
+ }
139
+
89
140
  super.decorateModule(moduleName, containedModule);
90
141
  }
91
142
 
@@ -115,15 +166,26 @@ export class Protocol<
115
166
  }
116
167
 
117
168
  export const VanillaProtocol = {
118
- create(): Protocol<{
119
- StateTransitionProver: typeof StateTransitionProver;
120
- BlockProver: typeof BlockProver;
121
- }> {
169
+ create(stateService?: StateService) {
170
+ return VanillaProtocol.from({}, stateService);
171
+ },
172
+
173
+ from<AdditonalModules extends GenericProtocolModuleRecord>(
174
+ additionalModules: AdditonalModules,
175
+ stateService?: StateService
176
+ ): Protocol<
177
+ AdditonalModules & {
178
+ StateTransitionProver: typeof StateTransitionProver;
179
+ BlockProver: typeof BlockProver;
180
+ }
181
+ > {
122
182
  return Protocol.from({
123
183
  modules: {
124
184
  StateTransitionProver,
125
185
  BlockProver,
186
+ ...additionalModules,
126
187
  },
188
+ state: stateService,
127
189
  });
128
190
  },
129
191
  };
@@ -1,23 +1,17 @@
1
1
  import {
2
2
  AreProofsEnabled,
3
3
  Configurable,
4
- ZkProgrammable,
5
4
  } from "@proto-kit/common";
6
5
 
7
6
  import type { Protocol, ProtocolModulesRecord } from "./Protocol";
8
7
 
9
- export abstract class ProtocolModule<PublicInput, PublicOutput>
10
- extends ZkProgrammable<PublicInput, PublicOutput>
8
+ export abstract class ProtocolModule
11
9
  implements Configurable<unknown>
12
10
  {
13
11
  public config = {};
14
12
 
15
13
  public protocol?: Protocol<ProtocolModulesRecord>;
16
14
 
17
- public constructor() {
18
- super();
19
- }
20
-
21
15
  public get appChain(): AreProofsEnabled | undefined {
22
16
  return this.protocol?.dependencyContainer.resolve<AreProofsEnabled>(
23
17
  "AppChain"
@@ -0,0 +1,7 @@
1
+ import { BlockProverExecutionData } from "../prover/block/BlockProvable";
2
+
3
+ import { TransitioningProtocolModule } from "./TransitioningProtocolModule";
4
+
5
+ export abstract class ProvableTransactionHook extends TransitioningProtocolModule {
6
+ public abstract onTransaction(executionData: BlockProverExecutionData): void;
7
+ }
@@ -0,0 +1,5 @@
1
+ import { ProtocolModule } from "./ProtocolModule";
2
+
3
+ export abstract class TransitioningProtocolModule extends ProtocolModule {
4
+ public name?: string;
5
+ }
@@ -1,5 +1,5 @@
1
1
  import { Field, Proof, Struct } from "snarkyjs";
2
- import { ZkProgrammable } from "@proto-kit/common";
2
+ import { WithZkProgrammable } from "@proto-kit/common";
3
3
 
4
4
  import { StateTransitionProof } from "../statetransition/StateTransitionProvable";
5
5
  import { MethodPublicOutput } from "../../model/MethodPublicOutput";
@@ -29,7 +29,7 @@ export class BlockProverExecutionData extends Struct({
29
29
  }) {}
30
30
 
31
31
  export interface BlockProvable
32
- extends ZkProgrammable<BlockProverPublicInput, BlockProverPublicOutput> {
32
+ extends WithZkProgrammable<BlockProverPublicInput, BlockProverPublicOutput> {
33
33
  proveTransaction: (
34
34
  publicInput: BlockProverPublicInput,
35
35
  stateProof: StateTransitionProof,