@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,60 @@
1
+ import { Field, type FlexibleProvablePure } from "snarkyjs";
2
+ import { Path } from "../model/Path";
3
+ import { Option } from "../model/Option";
4
+ import { StateServiceProvider } from "./StateServiceProvider";
5
+ export declare class WithPath {
6
+ path?: Field;
7
+ hasPathOrFail(): asserts this is {
8
+ path: Path;
9
+ };
10
+ }
11
+ export declare class WithStateServiceProvider {
12
+ stateServiceProvider?: StateServiceProvider;
13
+ hasStateServiceOrFail(): asserts this is {
14
+ stateServiceProvider: StateServiceProvider;
15
+ };
16
+ }
17
+ declare const State_base: import("ts-mixer/dist/types/types").Class<any[], WithPath & WithStateServiceProvider, typeof WithPath & typeof WithStateServiceProvider, false>;
18
+ /**
19
+ * Utilities for runtime module state, such as get/set
20
+ */
21
+ export declare class State<Value> extends State_base {
22
+ valueType: FlexibleProvablePure<Value>;
23
+ /**
24
+ * Creates a new state wrapper for the provided value type.
25
+ *
26
+ * @param valueType - Type of value to be stored (e.g. UInt64, Struct, ...)
27
+ * @returns New state for the given value type.
28
+ */
29
+ static from<Value>(valueType: FlexibleProvablePure<Value>): State<Value>;
30
+ constructor(valueType: FlexibleProvablePure<Value>);
31
+ private getState;
32
+ /**
33
+ * Provides an in-circuit witness for the current state representation,
34
+ * and constructs an Option out of it.
35
+ *
36
+ * @returns Optional value of the current state
37
+ */
38
+ private witnessState;
39
+ /**
40
+ * Retrieves the current state and creates a state transition
41
+ * anchoring the use of the current state value in the circuit.
42
+ *
43
+ * @returns Option representation of the current state.
44
+ */
45
+ get(): Option<Value>;
46
+ /**
47
+ * Sets a new state value by creating a state transition from
48
+ * the current value to the newly set value.
49
+ *
50
+ * The newly set value isn't available via state.get(), since the
51
+ * state transitions are not applied within the same circuit.
52
+ * You can however store and access your new value in
53
+ * a separate circuit variable.
54
+ *
55
+ * @param value - Value to be set as the current state
56
+ */
57
+ set(value: Value): void;
58
+ }
59
+ export {};
60
+ //# sourceMappingURL=State.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"State.d.ts","sourceRoot":"","sources":["../../src/state/State.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,KAAK,EAAY,KAAK,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAI5E,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAGzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAG9D,qBAAa,QAAQ;IACZ,IAAI,CAAC,EAAE,KAAK,CAAC;IAEb,aAAa,IAAI,OAAO,CAAC,IAAI,IAAI;QAAE,IAAI,EAAE,IAAI,CAAA;KAAE;CAOvD;AAED,qBAAa,wBAAwB;IAC5B,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;IAE5C,qBAAqB,IAAI,OAAO,CAAC,IAAI,IAAI;QAC9C,oBAAoB,EAAE,oBAAoB,CAAC;KAC5C;CAOF;;AAED;;GAEG;AACH,qBAAa,KAAK,CAAC,KAAK,CAAE,SAAQ,UAAyC;IAW/C,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAVhE;;;;;OAKG;WACW,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;gBAItC,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAIhE,OAAO,CAAC,QAAQ;IAuChB;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IAUpB;;;;;OAKG;IACI,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC;IAc3B;;;;;;;;;;OAUG;IACI,GAAG,CAAC,KAAK,EAAE,KAAK;CAiBxB"}
@@ -0,0 +1,116 @@
1
+ import { Mixin } from "ts-mixer";
2
+ import { Bool, Provable } from "snarkyjs";
3
+ import { container } from "tsyringe";
4
+ import { dummyValue } from "@proto-kit/common";
5
+ import { Option } from "../model/Option";
6
+ import { StateTransition } from "../model/StateTransition";
7
+ import { RuntimeMethodExecutionContext } from "./context/RuntimeMethodExecutionContext";
8
+ export class WithPath {
9
+ hasPathOrFail() {
10
+ if (!this.path) {
11
+ throw new Error("Could not find 'path', did you forget to add '@state' to your state property?");
12
+ }
13
+ }
14
+ }
15
+ export class WithStateServiceProvider {
16
+ hasStateServiceOrFail() {
17
+ if (!this.stateServiceProvider) {
18
+ throw new Error("Could not find 'stateServiceProvider', did you forget to add '@state' to your state property?");
19
+ }
20
+ }
21
+ }
22
+ /**
23
+ * Utilities for runtime module state, such as get/set
24
+ */
25
+ export class State extends Mixin(WithPath, WithStateServiceProvider) {
26
+ /**
27
+ * Creates a new state wrapper for the provided value type.
28
+ *
29
+ * @param valueType - Type of value to be stored (e.g. UInt64, Struct, ...)
30
+ * @returns New state for the given value type.
31
+ */
32
+ static from(valueType) {
33
+ return new State(valueType);
34
+ }
35
+ constructor(valueType) {
36
+ super();
37
+ this.valueType = valueType;
38
+ }
39
+ getState() {
40
+ this.hasStateServiceOrFail();
41
+ this.hasPathOrFail();
42
+ const { path, stateServiceProvider, valueType } = this;
43
+ const { stateTransitions } = container
44
+ .resolve(RuntimeMethodExecutionContext)
45
+ .current().result;
46
+ // First try to find a match inside already created stateTransitions
47
+ const previousMutatingTransitions = stateTransitions.filter((transition) => transition.path.equals(path).and(transition.to.isSome).toBoolean());
48
+ const pmtLength = previousMutatingTransitions.length;
49
+ let value = pmtLength > 0
50
+ ? // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
51
+ previousMutatingTransitions[pmtLength - 1].to.value
52
+ : undefined;
53
+ if (value !== undefined) {
54
+ return { value, isSome: Bool(true) };
55
+ }
56
+ // If the value is still undefined, look it up in the stateService
57
+ const fields = stateServiceProvider.stateService.get(path);
58
+ if (fields) {
59
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
60
+ value = valueType.fromFields(fields);
61
+ }
62
+ if (value !== undefined) {
63
+ return { value, isSome: Bool(true) };
64
+ }
65
+ return { value: dummyValue(valueType), isSome: Bool(false) };
66
+ }
67
+ /**
68
+ * Provides an in-circuit witness for the current state representation,
69
+ * and constructs an Option out of it.
70
+ *
71
+ * @returns Optional value of the current state
72
+ */
73
+ witnessState() {
74
+ // get the value from storage, or return a dummy value instead
75
+ const value = Provable.witness(this.valueType, () => this.getState().value);
76
+ // check if the value exists in the storage or not
77
+ const isSome = Provable.witness(Bool, () => this.getState().isSome);
78
+ return Option.from(isSome, value, this.valueType);
79
+ }
80
+ /**
81
+ * Retrieves the current state and creates a state transition
82
+ * anchoring the use of the current state value in the circuit.
83
+ *
84
+ * @returns Option representation of the current state.
85
+ */
86
+ get() {
87
+ const option = this.witnessState();
88
+ this.hasPathOrFail();
89
+ const stateTransition = StateTransition.from(this.path, option);
90
+ container
91
+ .resolve(RuntimeMethodExecutionContext)
92
+ .addStateTransition(stateTransition);
93
+ return option;
94
+ }
95
+ /**
96
+ * Sets a new state value by creating a state transition from
97
+ * the current value to the newly set value.
98
+ *
99
+ * The newly set value isn't available via state.get(), since the
100
+ * state transitions are not applied within the same circuit.
101
+ * You can however store and access your new value in
102
+ * a separate circuit variable.
103
+ *
104
+ * @param value - Value to be set as the current state
105
+ */
106
+ set(value) {
107
+ // link the transition to the current state
108
+ const fromOption = this.witnessState();
109
+ const toOption = Option.from(Bool(true), value, this.valueType);
110
+ this.hasPathOrFail();
111
+ const stateTransition = StateTransition.fromTo(this.path, fromOption, toOption);
112
+ container
113
+ .resolve(RuntimeMethodExecutionContext)
114
+ .addStateTransition(stateTransition);
115
+ }
116
+ }
@@ -0,0 +1,37 @@
1
+ import type { Field, FlexibleProvablePure } from "snarkyjs";
2
+ import { Option } from "../model/Option";
3
+ import { WithStateServiceProvider, WithPath } from "./State";
4
+ declare const StateMap_base: import("ts-mixer/dist/types/types").Class<any[], WithPath & WithStateServiceProvider, typeof WithPath & typeof WithStateServiceProvider, false>;
5
+ /**
6
+ * Map-like wrapper for state
7
+ */
8
+ export declare class StateMap<KeyType, ValueType> extends StateMap_base {
9
+ keyType: FlexibleProvablePure<KeyType>;
10
+ valueType: FlexibleProvablePure<ValueType>;
11
+ /**
12
+ * Create a new state map with the given key and value types
13
+ *
14
+ * @param keyType - Type to be used as a key
15
+ * @param valueType - Type to be stored as a value
16
+ * @returns State map with provided key and value types.
17
+ */
18
+ static from<KeyType, ValueType>(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>): StateMap<KeyType, ValueType>;
19
+ constructor(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>);
20
+ getPath(key: KeyType): Field;
21
+ /**
22
+ * Obtains a value for the provided key in the current state map.
23
+ *
24
+ * @param key - Key to obtain the state for
25
+ * @returns Value for the provided key.
26
+ */
27
+ get(key: KeyType): Option<ValueType>;
28
+ /**
29
+ * Sets a value for the given key in the current state map.
30
+ *
31
+ * @param key - Key to store the value under
32
+ * @param value - Value to be stored under the given key
33
+ */
34
+ set(key: KeyType, value: ValueType): void;
35
+ }
36
+ export {};
37
+ //# sourceMappingURL=StateMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StateMap.d.ts","sourceRoot":"","sources":["../../src/state/StateMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAI5D,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAS,wBAAwB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;;AAEpE;;GAEG;AAEH,qBAAa,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAE,SAAQ,aAGjD;IAgBU,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC;IACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAhBnD;;;;;;OAMG;WACW,IAAI,CAAC,OAAO,EAAE,SAAS,EACnC,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC,GACzC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC;gBAKtB,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAK5C,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK;IAKnC;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC;IAU3C;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI;CASjD"}
@@ -0,0 +1,56 @@
1
+ import { Mixin } from "ts-mixer";
2
+ import { Path } from "../model/Path";
3
+ import { State, WithStateServiceProvider, WithPath } from "./State";
4
+ /**
5
+ * Map-like wrapper for state
6
+ */
7
+ // eslint-disable-next-line new-cap
8
+ export class StateMap extends Mixin(WithPath, WithStateServiceProvider) {
9
+ /**
10
+ * Create a new state map with the given key and value types
11
+ *
12
+ * @param keyType - Type to be used as a key
13
+ * @param valueType - Type to be stored as a value
14
+ * @returns State map with provided key and value types.
15
+ */
16
+ static from(keyType, valueType) {
17
+ return new StateMap(keyType, valueType);
18
+ }
19
+ constructor(keyType, valueType) {
20
+ super();
21
+ this.keyType = keyType;
22
+ this.valueType = valueType;
23
+ }
24
+ getPath(key) {
25
+ this.hasPathOrFail();
26
+ return Path.fromKey(this.path, this.keyType, key);
27
+ }
28
+ /**
29
+ * Obtains a value for the provided key in the current state map.
30
+ *
31
+ * @param key - Key to obtain the state for
32
+ * @returns Value for the provided key.
33
+ */
34
+ get(key) {
35
+ const state = State.from(this.valueType);
36
+ this.hasPathOrFail();
37
+ this.hasStateServiceOrFail();
38
+ state.path = this.getPath(key);
39
+ state.stateServiceProvider = this.stateServiceProvider;
40
+ return state.get();
41
+ }
42
+ /**
43
+ * Sets a value for the given key in the current state map.
44
+ *
45
+ * @param key - Key to store the value under
46
+ * @param value - Value to be stored under the given key
47
+ */
48
+ set(key, value) {
49
+ const state = State.from(this.valueType);
50
+ this.hasPathOrFail();
51
+ this.hasStateServiceOrFail();
52
+ state.path = Path.fromKey(this.path, this.keyType, key);
53
+ state.stateServiceProvider = this.stateServiceProvider;
54
+ state.set(value);
55
+ }
56
+ }
@@ -0,0 +1,6 @@
1
+ import { Field } from "snarkyjs";
2
+ export interface StateService {
3
+ get: (key: Field) => Field[] | undefined;
4
+ set: (key: Field, value: Field[] | undefined) => void;
5
+ }
6
+ //# sourceMappingURL=StateService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StateService.d.ts","sourceRoot":"","sources":["../../src/state/StateService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,KAAK,EAAE,GAAG,SAAS,CAAC;IACzC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,SAAS,KAAK,IAAI,CAAC;CACvD"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { StateService } from "./StateService";
2
+ export declare class StateServiceProvider {
3
+ private currentStateService?;
4
+ private readonly defaultStateService?;
5
+ constructor(currentStateService?: StateService | undefined);
6
+ get stateService(): StateService;
7
+ setCurrentStateService(service: StateService): void;
8
+ resetToDefault(): void;
9
+ }
10
+ //# sourceMappingURL=StateServiceProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StateServiceProvider.d.ts","sourceRoot":"","sources":["../../src/state/StateServiceProvider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAW9C,qBACa,oBAAoB;IAKL,OAAO,CAAC,mBAAmB,CAAC;IAJtD,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CACV;gBAGO,mBAAmB,CAAC,0BAAc;IAGpE,IAAW,YAAY,IAAI,YAAY,CAKtC;IAEM,sBAAsB,CAAC,OAAO,EAAE,YAAY;IAI5C,cAAc;CAGtB"}
@@ -0,0 +1,42 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
11
+ return function (target, key) { decorator(target, key, paramIndex); }
12
+ };
13
+ import { inject, injectable } from "tsyringe";
14
+ const errors = {
15
+ stateServiceNotSet: () => new Error(`StateService has not been set yet. Be sure to either call your runtime or
16
+ protocol function by creating them with an AppChain or by setting the
17
+ stateService manually.`),
18
+ };
19
+ let StateServiceProvider = class StateServiceProvider {
20
+ constructor(currentStateService) {
21
+ this.currentStateService = currentStateService;
22
+ this.defaultStateService = this.currentStateService;
23
+ }
24
+ get stateService() {
25
+ if (this.currentStateService === undefined) {
26
+ throw errors.stateServiceNotSet();
27
+ }
28
+ return this.currentStateService;
29
+ }
30
+ setCurrentStateService(service) {
31
+ this.currentStateService = service;
32
+ }
33
+ resetToDefault() {
34
+ this.currentStateService = this.defaultStateService;
35
+ }
36
+ };
37
+ StateServiceProvider = __decorate([
38
+ injectable(),
39
+ __param(0, inject("StateService")),
40
+ __metadata("design:paramtypes", [Object])
41
+ ], StateServiceProvider);
42
+ export { StateServiceProvider };
@@ -0,0 +1,12 @@
1
+ import { Bool } from "snarkyjs";
2
+ /**
3
+ * Maintains an execution status of the current runtime module method,
4
+ * while prioritizing one-time failures. The assertion won't change the
5
+ * execution status if it has previously failed at least once within the
6
+ * same method execution context.
7
+ *
8
+ * @param condition - Result of the assertion made about the execution status
9
+ * @param message - Optional message describing the prior status
10
+ */
11
+ export declare function assert(condition: Bool, message?: string): void;
12
+ //# sourceMappingURL=assert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../src/state/assert/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAO1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAWvD"}
@@ -0,0 +1,23 @@
1
+ import { Bool, Provable } from "snarkyjs";
2
+ import { container } from "tsyringe";
3
+ import { log } from "@proto-kit/common";
4
+ import { RuntimeMethodExecutionContext } from "../context/RuntimeMethodExecutionContext";
5
+ /**
6
+ * Maintains an execution status of the current runtime module method,
7
+ * while prioritizing one-time failures. The assertion won't change the
8
+ * execution status if it has previously failed at least once within the
9
+ * same method execution context.
10
+ *
11
+ * @param condition - Result of the assertion made about the execution status
12
+ * @param message - Optional message describing the prior status
13
+ */
14
+ export function assert(condition, message) {
15
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
16
+ const previousStatus = executionContext.current().result.status;
17
+ const status = Provable.if(previousStatus, Bool, condition, previousStatus);
18
+ if (!condition.toBoolean()) {
19
+ log.debug("Assertion failed: ", message);
20
+ executionContext.setStatusMessage(message);
21
+ }
22
+ executionContext.setStatus(status);
23
+ }
@@ -0,0 +1,22 @@
1
+ import { TransitionMethodExecutionContext, TransitionMethodExecutionResult } from "./TransitionMethodExecutionContext";
2
+ import { StateTransition } from "../../model/StateTransition";
3
+ export declare class ProtocolMethodExecutionContext implements TransitionMethodExecutionContext {
4
+ result: TransitionMethodExecutionResult;
5
+ /**
6
+ * Adds an in-method generated state transition to the current context
7
+ * @param stateTransition - State transition to add to the context
8
+ */
9
+ addStateTransition<Value>(stateTransition: StateTransition<Value>): void;
10
+ /**
11
+ * Manually clears/resets the execution context
12
+ */
13
+ clear(): void;
14
+ /**
15
+ * Had to override current() otherwise it would not infer
16
+ * the type of result correctly (parent type would be reused)
17
+ */
18
+ current(): {
19
+ result: TransitionMethodExecutionResult;
20
+ };
21
+ }
22
+ //# sourceMappingURL=ProtocolMethodExecutionContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProtocolMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../../src/state/context/ProtocolMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gCAAgC,EAChC,+BAA+B,EAChC,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,qBAAa,8BAA+B,YAAW,gCAAgC;IAC9E,MAAM,kCAAyC;IAEtD;;;OAGG;IACI,kBAAkB,CAAC,KAAK,EAC7B,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAKzC;;OAEG;IACI,KAAK;IAIZ;;;OAGG;IACI,OAAO;;;CAKf"}
@@ -0,0 +1,28 @@
1
+ import { TransitionMethodExecutionResult } from "./TransitionMethodExecutionContext";
2
+ export class ProtocolMethodExecutionContext {
3
+ constructor() {
4
+ this.result = new TransitionMethodExecutionResult();
5
+ }
6
+ /**
7
+ * Adds an in-method generated state transition to the current context
8
+ * @param stateTransition - State transition to add to the context
9
+ */
10
+ addStateTransition(stateTransition) {
11
+ this.result.stateTransitions.push(stateTransition);
12
+ }
13
+ /**
14
+ * Manually clears/resets the execution context
15
+ */
16
+ clear() {
17
+ this.result = new TransitionMethodExecutionResult();
18
+ }
19
+ /**
20
+ * Had to override current() otherwise it would not infer
21
+ * the type of result correctly (parent type would be reused)
22
+ */
23
+ current() {
24
+ return {
25
+ result: this.result,
26
+ };
27
+ }
28
+ }
@@ -0,0 +1,60 @@
1
+ import { Bool } from "snarkyjs";
2
+ import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
3
+ import { StateTransition } from "../../model/StateTransition";
4
+ import { RuntimeTransaction } from "../../model/transaction/RuntimeTransaction";
5
+ import { NetworkState } from "../../model/network/NetworkState";
6
+ export declare class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
7
+ stateTransitions: StateTransition<any>[];
8
+ status: Bool;
9
+ statusMessage?: string;
10
+ }
11
+ export interface RuntimeMethodExecutionData {
12
+ transaction: RuntimeTransaction;
13
+ networkState: NetworkState;
14
+ }
15
+ /**
16
+ * Execution context used to wrap runtime module methods,
17
+ * allowing them to post relevant information (such as execution status)
18
+ * into the context without any unnecessary 'prop drilling'.
19
+ */
20
+ export declare class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
21
+ methods: string[];
22
+ input: RuntimeMethodExecutionData | undefined;
23
+ private lastInput;
24
+ result: RuntimeProvableMethodExecutionResult;
25
+ private isSimulated;
26
+ private assertSetupCalled;
27
+ /**
28
+ * Adds an in-method generated state transition to the current context
29
+ * @param stateTransition - State transition to add to the context
30
+ */
31
+ addStateTransition<Value>(stateTransition: StateTransition<Value>): void;
32
+ /**
33
+ * @param message - Status message to acompany the current status
34
+ */
35
+ setStatusMessage(message?: string): void;
36
+ /**
37
+ * @param status - Execution status of the current method
38
+ */
39
+ setStatus(status: Bool): void;
40
+ /**
41
+ * @param input Input witness data required for a runtime execution
42
+ */
43
+ setup(input: RuntimeMethodExecutionData): void;
44
+ setSimulated(simulated: boolean): void;
45
+ /**
46
+ * Manually clears/resets the execution context
47
+ */
48
+ clear(): void;
49
+ afterMethod(): void;
50
+ /**
51
+ * Had to override current() otherwise it would not infer
52
+ * the type of result correctly (parent type would be reused)
53
+ */
54
+ current(): {
55
+ isFinished: boolean;
56
+ result: RuntimeProvableMethodExecutionResult;
57
+ input: RuntimeMethodExecutionData | undefined;
58
+ };
59
+ }
60
+ //# sourceMappingURL=RuntimeMethodExecutionContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RuntimeMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../../src/state/context/RuntimeMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,EAAE,8BAA8B,EAAE,6BAA6B,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAShE,qBAAa,oCAAqC,SAAQ,6BAA6B;IAE9E,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAM;IAE9C,MAAM,EAAE,IAAI,CAAc;IAE1B,aAAa,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,0BAA0B;IACzC,WAAW,EAAE,kBAAkB,CAAC;IAChC,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED;;;;GAIG;AACH,qBACa,6BAA8B,SAAQ,8BAA8B;IACxE,OAAO,EAAE,MAAM,EAAE,CAAM;IAEvB,KAAK,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAGrD,OAAO,CAAC,SAAS,CAAyC;IAE1C,MAAM,uCAA8C;IAEpE,OAAO,CAAC,WAAW,CAAkB;IAErC,OAAO,CAAC,iBAAiB;IAQzB;;;OAGG;IACI,kBAAkB,CAAC,KAAK,EAAE,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAKxE;;OAEG;IACI,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM;IAQxC;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,IAAI;IAQ7B;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,0BAA0B;IAIvC,YAAY,CAAC,SAAS,EAAE,OAAO;IAItC;;OAEG;IACI,KAAK;IAIL,WAAW;IASlB;;;OAGG;IACI,OAAO;;;;;CAOf"}
@@ -0,0 +1,105 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { Bool } from "snarkyjs";
8
+ import { singleton } from "tsyringe";
9
+ import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
10
+ const errors = {
11
+ setupNotCalled: () => new Error("Setup has not been called prior to executing a runtime method. Be sure to do that so that the Runtime is setup property for execution"),
12
+ };
13
+ export class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
14
+ constructor() {
15
+ super(...arguments);
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ this.stateTransitions = [];
18
+ this.status = Bool(true);
19
+ }
20
+ }
21
+ /**
22
+ * Execution context used to wrap runtime module methods,
23
+ * allowing them to post relevant information (such as execution status)
24
+ * into the context without any unnecessary 'prop drilling'.
25
+ */
26
+ let RuntimeMethodExecutionContext = class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
27
+ constructor() {
28
+ super(...arguments);
29
+ this.methods = [];
30
+ this.result = new RuntimeProvableMethodExecutionResult();
31
+ this.isSimulated = false;
32
+ }
33
+ assertSetupCalled() {
34
+ if (this.input === undefined) {
35
+ throw errors.setupNotCalled();
36
+ }
37
+ }
38
+ /**
39
+ * Adds an in-method generated state transition to the current context
40
+ * @param stateTransition - State transition to add to the context
41
+ */
42
+ addStateTransition(stateTransition) {
43
+ this.assertSetupCalled();
44
+ this.result.stateTransitions.push(stateTransition);
45
+ }
46
+ /**
47
+ * @param message - Status message to acompany the current status
48
+ */
49
+ setStatusMessage(message) {
50
+ var _a;
51
+ this.assertSetupCalled();
52
+ if (this.isSimulated) {
53
+ return;
54
+ }
55
+ (_a = this.result).statusMessage ?? (_a.statusMessage = message);
56
+ }
57
+ /**
58
+ * @param status - Execution status of the current method
59
+ */
60
+ setStatus(status) {
61
+ this.assertSetupCalled();
62
+ if (this.isSimulated) {
63
+ return;
64
+ }
65
+ this.result.status = status;
66
+ }
67
+ /**
68
+ * @param input Input witness data required for a runtime execution
69
+ */
70
+ setup(input) {
71
+ this.input = input;
72
+ }
73
+ setSimulated(simulated) {
74
+ this.isSimulated = simulated;
75
+ }
76
+ /**
77
+ * Manually clears/resets the execution context
78
+ */
79
+ clear() {
80
+ this.result = new RuntimeProvableMethodExecutionResult();
81
+ }
82
+ afterMethod() {
83
+ super.afterMethod();
84
+ if (this.isFinished) {
85
+ this.lastInput = this.input;
86
+ this.input = undefined;
87
+ this.isSimulated = false;
88
+ }
89
+ }
90
+ /**
91
+ * Had to override current() otherwise it would not infer
92
+ * the type of result correctly (parent type would be reused)
93
+ */
94
+ current() {
95
+ return {
96
+ isFinished: this.isFinished,
97
+ result: this.result,
98
+ input: this.lastInput,
99
+ };
100
+ }
101
+ };
102
+ RuntimeMethodExecutionContext = __decorate([
103
+ singleton()
104
+ ], RuntimeMethodExecutionContext);
105
+ export { RuntimeMethodExecutionContext };
@@ -0,0 +1,23 @@
1
+ import { StateTransition } from "../../model/StateTransition";
2
+ export declare class TransitionMethodExecutionResult {
3
+ stateTransitions: StateTransition<any>[];
4
+ }
5
+ export interface TransitionMethodExecutionContext {
6
+ /**
7
+ * Adds an in-method generated state transition to the current context
8
+ * @param stateTransition - State transition to add to the context
9
+ */
10
+ addStateTransition: <Value>(stateTransition: StateTransition<Value>) => void;
11
+ /**
12
+ * Manually clears/resets the execution context
13
+ */
14
+ clear: () => void;
15
+ /**
16
+ * Had to override current() otherwise it would not infer
17
+ * the type of result correctly (parent type would be reused)
18
+ */
19
+ current: () => {
20
+ result: TransitionMethodExecutionResult;
21
+ };
22
+ }
23
+ //# sourceMappingURL=TransitionMethodExecutionContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TransitionMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../../src/state/context/TransitionMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,qBAAa,+BAA+B;IAEnC,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAM;CACtD;AAED,MAAM,WAAW,gCAAgC;IAC/C;;;OAGG;IACH,kBAAkB,EAAE,CAAC,KAAK,EAAE,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC;IAE7E;;OAEG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;IAElB;;;OAGG;IACH,OAAO,EAAE,MAAM;QACb,MAAM,EAAE,+BAA+B,CAAC;KACzC,CAAC;CACH"}
@@ -0,0 +1,6 @@
1
+ export class TransitionMethodExecutionResult {
2
+ constructor() {
3
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
+ this.stateTransitions = [];
5
+ }
6
+ }