diff --git a/devnets/chainSpec.ts b/devnets/chainSpec.ts index 37de44d02..3c964624f 100644 --- a/devnets/chainSpec.ts +++ b/devnets/chainSpec.ts @@ -8,7 +8,6 @@ export async function createCustomChainSpec( customize: (chainSpec: ChainSpec) => void, ) { await ensureDir(tempDir) - const specResult = await new Deno.Command(binary, { args: ["build-spec", "--disable-default-bootnode", "--chain", chain], }).output() @@ -18,21 +17,22 @@ export async function createCustomChainSpec( } const spec = JSON.parse(new TextDecoder().decode(specResult.stdout)) customize(spec) - const specPath = path.join(tempDir, `chainspec.json`) await Deno.writeTextFile(specPath, JSON.stringify(spec, undefined, 2)) + return createRawChainSpec(tempDir, binary, specPath) +} +export async function createRawChainSpec(tempDir: string, binary: string, chain: string) { + await ensureDir(tempDir) const rawResult = await new Deno.Command(binary, { - args: ["build-spec", "--disable-default-bootnode", "--chain", specPath, "--raw"], + args: ["build-spec", "--disable-default-bootnode", "--chain", chain, "--raw"], }).output() if (!rawResult.success) { // TODO: improve error message throw new Error("build-spec --raw failed") } - const rawPath = path.join(tempDir, `chainspec-raw.json`) await Deno.writeFile(rawPath, rawResult.stdout) - return rawPath } diff --git a/devnets/startNetwork.ts b/devnets/startNetwork.ts index 8051f5b06..7e82f065b 100644 --- a/devnets/startNetwork.ts +++ b/devnets/startNetwork.ts @@ -4,7 +4,12 @@ import { writableStreamFromWriter } from "../deps/std/streams.ts" import { getFreePort, portReady } from "../util/port.ts" import { resolveBinary } from "./binary.ts" import { NetworkConfig } from "./CapiConfig.ts" -import { createCustomChainSpec, GenesisConfig, getGenesisConfig } from "./chainSpec.ts" +import { + createCustomChainSpec, + createRawChainSpec, + GenesisConfig, + getGenesisConfig, +} from "./chainSpec.ts" import { addDevUsers } from "./devUsers.ts" export interface Network { @@ -17,6 +22,51 @@ export interface NetworkChain { ports: number[] } +export async function startNetworkForMetadata( + tempDir: string, + config: NetworkConfig, + signal: AbortSignal, +): Promise { + const relayBinary = await resolveBinary(config.binary, signal) + const relaySpec = await createRawChainSpec(path.join(tempDir, "relay"), relayBinary, config.chain) + const [relay, paras] = await Promise.all([ + spawnChain( + path.join(tempDir, "relay"), + relayBinary, + relaySpec, + 1, + [], + relayBinary, + signal, + ), + Promise.all( + Object.entries(config.parachains ?? {}).map(async ([name, config]) => { + const binary = await resolveBinary(config.binary, signal) + const chain = await spawnChain( + path.join(tempDir, name), + binary, + await createRawChainSpec(path.join(tempDir, name), binary, config.chain), + 1, + [ + "--", + "--execution", + "wasm", + "--chain", + relaySpec, + ], + relayBinary, + signal, + ) + return [name, chain] satisfies Narrow + }), + ), + ]) + return { + relay, + paras: Object.fromEntries(paras), + } +} + export async function startNetwork( tempDir: string, config: NetworkConfig, diff --git a/devnets/syncConfig.ts b/devnets/syncConfig.ts index 728843a1f..212cc1f57 100644 --- a/devnets/syncConfig.ts +++ b/devnets/syncConfig.ts @@ -8,7 +8,7 @@ import { $codegenSpec, CodegenEntry, CodegenSpec } from "../server/codegenSpec.t import { normalizePackageName, withSignal } from "../util/mod.ts" import { normalizeTypeName } from "../util/normalize.ts" import { CapiConfig } from "./CapiConfig.ts" -import { startNetwork } from "./startNetwork.ts" +import { startNetworkForMetadata } from "./startNetwork.ts" export async function syncConfig(tempDir: string, config: CapiConfig) { return withSignal(async (signal) => { @@ -26,7 +26,7 @@ export async function syncConfig(tempDir: string, config: CapiConfig) { }) return } - const network = await startNetwork(path.join(tempDir, name), chain, signal) + const network = await startNetworkForMetadata(path.join(tempDir, name), chain, signal) await Promise.all( [ [undefined, network.relay] as const,