Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/module/src/runtime/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,7 @@ export class Runtime<Modules extends RuntimeModulesRecord>
}

public get stateServiceProvider(): StateServiceProvider {
return this.dependencyContainer.resolve<StateServiceProvider>(
"StateServiceProvider"
);
return this.container.resolve<StateServiceProvider>("StateServiceProvider");
}

public get stateService(): SimpleAsyncStateService {
Expand Down
25 changes: 21 additions & 4 deletions packages/module/test/modules/Balances.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RuntimeMethodExecutionContext,
RuntimeTransaction,
NetworkState,
PROTOKIT_PREFIXES,
} from "@proto-kit/protocol";

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

const path = Path.fromProperty("Balances", "totalSupply");
const path = Path.fromProperty(
"Balances",
"totalSupply",
PROTOKIT_PREFIXES.STATE_RUNTIME
);

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

const path = Path.fromProperty("Balances", "totalSupply");
const path = Path.fromProperty(
"Balances",
"totalSupply",
PROTOKIT_PREFIXES.STATE_RUNTIME
);

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

const path = Path.fromProperty("Balances", "totalSupply");
const path = Path.fromProperty(
"Balances",
"totalSupply",
PROTOKIT_PREFIXES.STATE_RUNTIME
);

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

const path = Path.fromKey<PublicKey>(
Path.fromProperty("Balances", "balances"),
Path.fromProperty(
"Balances",
"balances",
PROTOKIT_PREFIXES.STATE_RUNTIME
),
PublicKey,
address
);
Expand Down
4 changes: 2 additions & 2 deletions packages/module/test/modules/Balances.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PublicKey, UInt64 } from "o1js";
import { State, StateMap } from "@proto-kit/protocol";
import { State, StateMap, state } from "@proto-kit/protocol";
import { Presets } from "@proto-kit/common";

import { RuntimeModule, runtimeMethod, runtimeModule, state } from "../../src";
import { RuntimeModule, runtimeMethod, runtimeModule } from "../../src";

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

Expand Down
108 changes: 52 additions & 56 deletions packages/protocol/src/state/protocol/ProtocolState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,6 @@ export interface StatefulModule {
};
}

export function createStateGetter<TargetModule extends StatefulModule>(
target: TargetModule,
propertyKey: string,
valueReference: Reference<State<unknown> | undefined>,
prefix: string,
debugInfo: { parentName: string; baseModuleNames: string }
) {
return () => {
const { value } = valueReference;
// Short-circuit this to return the state in case its already initialized
if (value !== undefined && value.path !== undefined) {
return value;
}

if (target.name === undefined) {
throw errors.missingName(target.constructor.name);
}

if (!target.parent) {
throw errors.missingParent(
target.constructor.name,
debugInfo.parentName,
debugInfo.baseModuleNames
);
}

const path = Path.fromProperty(target.name, propertyKey, prefix);
if (value) {
value.path = path;
value.stateServiceProvider = target.parent.stateServiceProvider;
}
return value;
};
}

/**
* Decorates a runtime module property as state, passing down some
* underlying values to improve developer experience.
Expand All @@ -71,31 +36,62 @@ export function state() {
target: TargetTransitioningModule,
propertyKey: string
) => {
const stateReference = createReference<State<unknown> | undefined>(
undefined
);

const isProtocol = target instanceof TransitioningProtocolModule;
const statePrefix = isProtocol
? PROTOKIT_PREFIXES.STATE_PROTOCOL
: PROTOKIT_PREFIXES.STATE_RUNTIME;
const debugInfo = isProtocol
? { parentName: "protocol", baseModuleNames: "...Hook" }
: { parentName: "runtime", baseModuleNames: "RuntimeModule" };

Object.defineProperty(target, propertyKey, {
enumerable: true,

get: createStateGetter(
target,
propertyKey,
stateReference,
statePrefix,
debugInfo
),
get: function get(this: TargetTransitioningModule) {
// The reason for why we store the state value in this weird way is that
// in the decorator on the prototype of the class. This means that if there
// are multiple instances of this class, any closure that this getter shares
// will be the same for all instances.
// Therefore, we need to somehow save the set instance on the instance itself

// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions,@typescript-eslint/no-unsafe-assignment
const reference: Reference<State<unknown>> | undefined = (this as any)[
`protokit_state_cache_${propertyKey}`
];

// Short-circuit this to return the state in case its already initialized
if (reference !== undefined && reference.value.path !== undefined) {
return reference.value;
}

if (this.name === undefined) {
throw errors.missingName(this.constructor.name);
}

const isProtocol = target instanceof TransitioningProtocolModule;

if (!this.parent) {
const debugInfo = isProtocol
? { parentName: "protocol", baseModuleNames: "...Hook" }
: { parentName: "runtime", baseModuleNames: "RuntimeModule" };

throw errors.missingParent(
this.constructor.name,
debugInfo.parentName,
debugInfo.baseModuleNames
);
}

const statePrefix = isProtocol
? PROTOKIT_PREFIXES.STATE_PROTOCOL
: PROTOKIT_PREFIXES.STATE_RUNTIME;
const path = Path.fromProperty(this.name, propertyKey, statePrefix);
if (reference) {
const { value } = reference;
value.path = path;
value.stateServiceProvider = this.parent.stateServiceProvider;
}
return reference?.value;
},

set: (newValue: State<unknown>) => {
stateReference.value = newValue;
set: function set(
this: TargetTransitioningModule & any,
newValue: State<unknown>
) {
this[`protokit_state_cache_${propertyKey}`] = createReference(newValue);
},
});
};
Expand Down
9 changes: 2 additions & 7 deletions packages/sdk/test/TestingAppChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import "reflect-metadata";
import { randomUUID } from "crypto";

import { inject } from "tsyringe";
import {
runtimeMethod,
RuntimeModule,
runtimeModule,
state,
} from "@proto-kit/module";
import { runtimeMethod, RuntimeModule, runtimeModule } from "@proto-kit/module";
import { PrivateKey, Provable, PublicKey } from "o1js";
import { assert, State } from "@proto-kit/protocol";
import { assert, State, state } from "@proto-kit/protocol";
import { Balances, BalancesKey, TokenId, UInt64 } from "@proto-kit/library";
import { log } from "@proto-kit/common";

Expand Down
9 changes: 2 additions & 7 deletions packages/sdk/test/XYK/XYK.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import "reflect-metadata";
import {
RuntimeModule,
runtimeMethod,
state,
runtimeModule,
} from "@proto-kit/module";
import { StateMap, assert } from "@proto-kit/protocol";
import { RuntimeModule, runtimeMethod, runtimeModule } from "@proto-kit/module";
import { StateMap, assert, state } from "@proto-kit/protocol";
import { Field, Poseidon, PublicKey, Provable, Struct } from "o1js";
import { inject } from "tsyringe";
import { Balance, Balances, TokenId } from "@proto-kit/library";
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/test/blockProof/TestBalances.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { runtimeModule, state, runtimeMethod } from "@proto-kit/module";
import { runtimeModule, runtimeMethod } from "@proto-kit/module";
import { PublicKey } from "o1js";
import { State } from "@proto-kit/protocol";
import { State, state } from "@proto-kit/protocol";
import { Balance, Balances, TokenId, UInt64 } from "@proto-kit/library";

interface BalancesConfig {
Expand Down
9 changes: 2 additions & 7 deletions packages/sdk/test/fee-hook-sts-regression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ import "reflect-metadata";
import { randomUUID } from "crypto";

import { inject } from "tsyringe";
import {
runtimeMethod,
RuntimeModule,
runtimeModule,
state,
} from "@proto-kit/module";
import { runtimeMethod, RuntimeModule, runtimeModule } from "@proto-kit/module";
import { PrivateKey, Provable, PublicKey } from "o1js";
import { assert, State } from "@proto-kit/protocol";
import { assert, State, state } from "@proto-kit/protocol";
import { Balances, BalancesKey, TokenId, UInt64 } from "@proto-kit/library";
import { log, expectDefined, sleep } from "@proto-kit/common";

Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/test/networkstate/Balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
TokenId,
UInt64,
} from "@proto-kit/library";
import { runtimeMethod, runtimeModule, state } from "@proto-kit/module";
import { runtimeMethod, runtimeModule } from "@proto-kit/module";
import { log, Presets, range, mapSequential } from "@proto-kit/common";
import { Bool, Field, PublicKey } from "o1js";
import { Admin } from "@proto-kit/module/test/modules/Admin";
import { State, assert } from "@proto-kit/protocol";
import { State, assert, state } from "@proto-kit/protocol";

@runtimeModule()
export class BalanceChild extends Balances {
Expand Down
9 changes: 2 additions & 7 deletions packages/sdk/test/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ import {
PublicKey,
ZkProgram,
} from "o1js";
import {
runtimeMethod,
RuntimeModule,
runtimeModule,
state,
} from "@proto-kit/module";
import { assert, State, StateMap } from "@proto-kit/protocol";
import { runtimeMethod, RuntimeModule, runtimeModule } from "@proto-kit/module";
import { assert, State, StateMap, state } from "@proto-kit/protocol";
import { expectDefined } from "@proto-kit/common";

import { TestingAppChain } from "../src/index";
Expand Down
4 changes: 4 additions & 0 deletions packages/sequencer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export * from "./worker/worker/WorkerReadyModule";
export * from "./protocol/baselayer/BaseLayer";
export * from "./protocol/baselayer/MinaBaseLayer";
export * from "./protocol/baselayer/NoopBaseLayer";
export * from "./protocol/baselayer/network-utils/MinaNetworkUtils";
export * from "./protocol/baselayer/network-utils/RemoteNetworkUtils";
export * from "./protocol/baselayer/network-utils/LightnetUtils";
export * from "./protocol/baselayer/network-utils/LocalBlockchainUtils";
export * from "./protocol/production/helpers/UntypedOption";
export * from "./protocol/production/helpers/UntypedStateTransition";
export * from "./protocol/production/tasks/TransactionProvingTask";
Expand Down
Loading