@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.
Files changed (155) hide show
  1. package/dist/factories/MethodIdFactory.d.ts +6 -6
  2. package/dist/factories/MethodIdFactory.d.ts.map +1 -1
  3. package/dist/factories/MethodIdFactory.js +10 -34
  4. package/dist/factories/MethodIdFactory.js.map +1 -0
  5. package/dist/index.d.ts +4 -7
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +5 -7
  8. package/dist/index.js.map +1 -0
  9. package/dist/messages/OutgoingMessage.d.ts +96 -0
  10. package/dist/messages/OutgoingMessage.d.ts.map +1 -0
  11. package/dist/messages/OutgoingMessage.js +68 -0
  12. package/dist/messages/OutgoingMessage.js.map +1 -0
  13. package/dist/messages/OutgoingMessages.d.ts +231 -0
  14. package/dist/messages/OutgoingMessages.d.ts.map +1 -0
  15. package/dist/messages/OutgoingMessages.js +67 -0
  16. package/dist/messages/OutgoingMessages.js.map +1 -0
  17. package/dist/method/MethodParameterEncoder.d.ts +26 -0
  18. package/dist/method/MethodParameterEncoder.d.ts.map +1 -0
  19. package/dist/method/MethodParameterEncoder.js +169 -0
  20. package/dist/method/MethodParameterEncoder.js.map +1 -0
  21. package/dist/method/runtimeMethod.d.ts +21 -5
  22. package/dist/method/runtimeMethod.d.ts.map +1 -1
  23. package/dist/method/runtimeMethod.js +69 -25
  24. package/dist/method/runtimeMethod.js.map +1 -0
  25. package/dist/module/decorator.js +1 -0
  26. package/dist/module/decorator.js.map +1 -0
  27. package/dist/runtime/MethodIdResolver.d.ts +10 -8
  28. package/dist/runtime/MethodIdResolver.d.ts.map +1 -1
  29. package/dist/runtime/MethodIdResolver.js +75 -33
  30. package/dist/runtime/MethodIdResolver.js.map +1 -0
  31. package/dist/runtime/Runtime.d.ts +20 -26
  32. package/dist/runtime/Runtime.d.ts.map +1 -1
  33. package/dist/runtime/Runtime.js +117 -62
  34. package/dist/runtime/Runtime.js.map +1 -0
  35. package/dist/runtime/RuntimeEnvironment.d.ts +10 -0
  36. package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -0
  37. package/dist/runtime/RuntimeEnvironment.js +2 -0
  38. package/dist/runtime/RuntimeEnvironment.js.map +1 -0
  39. package/dist/runtime/RuntimeModule.d.ts +18 -15
  40. package/dist/runtime/RuntimeModule.d.ts.map +1 -1
  41. package/dist/runtime/RuntimeModule.js +33 -8
  42. package/dist/runtime/RuntimeModule.js.map +1 -0
  43. package/dist/state/InMemoryStateService.d.ts +11 -10
  44. package/dist/state/InMemoryStateService.d.ts.map +1 -1
  45. package/dist/state/InMemoryStateService.js +11 -8
  46. package/dist/state/InMemoryStateService.js.map +1 -0
  47. package/dist/state/decorator.d.ts.map +1 -1
  48. package/dist/state/decorator.js +2 -4
  49. package/dist/state/decorator.js.map +1 -0
  50. package/dist/testing/TestingRuntime.d.ts +8 -0
  51. package/dist/testing/TestingRuntime.d.ts.map +1 -0
  52. package/dist/testing/TestingRuntime.js +31 -0
  53. package/dist/testing/TestingRuntime.js.map +1 -0
  54. package/jest.config.cjs +12 -1
  55. package/package.json +9 -9
  56. package/src/factories/MethodIdFactory.ts +10 -17
  57. package/src/index.ts +4 -4
  58. package/src/messages/OutgoingMessages.ts +124 -0
  59. package/src/method/MethodParameterEncoder.ts +262 -0
  60. package/src/method/runtimeMethod.ts +131 -33
  61. package/src/runtime/MethodIdResolver.ts +85 -46
  62. package/src/runtime/Runtime.ts +187 -96
  63. package/src/runtime/RuntimeEnvironment.ts +16 -0
  64. package/src/runtime/RuntimeModule.ts +58 -27
  65. package/src/state/InMemoryStateService.ts +14 -14
  66. package/test/Runtime.test.ts +68 -42
  67. package/test/TestingRuntime.ts +43 -0
  68. package/test/messages/message.test.ts +42 -0
  69. package/test/method/MethodParameterEncoder.test.ts +124 -0
  70. package/test/method/runtimeMethod-fail.test.ts +53 -0
  71. package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +3 -3
  72. package/test/modules/Admin.ts +4 -4
  73. package/test/modules/Balances.test.ts +88 -79
  74. package/test/modules/Balances.ts +15 -21
  75. package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +24 -35
  76. package/test/modules/State.test.ts +46 -53
  77. package/test/runtimeMethod.test.ts +192 -20
  78. package/test/tsconfig.json +7 -0
  79. package/tsconfig.json +2 -2
  80. package/LICENSE.md +0 -201
  81. package/dist/method/MethodParameterDecoder.d.ts +0 -22
  82. package/dist/method/MethodParameterDecoder.d.ts.map +0 -1
  83. package/dist/method/MethodParameterDecoder.js +0 -33
  84. package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
  85. package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
  86. package/dist/method/RuntimeMethodExecutionContext.js +0 -92
  87. package/dist/method/assert.d.ts +0 -12
  88. package/dist/method/assert.d.ts.map +0 -1
  89. package/dist/method/assert.js +0 -23
  90. package/dist/module/src/factories/MethodIdFactory.d.ts +0 -9
  91. package/dist/module/src/factories/MethodIdFactory.d.ts.map +0 -1
  92. package/dist/module/src/factories/MethodIdFactory.js +0 -36
  93. package/dist/module/src/index.d.ts +0 -11
  94. package/dist/module/src/index.d.ts.map +0 -1
  95. package/dist/module/src/index.js +0 -10
  96. package/dist/module/src/method/MethodParameterDecoder.d.ts +0 -22
  97. package/dist/module/src/method/MethodParameterDecoder.d.ts.map +0 -1
  98. package/dist/module/src/method/MethodParameterDecoder.js +0 -33
  99. package/dist/module/src/method/runtimeMethod.d.ts +0 -19
  100. package/dist/module/src/method/runtimeMethod.d.ts.map +0 -1
  101. package/dist/module/src/method/runtimeMethod.js +0 -123
  102. package/dist/module/src/module/decorator.d.ts +0 -8
  103. package/dist/module/src/module/decorator.d.ts.map +0 -1
  104. package/dist/module/src/module/decorator.js +0 -15
  105. package/dist/module/src/runtime/MethodIdResolver.d.ts +0 -18
  106. package/dist/module/src/runtime/MethodIdResolver.d.ts.map +0 -1
  107. package/dist/module/src/runtime/MethodIdResolver.js +0 -50
  108. package/dist/module/src/runtime/Runtime.d.ts +0 -72
  109. package/dist/module/src/runtime/Runtime.d.ts.map +0 -1
  110. package/dist/module/src/runtime/Runtime.js +0 -184
  111. package/dist/module/src/runtime/RuntimeModule.d.ts +0 -35
  112. package/dist/module/src/runtime/RuntimeModule.d.ts.map +0 -1
  113. package/dist/module/src/runtime/RuntimeModule.js +0 -56
  114. package/dist/module/src/state/InMemoryStateService.d.ts +0 -11
  115. package/dist/module/src/state/InMemoryStateService.d.ts.map +0 -1
  116. package/dist/module/src/state/InMemoryStateService.js +0 -21
  117. package/dist/module/src/state/decorator.d.ts +0 -7
  118. package/dist/module/src/state/decorator.d.ts.map +0 -1
  119. package/dist/module/src/state/decorator.js +0 -42
  120. package/dist/protocol/src/model/Option.d.ts +0 -98
  121. package/dist/protocol/src/model/Option.d.ts.map +0 -1
  122. package/dist/protocol/src/model/Option.js +0 -98
  123. package/dist/protocol/src/model/StateTransition.d.ts +0 -96
  124. package/dist/protocol/src/model/StateTransition.d.ts.map +0 -1
  125. package/dist/protocol/src/model/StateTransition.js +0 -65
  126. package/dist/protocol/src/model/network/NetworkState.d.ts +0 -64
  127. package/dist/protocol/src/model/network/NetworkState.d.ts.map +0 -1
  128. package/dist/protocol/src/model/network/NetworkState.js +0 -12
  129. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +0 -70
  130. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +0 -1
  131. package/dist/protocol/src/model/transaction/ProtocolTransaction.js +0 -18
  132. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +0 -63
  133. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +0 -1
  134. package/dist/protocol/src/model/transaction/RuntimeTransaction.js +0 -29
  135. package/dist/protocol/src/state/assert/assert.d.ts +0 -12
  136. package/dist/protocol/src/state/assert/assert.d.ts.map +0 -1
  137. package/dist/protocol/src/state/assert/assert.js +0 -23
  138. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +0 -60
  139. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +0 -1
  140. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +0 -105
  141. package/dist/state/State.d.ts +0 -65
  142. package/dist/state/State.d.ts.map +0 -1
  143. package/dist/state/State.js +0 -114
  144. package/dist/state/StateMap.d.ts +0 -37
  145. package/dist/state/StateMap.d.ts.map +0 -1
  146. package/dist/state/StateMap.js +0 -56
  147. package/dist/state/StateServiceProvider.d.ts +0 -10
  148. package/dist/state/StateServiceProvider.d.ts.map +0 -1
  149. package/dist/state/StateServiceProvider.js +0 -34
  150. package/src/method/MethodParameterDecoder.ts +0 -70
  151. package/src/state/decorator.ts +0 -63
  152. package/test/state/MerkleTree.test.ts +0 -95
  153. package/test/state/MockAsyncMerkleStore.ts +0 -28
  154. package/test/transaction.test.ts +0 -82
  155. package/tsconfig.test.json +0 -9
@@ -1,31 +1,42 @@
1
- import { FlexibleProvable } from "snarkyjs";
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 { DecoratedMethod, toProver, ZkProgrammable } from "@proto-kit/common";
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
- methodInputsNotProvided: () =>
28
+ runtimeNameNotSet: () => new Error("Runtime name was not set"),
29
+
30
+ fieldNotConstant: (name: string) =>
19
31
  new Error(
20
- "Method execution inputs not provided, provide them via context.inputs"
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
- stateTransitions: StateTransition<any>[]
37
+ stateTransitions: { toProvable: () => ProvableStateTransition }[]
27
38
  ) {
28
- const stateTransitionsHashList = new DefaultProvableHashList(
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
- // eslint-disable-next-line etc/prefer-interface
43
- export type WrappedMethod = (...args: unknown[]) => MethodPublicOutput;
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: unknown[]) => unknown
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: WrappedMethod = (...args): MethodPublicOutput => {
55
- Reflect.apply(moduleMethod, this, args);
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
- if (input === undefined) {
64
- throw errors.methodInputsNotProvided();
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
- const transactionHash = input.transaction.hash();
68
- const networkStateHash = input.networkState.hash();
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 function runtimeMethod() {
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: PropertyDescriptor
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: FlexibleProvable<unknown>[]
229
+ ...args: ArgumentTypes
143
230
  ) {
144
- const constructorName = this.constructor.name;
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
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.runtime) {
274
+ if (!this.parent) {
188
275
  throw errors.runtimeNotProvided(constructorName);
189
276
  }
190
- executionContext.setProver(prover.bind(this.runtime.zkProgrammable));
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 { singleFieldToString, stringToField } from "@proto-kit/protocol";
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 type { Runtime, RuntimeModulesRecord } from "./Runtime";
6
+ import {
7
+ RuntimeMethodInvocationType,
8
+ runtimeMethodTypeMetadataKey,
9
+ } from "../method/runtimeMethod";
4
10
 
5
- const offset = 128n;
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
- * How do we encode MethodIds
11
- * A MethodId is defined as the following in little-endian
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: { [key: string]: string } = {};
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<Record<string, string>>(
25
- (dict, moduleName) => {
26
- this.runtime.assertIsValidModuleName(modules, moduleName);
27
-
28
- dict[(stringToField(moduleName).toBigInt() % modulus).toString()] =
29
- moduleName;
30
-
31
- runtime.resolve(moduleName).runtimeMethodNames.forEach((methodName) => {
32
- dict[(stringToField(methodName).toBigInt() % modulus).toString()] =
33
- methodName;
34
- });
35
- return dict;
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
- public getMethodNameFromId(methodId: bigint): [string, string] | undefined {
42
- const moduleNameHash = singleFieldToString(methodId >> offset);
43
- const methodNameHash = singleFieldToString(methodId % modulus);
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 moduleName: string | undefined = this.dictionary[moduleNameHash];
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
- // eslint-disable-next-line no-warning-comments
48
- // TODO Replace by throwing exception?
49
- if (moduleName === undefined) {
50
- return undefined;
51
- }
52
- this.runtime.assertIsValidModuleName(this.modules, moduleName);
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
- const methodName: string | undefined = this.dictionary[methodNameHash];
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
- if (methodName === undefined) {
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(this.modules, moduleName);
103
+ this.runtime.assertIsValidModuleName(moduleName);
65
104
 
66
- return (
67
- (stringToField(moduleName).toBigInt() % modulus << offset) +
68
- (stringToField(methodName).toBigInt() % modulus)
69
- );
105
+ return Poseidon.hash([
106
+ stringToField(moduleName),
107
+ stringToField(methodName),
108
+ ]).toBigInt();
70
109
  }
71
110
  }