@proto-kit/module 0.1.1-develop.245 → 0.1.1-develop.254

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 (76) hide show
  1. package/dist/method/runtimeMethod.d.ts +2 -1
  2. package/dist/method/runtimeMethod.d.ts.map +1 -1
  3. package/dist/method/runtimeMethod.js +29 -3
  4. package/package.json +2 -2
  5. package/src/method/runtimeMethod.ts +58 -5
  6. package/test/modules/Balances.test.ts +4 -8
  7. package/test/modules/Balances.ts +2 -5
  8. package/test/modules/State.test.ts +8 -3
  9. package/test/runtimeMethod.test.ts +55 -6
  10. package/test/transaction.test.ts +1 -1
  11. package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
  12. package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
  13. package/dist/method/RuntimeMethodExecutionContext.js +0 -92
  14. package/dist/method/assert.d.ts +0 -12
  15. package/dist/method/assert.d.ts.map +0 -1
  16. package/dist/method/assert.js +0 -23
  17. package/dist/module/src/factories/MethodIdFactory.d.ts +0 -9
  18. package/dist/module/src/factories/MethodIdFactory.d.ts.map +0 -1
  19. package/dist/module/src/factories/MethodIdFactory.js +0 -36
  20. package/dist/module/src/index.d.ts +0 -11
  21. package/dist/module/src/index.d.ts.map +0 -1
  22. package/dist/module/src/index.js +0 -10
  23. package/dist/module/src/method/MethodParameterDecoder.d.ts +0 -22
  24. package/dist/module/src/method/MethodParameterDecoder.d.ts.map +0 -1
  25. package/dist/module/src/method/MethodParameterDecoder.js +0 -33
  26. package/dist/module/src/method/runtimeMethod.d.ts +0 -19
  27. package/dist/module/src/method/runtimeMethod.d.ts.map +0 -1
  28. package/dist/module/src/method/runtimeMethod.js +0 -123
  29. package/dist/module/src/module/decorator.d.ts +0 -8
  30. package/dist/module/src/module/decorator.d.ts.map +0 -1
  31. package/dist/module/src/module/decorator.js +0 -15
  32. package/dist/module/src/runtime/MethodIdResolver.d.ts +0 -18
  33. package/dist/module/src/runtime/MethodIdResolver.d.ts.map +0 -1
  34. package/dist/module/src/runtime/MethodIdResolver.js +0 -50
  35. package/dist/module/src/runtime/Runtime.d.ts +0 -72
  36. package/dist/module/src/runtime/Runtime.d.ts.map +0 -1
  37. package/dist/module/src/runtime/Runtime.js +0 -184
  38. package/dist/module/src/runtime/RuntimeModule.d.ts +0 -34
  39. package/dist/module/src/runtime/RuntimeModule.d.ts.map +0 -1
  40. package/dist/module/src/runtime/RuntimeModule.js +0 -56
  41. package/dist/module/src/state/InMemoryStateService.d.ts +0 -11
  42. package/dist/module/src/state/InMemoryStateService.d.ts.map +0 -1
  43. package/dist/module/src/state/InMemoryStateService.js +0 -21
  44. package/dist/module/src/state/decorator.d.ts +0 -7
  45. package/dist/module/src/state/decorator.d.ts.map +0 -1
  46. package/dist/module/src/state/decorator.js +0 -42
  47. package/dist/protocol/src/model/Option.d.ts +0 -98
  48. package/dist/protocol/src/model/Option.d.ts.map +0 -1
  49. package/dist/protocol/src/model/Option.js +0 -98
  50. package/dist/protocol/src/model/StateTransition.d.ts +0 -96
  51. package/dist/protocol/src/model/StateTransition.d.ts.map +0 -1
  52. package/dist/protocol/src/model/StateTransition.js +0 -65
  53. package/dist/protocol/src/model/network/NetworkState.d.ts +0 -64
  54. package/dist/protocol/src/model/network/NetworkState.d.ts.map +0 -1
  55. package/dist/protocol/src/model/network/NetworkState.js +0 -12
  56. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +0 -70
  57. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +0 -1
  58. package/dist/protocol/src/model/transaction/ProtocolTransaction.js +0 -18
  59. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +0 -63
  60. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +0 -1
  61. package/dist/protocol/src/model/transaction/RuntimeTransaction.js +0 -29
  62. package/dist/protocol/src/state/assert/assert.d.ts +0 -12
  63. package/dist/protocol/src/state/assert/assert.d.ts.map +0 -1
  64. package/dist/protocol/src/state/assert/assert.js +0 -23
  65. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +0 -60
  66. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +0 -1
  67. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +0 -105
  68. package/dist/state/State.d.ts +0 -65
  69. package/dist/state/State.d.ts.map +0 -1
  70. package/dist/state/State.js +0 -114
  71. package/dist/state/StateMap.d.ts +0 -37
  72. package/dist/state/StateMap.d.ts.map +0 -1
  73. package/dist/state/StateMap.js +0 -56
  74. package/dist/state/StateServiceProvider.d.ts +0 -10
  75. package/dist/state/StateServiceProvider.d.ts.map +0 -1
  76. package/dist/state/StateServiceProvider.js +0 -34
@@ -1,8 +1,9 @@
1
1
  import { StateTransition, MethodPublicOutput } from "@proto-kit/protocol";
2
+ import { ToFieldable } from "@proto-kit/common";
2
3
  import type { RuntimeModule } from "../runtime/RuntimeModule.js";
3
4
  export declare function toStateTransitionsHash(stateTransitions: StateTransition<any>[]): import("snarkyjs/dist/node/lib/field.js").Field;
4
5
  export type WrappedMethod = (...args: unknown[]) => MethodPublicOutput;
5
- export declare function toWrappedMethod(this: RuntimeModule<unknown>, methodName: string, moduleMethod: (...args: unknown[]) => unknown): WrappedMethod;
6
+ export declare function toWrappedMethod(this: RuntimeModule<unknown>, methodName: string, moduleMethod: (...args: unknown[]) => unknown, methodArguments: ToFieldable[]): WrappedMethod;
6
7
  export declare function combineMethodName(runtimeModuleName: string, methodName: string): string;
7
8
  export declare const runtimeMethodMetadataKey = "yab-method";
8
9
  export declare const runtimeMethodNamesMetadataKey = "proto-kit-runtime-methods";
@@ -1 +1 @@
1
- {"version":3,"file":"runtimeMethod.d.ts","sourceRoot":"","sources":["../../src/method/runtimeMethod.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EAGf,kBAAkB,EAEnB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAYjE,wBAAgB,sBAAsB,CAEpC,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,mDAczC;AAGD,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,kBAAkB,CAAC;AAEvE,wBAAgB,eAAe,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,iBAoC9C;AAED,wBAAgB,iBAAiB,CAC/B,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,UAGnB;AAED,eAAO,MAAM,wBAAwB,eAAe,CAAC;AACrD,eAAO,MAAM,6BAA6B,8BAA8B,CAAC;AAEzE;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAC9B,WAAW,EAAE,MAAM,WAKpB;AAED,wBAAgB,aAAa,aAEjB,cAAc,OAAO,CAAC,cAClB,MAAM,cACN,kBAAkB,UAuFjC"}
1
+ {"version":3,"file":"runtimeMethod.d.ts","sourceRoot":"","sources":["../../src/method/runtimeMethod.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,eAAe,EAGf,kBAAkB,EAEnB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAIL,WAAW,EACZ,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAoBjE,wBAAgB,sBAAsB,CAEpC,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,mDAczC;AAGD,MAAM,MAAM,aAAa,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,kBAAkB,CAAC;AAEvE,wBAAgB,eAAe,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAC7C,eAAe,EAAE,WAAW,EAAE,iBA0E/B;AAED,wBAAgB,iBAAiB,CAC/B,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,UAGnB;AAED,eAAO,MAAM,wBAAwB,eAAe,CAAC;AACrD,eAAO,MAAM,6BAA6B,8BAA8B,CAAC;AAEzE;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,EAC9B,WAAW,EAAE,MAAM,WAKpB;AAED,wBAAgB,aAAa,aAEjB,cAAc,OAAO,CAAC,cAClB,MAAM,cACN,kBAAkB,UAwFjC"}
@@ -1,9 +1,12 @@
1
+ import { Field, Poseidon } from "snarkyjs";
1
2
  import { container } from "tsyringe";
2
- import { DefaultProvableHashList, ProvableStateTransition, MethodPublicOutput, RuntimeMethodExecutionContext } from "@proto-kit/protocol";
3
- import { toProver } from "@proto-kit/common";
3
+ import { DefaultProvableHashList, ProvableStateTransition, MethodPublicOutput, RuntimeMethodExecutionContext, } from "@proto-kit/protocol";
4
+ import { toProver, } from "@proto-kit/common";
4
5
  const errors = {
5
6
  runtimeNotProvided: (name) => new Error(`Runtime was not provided for module: ${name}`),
6
7
  methodInputsNotProvided: () => new Error("Method execution inputs not provided, provide them via context.inputs"),
8
+ runtimeNameNotSet: () => new Error("Runtime name was not set"),
9
+ fieldNotConstant: (name) => new Error(`In-circuit field ${name} not a constant, this is likely a framework bug`),
7
10
  };
8
11
  export function toStateTransitionsHash(
9
12
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -14,7 +17,7 @@ stateTransitions) {
14
17
  .reduce((allStateTransitionsHashList, stateTransition) => allStateTransitionsHashList.push(stateTransition), stateTransitionsHashList)
15
18
  .toField();
16
19
  }
17
- export function toWrappedMethod(methodName, moduleMethod) {
20
+ export function toWrappedMethod(methodName, moduleMethod, methodArguments) {
18
21
  const executionContext = container.resolve(RuntimeMethodExecutionContext);
19
22
  const wrappedMethod = (...args) => {
20
23
  Reflect.apply(moduleMethod, this, args);
@@ -23,6 +26,28 @@ export function toWrappedMethod(methodName, moduleMethod) {
23
26
  if (input === undefined) {
24
27
  throw errors.methodInputsNotProvided();
25
28
  }
29
+ const { name, runtime } = this;
30
+ if (name === undefined) {
31
+ throw errors.runtimeNameNotSet();
32
+ }
33
+ if (runtime === undefined) {
34
+ throw errors.runtimeNotProvided(name);
35
+ }
36
+ // Assert that the given transaction has the correct methodId
37
+ const methodIdResolver = runtime.dependencyContainer.resolve("MethodIdResolver");
38
+ const thisMethodId = Field(methodIdResolver.getMethodId(name, methodName));
39
+ if (!thisMethodId.isConstant()) {
40
+ throw errors.fieldNotConstant("methodId");
41
+ }
42
+ input.transaction.methodId.assertEquals(thisMethodId, "Runtimemethod called with wrong methodId on the transaction object");
43
+ // Assert that the argsHash that has been signed matches the given arguments
44
+ // We can use js-if here, because methodArguments is statically sizes
45
+ // i.e. the result of the if-statement will be the same for all executions
46
+ // of this method
47
+ const argsHash = methodArguments.length > 0
48
+ ? Poseidon.hash(methodArguments.flatMap((argument) => argument.toFields()))
49
+ : Field(0);
50
+ input.transaction.argsHash.assertEquals(argsHash, "argsHash and therefore arguments of transaction and runtime call does not match");
26
51
  const transactionHash = input.transaction.hash();
27
52
  const networkStateHash = input.networkState.hash();
28
53
  return new MethodPublicOutput({
@@ -81,6 +106,7 @@ export function runtimeMethod() {
81
106
  const simulatedWrappedMethod = Reflect.apply(toWrappedMethod, this, [
82
107
  methodName,
83
108
  simulatedMethod,
109
+ args,
84
110
  ]);
85
111
  /**
86
112
  * Before the prover runs, make sure it is operating on the correct
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@proto-kit/module",
3
3
  "license": "MIT",
4
4
  "private": false,
5
- "version": "0.1.1-develop.245+b1d7d07",
5
+ "version": "0.1.1-develop.254+2fba72e",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
@@ -31,5 +31,5 @@
31
31
  "snarkyjs": "0.12.0",
32
32
  "tsyringe": "^4.7.0"
33
33
  },
34
- "gitHead": "b1d7d0756743006192c20bf6b2bf152b6a81e723"
34
+ "gitHead": "2fba72ec580c20289658d5c831430ca601ea5a71"
35
35
  }
@@ -1,15 +1,21 @@
1
- import { FlexibleProvable } from "snarkyjs";
1
+ import { Field, Poseidon } from "snarkyjs";
2
2
  import { container } from "tsyringe";
3
3
  import {
4
4
  StateTransition,
5
5
  DefaultProvableHashList,
6
6
  ProvableStateTransition,
7
7
  MethodPublicOutput,
8
- RuntimeMethodExecutionContext
8
+ RuntimeMethodExecutionContext,
9
9
  } from "@proto-kit/protocol";
10
- import { DecoratedMethod, toProver, ZkProgrammable } from "@proto-kit/common";
10
+ import {
11
+ DecoratedMethod,
12
+ toProver,
13
+ ZkProgrammable,
14
+ ToFieldable,
15
+ } from "@proto-kit/common";
11
16
 
12
17
  import type { RuntimeModule } from "../runtime/RuntimeModule.js";
18
+ import { MethodIdResolver } from "../runtime/MethodIdResolver";
13
19
 
14
20
  const errors = {
15
21
  runtimeNotProvided: (name: string) =>
@@ -19,6 +25,13 @@ const errors = {
19
25
  new Error(
20
26
  "Method execution inputs not provided, provide them via context.inputs"
21
27
  ),
28
+
29
+ runtimeNameNotSet: () => new Error("Runtime name was not set"),
30
+
31
+ fieldNotConstant: (name: string) =>
32
+ new Error(
33
+ `In-circuit field ${name} not a constant, this is likely a framework bug`
34
+ ),
22
35
  };
23
36
 
24
37
  export function toStateTransitionsHash(
@@ -45,7 +58,8 @@ export type WrappedMethod = (...args: unknown[]) => MethodPublicOutput;
45
58
  export function toWrappedMethod(
46
59
  this: RuntimeModule<unknown>,
47
60
  methodName: string,
48
- moduleMethod: (...args: unknown[]) => unknown
61
+ moduleMethod: (...args: unknown[]) => unknown,
62
+ methodArguments: ToFieldable[]
49
63
  ) {
50
64
  const executionContext = container.resolve<RuntimeMethodExecutionContext>(
51
65
  RuntimeMethodExecutionContext
@@ -64,6 +78,44 @@ export function toWrappedMethod(
64
78
  throw errors.methodInputsNotProvided();
65
79
  }
66
80
 
81
+ const { name, runtime } = this;
82
+
83
+ if (name === undefined) {
84
+ throw errors.runtimeNameNotSet();
85
+ }
86
+ if (runtime === undefined) {
87
+ throw errors.runtimeNotProvided(name);
88
+ }
89
+
90
+ // Assert that the given transaction has the correct methodId
91
+ const methodIdResolver =
92
+ runtime.dependencyContainer.resolve<MethodIdResolver>("MethodIdResolver");
93
+ const thisMethodId = Field(methodIdResolver.getMethodId(name, methodName));
94
+ if (!thisMethodId.isConstant()) {
95
+ throw errors.fieldNotConstant("methodId");
96
+ }
97
+
98
+ input.transaction.methodId.assertEquals(
99
+ thisMethodId,
100
+ "Runtimemethod called with wrong methodId on the transaction object"
101
+ );
102
+
103
+ // Assert that the argsHash that has been signed matches the given arguments
104
+ // We can use js-if here, because methodArguments is statically sizes
105
+ // i.e. the result of the if-statement will be the same for all executions
106
+ // of this method
107
+ const argsHash =
108
+ methodArguments.length > 0
109
+ ? Poseidon.hash(
110
+ methodArguments.flatMap((argument) => argument.toFields())
111
+ )
112
+ : Field(0);
113
+
114
+ input.transaction.argsHash.assertEquals(
115
+ argsHash,
116
+ "argsHash and therefore arguments of transaction and runtime call does not match"
117
+ );
118
+
67
119
  const transactionHash = input.transaction.hash();
68
120
  const networkStateHash = input.networkState.hash();
69
121
 
@@ -139,7 +191,7 @@ export function runtimeMethod() {
139
191
 
140
192
  descriptor.value = function value(
141
193
  this: RuntimeModule<unknown>,
142
- ...args: FlexibleProvable<unknown>[]
194
+ ...args: ToFieldable[]
143
195
  ) {
144
196
  const constructorName = this.constructor.name;
145
197
 
@@ -153,6 +205,7 @@ export function runtimeMethod() {
153
205
  const simulatedWrappedMethod = Reflect.apply(toWrappedMethod, this, [
154
206
  methodName,
155
207
  simulatedMethod,
208
+ args,
156
209
  ]);
157
210
 
158
211
  /**
@@ -14,16 +14,12 @@ import {
14
14
  type ProvableStateTransition,
15
15
  Path,
16
16
  MethodPublicOutput,
17
- StateService, NetworkState, RuntimeTransaction
17
+ StateService,
18
+ RuntimeMethodExecutionContext,
18
19
  } from "@proto-kit/protocol";
19
20
 
20
- import {
21
- InMemoryStateService,
22
- } from "../../src/state/InMemoryStateService.js";
23
- import {
24
- Runtime,
25
- RuntimeMethodExecutionContext,
26
- } from "../../src";
21
+ import { InMemoryStateService } from "../../src/state/InMemoryStateService.js";
22
+ import { Runtime } from "../../src";
27
23
 
28
24
  import { Balances } from "./Balances.js";
29
25
  import { Admin } from "./Admin.js";
@@ -1,11 +1,8 @@
1
1
  import { Bool, PublicKey, UInt64 } from "snarkyjs";
2
- import { Option } from "@proto-kit/protocol";
2
+ import { Option, State, StateMap } from "@proto-kit/protocol";
3
3
  import { Presets } from "@proto-kit/common";
4
4
 
5
- import { State } from "../../../protocol/src/state/State.js";
6
- import { state } from "../../src/state/decorator.js";
7
- import { StateMap } from "../../../protocol/src/state/StateMap.js";
8
- import { RuntimeModule, runtimeMethod, runtimeModule } from "../../src";
5
+ import { RuntimeModule, runtimeMethod, runtimeModule, state } from "../../src";
9
6
 
10
7
  import { Admin } from "./Admin.js";
11
8
 
@@ -1,12 +1,17 @@
1
1
  import "reflect-metadata";
2
2
  import { Bool, PublicKey, UInt64 } from "snarkyjs";
3
3
  import { container } from "tsyringe";
4
- import { NetworkState, Option, RuntimeTransaction, StateService } from "@proto-kit/protocol";
4
+ import {
5
+ NetworkState,
6
+ Option,
7
+ RuntimeMethodExecutionContext,
8
+ RuntimeTransaction,
9
+ StateService
10
+ } from "@proto-kit/protocol";
5
11
 
6
12
  import {
7
13
  InMemoryStateService,
8
- Runtime,
9
- RuntimeMethodExecutionContext,
14
+ Runtime
10
15
  } from "../../src";
11
16
 
12
17
  import { Admin } from "./Admin";
@@ -1,7 +1,14 @@
1
1
  import "reflect-metadata";
2
- import { Bool, PublicKey } from "snarkyjs";
2
+ import { Bool, Field, PublicKey, UInt64 } from "snarkyjs";
3
+ import {
4
+ NetworkState,
5
+ RuntimeMethodExecutionContext,
6
+ RuntimeTransaction,
7
+ } from "@proto-kit/protocol";
8
+ import { container } from "tsyringe";
9
+ import { AreProofsEnabled, log } from "@proto-kit/common";
3
10
 
4
- import { InMemoryStateService, Runtime } from "../src";
11
+ import { InMemoryStateService, MethodIdResolver, Runtime } from "../src";
5
12
  import { MethodParameterDecoder } from "../src/method/MethodParameterDecoder";
6
13
 
7
14
  import { Balances } from "./modules/Balances";
@@ -9,11 +16,11 @@ import { Balances } from "./modules/Balances";
9
16
  describe("runtimeMethod", () => {
10
17
  const parameters = [PublicKey.empty()];
11
18
 
12
- it("should create correct param types", () => {
13
- // eslint-disable-next-line jest/prefer-expect-assertions
14
- expect.assertions(1 + parameters.length);
19
+ let runtime: Runtime<{ Balances: typeof Balances }>;
15
20
 
16
- const runtime = Runtime.from({
21
+ beforeEach(() => {
22
+ log.setLevel(log.levels.DEBUG);
23
+ runtime = Runtime.from({
17
24
  state: new InMemoryStateService(),
18
25
 
19
26
  modules: {
@@ -26,6 +33,12 @@ describe("runtimeMethod", () => {
26
33
  },
27
34
  },
28
35
  });
36
+ runtime.start();
37
+ });
38
+
39
+ it("should create correct param types", () => {
40
+ // eslint-disable-next-line jest/prefer-expect-assertions
41
+ expect.assertions(1 + parameters.length);
29
42
 
30
43
  const module = runtime.resolve("Balances");
31
44
 
@@ -40,4 +53,40 @@ describe("runtimeMethod", () => {
40
53
  expect(parameter).toStrictEqual(recodedParameters[index]);
41
54
  });
42
55
  });
56
+
57
+ it("should throw on incorrect methodId on tx", async () => {
58
+ expect.assertions(1);
59
+
60
+ const context = container.resolve(RuntimeMethodExecutionContext);
61
+
62
+ runtime.registerValue({
63
+ AppChain: {
64
+ areProofsEnabled: false,
65
+ } as AreProofsEnabled,
66
+ });
67
+
68
+ const transaction = new RuntimeTransaction({
69
+ methodId: Field(0),
70
+ nonce: UInt64.zero,
71
+ argsHash: Field(0),
72
+ sender: PublicKey.empty(),
73
+ });
74
+
75
+ context.setup({
76
+ transaction,
77
+ networkState: new NetworkState({ block: { height: UInt64.zero } }),
78
+ });
79
+
80
+ const module = runtime.resolve("Balances");
81
+ module.getBalance(PublicKey.empty());
82
+
83
+ context.setup({
84
+ transaction,
85
+ networkState: new NetworkState({ block: { height: UInt64.zero } }),
86
+ });
87
+
88
+ await expect(context.current().result.prover!()).rejects.toThrow(
89
+ "Runtimemethod called with wrong methodId on the transaction object"
90
+ );
91
+ });
43
92
  });
@@ -10,7 +10,7 @@ import {
10
10
  } from "../src";
11
11
  import { runtimeModule } from "../src/module/decorator";
12
12
  import { RuntimeModule } from "../src/runtime/RuntimeModule";
13
- import { state } from "../src/state/decorator";
13
+ import { state } from "../../protocol/src/state/decorator";
14
14
  import { StateMap } from "@proto-kit/protocol";
15
15
 
16
16
  interface BalancesConfig {}
@@ -1,57 +0,0 @@
1
- import { Bool } from "snarkyjs";
2
- import type { StateTransition, NetworkState, ToFieldable } from "@proto-kit/protocol";
3
- import { ProvableMethodExecutionContext, ProvableMethodExecutionResult } from "@proto-kit/common";
4
- import { RuntimeTransaction } from "@proto-kit/protocol/src/model/transaction/RuntimeTransaction";
5
- export declare class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
6
- stateTransitions: StateTransition<any>[];
7
- status: Bool;
8
- statusMessage?: string;
9
- }
10
- export interface RuntimeMethodExecutionData {
11
- transaction: RuntimeTransaction;
12
- networkState: NetworkState;
13
- }
14
- /**
15
- * Execution context used to wrap runtime module methods,
16
- * allowing them to post relevant information (such as execution status)
17
- * into the context without any unnecessary 'prop drilling'.
18
- */
19
- export declare class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
20
- methods: string[];
21
- input: RuntimeMethodExecutionData | undefined;
22
- private lastInput;
23
- result: RuntimeProvableMethodExecutionResult;
24
- private assertSetupCalled;
25
- /**
26
- * Adds an in-method generated state transition to the current context
27
- * @param stateTransition - State transition to add to the context
28
- */
29
- addStateTransition<Value extends ToFieldable>(stateTransition: StateTransition<Value>): void;
30
- /**
31
- * @param message - Status message to acompany the current status
32
- */
33
- setStatusMessage(message?: string): void;
34
- /**
35
- * @param status - Execution status of the current method
36
- */
37
- setStatus(status: Bool): void;
38
- /**
39
- * @param input Input witness data required for a runtime execution
40
- */
41
- setup(input: RuntimeMethodExecutionData): void;
42
- /**
43
- * Manually clears/resets the execution context
44
- */
45
- clear(): void;
46
- afterMethod(): void;
47
- /**
48
- * Had to override current() otherwise it would not infer
49
- * the type of result correctly (parent type would be reused)
50
- */
51
- current(): {
52
- isFinished: boolean;
53
- result: RuntimeProvableMethodExecutionResult;
54
- input: RuntimeMethodExecutionData | undefined;
55
- };
56
- }
57
- //# sourceMappingURL=RuntimeMethodExecutionContext.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RuntimeMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../src/method/RuntimeMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,OAAO,KAAK,EACV,eAAe,EACf,YAAY,EACZ,WAAW,EACZ,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,8BAA8B,EAC9B,6BAA6B,EAC9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,kBAAkB,EAAE,MAAM,8DAA8D,CAAC;AASlG,qBAAa,oCAAqC,SAAQ,6BAA6B;IAE9E,gBAAgB,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,CAAM;IAE9C,MAAM,EAAE,IAAI,CAAc;IAE1B,aAAa,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,0BAA0B;IACzC,WAAW,EAAE,kBAAkB,CAAC;IAChC,YAAY,EAAE,YAAY,CAAC;CAC5B;AAED;;;;GAIG;AACH,qBACa,6BAA8B,SAAQ,8BAA8B;IACxE,OAAO,EAAE,MAAM,EAAE,CAAM;IAEvB,KAAK,EAAE,0BAA0B,GAAG,SAAS,CAAC;IAGrD,OAAO,CAAC,SAAS,CAAyC;IAE1C,MAAM,uCAA8C;IAEpE,OAAO,CAAC,iBAAiB;IAQzB;;;OAGG;IACI,kBAAkB,CAAC,KAAK,SAAS,WAAW,EACjD,eAAe,EAAE,eAAe,CAAC,KAAK,CAAC;IAMzC;;OAEG;IACI,gBAAgB,CAAC,OAAO,CAAC,EAAE,MAAM;IAKxC;;OAEG;IACI,SAAS,CAAC,MAAM,EAAE,IAAI;IAK7B;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,0BAA0B;IAI9C;;OAEG;IACI,KAAK;IAIL,WAAW;IAMlB;;;OAGG;IACI,OAAO;;;;;CAOf"}
@@ -1,92 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { Bool } from "snarkyjs";
8
- import { singleton } from "tsyringe";
9
- import { ProvableMethodExecutionContext, ProvableMethodExecutionResult, } from "@proto-kit/common";
10
- const errors = {
11
- setupNotCalled: () => new Error("Setup has not been called prior to executing a runtime method. Be sure to do that so that the Runtime is setup property for execution"),
12
- };
13
- export class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
14
- constructor() {
15
- super(...arguments);
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- this.stateTransitions = [];
18
- this.status = Bool(true);
19
- }
20
- }
21
- /**
22
- * Execution context used to wrap runtime module methods,
23
- * allowing them to post relevant information (such as execution status)
24
- * into the context without any unnecessary 'prop drilling'.
25
- */
26
- let RuntimeMethodExecutionContext = class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
27
- constructor() {
28
- super(...arguments);
29
- this.methods = [];
30
- this.result = new RuntimeProvableMethodExecutionResult();
31
- }
32
- assertSetupCalled() {
33
- if (this.input === undefined) {
34
- throw errors.setupNotCalled();
35
- }
36
- }
37
- /**
38
- * Adds an in-method generated state transition to the current context
39
- * @param stateTransition - State transition to add to the context
40
- */
41
- addStateTransition(stateTransition) {
42
- this.assertSetupCalled();
43
- this.result.stateTransitions.push(stateTransition);
44
- }
45
- /**
46
- * @param message - Status message to acompany the current status
47
- */
48
- setStatusMessage(message) {
49
- var _a;
50
- this.assertSetupCalled();
51
- (_a = this.result).statusMessage ?? (_a.statusMessage = message);
52
- }
53
- /**
54
- * @param status - Execution status of the current method
55
- */
56
- setStatus(status) {
57
- this.assertSetupCalled();
58
- this.result.status = status;
59
- }
60
- /**
61
- * @param input Input witness data required for a runtime execution
62
- */
63
- setup(input) {
64
- this.input = input;
65
- }
66
- /**
67
- * Manually clears/resets the execution context
68
- */
69
- clear() {
70
- this.result = new RuntimeProvableMethodExecutionResult();
71
- }
72
- afterMethod() {
73
- super.afterMethod();
74
- this.lastInput = this.input;
75
- this.input = undefined;
76
- }
77
- /**
78
- * Had to override current() otherwise it would not infer
79
- * the type of result correctly (parent type would be reused)
80
- */
81
- current() {
82
- return {
83
- isFinished: this.isFinished,
84
- result: this.result,
85
- input: this.lastInput,
86
- };
87
- }
88
- };
89
- RuntimeMethodExecutionContext = __decorate([
90
- singleton()
91
- ], RuntimeMethodExecutionContext);
92
- export { RuntimeMethodExecutionContext };
@@ -1,12 +0,0 @@
1
- import { Bool } from "snarkyjs";
2
- /**
3
- * Maintains an execution status of the current runtime module method,
4
- * while prioritizing one-time failures. The assertion won't change the
5
- * execution status if it has previously failed at least once within the
6
- * same method execution context.
7
- *
8
- * @param condition - Result of the assertion made about the execution status
9
- * @param message - Optional message describing the prior status
10
- */
11
- export declare function assert(condition: Bool, message?: string): void;
12
- //# sourceMappingURL=assert.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/method/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAY,MAAM,UAAU,CAAC;AAM1C;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,MAAM,QAWvD"}
@@ -1,23 +0,0 @@
1
- import { Bool, Provable } from "snarkyjs";
2
- import { container } from "tsyringe";
3
- import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext";
4
- import { log } from "@proto-kit/common";
5
- /**
6
- * Maintains an execution status of the current runtime module method,
7
- * while prioritizing one-time failures. The assertion won't change the
8
- * execution status if it has previously failed at least once within the
9
- * same method execution context.
10
- *
11
- * @param condition - Result of the assertion made about the execution status
12
- * @param message - Optional message describing the prior status
13
- */
14
- export function assert(condition, message) {
15
- const executionContext = container.resolve(RuntimeMethodExecutionContext);
16
- const previousStatus = executionContext.current().result.status;
17
- const status = Provable.if(previousStatus, Bool, condition, previousStatus);
18
- if (!condition.toBoolean()) {
19
- log.debug("Assertion failed: ", message);
20
- executionContext.setStatusMessage(message);
21
- }
22
- executionContext.setStatus(status);
23
- }
@@ -1,9 +0,0 @@
1
- import { DependencyFactory } from "@proto-kit/common";
2
- import { MethodIdResolver } from "../runtime/MethodIdResolver";
3
- import type { Runtime, RuntimeModulesRecord } from "../runtime/Runtime";
4
- export declare class MethodIdFactory extends DependencyFactory {
5
- private readonly runtime;
6
- constructor(runtime: Runtime<RuntimeModulesRecord>);
7
- methodIdResolver(): MethodIdResolver;
8
- }
9
- //# sourceMappingURL=MethodIdFactory.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MethodIdFactory.d.ts","sourceRoot":"","sources":["../../../../src/factories/MethodIdFactory.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,iBAAiB,EAClB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAExE,qBACa,eAAgB,SAAQ,iBAAiB;IAE/B,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,EAAE,OAAO,CAAC,oBAAoB,CAAC;IAMrE,gBAAgB,IAAI,gBAAgB;CAG5C"}
@@ -1,36 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
8
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
- };
10
- var __param = (this && this.__param) || function (paramIndex, decorator) {
11
- return function (target, key) { decorator(target, key, paramIndex); }
12
- };
13
- import { dependency, dependencyFactory, DependencyFactory } from "@proto-kit/common";
14
- import { inject } from "tsyringe";
15
- import { MethodIdResolver } from "../runtime/MethodIdResolver";
16
- let MethodIdFactory = class MethodIdFactory extends DependencyFactory {
17
- constructor(runtime) {
18
- super();
19
- this.runtime = runtime;
20
- }
21
- methodIdResolver() {
22
- return new MethodIdResolver(this.runtime, this.runtime.definition.modules);
23
- }
24
- };
25
- __decorate([
26
- dependency(),
27
- __metadata("design:type", Function),
28
- __metadata("design:paramtypes", []),
29
- __metadata("design:returntype", MethodIdResolver)
30
- ], MethodIdFactory.prototype, "methodIdResolver", null);
31
- MethodIdFactory = __decorate([
32
- dependencyFactory(),
33
- __param(0, inject("Runtime")),
34
- __metadata("design:paramtypes", [Function])
35
- ], MethodIdFactory);
36
- export { MethodIdFactory };
@@ -1,11 +0,0 @@
1
- export * from "../../protocol/src/state/assert/assert";
2
- export * from "./method/runtimeMethod";
3
- export * from "./module/decorator";
4
- export * from "./runtime/RuntimeModule";
5
- export * from "./runtime/Runtime";
6
- export * from "./state/InMemoryStateService";
7
- export * from "@proto-kit/protocol/src/state/StateServiceProvider";
8
- export * from "./state/decorator";
9
- export * from "./method/MethodParameterDecoder";
10
- export * from "./runtime/MethodIdResolver";
11
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC;AACvD,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oDAAoD,CAAC;AACnE,cAAc,mBAAmB,CAAC;AAClC,cAAc,iCAAiC,CAAC;AAChD,cAAc,4BAA4B,CAAC"}
@@ -1,10 +0,0 @@
1
- export * from "../../protocol/src/state/assert/assert";
2
- export * from "./method/runtimeMethod";
3
- export * from "./module/decorator";
4
- export * from "./runtime/RuntimeModule";
5
- export * from "./runtime/Runtime";
6
- export * from "./state/InMemoryStateService";
7
- export * from "@proto-kit/protocol/src/state/StateServiceProvider";
8
- export * from "./state/decorator";
9
- export * from "./method/MethodParameterDecoder";
10
- export * from "./runtime/MethodIdResolver";
@@ -1,22 +0,0 @@
1
- import { Field } from "snarkyjs";
2
- import { RuntimeModule } from "../runtime/RuntimeModule";
3
- export interface Fieldable {
4
- toFields: () => Field[];
5
- }
6
- export interface FromFieldClass {
7
- new: (...args: any[]) => any;
8
- fromFields: (fields: Field[]) => Fieldable;
9
- name: string;
10
- prototype: {
11
- _fields?: any[];
12
- };
13
- sizeInFields?: () => number;
14
- }
15
- export declare class MethodParameterDecoder {
16
- private readonly types;
17
- static fromMethod(target: RuntimeModule<unknown>, methodName: string): MethodParameterDecoder;
18
- private constructor();
19
- fromFields(fields: Field[]): Fieldable[];
20
- get fieldSize(): number;
21
- }
22
- //# sourceMappingURL=MethodParameterDecoder.d.ts.map