@proto-kit/module 0.1.1-develop.651 → 0.1.1-develop.833
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 -201
- package/dist/method/MethodParameterDecoder.d.ts +10 -10
- package/dist/method/MethodParameterDecoder.js +40 -40
- package/dist/method/MethodParameterEncoder.d.ts +15 -8
- package/dist/method/MethodParameterEncoder.d.ts.map +1 -1
- package/dist/method/MethodParameterEncoder.js +122 -51
- package/dist/method/runtimeMethod.d.ts +6 -5
- package/dist/method/runtimeMethod.d.ts.map +1 -1
- package/dist/method/runtimeMethod.js +7 -17
- package/dist/runtime/MethodIdResolver.d.ts.map +1 -1
- package/dist/runtime/MethodIdResolver.js +2 -2
- package/dist/runtime/Runtime.d.ts +5 -5
- package/dist/runtime/Runtime.d.ts.map +1 -1
- package/dist/runtime/Runtime.js +16 -23
- package/dist/runtime/RuntimeEnvironment.d.ts +2 -2
- package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -1
- package/dist/runtime/RuntimeModule.d.ts.map +1 -1
- package/dist/runtime/RuntimeModule.js +2 -3
- package/dist/src/factories/MethodIdFactory.d.ts +10 -0
- package/dist/src/factories/MethodIdFactory.d.ts.map +1 -0
- package/dist/src/factories/MethodIdFactory.js +10 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +10 -0
- package/dist/src/method/MethodParameterEncoder.d.ts +22 -0
- package/dist/src/method/MethodParameterEncoder.d.ts.map +1 -0
- package/dist/src/method/MethodParameterEncoder.js +103 -0
- package/dist/src/method/runtimeMethod.d.ts +26 -0
- package/dist/src/method/runtimeMethod.d.ts.map +1 -0
- package/dist/src/method/runtimeMethod.js +164 -0
- package/dist/src/module/decorator.d.ts +8 -0
- package/dist/src/module/decorator.d.ts.map +1 -0
- package/dist/src/module/decorator.js +15 -0
- package/dist/src/runtime/MethodIdResolver.d.ts +20 -0
- package/dist/src/runtime/MethodIdResolver.d.ts.map +1 -0
- package/dist/src/runtime/MethodIdResolver.js +100 -0
- package/dist/src/runtime/Runtime.d.ts +71 -0
- package/dist/src/runtime/Runtime.d.ts.map +1 -0
- package/dist/src/runtime/Runtime.js +215 -0
- package/dist/src/runtime/RuntimeEnvironment.d.ts +10 -0
- package/dist/src/runtime/RuntimeEnvironment.d.ts.map +1 -0
- package/dist/src/runtime/RuntimeEnvironment.js +1 -0
- package/dist/src/runtime/RuntimeModule.d.ts +26 -0
- package/dist/src/runtime/RuntimeModule.d.ts.map +1 -0
- package/dist/src/runtime/RuntimeModule.js +85 -0
- package/dist/src/state/InMemoryStateService.d.ts +15 -0
- package/dist/src/state/InMemoryStateService.d.ts.map +1 -0
- package/dist/src/state/InMemoryStateService.js +28 -0
- package/dist/src/state/decorator.d.ts +7 -0
- package/dist/src/state/decorator.d.ts.map +1 -0
- package/dist/src/state/decorator.js +39 -0
- package/dist/state/InMemoryStateService.d.ts +10 -6
- package/dist/state/InMemoryStateService.d.ts.map +1 -1
- package/dist/state/InMemoryStateService.js +10 -8
- package/dist/state/decorator.d.ts.map +1 -1
- package/dist/state/decorator.js +0 -3
- package/dist/test/Runtime.test.d.ts +2 -0
- package/dist/test/Runtime.test.d.ts.map +1 -0
- package/dist/test/Runtime.test.js +24 -0
- package/dist/test/TestingRuntime.d.ts +7 -0
- package/dist/test/TestingRuntime.d.ts.map +1 -0
- package/dist/test/TestingRuntime.js +29 -0
- package/dist/test/method/runtimeMethod.test.d.ts +2 -0
- package/dist/test/method/runtimeMethod.test.d.ts.map +1 -0
- package/dist/test/method/runtimeMethod.test.js +30 -0
- package/dist/test/modules/Admin.d.ts +10 -0
- package/dist/test/modules/Admin.d.ts.map +1 -0
- package/dist/test/modules/Admin.js +29 -0
- package/dist/test/modules/Balances.d.ts +23 -0
- package/dist/test/modules/Balances.d.ts.map +1 -0
- package/dist/test/modules/Balances.js +98 -0
- package/dist/test/modules/Balances.test.d.ts +2 -0
- package/dist/test/modules/Balances.test.d.ts.map +1 -0
- package/dist/test/modules/Balances.test.js +201 -0
- package/dist/test/modules/MethodIdResolver.test.d.ts +2 -0
- package/dist/test/modules/MethodIdResolver.test.d.ts.map +1 -0
- package/dist/test/modules/MethodIdResolver.test.js +67 -0
- package/dist/test/modules/State.test.d.ts +2 -0
- package/dist/test/modules/State.test.d.ts.map +1 -0
- package/dist/test/modules/State.test.js +42 -0
- package/dist/test/runtimeMethod.test.d.ts +2 -0
- package/dist/test/runtimeMethod.test.d.ts.map +1 -0
- package/dist/test/runtimeMethod.test.js +50 -0
- package/package.json +5 -5
- package/src/method/MethodParameterEncoder.ts +186 -84
- package/src/method/runtimeMethod.ts +19 -30
- package/src/runtime/MethodIdResolver.ts +1 -0
- package/src/runtime/Runtime.ts +27 -29
- package/src/runtime/RuntimeEnvironment.ts +4 -7
- package/src/runtime/RuntimeModule.ts +2 -8
- package/src/state/InMemoryStateService.ts +13 -13
- package/src/state/decorator.ts +1 -3
- package/test/Runtime.test.ts +68 -41
- package/test/TestingRuntime.ts +45 -0
- package/test/method/MethodParameterEncoder.test.ts +152 -0
- package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +2 -2
- package/test/modules/Admin.ts +3 -3
- package/test/modules/Balances.test.ts +57 -61
- package/test/modules/Balances.ts +15 -18
- package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +14 -23
- package/test/modules/State.test.ts +41 -50
- package/test/runtimeMethod.test.ts +19 -32
- package/test/tsconfig.json +7 -0
- package/tsconfig.json +2 -2
- package/test/transaction.test.ts +0 -82
- package/tsconfig.test.json +0 -9
|
@@ -0,0 +1,215 @@
|
|
|
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 { injectable } from "tsyringe";
|
|
14
|
+
import { ModuleContainer, ZkProgrammable, } from "@proto-kit/common";
|
|
15
|
+
import { MethodPublicOutput, } 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
|
+
Object.defineProperty(this, "runtime", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: runtime
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
get appChain() {
|
|
46
|
+
return this.runtime.appChain;
|
|
47
|
+
}
|
|
48
|
+
zkProgramFactory() {
|
|
49
|
+
// We need to use explicit type annotations here,
|
|
50
|
+
// therefore we can't use destructuring
|
|
51
|
+
// eslint-disable-next-line prefer-destructuring
|
|
52
|
+
const runtime = this.runtime;
|
|
53
|
+
const runtimeMethods = runtime.runtimeModuleNames.reduce((allMethods, runtimeModuleName) => {
|
|
54
|
+
runtime.isValidModuleName(runtime.definition.modules, runtimeModuleName);
|
|
55
|
+
/**
|
|
56
|
+
* Couldnt find a better way to circumvent the type assertion
|
|
57
|
+
* regarding resolving only known modules. We assert in the line above
|
|
58
|
+
* but we cast it to any anyways to satisfy the proof system.
|
|
59
|
+
*/
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
61
|
+
const runtimeModule = runtime.resolve(runtimeModuleName);
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
63
|
+
const modulePrototype = Object.getPrototypeOf(runtimeModule);
|
|
64
|
+
const modulePrototypeMethods = getAllPropertyNames(runtimeModule).map((method) => method.toString());
|
|
65
|
+
const moduleMethods = modulePrototypeMethods.reduce((allModuleMethods, methodName) => {
|
|
66
|
+
if (isRuntimeMethod(runtimeModule, methodName)) {
|
|
67
|
+
const combinedMethodName = combineMethodName(runtimeModuleName, methodName);
|
|
68
|
+
const method = modulePrototype[methodName];
|
|
69
|
+
const invocationType = Reflect.getMetadata(runtimeMethodTypeMetadataKey, runtimeModule, methodName);
|
|
70
|
+
const wrappedMethod = Reflect.apply(toWrappedMethod, runtimeModule, [methodName, method, { invocationType }]);
|
|
71
|
+
// TODO: find out how to import the Tuple type
|
|
72
|
+
const privateInputs = Reflect.getMetadata("design:paramtypes", runtimeModule, methodName);
|
|
73
|
+
return {
|
|
74
|
+
...allModuleMethods,
|
|
75
|
+
[combinedMethodName]: {
|
|
76
|
+
privateInputs,
|
|
77
|
+
method: async () => wrappedMethod(),
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return allModuleMethods;
|
|
82
|
+
}, {});
|
|
83
|
+
return {
|
|
84
|
+
...allMethods,
|
|
85
|
+
...moduleMethods,
|
|
86
|
+
};
|
|
87
|
+
}, {});
|
|
88
|
+
const sortedRuntimeMethods = Object.fromEntries(Object.entries(runtimeMethods).sort());
|
|
89
|
+
const program = ZkProgram({
|
|
90
|
+
name: "RuntimeProgram",
|
|
91
|
+
publicOutput: MethodPublicOutput,
|
|
92
|
+
methods: sortedRuntimeMethods,
|
|
93
|
+
});
|
|
94
|
+
const SelfProof = ZkProgram.Proof(program);
|
|
95
|
+
const methods = Object.keys(sortedRuntimeMethods).reduce((boundMethods, methodName) => {
|
|
96
|
+
boundMethods[methodName] = program[methodName].bind(program);
|
|
97
|
+
return boundMethods;
|
|
98
|
+
}, {});
|
|
99
|
+
return {
|
|
100
|
+
compile: program.compile.bind(program),
|
|
101
|
+
verify: program.verify.bind(program),
|
|
102
|
+
analyzeMethods: program.analyzeMethods.bind(program),
|
|
103
|
+
Proof: SelfProof,
|
|
104
|
+
methods,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Wrapper for an application specific runtime, which helps orchestrate
|
|
110
|
+
* runtime modules into an interoperable runtime.
|
|
111
|
+
*/
|
|
112
|
+
export let Runtime = Runtime_1 = class Runtime extends ModuleContainer {
|
|
113
|
+
static from(definition) {
|
|
114
|
+
return class RuntimeScoped extends Runtime_1 {
|
|
115
|
+
constructor() {
|
|
116
|
+
super(definition);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Creates a new Runtime from the provided config
|
|
122
|
+
*
|
|
123
|
+
* @param modules - Configuration object for the constructed Runtime
|
|
124
|
+
*/
|
|
125
|
+
constructor(definition) {
|
|
126
|
+
super(definition);
|
|
127
|
+
// runtime modules composed into a ZkProgram
|
|
128
|
+
Object.defineProperty(this, "program", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: void 0
|
|
133
|
+
});
|
|
134
|
+
Object.defineProperty(this, "definition", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: void 0
|
|
139
|
+
});
|
|
140
|
+
Object.defineProperty(this, "zkProgrammable", {
|
|
141
|
+
enumerable: true,
|
|
142
|
+
configurable: true,
|
|
143
|
+
writable: true,
|
|
144
|
+
value: void 0
|
|
145
|
+
});
|
|
146
|
+
this.definition = definition;
|
|
147
|
+
this.zkProgrammable = new RuntimeZkProgrammable(this);
|
|
148
|
+
}
|
|
149
|
+
// TODO Remove after changing DFs to type-based approach
|
|
150
|
+
create(childContainerProvider) {
|
|
151
|
+
super.create(childContainerProvider);
|
|
152
|
+
this.useDependencyFactory(this.container.resolve(MethodIdFactory));
|
|
153
|
+
}
|
|
154
|
+
get appChain() {
|
|
155
|
+
return this.container.resolve("AreProofsEnabled");
|
|
156
|
+
}
|
|
157
|
+
get stateServiceProvider() {
|
|
158
|
+
return this.dependencyContainer.resolve("StateServiceProvider");
|
|
159
|
+
}
|
|
160
|
+
get stateService() {
|
|
161
|
+
return this.stateServiceProvider.stateService;
|
|
162
|
+
}
|
|
163
|
+
get methodIdResolver() {
|
|
164
|
+
return this.container.resolve("MethodIdResolver");
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @returns The dependency injection container of this runtime
|
|
168
|
+
*/
|
|
169
|
+
get dependencyContainer() {
|
|
170
|
+
return this.container;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* @param methodId The encoded name of the method to call.
|
|
174
|
+
* Encoding: "stringToField(module.name) << 128 + stringToField(method-name)"
|
|
175
|
+
*/
|
|
176
|
+
getMethodById(methodId) {
|
|
177
|
+
const methodDescriptor = this.methodIdResolver.getMethodNameFromId(methodId);
|
|
178
|
+
if (methodDescriptor === undefined) {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
const [moduleName, methodName] = methodDescriptor;
|
|
182
|
+
this.assertIsValidModuleName(moduleName);
|
|
183
|
+
const module = this.resolve(moduleName);
|
|
184
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
185
|
+
const method = module[methodName];
|
|
186
|
+
if (method === undefined) {
|
|
187
|
+
throw errors.methodNotFound(`${moduleName}.${methodName}`);
|
|
188
|
+
}
|
|
189
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
190
|
+
return method.bind(module);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Add a name and other respective properties required by RuntimeModules,
|
|
194
|
+
* that come from the current Runtime
|
|
195
|
+
*
|
|
196
|
+
* @param moduleName - Name of the runtime module to decorate
|
|
197
|
+
* @param containedModule
|
|
198
|
+
*/
|
|
199
|
+
decorateModule(moduleName, containedModule) {
|
|
200
|
+
containedModule.name = moduleName;
|
|
201
|
+
containedModule.runtime = this;
|
|
202
|
+
super.decorateModule(moduleName, containedModule);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* @returns A list of names of all the registered module names
|
|
206
|
+
*/
|
|
207
|
+
get runtimeModuleNames() {
|
|
208
|
+
return Object.keys(this.definition.modules);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
Runtime = Runtime_1 = __decorate([
|
|
212
|
+
injectable(),
|
|
213
|
+
__metadata("design:paramtypes", [Object])
|
|
214
|
+
], Runtime);
|
|
215
|
+
/* eslint-enable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-argument */
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AreProofsEnabled, WithZkProgrammable } from "@proto-kit/common";
|
|
2
|
+
import { MethodPublicOutput, StateService, StateServiceProvider } from "@proto-kit/protocol";
|
|
3
|
+
import { MethodIdResolver } from "./MethodIdResolver";
|
|
4
|
+
export interface RuntimeEnvironment extends WithZkProgrammable<undefined, MethodPublicOutput> {
|
|
5
|
+
get appChain(): AreProofsEnabled | undefined;
|
|
6
|
+
get stateService(): StateService;
|
|
7
|
+
get stateServiceProvider(): StateServiceProvider;
|
|
8
|
+
get methodIdResolver(): MethodIdResolver;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=RuntimeEnvironment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RuntimeEnvironment.d.ts","sourceRoot":"","sources":["../../../src/runtime/RuntimeEnvironment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,MAAM,WAAW,kBACf,SAAQ,kBAAkB,CAAC,SAAS,EAAE,kBAAkB,CAAC;IACzD,IAAI,QAAQ,IAAI,gBAAgB,GAAG,SAAS,CAAC;IAC7C,IAAI,YAAY,IAAI,YAAY,CAAC;IACjC,IAAI,oBAAoB,IAAI,oBAAoB,CAAC;IACjD,IAAI,gBAAgB,IAAI,gBAAgB,CAAC;CAC1C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ConfigurableModule, NoConfig, Presets } from "@proto-kit/common";
|
|
2
|
+
import { NetworkState, RuntimeTransaction, RuntimeMethodExecutionData } from "@proto-kit/protocol";
|
|
3
|
+
import { RuntimeEnvironment } from "./RuntimeEnvironment";
|
|
4
|
+
/**
|
|
5
|
+
* Base class for runtime modules providing the necessary utilities.
|
|
6
|
+
*/
|
|
7
|
+
export declare class RuntimeModule<Config = NoConfig> extends ConfigurableModule<Config> {
|
|
8
|
+
static presets: Presets<unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Holds all method names that are callable throw transactions
|
|
11
|
+
*/
|
|
12
|
+
readonly runtimeMethodNames: string[];
|
|
13
|
+
/**
|
|
14
|
+
* This property exists only to typecheck that the RuntimeModule
|
|
15
|
+
* was extended correctly in e.g. a decorator. We need at least
|
|
16
|
+
* one non-optional property in this class to make the typechecking work.
|
|
17
|
+
*/
|
|
18
|
+
isRuntimeModule: boolean;
|
|
19
|
+
name?: string;
|
|
20
|
+
runtime?: RuntimeEnvironment;
|
|
21
|
+
constructor();
|
|
22
|
+
getInputs(): RuntimeMethodExecutionData;
|
|
23
|
+
get transaction(): RuntimeTransaction;
|
|
24
|
+
get network(): NetworkState;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=RuntimeModule.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RuntimeModule.d.ts","sourceRoot":"","sources":["../../../src/runtime/RuntimeModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE1E,OAAO,EACL,YAAY,EACZ,kBAAkB,EAElB,0BAA0B,EAE3B,MAAM,qBAAqB,CAAC;AAK7B,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAM1D;;GAEG;AACH,qBACa,aAAa,CACxB,MAAM,GAAG,QAAQ,CACjB,SAAQ,kBAAkB,CAAC,MAAM,CAAC;IAClC,OAAc,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAM;IAE7C;;OAEG;IACH,SAAgB,kBAAkB,EAAE,MAAM,EAAE,CAAM;IAElD;;;;OAIG;IACI,eAAe,UAAQ;IAEvB,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,OAAO,CAAC,EAAE,kBAAkB,CAAC;;IAY7B,SAAS,IAAI,0BAA0B;IAa9C,IAAW,WAAW,IAAI,kBAAkB,CAE3C;IAED,IAAW,OAAO,IAAI,YAAY,CAEjC;CACF"}
|
|
@@ -0,0 +1,85 @@
|
|
|
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 { ConfigurableModule } from "@proto-kit/common";
|
|
11
|
+
import { container, injectable } from "tsyringe";
|
|
12
|
+
import { RuntimeMethodExecutionContext, RuntimeMethodExecutionDataStruct, } from "@proto-kit/protocol";
|
|
13
|
+
import { Provable } from "o1js";
|
|
14
|
+
import { runtimeMethodNamesMetadataKey } from "../method/runtimeMethod";
|
|
15
|
+
const errors = {
|
|
16
|
+
inputDataNotSet: () => new Error("Input data for runtime execution not set"),
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Base class for runtime modules providing the necessary utilities.
|
|
20
|
+
*/
|
|
21
|
+
export let RuntimeModule = class RuntimeModule extends ConfigurableModule {
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
/**
|
|
25
|
+
* Holds all method names that are callable throw transactions
|
|
26
|
+
*/
|
|
27
|
+
Object.defineProperty(this, "runtimeMethodNames", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: []
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
* This property exists only to typecheck that the RuntimeModule
|
|
35
|
+
* was extended correctly in e.g. a decorator. We need at least
|
|
36
|
+
* one non-optional property in this class to make the typechecking work.
|
|
37
|
+
*/
|
|
38
|
+
Object.defineProperty(this, "isRuntimeModule", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: true
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "name", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "runtime", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
57
|
+
const methodNames = Reflect.getMetadata(runtimeMethodNamesMetadataKey, this);
|
|
58
|
+
this.runtimeMethodNames = methodNames ?? [];
|
|
59
|
+
}
|
|
60
|
+
getInputs() {
|
|
61
|
+
return Provable.witness(RuntimeMethodExecutionDataStruct, () => {
|
|
62
|
+
const { input } = container.resolve(RuntimeMethodExecutionContext);
|
|
63
|
+
if (input === undefined) {
|
|
64
|
+
throw errors.inputDataNotSet();
|
|
65
|
+
}
|
|
66
|
+
return input;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
get transaction() {
|
|
70
|
+
return this.getInputs().transaction;
|
|
71
|
+
}
|
|
72
|
+
get network() {
|
|
73
|
+
return this.getInputs().networkState;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
Object.defineProperty(RuntimeModule, "presets", {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
writable: true,
|
|
80
|
+
value: {}
|
|
81
|
+
});
|
|
82
|
+
RuntimeModule = __decorate([
|
|
83
|
+
injectable(),
|
|
84
|
+
__metadata("design:paramtypes", [])
|
|
85
|
+
], RuntimeModule);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Field } from "o1js";
|
|
2
|
+
import { StateService } from "@proto-kit/protocol";
|
|
3
|
+
/**
|
|
4
|
+
* Naive implementation of an in-memory variant of the StateService interface
|
|
5
|
+
*/
|
|
6
|
+
export declare class InMemoryStateService implements StateService {
|
|
7
|
+
/**
|
|
8
|
+
* This mapping container null values if the specific entry has been deleted.
|
|
9
|
+
* This is used by the CachedState service to keep track of deletions
|
|
10
|
+
*/
|
|
11
|
+
values: Record<string, Field[] | null>;
|
|
12
|
+
get(key: Field): Field[] | undefined;
|
|
13
|
+
set(key: Field, value: Field[] | undefined): void;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=InMemoryStateService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InMemoryStateService.d.ts","sourceRoot":"","sources":["../../../src/state/InMemoryStateService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD;;GAEG;AACH,qBAAa,oBAAqB,YAAW,YAAY;IACvD;;;OAGG;IACI,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,CAAM;IAE5C,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,KAAK,EAAE,GAAG,SAAS;IAIpC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,SAAS;CAOlD"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Naive implementation of an in-memory variant of the StateService interface
|
|
3
|
+
*/
|
|
4
|
+
export class InMemoryStateService {
|
|
5
|
+
constructor() {
|
|
6
|
+
/**
|
|
7
|
+
* This mapping container null values if the specific entry has been deleted.
|
|
8
|
+
* This is used by the CachedState service to keep track of deletions
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(this, "values", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: {}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
get(key) {
|
|
18
|
+
return this.values[key.toString()] ?? undefined;
|
|
19
|
+
}
|
|
20
|
+
set(key, value) {
|
|
21
|
+
if (value === undefined) {
|
|
22
|
+
this.values[key.toString()] = null;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.values[key.toString()] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RuntimeModule } from "../runtime/RuntimeModule.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decorates a runtime module property as state, passing down some
|
|
4
|
+
* underlying values to improve developer experience.
|
|
5
|
+
*/
|
|
6
|
+
export declare function state(): <TargetRuntimeModule extends RuntimeModule<unknown>>(target: TargetRuntimeModule, propertyKey: string) => void;
|
|
7
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorator.d.ts","sourceRoot":"","sources":["../../../src/state/decorator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAgBjE;;;GAGG;AACH,wBAAgB,KAAK,mGAGJ,MAAM,UAmCtB"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Path } from "@proto-kit/protocol";
|
|
2
|
+
const errors = {
|
|
3
|
+
missingName: (className) => new Error(`Unable to provide a unique identifier for state, ${className} is missing a name.
|
|
4
|
+
Did you forget to extend your runtime module with 'extends RuntimeModule'?`),
|
|
5
|
+
missingRuntime: (className) => new Error(`Unable to provide 'runtime' for state, ${className} is missing a name.
|
|
6
|
+
Did you forget to extend your runtime module with 'extends RuntimeModule'?`),
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Decorates a runtime module property as state, passing down some
|
|
10
|
+
* underlying values to improve developer experience.
|
|
11
|
+
*/
|
|
12
|
+
export function state() {
|
|
13
|
+
return (target, propertyKey) => {
|
|
14
|
+
let value;
|
|
15
|
+
Object.defineProperty(target, propertyKey, {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function get() {
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
19
|
+
const self = this;
|
|
20
|
+
if (self.name === undefined) {
|
|
21
|
+
throw errors.missingName(self.constructor.name);
|
|
22
|
+
}
|
|
23
|
+
if (!self.runtime) {
|
|
24
|
+
throw errors.missingRuntime(self.constructor.name);
|
|
25
|
+
}
|
|
26
|
+
const path = Path.fromProperty(self.name, propertyKey);
|
|
27
|
+
if (value) {
|
|
28
|
+
value.path = path;
|
|
29
|
+
// TODO: why is this complaining about `any`?
|
|
30
|
+
value.stateServiceProvider = self.runtime.stateServiceProvider;
|
|
31
|
+
}
|
|
32
|
+
return value;
|
|
33
|
+
},
|
|
34
|
+
set: (newValue) => {
|
|
35
|
+
value = newValue;
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { Field } from "o1js";
|
|
2
|
-
import {
|
|
2
|
+
import { SimpleAsyncStateService } from "@proto-kit/protocol";
|
|
3
3
|
/**
|
|
4
|
-
* Naive implementation of
|
|
4
|
+
* Naive implementation of an in-memory variant of the StateService interface
|
|
5
5
|
*/
|
|
6
|
-
export declare class InMemoryStateService implements
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
export declare class InMemoryStateService implements SimpleAsyncStateService {
|
|
7
|
+
/**
|
|
8
|
+
* This mapping container null values if the specific entry has been deleted.
|
|
9
|
+
* This is used by the CachedState service to keep track of deletions
|
|
10
|
+
*/
|
|
11
|
+
values: Record<string, Field[] | null>;
|
|
12
|
+
get(key: Field): Promise<Field[] | undefined>;
|
|
13
|
+
set(key: Field, value: Field[] | undefined): Promise<void>;
|
|
10
14
|
}
|
|
11
15
|
//# sourceMappingURL=InMemoryStateService.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"InMemoryStateService.d.ts","sourceRoot":"","sources":["../../src/state/InMemoryStateService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"InMemoryStateService.d.ts","sourceRoot":"","sources":["../../src/state/InMemoryStateService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D;;GAEG;AACH,qBAAa,oBAAqB,YAAW,uBAAuB;IAClE;;;OAGG;IACI,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,CAAM;IAEtC,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,OAAO,CAAC,KAAK,EAAE,GAAG,SAAS,CAAC;IAI7C,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,SAAS;CAOxD"}
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Naive implementation of
|
|
2
|
+
* Naive implementation of an in-memory variant of the StateService interface
|
|
3
3
|
*/
|
|
4
4
|
export class InMemoryStateService {
|
|
5
5
|
constructor() {
|
|
6
|
+
/**
|
|
7
|
+
* This mapping container null values if the specific entry has been deleted.
|
|
8
|
+
* This is used by the CachedState service to keep track of deletions
|
|
9
|
+
*/
|
|
6
10
|
this.values = {};
|
|
7
11
|
}
|
|
8
|
-
get(key) {
|
|
9
|
-
return this.values[key.toString()];
|
|
12
|
+
async get(key) {
|
|
13
|
+
return this.values[key.toString()] ?? undefined;
|
|
10
14
|
}
|
|
11
|
-
set(key, value) {
|
|
12
|
-
if (value === undefined
|
|
13
|
-
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
15
|
-
delete this.values[key.toString()];
|
|
15
|
+
async set(key, value) {
|
|
16
|
+
if (value === undefined) {
|
|
17
|
+
this.values[key.toString()] = null;
|
|
16
18
|
}
|
|
17
19
|
else {
|
|
18
20
|
this.values[key.toString()] = value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decorator.d.ts","sourceRoot":"","sources":["../../src/state/decorator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAgBjE;;;GAGG;AACH,wBAAgB,KAAK,mGAGJ,MAAM,
|
|
1
|
+
{"version":3,"file":"decorator.d.ts","sourceRoot":"","sources":["../../src/state/decorator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAgBjE;;;GAGG;AACH,wBAAgB,KAAK,mGAGJ,MAAM,UAmCtB"}
|
package/dist/state/decorator.js
CHANGED
|
@@ -11,12 +11,10 @@ const errors = {
|
|
|
11
11
|
*/
|
|
12
12
|
export function state() {
|
|
13
13
|
return (target, propertyKey) => {
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
15
14
|
let value;
|
|
16
15
|
Object.defineProperty(target, propertyKey, {
|
|
17
16
|
enumerable: true,
|
|
18
17
|
get: function get() {
|
|
19
|
-
// eslint-disable-next-line max-len
|
|
20
18
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
21
19
|
const self = this;
|
|
22
20
|
if (self.name === undefined) {
|
|
@@ -28,7 +26,6 @@ export function state() {
|
|
|
28
26
|
const path = Path.fromProperty(self.name, propertyKey);
|
|
29
27
|
if (value) {
|
|
30
28
|
value.path = path;
|
|
31
|
-
// eslint-disable-next-line no-warning-comments
|
|
32
29
|
// TODO: why is this complaining about `any`?
|
|
33
30
|
value.stateServiceProvider = self.runtime.stateServiceProvider;
|
|
34
31
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Runtime.test.d.ts","sourceRoot":"","sources":["../../test/Runtime.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { Balances } from "./modules/Balances";
|
|
3
|
+
import { createTestingRuntime } from "./TestingRuntime";
|
|
4
|
+
describe("runtime", () => {
|
|
5
|
+
it("should encode methodnames correctly", () => {
|
|
6
|
+
expect.assertions(2);
|
|
7
|
+
const { runtime } = createTestingRuntime({
|
|
8
|
+
Balances,
|
|
9
|
+
}, {
|
|
10
|
+
Balances: {},
|
|
11
|
+
});
|
|
12
|
+
const balances = runtime.resolve("Balances");
|
|
13
|
+
expect(balances).toBeDefined();
|
|
14
|
+
console.log(Object.keys(balances));
|
|
15
|
+
console.log(balances.getTotalSupply);
|
|
16
|
+
const moduleName = "Balances";
|
|
17
|
+
const methodName = "getTotalSupply";
|
|
18
|
+
const methodId = runtime.dependencyContainer
|
|
19
|
+
.resolve("MethodIdResolver")
|
|
20
|
+
.getMethodId(moduleName, methodName);
|
|
21
|
+
const method = runtime.getMethodById(methodId);
|
|
22
|
+
expect(method).toBeDefined();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ModulesConfig } from "@proto-kit/common";
|
|
2
|
+
import { InMemoryStateService, Runtime, RuntimeModulesRecord } from "../src";
|
|
3
|
+
export declare function createTestingRuntime<Modules extends RuntimeModulesRecord>(modules: Modules, config: ModulesConfig<Modules>): {
|
|
4
|
+
runtime: Runtime<Modules>;
|
|
5
|
+
state: InMemoryStateService;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=TestingRuntime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestingRuntime.d.ts","sourceRoot":"","sources":["../../test/TestingRuntime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIlD,OAAO,EAAE,oBAAoB,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAE7E,wBAAgB,oBAAoB,CAAC,OAAO,SAAS,oBAAoB,EACvE,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,GAC7B;IACD,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B,KAAK,EAAE,oBAAoB,CAAC;CAC7B,CAgCA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { StateServiceProvider } from "@proto-kit/protocol";
|
|
2
|
+
import { container } from "tsyringe";
|
|
3
|
+
import { InMemoryStateService, Runtime } from "../src";
|
|
4
|
+
export function createTestingRuntime(modules, config) {
|
|
5
|
+
const state = new InMemoryStateService();
|
|
6
|
+
const Runtimeclass = Runtime.from({
|
|
7
|
+
modules,
|
|
8
|
+
});
|
|
9
|
+
const runtime = new Runtimeclass();
|
|
10
|
+
runtime.configure(config);
|
|
11
|
+
runtime.create(() => container.createChildContainer());
|
|
12
|
+
runtime.dependencyContainer.register("AreProofsEnabled", {
|
|
13
|
+
useValue: {
|
|
14
|
+
areProofsEnabled: false,
|
|
15
|
+
setProofsEnabled(areProofsEnabled) {
|
|
16
|
+
this.areProofsEnabled = areProofsEnabled;
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
runtime.registerValue({
|
|
21
|
+
StateServiceProvider: new StateServiceProvider(),
|
|
22
|
+
Runtime: runtime,
|
|
23
|
+
});
|
|
24
|
+
runtime.stateServiceProvider.setCurrentStateService(state);
|
|
25
|
+
return {
|
|
26
|
+
runtime,
|
|
27
|
+
state,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtimeMethod.test.d.ts","sourceRoot":"","sources":["../../../test/method/runtimeMethod.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { Bool, Field } from "o1js";
|
|
3
|
+
import { Option, StateTransition } from "@proto-kit/protocol";
|
|
4
|
+
import { toStateTransitionsHash } from "../../src/method/runtimeMethod";
|
|
5
|
+
describe.skip("toStateTransitionsHash", () => {
|
|
6
|
+
const noneStateTransition = StateTransition.from(Field(0), new Option(Bool(false), Field(0), Field));
|
|
7
|
+
const someStateTransition = StateTransition.from(Field(0), new Option(Bool(true), Field(0), Field));
|
|
8
|
+
it.each([
|
|
9
|
+
[
|
|
10
|
+
[noneStateTransition],
|
|
11
|
+
"7067243248312463521220230733411703436580237248681301130001246160136823979683",
|
|
12
|
+
],
|
|
13
|
+
[
|
|
14
|
+
[someStateTransition],
|
|
15
|
+
"12841542804403638489097503092490970035615082088155587790175618374946575398395",
|
|
16
|
+
],
|
|
17
|
+
[
|
|
18
|
+
[noneStateTransition, someStateTransition],
|
|
19
|
+
"20641278138648130746922286021889771603940136555847557324578879341962747943601",
|
|
20
|
+
],
|
|
21
|
+
[
|
|
22
|
+
[someStateTransition, noneStateTransition],
|
|
23
|
+
"10362098987098600767020985423446775093761176563902435645494178193997179006954",
|
|
24
|
+
],
|
|
25
|
+
])("should calculate a hash of all provided state transitions", (stateTransitions, expectedHash) => {
|
|
26
|
+
expect.assertions(1);
|
|
27
|
+
const hash = toStateTransitionsHash(stateTransitions).toString();
|
|
28
|
+
expect(hash).toBe(expectedHash);
|
|
29
|
+
});
|
|
30
|
+
});
|