@proto-kit/module 0.1.1-develop.191 → 0.1.1-develop.239

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 (93) hide show
  1. package/dist/index.d.ts +0 -5
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +0 -5
  4. package/dist/method/MethodParameterDecoder.d.ts.map +1 -1
  5. package/dist/method/MethodParameterDecoder.js +1 -1
  6. package/dist/method/RuntimeMethodExecutionContext.d.ts +2 -2
  7. package/dist/method/RuntimeMethodExecutionContext.d.ts.map +1 -1
  8. package/dist/method/runtimeMethod.d.ts.map +1 -1
  9. package/dist/method/runtimeMethod.js +1 -2
  10. package/dist/module/src/factories/MethodIdFactory.d.ts +9 -0
  11. package/dist/module/src/factories/MethodIdFactory.d.ts.map +1 -0
  12. package/dist/module/src/factories/MethodIdFactory.js +36 -0
  13. package/dist/module/src/index.d.ts +11 -0
  14. package/dist/module/src/index.d.ts.map +1 -0
  15. package/dist/module/src/index.js +10 -0
  16. package/dist/module/src/method/MethodParameterDecoder.d.ts +22 -0
  17. package/dist/module/src/method/MethodParameterDecoder.d.ts.map +1 -0
  18. package/dist/module/src/method/MethodParameterDecoder.js +33 -0
  19. package/dist/module/src/method/runtimeMethod.d.ts +19 -0
  20. package/dist/module/src/method/runtimeMethod.d.ts.map +1 -0
  21. package/dist/module/src/method/runtimeMethod.js +123 -0
  22. package/dist/module/src/module/decorator.d.ts +8 -0
  23. package/dist/module/src/module/decorator.d.ts.map +1 -0
  24. package/dist/module/src/module/decorator.js +15 -0
  25. package/dist/module/src/runtime/MethodIdResolver.d.ts +18 -0
  26. package/dist/module/src/runtime/MethodIdResolver.d.ts.map +1 -0
  27. package/dist/module/src/runtime/MethodIdResolver.js +50 -0
  28. package/dist/module/src/runtime/Runtime.d.ts +72 -0
  29. package/dist/module/src/runtime/Runtime.d.ts.map +1 -0
  30. package/dist/module/src/runtime/Runtime.js +184 -0
  31. package/dist/module/src/runtime/RuntimeModule.d.ts +34 -0
  32. package/dist/module/src/runtime/RuntimeModule.d.ts.map +1 -0
  33. package/dist/module/src/runtime/RuntimeModule.js +56 -0
  34. package/dist/module/src/state/InMemoryStateService.d.ts +11 -0
  35. package/dist/module/src/state/InMemoryStateService.d.ts.map +1 -0
  36. package/dist/module/src/state/InMemoryStateService.js +21 -0
  37. package/dist/module/src/state/decorator.d.ts +7 -0
  38. package/dist/module/src/state/decorator.d.ts.map +1 -0
  39. package/dist/module/src/state/decorator.js +42 -0
  40. package/dist/protocol/src/model/Option.d.ts +98 -0
  41. package/dist/protocol/src/model/Option.d.ts.map +1 -0
  42. package/dist/protocol/src/model/Option.js +98 -0
  43. package/dist/protocol/src/model/StateTransition.d.ts +96 -0
  44. package/dist/protocol/src/model/StateTransition.d.ts.map +1 -0
  45. package/dist/protocol/src/model/StateTransition.js +65 -0
  46. package/dist/protocol/src/model/network/NetworkState.d.ts +64 -0
  47. package/dist/protocol/src/model/network/NetworkState.d.ts.map +1 -0
  48. package/dist/protocol/src/model/network/NetworkState.js +12 -0
  49. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +70 -0
  50. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +1 -0
  51. package/dist/protocol/src/model/transaction/ProtocolTransaction.js +18 -0
  52. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +63 -0
  53. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +1 -0
  54. package/dist/protocol/src/model/transaction/RuntimeTransaction.js +29 -0
  55. package/dist/protocol/src/state/assert/assert.d.ts +12 -0
  56. package/dist/protocol/src/state/assert/assert.d.ts.map +1 -0
  57. package/dist/protocol/src/state/assert/assert.js +23 -0
  58. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +60 -0
  59. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +1 -0
  60. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +105 -0
  61. package/dist/runtime/Runtime.d.ts +6 -5
  62. package/dist/runtime/Runtime.d.ts.map +1 -1
  63. package/dist/runtime/Runtime.js +5 -7
  64. package/dist/runtime/RuntimeModule.d.ts +1 -3
  65. package/dist/runtime/RuntimeModule.d.ts.map +1 -1
  66. package/dist/runtime/RuntimeModule.js +1 -1
  67. package/dist/state/InMemoryStateService.d.ts +1 -4
  68. package/dist/state/InMemoryStateService.d.ts.map +1 -1
  69. package/dist/state/StateMap.d.ts +3 -3
  70. package/dist/state/StateMap.d.ts.map +1 -1
  71. package/dist/state/decorator.d.ts.map +1 -1
  72. package/dist/state/decorator.js +1 -1
  73. package/package.json +3 -4
  74. package/src/index.ts +0 -5
  75. package/src/method/MethodParameterDecoder.ts +3 -1
  76. package/src/method/runtimeMethod.ts +1 -2
  77. package/src/runtime/Runtime.ts +12 -10
  78. package/src/runtime/RuntimeModule.ts +5 -6
  79. package/src/state/InMemoryStateService.ts +1 -5
  80. package/src/state/decorator.ts +2 -4
  81. package/test/Runtime.test.ts +2 -2
  82. package/test/modules/Admin.ts +1 -1
  83. package/test/modules/Balances.test.ts +13 -8
  84. package/test/modules/Balances.ts +7 -3
  85. package/test/modules/State.test.ts +88 -0
  86. package/test/modules/methodId.test.ts +0 -1
  87. package/test/transaction.test.ts +1 -1
  88. package/src/method/RuntimeMethodExecutionContext.ts +0 -111
  89. package/src/method/assert.test.ts +0 -49
  90. package/src/method/assert.ts +0 -27
  91. package/src/state/State.ts +0 -154
  92. package/src/state/StateMap.ts +0 -69
  93. package/src/state/StateServiceProvider.ts +0 -24
@@ -1 +1 @@
1
- {"version":3,"file":"InMemoryStateService.d.ts","sourceRoot":"","sources":["../../src/state/InMemoryStateService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,KAAK,EAAE,GAAG,SAAS,CAAC;IACzC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,SAAS,KAAK,IAAI,CAAC;CACvD;AAED;;GAEG;AACH,qBAAa,oBAAqB,YAAW,YAAY;IAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,CAAC,CAAM;IAEjD,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,KAAK,EAAE,GAAG,SAAS;IAIpC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,SAAS;CAWlD"}
1
+ {"version":3,"file":"InMemoryStateService.d.ts","sourceRoot":"","sources":["../../src/state/InMemoryStateService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD;;GAEG;AACH,qBAAa,oBAAqB,YAAW,YAAY;IAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,CAAC,CAAM;IAEjD,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,KAAK,EAAE,GAAG,SAAS;IAIpC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,SAAS;CAWlD"}
@@ -1,11 +1,11 @@
1
1
  import type { Field, FlexibleProvablePure } from "snarkyjs";
2
- import { type Option } from "@proto-kit/protocol";
2
+ import { type Option, ToFieldable } from "@proto-kit/protocol";
3
3
  import { WithRuntime, WithPath } from "./State.js";
4
4
  declare const StateMap_base: import("ts-mixer/dist/types/types.js").Class<any[], WithPath & WithRuntime, typeof WithPath & typeof WithRuntime, false>;
5
5
  /**
6
6
  * Map-like wrapper for state
7
7
  */
8
- export declare class StateMap<KeyType, ValueType> extends StateMap_base {
8
+ export declare class StateMap<KeyType, ValueType extends ToFieldable> extends StateMap_base {
9
9
  keyType: FlexibleProvablePure<KeyType>;
10
10
  valueType: FlexibleProvablePure<ValueType>;
11
11
  /**
@@ -15,7 +15,7 @@ export declare class StateMap<KeyType, ValueType> extends StateMap_base {
15
15
  * @param valueType - Type to be stored as a value
16
16
  * @returns State map with provided key and value types.
17
17
  */
18
- static from<KeyType, ValueType>(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>): StateMap<KeyType, ValueType>;
18
+ static from<KeyType, ValueType extends ToFieldable>(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>): StateMap<KeyType, ValueType>;
19
19
  constructor(keyType: FlexibleProvablePure<KeyType>, valueType: FlexibleProvablePure<ValueType>);
20
20
  getPath(key: KeyType): Field;
21
21
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"StateMap.d.ts","sourceRoot":"","sources":["../../src/state/StateMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAQ,KAAK,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGxD,OAAO,EAAS,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;;AAE1D;;GAEG;AAEH,qBAAa,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAE,SAAQ,aAA4B;IAgBnE,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC;IACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAhBnD;;;;;;OAMG;WACW,IAAI,CAAC,OAAO,EAAE,SAAS,EACnC,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;gBAMnC,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAK5C,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK;IAKnC;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC;IAU3C;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;CAS1C"}
1
+ {"version":3,"file":"StateMap.d.ts","sourceRoot":"","sources":["../../src/state/StateMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAQ,KAAK,MAAM,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGrE,OAAO,EAAS,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;;AAE1D;;GAEG;AAEH,qBAAa,QAAQ,CAAC,OAAO,EAAE,SAAS,SAAS,WAAW,CAAE,SAAQ,aAA4B;IAgBvF,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC;IACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAhBnD;;;;;;OAMG;WACW,IAAI,CAAC,OAAO,EAAE,SAAS,SAAS,WAAW,EACvD,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;gBAMnC,OAAO,EAAE,oBAAoB,CAAC,OAAO,CAAC,EACtC,SAAS,EAAE,oBAAoB,CAAC,SAAS,CAAC;IAK5C,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK;IAKnC;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC;IAU3C;;;;;OAKG;IACI,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;CAS1C"}
@@ -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;AAkBjE;;;GAGG;AACH,wBAAgB,KAAK,mGAGJ,MAAM,UAqCtB"}
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,UAqCtB"}
@@ -30,7 +30,7 @@ export function state() {
30
30
  value.path = path;
31
31
  // eslint-disable-next-line no-warning-comments
32
32
  // TODO: why is this complaining about `any`?
33
- value.runtime = self.runtime;
33
+ value.stateServiceProvider = self.runtime.stateServiceProvider;
34
34
  }
35
35
  return value;
36
36
  },
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.191+554ba10",
5
+ "version": "0.1.1-develop.239+0bf6910",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
@@ -21,8 +21,7 @@
21
21
  "@proto-kit/protocol": "*",
22
22
  "lodash": "^4.17.21",
23
23
  "loglevel": "^1.8.1",
24
- "reflect-metadata": "^0.1.13",
25
- "ts-mixer": "^6.0.3"
24
+ "reflect-metadata": "^0.1.13"
26
25
  },
27
26
  "devDependencies": {
28
27
  "@jest/globals": "^29.5.0",
@@ -32,5 +31,5 @@
32
31
  "snarkyjs": "0.12.0",
33
32
  "tsyringe": "^4.7.0"
34
33
  },
35
- "gitHead": "554ba102c48f41a62409ff01354582567ef4124a"
34
+ "gitHead": "0bf6910a100b9ae24fb00a253c9f58b5fd73ca55"
36
35
  }
package/src/index.ts CHANGED
@@ -1,13 +1,8 @@
1
- export * from "./method/RuntimeMethodExecutionContext";
2
- export * from "./method/assert";
3
1
  export * from "./method/runtimeMethod";
4
2
  export * from "./module/decorator";
5
3
  export * from "./runtime/RuntimeModule";
6
4
  export * from "./runtime/Runtime";
7
5
  export * from "./state/InMemoryStateService";
8
- export * from "./state/State";
9
- export * from "./state/StateMap";
10
- export * from "./state/StateServiceProvider";
11
6
  export * from "./state/decorator";
12
7
  export * from "./method/MethodParameterDecoder";
13
8
  export * from "./runtime/MethodIdResolver";
@@ -45,7 +45,9 @@ export class MethodParameterDecoder {
45
45
  if (fields.length < this.fieldSize) {
46
46
  throw errors.fieldLengthNotMatching(this.fieldSize, fields.length);
47
47
  }
48
+
48
49
  let stack = fields.slice();
50
+
49
51
  return this.types.map((type) => {
50
52
  const numberFieldsNeeded =
51
53
  type.prototype._fields?.length ?? type.sizeInFields?.() ?? -1;
@@ -63,6 +65,6 @@ export class MethodParameterDecoder {
63
65
  .map(
64
66
  (type) => type.prototype._fields?.length ?? type.sizeInFields?.() ?? 0
65
67
  )
66
- .reduce((a, b) => a + b);
68
+ .reduce((a, b) => a + b, 0);
67
69
  }
68
70
  }
@@ -5,13 +5,12 @@ import {
5
5
  DefaultProvableHashList,
6
6
  ProvableStateTransition,
7
7
  MethodPublicOutput,
8
+ RuntimeMethodExecutionContext
8
9
  } from "@proto-kit/protocol";
9
10
  import { DecoratedMethod, toProver, ZkProgrammable } from "@proto-kit/common";
10
11
 
11
12
  import type { RuntimeModule } from "../runtime/RuntimeModule.js";
12
13
 
13
- import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext.js";
14
-
15
14
  const errors = {
16
15
  runtimeNotProvided: (name: string) =>
17
16
  new Error(`Runtime was not provided for module: ${name}`),
@@ -13,19 +13,21 @@ import {
13
13
  WithZkProgrammable,
14
14
  AreProofsEnabled,
15
15
  } from "@proto-kit/common";
16
- import { MethodPublicOutput } from "@proto-kit/protocol";
16
+ import {
17
+ MethodPublicOutput,
18
+ StateServiceProvider,
19
+ StateService,
20
+ } from "@proto-kit/protocol";
17
21
 
18
22
  import {
19
23
  combineMethodName,
20
24
  isRuntimeMethod,
21
25
  toWrappedMethod,
22
26
  WrappedMethod,
23
- } from "../method/runtimeMethod.js";
24
- import { StateService } from "../state/InMemoryStateService.js";
25
- import { StateServiceProvider } from "../state/StateServiceProvider";
27
+ } from "../method/runtimeMethod";
26
28
  import { MethodIdFactory } from "../factories/MethodIdFactory";
27
29
 
28
- import { RuntimeModule } from "./RuntimeModule.js";
30
+ import { RuntimeModule } from "./RuntimeModule";
29
31
  import { MethodIdResolver } from "./MethodIdResolver";
30
32
 
31
33
  /**
@@ -47,7 +49,10 @@ const errors = {
47
49
  * Definition / required arguments for the Runtime class
48
50
  */
49
51
  export interface RuntimeDefinition<Modules extends RuntimeModulesRecord> {
50
- state: StateService;
52
+ /**
53
+ * @deprecated
54
+ */
55
+ state?: StateService;
51
56
  modules: Modules;
52
57
  config?: ModulesConfig<Modules>;
53
58
  }
@@ -200,9 +205,8 @@ export class Runtime<Modules extends RuntimeModulesRecord>
200
205
 
201
206
  public zkProgrammable: ZkProgrammable<undefined, MethodPublicOutput>;
202
207
 
203
- // eslint-disable-next-line no-warning-comments
204
- // TODO DI
205
208
  private readonly stateServiceProviderInstance = new StateServiceProvider(
209
+ // eslint-disable-next-line etc/no-deprecated
206
210
  this.definition.state
207
211
  );
208
212
 
@@ -215,8 +219,6 @@ export class Runtime<Modules extends RuntimeModulesRecord>
215
219
  super(definition);
216
220
  this.definition = definition;
217
221
  this.zkProgrammable = new RuntimeZkProgrammable<Modules>(this);
218
-
219
- // this.registerDependencyFactories([MethodIdFactory]);
220
222
  }
221
223
 
222
224
  // eslint-disable-next-line no-warning-comments
@@ -1,12 +1,13 @@
1
1
  import { ConfigurableModule, Presets } from "@proto-kit/common";
2
2
  import { container, injectable } from "tsyringe";
3
- import { NetworkState, RuntimeTransaction } from "@proto-kit/protocol";
4
-
5
3
  import {
4
+ NetworkState,
5
+ RuntimeTransaction,
6
+ StateService,
6
7
  RuntimeMethodExecutionContext,
7
8
  RuntimeMethodExecutionData,
8
- } from "../method/RuntimeMethodExecutionContext";
9
- import { StateService } from "../state/InMemoryStateService";
9
+ } from "@proto-kit/protocol";
10
+
10
11
  import { runtimeMethodNamesMetadataKey } from "../method/runtimeMethod";
11
12
 
12
13
  import type {
@@ -51,8 +52,6 @@ export class RuntimeModule<Config> extends ConfigurableModule<Config> {
51
52
 
52
53
  public name?: string;
53
54
 
54
- public test?: number;
55
-
56
55
  public runtime?: Runtime<RuntimeModulesRecord>;
57
56
 
58
57
  public constructor() {
@@ -1,9 +1,5 @@
1
1
  import { Field } from "snarkyjs";
2
-
3
- export interface StateService {
4
- get: (key: Field) => Field[] | undefined;
5
- set: (key: Field, value: Field[] | undefined) => void;
6
- }
2
+ import { StateService } from "@proto-kit/protocol";
7
3
 
8
4
  /**
9
5
  * Naive implementation of a StateService for testing purposes
@@ -1,9 +1,7 @@
1
- import { Path } from "@proto-kit/protocol";
1
+ import { Path, State } from "@proto-kit/protocol";
2
2
 
3
3
  import type { RuntimeModule } from "../runtime/RuntimeModule.js";
4
4
 
5
- import type { State } from "./State.js";
6
-
7
5
  const errors = {
8
6
  missingName: (className: string) =>
9
7
  new Error(
@@ -52,7 +50,7 @@ export function state() {
52
50
  // eslint-disable-next-line no-warning-comments
53
51
  // TODO: why is this complaining about `any`?
54
52
 
55
- value.runtime = self.runtime;
53
+ value.stateServiceProvider = self.runtime.stateServiceProvider;
56
54
  }
57
55
  return value;
58
56
  },
@@ -1,6 +1,6 @@
1
1
  import { Bool } from "snarkyjs";
2
2
 
3
- import { InMemoryStateService, Runtime } from "../src";
3
+ import { InMemoryStateService, MethodIdResolver, Runtime } from "../src";
4
4
 
5
5
  import { Balances } from "./modules/Balances";
6
6
 
@@ -32,7 +32,7 @@ describe("runtime", () => {
32
32
  const moduleName = "Balances";
33
33
  const methodName = "getTotalSupply";
34
34
 
35
- const methodId = runtime.methodIdResolver.getMethodId(
35
+ const methodId = runtime.dependencyContainer.resolve<MethodIdResolver>("MethodIdResolver").getMethodId(
36
36
  moduleName,
37
37
  methodName
38
38
  );
@@ -3,7 +3,7 @@ import { PublicKey } from "snarkyjs";
3
3
  import { runtimeModule } from "../../src/module/decorator.js";
4
4
  import { RuntimeModule } from "../../src/runtime/RuntimeModule.js";
5
5
  import { runtimeMethod } from "../../src/method/runtimeMethod.js";
6
- import { assert } from "../../src/method/assert.js";
6
+ import { assert } from "@proto-kit/protocol";
7
7
 
8
8
  interface AdminConfig {
9
9
  publicKey: string;
@@ -10,14 +10,17 @@ import {
10
10
  UInt64,
11
11
  } from "snarkyjs";
12
12
  import { container } from "tsyringe";
13
- import { type ProvableStateTransition, Path } from "@proto-kit/protocol";
13
+ import {
14
+ type ProvableStateTransition,
15
+ Path,
16
+ MethodPublicOutput,
17
+ StateService, NetworkState, RuntimeTransaction
18
+ } from "@proto-kit/protocol";
14
19
 
15
20
  import {
16
21
  InMemoryStateService,
17
- StateService,
18
22
  } from "../../src/state/InMemoryStateService.js";
19
23
  import {
20
- MethodPublicOutput,
21
24
  Runtime,
22
25
  RuntimeMethodExecutionContext,
23
26
  } from "../../src";
@@ -61,13 +64,15 @@ describe("balances", () => {
61
64
  },
62
65
  });
63
66
 
64
- runtime.appChain = {
65
- areProofsEnabled: false,
67
+ runtime.dependencyContainer.register("AppChain", {
68
+ useValue: {
69
+ areProofsEnabled: false,
66
70
 
67
- setProofsEnabled(areProofsEnabled) {
68
- this.areProofsEnabled = areProofsEnabled;
71
+ setProofsEnabled(areProofsEnabled: boolean) {
72
+ this.areProofsEnabled = areProofsEnabled;
73
+ },
69
74
  },
70
- };
75
+ });
71
76
 
72
77
  runtime.configure({
73
78
  Admin: {
@@ -2,9 +2,9 @@ import { Bool, PublicKey, UInt64 } from "snarkyjs";
2
2
  import { Option } from "@proto-kit/protocol";
3
3
  import { Presets } from "@proto-kit/common";
4
4
 
5
- import { State } from "../../src/state/State.js";
5
+ import { State } from "../../../protocol/src/state/State.js";
6
6
  import { state } from "../../src/state/decorator.js";
7
- import { StateMap } from "../../src/state/StateMap.js";
7
+ import { StateMap } from "../../../protocol/src/state/StateMap.js";
8
8
  import { RuntimeModule, runtimeMethod, runtimeModule } from "../../src";
9
9
 
10
10
  import { Admin } from "./Admin.js";
@@ -50,7 +50,11 @@ export class Balances extends RuntimeModule<BalancesConfig> {
50
50
  }
51
51
 
52
52
  @runtimeMethod()
53
- public setBalanceIf() {
53
+ public transientState() {
54
+ const totalSupply = this.totalSupply.get();
55
+ this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(100));
54
56
 
57
+ const totalSupply2 = this.totalSupply.get();
58
+ this.totalSupply.set(totalSupply2.orElse(UInt64.zero).add(100));
55
59
  }
56
60
  }
@@ -0,0 +1,88 @@
1
+ import "reflect-metadata";
2
+ import { Bool, PublicKey, UInt64 } from "snarkyjs";
3
+ import { container } from "tsyringe";
4
+ import { NetworkState, Option, RuntimeTransaction, StateService } from "@proto-kit/protocol";
5
+
6
+ import {
7
+ InMemoryStateService,
8
+ Runtime,
9
+ RuntimeMethodExecutionContext,
10
+ } from "../../src";
11
+
12
+ import { Admin } from "./Admin";
13
+ import { Balances } from "./Balances";
14
+
15
+ describe("transient state", () => {
16
+ let balances: Balances;
17
+
18
+ let state: StateService;
19
+
20
+ let runtime: Runtime<{
21
+ Admin: typeof Admin;
22
+ Balances: typeof Balances;
23
+ }>;
24
+
25
+ function createChain() {
26
+ state = new InMemoryStateService();
27
+
28
+ runtime = Runtime.from({
29
+ state,
30
+
31
+ modules: {
32
+ Admin,
33
+ Balances,
34
+ },
35
+ });
36
+
37
+ runtime.dependencyContainer.register("AppChain", {
38
+ useValue: {
39
+ areProofsEnabled: false,
40
+
41
+ setProofsEnabled(areProofsEnabled: boolean) {
42
+ this.areProofsEnabled = areProofsEnabled;
43
+ },
44
+ },
45
+ });
46
+
47
+ runtime.configure({
48
+ Admin: {
49
+ publicKey: PublicKey.empty().toBase58(),
50
+ },
51
+
52
+ Balances: {
53
+ test: Bool(true),
54
+ },
55
+ });
56
+
57
+ balances = runtime.resolve("Balances");
58
+ }
59
+
60
+ beforeEach(() => {
61
+ createChain();
62
+ });
63
+
64
+ it("should track previously set state", () => {
65
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
66
+ executionContext.setup({
67
+ networkState: new NetworkState({ block: { height: UInt64.zero } }),
68
+ transaction: undefined as unknown as RuntimeTransaction,
69
+ });
70
+ balances.transientState();
71
+
72
+ const stateTransitions = executionContext
73
+ .current()
74
+ .result.stateTransitions.map((stateTransition) =>
75
+ stateTransition.toProvable()
76
+ );
77
+
78
+ const expectedLastOption = Option.fromValue(
79
+ UInt64.from(200),
80
+ UInt64
81
+ ).toProvable();
82
+
83
+ const last = stateTransitions.at(-1);
84
+
85
+ expect(last).toBeDefined();
86
+ expect(last!.to.value).toStrictEqual(expectedLastOption.value);
87
+ });
88
+ });
@@ -56,7 +56,6 @@ describe("methodId", () => {
56
56
  ["Admin", "isAdminWithAVeryVeryVeryVeryLongName"],
57
57
  ["Balance", "getTotalSupply"],
58
58
  ["Balance", "getBalance"],
59
- ["Balance", "setBalanceIf"],
60
59
  ])(
61
60
  "should pass and encode correctly",
62
61
  (givenModuleName, givenMethodName) => {
@@ -11,7 +11,7 @@ import {
11
11
  import { runtimeModule } from "../src/module/decorator";
12
12
  import { RuntimeModule } from "../src/runtime/RuntimeModule";
13
13
  import { state } from "../src/state/decorator";
14
- import { StateMap } from "../src/state/StateMap";
14
+ import { StateMap } from "@proto-kit/protocol";
15
15
 
16
16
  interface BalancesConfig {}
17
17
  @runtimeModule()
@@ -1,111 +0,0 @@
1
- import { Bool } from "snarkyjs";
2
- import { singleton } from "tsyringe";
3
- import type { StateTransition, NetworkState } from "@proto-kit/protocol";
4
- import {
5
- ProvableMethodExecutionContext,
6
- ProvableMethodExecutionResult,
7
- } from "@proto-kit/common";
8
- import { RuntimeTransaction } from "@proto-kit/protocol/src/model/transaction/RuntimeTransaction";
9
-
10
- const errors = {
11
- setupNotCalled: () =>
12
- new Error(
13
- "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"
14
- ),
15
- };
16
-
17
- export class RuntimeProvableMethodExecutionResult extends ProvableMethodExecutionResult {
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
- public stateTransitions: StateTransition<any>[] = [];
20
-
21
- public status: Bool = Bool(true);
22
-
23
- public statusMessage?: string;
24
- }
25
-
26
- export interface RuntimeMethodExecutionData {
27
- transaction: RuntimeTransaction;
28
- networkState: NetworkState;
29
- }
30
-
31
- /**
32
- * Execution context used to wrap runtime module methods,
33
- * allowing them to post relevant information (such as execution status)
34
- * into the context without any unnecessary 'prop drilling'.
35
- */
36
- @singleton()
37
- export class RuntimeMethodExecutionContext extends ProvableMethodExecutionContext {
38
- public methods: string[] = [];
39
-
40
- public input: RuntimeMethodExecutionData | undefined;
41
-
42
- // The input corresponding to the current result
43
- private lastInput: RuntimeMethodExecutionData | undefined;
44
-
45
- public override result = new RuntimeProvableMethodExecutionResult();
46
-
47
- private assertSetupCalled(): asserts this is {
48
- input: RuntimeMethodExecutionData;
49
- } {
50
- if (this.input === undefined) {
51
- throw errors.setupNotCalled();
52
- }
53
- }
54
-
55
- /**
56
- * Adds an in-method generated state transition to the current context
57
- * @param stateTransition - State transition to add to the context
58
- */
59
- public addStateTransition<Value>(stateTransition: StateTransition<Value>) {
60
- this.assertSetupCalled();
61
- this.result.stateTransitions.push(stateTransition);
62
- }
63
-
64
- /**
65
- * @param message - Status message to acompany the current status
66
- */
67
- public setStatusMessage(message?: string) {
68
- this.assertSetupCalled();
69
- this.result.statusMessage ??= message;
70
- }
71
-
72
- /**
73
- * @param status - Execution status of the current method
74
- */
75
- public setStatus(status: Bool) {
76
- this.assertSetupCalled();
77
- this.result.status = status;
78
- }
79
-
80
- /**
81
- * @param input Input witness data required for a runtime execution
82
- */
83
- public setup(input: RuntimeMethodExecutionData) {
84
- this.input = input;
85
- }
86
-
87
- /**
88
- * Manually clears/resets the execution context
89
- */
90
- public clear() {
91
- this.result = new RuntimeProvableMethodExecutionResult();
92
- }
93
-
94
- public afterMethod() {
95
- super.afterMethod();
96
- this.lastInput = this.input;
97
- this.input = undefined;
98
- }
99
-
100
- /**
101
- * Had to override current() otherwise it would not infer
102
- * the type of result correctly (parent type would be reused)
103
- */
104
- public current() {
105
- return {
106
- isFinished: this.isFinished,
107
- result: this.result,
108
- input: this.lastInput,
109
- };
110
- }
111
- }
@@ -1,49 +0,0 @@
1
- import "reflect-metadata";
2
-
3
- import { Bool } from "snarkyjs";
4
- import { container } from "tsyringe";
5
-
6
- import { assert } from "./assert.js";
7
- import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext.js";
8
-
9
- describe("assert", () => {
10
- const defaultStatusMessage = "something went wrong";
11
- const executionContext = container.resolve(RuntimeMethodExecutionContext);
12
-
13
- beforeEach(() => {
14
- executionContext.beforeMethod("testConstructor", "test");
15
- });
16
-
17
- afterEach(() => {
18
- executionContext.afterMethod();
19
- });
20
-
21
- describe.each([
22
- [true, undefined],
23
- [false, undefined],
24
- [false, defaultStatusMessage],
25
- ])("status and message propagation", (status, statusMessage) => {
26
- it("should propagate the assertion status and message", () => {
27
- expect.assertions(2);
28
-
29
- assert(Bool(status), statusMessage);
30
-
31
- const { status: resultStatus, statusMessage: resultStatusMessage } =
32
- executionContext.current().result;
33
-
34
- expect(status).toBe(resultStatus.toBoolean());
35
- expect(statusMessage).toBe(resultStatusMessage);
36
- });
37
- });
38
-
39
- it("should keep a false status, once it was already set", () => {
40
- expect.assertions(1);
41
-
42
- assert(Bool(false));
43
- assert(Bool(true));
44
-
45
- const { status } = executionContext.current().result;
46
-
47
- expect(status.toBoolean()).toBe(false);
48
- });
49
- });
@@ -1,27 +0,0 @@
1
- import { Bool, Provable } from "snarkyjs";
2
- import { container } from "tsyringe";
3
-
4
- import { RuntimeMethodExecutionContext } from "./RuntimeMethodExecutionContext";
5
- import { log } from "@proto-kit/common";
6
-
7
- /**
8
- * Maintains an execution status of the current runtime module method,
9
- * while prioritizing one-time failures. The assertion won't change the
10
- * execution status if it has previously failed at least once within the
11
- * same method execution context.
12
- *
13
- * @param condition - Result of the assertion made about the execution status
14
- * @param message - Optional message describing the prior status
15
- */
16
- export function assert(condition: Bool, message?: string) {
17
- const executionContext = container.resolve(RuntimeMethodExecutionContext);
18
- const previousStatus = executionContext.current().result.status;
19
- const status = Provable.if(previousStatus, Bool, condition, previousStatus);
20
-
21
- if (!condition.toBoolean()) {
22
- log.debug("Assertion failed: ", message);
23
- executionContext.setStatusMessage(message);
24
- }
25
-
26
- executionContext.setStatus(status);
27
- }