@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.
- 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 +67 -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 +75 -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 +9 -9
- package/src/factories/MethodIdFactory.ts +10 -17
- package/src/index.ts +4 -4
- package/src/messages/OutgoingMessages.ts +124 -0
- package/src/method/MethodParameterEncoder.ts +262 -0
- package/src/method/runtimeMethod.ts +131 -33
- package/src/runtime/MethodIdResolver.ts +85 -46
- package/src/runtime/Runtime.ts +187 -96
- package/src/runtime/RuntimeEnvironment.ts +16 -0
- package/src/runtime/RuntimeModule.ts +58 -27
- package/src/state/InMemoryStateService.ts +14 -14
- 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 +88 -79
- package/test/modules/Balances.ts +15 -21
- package/test/modules/{methodId.test.ts → MethodIdResolver.test.ts} +24 -35
- package/test/modules/State.test.ts +46 -53
- 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/module/src/factories/MethodIdFactory.d.ts +0 -9
- package/dist/module/src/factories/MethodIdFactory.d.ts.map +0 -1
- package/dist/module/src/factories/MethodIdFactory.js +0 -36
- package/dist/module/src/index.d.ts +0 -11
- package/dist/module/src/index.d.ts.map +0 -1
- package/dist/module/src/index.js +0 -10
- package/dist/module/src/method/MethodParameterDecoder.d.ts +0 -22
- package/dist/module/src/method/MethodParameterDecoder.d.ts.map +0 -1
- package/dist/module/src/method/MethodParameterDecoder.js +0 -33
- package/dist/module/src/method/runtimeMethod.d.ts +0 -19
- package/dist/module/src/method/runtimeMethod.d.ts.map +0 -1
- package/dist/module/src/method/runtimeMethod.js +0 -123
- package/dist/module/src/module/decorator.d.ts +0 -8
- package/dist/module/src/module/decorator.d.ts.map +0 -1
- package/dist/module/src/module/decorator.js +0 -15
- package/dist/module/src/runtime/MethodIdResolver.d.ts +0 -18
- package/dist/module/src/runtime/MethodIdResolver.d.ts.map +0 -1
- package/dist/module/src/runtime/MethodIdResolver.js +0 -50
- package/dist/module/src/runtime/Runtime.d.ts +0 -72
- package/dist/module/src/runtime/Runtime.d.ts.map +0 -1
- package/dist/module/src/runtime/Runtime.js +0 -184
- package/dist/module/src/runtime/RuntimeModule.d.ts +0 -35
- package/dist/module/src/runtime/RuntimeModule.d.ts.map +0 -1
- package/dist/module/src/runtime/RuntimeModule.js +0 -56
- package/dist/module/src/state/InMemoryStateService.d.ts +0 -11
- package/dist/module/src/state/InMemoryStateService.d.ts.map +0 -1
- package/dist/module/src/state/InMemoryStateService.js +0 -21
- package/dist/module/src/state/decorator.d.ts +0 -7
- package/dist/module/src/state/decorator.d.ts.map +0 -1
- package/dist/module/src/state/decorator.js +0 -42
- package/dist/protocol/src/model/Option.d.ts +0 -98
- package/dist/protocol/src/model/Option.d.ts.map +0 -1
- package/dist/protocol/src/model/Option.js +0 -98
- package/dist/protocol/src/model/StateTransition.d.ts +0 -96
- package/dist/protocol/src/model/StateTransition.d.ts.map +0 -1
- package/dist/protocol/src/model/StateTransition.js +0 -65
- package/dist/protocol/src/model/network/NetworkState.d.ts +0 -64
- package/dist/protocol/src/model/network/NetworkState.d.ts.map +0 -1
- package/dist/protocol/src/model/network/NetworkState.js +0 -12
- package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts +0 -70
- package/dist/protocol/src/model/transaction/ProtocolTransaction.d.ts.map +0 -1
- package/dist/protocol/src/model/transaction/ProtocolTransaction.js +0 -18
- package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts +0 -63
- package/dist/protocol/src/model/transaction/RuntimeTransaction.d.ts.map +0 -1
- package/dist/protocol/src/model/transaction/RuntimeTransaction.js +0 -29
- package/dist/protocol/src/state/assert/assert.d.ts +0 -12
- package/dist/protocol/src/state/assert/assert.d.ts.map +0 -1
- package/dist/protocol/src/state/assert/assert.js +0 -23
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts +0 -60
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.d.ts.map +0 -1
- package/dist/protocol/src/state/context/RuntimeMethodExecutionContext.js +0 -105
- 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 -70
- package/src/state/decorator.ts +0 -63
- 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,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
|
-
|
|
8
|
+
SimpleAsyncStateService,
|
|
9
|
+
RuntimeMethodExecutionContext,
|
|
10
|
+
RuntimeTransaction,
|
|
11
|
+
NetworkState,
|
|
12
|
+
PROTOKIT_PREFIXES,
|
|
18
13
|
} from "@proto-kit/protocol";
|
|
19
14
|
|
|
20
|
-
import {
|
|
21
|
-
|
|
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:
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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(
|
|
329
|
+
Path.fromProperty(
|
|
330
|
+
"Balances",
|
|
331
|
+
"balances",
|
|
332
|
+
PROTOKIT_PREFIXES.STATE_RUNTIME
|
|
333
|
+
),
|
|
325
334
|
PublicKey,
|
|
326
335
|
address
|
|
327
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 "../../../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
|
-
|
|
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 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 {
|
|
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,21 +47,19 @@ describe("methodId", () => {
|
|
|
56
47
|
["Admin", "isAdminWithAVeryVeryVeryVeryLongName"],
|
|
57
48
|
["Balance", "getTotalSupply"],
|
|
58
49
|
["Balance", "getBalance"],
|
|
59
|
-
])(
|
|
60
|
-
|
|
61
|
-
(givenModuleName, givenMethodName) => {
|
|
62
|
-
expect.assertions(2);
|
|
50
|
+
])("should pass and encode correctly", (givenModuleName, givenMethodName) => {
|
|
51
|
+
expect.assertions(2);
|
|
63
52
|
|
|
64
|
-
|
|
53
|
+
const methodId = resolver.getMethodId(givenModuleName, givenMethodName);
|
|
65
54
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
55
|
+
const [moduleName, methodName] = resolver.getMethodNameFromId(methodId) ?? [
|
|
56
|
+
undefined,
|
|
57
|
+
undefined,
|
|
58
|
+
];
|
|
69
59
|
|
|
70
|
-
|
|
71
|
-
|
|
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 {
|
|
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
|
-
|
|
8
|
-
|
|
5
|
+
NetworkState,
|
|
6
|
+
Option,
|
|
9
7
|
RuntimeMethodExecutionContext,
|
|
10
|
-
|
|
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("
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("transient state", () => {
|
|
54
|
+
it("should track previously set state", async () => {
|
|
55
|
+
expect.assertions(2);
|
|
71
56
|
|
|
72
|
-
|
|
73
|
-
.
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
64
|
+
const stateTransitions = executionContext
|
|
65
|
+
.current()
|
|
66
|
+
.result.stateTransitions.map((stateTransition) =>
|
|
67
|
+
stateTransition.toProvable()
|
|
68
|
+
);
|
|
82
69
|
|
|
83
|
-
|
|
70
|
+
const expectedLastOption = Option.fromValue(
|
|
71
|
+
UInt64.from(200),
|
|
72
|
+
UInt64
|
|
73
|
+
).toProvable();
|
|
84
74
|
|
|
85
|
-
|
|
86
|
-
|
|
75
|
+
const last = stateTransitions.at(-1);
|
|
76
|
+
|
|
77
|
+
expect(last).toBeDefined();
|
|
78
|
+
expect(last!.to.value).toStrictEqual(expectedLastOption.value);
|
|
79
|
+
});
|
|
87
80
|
});
|
|
88
81
|
});
|