Skip to content

Commit b58433c

Browse files
committed
Refactoring
1 parent 76b4fa8 commit b58433c

File tree

8 files changed

+58
-90
lines changed

8 files changed

+58
-90
lines changed

packages/common/src/compiling/CompileRegistry.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ export class CompileRegistry {
4141

4242
private artifacts: ArtifactRecord = {};
4343

44-
// private cachedModuleOutputs: Record<string, ArtifactRecord | void> = {};
45-
4644
// TODO Add possibility to force recompilation for non-sideloaded dependencies
4745

4846
public async compile(target: CompileTarget) {
@@ -54,47 +52,6 @@ export class CompileRegistry {
5452
return this.artifacts[target.name];
5553
}
5654

57-
// public async compileModule<
58-
// ReturnType extends ArtifactRecord,
59-
// Dependencies extends Record<string, CompilableModule>,
60-
// >(
61-
// compile: (
62-
// compiler: AtomicCompileHelper,
63-
// args: InferDependencyArtifacts<Dependencies>
64-
// ) => Promise<ReturnType>,
65-
// dependencies?: Dependencies
66-
// ): Promise<ReturnType | undefined> {
67-
// const collectedArtifacts = await mapSequential(
68-
// Object.entries(dependencies ?? {}),
69-
// async ([depName, dep]) => {
70-
// if (this.cachedModuleOutputs[depName] !== undefined) {
71-
// return [depName, this.cachedModuleOutputs[depName]];
72-
// }
73-
// const artifact = await dep.compile(this);
74-
// if (artifact !== undefined) {
75-
// this.artifacts = {
76-
// ...this.artifacts,
77-
// ...artifact,
78-
// };
79-
// }
80-
// this.cachedModuleOutputs[depName] = artifact;
81-
// return [depName, artifact];
82-
// }
83-
// );
84-
//
85-
// const artifacts = await compile(
86-
// this.compile,
87-
// Object.fromEntries(collectedArtifacts)
88-
// );
89-
//
90-
// this.artifacts = {
91-
// ...this.artifacts,
92-
// ...artifacts,
93-
// };
94-
//
95-
// return artifacts;
96-
// }
97-
9855
public getArtifact(name: string) {
9956
if (this.artifacts[name] === undefined) {
10057
throw new Error(

packages/common/src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,9 @@ export function isSubtypeOfName(
182182
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
183183
return isSubtypeOfName(Object.getPrototypeOf(clas), name);
184184
}
185+
186+
// TODO Eventually, replace this by a schema validation library
187+
export function safeParseJson<T>(json: string) {
188+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
189+
return JSON.parse(json) as T;
190+
}

packages/protocol/src/settlement/modularity/ProvableSettlementHook.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Field, PublicKey, UInt32 } from "o1js";
2+
import { InferProofBase } from "@proto-kit/common";
23

34
import { ProtocolModule } from "../../protocol/ProtocolModule";
45
import { NetworkState } from "../../model/network/NetworkState";
56
import type { BlockProof } from "../../prover/block/BlockProver";
67
import type { SettlementSmartContractBase } from "../contracts/SettlementSmartContract";
78

9+
export type InputBlockProof = InferProofBase<BlockProof>;
10+
811
export type SettlementStateRecord = {
912
sequencerKey: PublicKey;
1013
lastSettlementL1BlockHeight: UInt32;
@@ -15,7 +18,7 @@ export type SettlementStateRecord = {
1518
};
1619

1720
export type SettlementHookInputs = {
18-
blockProof: BlockProof;
21+
blockProof: InputBlockProof;
1922
fromNetworkState: NetworkState;
2023
toNetworkState: NetworkState;
2124
newPromisedMessagesHash: Field;

packages/sequencer/src/protocol/production/tasks/CircuitCompilerTask.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ArtifactRecord,
88
CompileRegistry,
99
CompilableModule,
10+
safeParseJson,
1011
} from "@proto-kit/common";
1112
import {
1213
MandatorySettlementModulesRecord,
@@ -25,21 +26,11 @@ export type CompilerTaskParams = {
2526
runtimeVKRoot?: string;
2627
};
2728

28-
type SerializedArtifactRecord = Record<
29+
export type SerializedArtifactRecord = Record<
2930
string,
3031
{ verificationKey: { hash: string; data: string } }
3132
>;
3233

33-
export class SimpleJSONSerializer<Type> implements TaskSerializer<Type> {
34-
public toJSON(parameters: Type): string {
35-
return JSON.stringify(parameters);
36-
}
37-
38-
public fromJSON(json: string): Type {
39-
return JSON.parse(json) as Type;
40-
}
41-
}
42-
4334
export class ArtifactRecordSerializer {
4435
public toJSON(input: ArtifactRecord): SerializedArtifactRecord {
4536
const temp: SerializedArtifactRecord = Object.keys(
@@ -69,7 +60,7 @@ export class ArtifactRecordSerializer {
6960
),
7061
},
7162
};
72-
}, {} as ArtifactRecord);
63+
}, {});
7364
}
7465
}
7566

@@ -90,19 +81,25 @@ export class CircuitCompilerTask extends UnpreparingTask<
9081
}
9182

9283
public inputSerializer(): TaskSerializer<CompilerTaskParams> {
84+
type CompilerTaskParamsJSON = {
85+
targets: string[];
86+
runtimeVKRoot?: string;
87+
existingArtifacts: SerializedArtifactRecord;
88+
};
89+
9390
const serializer = new ArtifactRecordSerializer();
9491
return {
9592
toJSON: (input) =>
9693
JSON.stringify({
9794
targets: input.targets,
98-
root: input.runtimeVKRoot,
95+
runtimeVKRoot: input.runtimeVKRoot,
9996
existingArtifacts: serializer.toJSON(input.existingArtifacts),
100-
}),
97+
} satisfies CompilerTaskParamsJSON),
10198
fromJSON: (input) => {
102-
const json = JSON.parse(input);
99+
const json = safeParseJson<CompilerTaskParamsJSON>(input);
103100
return {
104101
targets: json.targets,
105-
root: json.runtimeVKRoot,
102+
runtimeVKRoot: json.runtimeVKRoot,
106103
existingArtifacts: serializer.fromJSON(json.existingArtifacts),
107104
};
108105
},
@@ -113,7 +110,8 @@ export class CircuitCompilerTask extends UnpreparingTask<
113110
const serializer = new ArtifactRecordSerializer();
114111
return {
115112
toJSON: (input) => JSON.stringify(serializer.toJSON(input)),
116-
fromJSON: (input) => serializer.fromJSON(JSON.parse(input)),
113+
fromJSON: (input) =>
114+
serializer.fromJSON(safeParseJson<SerializedArtifactRecord>(input)),
117115
};
118116
}
119117

packages/sequencer/src/sequencer/SequencerStartupModule.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ export class SequencerStartupModule extends SequencerModule {
9191
targets: ["protocol"],
9292
runtimeVKRoot: undefined,
9393
},
94-
async (result) => {
95-
results.protocol = result;
94+
async (protocolResult) => {
95+
results.protocol = protocolResult;
9696
resolveIfPossible();
9797
}
9898
);
@@ -104,8 +104,8 @@ export class SequencerStartupModule extends SequencerModule {
104104
targets: ["Settlement.BridgeContract"],
105105
runtimeVKRoot: undefined,
106106
},
107-
async (result) => {
108-
results.bridge = result;
107+
async (bridgeResult) => {
108+
results.bridge = bridgeResult;
109109
resolveIfPossible();
110110
}
111111
);
@@ -143,6 +143,9 @@ export class SequencerStartupModule extends SequencerModule {
143143

144144
this.compileRegistry.addArtifactsRaw(record);
145145

146+
// TODO Compile all contracts and retrieve artifacts to enable crafting of
147+
// the deployments - edit: can also be done on-demand with the CompileTask
148+
146149
await this.registrationFlow.start({
147150
runtimeVerificationKeyRoot: root,
148151
bridgeContractVerificationKey: bridgeVk?.verificationKey,

packages/sequencer/src/settlement/tasks/SettlementProvingTask.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,11 @@ export class SettlementProvingTask
393393
contractClasses[key] = module.contractFactory();
394394
}
395395

396-
try {
397-
for (const [key, module] of modules) {
398-
log.debug(`Compiling Settlement Module ${key}`);
396+
for (const [key, module] of modules) {
397+
log.debug(`Compiling Settlement Module ${key}`);
399398

400-
// eslint-disable-next-line no-await-in-loop
401-
await module.compile(this.compileRegistry);
402-
}
403-
} catch (e) {
404-
console.error(e);
405-
throw e;
399+
// eslint-disable-next-line no-await-in-loop
400+
await module.compile(this.compileRegistry);
406401
}
407402

408403
this.contractRegistry = new ContractRegistry(contractClasses);

packages/sequencer/src/worker/worker/startup/WorkerRegistrationTask.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ArtifactRecord,
55
ChildVerificationKeyService,
66
CompileRegistry,
7+
safeParseJson,
78
} from "@proto-kit/common";
89
import { inject, injectable } from "tsyringe";
910
import {
@@ -15,8 +16,14 @@ import { VerificationKey } from "o1js";
1516

1617
import { Task } from "../../flow/Task";
1718
import { AbstractStartupTask } from "../../flow/AbstractStartupTask";
18-
import { VerificationKeySerializer } from "../../../protocol/production/helpers/VerificationKeySerializer";
19-
import { ArtifactRecordSerializer } from "../../../protocol/production/tasks/CircuitCompilerTask";
19+
import {
20+
VerificationKeyJSON,
21+
VerificationKeySerializer,
22+
} from "../../../protocol/production/helpers/VerificationKeySerializer";
23+
import {
24+
ArtifactRecordSerializer,
25+
SerializedArtifactRecord,
26+
} from "../../../protocol/production/tasks/CircuitCompilerTask";
2027

2128
import { CloseWorkerError } from "./CloseWorkerError";
2229

@@ -76,6 +83,12 @@ export class WorkerRegistrationTask
7683
}
7784

7885
public inputSerializer() {
86+
type WorkerStartupPayloadJSON = {
87+
runtimeVerificationKeyRoot: string;
88+
bridgeContractVerificationKey: VerificationKeyJSON | undefined;
89+
compiledArtifacts: SerializedArtifactRecord;
90+
};
91+
7992
const artifactSerializer = new ArtifactRecordSerializer();
8093
return {
8194
toJSON: (payload: WorkerStartupPayload) => {
@@ -91,27 +104,22 @@ export class WorkerRegistrationTask
91104
compiledArtifacts: artifactSerializer.toJSON(
92105
payload.compiledArtifacts
93106
),
94-
});
107+
} satisfies WorkerStartupPayloadJSON);
95108
},
96109
fromJSON: (payload: string) => {
97-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
98-
const jsonObject = JSON.parse(payload);
110+
const jsonObject = safeParseJson<WorkerStartupPayloadJSON>(payload);
99111

100112
return {
101113
runtimeVerificationKeyRoot: BigInt(
102-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
103114
jsonObject.runtimeVerificationKeyRoot
104115
),
105116
bridgeContractVerificationKey:
106117
jsonObject.bridgeContractVerificationKey !== undefined
107118
? VerificationKeySerializer.fromJSON(
108-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
109119
jsonObject.bridgeContractVerificationKey
110120
)
111121
: undefined,
112-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
113122
compiledArtifacts: artifactSerializer.fromJSON(
114-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
115123
jsonObject.compiledArtifacts
116124
),
117125
};

packages/sequencer/test/integration/Proven.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,9 @@ import {
1818
import { VanillaProtocolModules } from "@proto-kit/library";
1919
import { AppChain, InMemoryAreProofsEnabled } from "@proto-kit/sdk";
2020
import { container } from "tsyringe";
21-
import { PrivateKey, UInt64, VerificationKey } from "o1js";
21+
import { PrivateKey, UInt64 } from "o1js";
2222

2323
import { testingSequencerFromModules } from "../TestingSequencer";
24-
25-
import { ProtocolStateTestHook } from "./mocks/ProtocolStateTestHook";
26-
import { BlockTestService } from "./services/BlockTestService";
27-
import { ProvenBalance } from "./mocks/ProvenBalance";
28-
import { FungibleTokenContractModule } from "../../src/settlement/utils/FungibleTokenContractModule";
29-
import { FungibleTokenAdminContractModule } from "../../src/settlement/utils/FungibleTokenAdminContractModule";
3024
import {
3125
MinaBaseLayer,
3226
ProvenSettlementPermissions,
@@ -35,6 +29,10 @@ import {
3529
WithdrawalQueue,
3630
} from "../../src";
3731

32+
import { ProtocolStateTestHook } from "./mocks/ProtocolStateTestHook";
33+
import { BlockTestService } from "./services/BlockTestService";
34+
import { ProvenBalance } from "./mocks/ProvenBalance";
35+
3836
const timeout = 300000;
3937

4038
describe("Proven", () => {

0 commit comments

Comments
 (0)