@proto-kit/module 0.1.1-develop.600 → 0.1.1-develop.833

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/LICENSE.md +201 -201
  2. package/dist/method/MethodParameterDecoder.d.ts +10 -10
  3. package/dist/method/MethodParameterDecoder.js +40 -40
  4. package/dist/method/MethodParameterEncoder.d.ts +15 -8
  5. package/dist/method/MethodParameterEncoder.d.ts.map +1 -1
  6. package/dist/method/MethodParameterEncoder.js +122 -48
  7. package/dist/method/runtimeMethod.d.ts +6 -5
  8. package/dist/method/runtimeMethod.d.ts.map +1 -1
  9. package/dist/method/runtimeMethod.js +13 -26
  10. package/dist/runtime/MethodIdResolver.d.ts +7 -0
  11. package/dist/runtime/MethodIdResolver.d.ts.map +1 -1
  12. package/dist/runtime/MethodIdResolver.js +35 -2
  13. package/dist/runtime/Runtime.d.ts +6 -5
  14. package/dist/runtime/Runtime.d.ts.map +1 -1
  15. package/dist/runtime/Runtime.js +29 -22
  16. package/dist/runtime/RuntimeEnvironment.d.ts +2 -2
  17. package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -1
  18. package/dist/runtime/RuntimeModule.d.ts +2 -2
  19. package/dist/runtime/RuntimeModule.d.ts.map +1 -1
  20. package/dist/runtime/RuntimeModule.js +10 -8
  21. package/dist/src/factories/MethodIdFactory.d.ts +10 -0
  22. package/dist/src/factories/MethodIdFactory.d.ts.map +1 -0
  23. package/dist/src/factories/MethodIdFactory.js +10 -0
  24. package/dist/src/index.d.ts +11 -0
  25. package/dist/src/index.d.ts.map +1 -0
  26. package/dist/src/index.js +10 -0
  27. package/dist/src/method/MethodParameterEncoder.d.ts +22 -0
  28. package/dist/src/method/MethodParameterEncoder.d.ts.map +1 -0
  29. package/dist/src/method/MethodParameterEncoder.js +103 -0
  30. package/dist/src/method/runtimeMethod.d.ts +26 -0
  31. package/dist/src/method/runtimeMethod.d.ts.map +1 -0
  32. package/dist/src/method/runtimeMethod.js +164 -0
  33. package/dist/src/module/decorator.d.ts +8 -0
  34. package/dist/src/module/decorator.d.ts.map +1 -0
  35. package/dist/src/module/decorator.js +15 -0
  36. package/dist/src/runtime/MethodIdResolver.d.ts +20 -0
  37. package/dist/src/runtime/MethodIdResolver.d.ts.map +1 -0
  38. package/dist/src/runtime/MethodIdResolver.js +100 -0
  39. package/dist/src/runtime/Runtime.d.ts +71 -0
  40. package/dist/src/runtime/Runtime.d.ts.map +1 -0
  41. package/dist/src/runtime/Runtime.js +215 -0
  42. package/dist/src/runtime/RuntimeEnvironment.d.ts +10 -0
  43. package/dist/src/runtime/RuntimeEnvironment.d.ts.map +1 -0
  44. package/dist/src/runtime/RuntimeEnvironment.js +1 -0
  45. package/dist/src/runtime/RuntimeModule.d.ts +26 -0
  46. package/dist/src/runtime/RuntimeModule.d.ts.map +1 -0
  47. package/dist/src/runtime/RuntimeModule.js +85 -0
  48. package/dist/src/state/InMemoryStateService.d.ts +15 -0
  49. package/dist/src/state/InMemoryStateService.d.ts.map +1 -0
  50. package/dist/src/state/InMemoryStateService.js +28 -0
  51. package/dist/src/state/decorator.d.ts +7 -0
  52. package/dist/src/state/decorator.d.ts.map +1 -0
  53. package/dist/src/state/decorator.js +39 -0
  54. package/dist/state/InMemoryStateService.d.ts +10 -6
  55. package/dist/state/InMemoryStateService.d.ts.map +1 -1
  56. package/dist/state/InMemoryStateService.js +10 -8
  57. package/dist/state/decorator.d.ts.map +1 -1
  58. package/dist/state/decorator.js +0 -3
  59. package/dist/test/Runtime.test.d.ts +2 -0
  60. package/dist/test/Runtime.test.d.ts.map +1 -0
  61. package/dist/test/Runtime.test.js +24 -0
  62. package/dist/test/TestingRuntime.d.ts +7 -0
  63. package/dist/test/TestingRuntime.d.ts.map +1 -0
  64. package/dist/test/TestingRuntime.js +29 -0
  65. package/dist/test/method/runtimeMethod.test.d.ts +2 -0
  66. package/dist/test/method/runtimeMethod.test.d.ts.map +1 -0
  67. package/dist/test/method/runtimeMethod.test.js +30 -0
  68. package/dist/test/modules/Admin.d.ts +10 -0
  69. package/dist/test/modules/Admin.d.ts.map +1 -0
  70. package/dist/test/modules/Admin.js +29 -0
  71. package/dist/test/modules/Balances.d.ts +23 -0
  72. package/dist/test/modules/Balances.d.ts.map +1 -0
  73. package/dist/test/modules/Balances.js +98 -0
  74. package/dist/test/modules/Balances.test.d.ts +2 -0
  75. package/dist/test/modules/Balances.test.d.ts.map +1 -0
  76. package/dist/test/modules/Balances.test.js +201 -0
  77. package/dist/test/modules/MethodIdResolver.test.d.ts +2 -0
  78. package/dist/test/modules/MethodIdResolver.test.d.ts.map +1 -0
  79. package/dist/test/modules/MethodIdResolver.test.js +67 -0
  80. package/dist/test/modules/State.test.d.ts +2 -0
  81. package/dist/test/modules/State.test.d.ts.map +1 -0
  82. package/dist/test/modules/State.test.js +42 -0
  83. package/dist/test/runtimeMethod.test.d.ts +2 -0
  84. package/dist/test/runtimeMethod.test.d.ts.map +1 -0
  85. package/dist/test/runtimeMethod.test.js +50 -0
  86. package/package.json +5 -5
  87. package/src/method/MethodParameterEncoder.ts +192 -81
  88. package/src/method/runtimeMethod.ts +29 -44
  89. package/src/runtime/MethodIdResolver.ts +51 -1
  90. package/src/runtime/Runtime.ts +48 -29
  91. package/src/runtime/RuntimeEnvironment.ts +4 -7
  92. package/src/runtime/RuntimeModule.ts +13 -15
  93. package/src/state/InMemoryStateService.ts +13 -13
  94. package/src/state/decorator.ts +1 -3
  95. package/test/Runtime.test.ts +68 -41
  96. package/test/TestingRuntime.ts +45 -0
  97. package/test/method/MethodParameterEncoder.test.ts +152 -0
  98. package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +2 -2
  99. package/test/modules/Admin.ts +3 -3
  100. package/test/modules/Balances.test.ts +57 -61
  101. package/test/modules/Balances.ts +15 -18
  102. package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +14 -23
  103. package/test/modules/State.test.ts +41 -50
  104. package/test/runtimeMethod.test.ts +19 -32
  105. package/test/tsconfig.json +7 -0
  106. package/tsconfig.json +2 -2
  107. package/test/transaction.test.ts +0 -82
  108. package/tsconfig.test.json +0 -9
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TestingRuntime.d.ts","sourceRoot":"","sources":["../../test/TestingRuntime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIlD,OAAO,EAAE,oBAAoB,EAAE,OAAO,EAAE,oBAAoB,EAAE,MAAM,QAAQ,CAAC;AAE7E,wBAAgB,oBAAoB,CAAC,OAAO,SAAS,oBAAoB,EACvE,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,GAC7B;IACD,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B,KAAK,EAAE,oBAAoB,CAAC;CAC7B,CAgCA"}
@@ -0,0 +1,29 @@
1
+ import { StateServiceProvider } from "@proto-kit/protocol";
2
+ import { container } from "tsyringe";
3
+ import { InMemoryStateService, Runtime } from "../src";
4
+ export function createTestingRuntime(modules, config) {
5
+ const state = new InMemoryStateService();
6
+ const Runtimeclass = Runtime.from({
7
+ modules,
8
+ });
9
+ const runtime = new Runtimeclass();
10
+ runtime.configure(config);
11
+ runtime.create(() => container.createChildContainer());
12
+ runtime.dependencyContainer.register("AreProofsEnabled", {
13
+ useValue: {
14
+ areProofsEnabled: false,
15
+ setProofsEnabled(areProofsEnabled) {
16
+ this.areProofsEnabled = areProofsEnabled;
17
+ },
18
+ },
19
+ });
20
+ runtime.registerValue({
21
+ StateServiceProvider: new StateServiceProvider(),
22
+ Runtime: runtime,
23
+ });
24
+ runtime.stateServiceProvider.setCurrentStateService(state);
25
+ return {
26
+ runtime,
27
+ state,
28
+ };
29
+ }
@@ -0,0 +1,2 @@
1
+ import "reflect-metadata";
2
+ //# sourceMappingURL=runtimeMethod.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtimeMethod.test.d.ts","sourceRoot":"","sources":["../../../test/method/runtimeMethod.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,30 @@
1
+ import "reflect-metadata";
2
+ import { Bool, Field } from "o1js";
3
+ import { Option, StateTransition } from "@proto-kit/protocol";
4
+ import { toStateTransitionsHash } from "../../src/method/runtimeMethod";
5
+ describe.skip("toStateTransitionsHash", () => {
6
+ const noneStateTransition = StateTransition.from(Field(0), new Option(Bool(false), Field(0), Field));
7
+ const someStateTransition = StateTransition.from(Field(0), new Option(Bool(true), Field(0), Field));
8
+ it.each([
9
+ [
10
+ [noneStateTransition],
11
+ "7067243248312463521220230733411703436580237248681301130001246160136823979683",
12
+ ],
13
+ [
14
+ [someStateTransition],
15
+ "12841542804403638489097503092490970035615082088155587790175618374946575398395",
16
+ ],
17
+ [
18
+ [noneStateTransition, someStateTransition],
19
+ "20641278138648130746922286021889771603940136555847557324578879341962747943601",
20
+ ],
21
+ [
22
+ [someStateTransition, noneStateTransition],
23
+ "10362098987098600767020985423446775093761176563902435645494178193997179006954",
24
+ ],
25
+ ])("should calculate a hash of all provided state transitions", (stateTransitions, expectedHash) => {
26
+ expect.assertions(1);
27
+ const hash = toStateTransitionsHash(stateTransitions).toString();
28
+ expect(hash).toBe(expectedHash);
29
+ });
30
+ });
@@ -0,0 +1,10 @@
1
+ import { PublicKey } from "o1js";
2
+ import { RuntimeModule } from "../../src/runtime/RuntimeModule.js";
3
+ interface AdminConfig {
4
+ publicKey: string;
5
+ }
6
+ export declare class Admin extends RuntimeModule<AdminConfig> {
7
+ isAdmin(publicKey: PublicKey): void;
8
+ }
9
+ export {};
10
+ //# sourceMappingURL=Admin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Admin.d.ts","sourceRoot":"","sources":["../../../test/modules/Admin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAIjC,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AAGnE,UAAU,WAAW;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,qBACa,KAAM,SAAQ,aAAa,CAAC,WAAW,CAAC;IAE5C,OAAO,CAAC,SAAS,EAAE,SAAS;CAIpC"}
@@ -0,0 +1,29 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { PublicKey } from "o1js";
11
+ import { assert } from "@proto-kit/protocol";
12
+ import { runtimeModule } from "../../src/module/decorator.js";
13
+ import { RuntimeModule } from "../../src/runtime/RuntimeModule.js";
14
+ import { runtimeMethod } from "../../src/method/runtimeMethod.js";
15
+ export let Admin = class Admin extends RuntimeModule {
16
+ isAdmin(publicKey) {
17
+ const admin = PublicKey.empty().toConstant();
18
+ assert(admin.equals(publicKey));
19
+ }
20
+ };
21
+ __decorate([
22
+ runtimeMethod(),
23
+ __metadata("design:type", Function),
24
+ __metadata("design:paramtypes", [PublicKey]),
25
+ __metadata("design:returntype", void 0)
26
+ ], Admin.prototype, "isAdmin", null);
27
+ Admin = __decorate([
28
+ runtimeModule()
29
+ ], Admin);
@@ -0,0 +1,23 @@
1
+ import { PublicKey, UInt64 } from "o1js";
2
+ import { State, StateMap } from "@proto-kit/protocol";
3
+ import { RuntimeModule } from "../../src";
4
+ import { Admin } from "./Admin.js";
5
+ interface BalancesConfig {
6
+ }
7
+ export declare class Balances extends RuntimeModule<BalancesConfig> {
8
+ admin: Admin;
9
+ /**
10
+ * We use `satisfies` here in order to be able to access
11
+ * presets by key in a type safe way.
12
+ */
13
+ static presets: {};
14
+ totalSupply: State<UInt64>;
15
+ balances: StateMap<PublicKey, UInt64>;
16
+ constructor(admin: Admin);
17
+ getTotalSupply(): void;
18
+ setTotalSupply(): void;
19
+ getBalance(address: PublicKey): void;
20
+ transientState(): void;
21
+ }
22
+ export {};
23
+ //# sourceMappingURL=Balances.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Balances.d.ts","sourceRoot":"","sources":["../../../test/modules/Balances.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAGtD,OAAO,EAAE,aAAa,EAAuC,MAAM,WAAW,CAAC;AAE/E,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,UAAU,cAAc;CAAG;AAE3B,qBACa,QAAS,SAAQ,aAAa,CAAC,cAAc,CAAC;IAc/B,KAAK,EAAE,KAAK;IAbtC;;;OAGG;IACH,OAAc,OAAO,KAAwC;IAE7C,WAAW,gBAA8B;IAEzC,QAAQ,8BAGtB;gBAEwB,KAAK,EAAE,KAAK;IAK/B,cAAc;IAKd,cAAc;IAMd,UAAU,CAAC,OAAO,EAAE,SAAS;IAK7B,cAAc;CAOtB"}
@@ -0,0 +1,98 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { PublicKey, UInt64 } from "o1js";
11
+ import { State, StateMap } from "@proto-kit/protocol";
12
+ import { RuntimeModule, runtimeMethod, runtimeModule, state } from "../../src";
13
+ import { Admin } from "./Admin.js";
14
+ export let Balances = class Balances extends RuntimeModule {
15
+ constructor(admin) {
16
+ super();
17
+ Object.defineProperty(this, "admin", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: admin
22
+ });
23
+ Object.defineProperty(this, "totalSupply", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: State.from(UInt64)
28
+ });
29
+ Object.defineProperty(this, "balances", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: StateMap.from(PublicKey, UInt64)
34
+ });
35
+ }
36
+ getTotalSupply() {
37
+ this.totalSupply.get();
38
+ }
39
+ setTotalSupply() {
40
+ this.totalSupply.set(UInt64.from(20));
41
+ this.admin.isAdmin(this.transaction.sender.value);
42
+ }
43
+ getBalance(address) {
44
+ this.balances.get(address).orElse(UInt64.zero);
45
+ }
46
+ transientState() {
47
+ const totalSupply = this.totalSupply.get();
48
+ this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(100));
49
+ const totalSupply2 = this.totalSupply.get();
50
+ this.totalSupply.set(totalSupply2.orElse(UInt64.zero).add(100));
51
+ }
52
+ };
53
+ /**
54
+ * We use `satisfies` here in order to be able to access
55
+ * presets by key in a type safe way.
56
+ */
57
+ Object.defineProperty(Balances, "presets", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: {}
62
+ });
63
+ __decorate([
64
+ state(),
65
+ __metadata("design:type", Object)
66
+ ], Balances.prototype, "totalSupply", void 0);
67
+ __decorate([
68
+ state(),
69
+ __metadata("design:type", Object)
70
+ ], Balances.prototype, "balances", void 0);
71
+ __decorate([
72
+ runtimeMethod(),
73
+ __metadata("design:type", Function),
74
+ __metadata("design:paramtypes", []),
75
+ __metadata("design:returntype", void 0)
76
+ ], Balances.prototype, "getTotalSupply", null);
77
+ __decorate([
78
+ runtimeMethod(),
79
+ __metadata("design:type", Function),
80
+ __metadata("design:paramtypes", []),
81
+ __metadata("design:returntype", void 0)
82
+ ], Balances.prototype, "setTotalSupply", null);
83
+ __decorate([
84
+ runtimeMethod(),
85
+ __metadata("design:type", Function),
86
+ __metadata("design:paramtypes", [PublicKey]),
87
+ __metadata("design:returntype", void 0)
88
+ ], Balances.prototype, "getBalance", null);
89
+ __decorate([
90
+ runtimeMethod(),
91
+ __metadata("design:type", Function),
92
+ __metadata("design:paramtypes", []),
93
+ __metadata("design:returntype", void 0)
94
+ ], Balances.prototype, "transientState", null);
95
+ Balances = __decorate([
96
+ runtimeModule(),
97
+ __metadata("design:paramtypes", [Admin])
98
+ ], Balances);
@@ -0,0 +1,2 @@
1
+ import "reflect-metadata";
2
+ //# sourceMappingURL=Balances.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Balances.test.d.ts","sourceRoot":"","sources":["../../../test/modules/Balances.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,201 @@
1
+ import "reflect-metadata";
2
+ import { Field, Poseidon, PrivateKey, PublicKey, UInt64 } from "o1js";
3
+ import { container } from "tsyringe";
4
+ import { Path, RuntimeMethodExecutionContext, RuntimeTransaction, NetworkState, } from "@proto-kit/protocol";
5
+ import { createTestingRuntime } from "../TestingRuntime";
6
+ import { Balances } from "./Balances.js";
7
+ import { Admin } from "./Admin.js";
8
+ describe("balances", () => {
9
+ let balances;
10
+ let state;
11
+ let runtime;
12
+ function getStateValue(path) {
13
+ if (!path) {
14
+ throw new Error("Path not found");
15
+ }
16
+ const stateValue = state.get(path);
17
+ if (!stateValue) {
18
+ throw new Error("stateValue is undefined");
19
+ }
20
+ return stateValue;
21
+ }
22
+ function createChain() {
23
+ ({ runtime, state } = createTestingRuntime({
24
+ Balances,
25
+ Admin,
26
+ }, {
27
+ Admin: {
28
+ publicKey: PublicKey.empty().toBase58(),
29
+ },
30
+ Balances: {},
31
+ }));
32
+ balances = runtime.resolve("Balances");
33
+ state.set(balances.totalSupply.path, UInt64.from(10).toFields());
34
+ }
35
+ describe.skip("compile and prove", () => {
36
+ beforeAll(createChain);
37
+ // Disabled until we implement a mechanism to enable/disable compiling tests
38
+ it("should compile and prove a method execution", async () => {
39
+ expect.assertions(3);
40
+ runtime.zkProgrammable.appChain?.setProofsEnabled(true);
41
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
42
+ executionContext.setup({
43
+ transaction: RuntimeTransaction.dummyTransaction(),
44
+ networkState: NetworkState.empty(),
45
+ });
46
+ const expectedStateTransitionsHash = "1439144406936083177718146178121957896974210157062549589517697792374542035761";
47
+ const expectedStatus = true;
48
+ await runtime.zkProgrammable.zkProgram.compile();
49
+ balances.getTotalSupply();
50
+ const { result } = executionContext.current();
51
+ const proof = await result.prove();
52
+ const verified = await runtime.zkProgrammable.zkProgram.verify(proof);
53
+ runtime.zkProgrammable.appChain?.setProofsEnabled(false);
54
+ expect(verified).toBe(true);
55
+ expect(proof.publicOutput.stateTransitionsHash.toString()).toStrictEqual(expectedStateTransitionsHash);
56
+ expect(proof.publicOutput.status.toBoolean()).toBe(expectedStatus);
57
+ }, 180000);
58
+ });
59
+ describe("getTotalSupply", () => {
60
+ beforeAll(createChain);
61
+ describe("state transitions", () => {
62
+ let stateTransitions;
63
+ beforeEach(() => {
64
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
65
+ executionContext.setup({
66
+ transaction: RuntimeTransaction.dummyTransaction(),
67
+ networkState: NetworkState.empty(),
68
+ });
69
+ balances.getTotalSupply();
70
+ stateTransitions = executionContext
71
+ .current()
72
+ .result.stateTransitions.map((stateTransition) => stateTransition.toProvable());
73
+ });
74
+ it("should return a single state transition", () => {
75
+ expect.assertions(1);
76
+ expect(stateTransitions).toHaveLength(1);
77
+ });
78
+ it("should have a state transition for the correct path", () => {
79
+ expect.assertions(1);
80
+ const path = Path.fromProperty("Balances", "totalSupply");
81
+ expect(stateTransitions[0].path.toString()).toStrictEqual(path.toString());
82
+ });
83
+ it("should produce a from-only state transition", () => {
84
+ expect.assertions(3);
85
+ const [stateTransition] = stateTransitions;
86
+ const value = UInt64.fromFields(getStateValue(balances.totalSupply.path));
87
+ const treeValue = Poseidon.hash(value.toFields());
88
+ expect(stateTransition.from.isSome.toBoolean()).toBe(true);
89
+ expect(stateTransition.from.value.toString()).toBe(treeValue.toString());
90
+ expect(stateTransition.to.isSome.toBoolean()).toBe(false);
91
+ });
92
+ });
93
+ describe("state transitions from empty state", () => {
94
+ let stateTransitions;
95
+ beforeAll(() => {
96
+ createChain();
97
+ state.set(balances.totalSupply.path, undefined);
98
+ });
99
+ beforeEach(() => {
100
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
101
+ executionContext.setup({
102
+ transaction: RuntimeTransaction.dummyTransaction(),
103
+ networkState: NetworkState.empty(),
104
+ });
105
+ balances.getTotalSupply();
106
+ stateTransitions = executionContext
107
+ .current()
108
+ .result.stateTransitions.map((stateTransition) => stateTransition.toProvable());
109
+ });
110
+ it("should return a single state transition", () => {
111
+ expect.assertions(1);
112
+ expect(stateTransitions).toHaveLength(1);
113
+ });
114
+ it("should have a state transition for the correct path", () => {
115
+ expect.assertions(1);
116
+ const path = Path.fromProperty("Balances", "totalSupply");
117
+ expect(stateTransitions[0].path.toString()).toStrictEqual(path.toString());
118
+ });
119
+ it("should produce a from-only state transition", () => {
120
+ expect.assertions(3);
121
+ const [stateTransition] = stateTransitions;
122
+ const treeValue = Field(0);
123
+ expect(stateTransition.from.isSome.toBoolean()).toBe(true);
124
+ expect(stateTransition.from.value.toString()).toBe(treeValue.toString());
125
+ expect(stateTransition.to.isSome.toBoolean()).toBe(false);
126
+ });
127
+ });
128
+ });
129
+ describe("setTotalSupply", () => {
130
+ beforeAll(createChain);
131
+ describe("state transitions", () => {
132
+ let stateTransitions;
133
+ beforeEach(() => {
134
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
135
+ executionContext.setup({
136
+ transaction: RuntimeTransaction.dummyTransaction(),
137
+ networkState: NetworkState.empty(),
138
+ });
139
+ balances.setTotalSupply();
140
+ stateTransitions = executionContext
141
+ .current()
142
+ .result.stateTransitions.map((stateTransition) => stateTransition.toProvable());
143
+ });
144
+ it("should return a single state transition", () => {
145
+ expect.assertions(1);
146
+ expect(stateTransitions).toHaveLength(1);
147
+ });
148
+ it("should have a state transition for the correct path", () => {
149
+ expect.assertions(1);
150
+ const path = Path.fromProperty("Balances", "totalSupply");
151
+ expect(stateTransitions[0].path.toString()).toStrictEqual(path.toString());
152
+ });
153
+ it("should produce a from-to state transition", () => {
154
+ expect.assertions(4);
155
+ const [stateTransition] = stateTransitions;
156
+ const fromValue = UInt64.fromFields(getStateValue(balances.totalSupply.path));
157
+ const fromTreeValue = Poseidon.hash(fromValue.toFields());
158
+ const toValue = UInt64.from(20);
159
+ const toTreeValue = Poseidon.hash(toValue.toFields());
160
+ expect(stateTransition.from.isSome.toBoolean()).toBe(true);
161
+ expect(stateTransition.from.value.toString()).toBe(fromTreeValue.toString());
162
+ expect(stateTransition.to.isSome.toBoolean()).toBe(true);
163
+ expect(stateTransition.to.value.toString()).toBe(toTreeValue.toString());
164
+ });
165
+ });
166
+ });
167
+ describe("getBalance", () => {
168
+ beforeAll(createChain);
169
+ describe("state transitions", () => {
170
+ let stateTransitions;
171
+ const address = PrivateKey.random().toPublicKey();
172
+ beforeEach(() => {
173
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
174
+ executionContext.setup({
175
+ transaction: RuntimeTransaction.dummyTransaction(),
176
+ networkState: NetworkState.empty(),
177
+ });
178
+ balances.getBalance(address);
179
+ stateTransitions = executionContext
180
+ .current()
181
+ .result.stateTransitions.map((stateTransition) => stateTransition.toProvable());
182
+ });
183
+ it("should return a single state transition", () => {
184
+ expect.assertions(1);
185
+ expect(stateTransitions).toHaveLength(1);
186
+ });
187
+ it("should have a state transition for the correct path", () => {
188
+ expect.assertions(1);
189
+ const path = Path.fromKey(Path.fromProperty("Balances", "balances"), PublicKey, address);
190
+ expect(stateTransitions[0].path.toString()).toStrictEqual(path.toString());
191
+ });
192
+ it("should produce a from-only state transition, for non-existing state", () => {
193
+ expect.assertions(3);
194
+ const [stateTransition] = stateTransitions;
195
+ expect(stateTransition.from.isSome.toBoolean()).toBe(true);
196
+ expect(stateTransition.from.value.toString()).toBe(Field(0).toString());
197
+ expect(stateTransition.to.isSome.toBoolean()).toBe(false);
198
+ });
199
+ });
200
+ });
201
+ });
@@ -0,0 +1,2 @@
1
+ import "reflect-metadata";
2
+ //# sourceMappingURL=MethodIdResolver.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MethodIdResolver.test.d.ts","sourceRoot":"","sources":["../../../test/modules/MethodIdResolver.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,67 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import "reflect-metadata";
11
+ import { assert } from "@proto-kit/protocol";
12
+ import { Field } from "o1js";
13
+ import { beforeAll } from "@jest/globals";
14
+ import { container } from "tsyringe";
15
+ import { runtimeMethod, RuntimeModule, runtimeModule } from "../../src";
16
+ import { createTestingRuntime } from "../TestingRuntime";
17
+ import { Balances } from "./Balances";
18
+ let Admin = class Admin extends RuntimeModule {
19
+ isAdminWithAVeryVeryVeryVeryLongName() {
20
+ assert(Field(1).equals(Field(1)));
21
+ }
22
+ };
23
+ __decorate([
24
+ runtimeMethod(),
25
+ __metadata("design:type", Function),
26
+ __metadata("design:paramtypes", []),
27
+ __metadata("design:returntype", void 0)
28
+ ], Admin.prototype, "isAdminWithAVeryVeryVeryVeryLongName", null);
29
+ Admin = __decorate([
30
+ runtimeModule()
31
+ ], Admin);
32
+ describe("methodId", () => {
33
+ let runtime;
34
+ let resolver;
35
+ beforeAll(() => {
36
+ container.clearInstances();
37
+ ({ runtime } = createTestingRuntime({
38
+ Admin,
39
+ Balance: Balances,
40
+ }, {
41
+ Admin: {},
42
+ Balance: {},
43
+ }));
44
+ resolver =
45
+ runtime.dependencyContainer.resolve("MethodIdResolver");
46
+ });
47
+ it.each([
48
+ ["Admin", "isAdminWithAVeryVeryVeryVeryLongName"],
49
+ ["Balance", "getTotalSupply"],
50
+ ["Balance", "getBalance"],
51
+ ])("should pass and encode correctly", (givenModuleName, givenMethodName) => {
52
+ expect.assertions(2);
53
+ const methodId = resolver.getMethodId(givenModuleName, givenMethodName);
54
+ const [moduleName, methodName] = resolver.getMethodNameFromId(methodId) ?? [
55
+ undefined,
56
+ undefined,
57
+ ];
58
+ expect(moduleName).toBe(givenModuleName);
59
+ expect(methodName).toBe(givenMethodName);
60
+ });
61
+ it("should fail for invalid module name", () => {
62
+ expect.assertions(1);
63
+ expect(() => {
64
+ resolver.getMethodId("Admin2", "isAdminWithAVeryVeryVeryVeryLongName");
65
+ }).toThrow("Only known module names are allowed, using unknown module name: Admin2");
66
+ });
67
+ });
@@ -0,0 +1,2 @@
1
+ import "reflect-metadata";
2
+ //# sourceMappingURL=State.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"State.test.d.ts","sourceRoot":"","sources":["../../../test/modules/State.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,42 @@
1
+ import "reflect-metadata";
2
+ import { PublicKey, UInt64 } from "o1js";
3
+ import { container } from "tsyringe";
4
+ import { NetworkState, Option, RuntimeMethodExecutionContext, RuntimeTransaction, } from "@proto-kit/protocol";
5
+ import { createTestingRuntime } from "../TestingRuntime";
6
+ import { Admin } from "./Admin";
7
+ import { Balances } from "./Balances";
8
+ describe("transient state", () => {
9
+ let balances;
10
+ let runtime;
11
+ function createChain() {
12
+ ({ runtime } = createTestingRuntime({
13
+ Admin,
14
+ Balances,
15
+ }, {
16
+ Admin: {
17
+ publicKey: PublicKey.empty().toBase58(),
18
+ },
19
+ Balances: {},
20
+ }));
21
+ balances = runtime.resolve("Balances");
22
+ }
23
+ beforeEach(() => {
24
+ createChain();
25
+ });
26
+ it("should track previously set state", () => {
27
+ expect.assertions(2);
28
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
29
+ executionContext.setup({
30
+ networkState: NetworkState.empty(),
31
+ transaction: RuntimeTransaction.dummyTransaction(),
32
+ });
33
+ balances.transientState();
34
+ const stateTransitions = executionContext
35
+ .current()
36
+ .result.stateTransitions.map((stateTransition) => stateTransition.toProvable());
37
+ const expectedLastOption = Option.fromValue(UInt64.from(200), UInt64).toProvable();
38
+ const last = stateTransitions.at(-1);
39
+ expect(last).toBeDefined();
40
+ expect(last.to.value).toStrictEqual(expectedLastOption.value);
41
+ });
42
+ });
@@ -0,0 +1,2 @@
1
+ import "reflect-metadata";
2
+ //# sourceMappingURL=runtimeMethod.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtimeMethod.test.d.ts","sourceRoot":"","sources":["../../test/runtimeMethod.test.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC"}
@@ -0,0 +1,50 @@
1
+ import "reflect-metadata";
2
+ import { PublicKey } from "o1js";
3
+ import { NetworkState, RuntimeMethodExecutionContext, RuntimeTransaction, } from "@proto-kit/protocol";
4
+ import { container } from "tsyringe";
5
+ import { log } from "@proto-kit/common";
6
+ import { MethodParameterEncoder } from "../src";
7
+ import { Balances } from "./modules/Balances";
8
+ import { createTestingRuntime } from "./TestingRuntime";
9
+ describe("runtimeMethod", () => {
10
+ const parameters = [PublicKey.empty()];
11
+ let runtime;
12
+ beforeEach(() => {
13
+ log.setLevel(log.levels.DEBUG);
14
+ ({ runtime } = createTestingRuntime({
15
+ Balances,
16
+ }, {
17
+ Balances: {},
18
+ }));
19
+ });
20
+ it("should create correct param types", () => {
21
+ expect.assertions(1 + parameters.length);
22
+ const module = runtime.resolve("Balances");
23
+ const decoder = MethodParameterEncoder.fromMethod(module, "getBalance");
24
+ const recodedParameters = decoder.decodeFields(parameters.flatMap((x) => x.toFields()));
25
+ expect(parameters).toHaveLength(recodedParameters.length);
26
+ parameters.forEach((parameter, index) => {
27
+ expect(parameter).toStrictEqual(recodedParameters[index]);
28
+ });
29
+ });
30
+ it("should throw on incorrect methodId on tx", async () => {
31
+ expect.assertions(1);
32
+ const context = container.resolve(RuntimeMethodExecutionContext);
33
+ runtime.registerValue({
34
+ AppChain: {
35
+ areProofsEnabled: false,
36
+ },
37
+ });
38
+ context.setup({
39
+ transaction: RuntimeTransaction.dummyTransaction(),
40
+ networkState: NetworkState.empty(),
41
+ });
42
+ const module = runtime.resolve("Balances");
43
+ module.getBalance(PublicKey.empty());
44
+ context.setup({
45
+ transaction: RuntimeTransaction.dummyTransaction(),
46
+ networkState: NetworkState.empty(),
47
+ });
48
+ await expect(context.current().result.prover()).rejects.toThrow("Runtimemethod called with wrong methodId on the transaction object");
49
+ });
50
+ });