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

Commit

Permalink
lots more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay committed May 1, 2023
1 parent 5336294 commit e2e2a76
Show file tree
Hide file tree
Showing 29 changed files with 187 additions and 180 deletions.
4 changes: 2 additions & 2 deletions _tasks/use_remote.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This task is used in CI to edit the import map to use capi.dev for codegen
// instead of the local server.

import { checkCodegenUploaded, syncConfig } from "../mod.ts"
import * as nets from "../nets.ts"
import { checkCodegenUploaded, sync } from "../server/mod.ts"

const LOCAL_SERVER = "http://localhost:4646/"

Expand Down Expand Up @@ -33,5 +33,5 @@ if (await checkCodegenUploaded(capiServer, codegenHash)) {
console.log("codegen already uploaded")
} else {
console.log("uploading codegen")
await syncConfig("target/capi", nets, "https://capi.dev/")
await sync("https://capi.dev/", "target/capi", nets)
}
26 changes: 0 additions & 26 deletions cli/bin.ts

This file was deleted.

13 changes: 9 additions & 4 deletions cli/serve.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as flags from "../deps/std/flags.ts"
import { blue, gray, yellow } from "../deps/std/fmt/colors.ts"
import { serve } from "../deps/std/http.ts"
import { createDevnetsHandler, createTempDir } from "../nets/mod.ts"
import { createCodegenHandler, createCorsHandler, createErrorHandler } from "../server/mod.ts"
import {
createCodegenHandler,
createCorsHandler,
createDevnetsHandler,
createErrorHandler,
} from "../server/mod.ts"
import { FsCache, InMemoryCache } from "../util/cache/mod.ts"
import { gracefulExit } from "../util/mod.ts"
import { tempDir } from "../util/tempDir.ts"
import { resolveConfig } from "./resolveConfig.ts"

export default async function(...args: string[]) {
Expand All @@ -19,7 +24,7 @@ export default async function(...args: string[]) {

const config = await resolveConfig(...args)

const tempDir = await createTempDir()
const devnetTempDir = await tempDir(out, "devnet")

const href = `http://localhost:${port}/`

Expand All @@ -35,7 +40,7 @@ export default async function(...args: string[]) {
.catch(() => false)

if (!running) {
const devnetsHandler = createDevnetsHandler(tempDir, config, signal)
const devnetsHandler = createDevnetsHandler(devnetTempDir, config, signal)
const codegenHandler = createCodegenHandler(dataCache, tempCache)
const handler = createCorsHandler(createErrorHandler(async (request) => {
const { pathname } = new URL(request.url)
Expand Down
15 changes: 10 additions & 5 deletions cli/sync.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import * as flags from "../deps/std/flags.ts"
import { blue, gray } from "../deps/std/fmt/colors.ts"
import { assertEquals } from "../deps/std/testing/asserts.ts"
import { createTempDir, syncConfig } from "../nets/mod.ts"
import { sync } from "../server/mod.ts"
import { normalizePackageName } from "../util/mod.ts"
import { tempDir } from "../util/tempDir.ts"
import { resolveConfig } from "./resolveConfig.ts"

export default async function(...args: string[]) {
const {
"import-map": importMapFile,
"package-json": packageJsonFile,
check,
out,
server,
} = flags.parse(args, {
string: ["config", "import-map", "package-json", "server"],
string: ["config", "import-map", "out", "package-json", "server"],
boolean: ["check"],
default: { server: "https://capi.dev/" },
default: {
server: "https://capi.dev/",
out: "target/capi",
},
})

const config = await resolveConfig(...args)

const tempDir = await createTempDir()
const devnetTempDir = await tempDir(out, "devnet")

const baseUrl = await syncConfig(tempDir, config, server)
const baseUrl = await sync(server, devnetTempDir, config)

if (importMapFile) {
syncFile(importMapFile, (importMap) => {
Expand Down
3 changes: 1 addition & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import "./deps/shims/register.ts"

import bin from "./cli/bin.ts"
import serve from "./cli/serve.ts"
import sync from "./cli/sync.ts"

const commands: Record<string, (...args: string[]) => void> = { bin, serve, sync }
const commands: Record<string, (...args: string[]) => void> = { serve, sync }

if (Deno.args[0]! in commands) {
commands[Deno.args[0]!]!(...Deno.args.slice(1))
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * as $ from "./deps/scale.ts"
export { BitSequence } from "./deps/scale.ts"
export * from "./util/autobin.ts"

// moderate --exclude main.ts nets.ts server util

Expand Down
33 changes: 18 additions & 15 deletions nets.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import { binary, net } from "./mod.ts"
import { autobin, net } from "./mod.ts"

const polkadotBin = binary("polkadot", "v0.9.38")
const polkadotParachainBin = binary("polkadot-parachain", "v0.9.380")
const substrateContractsNodeBin = binary("substrate-contracts-node", "v0.24.0")
const trappistPolkadotBin = binary("polkadot-parachain", "v0.9.370")
const trappistBin = binary("trappist-collator", "79bba6e")
const bin = autobin({
polkadot: ["polkadot", "v0.9.38"],
polkadotParachain: ["polkadot-parachain", "v0.9.380"],
substrateContractsNode: ["substrate-contracts-node", "v0.24.0"],
trappistPolkadot: ["polkadot", "v0.9.37"],
trappistPolkadotParachain: ["polkadot-parachain", "v0.9.370"],
trappist: ["trappist-collator", "79bba6e"],
})

export const polkadot = net.ws("wss://rpc.polkadot.io/", "v0.9.40")
export const westend = net.ws("wss://westend-rpc.polkadot.io/")
export const statemint = net.ws("wss://statemint-rpc.polkadot.io/")

export const polkadotDev = net.dev(polkadotBin, "polkadot-dev")
export const westendDev = net.dev(polkadotBin, "westend-dev")
export const contractsDev = net.dev(substrateContractsNodeBin, "dev")
export const polkadotDev = net.dev(bin.polkadot, "polkadot-dev")
export const westendDev = net.dev(bin.polkadot, "westend-dev")
export const contractsDev = net.dev(bin.substrateContractsNode, "dev")

export const rococoDev = net.dev(polkadotBin, "rococo-local")
export const rococoDevWestmint = rococoDev.parachain(polkadotParachainBin, "westmint-local", 1000)
export const rococoDev = net.dev(bin.polkadot, "rococo-local")
export const rococoDevWestmint = rococoDev.parachain(bin.polkadotParachain, "westmint-local", 1000)
export const rococoDevContracts = rococoDev.parachain(
polkadotParachainBin,
bin.polkadotParachain,
"contracts-rococo-local",
2000,
)

export const rococoDevXcm = net.dev(binary("polkadot", "v0.9.37"), "rococo-local")
export const rococoDevXcm = net.dev(bin.trappistPolkadot, "rococo-local")
export const rococoDevXcmStatemine = rococoDevXcm.parachain(
trappistPolkadotBin,
bin.trappistPolkadotParachain,
"statemine-local",
1000,
)
export const rococoDevXcmTrappist = rococoDevXcm.parachain(trappistBin, "local", 2000)
export const rococoDevXcmTrappist = rococoDevXcm.parachain(bin.trappist, "local", 2000)

export const polkadotFromMetadata = net.metadata(
await Deno.readFile("examples/raw_rpc/metadata"),
Expand Down
43 changes: 20 additions & 23 deletions nets/DevNet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as path from "../deps/std/path.ts"
import { Binary, resolveBinary } from "./binary.ts"
import { createRawChainSpec } from "./chain_spec.ts"
import { createRawChainSpec } from "./common/chain_spec.ts"
import { spawnDevNet } from "./common/spawnDevNet.ts"
import { getMetadataFromWsUrl, Net } from "./Net.ts"
import { spawnDevNet } from "./spawnDevNet.ts"

export abstract class DevNet extends Net {
constructor(readonly binary: Binary, readonly chain: string, readonly nodeCount?: number) {
constructor(readonly binary: BinaryGetter, readonly chain: string, readonly nodeCount?: number) {
super()
}

Expand All @@ -16,55 +15,53 @@ export abstract class DevNet extends Net {
}
}

_binaryPath?: Promise<string>
binaryPath(signal: AbortSignal) {
if (!this._binaryPath) this._binaryPath = resolveBinary(this.binary, signal)
return this._binaryPath
}

_tempDir?: string
tempDir(parentDir: string) {
if (!this._tempDir) this._tempDir = path.join(parentDir, crypto.randomUUID())
return this._tempDir
}

_rawChainSpecPath?: Promise<string>
rawChainSpecPath(signal: AbortSignal, tempParentDir: string) {
rawChainSpecPath(signal: AbortSignal, devnetTempDir: string) {
if (!this._rawChainSpecPath) {
this._rawChainSpecPath = (async () => {
const binary = await this.binaryPath(signal)
return createRawChainSpec(this.tempDir(tempParentDir), binary, this.chain)
return createRawChainSpec(
this.tempDir(devnetTempDir),
await this.binary(signal),
this.chain,
)
})()
}
return this._rawChainSpecPath
}

async preflightNetwork(signal: AbortSignal, tempParentDir: string) {
const [binaryPath, chainSpecPath, extraArgs] = await Promise.all([
this.binaryPath(signal),
this.rawChainSpecPath(signal, tempParentDir),
this.preflightNetworkArgs(signal, tempParentDir),
async preflightNetwork(signal: AbortSignal, devnetTempDir: string) {
const [chainSpecPath, extraArgs] = await Promise.all([
this.rawChainSpecPath(signal, devnetTempDir),
this.preflightNetworkArgs(signal, devnetTempDir),
])
return spawnDevNet({
tempDir: this.tempDir(tempParentDir),
binaryPath,
tempDir: this.tempDir(devnetTempDir),
binary: await this.binary(signal),
chainSpecPath,
nodeCount: 1,
extraArgs,
signal,
})
}

abstract preflightNetworkArgs(signal: AbortSignal, tempParentDir: string): Promise<string[]>
abstract preflightNetworkArgs(signal: AbortSignal, devnetTempDir: string): Promise<string[]>

_metadata?: Promise<Uint8Array>
metadata(signal: AbortSignal, tempParentDir: string) {
metadata(signal: AbortSignal, devnetTempDir: string) {
if (!this._metadata) {
this._metadata = (async () => {
const { ports: [port0] } = await this.preflightNetwork(signal, tempParentDir)
const { ports: [port0] } = await this.preflightNetwork(signal, devnetTempDir)
return getMetadataFromWsUrl(`ws://127.0.0.1:${port0}`)
})()
}
return this._metadata
}
}

export type BinaryGetter = (signal: AbortSignal) => Promise<string>
8 changes: 4 additions & 4 deletions nets/Net.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { hex } from "../crypto/mod.ts"
import { WsConnection } from "../rpc/mod.ts"
import { ConnectionV0 } from "../server/codegenSpec.ts"
import { ConnectionV0 } from "../server/mod.ts"
import { withSignal } from "../util/mod.ts"
import { SpawnDevNetResult } from "./spawnDevNet.ts"
import { SpawnDevNetResult } from "./common/spawnDevNet.ts"

export abstract class Net {
abstract connection(name: string): ConnectionV0 | undefined
abstract metadata(signal: AbortSignal, tempParentDir: string): Promise<Uint8Array>
spawn?: (signal: AbortSignal, tempParentDir: string) => Promise<SpawnDevNetResult>
abstract metadata(signal: AbortSignal, devnetTempDir: string): Promise<Uint8Array>
spawn?: (signal: AbortSignal, devnetTempDir: string) => Promise<SpawnDevNetResult>
}

export function getMetadataFromWsUrl(url: string) {
Expand Down
26 changes: 14 additions & 12 deletions nets/Parachain.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Binary } from "./binary.ts"
import { createCustomChainSpec, exportParachainGenesis, getGenesisConfig } from "./chain_spec.ts"
import { addDevUsers } from "./dev_users.ts"
import { DevNet } from "./DevNet.ts"
import {
createCustomChainSpec,
exportParachainGenesis,
getGenesisConfig,
} from "./common/chain_spec.ts"
import { addDevUsers } from "./common/dev_users.ts"
import { spawnDevNet, SpawnDevNetResult } from "./common/spawnDevNet.ts"
import { BinaryGetter, DevNet } from "./DevNet.ts"
import { RelayChain } from "./RelayChain.ts"
import { spawnDevNet, SpawnDevNetResult } from "./spawnDevNet.ts"

export class Parachain extends DevNet {
constructor(
readonly relayChain: RelayChain,
binary: Binary,
binary: BinaryGetter,
chain: string,
readonly id: number,
nodeCount: number = 2,
Expand All @@ -27,10 +30,10 @@ export class Parachain extends DevNet {
if (!this._parachainInfo) {
this._parachainInfo = (async () => {
const tempDir = this.tempDir(tempParentDir)
const binaryPath = await this.binaryPath(signal)
const binary = await this.binary(signal)
const chainSpecPath = await createCustomChainSpec(
tempDir,
binaryPath,
binary,
this.chain,
(chainSpec) => {
chainSpec.para_id = this.id
Expand All @@ -39,7 +42,7 @@ export class Parachain extends DevNet {
addDevUsers(genesisConfig.balances.balances)
},
)
const genesis = await exportParachainGenesis(binaryPath, chainSpecPath, signal)
const genesis = await exportParachainGenesis(binary, chainSpecPath, signal)
return { id: this.id, chainSpecPath, genesis }
})()
}
Expand All @@ -54,16 +57,15 @@ export class Parachain extends DevNet {
if (!this._network) {
const tempDir = this.tempDir(tempParentDir)
this._network = (async () => {
const [binaryPath, { chainSpecPath }, relayChainSpecPath, relayChain] = await Promise.all([
this.binaryPath(signal),
const [{ chainSpecPath }, relayChainSpecPath, relayChain] = await Promise.all([
this.parachainInfo(signal, tempParentDir),
this.relayChain.chainSpecPath(signal, tempParentDir),
this.relayChain.spawn(signal, tempParentDir),
])
const nodeCount = this.nodeCount ?? 2
return spawnDevNet({
tempDir,
binaryPath,
binary: await this.binary(signal),
chainSpecPath,
nodeCount,
extraArgs: [
Expand Down
Loading

0 comments on commit e2e2a76

Please sign in to comment.