Skip to content

Commit

Permalink
dynamic networks
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSulpiride committed Sep 14, 2023
1 parent e56525b commit d8f3233
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
17 changes: 7 additions & 10 deletions packages/api/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NETWORK_NAME_TO_CHAIN_ID, NetworkName } from "types/lib";
import { NetworkName } from "types/lib";
import { IDbController } from "types/lib";
import { Executor } from "executor/lib/executor";
import { Config } from "executor/lib/config";
Expand Down Expand Up @@ -58,24 +58,21 @@ export class ApiApp {

private setupRoutes(): void {
if (this.testingMode) {
this.server.post("/rpc/", this.setupRouteFor("dev"));
this.server.post("/rpc/", this.setupRouteFor("dev", 1337));
logger.info("Setup route for dev: /rpc/");
return;
}

const networkNames: NetworkName[] = this.config.supportedNetworks;
for (const network of networkNames) {
const chainId: number | undefined = NETWORK_NAME_TO_CHAIN_ID[network];
if (chainId == undefined) {
continue;
}
this.server.post(`/${chainId}`, this.setupRouteFor(network));
const networkNames = this.config.supportedNetworks;
for (const [network, chainId] of Object.entries(networkNames)) {
this.server.post(`/${chainId}`, this.setupRouteFor(network, chainId));
logger.info(`Setup route for ${network}: /${chainId}/`);
}
}

private setupRouteFor(network: NetworkName): RouteHandler {
private setupRouteFor(network: NetworkName, chainId: number): RouteHandler {
const relayer = new Executor({
chainId,
network,
db: this.db,
config: this.config,
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/cmds/start/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export async function bundlerHandler(
});
}

await config.fetchChainIds();

if (unsafeMode) {
logger.warn(
"WARNING: Running in unsafe mode, skips opcode check and stake check"
Expand Down
32 changes: 25 additions & 7 deletions packages/executor/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// TODO: create a new package "config" instead of this file and refactor
import { Wallet, providers, utils } from "ethers";
import { NetworkName } from "types/lib";
import {
BundlerConfig,
ConfigOptions,
Expand All @@ -8,7 +9,9 @@ import {
} from "./interfaces";

export class Config {
supportedNetworks: string[];
supportedNetworks: {
[networkName in NetworkName]: number;
};
networks: Networks;
testingMode: boolean;
unsafeMode: boolean;
Expand Down Expand Up @@ -64,20 +67,35 @@ export class Config {
return config;
}

private parseSupportedNetworks(): string[] {
async fetchChainIds(): Promise<void> {
for (const networkName of Object.keys(this.supportedNetworks)) {
const provider = this.getNetworkProvider(networkName);
if (!provider) {
throw new Error(`No provider for ${networkName}`);
}
const network = await provider.getNetwork();
this.supportedNetworks[networkName] = network.chainId;
}
}

private parseSupportedNetworks(): { [name: string]: number } {
if (this.testingMode) {
return ["dev"];
return { dev: 1337 };
}
const envNetworks = NETWORKS_ENV();
if (envNetworks) {
return envNetworks;
let networkNames = envNetworks;
if (!networkNames) {
networkNames = Object.keys(this.config.networks);
}
return Object.keys(this.config.networks);
return networkNames.reduce((networks, networkName) => {
networks[networkName] = 0;
return networks;
}, {} as { [name: string]: number });
}

private parseNetworkConfigs(): Networks {
const networks: Networks = {};
for (const network of this.supportedNetworks) {
for (const network of Object.keys(this.supportedNetworks)) {
const config = this.getDefaultNetworkConfig(network);
networks[network] = {
...config,
Expand Down
5 changes: 2 additions & 3 deletions packages/executor/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ export class Executor {
this.networkName
) as providers.JsonRpcProvider;

const chainId = this.provider.network.chainId;
this.reputationService = new ReputationService(
this.db,
chainId,
this.chainId,
this.networkConfig.minInclusionDenominator,
this.networkConfig.throttlingSlack,
this.networkConfig.banSlack,
Expand All @@ -77,7 +76,7 @@ export class Executor {
);
this.mempoolService = new MempoolService(
this.db,
chainId,
this.chainId,
this.reputationService
);
this.bundlingService = new BundlingService(
Expand Down

0 comments on commit d8f3233

Please sign in to comment.