Skip to content

Commit a1af0d4

Browse files
committed
Added WorkerReadyModule
1 parent d0d597d commit a1af0d4

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

packages/sdk/src/appChain/AppChain.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
QueryTransportModule,
2424
NetworkStateTransportModule,
2525
DummyStateService,
26+
WorkerReadyModule,
2627
} from "@proto-kit/sequencer";
2728
import {
2829
NetworkState,
@@ -341,6 +342,11 @@ export class AppChain<
341342

342343
// this.runtime.start();
343344
await this.sequencer.start();
345+
346+
// Wait for readyness for worker-ish configurations
347+
await this.sequencer.dependencyContainer
348+
.resolve(WorkerReadyModule)
349+
.waitForReady();
344350
}
345351
}
346352
/* eslint-enable @typescript-eslint/consistent-type-assertions */

packages/sequencer/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from "./worker/queue/LocalTaskQueue";
1515
export * from "./worker/worker/FlowTaskWorker";
1616
export * from "./worker/worker/LocalTaskWorkerModule";
1717
export * from "./worker/worker/TaskWorkerModule";
18+
export * from "./worker/worker/WorkerReadyModule";
1819
export * from "./protocol/baselayer/BaseLayer";
1920
export * from "./protocol/baselayer/MinaBaseLayer";
2021
export * from "./protocol/baselayer/NoopBaseLayer";

packages/sequencer/src/protocol/production/helpers/CompileRegistry.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ export class CompileRegistry {
2020
name: string,
2121
zkProgram: { compile: () => Promise<CompileArtifact> }
2222
) {
23+
let newPromise = false;
2324
if (this.compilationPromises[name] === undefined) {
2425
log.time(`Compiling ${name}`);
2526
this.compilationPromises[name] = zkProgram.compile();
26-
log.timeEnd.info(`Compiling ${name}`);
27+
newPromise = true;
2728
}
2829
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
29-
return (await this.compilationPromises[name]) as CompileArtifact;
30+
const result = (await this.compilationPromises[name]) as CompileArtifact;
31+
if (newPromise) {
32+
log.timeEnd.info(`Compiling ${name}`);
33+
}
34+
return result;
3035
}
3136

3237
public async compileSmartContract(
@@ -36,16 +41,22 @@ export class CompileRegistry {
3641
},
3742
proofsEnabled: boolean = true
3843
) {
44+
let newPromise = false;
3945
if (this.compilationPromises[name] === undefined) {
4046
if (proofsEnabled) {
4147
log.time(`Compiling ${name}`);
4248
this.compilationPromises[name] = contract.compile();
43-
log.timeEnd.info(`Compiling ${name}`);
4449
} else {
4550
this.compilationPromises[name] = Promise.resolve({});
4651
}
4752
}
4853
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
49-
return (await this.compilationPromises[name]) as ContractCompileArtifact;
54+
const result = (await this.compilationPromises[
55+
name
56+
]) as ContractCompileArtifact;
57+
if (newPromise) {
58+
log.timeEnd.info(`Compiling ${name}`);
59+
}
60+
return result;
5061
}
5162
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { injectable } from "tsyringe";
2+
import { injectOptional } from "@proto-kit/common";
3+
import { LocalTaskWorkerModule } from "./LocalTaskWorkerModule";
4+
5+
/**
6+
* Module to safely wait for the finish of the worker startup
7+
* Behaves like a noop for non-worker appchain configurations
8+
*/
9+
@injectable()
10+
export class WorkerReadyModule {
11+
public constructor(
12+
@injectOptional("LocalTaskWorkerModule")
13+
private readonly localTaskWorkerModule:
14+
| LocalTaskWorkerModule<any>
15+
| undefined
16+
) {}
17+
18+
public async waitForReady(): Promise<void> {
19+
if (this.localTaskWorkerModule !== undefined) {
20+
const module = this.localTaskWorkerModule;
21+
return new Promise<void>((res, rej) => {
22+
module.containerEvents.on("ready", (ready) => {
23+
if (ready) {
24+
res();
25+
} else {
26+
rej(new Error("Couldn't get ready"));
27+
}
28+
});
29+
});
30+
}
31+
}
32+
}

packages/sequencer/test/integration/BlockProduction.test.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ describe("block production", () => {
156156
throw e;
157157
}
158158

159-
// Await worker readiness
160-
await new Promise<boolean>((res) => {
161-
app
162-
.resolve("Sequencer")
163-
.resolve("LocalTaskWorkerModule")
164-
.containerEvents.on("ready", res);
165-
});
166-
167159
appChain = app;
168160

169161
({ runtime, sequencer, protocol } = app);

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,6 @@ describe("Proven", () => {
8282
const childContainer = container.createChildContainer();
8383
await app.start(true, childContainer);
8484

85-
const ready = await new Promise<boolean>((res) => {
86-
app
87-
.resolve("Sequencer")
88-
.resolve("LocalTaskWorkerModule")
89-
.containerEvents.on("ready", res);
90-
});
91-
92-
expect(ready).toBe(true);
93-
9485
test = app.sequencer.dependencyContainer.resolve(BlockTestService);
9586
} catch (e) {
9687
console.error(e);

0 commit comments

Comments
 (0)