Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow load config from url #164

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ vendor/
.pnp.cjs
.pnp.loader.mjs
dist/
preview/
2 changes: 1 addition & 1 deletion executor/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
],
"main": "chopsticks_executor.js",
"types": "chopsticks_executor.d.ts"
}
}
29 changes: 18 additions & 11 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { HexString } from '@polkadot/util/types'
import { hideBin } from 'yargs/helpers'
import { readFileSync } from 'node:fs'
import axios from 'axios'
import yaml from 'js-yaml'
import yargs from 'yargs'

import { Blockchain, BuildBlockMode, connectParachains, connectVertical, setup, setupWithServer } from '.'
import { configSchema } from './schema'
import { decodeKey } from './utils/decoder'
import { dryRun } from './dry-run'
import { isUrl } from './utils'
import { runBlock } from './run-block'

const processConfig = (path: string) => {
const configFile = readFileSync(path, 'utf8')
const config = yaml.load(configFile) as any
const processConfig = async (path: string) => {
let file
if (isUrl(path)) {
file = await axios.get(path).then((x) => x.data)
} else {
file = readFileSync(path, 'utf8')
}
const config = yaml.load(file) as any
return configSchema.parse(config)
}

const processArgv = (argv: any) => {
const processArgv = async (argv: any) => {
if (argv.config) {
return { ...processConfig(argv.config), ...argv }
return { ...(await processConfig(argv.config)), ...argv }
}
return argv
}
Expand Down Expand Up @@ -69,7 +76,7 @@ yargs(hideBin(process.argv))
},
}),
async (argv) => {
await runBlock(processArgv(argv))
await runBlock(await processArgv(argv))
}
)
.command(
Expand Down Expand Up @@ -103,7 +110,7 @@ yargs(hideBin(process.argv))
},
}),
async (argv) => {
await dryRun(processArgv(argv))
await dryRun(await processArgv(argv))
}
)
.command(
Expand Down Expand Up @@ -134,7 +141,7 @@ yargs(hideBin(process.argv))
},
}),
async (argv) => {
await setupWithServer(processArgv(argv))
await setupWithServer(await processArgv(argv))
}
)
.command(
Expand All @@ -150,7 +157,7 @@ yargs(hideBin(process.argv))
...defaultOptions,
}),
async (argv) => {
const context = await setup(processArgv(argv))
const context = await setup(await processArgv(argv))
const { storage, decodedKey } = await decodeKey(context.chain.head, argv.key as HexString)
if (storage && decodedKey) {
console.log(
Expand Down Expand Up @@ -182,7 +189,7 @@ yargs(hideBin(process.argv))
async (argv) => {
const parachains: Blockchain[] = []
for (const config of argv.parachain) {
const { chain } = await setupWithServer(processConfig(config))
const { chain } = await setupWithServer(await processConfig(config))
parachains.push(chain)
}

Expand All @@ -191,7 +198,7 @@ yargs(hideBin(process.argv))
}

if (argv.relaychain) {
const { chain: relaychain } = await setupWithServer(processConfig(argv.relaychain))
const { chain: relaychain } = await setupWithServer(await processConfig(argv.relaychain))
for (const parachain of parachains) {
await connectVertical(relaychain, parachain)
}
Expand Down
11 changes: 2 additions & 9 deletions src/genesis-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import axios from 'axios'

import { Genesis, genesisSchema } from './schema'
import { calculateStateRoot, runTask } from './executor'
import { isUrl } from './utils'

export class GenesisProvider implements ProviderInterface {
#isConnected = false
Expand Down Expand Up @@ -45,16 +46,8 @@ export class GenesisProvider implements ProviderInterface {
}

static fromUrl = async (url: string) => {
let isUrl = false
try {
new URL(url)
isUrl = true
} catch (e) {
void e
}

let file: any
if (isUrl) {
if (isUrl(url)) {
file = await axios.get(url).then((x) => x.data)
} else if (lstatSync(url).isFile()) {
file = JSON.parse(String(readFileSync(url)))
Expand Down
9 changes: 9 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ export const getParaId = async (chain: Blockchain) => {
if (!raw) throw new Error('Cannot find parachain id')
return meta.registry.createType('u32', hexToU8a(raw))
}

export const isUrl = (url: string) => {
try {
new URL(url)
return true
} catch (e) {
return false
}
}