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

Commit

Permalink
feat: add createRawChainSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
kratico committed Apr 19, 2023
1 parent e33ae91 commit 0a6ec57
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 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
69 changes: 33 additions & 36 deletions 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 @@ -23,14 +28,9 @@ export async function startNetworkForMetadata(
signal: AbortSignal,
): Promise<Network> {
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,
Expand All @@ -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),
}
}

Expand Down

0 comments on commit 0a6ec57

Please sign in to comment.