Skip to content

Commit 80d54ff

Browse files
committed
Implemented unified state decorators
1 parent 0127730 commit 80d54ff

File tree

17 files changed

+167
-139
lines changed

17 files changed

+167
-139
lines changed

packages/common/src/utils.ts

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

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

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/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+
});

packages/protocol/src/hooks/AccountStateHook.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { injectable } from "tsyringe";
33
import { noop } from "@proto-kit/common";
44

55
import { StateMap } from "../state/StateMap";
6-
import { protocolState } from "../state/protocol/ProtocolState";
6+
import { state } from "../state/protocol/ProtocolState";
77
import {
88
ProvableTransactionHook,
99
BeforeTransactionHookArguments,
@@ -16,7 +16,7 @@ export class AccountState extends Struct({
1616

1717
@injectable()
1818
export class AccountStateHook extends ProvableTransactionHook {
19-
@protocolState() public accountState = StateMap.from<PublicKey, AccountState>(
19+
@state() public accountState = StateMap.from<PublicKey, AccountState>(
2020
PublicKey,
2121
AccountState
2222
);

packages/protocol/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ export * from "./settlement/modules/NetworkStateSettlementModule";
6060
export * from "./settlement/messages/Deposit";
6161
export * from "./settlement/messages/Withdrawal";
6262
export { constants as ProtocolConstants } from "./Constants";
63+
export * from "./hashing/protokit-prefixes";
64+
export * from "./hashing/mina-prefixes";

packages/protocol/src/model/Path.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Field, type FlexibleProvablePure, Poseidon } from "o1js";
2+
import { hashWithPrefix } from "@proto-kit/common";
23

34
import { stringToField } from "../utils/utils";
45

@@ -21,10 +22,15 @@ export class Path {
2122
*
2223
* @param className
2324
* @param propertyKey
25+
* @param prefix
2426
* @returns Field representation of class name + property name
2527
*/
26-
public static fromProperty(className: string, propertyKey: string): Field {
27-
return Poseidon.hash([
28+
public static fromProperty(
29+
className: string,
30+
propertyKey: string,
31+
prefix: string
32+
): Field {
33+
return hashWithPrefix(prefix, [
2834
Path.toField(className),
2935
Path.toField(propertyKey),
3036
Field(0),

0 commit comments

Comments
 (0)