From e772ebd5a90fe9362f3beb1d78007e5bdbb61ec7 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Tue, 18 Apr 2023 16:17:31 -0300 Subject: [PATCH 1/2] feat: add startNetworkForMetadata --- devnets/startNetwork.ts | 53 +++++++++++++++++++++++++++++++++++++++++ devnets/syncConfig.ts | 4 ++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/devnets/startNetwork.ts b/devnets/startNetwork.ts index 8051f5b06..281e4792b 100644 --- a/devnets/startNetwork.ts +++ b/devnets/startNetwork.ts @@ -17,6 +17,59 @@ 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 createCustomChainSpec( + path.join(tempDir, "relay"), + relayBinary, + config.chain, + () => {}, + ) + return { + relay: await spawnChain( + path.join(tempDir, "relay"), + relayBinary, + relaySpec, + 1, + [], + relayBinary, + signal, + ), + paras: Object.fromEntries( + await 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 createCustomChainSpec( + path.join(tempDir, name), + binary, + config.chain, + () => {}, + ), + 1, + [ + "--", + "--execution", + "wasm", + "--chain", + relaySpec, + ], + relayBinary, + signal, + ) + return [name, chain] satisfies Narrow + }), + ), + ), + } +} + 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, From a4cf46e7a3f65ae1a420698101a58e54d03a1513 Mon Sep 17 00:00:00 2001 From: Matias Volpe Date: Wed, 19 Apr 2023 09:16:24 -0300 Subject: [PATCH 2/2] feat: add createRawChainSpec --- devnets/chainSpec.ts | 10 +++--- devnets/startNetwork.ts | 69 ++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 41 deletions(-) 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 281e4792b..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 { @@ -23,14 +28,9 @@ export async function startNetworkForMetadata( signal: AbortSignal, ): Promise { const relayBinary = await resolveBinary(config.binary, signal) - const relaySpec = await createCustomChainSpec( - path.join(tempDir, "relay"), - relayBinary, - config.chain, - () => {}, - ) - return { - relay: await spawnChain( + const relaySpec = await createRawChainSpec(path.join(tempDir, "relay"), relayBinary, config.chain) + const [relay, paras] = await Promise.all([ + spawnChain( path.join(tempDir, "relay"), relayBinary, relaySpec, @@ -39,34 +39,31 @@ export async function startNetworkForMetadata( relayBinary, signal, ), - paras: Object.fromEntries( - await 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 createCustomChainSpec( - path.join(tempDir, name), - binary, - config.chain, - () => {}, - ), - 1, - [ - "--", - "--execution", - "wasm", - "--chain", - relaySpec, - ], - relayBinary, - signal, - ) - return [name, chain] satisfies Narrow - }), - ), + 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), } }