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