@proto-kit/module 0.1.1-develop.164 → 0.1.1-develop.185

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 (37) 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 +36 -0
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1 -0
  7. package/dist/method/runtimeMethod.d.ts +1 -0
  8. package/dist/method/runtimeMethod.d.ts.map +1 -1
  9. package/dist/method/runtimeMethod.js +10 -0
  10. package/dist/runtime/MethodIdResolver.d.ts +18 -0
  11. package/dist/runtime/MethodIdResolver.d.ts.map +1 -0
  12. package/dist/runtime/MethodIdResolver.js +50 -0
  13. package/dist/runtime/Runtime.d.ts +4 -4
  14. package/dist/runtime/Runtime.d.ts.map +1 -1
  15. package/dist/runtime/Runtime.js +21 -20
  16. package/dist/runtime/RuntimeModule.d.ts +6 -0
  17. package/dist/runtime/RuntimeModule.d.ts.map +1 -1
  18. package/dist/runtime/RuntimeModule.js +14 -2
  19. package/package.json +5 -5
  20. package/src/factories/MethodIdFactory.ts +22 -0
  21. package/src/index.ts +1 -0
  22. package/src/method/runtimeMethod.ts +14 -1
  23. package/src/runtime/MethodIdResolver.ts +71 -0
  24. package/src/runtime/Runtime.ts +28 -29
  25. package/src/runtime/RuntimeModule.ts +18 -0
  26. package/test/Runtime.test.ts +21 -14
  27. package/test/modules/Balances.ts +5 -0
  28. package/test/modules/methodId.test.ts +85 -0
  29. package/dist/chain/Chain.d.ts +0 -109
  30. package/dist/chain/Chain.d.ts.map +0 -1
  31. package/dist/chain/Chain.js +0 -229
  32. package/dist/method/MethodExecutionContext.d.ts +0 -73
  33. package/dist/method/MethodExecutionContext.d.ts.map +0 -1
  34. package/dist/method/MethodExecutionContext.js +0 -112
  35. package/dist/method/decorator.d.ts +0 -45
  36. package/dist/method/decorator.d.ts.map +0 -1
  37. package/dist/method/decorator.js +0 -140
@@ -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,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
- }