Skip to content

Commit 8e5f578

Browse files
authored
Merge pull request #247 from proto-kit/feature/inject-runtime
Make RuntimeModules Injectable
2 parents 4ecf421 + 3d6ebd8 commit 8e5f578

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

packages/library/src/hooks/TransactionFeeHook.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ const errors = {
5151
export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHookConfig> {
5252
public constructor(
5353
// dependency on runtime, since balances are part of runtime logic
54-
@inject("Runtime") public runtime: Runtime<RuntimeModulesRecord>
54+
@inject("Runtime") public runtime: Runtime<RuntimeModulesRecord>,
55+
@inject("Balances") public balances: Balances
5556
) {
5657
super();
5758
}
@@ -91,10 +92,6 @@ export class TransactionFeeHook extends ProvableTransactionHook<TransactionFeeHo
9192
super.config = value;
9293
}
9394

94-
public get balances() {
95-
return this.runtime.dependencyContainer.resolve<Balances>("Balances");
96-
}
97-
9895
public get feeAnalyzer() {
9996
if (this.persistedFeeAnalyzer === undefined) {
10097
throw new Error("TransactionFeeHook.start not called by protocol");

packages/protocol/src/protocol/Protocol.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,28 @@ export class Protocol<
187187
);
188188
}
189189
});
190+
191+
// Cross-register all runtime modules to the protocol container for easier
192+
// access of runtime modules inside protocol hooks
193+
if (this.container.isRegistered("Runtime", true)) {
194+
const runtimeContainer: ModuleContainer<any> =
195+
this.container.resolve("Runtime");
196+
197+
runtimeContainer.moduleNames.forEach((runtimeModuleName) => {
198+
this.container.register(runtimeModuleName, {
199+
useFactory: (dependencyContainer) => {
200+
// Prevents creation of closure
201+
const runtime: ModuleContainer<any> =
202+
dependencyContainer.resolve("Runtime");
203+
return runtime.resolve(runtimeModuleName);
204+
},
205+
});
206+
});
207+
} else {
208+
log.warn(
209+
"Couldn't resolve Runtime reference in Protocol, resolving RuntimeModules in hooks won't be available"
210+
);
211+
}
190212
}
191213

192214
public async start() {

packages/protocol/test/TestingProtocol.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import { WithZkProgrammable, ZkProgrammable } from "@proto-kit/common";
21
import { container } from "tsyringe";
2+
import { Runtime } from "@proto-kit/module";
3+
import { Balance } from "@proto-kit/sequencer/test/integration/mocks/Balance";
4+
import { NoopRuntime } from "@proto-kit/sequencer/test/integration/mocks/NoopRuntime";
35

46
import {
57
AccountStateHook,
68
BlockHeightHook,
79
BlockProver,
810
LastStateRootBlockHook,
9-
MethodPublicOutput,
1011
Protocol,
1112
StateTransitionProver,
1213
} from "../src";
1314

14-
class RuntimeMock implements WithZkProgrammable<undefined, MethodPublicOutput> {
15-
zkProgrammable: ZkProgrammable<undefined, MethodPublicOutput> =
16-
undefined as unknown as ZkProgrammable<undefined, MethodPublicOutput>;
17-
}
18-
1915
export function createAndInitTestingProtocol() {
2016
const ProtocolClass = Protocol.from({
2117
modules: {
@@ -35,11 +31,22 @@ export function createAndInitTestingProtocol() {
3531
StateTransitionProver: {},
3632
LastStateRoot: {},
3733
});
38-
protocol.create(() => container.createChildContainer());
3934

40-
protocol.registerValue({
41-
Runtime: new RuntimeMock(),
35+
const appChain = container.createChildContainer();
36+
37+
appChain.register("Runtime", {
38+
useClass: Runtime.from({
39+
modules: {
40+
Balance,
41+
NoopRuntime,
42+
},
43+
config: {
44+
Balance: {},
45+
NoopRuntime: {},
46+
},
47+
}),
4248
});
49+
protocol.create(() => appChain.createChildContainer());
4350

4451
return protocol;
4552
}

packages/sdk/test/modularization.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import "reflect-metadata";
22
import { MethodIdResolver, Runtime, RuntimeModule } from "@proto-kit/module";
33
import { ChildContainerProvider } from "@proto-kit/common";
44
import { Protocol, ProtocolModule } from "@proto-kit/protocol";
5-
import { VanillaProtocolModules } from "@proto-kit/library";
5+
import {
6+
VanillaProtocolModules,
7+
VanillaRuntimeModules,
8+
} from "@proto-kit/library";
69
import { Sequencer, SequencerModule } from "@proto-kit/sequencer";
710
import { PrivateKey } from "o1js";
811

@@ -13,7 +16,6 @@ class TestRuntimeModule extends RuntimeModule<object> {
1316

1417
public create(childContainerProvider: ChildContainerProvider) {
1518
super.create(childContainerProvider);
16-
1719
// Just to test if it doesn't throw
1820
childContainerProvider();
1921

@@ -47,9 +49,9 @@ describe("modularization", () => {
4749
it("should initialize all modules correctly", async () => {
4850
const appChain = AppChain.from({
4951
Runtime: Runtime.from({
50-
modules: {
52+
modules: VanillaRuntimeModules.with({
5153
TestRuntimeModule,
52-
},
54+
}),
5355
}),
5456
Protocol: Protocol.from({
5557
modules: VanillaProtocolModules.with({
@@ -66,6 +68,7 @@ describe("modularization", () => {
6668

6769
appChain.configurePartial({
6870
Runtime: {
71+
Balances: {},
6972
TestRuntimeModule: {},
7073
},
7174
Protocol: {

0 commit comments

Comments
 (0)