@proto-kit/module 0.1.1-develop.211 → 0.1.1-develop.2137
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/dist/factories/MethodIdFactory.d.ts +6 -6
- package/dist/factories/MethodIdFactory.d.ts.map +1 -1
- package/dist/factories/MethodIdFactory.js +10 -34
- package/dist/factories/MethodIdFactory.js.map +1 -0
- package/dist/index.d.ts +4 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -7
- package/dist/index.js.map +1 -0
- package/dist/messages/OutgoingMessage.d.ts +96 -0
- package/dist/messages/OutgoingMessage.d.ts.map +1 -0
- package/dist/messages/OutgoingMessage.js +68 -0
- package/dist/messages/OutgoingMessage.js.map +1 -0
- package/dist/messages/OutgoingMessages.d.ts +231 -0
- package/dist/messages/OutgoingMessages.d.ts.map +1 -0
- package/dist/messages/OutgoingMessages.js +67 -0
- package/dist/messages/OutgoingMessages.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 +21 -5
- package/dist/method/runtimeMethod.d.ts.map +1 -1
- package/dist/method/runtimeMethod.js +69 -25
- package/dist/method/runtimeMethod.js.map +1 -0
- package/dist/module/decorator.js +1 -0
- package/dist/module/decorator.js.map +1 -0
- package/dist/runtime/MethodIdResolver.d.ts +10 -8
- package/dist/runtime/MethodIdResolver.d.ts.map +1 -1
- package/dist/runtime/MethodIdResolver.js +75 -33
- package/dist/runtime/MethodIdResolver.js.map +1 -0
- package/dist/runtime/Runtime.d.ts +20 -26
- package/dist/runtime/Runtime.d.ts.map +1 -1
- package/dist/runtime/Runtime.js +117 -62
- 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 +18 -15
- package/dist/runtime/RuntimeModule.d.ts.map +1 -1
- package/dist/runtime/RuntimeModule.js +33 -8
- package/dist/runtime/RuntimeModule.js.map +1 -0
- package/dist/state/InMemoryStateService.d.ts +11 -10
- package/dist/state/InMemoryStateService.d.ts.map +1 -1
- package/dist/state/InMemoryStateService.js +11 -8
- package/dist/state/InMemoryStateService.js.map +1 -0
- package/dist/state/decorator.d.ts.map +1 -1
- package/dist/state/decorator.js +2 -4
- package/dist/state/decorator.js.map +1 -0
- package/dist/testing/TestingRuntime.d.ts +8 -0
- package/dist/testing/TestingRuntime.d.ts.map +1 -0
- package/dist/testing/TestingRuntime.js +31 -0
- package/dist/testing/TestingRuntime.js.map +1 -0
- package/jest.config.cjs +12 -1
- package/package.json +9 -9
- package/src/factories/MethodIdFactory.ts +10 -17
- package/src/index.ts +4 -4
- package/src/messages/OutgoingMessages.ts +124 -0
- package/src/method/MethodParameterEncoder.ts +262 -0
- package/src/method/runtimeMethod.ts +131 -33
- package/src/runtime/MethodIdResolver.ts +85 -46
- package/src/runtime/Runtime.ts +187 -96
- package/src/runtime/RuntimeEnvironment.ts +16 -0
- package/src/runtime/RuntimeModule.ts +58 -27
- package/src/state/InMemoryStateService.ts +14 -14
- package/test/Runtime.test.ts +68 -42
- package/test/TestingRuntime.ts +43 -0
- package/test/messages/message.test.ts +42 -0
- package/test/method/MethodParameterEncoder.test.ts +124 -0
- package/test/method/runtimeMethod-fail.test.ts +53 -0
- package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +3 -3
- package/test/modules/Admin.ts +4 -4
- package/test/modules/Balances.test.ts +88 -79
- package/test/modules/Balances.ts +15 -21
- package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +24 -35
- package/test/modules/State.test.ts +46 -53
- package/test/runtimeMethod.test.ts +192 -20
- package/test/tsconfig.json +7 -0
- package/tsconfig.json +2 -2
- package/LICENSE.md +0 -201
- package/dist/method/MethodParameterDecoder.d.ts +0 -22
- package/dist/method/MethodParameterDecoder.d.ts.map +0 -1
- package/dist/method/MethodParameterDecoder.js +0 -33
- package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
- package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
- package/dist/method/RuntimeMethodExecutionContext.js +0 -92
- package/dist/method/assert.d.ts +0 -12
- package/dist/method/assert.d.ts.map +0 -1
- package/dist/method/assert.js +0 -23
- package/dist/module/src/factories/MethodIdFactory.d.ts +0 -9
- package/dist/module/src/factories/MethodIdFactory.d.ts.map +0 -1
- package/dist/module/src/factories/MethodIdFactory.js +0 -36
- package/dist/module/src/index.d.ts +0 -11
- package/dist/module/src/index.d.ts.map +0 -1
- package/dist/module/src/index.js +0 -10
- package/dist/module/src/method/MethodParameterDecoder.d.ts +0 -22
- package/dist/module/src/method/MethodParameterDecoder.d.ts.map +0 -1
- package/dist/module/src/method/MethodParameterDecoder.js +0 -33
- package/dist/module/src/method/runtimeMethod.d.ts +0 -19
- package/dist/module/src/method/runtimeMethod.d.ts.map +0 -1
- package/dist/module/src/method/runtimeMethod.js +0 -123
- package/dist/module/src/module/decorator.d.ts +0 -8
- package/dist/module/src/module/decorator.d.ts.map +0 -1
- package/dist/module/src/module/decorator.js +0 -15
- package/dist/module/src/runtime/MethodIdResolver.d.ts +0 -18
- package/dist/module/src/runtime/MethodIdResolver.d.ts.map +0 -1
- package/dist/module/src/runtime/MethodIdResolver.js +0 -50
- package/dist/module/src/runtime/Runtime.d.ts +0 -72
- package/dist/module/src/runtime/Runtime.d.ts.map +0 -1
- package/dist/module/src/runtime/Runtime.js +0 -184
- package/dist/module/src/runtime/RuntimeModule.d.ts +0 -35
- package/dist/module/src/runtime/RuntimeModule.d.ts.map +0 -1
- package/dist/module/src/runtime/RuntimeModule.js +0 -56
- package/dist/module/src/state/InMemoryStateService.d.ts +0 -11
- package/dist/module/src/state/InMemoryStateService.d.ts.map +0 -1
- package/dist/module/src/state/InMemoryStateService.js +0 -21
- package/dist/module/src/state/decorator.d.ts +0 -7
- package/dist/module/src/state/decorator.d.ts.map +0 -1
- package/dist/module/src/state/decorator.js +0 -42
- package/dist/protocol/src/model/Option.d.ts +0 -98
- package/dist/protocol/src/model/Option.d.ts.map +0 -1
- package/dist/protocol/src/model/Option.js +0 -98
- package/dist/protocol/src/model/StateTransition.d.ts +0 -96
- package/dist/protocol/src/model/StateTransition.d.ts.map +0 -1
- package/dist/protocol/src/model/StateTransition.js +0 -65
- package/dist/protocol/src/model/network/NetworkState.d.ts +0 -64
- package/dist/protocol/src/model/network/NetworkState.d.ts.map +0 -1
- package/dist/protocol/src/model/network/NetworkState.js +0 -12
- package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +0 -70
- package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +0 -1
- package/dist/protocol/src/model/transaction/ProtocolTransaction.js +0 -18
- package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +0 -63
- package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +0 -1
- package/dist/protocol/src/model/transaction/RuntimeTransaction.js +0 -29
- package/dist/protocol/src/state/assert/assert.d.ts +0 -12
- package/dist/protocol/src/state/assert/assert.d.ts.map +0 -1
- package/dist/protocol/src/state/assert/assert.js +0 -23
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +0 -60
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +0 -1
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +0 -105
- package/dist/state/State.d.ts +0 -65
- package/dist/state/State.d.ts.map +0 -1
- package/dist/state/State.js +0 -114
- package/dist/state/StateMap.d.ts +0 -37
- package/dist/state/StateMap.d.ts.map +0 -1
- package/dist/state/StateMap.js +0 -56
- package/dist/state/StateServiceProvider.d.ts +0 -10
- package/dist/state/StateServiceProvider.d.ts.map +0 -1
- package/dist/state/StateServiceProvider.js +0 -34
- package/src/method/MethodParameterDecoder.ts +0 -70
- package/src/state/decorator.ts +0 -63
- package/test/state/MerkleTree.test.ts +0 -95
- package/test/state/MockAsyncMerkleStore.ts +0 -28
- package/test/transaction.test.ts +0 -82
- package/tsconfig.test.json +0 -9
|
@@ -1,31 +1,42 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Bool, Field, FlexibleProvablePure, Poseidon } from "o1js";
|
|
2
2
|
import { container } from "tsyringe";
|
|
3
3
|
import {
|
|
4
|
-
StateTransition,
|
|
5
|
-
DefaultProvableHashList,
|
|
6
4
|
ProvableStateTransition,
|
|
7
5
|
MethodPublicOutput,
|
|
8
|
-
RuntimeMethodExecutionContext
|
|
6
|
+
RuntimeMethodExecutionContext,
|
|
7
|
+
StateTransitionReductionList,
|
|
8
|
+
DefaultProvableHashList,
|
|
9
9
|
} from "@proto-kit/protocol";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
DecoratedMethod,
|
|
12
|
+
toProver,
|
|
13
|
+
ZkProgrammable,
|
|
14
|
+
ArgumentTypes,
|
|
15
|
+
} from "@proto-kit/common";
|
|
11
16
|
|
|
12
17
|
import type { RuntimeModule } from "../runtime/RuntimeModule.js";
|
|
13
18
|
|
|
19
|
+
import {
|
|
20
|
+
MethodParameterEncoder,
|
|
21
|
+
checkArgsProvable,
|
|
22
|
+
} from "./MethodParameterEncoder";
|
|
23
|
+
|
|
14
24
|
const errors = {
|
|
15
25
|
runtimeNotProvided: (name: string) =>
|
|
16
26
|
new Error(`Runtime was not provided for module: ${name}`),
|
|
17
27
|
|
|
18
|
-
|
|
28
|
+
runtimeNameNotSet: () => new Error("Runtime name was not set"),
|
|
29
|
+
|
|
30
|
+
fieldNotConstant: (name: string) =>
|
|
19
31
|
new Error(
|
|
20
|
-
|
|
32
|
+
`In-circuit field ${name} not a constant, this is likely a framework bug`
|
|
21
33
|
),
|
|
22
34
|
};
|
|
23
35
|
|
|
24
36
|
export function toStateTransitionsHash(
|
|
25
|
-
|
|
26
|
-
stateTransitions: StateTransition<any>[]
|
|
37
|
+
stateTransitions: { toProvable: () => ProvableStateTransition }[]
|
|
27
38
|
) {
|
|
28
|
-
const stateTransitionsHashList = new
|
|
39
|
+
const stateTransitionsHashList = new StateTransitionReductionList(
|
|
29
40
|
ProvableStateTransition
|
|
30
41
|
);
|
|
31
42
|
|
|
@@ -39,39 +50,102 @@ export function toStateTransitionsHash(
|
|
|
39
50
|
.toField();
|
|
40
51
|
}
|
|
41
52
|
|
|
42
|
-
|
|
43
|
-
|
|
53
|
+
export function toEventsHash(
|
|
54
|
+
events: {
|
|
55
|
+
eventType: FlexibleProvablePure<any>;
|
|
56
|
+
event: any;
|
|
57
|
+
eventName: string;
|
|
58
|
+
condition: Bool;
|
|
59
|
+
}[]
|
|
60
|
+
) {
|
|
61
|
+
return events.reduce((acc, event) => {
|
|
62
|
+
const hashList = new DefaultProvableHashList(event.eventType, acc);
|
|
63
|
+
hashList.pushIf(event.event, event.condition);
|
|
64
|
+
return hashList.commitment;
|
|
65
|
+
}, Field(0));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type WrappedMethod = (...args: ArgumentTypes) => MethodPublicOutput;
|
|
69
|
+
export type AsyncWrappedMethod = (
|
|
70
|
+
...args: ArgumentTypes
|
|
71
|
+
) => Promise<MethodPublicOutput>;
|
|
44
72
|
|
|
45
73
|
export function toWrappedMethod(
|
|
46
74
|
this: RuntimeModule<unknown>,
|
|
47
75
|
methodName: string,
|
|
48
|
-
moduleMethod: (...args:
|
|
49
|
-
|
|
76
|
+
moduleMethod: (...args: ArgumentTypes) => Promise<any>,
|
|
77
|
+
options: {
|
|
78
|
+
invocationType: RuntimeMethodInvocationType;
|
|
79
|
+
}
|
|
80
|
+
): AsyncWrappedMethod {
|
|
50
81
|
const executionContext = container.resolve<RuntimeMethodExecutionContext>(
|
|
51
82
|
RuntimeMethodExecutionContext
|
|
52
83
|
);
|
|
53
84
|
|
|
54
|
-
const wrappedMethod:
|
|
55
|
-
|
|
85
|
+
const wrappedMethod: AsyncWrappedMethod = async (
|
|
86
|
+
...args
|
|
87
|
+
): Promise<MethodPublicOutput> => {
|
|
88
|
+
await Reflect.apply(moduleMethod, this, args);
|
|
56
89
|
const {
|
|
57
|
-
result: { stateTransitions, status },
|
|
58
|
-
input,
|
|
90
|
+
result: { stateTransitions, status, events },
|
|
59
91
|
} = executionContext.current();
|
|
60
92
|
|
|
61
93
|
const stateTransitionsHash = toStateTransitionsHash(stateTransitions);
|
|
94
|
+
const eventsHash = toEventsHash(events);
|
|
95
|
+
|
|
96
|
+
const { name, parent: runtime } = this;
|
|
97
|
+
|
|
98
|
+
if (name === undefined) {
|
|
99
|
+
throw errors.runtimeNameNotSet();
|
|
100
|
+
}
|
|
101
|
+
if (runtime === undefined) {
|
|
102
|
+
throw errors.runtimeNotProvided(name);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const { transaction, networkState } = executionContext.witnessInput();
|
|
106
|
+
const { methodIdResolver } = runtime;
|
|
62
107
|
|
|
63
|
-
|
|
64
|
-
|
|
108
|
+
// Assert that the given transaction has the correct methodId
|
|
109
|
+
const thisMethodId = Field(methodIdResolver.getMethodId(name, methodName));
|
|
110
|
+
if (!thisMethodId.isConstant()) {
|
|
111
|
+
throw errors.fieldNotConstant("methodId");
|
|
65
112
|
}
|
|
66
113
|
|
|
67
|
-
|
|
68
|
-
|
|
114
|
+
transaction.methodId.assertEquals(
|
|
115
|
+
thisMethodId,
|
|
116
|
+
"Runtimemethod called with wrong methodId on the transaction object"
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Use the type info obtained previously to convert
|
|
121
|
+
* the args passed to fields
|
|
122
|
+
*/
|
|
123
|
+
const { fields } = MethodParameterEncoder.fromMethod(
|
|
124
|
+
this,
|
|
125
|
+
methodName
|
|
126
|
+
).encode(args);
|
|
127
|
+
|
|
128
|
+
// Assert that the argsHash that has been signed matches the given arguments
|
|
129
|
+
const argsHash = Poseidon.hash(fields);
|
|
130
|
+
|
|
131
|
+
transaction.argsHash.assertEquals(
|
|
132
|
+
argsHash,
|
|
133
|
+
"argsHash and therefore arguments of transaction and runtime call does not match"
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const isMessage = Bool(options.invocationType === "INCOMING_MESSAGE");
|
|
137
|
+
transaction.assertTransactionType(Bool(isMessage));
|
|
138
|
+
|
|
139
|
+
const transactionHash = transaction.hash();
|
|
140
|
+
const networkStateHash = networkState.hash();
|
|
69
141
|
|
|
70
142
|
return new MethodPublicOutput({
|
|
71
143
|
stateTransitionsHash,
|
|
72
144
|
status,
|
|
73
145
|
transactionHash,
|
|
74
146
|
networkStateHash,
|
|
147
|
+
isMessage,
|
|
148
|
+
eventsHash,
|
|
75
149
|
});
|
|
76
150
|
};
|
|
77
151
|
|
|
@@ -92,6 +166,7 @@ export function combineMethodName(
|
|
|
92
166
|
|
|
93
167
|
export const runtimeMethodMetadataKey = "yab-method";
|
|
94
168
|
export const runtimeMethodNamesMetadataKey = "proto-kit-runtime-methods";
|
|
169
|
+
export const runtimeMethodTypeMetadataKey = "proto-kit-runtime-method-type";
|
|
95
170
|
|
|
96
171
|
/**
|
|
97
172
|
* Checks the metadata of the provided runtime module and its method,
|
|
@@ -110,12 +185,17 @@ export function isRuntimeMethod(
|
|
|
110
185
|
);
|
|
111
186
|
}
|
|
112
187
|
|
|
113
|
-
export
|
|
188
|
+
export type RuntimeMethodInvocationType = "SIGNATURE" | "INCOMING_MESSAGE";
|
|
189
|
+
|
|
190
|
+
function runtimeMethodInternal(options: {
|
|
191
|
+
invocationType: RuntimeMethodInvocationType;
|
|
192
|
+
}) {
|
|
114
193
|
return (
|
|
115
194
|
target: RuntimeModule<unknown>,
|
|
116
195
|
methodName: string,
|
|
117
|
-
descriptor:
|
|
196
|
+
descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any>>
|
|
118
197
|
) => {
|
|
198
|
+
checkArgsProvable(target, methodName);
|
|
119
199
|
const executionContext = container.resolve<RuntimeMethodExecutionContext>(
|
|
120
200
|
RuntimeMethodExecutionContext
|
|
121
201
|
);
|
|
@@ -134,14 +214,21 @@ export function runtimeMethod() {
|
|
|
134
214
|
|
|
135
215
|
Reflect.defineMetadata(runtimeMethodMetadataKey, true, target, methodName);
|
|
136
216
|
|
|
217
|
+
Reflect.defineMetadata(
|
|
218
|
+
runtimeMethodTypeMetadataKey,
|
|
219
|
+
options.invocationType,
|
|
220
|
+
target,
|
|
221
|
+
methodName
|
|
222
|
+
);
|
|
223
|
+
|
|
137
224
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
138
225
|
const simulatedMethod = descriptor.value as DecoratedMethod;
|
|
139
226
|
|
|
140
|
-
descriptor.value = function value(
|
|
227
|
+
descriptor.value = async function value(
|
|
141
228
|
this: RuntimeModule<unknown>,
|
|
142
|
-
...args:
|
|
229
|
+
...args: ArgumentTypes
|
|
143
230
|
) {
|
|
144
|
-
const constructorName = this.
|
|
231
|
+
const constructorName = this.name!;
|
|
145
232
|
|
|
146
233
|
/**
|
|
147
234
|
* If its a top level method call, wrap it into a wrapped method,
|
|
@@ -153,6 +240,7 @@ export function runtimeMethod() {
|
|
|
153
240
|
const simulatedWrappedMethod = Reflect.apply(toWrappedMethod, this, [
|
|
154
241
|
methodName,
|
|
155
242
|
simulatedMethod,
|
|
243
|
+
options,
|
|
156
244
|
]);
|
|
157
245
|
|
|
158
246
|
/**
|
|
@@ -160,7 +248,7 @@ export function runtimeMethod() {
|
|
|
160
248
|
* RuntimeMethodExecutionContext state, meaning it enters and exits
|
|
161
249
|
* the context properly.
|
|
162
250
|
*/
|
|
163
|
-
|
|
251
|
+
|
|
164
252
|
async function prover(this: ZkProgrammable<any, any>) {
|
|
165
253
|
executionContext.beforeMethod(constructorName, methodName, args);
|
|
166
254
|
const innerProver = toProver(
|
|
@@ -169,7 +257,6 @@ export function runtimeMethod() {
|
|
|
169
257
|
false,
|
|
170
258
|
...args
|
|
171
259
|
).bind(this);
|
|
172
|
-
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
173
260
|
let result: Awaited<ReturnType<typeof innerProver>>;
|
|
174
261
|
try {
|
|
175
262
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
@@ -184,16 +271,15 @@ export function runtimeMethod() {
|
|
|
184
271
|
executionContext.beforeMethod(constructorName, methodName, args);
|
|
185
272
|
|
|
186
273
|
if (executionContext.isTopLevel) {
|
|
187
|
-
if (!this.
|
|
274
|
+
if (!this.parent) {
|
|
188
275
|
throw errors.runtimeNotProvided(constructorName);
|
|
189
276
|
}
|
|
190
|
-
executionContext.setProver(prover.bind(this.
|
|
277
|
+
executionContext.setProver(prover.bind(this.parent.zkProgrammable));
|
|
191
278
|
}
|
|
192
279
|
|
|
193
|
-
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
194
280
|
let result: unknown;
|
|
195
281
|
try {
|
|
196
|
-
result = Reflect.apply(simulatedMethod, this, args);
|
|
282
|
+
result = await Reflect.apply(simulatedMethod, this, args);
|
|
197
283
|
} finally {
|
|
198
284
|
executionContext.afterMethod();
|
|
199
285
|
}
|
|
@@ -202,3 +288,15 @@ export function runtimeMethod() {
|
|
|
202
288
|
};
|
|
203
289
|
};
|
|
204
290
|
}
|
|
291
|
+
|
|
292
|
+
export function runtimeMessage() {
|
|
293
|
+
return runtimeMethodInternal({
|
|
294
|
+
invocationType: "INCOMING_MESSAGE",
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function runtimeMethod() {
|
|
299
|
+
return runtimeMethodInternal({
|
|
300
|
+
invocationType: "SIGNATURE",
|
|
301
|
+
});
|
|
302
|
+
}
|
|
@@ -1,71 +1,110 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { filterNonUndefined } from "@proto-kit/common";
|
|
2
|
+
import { stringToField, RuntimeMethodIdMapping } from "@proto-kit/protocol";
|
|
3
|
+
import { Poseidon } from "o1js";
|
|
4
|
+
import { inject, injectable } from "tsyringe";
|
|
2
5
|
|
|
3
|
-
import
|
|
6
|
+
import {
|
|
7
|
+
RuntimeMethodInvocationType,
|
|
8
|
+
runtimeMethodTypeMetadataKey,
|
|
9
|
+
} from "../method/runtimeMethod";
|
|
4
10
|
|
|
5
|
-
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
|
|
7
|
-
const modulus = 2n ** (offset - 1n);
|
|
11
|
+
import type { Runtime, RuntimeModulesRecord } from "./Runtime";
|
|
8
12
|
|
|
9
13
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* [0
|
|
13
|
-
* ...hash(stringToField(moduleName))[0..128],
|
|
14
|
-
* ...hash(stringToField(methodName))[0..128]
|
|
15
|
-
* ]
|
|
14
|
+
* Please see `getMethodId` to learn more about
|
|
15
|
+
* methodId encoding
|
|
16
16
|
*/
|
|
17
|
+
@injectable()
|
|
17
18
|
export class MethodIdResolver {
|
|
18
|
-
private readonly dictionary: {
|
|
19
|
+
private readonly dictionary: {
|
|
20
|
+
[key: string]: { moduleName: string; methodName: string };
|
|
21
|
+
} = {};
|
|
19
22
|
|
|
20
23
|
public constructor(
|
|
21
|
-
private readonly runtime: Runtime<RuntimeModulesRecord
|
|
22
|
-
private readonly modules: RuntimeModulesRecord
|
|
24
|
+
@inject("Runtime") private readonly runtime: Runtime<RuntimeModulesRecord>
|
|
23
25
|
) {
|
|
24
|
-
this.dictionary = runtime.runtimeModuleNames.reduce<
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
this.dictionary = runtime.runtimeModuleNames.reduce<
|
|
27
|
+
Record<string, { moduleName: string; methodName: string }>
|
|
28
|
+
>((dict, moduleName) => {
|
|
29
|
+
this.runtime.assertIsValidModuleName(moduleName);
|
|
30
|
+
|
|
31
|
+
runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
|
|
32
|
+
const methodId = this.getMethodId(moduleName, methodName).toString();
|
|
33
|
+
|
|
34
|
+
dict[methodId] = {
|
|
35
|
+
moduleName,
|
|
36
|
+
methodName,
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return dict;
|
|
41
|
+
}, {});
|
|
39
42
|
}
|
|
40
43
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
/**
|
|
45
|
+
* The purpose of this method is to provide a dictionary where
|
|
46
|
+
* we can look up properties like methodId and invocationType
|
|
47
|
+
* for each runtimeMethod using their module name and method name
|
|
48
|
+
*/
|
|
49
|
+
public methodIdMap(): RuntimeMethodIdMapping {
|
|
50
|
+
const methodIdResolver =
|
|
51
|
+
this.runtime.dependencyContainer.resolve<MethodIdResolver>(
|
|
52
|
+
"MethodIdResolver"
|
|
53
|
+
);
|
|
44
54
|
|
|
45
|
-
const
|
|
55
|
+
const rawMappings = this.runtime.moduleNames.flatMap((moduleName) => {
|
|
56
|
+
const module = this.runtime.resolve(moduleName);
|
|
57
|
+
return module.runtimeMethodNames.map((method) => {
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
59
|
+
const type = Reflect.getMetadata(
|
|
60
|
+
runtimeMethodTypeMetadataKey,
|
|
61
|
+
module,
|
|
62
|
+
method
|
|
63
|
+
) as RuntimeMethodInvocationType | undefined;
|
|
46
64
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
65
|
+
if (type !== undefined) {
|
|
66
|
+
return {
|
|
67
|
+
name: `${moduleName}.${method}`,
|
|
68
|
+
methodId: methodIdResolver.getMethodId(moduleName, method),
|
|
69
|
+
type,
|
|
70
|
+
} as const;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return undefined;
|
|
74
|
+
});
|
|
75
|
+
});
|
|
53
76
|
|
|
54
|
-
|
|
77
|
+
return rawMappings
|
|
78
|
+
.filter(filterNonUndefined)
|
|
79
|
+
.reduce<RuntimeMethodIdMapping>((acc, entry) => {
|
|
80
|
+
acc[entry.name] = {
|
|
81
|
+
methodId: entry.methodId,
|
|
82
|
+
type: entry.type,
|
|
83
|
+
};
|
|
84
|
+
return acc;
|
|
85
|
+
}, {});
|
|
86
|
+
}
|
|
55
87
|
|
|
56
|
-
|
|
88
|
+
public getMethodNameFromId(methodId: bigint): [string, string] | undefined {
|
|
89
|
+
const methodPath = this.dictionary[methodId.toString()];
|
|
90
|
+
|
|
91
|
+
if (methodPath === undefined) {
|
|
57
92
|
return undefined;
|
|
58
93
|
}
|
|
59
94
|
|
|
95
|
+
const { moduleName, methodName } = methodPath;
|
|
96
|
+
|
|
97
|
+
this.runtime.assertIsValidModuleName(moduleName);
|
|
98
|
+
|
|
60
99
|
return [moduleName, methodName];
|
|
61
100
|
}
|
|
62
101
|
|
|
63
102
|
public getMethodId(moduleName: string, methodName: string): bigint {
|
|
64
|
-
this.runtime.assertIsValidModuleName(
|
|
103
|
+
this.runtime.assertIsValidModuleName(moduleName);
|
|
65
104
|
|
|
66
|
-
return (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
);
|
|
105
|
+
return Poseidon.hash([
|
|
106
|
+
stringToField(moduleName),
|
|
107
|
+
stringToField(methodName),
|
|
108
|
+
]).toBigInt();
|
|
70
109
|
}
|
|
71
110
|
}
|