@proto-kit/module 0.1.1-develop.211 → 0.1.1-develop.2137

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/dist/factories/MethodIdFactory.d.ts +6 -6
  2. package/dist/factories/MethodIdFactory.d.ts.map +1 -1
  3. package/dist/factories/MethodIdFactory.js +10 -34
  4. package/dist/factories/MethodIdFactory.js.map +1 -0
  5. package/dist/index.d.ts +4 -7
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +5 -7
  8. package/dist/index.js.map +1 -0
  9. package/dist/messages/OutgoingMessage.d.ts +96 -0
  10. package/dist/messages/OutgoingMessage.d.ts.map +1 -0
  11. package/dist/messages/OutgoingMessage.js +68 -0
  12. package/dist/messages/OutgoingMessage.js.map +1 -0
  13. package/dist/messages/OutgoingMessages.d.ts +231 -0
  14. package/dist/messages/OutgoingMessages.d.ts.map +1 -0
  15. package/dist/messages/OutgoingMessages.js +67 -0
  16. package/dist/messages/OutgoingMessages.js.map +1 -0
  17. package/dist/method/MethodParameterEncoder.d.ts +26 -0
  18. package/dist/method/MethodParameterEncoder.d.ts.map +1 -0
  19. package/dist/method/MethodParameterEncoder.js +169 -0
  20. package/dist/method/MethodParameterEncoder.js.map +1 -0
  21. package/dist/method/runtimeMethod.d.ts +21 -5
  22. package/dist/method/runtimeMethod.d.ts.map +1 -1
  23. package/dist/method/runtimeMethod.js +69 -25
  24. package/dist/method/runtimeMethod.js.map +1 -0
  25. package/dist/module/decorator.js +1 -0
  26. package/dist/module/decorator.js.map +1 -0
  27. package/dist/runtime/MethodIdResolver.d.ts +10 -8
  28. package/dist/runtime/MethodIdResolver.d.ts.map +1 -1
  29. package/dist/runtime/MethodIdResolver.js +75 -33
  30. package/dist/runtime/MethodIdResolver.js.map +1 -0
  31. package/dist/runtime/Runtime.d.ts +20 -26
  32. package/dist/runtime/Runtime.d.ts.map +1 -1
  33. package/dist/runtime/Runtime.js +117 -62
  34. package/dist/runtime/Runtime.js.map +1 -0
  35. package/dist/runtime/RuntimeEnvironment.d.ts +10 -0
  36. package/dist/runtime/RuntimeEnvironment.d.ts.map +1 -0
  37. package/dist/runtime/RuntimeEnvironment.js +2 -0
  38. package/dist/runtime/RuntimeEnvironment.js.map +1 -0
  39. package/dist/runtime/RuntimeModule.d.ts +18 -15
  40. package/dist/runtime/RuntimeModule.d.ts.map +1 -1
  41. package/dist/runtime/RuntimeModule.js +33 -8
  42. package/dist/runtime/RuntimeModule.js.map +1 -0
  43. package/dist/state/InMemoryStateService.d.ts +11 -10
  44. package/dist/state/InMemoryStateService.d.ts.map +1 -1
  45. package/dist/state/InMemoryStateService.js +11 -8
  46. package/dist/state/InMemoryStateService.js.map +1 -0
  47. package/dist/state/decorator.d.ts.map +1 -1
  48. package/dist/state/decorator.js +2 -4
  49. package/dist/state/decorator.js.map +1 -0
  50. package/dist/testing/TestingRuntime.d.ts +8 -0
  51. package/dist/testing/TestingRuntime.d.ts.map +1 -0
  52. package/dist/testing/TestingRuntime.js +31 -0
  53. package/dist/testing/TestingRuntime.js.map +1 -0
  54. package/jest.config.cjs +12 -1
  55. package/package.json +9 -9
  56. package/src/factories/MethodIdFactory.ts +10 -17
  57. package/src/index.ts +4 -4
  58. package/src/messages/OutgoingMessages.ts +124 -0
  59. package/src/method/MethodParameterEncoder.ts +262 -0
  60. package/src/method/runtimeMethod.ts +131 -33
  61. package/src/runtime/MethodIdResolver.ts +85 -46
  62. package/src/runtime/Runtime.ts +187 -96
  63. package/src/runtime/RuntimeEnvironment.ts +16 -0
  64. package/src/runtime/RuntimeModule.ts +58 -27
  65. package/src/state/InMemoryStateService.ts +14 -14
  66. package/test/Runtime.test.ts +68 -42
  67. package/test/TestingRuntime.ts +43 -0
  68. package/test/messages/message.test.ts +42 -0
  69. package/test/method/MethodParameterEncoder.test.ts +124 -0
  70. package/test/method/runtimeMethod-fail.test.ts +53 -0
  71. package/{src/method/decorator.test.ts → test/method/runtimeMethod.test.ts} +3 -3
  72. package/test/modules/Admin.ts +4 -4
  73. package/test/modules/Balances.test.ts +88 -79
  74. package/test/modules/Balances.ts +15 -21
  75. package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +24 -35
  76. package/test/modules/State.test.ts +46 -53
  77. package/test/runtimeMethod.test.ts +192 -20
  78. package/test/tsconfig.json +7 -0
  79. package/tsconfig.json +2 -2
  80. package/LICENSE.md +0 -201
  81. package/dist/method/MethodParameterDecoder.d.ts +0 -22
  82. package/dist/method/MethodParameterDecoder.d.ts.map +0 -1
  83. package/dist/method/MethodParameterDecoder.js +0 -33
  84. package/dist/method/RuntimeMethodExecutionContext.d.ts +0 -57
  85. package/dist/method/RuntimeMethodExecutionContext.d.ts.map +0 -1
  86. package/dist/method/RuntimeMethodExecutionContext.js +0 -92
  87. package/dist/method/assert.d.ts +0 -12
  88. package/dist/method/assert.d.ts.map +0 -1
  89. package/dist/method/assert.js +0 -23
  90. package/dist/module/src/factories/MethodIdFactory.d.ts +0 -9
  91. package/dist/module/src/factories/MethodIdFactory.d.ts.map +0 -1
  92. package/dist/module/src/factories/MethodIdFactory.js +0 -36
  93. package/dist/module/src/index.d.ts +0 -11
  94. package/dist/module/src/index.d.ts.map +0 -1
  95. package/dist/module/src/index.js +0 -10
  96. package/dist/module/src/method/MethodParameterDecoder.d.ts +0 -22
  97. package/dist/module/src/method/MethodParameterDecoder.d.ts.map +0 -1
  98. package/dist/module/src/method/MethodParameterDecoder.js +0 -33
  99. package/dist/module/src/method/runtimeMethod.d.ts +0 -19
  100. package/dist/module/src/method/runtimeMethod.d.ts.map +0 -1
  101. package/dist/module/src/method/runtimeMethod.js +0 -123
  102. package/dist/module/src/module/decorator.d.ts +0 -8
  103. package/dist/module/src/module/decorator.d.ts.map +0 -1
  104. package/dist/module/src/module/decorator.js +0 -15
  105. package/dist/module/src/runtime/MethodIdResolver.d.ts +0 -18
  106. package/dist/module/src/runtime/MethodIdResolver.d.ts.map +0 -1
  107. package/dist/module/src/runtime/MethodIdResolver.js +0 -50
  108. package/dist/module/src/runtime/Runtime.d.ts +0 -72
  109. package/dist/module/src/runtime/Runtime.d.ts.map +0 -1
  110. package/dist/module/src/runtime/Runtime.js +0 -184
  111. package/dist/module/src/runtime/RuntimeModule.d.ts +0 -35
  112. package/dist/module/src/runtime/RuntimeModule.d.ts.map +0 -1
  113. package/dist/module/src/runtime/RuntimeModule.js +0 -56
  114. package/dist/module/src/state/InMemoryStateService.d.ts +0 -11
  115. package/dist/module/src/state/InMemoryStateService.d.ts.map +0 -1
  116. package/dist/module/src/state/InMemoryStateService.js +0 -21
  117. package/dist/module/src/state/decorator.d.ts +0 -7
  118. package/dist/module/src/state/decorator.d.ts.map +0 -1
  119. package/dist/module/src/state/decorator.js +0 -42
  120. package/dist/protocol/src/model/Option.d.ts +0 -98
  121. package/dist/protocol/src/model/Option.d.ts.map +0 -1
  122. package/dist/protocol/src/model/Option.js +0 -98
  123. package/dist/protocol/src/model/StateTransition.d.ts +0 -96
  124. package/dist/protocol/src/model/StateTransition.d.ts.map +0 -1
  125. package/dist/protocol/src/model/StateTransition.js +0 -65
  126. package/dist/protocol/src/model/network/NetworkState.d.ts +0 -64
  127. package/dist/protocol/src/model/network/NetworkState.d.ts.map +0 -1
  128. package/dist/protocol/src/model/network/NetworkState.js +0 -12
  129. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +0 -70
  130. package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +0 -1
  131. package/dist/protocol/src/model/transaction/ProtocolTransaction.js +0 -18
  132. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +0 -63
  133. package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +0 -1
  134. package/dist/protocol/src/model/transaction/RuntimeTransaction.js +0 -29
  135. package/dist/protocol/src/state/assert/assert.d.ts +0 -12
  136. package/dist/protocol/src/state/assert/assert.d.ts.map +0 -1
  137. package/dist/protocol/src/state/assert/assert.js +0 -23
  138. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +0 -60
  139. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +0 -1
  140. package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +0 -105
  141. package/dist/state/State.d.ts +0 -65
  142. package/dist/state/State.d.ts.map +0 -1
  143. package/dist/state/State.js +0 -114
  144. package/dist/state/StateMap.d.ts +0 -37
  145. package/dist/state/StateMap.d.ts.map +0 -1
  146. package/dist/state/StateMap.js +0 -56
  147. package/dist/state/StateServiceProvider.d.ts +0 -10
  148. package/dist/state/StateServiceProvider.d.ts.map +0 -1
  149. package/dist/state/StateServiceProvider.js +0 -34
  150. package/src/method/MethodParameterDecoder.ts +0 -70
  151. package/src/state/decorator.ts +0 -63
  152. package/test/state/MerkleTree.test.ts +0 -95
  153. package/test/state/MockAsyncMerkleStore.ts +0 -28
  154. package/test/transaction.test.ts +0 -82
  155. package/tsconfig.test.json +0 -9
@@ -1,29 +1,19 @@
1
- /* eslint-disable max-lines */
2
1
  import "reflect-metadata";
3
- import {
4
- Bool,
5
- Field,
6
- Poseidon,
7
- PrivateKey,
8
- Proof,
9
- PublicKey,
10
- UInt64,
11
- } from "snarkyjs";
2
+ import { Field, Poseidon, PrivateKey, Proof, PublicKey, UInt64 } from "o1js";
12
3
  import { container } from "tsyringe";
13
4
  import {
14
5
  type ProvableStateTransition,
15
6
  Path,
16
7
  MethodPublicOutput,
17
- StateService, NetworkState, RuntimeTransaction
8
+ SimpleAsyncStateService,
9
+ RuntimeMethodExecutionContext,
10
+ RuntimeTransaction,
11
+ NetworkState,
12
+ PROTOKIT_PREFIXES,
18
13
  } from "@proto-kit/protocol";
19
14
 
20
- import {
21
- InMemoryStateService,
22
- } from "../../src/state/InMemoryStateService.js";
23
- import {
24
- Runtime,
25
- RuntimeMethodExecutionContext,
26
- } from "../../src";
15
+ import { Runtime } from "../../src";
16
+ import { createTestingRuntime } from "../TestingRuntime";
27
17
 
28
18
  import { Balances } from "./Balances.js";
29
19
  import { Admin } from "./Admin.js";
@@ -31,19 +21,19 @@ import { Admin } from "./Admin.js";
31
21
  describe("balances", () => {
32
22
  let balances: Balances;
33
23
 
34
- let state: StateService;
24
+ let state: SimpleAsyncStateService;
35
25
 
36
26
  let runtime: Runtime<{
37
27
  Admin: typeof Admin;
38
28
  Balances: typeof Balances;
39
29
  }>;
40
30
 
41
- function getStateValue(path: Field | undefined) {
31
+ async function getStateValue(path: Field | undefined) {
42
32
  if (!path) {
43
33
  throw new Error("Path not found");
44
34
  }
45
35
 
46
- const stateValue = state.get(path);
36
+ const stateValue = await state.get(path);
47
37
 
48
38
  if (!stateValue) {
49
39
  throw new Error("stateValue is undefined");
@@ -52,72 +42,56 @@ describe("balances", () => {
52
42
  return stateValue;
53
43
  }
54
44
 
55
- function createChain() {
56
- state = new InMemoryStateService();
57
-
58
- runtime = Runtime.from({
59
- state,
60
-
61
- modules: {
62
- Admin,
45
+ async function createChain() {
46
+ ({ runtime, state } = createTestingRuntime(
47
+ {
63
48
  Balances,
49
+ Admin,
64
50
  },
65
- });
66
-
67
- runtime.dependencyContainer.register("AppChain", {
68
- useValue: {
69
- areProofsEnabled: false,
70
-
71
- setProofsEnabled(areProofsEnabled: boolean) {
72
- this.areProofsEnabled = areProofsEnabled;
51
+ {
52
+ Admin: {
53
+ publicKey: PublicKey.empty<typeof PublicKey>().toBase58(),
73
54
  },
74
- },
75
- });
76
55
 
77
- runtime.configure({
78
- Admin: {
79
- publicKey: PublicKey.empty().toBase58(),
80
- },
81
-
82
- Balances: {
83
- test: Bool(true),
84
- },
85
- });
56
+ Balances: {},
57
+ }
58
+ ));
86
59
 
87
60
  balances = runtime.resolve("Balances");
88
61
 
89
- state.set(
90
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
91
- balances.totalSupply.path!,
92
- UInt64.from(10).toFields()
93
- );
62
+ await state.set(balances.totalSupply.path!, UInt64.from(10).toFields());
94
63
  }
95
64
 
96
- describe("compile and prove", () => {
65
+ describe.skip("compile and prove", () => {
97
66
  beforeAll(createChain);
98
67
 
99
68
  // Disabled until we implement a mechanism to enable/disable compiling tests
100
69
  it("should compile and prove a method execution", async () => {
101
70
  expect.assertions(3);
102
71
 
103
- runtime.zkProgrammable.appChain?.setProofsEnabled(true);
72
+ runtime.zkProgrammable.areProofsEnabled?.setProofsEnabled(true);
104
73
 
105
74
  const executionContext = container.resolve(RuntimeMethodExecutionContext);
75
+ executionContext.setup({
76
+ transaction: RuntimeTransaction.dummyTransaction(),
77
+ networkState: NetworkState.empty(),
78
+ });
79
+
106
80
  const expectedStateTransitionsHash =
107
81
  "1439144406936083177718146178121957896974210157062549589517697792374542035761";
108
82
  const expectedStatus = true;
109
83
 
110
- await runtime.zkProgrammable.zkProgram.compile();
84
+ await runtime.zkProgrammable.zkProgram[0].compile();
111
85
 
112
- balances.getTotalSupply();
86
+ await balances.getTotalSupply();
113
87
 
114
88
  const { result } = executionContext.current();
115
89
 
116
90
  const proof = await result.prove<Proof<undefined, MethodPublicOutput>>();
117
91
 
118
- const verified = await runtime.zkProgrammable.zkProgram.verify(proof);
92
+ const verified = await runtime.zkProgrammable.zkProgram[0].verify(proof);
119
93
 
120
- runtime.zkProgrammable.appChain?.setProofsEnabled(false);
94
+ runtime.zkProgrammable.areProofsEnabled?.setProofsEnabled(false);
121
95
 
122
96
  expect(verified).toBe(true);
123
97
 
@@ -134,11 +108,15 @@ describe("balances", () => {
134
108
  describe("state transitions", () => {
135
109
  let stateTransitions: ProvableStateTransition[];
136
110
 
137
- beforeEach(() => {
111
+ beforeEach(async () => {
138
112
  const executionContext = container.resolve(
139
113
  RuntimeMethodExecutionContext
140
114
  );
141
- balances.getTotalSupply();
115
+ executionContext.setup({
116
+ transaction: RuntimeTransaction.dummyTransaction(),
117
+ networkState: NetworkState.empty(),
118
+ });
119
+ await balances.getTotalSupply();
142
120
 
143
121
  stateTransitions = executionContext
144
122
  .current()
@@ -155,20 +133,24 @@ describe("balances", () => {
155
133
  it("should have a state transition for the correct path", () => {
156
134
  expect.assertions(1);
157
135
 
158
- const path = Path.fromProperty("Balances", "totalSupply");
136
+ const path = Path.fromProperty(
137
+ "Balances",
138
+ "totalSupply",
139
+ PROTOKIT_PREFIXES.STATE_RUNTIME
140
+ );
159
141
 
160
142
  expect(stateTransitions[0].path.toString()).toStrictEqual(
161
143
  path.toString()
162
144
  );
163
145
  });
164
146
 
165
- it("should produce a from-only state transition", () => {
147
+ it("should produce a from-only state transition", async () => {
166
148
  expect.assertions(3);
167
149
 
168
150
  const [stateTransition] = stateTransitions;
169
151
 
170
152
  const value = UInt64.fromFields(
171
- getStateValue(balances.totalSupply.path)
153
+ await getStateValue(balances.totalSupply.path)
172
154
  );
173
155
  const treeValue = Poseidon.hash(value.toFields());
174
156
 
@@ -183,17 +165,22 @@ describe("balances", () => {
183
165
  describe("state transitions from empty state", () => {
184
166
  let stateTransitions: ProvableStateTransition[];
185
167
 
186
- beforeAll(() => {
187
- createChain();
188
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
189
- state.set(balances.totalSupply.path!, undefined);
168
+ beforeAll(async () => {
169
+ await createChain();
170
+
171
+ await state.set(balances.totalSupply.path!, undefined);
190
172
  });
191
173
 
192
- beforeEach(() => {
174
+ beforeEach(async () => {
193
175
  const executionContext = container.resolve(
194
176
  RuntimeMethodExecutionContext
195
177
  );
196
- balances.getTotalSupply();
178
+ executionContext.setup({
179
+ transaction: RuntimeTransaction.dummyTransaction(),
180
+ networkState: NetworkState.empty(),
181
+ });
182
+
183
+ await balances.getTotalSupply();
197
184
 
198
185
  stateTransitions = executionContext
199
186
  .current()
@@ -210,7 +197,11 @@ describe("balances", () => {
210
197
  it("should have a state transition for the correct path", () => {
211
198
  expect.assertions(1);
212
199
 
213
- const path = Path.fromProperty("Balances", "totalSupply");
200
+ const path = Path.fromProperty(
201
+ "Balances",
202
+ "totalSupply",
203
+ PROTOKIT_PREFIXES.STATE_RUNTIME
204
+ );
214
205
 
215
206
  expect(stateTransitions[0].path.toString()).toStrictEqual(
216
207
  path.toString()
@@ -239,11 +230,16 @@ describe("balances", () => {
239
230
  describe("state transitions", () => {
240
231
  let stateTransitions: ProvableStateTransition[];
241
232
 
242
- beforeEach(() => {
233
+ beforeEach(async () => {
243
234
  const executionContext = container.resolve(
244
235
  RuntimeMethodExecutionContext
245
236
  );
246
- balances.setTotalSupply();
237
+ executionContext.setup({
238
+ transaction: RuntimeTransaction.dummyTransaction(),
239
+ networkState: NetworkState.empty(),
240
+ });
241
+
242
+ await balances.setTotalSupply();
247
243
 
248
244
  stateTransitions = executionContext
249
245
  .current()
@@ -260,19 +256,23 @@ describe("balances", () => {
260
256
  it("should have a state transition for the correct path", () => {
261
257
  expect.assertions(1);
262
258
 
263
- const path = Path.fromProperty("Balances", "totalSupply");
259
+ const path = Path.fromProperty(
260
+ "Balances",
261
+ "totalSupply",
262
+ PROTOKIT_PREFIXES.STATE_RUNTIME
263
+ );
264
264
 
265
265
  expect(stateTransitions[0].path.toString()).toStrictEqual(
266
266
  path.toString()
267
267
  );
268
268
  });
269
269
 
270
- it("should produce a from-to state transition", () => {
270
+ it("should produce a from-to state transition", async () => {
271
271
  expect.assertions(4);
272
272
 
273
273
  const [stateTransition] = stateTransitions;
274
274
  const fromValue = UInt64.fromFields(
275
- getStateValue(balances.totalSupply.path)
275
+ await getStateValue(balances.totalSupply.path)
276
276
  );
277
277
  const fromTreeValue = Poseidon.hash(fromValue.toFields());
278
278
 
@@ -299,11 +299,16 @@ describe("balances", () => {
299
299
  let stateTransitions: ProvableStateTransition[];
300
300
  const address = PrivateKey.random().toPublicKey();
301
301
 
302
- beforeEach(() => {
302
+ beforeEach(async () => {
303
303
  const executionContext = container.resolve(
304
304
  RuntimeMethodExecutionContext
305
305
  );
306
- balances.getBalance(address);
306
+ executionContext.setup({
307
+ transaction: RuntimeTransaction.dummyTransaction(),
308
+ networkState: NetworkState.empty(),
309
+ });
310
+
311
+ await balances.getBalance(address);
307
312
 
308
313
  stateTransitions = executionContext
309
314
  .current()
@@ -321,7 +326,11 @@ describe("balances", () => {
321
326
  expect.assertions(1);
322
327
 
323
328
  const path = Path.fromKey<PublicKey>(
324
- Path.fromProperty("Balances", "balances"),
329
+ Path.fromProperty(
330
+ "Balances",
331
+ "balances",
332
+ PROTOKIT_PREFIXES.STATE_RUNTIME
333
+ ),
325
334
  PublicKey,
326
335
  address
327
336
  );
@@ -1,17 +1,12 @@
1
- import { Bool, PublicKey, UInt64 } from "snarkyjs";
2
- import { Option } from "@proto-kit/protocol";
1
+ import { PublicKey, UInt64 } from "o1js";
2
+ import { State, StateMap, state } 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
5
  import { RuntimeModule, runtimeMethod, runtimeModule } from "../../src";
9
6
 
10
7
  import { Admin } from "./Admin.js";
11
8
 
12
- interface BalancesConfig {
13
- test: Bool;
14
- }
9
+ interface BalancesConfig {}
15
10
 
16
11
  @runtimeModule()
17
12
  export class Balances extends RuntimeModule<BalancesConfig> {
@@ -33,28 +28,27 @@ export class Balances extends RuntimeModule<BalancesConfig> {
33
28
  }
34
29
 
35
30
  @runtimeMethod()
36
- public getTotalSupply() {
37
- this.totalSupply.get();
31
+ public async getTotalSupply() {
32
+ await this.totalSupply.get();
38
33
  }
39
34
 
40
35
  @runtimeMethod()
41
- public setTotalSupply() {
42
- // eslint-disable-next-line @typescript-eslint/no-magic-numbers
43
- this.totalSupply.set(UInt64.from(20));
44
- this.admin.isAdmin(this.transaction.sender);
36
+ public async setTotalSupply() {
37
+ await this.totalSupply.set(UInt64.from(20));
38
+ await this.admin.isAdmin(this.transaction.sender.value);
45
39
  }
46
40
 
47
41
  @runtimeMethod()
48
- public getBalance(address: PublicKey): Option<UInt64> {
49
- return this.balances.get(address);
42
+ public async getBalance(address: PublicKey) {
43
+ (await this.balances.get(address)).orElse(UInt64.zero);
50
44
  }
51
45
 
52
46
  @runtimeMethod()
53
- public transientState() {
54
- const totalSupply = this.totalSupply.get();
55
- this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(100));
47
+ public async transientState() {
48
+ const totalSupply = await this.totalSupply.get();
49
+ await this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(100));
56
50
 
57
- const totalSupply2 = this.totalSupply.get();
58
- this.totalSupply.set(totalSupply2.orElse(UInt64.zero).add(100));
51
+ const totalSupply2 = await this.totalSupply.get();
52
+ await this.totalSupply.set(totalSupply2.orElse(UInt64.zero).add(100));
59
53
  }
60
54
  }
@@ -1,17 +1,14 @@
1
1
  import "reflect-metadata";
2
- import { Bool, Field } from "snarkyjs";
3
- import { beforeAll, beforeEach } from "@jest/globals";
2
+ import { assert } from "@proto-kit/protocol";
3
+ import { Field } from "o1js";
4
+ import { beforeAll } from "@jest/globals";
5
+ import { container } from "tsyringe";
4
6
 
5
7
  import { Runtime } from "../../src/runtime/Runtime";
6
8
  import { MethodIdResolver } from "../../src/runtime/MethodIdResolver";
7
- import {
8
- assert,
9
- InMemoryStateService,
10
- runtimeMethod,
11
- RuntimeModule,
12
- runtimeModule,
13
- } from "../../src";
14
- import { container } from "tsyringe";
9
+ import { runtimeMethod, RuntimeModule, runtimeModule } from "../../src";
10
+ import { createTestingRuntime } from "../TestingRuntime";
11
+
15
12
  import { Balances } from "./Balances";
16
13
 
17
14
  interface AdminConfig {}
@@ -19,7 +16,7 @@ interface AdminConfig {}
19
16
  @runtimeModule()
20
17
  class Admin extends RuntimeModule<AdminConfig> {
21
18
  @runtimeMethod()
22
- public isAdminWithAVeryVeryVeryVeryLongName() {
19
+ public async isAdminWithAVeryVeryVeryVeryLongName() {
23
20
  assert(Field(1).equals(Field(1)));
24
21
  }
25
22
  }
@@ -31,22 +28,16 @@ describe("methodId", () => {
31
28
  beforeAll(() => {
32
29
  container.clearInstances();
33
30
 
34
- runtime = Runtime.from({
35
- modules: {
31
+ ({ runtime } = createTestingRuntime(
32
+ {
36
33
  Admin,
37
34
  Balance: Balances,
38
35
  },
39
-
40
- config: {
36
+ {
41
37
  Admin: {},
42
- Balance: {
43
- test: Bool(true),
44
- },
45
- },
46
-
47
- state: new InMemoryStateService(),
48
- });
49
- runtime.start();
38
+ Balance: {},
39
+ }
40
+ ));
50
41
 
51
42
  resolver =
52
43
  runtime.dependencyContainer.resolve<MethodIdResolver>("MethodIdResolver");
@@ -56,21 +47,19 @@ describe("methodId", () => {
56
47
  ["Admin", "isAdminWithAVeryVeryVeryVeryLongName"],
57
48
  ["Balance", "getTotalSupply"],
58
49
  ["Balance", "getBalance"],
59
- ])(
60
- "should pass and encode correctly",
61
- (givenModuleName, givenMethodName) => {
62
- expect.assertions(2);
50
+ ])("should pass and encode correctly", (givenModuleName, givenMethodName) => {
51
+ expect.assertions(2);
63
52
 
64
- const methodId = resolver.getMethodId(givenModuleName, givenMethodName);
53
+ const methodId = resolver.getMethodId(givenModuleName, givenMethodName);
65
54
 
66
- const [moduleName, methodName] = resolver.getMethodNameFromId(
67
- methodId
68
- ) ?? [undefined, undefined];
55
+ const [moduleName, methodName] = resolver.getMethodNameFromId(methodId) ?? [
56
+ undefined,
57
+ undefined,
58
+ ];
69
59
 
70
- expect(moduleName).toBe(givenModuleName);
71
- expect(methodName).toBe(givenMethodName);
72
- }
73
- );
60
+ expect(moduleName).toBe(givenModuleName);
61
+ expect(methodName).toBe(givenMethodName);
62
+ });
74
63
 
75
64
  it("should fail for invalid module name", () => {
76
65
  expect.assertions(1);
@@ -1,58 +1,41 @@
1
1
  import "reflect-metadata";
2
- import { Bool, PublicKey, UInt64 } from "snarkyjs";
2
+ import { PublicKey, UInt64 } from "o1js";
3
3
  import { container } from "tsyringe";
4
- import { NetworkState, Option, RuntimeTransaction, StateService } from "@proto-kit/protocol";
5
-
6
4
  import {
7
- InMemoryStateService,
8
- Runtime,
5
+ NetworkState,
6
+ Option,
9
7
  RuntimeMethodExecutionContext,
10
- } from "../../src";
8
+ RuntimeTransaction,
9
+ } from "@proto-kit/protocol";
10
+ import { expectDefined } from "@proto-kit/common";
11
+
12
+ import { Runtime } from "../../src";
13
+ import { createTestingRuntime } from "../TestingRuntime";
11
14
 
12
15
  import { Admin } from "./Admin";
13
16
  import { Balances } from "./Balances";
14
17
 
15
- describe("transient state", () => {
18
+ describe("state", () => {
16
19
  let balances: Balances;
17
20
 
18
- let state: StateService;
19
-
20
21
  let runtime: Runtime<{
21
22
  Admin: typeof Admin;
22
23
  Balances: typeof Balances;
23
24
  }>;
24
25
 
25
26
  function createChain() {
26
- state = new InMemoryStateService();
27
-
28
- runtime = Runtime.from({
29
- state,
30
-
31
- modules: {
27
+ ({ runtime } = createTestingRuntime(
28
+ {
32
29
  Admin,
33
30
  Balances,
34
31
  },
35
- });
36
-
37
- runtime.dependencyContainer.register("AppChain", {
38
- useValue: {
39
- areProofsEnabled: false,
40
-
41
- setProofsEnabled(areProofsEnabled: boolean) {
42
- this.areProofsEnabled = areProofsEnabled;
32
+ {
33
+ Admin: {
34
+ publicKey: PublicKey.empty<typeof PublicKey>().toBase58(),
43
35
  },
44
- },
45
- });
46
-
47
- runtime.configure({
48
- Admin: {
49
- publicKey: PublicKey.empty().toBase58(),
50
- },
51
-
52
- Balances: {
53
- test: Bool(true),
54
- },
55
- });
36
+ Balances: {},
37
+ }
38
+ ));
56
39
 
57
40
  balances = runtime.resolve("Balances");
58
41
  }
@@ -61,28 +44,38 @@ describe("transient state", () => {
61
44
  createChain();
62
45
  });
63
46
 
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,
47
+ describe("state decorator", () => {
48
+ it("should decorate state properties correctly", () => {
49
+ expectDefined(balances.totalSupply.path);
69
50
  });
70
- balances.transientState();
51
+ });
52
+
53
+ describe("transient state", () => {
54
+ it("should track previously set state", async () => {
55
+ expect.assertions(2);
71
56
 
72
- const stateTransitions = executionContext
73
- .current()
74
- .result.stateTransitions.map((stateTransition) =>
75
- stateTransition.toProvable()
76
- );
57
+ const executionContext = container.resolve(RuntimeMethodExecutionContext);
58
+ executionContext.setup({
59
+ networkState: NetworkState.empty(),
60
+ transaction: RuntimeTransaction.dummyTransaction(),
61
+ });
62
+ await balances.transientState();
77
63
 
78
- const expectedLastOption = Option.fromValue(
79
- UInt64.from(200),
80
- UInt64
81
- ).toProvable();
64
+ const stateTransitions = executionContext
65
+ .current()
66
+ .result.stateTransitions.map((stateTransition) =>
67
+ stateTransition.toProvable()
68
+ );
82
69
 
83
- const last = stateTransitions.at(-1);
70
+ const expectedLastOption = Option.fromValue(
71
+ UInt64.from(200),
72
+ UInt64
73
+ ).toProvable();
84
74
 
85
- expect(last).toBeDefined();
86
- expect(last!.to.value).toStrictEqual(expectedLastOption.value);
75
+ const last = stateTransitions.at(-1);
76
+
77
+ expect(last).toBeDefined();
78
+ expect(last!.to.value).toStrictEqual(expectedLastOption.value);
79
+ });
87
80
  });
88
81
  });