Skip to content

Commit 7da2dd2

Browse files
authored
Merge pull request #264 from proto-kit/refactor/state-homogenization
State Decorator homogenization
2 parents 7e81848 + 80d54ff commit 7da2dd2

File tree

22 files changed

+176
-146
lines changed

22 files changed

+176
-146
lines changed

packages/common/src/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,24 @@ export function batch<T>(
259259

260260
return range(0, numBatches).map((i) => partitioned[i].map((x) => x[0]));
261261
}
262+
263+
export type Reference<T> = {
264+
set value(value: T);
265+
get value(): T;
266+
};
267+
268+
class ReferenceObject<T> {
269+
public constructor(private internalValue: T) {}
270+
271+
get value() {
272+
return this.internalValue;
273+
}
274+
275+
set value(t: T) {
276+
this.internalValue = t;
277+
}
278+
}
279+
280+
export function createReference<T>(initial: T): Reference<T> {
281+
return new ReferenceObject(initial);
282+
}

packages/library/src/runtime/Balances.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { EventsRecord, NoConfig } from "@proto-kit/common";
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, PublicKey, Struct, Provable } from "o1js";
105

116
import { UInt64 } from "../math/UInt64";

packages/library/src/runtime/Withdrawals.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
RuntimeEvents,
3-
runtimeModule,
4-
RuntimeModule,
5-
state,
6-
} from "@proto-kit/module";
7-
import { StateMap, Withdrawal } from "@proto-kit/protocol";
1+
import { RuntimeEvents, runtimeModule, RuntimeModule } from "@proto-kit/module";
2+
import { StateMap, Withdrawal, state } from "@proto-kit/protocol";
83
import { Field, PublicKey, Struct } from "o1js";
94
import { inject } from "tsyringe";
105

packages/module/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export * from "./runtime/RuntimeModule";
44
export * from "./runtime/RuntimeEnvironment";
55
export * from "./runtime/Runtime";
66
export * from "./state/InMemoryStateService";
7-
export * from "./state/decorator";
87
export * from "./method/MethodParameterEncoder";
98
export * from "./runtime/MethodIdResolver";
109
export * from "./factories/MethodIdFactory";

packages/module/src/method/runtimeMethod.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function toWrappedMethod(
9898
const stateTransitionsHash = toStateTransitionsHash(stateTransitions);
9999
const eventsHash = toEventsHash(events);
100100

101-
const { name, runtime } = this;
101+
const { name, parent: runtime } = this;
102102

103103
if (name === undefined) {
104104
throw errors.runtimeNameNotSet();
@@ -276,10 +276,10 @@ function runtimeMethodInternal(options: {
276276
executionContext.beforeMethod(constructorName, methodName, args);
277277

278278
if (executionContext.isTopLevel) {
279-
if (!this.runtime) {
279+
if (!this.parent) {
280280
throw errors.runtimeNotProvided(constructorName);
281281
}
282-
executionContext.setProver(prover.bind(this.runtime.zkProgrammable));
282+
executionContext.setProver(prover.bind(this.parent.zkProgrammable));
283283
}
284284

285285
let result: unknown;

packages/module/src/runtime/Runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export class Runtime<Modules extends RuntimeModulesRecord>
371371
containedModule: InstanceType<Modules[StringKeyOf<Modules>]>
372372
) {
373373
containedModule.name = moduleName;
374-
containedModule.runtime = this;
374+
containedModule.parent = this;
375375

376376
super.decorateModule(moduleName, containedModule);
377377
}

packages/module/src/runtime/RuntimeModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class RuntimeModule<
7575

7676
public name?: string;
7777

78-
public runtime?: RuntimeEnvironment;
78+
public parent?: RuntimeEnvironment;
7979

8080
public events?: RuntimeEvents<any> = undefined;
8181

packages/module/src/state/decorator.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const MINA_PREFIXES = {
2+
event: "MinaZkappEvent******",
3+
events: "MinaZkappEvents*****",
4+
sequenceEvents: "MinaZkappSeqEvents**",
5+
} as const;
6+
7+
export const MINA_SALTS = {
8+
empty_actions: "MinaZkappActionsEmpty",
9+
empty_events: "MinaZkappEventsEmpty",
10+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import padEnd from "lodash/padEnd";
2+
import mapValues from "lodash/mapValues";
3+
4+
const length = 20;
5+
function padToHashPrefix(s: string): string {
6+
if (s.length > 20) {
7+
throw new Error(`Prefix string ${s} is too long (max ${length})`);
8+
}
9+
return padEnd(s, length, "*");
10+
}
11+
12+
function padPrefixRecord<T extends Record<string, string>>(
13+
record: T
14+
): {
15+
[Key in keyof T]: string;
16+
} {
17+
return mapValues(record, padToHashPrefix);
18+
}
19+
20+
export const PROTOKIT_PREFIXES = padPrefixRecord({
21+
STATE_PROTOCOL: "pk-protocol-state",
22+
STATE_RUNTIME: "pk-runtime-state",
23+
});

0 commit comments

Comments
 (0)