@proto-kit/module 0.1.1-develop.165 → 0.1.1-develop.1661

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 (118) hide show
  1. package/dist/factories/MethodIdFactory.d.ts +9 -0
  2. package/dist/factories/MethodIdFactory.d.ts.map +1 -0
  3. package/dist/factories/MethodIdFactory.js +11 -0
  4. package/dist/factories/MethodIdFactory.js.map +1 -0
  5. package/dist/index.d.ts +5 -7
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +6 -7
  8. package/dist/index.js.map +1 -0
  9. package/dist/messages/OutgoingMessage.d.ts +96 -0
  10. package/dist/messages/OutgoingMessage.d.ts.map +1 -0
  11. package/dist/messages/OutgoingMessage.js +68 -0
  12. package/dist/messages/OutgoingMessage.js.map +1 -0
  13. package/dist/messages/OutgoingMessages.d.ts +215 -0
  14. package/dist/messages/OutgoingMessages.d.ts.map +1 -0
  15. package/dist/messages/OutgoingMessages.js +66 -0
  16. package/dist/messages/OutgoingMessages.js.map +1 -0
  17. package/dist/method/MethodParameterEncoder.d.ts +26 -0
  18. package/dist/method/MethodParameterEncoder.d.ts.map +1 -0
  19. package/dist/method/MethodParameterEncoder.js +169 -0
  20. package/dist/method/MethodParameterEncoder.js.map +1 -0
  21. package/dist/method/runtimeMethod.d.ts +22 -5
  22. package/dist/method/runtimeMethod.d.ts.map +1 -1
  23. package/dist/method/runtimeMethod.js +79 -24
  24. package/dist/method/runtimeMethod.js.map +1 -0
  25. package/dist/module/decorator.js +1 -0
  26. package/dist/module/decorator.js.map +1 -0
  27. package/dist/runtime/MethodIdResolver.d.ts +20 -0
  28. package/dist/runtime/MethodIdResolver.d.ts.map +1 -0
  29. package/dist/runtime/MethodIdResolver.js +91 -0
  30. package/dist/runtime/MethodIdResolver.js.map +1 -0
  31. package/dist/runtime/Runtime.d.ts +22 -28
  32. package/dist/runtime/Runtime.d.ts.map +1 -1
  33. package/dist/runtime/Runtime.js +116 -70
  34. package/dist/runtime/Runtime.js.map +1 -0
  35. package/dist/runtime/RuntimeEnvironment.d.ts +10 -0
  36. package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -0
  37. package/dist/runtime/RuntimeEnvironment.js +2 -0
  38. package/dist/runtime/RuntimeEnvironment.js.map +1 -0
  39. package/dist/runtime/RuntimeModule.d.ts +23 -14
  40. package/dist/runtime/RuntimeModule.d.ts.map +1 -1
  41. package/dist/runtime/RuntimeModule.js +47 -10
  42. package/dist/runtime/RuntimeModule.js.map +1 -0
  43. package/dist/state/InMemoryStateService.d.ts +11 -10
  44. package/dist/state/InMemoryStateService.d.ts.map +1 -1
  45. package/dist/state/InMemoryStateService.js +11 -8
  46. package/dist/state/InMemoryStateService.js.map +1 -0
  47. package/dist/state/decorator.d.ts.map +1 -1
  48. package/dist/state/decorator.js +2 -4
  49. package/dist/state/decorator.js.map +1 -0
  50. package/dist/testing/TestingRuntime.d.ts +8 -0
  51. package/dist/testing/TestingRuntime.d.ts.map +1 -0
  52. package/dist/testing/TestingRuntime.js +31 -0
  53. package/dist/testing/TestingRuntime.js.map +1 -0
  54. package/jest.config.cjs +12 -1
  55. package/package.json +10 -11
  56. package/src/factories/MethodIdFactory.ts +13 -0
  57. package/src/index.ts +5 -7
  58. package/src/messages/OutgoingMessages.ts +122 -0
  59. package/src/method/MethodParameterEncoder.ts +260 -0
  60. package/src/method/runtimeMethod.ts +146 -31
  61. package/src/runtime/MethodIdResolver.ts +108 -0
  62. package/src/runtime/Runtime.ts +191 -112
  63. package/src/runtime/RuntimeEnvironment.ts +16 -0
  64. package/src/runtime/RuntimeModule.ts +77 -27
  65. package/src/state/InMemoryStateService.ts +14 -18
  66. package/test/Runtime.test.ts +69 -36
  67. package/test/TestingRuntime.ts +43 -0
  68. package/test/messages/message.test.ts +42 -0
  69. package/test/method/MethodParameterEncoder.test.ts +121 -0
  70. package/test/method/runtimeMethod-fail.test.ts +50 -0
  71. package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +3 -3
  72. package/test/modules/Admin.ts +4 -4
  73. package/test/modules/Balances.test.ts +92 -78
  74. package/test/modules/Balances.ts +19 -16
  75. package/test/modules/MethodIdResolver.test.ts +73 -0
  76. package/test/modules/State.test.ts +81 -0
  77. package/test/runtimeMethod.test.ts +192 -20
  78. package/test/tsconfig.json +7 -0
  79. package/tsconfig.json +2 -2
  80. package/dist/chain/Chain.d.ts +0 -109
  81. package/dist/chain/Chain.d.ts.map +0 -1
  82. package/dist/chain/Chain.js +0 -229
  83. package/dist/method/MethodExecutionContext.d.ts +0 -73
  84. package/dist/method/MethodExecutionContext.d.ts.map +0 -1
  85. package/dist/method/MethodExecutionContext.js +0 -112
  86. package/dist/method/MethodParameterDecoder.d.ts +0 -22
  87. package/dist/method/MethodParameterDecoder.d.ts.map +0 -1
  88. package/dist/method/MethodParameterDecoder.js +0 -33
  89. package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
  90. package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
  91. package/dist/method/RuntimeMethodExecutionContext.js +0 -92
  92. package/dist/method/assert.d.ts +0 -12
  93. package/dist/method/assert.d.ts.map +0 -1
  94. package/dist/method/assert.js +0 -23
  95. package/dist/method/decorator.d.ts +0 -45
  96. package/dist/method/decorator.d.ts.map +0 -1
  97. package/dist/method/decorator.js +0 -140
  98. package/dist/state/State.d.ts +0 -65
  99. package/dist/state/State.d.ts.map +0 -1
  100. package/dist/state/State.js +0 -114
  101. package/dist/state/StateMap.d.ts +0 -37
  102. package/dist/state/StateMap.d.ts.map +0 -1
  103. package/dist/state/StateMap.js +0 -56
  104. package/dist/state/StateServiceProvider.d.ts +0 -10
  105. package/dist/state/StateServiceProvider.d.ts.map +0 -1
  106. package/dist/state/StateServiceProvider.js +0 -34
  107. package/src/method/MethodParameterDecoder.ts +0 -68
  108. package/src/method/RuntimeMethodExecutionContext.ts +0 -111
  109. package/src/method/assert.test.ts +0 -49
  110. package/src/method/assert.ts +0 -27
  111. package/src/state/State.ts +0 -154
  112. package/src/state/StateMap.ts +0 -69
  113. package/src/state/StateServiceProvider.ts +0 -24
  114. package/src/state/decorator.ts +0 -65
  115. package/test/state/MerkleTree.test.ts +0 -95
  116. package/test/state/MockAsyncMerkleStore.ts +0 -28
  117. package/test/transaction.test.ts +0 -82
  118. package/tsconfig.test.json +0 -9
@@ -1,112 +0,0 @@
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
- import { Bool } from "snarkyjs";
11
- import { singleton } from "tsyringe";
12
- export class MethodExecutionResult {
13
- constructor() {
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
- this.stateTransitions = [];
16
- this.status = Bool(true);
17
- }
18
- }
19
- /**
20
- * Execution context used to wrap runtime module methods,
21
- * allowing them to post relevant information (such as execution status)
22
- * into the context without any unnecessary 'prop drilling'.
23
- */
24
- let MethodExecutionContext = class MethodExecutionContext {
25
- constructor(result = new MethodExecutionResult()) {
26
- this.result = result;
27
- this.methods = [];
28
- }
29
- /**
30
- * Adds an in-method generated state transition to the current context
31
- * @param stateTransition - State transition to add to the context
32
- */
33
- addStateTransition(stateTransition) {
34
- this.result.stateTransitions.push(stateTransition);
35
- }
36
- /**
37
- * @param message - Status message to acompany the current status
38
- */
39
- setStatusMessage(message) {
40
- var _a;
41
- (_a = this.result).statusMessage ?? (_a.statusMessage = message);
42
- }
43
- /**
44
- * @param status - Execution status of the current method
45
- */
46
- setStatus(status) {
47
- this.result.status = status;
48
- }
49
- /**
50
- * @param value = Return value of the executed method
51
- */
52
- setValue(value) {
53
- this.result.value = value;
54
- }
55
- /**
56
- * Adds a method prover to the current execution context,
57
- * which can be collected and ran asynchronously at a later point in time.
58
- *
59
- * @param prove - Prover function to be ran later,
60
- * when the method execution needs to be proven
61
- */
62
- setProve(prove) {
63
- this.result.prove = prove;
64
- }
65
- setPublicInput(publicInput) { }
66
- /**
67
- * Adds a method to the method execution stack, reseting the execution context
68
- * in a case a new top-level (non nested) method call is made.
69
- *
70
- * @param methodName - Name of the method being captured in the context
71
- */
72
- beforeMethod(methodName) {
73
- if (this.isFinished) {
74
- this.result = new MethodExecutionResult();
75
- }
76
- this.methods.push(methodName);
77
- }
78
- /**
79
- * Removes the latest method from the execution context stack,
80
- * keeping track of the amount of 'unfinished' methods. Allowing
81
- * for the context to distinguish between top-level and nested method calls.
82
- */
83
- afterMethod() {
84
- this.methods.pop();
85
- }
86
- get isTopLevel() {
87
- return this.isFinished;
88
- }
89
- get isFinished() {
90
- return this.methods.length === 0;
91
- }
92
- /**
93
- * @returns - Current execution context state
94
- */
95
- current() {
96
- return {
97
- isFinished: this.isFinished,
98
- result: this.result,
99
- };
100
- }
101
- /**
102
- * Manually clears/resets the execution context
103
- */
104
- clear() {
105
- this.result = new MethodExecutionResult();
106
- }
107
- };
108
- MethodExecutionContext = __decorate([
109
- singleton(),
110
- __metadata("design:paramtypes", [MethodExecutionResult])
111
- ], MethodExecutionContext);
112
- export { MethodExecutionContext };
@@ -1,22 +0,0 @@
1
- import { Field } from "snarkyjs";
2
- import { RuntimeModule } from "../runtime/RuntimeModule";
3
- export interface Fieldable {
4
- toFields: () => Field[];
5
- }
6
- export interface FromFieldClass {
7
- new: (...args: any[]) => any;
8
- fromFields: (fields: Field[]) => Fieldable;
9
- name: string;
10
- prototype: {
11
- _fields?: any[];
12
- };
13
- sizeInFields?: () => number;
14
- }
15
- export declare class MethodParameterDecoder {
16
- private readonly types;
17
- static fromMethod(target: RuntimeModule<unknown>, methodName: string): MethodParameterDecoder;
18
- private constructor();
19
- fromFields(fields: Field[]): Fieldable[];
20
- get fieldSize(): number;
21
- }
22
- //# sourceMappingURL=MethodParameterDecoder.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MethodParameterDecoder.d.ts","sourceRoot":"","sources":["../../src/method/MethodParameterDecoder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,MAAM,KAAK,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;IAC7B,UAAU,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,SAAS,CAAC;IAC3C,IAAI,EAAE,MAAM,CAAC;IAEb,SAAS,EAAE;QACT,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;KACjB,CAAC;IACF,YAAY,CAAC,EAAE,MAAM,MAAM,CAAC;CAC7B;AAYD,qBAAa,sBAAsB;IAWb,OAAO,CAAC,QAAQ,CAAC,KAAK;WAV5B,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,MAAM;IAU3E,OAAO;IAEA,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAiB/C,IAAW,SAAS,IAAI,MAAM,CAM7B;CACF"}
@@ -1,33 +0,0 @@
1
- const errors = {
2
- fieldLengthNotMatching: (expected, actual) => new Error(`Expected ${expected} field elements, got ${actual}`),
3
- typeNotCompatible: (name) => new Error(`Cannot decode type ${name}, it has to be either a Struct, CircuitValue or built-in snarkyjs type`),
4
- };
5
- export class MethodParameterDecoder {
6
- static fromMethod(target, methodName) {
7
- const paramtypes = Reflect.getMetadata("design:paramtypes", target, methodName);
8
- return new MethodParameterDecoder(paramtypes);
9
- }
10
- constructor(types) {
11
- this.types = types;
12
- }
13
- fromFields(fields) {
14
- if (fields.length < this.fieldSize) {
15
- throw errors.fieldLengthNotMatching(this.fieldSize, fields.length);
16
- }
17
- let stack = fields.slice();
18
- return this.types.map((type) => {
19
- const numberFieldsNeeded = type.prototype._fields?.length ?? type.sizeInFields?.() ?? -1;
20
- if (numberFieldsNeeded === -1) {
21
- throw errors.typeNotCompatible(type.name);
22
- }
23
- const structFields = stack.slice(0, numberFieldsNeeded);
24
- stack = stack.slice(numberFieldsNeeded);
25
- return type.fromFields(structFields);
26
- });
27
- }
28
- get fieldSize() {
29
- return this.types
30
- .map((type) => type.prototype._fields?.length ?? type.sizeInFields?.() ?? 0)
31
- .reduce((a, b) => a + b);
32
- }
33
- }
@@ -1,57 +0,0 @@
1
- import { Bool } from "snarkyjs";
2
- import type { StateTransition, NetworkState } from "@proto-kit/protocol";
3
- import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
4
- import { RuntimeTransaction } from "@proto-kit/protocol/src/model/transaction/RuntimeTransaction";
5
- export declare class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
6
- stateTransitions: StateTransition<any>[];
7
- status: Bool;
8
- statusMessage?: string;
9
- }
10
- export interface RuntimeMethodExecutionData {
11
- transaction: RuntimeTransaction;
12
- networkState: NetworkState;
13
- }
14
- /**
15
- * Execution context used to wrap runtime module methods,
16
- * allowing them to post relevant information (such as execution status)
17
- * into the context without any unnecessary 'prop drilling'.
18
- */
19
- export declare class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
20
- methods: string[];
21
- input: RuntimeMethodExecutionData | undefined;
22
- private lastInput;
23
- result: RuntimeProvableMethodExecutionResult;
24
- private assertSetupCalled;
25
- /**
26
- * Adds an in-method generated state transition to the current context
27
- * @param stateTransition - State transition to add to the context
28
- */
29
- addStateTransition<Value>(stateTransition: StateTransition<Value>): void;
30
- /**
31
- * @param message - Status message to acompany the current status
32
- */
33
- setStatusMessage(message?: string): void;
34
- /**
35
- * @param status - Execution status of the current method
36
- */
37
- setStatus(status: Bool): void;
38
- /**
39
- * @param input Input witness data required for a runtime execution
40
- */
41
- setup(input: RuntimeMethodExecutionData): void;
42
- /**
43
- * Manually clears/resets the execution context
44
- */
45
- clear(): void;
46
- afterMethod(): void;
47
- /**
48
- * Had to override current() otherwise it would not infer
49
- * the type of result correctly (parent type would be reused)
50
- */
51
- current(): {
52
- isFinished: boolean;
53
- result: RuntimeProvableMethodExecutionResult;
54
- input: RuntimeMethodExecutionData | undefined;
55
- };
56
- }
57
- //# sourceMappingURL=RuntimeMethodExecutionContext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RuntimeMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../src/method/RuntimeMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EACL,8BAA8B,EAC9B,6BAA6B,EAC9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,kBAAkB,EAAE,MAAM,8DAA8D,CAAC;AASlG,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,iBAAiB;IAQzB;;;OAGG;IACI,kBAAkB,CAAC,KAAK,EAAE,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAKxE;;OAEG;IACI,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM;IAKxC;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,IAAI;IAK7B;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,0BAA0B;IAI9C;;OAEG;IACI,KAAK;IAIL,WAAW;IAMlB;;;OAGG;IACI,OAAO;;;;;CAOf"}
@@ -1,92 +0,0 @@
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
- }
32
- assertSetupCalled() {
33
- if (this.input === undefined) {
34
- throw errors.setupNotCalled();
35
- }
36
- }
37
- /**
38
- * Adds an in-method generated state transition to the current context
39
- * @param stateTransition - State transition to add to the context
40
- */
41
- addStateTransition(stateTransition) {
42
- this.assertSetupCalled();
43
- this.result.stateTransitions.push(stateTransition);
44
- }
45
- /**
46
- * @param message - Status message to acompany the current status
47
- */
48
- setStatusMessage(message) {
49
- var _a;
50
- this.assertSetupCalled();
51
- (_a = this.result).statusMessage ?? (_a.statusMessage = message);
52
- }
53
- /**
54
- * @param status - Execution status of the current method
55
- */
56
- setStatus(status) {
57
- this.assertSetupCalled();
58
- this.result.status = status;
59
- }
60
- /**
61
- * @param input Input witness data required for a runtime execution
62
- */
63
- setup(input) {
64
- this.input = input;
65
- }
66
- /**
67
- * Manually clears/resets the execution context
68
- */
69
- clear() {
70
- this.result = new RuntimeProvableMethodExecutionResult();
71
- }
72
- afterMethod() {
73
- super.afterMethod();
74
- this.lastInput = this.input;
75
- this.input = undefined;
76
- }
77
- /**
78
- * Had to override current() otherwise it would not infer
79
- * the type of result correctly (parent type would be reused)
80
- */
81
- current() {
82
- return {
83
- isFinished: this.isFinished,
84
- result: this.result,
85
- input: this.lastInput,
86
- };
87
- }
88
- };
89
- RuntimeMethodExecutionContext = __decorate([
90
- singleton()
91
- ], RuntimeMethodExecutionContext);
92
- export { RuntimeMethodExecutionContext };
@@ -1,12 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/method/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAM1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAWvD"}
@@ -1,23 +0,0 @@
1
- import { Bool, Provable } from "snarkyjs";
2
- import { container } from "tsyringe";
3
- import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext";
4
- import { log } from "@proto-kit/common";
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
- }
@@ -1,45 +0,0 @@
1
- import { Field } from "snarkyjs";
2
- import { StateTransition, MethodPublicInput } from "@yab/protocol";
3
- import type { RuntimeModule } from "../runtime/RuntimeModule.js";
4
- /**
5
- * Runs a method wrapped in a method execution context.
6
- */
7
- export declare function runInContext(this: RuntimeModule<unknown>, methodName: string, moduleMethod: (...args: unknown[]) => unknown, args: unknown[]): {
8
- isFinished: boolean;
9
- result: import("./MethodExecutionContext.js").MethodExecutionResult<unknown>;
10
- };
11
- export declare function toStateTransitionsHash(stateTransitions: StateTransition<any>[]): Field;
12
- export type WrappedMethod = (publicInput: MethodPublicInput, ...args: unknown[]) => unknown;
13
- export declare function toWrappedMethod(this: RuntimeModule<unknown>, methodName: string, moduleMethod: (...args: unknown[]) => unknown): WrappedMethod;
14
- export declare function combineMethodName(runtimeModuleName: string, methodName: string): string;
15
- /**
16
- * Precomputes the public inputs required to run
17
- * an actual wrapped method, produced from the provided moduleMethod.
18
- *
19
- * Execute the wrapped method with the precomputed public inputs.
20
- */
21
- export declare function runWithCommitments(this: RuntimeModule<unknown>, methodName: string, moduleMethod: (...args: unknown[]) => unknown, args: unknown[]): unknown;
22
- export declare const methodMetadataKey = "yab-method";
23
- /**
24
- * Checks the metadata of the provided runtime module and its method,
25
- * to see if it has been decorated with @method()
26
- *
27
- * @param target - Runtime module to check
28
- * @param propertyKey - Name of the method to check in the prior runtime module
29
- * @returns - If the provided method name is a runtime method or not
30
- */
31
- export declare function isMethod(target: RuntimeModule<unknown>, propertyKey: string): boolean;
32
- export type DecoratedMethod = (...args: unknown[]) => unknown;
33
- /**
34
- * Decorates a runtime module method and toggles execution of
35
- * either of its variants, depending on the state of the current
36
- * method execution context. If the method is called at the 'top-level',
37
- * it is executed 'with commitments'. If the method is called from another
38
- * method, it's executed directly.
39
- *
40
- * Additionally the method is marked with metadata as a 'runtime module method'.
41
- *
42
- * @returns A decorated runtime module method
43
- */
44
- export declare function method(): (target: RuntimeModule<unknown>, propertyKey: string, descriptor: PropertyDescriptor) => void;
45
- //# sourceMappingURL=decorator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"decorator.d.ts","sourceRoot":"","sources":["../../src/method/decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,EACL,eAAe,EAGf,iBAAiB,EAClB,MAAM,eAAe,CAAC;AAEvB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAqBjE;;GAEG;AAEH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAC7C,IAAI,EAAE,OAAO,EAAE;;;EAuBhB;AAED,wBAAgB,sBAAsB,CAEpC,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,SAczC;AAGD,MAAM,MAAM,aAAa,GAAG,CAC1B,WAAW,EAAE,iBAAiB,EAC9B,GAAG,IAAI,EAAE,OAAO,EAAE,KACf,OAAO,CAAC;AAEb,wBAAgB,eAAe,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,iBAiC9C;AAED,wBAAgB,iBAAiB,CAC/B,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,UAGnB;AAED;;;;;GAKG;AAEH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAC7C,IAAI,EAAE,OAAO,EAAE,WA4ChB;AAED,eAAO,MAAM,iBAAiB,eAAe,CAAC;AAE9C;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,WAAW,EAAE,MAAM,WAE3E;AAGD,MAAM,MAAM,eAAe,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC;AAE9D;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,aAEV,cAAc,OAAO,CAAC,eACjB,MAAM,cACP,kBAAkB,UAyBjC"}
@@ -1,140 +0,0 @@
1
- import { Field } from "snarkyjs";
2
- import { container } from "tsyringe";
3
- import { DefaultProvableHashList, ProvableStateTransition, } from "@yab/protocol";
4
- import { MethodExecutionContext } from "./MethodExecutionContext.js";
5
- const errors = {
6
- inconsistentStateTransitions: (methodName) => `State transitions produced by '@method ${methodName}' are not consistent
7
- through multiple method executions, does your method contain
8
- any circuit-unfriendly conditional logic?`,
9
- inconsistentExecutionStatus: (methodName) => `Execution status of '@method ${methodName}' differs across multiple method executions,
10
- does your status change by any circuit-unfriendly conditional logic?`,
11
- proverMissing: (methodName) => new Error(`Unable to find a provable method for '@method ${methodName}',
12
- did you forget to run chain.compile()?`),
13
- };
14
- /**
15
- * Runs a method wrapped in a method execution context.
16
- */
17
- export function runInContext(methodName, moduleMethod, args) {
18
- const executionContext = container.resolve(MethodExecutionContext);
19
- // eslint-disable-next-line @typescript-eslint/init-declarations
20
- let resultValue;
21
- executionContext.beforeMethod(methodName);
22
- try {
23
- resultValue = Reflect.apply(moduleMethod, this, args);
24
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
- }
26
- catch (error) {
27
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
28
- throw new Error(error);
29
- }
30
- finally {
31
- executionContext.afterMethod();
32
- }
33
- executionContext.setValue(resultValue);
34
- return executionContext.current();
35
- }
36
- export function toStateTransitionsHash(
37
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- stateTransitions) {
39
- const stateTransitionsHashList = new DefaultProvableHashList(ProvableStateTransition);
40
- return stateTransitions
41
- .map((stateTransition) => stateTransition.toProvable())
42
- .reduce((allStateTransitionsHashList, stateTransition) => allStateTransitionsHashList.push(stateTransition), stateTransitionsHashList)
43
- .toField();
44
- }
45
- export function toWrappedMethod(methodName, moduleMethod) {
46
- const wrappedMethod = (publicInput, ...args) => {
47
- const { result: { stateTransitions, status, value }, } = Reflect.apply(runInContext, this, [methodName, moduleMethod, args]);
48
- const stateTransitionsHash = toStateTransitionsHash(stateTransitions);
49
- publicInput.stateTransitionsHash.assertEquals(stateTransitionsHash, errors.inconsistentStateTransitions(methodName));
50
- // eslint-disable-next-line no-warning-comments
51
- // TODO: implement the transactionHash commitment
52
- publicInput.transactionHash.assertEquals(Field(0));
53
- publicInput.status.assertEquals(status, errors.inconsistentExecutionStatus(methodName));
54
- return value;
55
- };
56
- Object.defineProperty(wrappedMethod, "name", {
57
- value: `wrapped_${methodName}`,
58
- writable: false,
59
- });
60
- return wrappedMethod;
61
- }
62
- export function combineMethodName(runtimeModuleName, methodName) {
63
- return `${runtimeModuleName}.${methodName}`;
64
- }
65
- /**
66
- * Precomputes the public inputs required to run
67
- * an actual wrapped method, produced from the provided moduleMethod.
68
- *
69
- * Execute the wrapped method with the precomputed public inputs.
70
- */
71
- export function runWithCommitments(methodName, moduleMethod, args) {
72
- const executionContext = container.resolve(MethodExecutionContext);
73
- const wrappedMethod = Reflect.apply(toWrappedMethod, this, [
74
- methodName,
75
- moduleMethod,
76
- ]);
77
- const { result: { stateTransitions, status }, } = Reflect.apply(runInContext, this, [methodName, moduleMethod, args]);
78
- const stateTransitionsHash = toStateTransitionsHash(stateTransitions);
79
- const methodPublicInput = {
80
- stateTransitionsHash,
81
- transactionHash: Field(0),
82
- status,
83
- };
84
- executionContext.setPublicInput(methodPublicInput);
85
- // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
86
- if (this.runtime?.areProofsEnabled) {
87
- const runtimeModuleName = this.constructor.name;
88
- const combinedMethodName = combineMethodName(runtimeModuleName, methodName);
89
- const provableMethod = this.runtime.program?.[combinedMethodName];
90
- if (!provableMethod) {
91
- throw errors.proverMissing(methodName);
92
- }
93
- const prove = async () => await Reflect.apply(provableMethod, this, [methodPublicInput, ...args]);
94
- const result = wrappedMethod(methodPublicInput, ...args);
95
- executionContext.setProve(prove);
96
- return result;
97
- }
98
- return wrappedMethod(methodPublicInput, ...args);
99
- }
100
- export const methodMetadataKey = "yab-method";
101
- /**
102
- * Checks the metadata of the provided runtime module and its method,
103
- * to see if it has been decorated with @method()
104
- *
105
- * @param target - Runtime module to check
106
- * @param propertyKey - Name of the method to check in the prior runtime module
107
- * @returns - If the provided method name is a runtime method or not
108
- */
109
- export function isMethod(target, propertyKey) {
110
- return Boolean(Reflect.getMetadata(methodMetadataKey, target, propertyKey));
111
- }
112
- /**
113
- * Decorates a runtime module method and toggles execution of
114
- * either of its variants, depending on the state of the current
115
- * method execution context. If the method is called at the 'top-level',
116
- * it is executed 'with commitments'. If the method is called from another
117
- * method, it's executed directly.
118
- *
119
- * Additionally the method is marked with metadata as a 'runtime module method'.
120
- *
121
- * @returns A decorated runtime module method
122
- */
123
- export function method() {
124
- return (target, propertyKey, descriptor) => {
125
- const executionContext = container.resolve(MethodExecutionContext);
126
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
127
- const originalFunction = descriptor.value;
128
- Reflect.defineMetadata(methodMetadataKey, true, target, propertyKey);
129
- descriptor.value = function value(...args) {
130
- if (executionContext.isTopLevel) {
131
- return Reflect.apply(runWithCommitments, this, [
132
- propertyKey,
133
- originalFunction,
134
- args,
135
- ]);
136
- }
137
- return Reflect.apply(originalFunction, this, args);
138
- };
139
- };
140
- }
@@ -1,65 +0,0 @@
1
- import { Field, type FlexibleProvablePure } from "snarkyjs";
2
- import { Option, type Path } from "@proto-kit/protocol";
3
- import { PartialRuntime } from "../runtime/RuntimeModule.js";
4
- export declare class WithPath {
5
- path?: Field;
6
- hasPathOrFail(): asserts this is {
7
- path: Path;
8
- };
9
- }
10
- export declare class WithRuntime {
11
- runtime?: PartialRuntime;
12
- hasRuntimeOrFail(): asserts this is {
13
- runtime: PartialRuntime;
14
- };
15
- }
16
- declare const State_base: import("ts-mixer/dist/types/types.js").Class<any[], WithPath & WithRuntime, typeof WithPath & typeof WithRuntime, false>;
17
- /**
18
- * Utilities for runtime module state, such as get/set
19
- */
20
- export declare class State<Value> extends State_base {
21
- valueType: FlexibleProvablePure<Value>;
22
- /**
23
- * Creates a new state wrapper for the provided value type.
24
- *
25
- * @param valueType - Type of value to be stored (e.g. UInt64, Struct, ...)
26
- * @returns New state for the given value type.
27
- */
28
- static from<Value>(valueType: FlexibleProvablePure<Value>): State<Value>;
29
- /**
30
- * Computes a dummy value for the given value type.
31
- *
32
- * @param valueType - Value type to generate the dummy value for
33
- * @returns Dummy value for the given value type
34
- */
35
- static dummyValue<Value>(valueType: FlexibleProvablePure<Value>): Value;
36
- constructor(valueType: FlexibleProvablePure<Value>);
37
- /**
38
- * Provides an in-circuit witness for the current state representation,
39
- * and constructs an Option out of it.
40
- *
41
- * @returns Optional value of the current state
42
- */
43
- private witnessState;
44
- /**
45
- * Retrieves the current state and creates a state transition
46
- * anchoring the use of the current state value in the circuit.
47
- *
48
- * @returns Option representation of the current state.
49
- */
50
- get(): Option<Value>;
51
- /**
52
- * Sets a new state value by creating a state transition from
53
- * the current value to the newly set value.
54
- *
55
- * The newly set value isn't available via state.get(), since the
56
- * state transitions are not applied within the same circuit.
57
- * You can however store and access your new value in
58
- * a separate circuit variable.
59
- *
60
- * @param value - Value to be set as the current state
61
- */
62
- set(value: Value): void;
63
- }
64
- export {};
65
- //# sourceMappingURL=State.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"State.d.ts","sourceRoot":"","sources":["../../src/state/State.ts"],"names":[],"mappings":"AAGA,OAAO,EAAQ,KAAK,EAAY,KAAK,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAE5E,OAAO,EAAE,MAAM,EAAmB,KAAK,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAEzE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAG7D,qBAAa,QAAQ;IACZ,IAAI,CAAC,EAAE,KAAK,CAAC;IAEb,aAAa,IAAI,OAAO,CAAC,IAAI,IAAI;QAAE,IAAI,EAAE,IAAI,CAAA;KAAE;CAOvD;AAED,qBAAa,WAAW;IACf,OAAO,CAAC,EAAE,cAAc,CAAC;IAEzB,gBAAgB,IAAI,OAAO,CAAC,IAAI,IAAI;QACzC,OAAO,EAAE,cAAc,CAAC;KACzB;CAOF;;AAED;;GAEG;AACH,qBAAa,KAAK,CAAC,KAAK,CAAE,SAAQ,UAA4B;IA2BlC,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IA1BhE;;;;;OAKG;WACW,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAIhE;;;;;OAKG;WACW,UAAU,CAAC,KAAK,EAC5B,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC,GACrC,KAAK;gBAQkB,SAAS,EAAE,oBAAoB,CAAC,KAAK,CAAC;IAIhE;;;;;OAKG;IACH,OAAO,CAAC,YAAY;IA6BpB;;;;;OAKG;IACI,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC;IAc3B;;;;;;;;;;OAUG;IACI,GAAG,CAAC,KAAK,EAAE,KAAK;CAiBxB"}