@proto-kit/module 0.1.1-develop.0
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.
- package/LICENSE.md +201 -0
- package/README.md +114 -0
- package/dist/factories/MethodIdFactory.d.ts +10 -0
- package/dist/factories/MethodIdFactory.d.ts.map +1 -0
- package/dist/factories/MethodIdFactory.js +11 -0
- package/dist/factories/MethodIdFactory.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/method/MethodParameterEncoder.d.ts +26 -0
- package/dist/method/MethodParameterEncoder.d.ts.map +1 -0
- package/dist/method/MethodParameterEncoder.js +169 -0
- package/dist/method/MethodParameterEncoder.js.map +1 -0
- package/dist/method/runtimeMethod.d.ts +33 -0
- package/dist/method/runtimeMethod.d.ts.map +1 -0
- package/dist/method/runtimeMethod.js +169 -0
- package/dist/method/runtimeMethod.js.map +1 -0
- package/dist/module/decorator.d.ts +8 -0
- package/dist/module/decorator.d.ts.map +1 -0
- package/dist/module/decorator.js +16 -0
- package/dist/module/decorator.js.map +1 -0
- package/dist/runtime/MethodIdResolver.d.ts +20 -0
- package/dist/runtime/MethodIdResolver.d.ts.map +1 -0
- package/dist/runtime/MethodIdResolver.js +91 -0
- package/dist/runtime/MethodIdResolver.js.map +1 -0
- package/dist/runtime/Runtime.d.ts +72 -0
- package/dist/runtime/Runtime.d.ts.map +1 -0
- package/dist/runtime/Runtime.js +231 -0
- package/dist/runtime/Runtime.js.map +1 -0
- package/dist/runtime/RuntimeEnvironment.d.ts +10 -0
- package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -0
- package/dist/runtime/RuntimeEnvironment.js +2 -0
- package/dist/runtime/RuntimeEnvironment.js.map +1 -0
- package/dist/runtime/RuntimeModule.d.ts +37 -0
- package/dist/runtime/RuntimeModule.d.ts.map +1 -0
- package/dist/runtime/RuntimeModule.js +80 -0
- package/dist/runtime/RuntimeModule.js.map +1 -0
- package/dist/state/InMemoryStateService.d.ts +15 -0
- package/dist/state/InMemoryStateService.d.ts.map +1 -0
- package/dist/state/InMemoryStateService.js +24 -0
- package/dist/state/InMemoryStateService.js.map +1 -0
- package/dist/state/decorator.d.ts +7 -0
- package/dist/state/decorator.d.ts.map +1 -0
- package/dist/state/decorator.js +40 -0
- package/dist/state/decorator.js.map +1 -0
- package/jest.config.cjs +12 -0
- package/package.json +35 -0
- package/src/factories/MethodIdFactory.ts +13 -0
- package/src/index.ts +10 -0
- package/src/method/MethodParameterEncoder.ts +260 -0
- package/src/method/runtimeMethod.ts +308 -0
- package/src/module/decorator.ts +21 -0
- package/src/runtime/MethodIdResolver.ts +108 -0
- package/src/runtime/Runtime.ts +395 -0
- package/src/runtime/RuntimeEnvironment.ts +16 -0
- package/src/runtime/RuntimeModule.ts +112 -0
- package/src/state/InMemoryStateService.ts +25 -0
- package/src/state/decorator.ts +61 -0
- package/test/Runtime.test.ts +70 -0
- package/test/TestingRuntime.ts +45 -0
- package/test/method/MethodParameterEncoder.test.ts +121 -0
- package/test/method/runtimeMethod-fail.test.ts +50 -0
- package/test/method/runtimeMethod.test.ts +46 -0
- package/test/modules/Admin.ts +19 -0
- package/test/modules/Balances.test.ts +337 -0
- package/test/modules/Balances.ts +54 -0
- package/test/modules/MethodIdResolver.test.ts +73 -0
- package/test/modules/State.test.ts +81 -0
- package/test/runtimeMethod.test.ts +215 -0
- package/test/tsconfig.json +7 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Bool, Field, Poseidon } from "o1js";
|
|
2
|
+
import { container } from "tsyringe";
|
|
3
|
+
import { ProvableStateTransition, MethodPublicOutput, RuntimeMethodExecutionContext, StateTransitionReductionList, DefaultProvableHashList, } from "@proto-kit/protocol";
|
|
4
|
+
import { toProver, } from "@proto-kit/common";
|
|
5
|
+
import { MethodParameterEncoder, checkArgsProvable, } from "./MethodParameterEncoder";
|
|
6
|
+
const errors = {
|
|
7
|
+
runtimeNotProvided: (name) => new Error(`Runtime was not provided for module: ${name}`),
|
|
8
|
+
methodInputsNotProvided: () => new Error("Method execution inputs not provided, provide them via context.inputs"),
|
|
9
|
+
runtimeNameNotSet: () => new Error("Runtime name was not set"),
|
|
10
|
+
fieldNotConstant: (name) => new Error(`In-circuit field ${name} not a constant, this is likely a framework bug`),
|
|
11
|
+
};
|
|
12
|
+
export function toStateTransitionsHash(stateTransitions) {
|
|
13
|
+
const stateTransitionsHashList = new StateTransitionReductionList(ProvableStateTransition);
|
|
14
|
+
return stateTransitions
|
|
15
|
+
.map((stateTransition) => stateTransition.toProvable())
|
|
16
|
+
.reduce((allStateTransitionsHashList, stateTransition) => allStateTransitionsHashList.push(stateTransition), stateTransitionsHashList)
|
|
17
|
+
.toField();
|
|
18
|
+
}
|
|
19
|
+
export function toEventsHash(events) {
|
|
20
|
+
return events.reduce((acc, event) => {
|
|
21
|
+
const hashList = new DefaultProvableHashList(event.eventType, acc);
|
|
22
|
+
hashList.pushIf(event.event, event.condition);
|
|
23
|
+
return hashList.commitment;
|
|
24
|
+
}, Field(0));
|
|
25
|
+
}
|
|
26
|
+
export function toWrappedMethod(methodName, moduleMethod, options) {
|
|
27
|
+
const executionContext = container.resolve(RuntimeMethodExecutionContext);
|
|
28
|
+
const wrappedMethod = async (...args) => {
|
|
29
|
+
await Reflect.apply(moduleMethod, this, args);
|
|
30
|
+
const { result: { stateTransitions, status, events }, } = executionContext.current();
|
|
31
|
+
const stateTransitionsHash = toStateTransitionsHash(stateTransitions);
|
|
32
|
+
const eventsHash = toEventsHash(events);
|
|
33
|
+
const { name, runtime } = this;
|
|
34
|
+
if (name === undefined) {
|
|
35
|
+
throw errors.runtimeNameNotSet();
|
|
36
|
+
}
|
|
37
|
+
if (runtime === undefined) {
|
|
38
|
+
throw errors.runtimeNotProvided(name);
|
|
39
|
+
}
|
|
40
|
+
const { transaction, networkState } = executionContext.witnessInput();
|
|
41
|
+
const { methodIdResolver } = runtime;
|
|
42
|
+
// Assert that the given transaction has the correct methodId
|
|
43
|
+
const thisMethodId = Field(methodIdResolver.getMethodId(name, methodName));
|
|
44
|
+
if (!thisMethodId.isConstant()) {
|
|
45
|
+
throw errors.fieldNotConstant("methodId");
|
|
46
|
+
}
|
|
47
|
+
transaction.methodId.assertEquals(thisMethodId, "Runtimemethod called with wrong methodId on the transaction object");
|
|
48
|
+
/**
|
|
49
|
+
* Use the type info obtained previously to convert
|
|
50
|
+
* the args passed to fields
|
|
51
|
+
*/
|
|
52
|
+
const { fields } = MethodParameterEncoder.fromMethod(this, methodName).encode(args);
|
|
53
|
+
// Assert that the argsHash that has been signed matches the given arguments
|
|
54
|
+
const argsHash = Poseidon.hash(fields);
|
|
55
|
+
transaction.argsHash.assertEquals(argsHash, "argsHash and therefore arguments of transaction and runtime call does not match");
|
|
56
|
+
const isMessage = Bool(options.invocationType === "INCOMING_MESSAGE");
|
|
57
|
+
transaction.assertTransactionType(Bool(isMessage));
|
|
58
|
+
const transactionHash = transaction.hash();
|
|
59
|
+
const networkStateHash = networkState.hash();
|
|
60
|
+
return new MethodPublicOutput({
|
|
61
|
+
stateTransitionsHash,
|
|
62
|
+
status,
|
|
63
|
+
transactionHash,
|
|
64
|
+
networkStateHash,
|
|
65
|
+
isMessage,
|
|
66
|
+
eventsHash,
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
Object.defineProperty(wrappedMethod, "name", {
|
|
70
|
+
value: `wrapped_${methodName}`,
|
|
71
|
+
writable: false,
|
|
72
|
+
});
|
|
73
|
+
return wrappedMethod;
|
|
74
|
+
}
|
|
75
|
+
export function combineMethodName(runtimeModuleName, methodName) {
|
|
76
|
+
return `${runtimeModuleName}.${methodName}`;
|
|
77
|
+
}
|
|
78
|
+
export const runtimeMethodMetadataKey = "yab-method";
|
|
79
|
+
export const runtimeMethodNamesMetadataKey = "proto-kit-runtime-methods";
|
|
80
|
+
export const runtimeMethodTypeMetadataKey = "proto-kit-runtime-method-type";
|
|
81
|
+
/**
|
|
82
|
+
* Checks the metadata of the provided runtime module and its method,
|
|
83
|
+
* to see if it has been decorated with @runtimeMethod()
|
|
84
|
+
*
|
|
85
|
+
* @param target - Runtime module to check
|
|
86
|
+
* @param propertyKey - Name of the method to check in the prior runtime module
|
|
87
|
+
* @returns - If the provided method name is a runtime method or not
|
|
88
|
+
*/
|
|
89
|
+
export function isRuntimeMethod(target, propertyKey) {
|
|
90
|
+
return Boolean(Reflect.getMetadata(runtimeMethodMetadataKey, target, propertyKey));
|
|
91
|
+
}
|
|
92
|
+
function runtimeMethodInternal(options) {
|
|
93
|
+
return (target, methodName, descriptor) => {
|
|
94
|
+
checkArgsProvable(target, methodName);
|
|
95
|
+
const executionContext = container.resolve(RuntimeMethodExecutionContext);
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
97
|
+
let data = Reflect.getMetadata(runtimeMethodNamesMetadataKey, target);
|
|
98
|
+
if (data !== undefined) {
|
|
99
|
+
data.push(methodName);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
data = [methodName];
|
|
103
|
+
}
|
|
104
|
+
Reflect.defineMetadata(runtimeMethodNamesMetadataKey, data, target);
|
|
105
|
+
Reflect.defineMetadata(runtimeMethodMetadataKey, true, target, methodName);
|
|
106
|
+
Reflect.defineMetadata(runtimeMethodTypeMetadataKey, options.invocationType, target, methodName);
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
108
|
+
const simulatedMethod = descriptor.value;
|
|
109
|
+
descriptor.value = async function value(...args) {
|
|
110
|
+
const constructorName = this.name;
|
|
111
|
+
/**
|
|
112
|
+
* If its a top level method call, wrap it into a wrapped method,
|
|
113
|
+
* since it'll be turned into a real/mock prover in provableMethod().
|
|
114
|
+
*
|
|
115
|
+
* Otherwise provableMethod() will just call the originalMethod provided
|
|
116
|
+
* if method is not called at the top level.
|
|
117
|
+
*/
|
|
118
|
+
const simulatedWrappedMethod = Reflect.apply(toWrappedMethod, this, [
|
|
119
|
+
methodName,
|
|
120
|
+
simulatedMethod,
|
|
121
|
+
options,
|
|
122
|
+
]);
|
|
123
|
+
/**
|
|
124
|
+
* Before the prover runs, make sure it is operating on the correct
|
|
125
|
+
* RuntimeMethodExecutionContext state, meaning it enters and exits
|
|
126
|
+
* the context properly.
|
|
127
|
+
*/
|
|
128
|
+
async function prover() {
|
|
129
|
+
executionContext.beforeMethod(constructorName, methodName, args);
|
|
130
|
+
const innerProver = toProver(combineMethodName(constructorName, methodName), simulatedWrappedMethod, false, ...args).bind(this);
|
|
131
|
+
let result;
|
|
132
|
+
try {
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
134
|
+
result = await Reflect.apply(innerProver, this, args);
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
executionContext.afterMethod();
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
executionContext.beforeMethod(constructorName, methodName, args);
|
|
142
|
+
if (executionContext.isTopLevel) {
|
|
143
|
+
if (!this.runtime) {
|
|
144
|
+
throw errors.runtimeNotProvided(constructorName);
|
|
145
|
+
}
|
|
146
|
+
executionContext.setProver(prover.bind(this.runtime.zkProgrammable));
|
|
147
|
+
}
|
|
148
|
+
let result;
|
|
149
|
+
try {
|
|
150
|
+
result = await Reflect.apply(simulatedMethod, this, args);
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
executionContext.afterMethod();
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export function runtimeMessage() {
|
|
160
|
+
return runtimeMethodInternal({
|
|
161
|
+
invocationType: "INCOMING_MESSAGE",
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
export function runtimeMethod() {
|
|
165
|
+
return runtimeMethodInternal({
|
|
166
|
+
invocationType: "SIGNATURE",
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=runtimeMethod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtimeMethod.js","sourceRoot":"","sources":["../../src/method/runtimeMethod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAwB,QAAQ,EAAE,MAAM,MAAM,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAEL,uBAAuB,EACvB,kBAAkB,EAClB,6BAA6B,EAC7B,4BAA4B,EAC5B,uBAAuB,GACxB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,QAAQ,GAGT,MAAM,mBAAmB,CAAC;AAI3B,OAAO,EACL,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,0BAA0B,CAAC;AAElC,MAAM,MAAM,GAAG;IACb,kBAAkB,EAAE,CAAC,IAAY,EAAE,EAAE,CACnC,IAAI,KAAK,CAAC,wCAAwC,IAAI,EAAE,CAAC;IAE3D,uBAAuB,EAAE,GAAG,EAAE,CAC5B,IAAI,KAAK,CACP,uEAAuE,CACxE;IAEH,iBAAiB,EAAE,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,0BAA0B,CAAC;IAE9D,gBAAgB,EAAE,CAAC,IAAY,EAAE,EAAE,CACjC,IAAI,KAAK,CACP,oBAAoB,IAAI,iDAAiD,CAC1E;CACJ,CAAC;AAEF,MAAM,UAAU,sBAAsB,CACpC,gBAAwC;IAExC,MAAM,wBAAwB,GAAG,IAAI,4BAA4B,CAC/D,uBAAuB,CACxB,CAAC;IAEF,OAAO,gBAAgB;SACpB,GAAG,CAAC,CAAC,eAAe,EAAE,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,CAAC;SACtD,MAAM,CACL,CAAC,2BAA2B,EAAE,eAAe,EAAE,EAAE,CAC/C,2BAA2B,CAAC,IAAI,CAAC,eAAe,CAAC,EACnD,wBAAwB,CACzB;SACA,OAAO,EAAE,CAAC;AAC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,CAAC,CAAC;aAC3D;oBAAS;gBACR,gBAAgB,CAAC,WAAW,EAAE,CAAC;aAChC;YAED,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,cAAc;IAC5B,OAAO,qBAAqB,CAAC;QAC3B,cAAc,EAAE,kBAAkB;KACnC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,aAAa;IAC3B,OAAO,qBAAqB,CAAC;QAC3B,cAAc,EAAE,WAAW;KAC5B,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { StaticConfigurableModule, TypedClass } from "@proto-kit/common";
|
|
2
|
+
import { RuntimeModule } from "../runtime/RuntimeModule.js";
|
|
3
|
+
/**
|
|
4
|
+
* Marks the decorated class as a runtime module, while also
|
|
5
|
+
* making it injectable with our dependency injection solution.
|
|
6
|
+
*/
|
|
7
|
+
export declare function runtimeModule(): (target: StaticConfigurableModule<unknown> & TypedClass<RuntimeModule<unknown>>) => void;
|
|
8
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorator.d.ts","sourceRoot":"","sources":["../../src/module/decorator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEzE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D;;;GAGG;AACH,wBAAgB,aAAa,aAMjB,yBAAyB,OAAO,CAAC,GACvC,WAAW,cAAc,OAAO,CAAC,CAAC,UAIvC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { injectable } from "tsyringe";
|
|
2
|
+
/**
|
|
3
|
+
* Marks the decorated class as a runtime module, while also
|
|
4
|
+
* making it injectable with our dependency injection solution.
|
|
5
|
+
*/
|
|
6
|
+
export function runtimeModule() {
|
|
7
|
+
return (
|
|
8
|
+
/**
|
|
9
|
+
* Check if the target class extends RuntimeModule, while
|
|
10
|
+
* also providing static config presets
|
|
11
|
+
*/
|
|
12
|
+
target) => {
|
|
13
|
+
injectable()(target);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorator.js","sourceRoot":"","sources":["../../src/module/decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAKtC;;;GAGG;AACH,MAAM,UAAU,aAAa;IAC3B,OAAO;IACL;;;OAGG;IACH,MACoC,EACpC,EAAE;QACF,UAAU,EAAE,CAAC,MAAM,CAAC,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { RuntimeMethodIdMapping } from "@proto-kit/protocol";
|
|
2
|
+
import type { Runtime, RuntimeModulesRecord } from "./Runtime";
|
|
3
|
+
/**
|
|
4
|
+
* Please see `getMethodId` to learn more about
|
|
5
|
+
* methodId encoding
|
|
6
|
+
*/
|
|
7
|
+
export declare class MethodIdResolver {
|
|
8
|
+
private readonly runtime;
|
|
9
|
+
private readonly dictionary;
|
|
10
|
+
constructor(runtime: Runtime<RuntimeModulesRecord>);
|
|
11
|
+
/**
|
|
12
|
+
* The purpose of this method is to provide a dictionary where
|
|
13
|
+
* we can look up properties like methodId and invocationType
|
|
14
|
+
* for each runtimeMethod using their module name and method name
|
|
15
|
+
*/
|
|
16
|
+
methodIdMap(): RuntimeMethodIdMapping;
|
|
17
|
+
getMethodNameFromId(methodId: bigint): [string, string] | undefined;
|
|
18
|
+
getMethodId(moduleName: string, methodName: string): bigint;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=MethodIdResolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MethodIdResolver.d.ts","sourceRoot":"","sources":["../../src/runtime/MethodIdResolver.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAS5E,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAE/D;;;GAGG;AACH,qBACa,gBAAgB;IAMN,OAAO,CAAC,QAAQ,CAAC,OAAO;IAL7C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAEpB;gBAG+B,OAAO,EAAE,OAAO,CAAC,oBAAoB,CAAC;IAkB5E;;;;OAIG;IACI,WAAW,IAAI,sBAAsB;IAuCrC,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS;IAcnE,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM;CAQnE"}
|
|
@@ -0,0 +1,91 @@
|
|
|
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 { filterNonUndefined } from "@proto-kit/common";
|
|
14
|
+
import { stringToField } from "@proto-kit/protocol";
|
|
15
|
+
import { Poseidon } from "o1js";
|
|
16
|
+
import { inject, injectable } from "tsyringe";
|
|
17
|
+
import { runtimeMethodTypeMetadataKey, } from "../method/runtimeMethod";
|
|
18
|
+
/**
|
|
19
|
+
* Please see `getMethodId` to learn more about
|
|
20
|
+
* methodId encoding
|
|
21
|
+
*/
|
|
22
|
+
export let MethodIdResolver = class MethodIdResolver {
|
|
23
|
+
constructor(runtime) {
|
|
24
|
+
this.runtime = runtime;
|
|
25
|
+
this.dictionary = {};
|
|
26
|
+
this.dictionary = runtime.runtimeModuleNames.reduce((dict, moduleName) => {
|
|
27
|
+
this.runtime.assertIsValidModuleName(moduleName);
|
|
28
|
+
runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
|
|
29
|
+
dict[this.getMethodId(moduleName, methodName).toString()] = {
|
|
30
|
+
moduleName,
|
|
31
|
+
methodName,
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
return dict;
|
|
35
|
+
}, {});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The purpose of this method is to provide a dictionary where
|
|
39
|
+
* we can look up properties like methodId and invocationType
|
|
40
|
+
* for each runtimeMethod using their module name and method name
|
|
41
|
+
*/
|
|
42
|
+
methodIdMap() {
|
|
43
|
+
const methodIdResolver = this.runtime.dependencyContainer.resolve("MethodIdResolver");
|
|
44
|
+
const rawMappings = this.runtime.moduleNames.flatMap((moduleName) => {
|
|
45
|
+
const module = this.runtime.resolve(moduleName);
|
|
46
|
+
return module.runtimeMethodNames.map((method) => {
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
48
|
+
const type = Reflect.getMetadata(runtimeMethodTypeMetadataKey, module, method);
|
|
49
|
+
if (type !== undefined) {
|
|
50
|
+
return {
|
|
51
|
+
name: `${moduleName}.${method}`,
|
|
52
|
+
methodId: methodIdResolver.getMethodId(moduleName, method),
|
|
53
|
+
type,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
return rawMappings
|
|
60
|
+
.filter(filterNonUndefined)
|
|
61
|
+
.reduce((acc, entry) => {
|
|
62
|
+
acc[entry.name] = {
|
|
63
|
+
methodId: entry.methodId,
|
|
64
|
+
type: entry.type,
|
|
65
|
+
};
|
|
66
|
+
return acc;
|
|
67
|
+
}, {});
|
|
68
|
+
}
|
|
69
|
+
getMethodNameFromId(methodId) {
|
|
70
|
+
const methodPath = this.dictionary[methodId.toString()];
|
|
71
|
+
if (methodPath === undefined) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const { moduleName, methodName } = methodPath;
|
|
75
|
+
this.runtime.assertIsValidModuleName(moduleName);
|
|
76
|
+
return [moduleName, methodName];
|
|
77
|
+
}
|
|
78
|
+
getMethodId(moduleName, methodName) {
|
|
79
|
+
this.runtime.assertIsValidModuleName(moduleName);
|
|
80
|
+
return Poseidon.hash([
|
|
81
|
+
stringToField(moduleName),
|
|
82
|
+
stringToField(methodName),
|
|
83
|
+
]).toBigInt();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
MethodIdResolver = __decorate([
|
|
87
|
+
injectable(),
|
|
88
|
+
__param(0, inject("Runtime")),
|
|
89
|
+
__metadata("design:paramtypes", [Function])
|
|
90
|
+
], MethodIdResolver);
|
|
91
|
+
//# sourceMappingURL=MethodIdResolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MethodIdResolver.js","sourceRoot":"","sources":["../../src/runtime/MethodIdResolver.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,aAAa,EAA0B,MAAM,qBAAqB,CAAC;AAC5E,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,EAEL,4B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AgB,CA0F5B"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ZkProgram } from "o1js";
|
|
2
|
+
import { DependencyContainer } from "tsyringe";
|
|
3
|
+
import { StringKeyOf, ModuleContainer, ModulesConfig, ModulesRecord, TypedClass, ZkProgrammable, PlainZkProgram, AreProofsEnabled, ChildContainerProvider, CompilableModule, CompileRegistry } from "@proto-kit/common";
|
|
4
|
+
import { MethodPublicOutput, StateServiceProvider, SimpleAsyncStateService } from "@proto-kit/protocol";
|
|
5
|
+
import { RuntimeModule } from "./RuntimeModule";
|
|
6
|
+
import { MethodIdResolver } from "./MethodIdResolver";
|
|
7
|
+
import { RuntimeEnvironment } from "./RuntimeEnvironment";
|
|
8
|
+
export declare function getAllPropertyNames(obj: any): (string | symbol)[];
|
|
9
|
+
/**
|
|
10
|
+
* Record of modules accepted by the Runtime module container.
|
|
11
|
+
*
|
|
12
|
+
* We have to use TypedClass since RuntimeModule
|
|
13
|
+
* is an abstract class
|
|
14
|
+
*/
|
|
15
|
+
export type RuntimeModulesRecord = ModulesRecord<TypedClass<RuntimeModule<unknown>>>;
|
|
16
|
+
/**
|
|
17
|
+
* Definition / required arguments for the Runtime class
|
|
18
|
+
*/
|
|
19
|
+
export interface RuntimeDefinition<Modules extends RuntimeModulesRecord> {
|
|
20
|
+
modules: Modules;
|
|
21
|
+
config?: ModulesConfig<Modules>;
|
|
22
|
+
}
|
|
23
|
+
export declare class RuntimeZkProgrammable<Modules extends RuntimeModulesRecord> extends ZkProgrammable<undefined, MethodPublicOutput> {
|
|
24
|
+
runtime: Runtime<Modules>;
|
|
25
|
+
constructor(runtime: Runtime<Modules>);
|
|
26
|
+
get areProofsEnabled(): AreProofsEnabled | undefined;
|
|
27
|
+
zkProgramFactory(): PlainZkProgram<undefined, MethodPublicOutput>[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Wrapper for an application specific runtime, which helps orchestrate
|
|
31
|
+
* runtime modules into an interoperable runtime.
|
|
32
|
+
*/
|
|
33
|
+
export declare class Runtime<Modules extends RuntimeModulesRecord> extends ModuleContainer<Modules> implements RuntimeEnvironment, CompilableModule {
|
|
34
|
+
static from<Modules extends RuntimeModulesRecord>(definition: RuntimeDefinition<Modules>): TypedClass<Runtime<Modules>>;
|
|
35
|
+
program?: ReturnType<typeof ZkProgram>;
|
|
36
|
+
definition: RuntimeDefinition<Modules>;
|
|
37
|
+
zkProgrammable: ZkProgrammable<undefined, MethodPublicOutput>;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new Runtime from the provided config
|
|
40
|
+
*
|
|
41
|
+
* @param modules - Configuration object for the constructed Runtime
|
|
42
|
+
*/
|
|
43
|
+
constructor(definition: RuntimeDefinition<Modules>);
|
|
44
|
+
create(childContainerProvider: ChildContainerProvider): void;
|
|
45
|
+
get areProofsEnabled(): AreProofsEnabled | undefined;
|
|
46
|
+
get stateServiceProvider(): StateServiceProvider;
|
|
47
|
+
get stateService(): SimpleAsyncStateService;
|
|
48
|
+
get methodIdResolver(): MethodIdResolver;
|
|
49
|
+
/**
|
|
50
|
+
* @returns The dependency injection container of this runtime
|
|
51
|
+
*/
|
|
52
|
+
get dependencyContainer(): DependencyContainer;
|
|
53
|
+
/**
|
|
54
|
+
* @param methodId The encoded name of the method to call.
|
|
55
|
+
* Encoding: "stringToField(module.name) << 128 + stringToField(method-name)"
|
|
56
|
+
*/
|
|
57
|
+
getMethodById(methodId: bigint): ((...args: unknown[]) => Promise<unknown>) | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Add a name and other respective properties required by RuntimeModules,
|
|
60
|
+
* that come from the current Runtime
|
|
61
|
+
*
|
|
62
|
+
* @param moduleName - Name of the runtime module to decorate
|
|
63
|
+
* @param containedModule
|
|
64
|
+
*/
|
|
65
|
+
decorateModule(moduleName: StringKeyOf<Modules>, containedModule: InstanceType<Modules[StringKeyOf<Modules>]>): void;
|
|
66
|
+
/**
|
|
67
|
+
* @returns A list of names of all the registered module names
|
|
68
|
+
*/
|
|
69
|
+
get runtimeModuleNames(): string[];
|
|
70
|
+
compile(registry: CompileRegistry): Promise<Record<string, import("@proto-kit/common").CompileArtifact>>;
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=Runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Runtime.d.ts","sourceRoot":"","sources":["../../src/runtime/Runtime.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAa,mBAAmB,EAAc,MAAM,UAAU,CAAC;AACtE,OAAO,EACL,WAAW,EACX,eAAe,EACf,aAAa,EACb,aAAa,EACb,UAAU,EACV,cAAc,EACd,cAAc,EACd,gBAAgB,EAChB,sBAAsB,EACtB,gBAAgB,EAChB,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,uBAAuB,EAIxB,MAAM,qBAAqB,CAAC;AAW7B,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,GAAG,uBAa3C;AAED;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG,aAAa,CAC9C,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CACnC,CAAC;AAOF;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,OAAO,SAAS,oBAAoB;IACrE,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;CACjC;AAED,qBAAa,qBAAqB,CAChC,OAAO,SAAS,oBAAoB,CACpC,SAAQ,cAAc,CAAC,SAAS,EAAE,kBAAkB,CAAC;IAC3B,OAAO,EAAE,OAAO,CAAC,OAAO,CAA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W,kBAAkB,aAE5B;IAEY,OAAO,CAAC,QAAQ,EAAE,eAAe;CAQ/C"}
|
|
@@ -0,0 +1,231 @@
|
|
|
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 Runtime_1;
|
|
11
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument */
|
|
12
|
+
import { ZkProgram } from "o1js";
|
|
13
|
+
import { container, injectable } from "tsyringe";
|
|
14
|
+
import { ModuleContainer, ZkProgrammable, } from "@proto-kit/common";
|
|
15
|
+
import { MethodPublicOutput, RuntimeMethodExecutionContext, RuntimeTransaction, NetworkState, } from "@proto-kit/protocol";
|
|
16
|
+
import { combineMethodName, isRuntimeMethod, runtimeMethodTypeMetadataKey, toWrappedMethod, } from "../method/runtimeMethod";
|
|
17
|
+
import { MethodIdFactory } from "../factories/MethodIdFactory";
|
|
18
|
+
export function getAllPropertyNames(obj) {
|
|
19
|
+
let currentPrototype = obj;
|
|
20
|
+
let keys = [];
|
|
21
|
+
// if primitive (primitives still have keys) skip the first iteration
|
|
22
|
+
if (!(obj instanceof Object)) {
|
|
23
|
+
currentPrototype = Object.getPrototypeOf(obj);
|
|
24
|
+
}
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
26
|
+
while (currentPrototype) {
|
|
27
|
+
keys = keys.concat(Reflect.ownKeys(currentPrototype));
|
|
28
|
+
currentPrototype = Object.getPrototypeOf(currentPrototype);
|
|
29
|
+
}
|
|
30
|
+
return keys;
|
|
31
|
+
}
|
|
32
|
+
const errors = {
|
|
33
|
+
methodNotFound: (methodKey) => new Error(`Unable to find method with id ${methodKey}`),
|
|
34
|
+
};
|
|
35
|
+
export class RuntimeZkProgrammable extends ZkProgrammable {
|
|
36
|
+
constructor(runtime) {
|
|
37
|
+
super();
|
|
38
|
+
this.runtime = runtime;
|
|
39
|
+
}
|
|
40
|
+
get areProofsEnabled() {
|
|
41
|
+
return this.runtime.areProofsEnabled;
|
|
42
|
+
}
|
|
43
|
+
zkProgramFactory() {
|
|
44
|
+
// We need to use explicit type annotations here,
|
|
45
|
+
// therefore we can't use destructuring
|
|
46
|
+
// eslint-disable-next-line prefer-destructuring
|
|
47
|
+
const runtime = this.runtime;
|
|
48
|
+
const MAXIMUM_METHODS_PER_ZK_PROGRAM = 8;
|
|
49
|
+
const runtimeMethods = runtime.runtimeModuleNames.reduce((allMethods, runtimeModuleName) => {
|
|
50
|
+
runtime.isValidModuleName(runtime.definition.modules, runtimeModuleName);
|
|
51
|
+
/**
|
|
52
|
+
* Couldnt find a better way to circumvent the type assertion
|
|
53
|
+
* regarding resolving only known modules. We assert in the line above
|
|
54
|
+
* but we cast it to any anyways to satisfy the proof system.
|
|
55
|
+
*/
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
57
|
+
const runtimeModule = runtime.resolve(runtimeModuleName);
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
59
|
+
const modulePrototype = Object.getPrototypeOf(runtimeModule);
|
|
60
|
+
const modulePrototypeMethods = getAllPropertyNames(runtimeModule).map((method) => method.toString());
|
|
61
|
+
const moduleMethods = modulePrototypeMethods.reduce((allModuleMethods, methodName) => {
|
|
62
|
+
if (isRuntimeMethod(runtimeModule, methodName)) {
|
|
63
|
+
const combinedMethodName = combineMethodName(runtimeModuleName, methodName);
|
|
64
|
+
const method = modulePrototype[methodName];
|
|
65
|
+
const invocationType = Reflect.getMetadata(runtimeMethodTypeMetadataKey, runtimeModule, methodName);
|
|
66
|
+
const wrappedMethod = Reflect.apply(toWrappedMethod, runtimeModule, [methodName, method, { invocationType }]);
|
|
67
|
+
const privateInputs = Reflect.getMetadata("design:paramtypes", runtimeModule, methodName);
|
|
68
|
+
return {
|
|
69
|
+
...allModuleMethods,
|
|
70
|
+
[combinedMethodName]: {
|
|
71
|
+
privateInputs,
|
|
72
|
+
method: wrappedMethod,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return allModuleMethods;
|
|
77
|
+
}, {});
|
|
78
|
+
return {
|
|
79
|
+
...allMethods,
|
|
80
|
+
...moduleMethods,
|
|
81
|
+
};
|
|
82
|
+
}, {});
|
|
83
|
+
const sortedRuntimeMethods = Object.fromEntries(Object.entries(runtimeMethods).sort());
|
|
84
|
+
const splitRuntimeMethods = () => {
|
|
85
|
+
const buckets = [];
|
|
86
|
+
Object.entries(sortedRuntimeMethods).forEach(async ([methodName, method]) => {
|
|
87
|
+
let methodAdded = false;
|
|
88
|
+
for (const bucket of buckets) {
|
|
89
|
+
if (buckets.length === 0) {
|
|
90
|
+
const record = {};
|
|
91
|
+
record[methodName] = method;
|
|
92
|
+
buckets.push(record);
|
|
93
|
+
methodAdded = true;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
else if (Object.keys(bucket).length <=
|
|
97
|
+
MAXIMUM_METHODS_PER_ZK_PROGRAM - 1) {
|
|
98
|
+
bucket[methodName] = method;
|
|
99
|
+
methodAdded = true;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!methodAdded) {
|
|
104
|
+
const record = {};
|
|
105
|
+
record[methodName] = method;
|
|
106
|
+
buckets.push(record);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return buckets;
|
|
110
|
+
};
|
|
111
|
+
return splitRuntimeMethods().map((bucket, index) => {
|
|
112
|
+
const name = `RuntimeProgram-${index}`;
|
|
113
|
+
const program = ZkProgram({
|
|
114
|
+
name,
|
|
115
|
+
publicOutput: MethodPublicOutput,
|
|
116
|
+
methods: bucket,
|
|
117
|
+
});
|
|
118
|
+
const SelfProof = ZkProgram.Proof(program);
|
|
119
|
+
const methods = Object.keys(bucket).reduce((boundMethods, methodName) => {
|
|
120
|
+
boundMethods[methodName] = program[methodName].bind(program);
|
|
121
|
+
return boundMethods;
|
|
122
|
+
}, {});
|
|
123
|
+
return {
|
|
124
|
+
name,
|
|
125
|
+
compile: program.compile.bind(program),
|
|
126
|
+
verify: program.verify.bind(program),
|
|
127
|
+
analyzeMethods: program.analyzeMethods.bind(program),
|
|
128
|
+
Proof: SelfProof,
|
|
129
|
+
methods,
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Wrapper for an application specific runtime, which helps orchestrate
|
|
136
|
+
* runtime modules into an interoperable runtime.
|
|
137
|
+
*/
|
|
138
|
+
export let Runtime = Runtime_1 = class Runtime extends ModuleContainer {
|
|
139
|
+
static from(definition) {
|
|
140
|
+
return class RuntimeScoped extends Runtime_1 {
|
|
141
|
+
constructor() {
|
|
142
|
+
super(definition);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Creates a new Runtime from the provided config
|
|
148
|
+
*
|
|
149
|
+
* @param modules - Configuration object for the constructed Runtime
|
|
150
|
+
*/
|
|
151
|
+
constructor(definition) {
|
|
152
|
+
super(definition);
|
|
153
|
+
this.definition = definition;
|
|
154
|
+
this.zkProgrammable = new RuntimeZkProgrammable(this);
|
|
155
|
+
}
|
|
156
|
+
// TODO Remove after changing DFs to type-based approach
|
|
157
|
+
create(childContainerProvider) {
|
|
158
|
+
super.create(childContainerProvider);
|
|
159
|
+
this.useDependencyFactory(this.container.resolve(MethodIdFactory));
|
|
160
|
+
}
|
|
161
|
+
get areProofsEnabled() {
|
|
162
|
+
return this.container.resolve("AreProofsEnabled");
|
|
163
|
+
}
|
|
164
|
+
get stateServiceProvider() {
|
|
165
|
+
return this.dependencyContainer.resolve("StateServiceProvider");
|
|
166
|
+
}
|
|
167
|
+
get stateService() {
|
|
168
|
+
return this.stateServiceProvider.stateService;
|
|
169
|
+
}
|
|
170
|
+
get methodIdResolver() {
|
|
171
|
+
return this.container.resolve("MethodIdResolver");
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* @returns The dependency injection container of this runtime
|
|
175
|
+
*/
|
|
176
|
+
get dependencyContainer() {
|
|
177
|
+
return this.container;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* @param methodId The encoded name of the method to call.
|
|
181
|
+
* Encoding: "stringToField(module.name) << 128 + stringToField(method-name)"
|
|
182
|
+
*/
|
|
183
|
+
getMethodById(methodId) {
|
|
184
|
+
const methodDescriptor = this.methodIdResolver.getMethodNameFromId(methodId);
|
|
185
|
+
if (methodDescriptor === undefined) {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
const [moduleName, methodName] = methodDescriptor;
|
|
189
|
+
this.assertIsValidModuleName(moduleName);
|
|
190
|
+
const module = this.resolve(moduleName);
|
|
191
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
192
|
+
const method = module[methodName];
|
|
193
|
+
if (method === undefined) {
|
|
194
|
+
throw errors.methodNotFound(`${moduleName}.${methodName}`);
|
|
195
|
+
}
|
|
196
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
197
|
+
return method.bind(module);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Add a name and other respective properties required by RuntimeModules,
|
|
201
|
+
* that come from the current Runtime
|
|
202
|
+
*
|
|
203
|
+
* @param moduleName - Name of the runtime module to decorate
|
|
204
|
+
* @param containedModule
|
|
205
|
+
*/
|
|
206
|
+
decorateModule(moduleName, containedModule) {
|
|
207
|
+
containedModule.name = moduleName;
|
|
208
|
+
containedModule.runtime = this;
|
|
209
|
+
super.decorateModule(moduleName, containedModule);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* @returns A list of names of all the registered module names
|
|
213
|
+
*/
|
|
214
|
+
get runtimeModuleNames() {
|
|
215
|
+
return Object.keys(this.definition.modules);
|
|
216
|
+
}
|
|
217
|
+
async compile(registry) {
|
|
218
|
+
const context = container.resolve(RuntimeMethodExecutionContext);
|
|
219
|
+
context.setup({
|
|
220
|
+
transaction: RuntimeTransaction.dummyTransaction(),
|
|
221
|
+
networkState: NetworkState.empty(),
|
|
222
|
+
});
|
|
223
|
+
return await this.zkProgrammable.compile(registry);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
Runtime = Runtime_1 = __decorate([
|
|
227
|
+
injectable(),
|
|
228
|
+
__metadata("design:paramtypes", [Object])
|
|
229
|
+
], Runtime);
|
|
230
|
+
/* eslint-enable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument */
|
|
231
|
+
//# sourceMappingURL=Runtime.js.map
|