Skip to content

Commit b677d53

Browse files
authored
Merge pull request #278 from proto-kit/feature/network-utils
Added NetworkUtils and removed BlockchainAccount utility
2 parents 4e882f8 + 966dbb4 commit b677d53

File tree

29 files changed

+468
-239
lines changed

29 files changed

+468
-239
lines changed

packages/module/src/runtime/Runtime.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,7 @@ export class Runtime<Modules extends RuntimeModulesRecord>
311311
}
312312

313313
public get stateServiceProvider(): StateServiceProvider {
314-
return this.dependencyContainer.resolve<StateServiceProvider>(
315-
"StateServiceProvider"
316-
);
314+
return this.container.resolve<StateServiceProvider>("StateServiceProvider");
317315
}
318316

319317
public get stateService(): SimpleAsyncStateService {

packages/module/test/modules/Balances.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
RuntimeMethodExecutionContext,
1010
RuntimeTransaction,
1111
NetworkState,
12+
PROTOKIT_PREFIXES,
1213
} from "@proto-kit/protocol";
1314

1415
import { Runtime } from "../../src";
@@ -132,7 +133,11 @@ describe("balances", () => {
132133
it("should have a state transition for the correct path", () => {
133134
expect.assertions(1);
134135

135-
const path = Path.fromProperty("Balances", "totalSupply");
136+
const path = Path.fromProperty(
137+
"Balances",
138+
"totalSupply",
139+
PROTOKIT_PREFIXES.STATE_RUNTIME
140+
);
136141

137142
expect(stateTransitions[0].path.toString()).toStrictEqual(
138143
path.toString()
@@ -192,7 +197,11 @@ describe("balances", () => {
192197
it("should have a state transition for the correct path", () => {
193198
expect.assertions(1);
194199

195-
const path = Path.fromProperty("Balances", "totalSupply");
200+
const path = Path.fromProperty(
201+
"Balances",
202+
"totalSupply",
203+
PROTOKIT_PREFIXES.STATE_RUNTIME
204+
);
196205

197206
expect(stateTransitions[0].path.toString()).toStrictEqual(
198207
path.toString()
@@ -247,7 +256,11 @@ describe("balances", () => {
247256
it("should have a state transition for the correct path", () => {
248257
expect.assertions(1);
249258

250-
const path = Path.fromProperty("Balances", "totalSupply");
259+
const path = Path.fromProperty(
260+
"Balances",
261+
"totalSupply",
262+
PROTOKIT_PREFIXES.STATE_RUNTIME
263+
);
251264

252265
expect(stateTransitions[0].path.toString()).toStrictEqual(
253266
path.toString()
@@ -313,7 +326,11 @@ describe("balances", () => {
313326
expect.assertions(1);
314327

315328
const path = Path.fromKey<PublicKey>(
316-
Path.fromProperty("Balances", "balances"),
329+
Path.fromProperty(
330+
"Balances",
331+
"balances",
332+
PROTOKIT_PREFIXES.STATE_RUNTIME
333+
),
317334
PublicKey,
318335
address
319336
);

packages/module/test/modules/Balances.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { PublicKey, UInt64 } from "o1js";
2-
import { State, StateMap } from "@proto-kit/protocol";
2+
import { State, StateMap, state } from "@proto-kit/protocol";
33
import { Presets } from "@proto-kit/common";
44

5-
import { RuntimeModule, runtimeMethod, runtimeModule, state } from "../../src";
5+
import { RuntimeModule, runtimeMethod, runtimeModule } from "../../src";
66

77
import { Admin } from "./Admin.js";
88

packages/protocol/src/state/protocol/ProtocolState.ts

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,6 @@ export interface StatefulModule {
2727
};
2828
}
2929

30-
export function createStateGetter<TargetModule extends StatefulModule>(
31-
target: TargetModule,
32-
propertyKey: string,
33-
valueReference: Reference<State<unknown> | undefined>,
34-
prefix: string,
35-
debugInfo: { parentName: string; baseModuleNames: string }
36-
) {
37-
return () => {
38-
const { value } = valueReference;
39-
// Short-circuit this to return the state in case its already initialized
40-
if (value !== undefined && value.path !== undefined) {
41-
return value;
42-
}
43-
44-
if (target.name === undefined) {
45-
throw errors.missingName(target.constructor.name);
46-
}
47-
48-
if (!target.parent) {
49-
throw errors.missingParent(
50-
target.constructor.name,
51-
debugInfo.parentName,
52-
debugInfo.baseModuleNames
53-
);
54-
}
55-
56-
const path = Path.fromProperty(target.name, propertyKey, prefix);
57-
if (value) {
58-
value.path = path;
59-
value.stateServiceProvider = target.parent.stateServiceProvider;
60-
}
61-
return value;
62-
};
63-
}
64-
6530
/**
6631
* Decorates a runtime module property as state, passing down some
6732
* underlying values to improve developer experience.
@@ -71,31 +36,62 @@ export function state() {
7136
target: TargetTransitioningModule,
7237
propertyKey: string
7338
) => {
74-
const stateReference = createReference<State<unknown> | undefined>(
75-
undefined
76-
);
77-
78-
const isProtocol = target instanceof TransitioningProtocolModule;
79-
const statePrefix = isProtocol
80-
? PROTOKIT_PREFIXES.STATE_PROTOCOL
81-
: PROTOKIT_PREFIXES.STATE_RUNTIME;
82-
const debugInfo = isProtocol
83-
? { parentName: "protocol", baseModuleNames: "...Hook" }
84-
: { parentName: "runtime", baseModuleNames: "RuntimeModule" };
85-
8639
Object.defineProperty(target, propertyKey, {
8740
enumerable: true,
8841

89-
get: createStateGetter(
90-
target,
91-
propertyKey,
92-
stateReference,
93-
statePrefix,
94-
debugInfo
95-
),
42+
get: function get(this: TargetTransitioningModule) {
43+
// The reason for why we store the state value in this weird way is that
44+
// in the decorator on the prototype of the class. This means that if there
45+
// are multiple instances of this class, any closure that this getter shares
46+
// will be the same for all instances.
47+
// Therefore, we need to somehow save the set instance on the instance itself
48+
49+
// eslint-disable-next-line max-len
50+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions,@typescript-eslint/no-unsafe-assignment
51+
const reference: Reference<State<unknown>> | undefined = (this as any)[
52+
`protokit_state_cache_${propertyKey}`
53+
];
54+
55+
// Short-circuit this to return the state in case its already initialized
56+
if (reference !== undefined && reference.value.path !== undefined) {
57+
return reference.value;
58+
}
59+
60+
if (this.name === undefined) {
61+
throw errors.missingName(this.constructor.name);
62+
}
63+
64+
const isProtocol = target instanceof TransitioningProtocolModule;
65+
66+
if (!this.parent) {
67+
const debugInfo = isProtocol
68+
? { parentName: "protocol", baseModuleNames: "...Hook" }
69+
: { parentName: "runtime", baseModuleNames: "RuntimeModule" };
70+
71+
throw errors.missingParent(
72+
this.constructor.name,
73+
debugInfo.parentName,
74+
debugInfo.baseModuleNames
75+
);
76+
}
77+
78+
const statePrefix = isProtocol
79+
? PROTOKIT_PREFIXES.STATE_PROTOCOL
80+
: PROTOKIT_PREFIXES.STATE_RUNTIME;
81+
const path = Path.fromProperty(this.name, propertyKey, statePrefix);
82+
if (reference) {
83+
const { value } = reference;
84+
value.path = path;
85+
value.stateServiceProvider = this.parent.stateServiceProvider;
86+
}
87+
return reference?.value;
88+
},
9689

97-
set: (newValue: State<unknown>) => {
98-
stateReference.value = newValue;
90+
set: function set(
91+
this: TargetTransitioningModule & any,
92+
newValue: State<unknown>
93+
) {
94+
this[`protokit_state_cache_${propertyKey}`] = createReference(newValue);
9995
},
10096
});
10197
};

packages/sdk/test/TestingAppChain.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ import "reflect-metadata";
22
import { randomUUID } from "crypto";
33

44
import { inject } from "tsyringe";
5-
import {
6-
runtimeMethod,
7-
RuntimeModule,
8-
runtimeModule,
9-
state,
10-
} from "@proto-kit/module";
5+
import { runtimeMethod, RuntimeModule, runtimeModule } from "@proto-kit/module";
116
import { PrivateKey, Provable, PublicKey } from "o1js";
12-
import { assert, State } from "@proto-kit/protocol";
7+
import { assert, State, state } from "@proto-kit/protocol";
138
import { Balances, BalancesKey, TokenId, UInt64 } from "@proto-kit/library";
149
import { log } from "@proto-kit/common";
1510

packages/sdk/test/XYK/XYK.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import "reflect-metadata";
2-
import {
3-
RuntimeModule,
4-
runtimeMethod,
5-
state,
6-
runtimeModule,
7-
} from "@proto-kit/module";
8-
import { StateMap, assert } from "@proto-kit/protocol";
2+
import { RuntimeModule, runtimeMethod, runtimeModule } from "@proto-kit/module";
3+
import { StateMap, assert, state } from "@proto-kit/protocol";
94
import { Field, Poseidon, PublicKey, Provable, Struct } from "o1js";
105
import { inject } from "tsyringe";
116
import { Balance, Balances, TokenId } from "@proto-kit/library";

packages/sdk/test/blockProof/TestBalances.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { runtimeModule, state, runtimeMethod } from "@proto-kit/module";
1+
import { runtimeModule, runtimeMethod } from "@proto-kit/module";
22
import { PublicKey } from "o1js";
3-
import { State } from "@proto-kit/protocol";
3+
import { State, state } from "@proto-kit/protocol";
44
import { Balance, Balances, TokenId, UInt64 } from "@proto-kit/library";
55

66
interface BalancesConfig {

packages/sdk/test/fee-hook-sts-regression.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ import "reflect-metadata";
22
import { randomUUID } from "crypto";
33

44
import { inject } from "tsyringe";
5-
import {
6-
runtimeMethod,
7-
RuntimeModule,
8-
runtimeModule,
9-
state,
10-
} from "@proto-kit/module";
5+
import { runtimeMethod, RuntimeModule, runtimeModule } from "@proto-kit/module";
116
import { PrivateKey, Provable, PublicKey } from "o1js";
12-
import { assert, State } from "@proto-kit/protocol";
7+
import { assert, State, state } from "@proto-kit/protocol";
138
import { Balances, BalancesKey, TokenId, UInt64 } from "@proto-kit/library";
149
import { log, expectDefined, sleep } from "@proto-kit/common";
1510

packages/sdk/test/networkstate/Balance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
TokenId,
66
UInt64,
77
} from "@proto-kit/library";
8-
import { runtimeMethod, runtimeModule, state } from "@proto-kit/module";
8+
import { runtimeMethod, runtimeModule } from "@proto-kit/module";
99
import { log, Presets, range, mapSequential } from "@proto-kit/common";
1010
import { Bool, Field, PublicKey } from "o1js";
1111
import { Admin } from "@proto-kit/module/test/modules/Admin";
12-
import { State, assert } from "@proto-kit/protocol";
12+
import { State, assert, state } from "@proto-kit/protocol";
1313

1414
@runtimeModule()
1515
export class BalanceChild extends Balances {

packages/sdk/test/parameters.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ import {
1111
PublicKey,
1212
ZkProgram,
1313
} from "o1js";
14-
import {
15-
runtimeMethod,
16-
RuntimeModule,
17-
runtimeModule,
18-
state,
19-
} from "@proto-kit/module";
20-
import { assert, State, StateMap } from "@proto-kit/protocol";
14+
import { runtimeMethod, RuntimeModule, runtimeModule } from "@proto-kit/module";
15+
import { assert, State, StateMap, state } from "@proto-kit/protocol";
2116
import { expectDefined } from "@proto-kit/common";
2217

2318
import { TestingAppChain } from "../src/index";

0 commit comments

Comments
 (0)