Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: add startNetworkForMetadata (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
kratico authored Apr 19, 2023
1 parent 000c385 commit a611e15
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
10 changes: 5 additions & 5 deletions devnets/chainSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

Expand Down
52 changes: 51 additions & 1 deletion devnets/startNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,6 +22,51 @@ export interface NetworkChain {
ports: number[]
}

export async function startNetworkForMetadata(
tempDir: string,
config: NetworkConfig,
signal: AbortSignal,
): Promise<Network> {
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,
Expand Down
4 changes: 2 additions & 2 deletions devnets/syncConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down

0 comments on commit a611e15

Please sign in to comment.