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
21 changes: 21 additions & 0 deletions packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,24 @@ export function batch<T>(

return range(0, numBatches).map((i) => partitioned[i].map((x) => x[0]));
}

export type Reference<T> = {
set value(value: T);
get value(): T;
};

class ReferenceObject<T> {
public constructor(private internalValue: T) {}

get value() {
return this.internalValue;
}

set value(t: T) {
this.internalValue = t;
}
}

export function createReference<T>(initial: T): Reference<T> {
return new ReferenceObject(initial);
}
9 changes: 2 additions & 7 deletions packages/library/src/runtime/Balances.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { EventsRecord, NoConfig } from "@proto-kit/common";
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, PublicKey, Struct, Provable } from "o1js";

import { UInt64 } from "../math/UInt64";
Expand Down
9 changes: 2 additions & 7 deletions packages/library/src/runtime/Withdrawals.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
RuntimeEvents,
runtimeModule,
RuntimeModule,
state,
} from "@proto-kit/module";
import { StateMap, Withdrawal } from "@proto-kit/protocol";
import { RuntimeEvents, runtimeModule, RuntimeModule } from "@proto-kit/module";
import { StateMap, Withdrawal, state } from "@proto-kit/protocol";
import { Field, PublicKey, Struct } from "o1js";
import { inject } from "tsyringe";

Expand Down
1 change: 0 additions & 1 deletion packages/module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export * from "./runtime/RuntimeModule";
export * from "./runtime/RuntimeEnvironment";
export * from "./runtime/Runtime";
export * from "./state/InMemoryStateService";
export * from "./state/decorator";
export * from "./method/MethodParameterEncoder";
export * from "./runtime/MethodIdResolver";
export * from "./factories/MethodIdFactory";
6 changes: 3 additions & 3 deletions packages/module/src/method/runtimeMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function toWrappedMethod(
const stateTransitionsHash = toStateTransitionsHash(stateTransitions);
const eventsHash = toEventsHash(events);

const { name, runtime } = this;
const { name, parent: runtime } = this;

if (name === undefined) {
throw errors.runtimeNameNotSet();
Expand Down Expand Up @@ -275,10 +275,10 @@ function runtimeMethodInternal(options: {
executionContext.beforeMethod(constructorName, methodName, args);

if (executionContext.isTopLevel) {
if (!this.runtime) {
if (!this.parent) {
throw errors.runtimeNotProvided(constructorName);
}
executionContext.setProver(prover.bind(this.runtime.zkProgrammable));
executionContext.setProver(prover.bind(this.parent.zkProgrammable));
}

let result: unknown;
Expand Down
2 changes: 1 addition & 1 deletion packages/module/src/runtime/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class Runtime<Modules extends RuntimeModulesRecord>
containedModule: InstanceType<Modules[StringKeyOf<Modules>]>
) {
containedModule.name = moduleName;
containedModule.runtime = this;
containedModule.parent = this;

super.decorateModule(moduleName, containedModule);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/module/src/runtime/RuntimeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class RuntimeModule<

public name?: string;

public runtime?: RuntimeEnvironment;
public parent?: RuntimeEnvironment;

public events?: RuntimeEvents<any> = undefined;

Expand Down
61 changes: 0 additions & 61 deletions packages/module/src/state/decorator.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/protocol/src/hashing/mina-prefixes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const MINA_PREFIXES = {
event: "MinaZkappEvent******",
events: "MinaZkappEvents*****",
sequenceEvents: "MinaZkappSeqEvents**",
} as const;

export const MINA_SALTS = {
empty_actions: "MinaZkappActionsEmpty",
empty_events: "MinaZkappEventsEmpty",
};
23 changes: 23 additions & 0 deletions packages/protocol/src/hashing/protokit-prefixes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import padEnd from "lodash/padEnd";
import mapValues from "lodash/mapValues";

const length = 20;
function padToHashPrefix(s: string): string {
if (s.length > 20) {
throw new Error(`Prefix string ${s} is too long (max ${length})`);
}
return padEnd(s, length, "*");
}

function padPrefixRecord<T extends Record<string, string>>(
record: T
): {
[Key in keyof T]: string;
} {
return mapValues(record, padToHashPrefix);
}

export const PROTOKIT_PREFIXES = padPrefixRecord({
STATE_PROTOCOL: "pk-protocol-state",
STATE_RUNTIME: "pk-runtime-state",
});
4 changes: 2 additions & 2 deletions packages/protocol/src/hooks/AccountStateHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { injectable } from "tsyringe";
import { noop } from "@proto-kit/common";

import { StateMap } from "../state/StateMap";
import { protocolState } from "../state/protocol/ProtocolState";
import { state } from "../state/protocol/ProtocolState";
import {
ProvableTransactionHook,
BeforeTransactionHookArguments,
Expand All @@ -16,7 +16,7 @@ export class AccountState extends Struct({

@injectable()
export class AccountStateHook extends ProvableTransactionHook {
@protocolState() public accountState = StateMap.from<PublicKey, AccountState>(
@state() public accountState = StateMap.from<PublicKey, AccountState>(
PublicKey,
AccountState
);
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ export * from "./settlement/modules/NetworkStateSettlementModule";
export * from "./settlement/messages/Deposit";
export * from "./settlement/messages/Withdrawal";
export { constants as ProtocolConstants } from "./Constants";
export * from "./hashing/protokit-prefixes";
export * from "./hashing/mina-prefixes";
10 changes: 8 additions & 2 deletions packages/protocol/src/model/Path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field, type FlexibleProvablePure, Poseidon } from "o1js";
import { hashWithPrefix } from "@proto-kit/common";

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

Expand All @@ -21,10 +22,15 @@ export class Path {
*
* @param className
* @param propertyKey
* @param prefix
* @returns Field representation of class name + property name
*/
public static fromProperty(className: string, propertyKey: string): Field {
return Poseidon.hash([
public static fromProperty(
className: string,
propertyKey: string,
prefix: string
): Field {
return hashWithPrefix(prefix, [
Path.toField(className),
Path.toField(propertyKey),
Field(0),
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/src/protocol/Protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class Protocol<
containedModule: InstanceType<Modules[StringKeyOf<Modules>]>
) {
log.debug(`Decorated ${moduleName}`);
containedModule.protocol = this;
containedModule.parent = this;

if (containedModule instanceof TransitioningProtocolModule) {
containedModule.name = moduleName;
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/src/protocol/ProtocolModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { ProtocolEnvironment } from "./ProtocolEnvironment";
export abstract class ProtocolModule<
Config = NoConfig,
> extends ConfigurableModule<Config> {
public protocol?: ProtocolEnvironment;
public parent?: ProtocolEnvironment;

public get areProofsEnabled(): AreProofsEnabled | undefined {
return this.protocol?.getAreProofsEnabled();
return this.parent?.getAreProofsEnabled();
}

public create(childContainerProvider: ChildContainerProvider): void {
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol/src/protocol/ProvableBlockHook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Field } from "o1js";
import { NoConfig } from "@proto-kit/common";

import { NetworkState } from "../model/network/NetworkState";
import { MethodPublicOutput } from "../model/MethodPublicOutput";
Expand Down Expand Up @@ -53,7 +54,7 @@ export function toAfterTransactionHookArgument(

// Purpose is to build transition from -> to network state
export abstract class ProvableBlockHook<
Config,
Config = NoConfig,
> extends TransitioningProtocolModule<Config> {
public abstract beforeBlock(
networkState: NetworkState,
Expand Down
7 changes: 6 additions & 1 deletion packages/protocol/src/settlement/contracts/BridgeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "../messages/OutgoingMessageArgument";
import { Path } from "../../model/Path";
import { Withdrawal } from "../messages/Withdrawal";
import { PROTOKIT_PREFIXES } from "../../hashing/protokit-prefixes";

import type { SettlementContractType } from "./SettlementSmartContract";

Expand Down Expand Up @@ -137,7 +138,11 @@ export abstract class BridgeContractBase extends TokenContractV2 {

const [withdrawalModule, withdrawalStateName] =
BridgeContractBase.args.withdrawalStatePath;
const mapPath = Path.fromProperty(withdrawalModule, withdrawalStateName);
const mapPath = Path.fromProperty(
withdrawalModule,
withdrawalStateName,
PROTOKIT_PREFIXES.STATE_RUNTIME
);

// Count account creation fee to return later, so that the sender can fund
// those accounts with a separate AU
Expand Down
Loading