Skip to content

Commit

Permalink
accept config from config file (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc authored Nov 4, 2022
1 parent 3a10775 commit 4030860
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
2 changes: 2 additions & 0 deletions karura-rococo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
endpoint: wss://karura-rococo-rpc.aca-staging.network/ws
mock-signature-host: true
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "vitest --silent",
"test:native": "EXECUTOR_CMD='cargo run --manifest-path executor/Cargo.toml --' vitest",
"test:dev": "LOG_LEVEL=trace vitest --inspect | pino-pretty",
"dev:base": "LOG_LEVEL=trace node-dev --inspect --notify=false src/index.ts -- dev --mock-signature-host=true --endpoint=wss://karura-rococo-rpc.aca-staging.network/ws",
"dev:base": "LOG_LEVEL=trace node-dev --inspect --notify=false src/index.ts -- dev --config=karura-rococo.yml",
"dev": "yarn dev:base | pino-pretty",
"dev:native": "yarn dev:base --executor-cmd='cargo run --manifest-path executor/Cargo.toml --' | pino-pretty"
},
Expand All @@ -27,7 +27,8 @@
"lodash": "^4.17.21",
"pino": "^8.6.1",
"ws": "^8.9.0",
"yargs": "^17.6.0"
"yargs": "^17.6.0",
"zod": "^3.19.1"
},
"devDependencies": {
"@types/js-yaml": "^4",
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain/txpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { defaultLogger } from '../logger'

const logger = defaultLogger.child({ name: 'txpool' })

export const enum BuildBlockMode {
export enum BuildBlockMode {
Batch, // one block per batch, default
Instant, // one block per tx
Manual, // only build when triggered
Expand Down
40 changes: 35 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { hideBin } from 'yargs/helpers'
import { writeFileSync } from 'fs'
import { readFileSync, writeFileSync } from 'fs'
import { z } from 'zod'
import yaml from 'js-yaml'
import yargs from 'yargs'

import { Blockchain } from './blockchain'
Expand Down Expand Up @@ -84,6 +86,28 @@ const runBlock = async (argv: any) => {
setTimeout(() => process.exit(0), 50)
}

const configSchema = z
.object({
port: z.number().optional(),
endpoint: z.string().optional(),
block: z.union([z.string(), z.number()]).optional(),
'executor-cmd': z.string().optional(),
'build-block-mode': z.nativeEnum(BuildBlockMode),
'import-storage': z.string().optional(),
'mock-signature-host': z.boolean().optional(),
})
.strict()

const processConfig = (argv: any) => {
if (argv.config) {
const configFile = readFileSync(argv.config, 'utf8')
const config = yaml.load(configFile) as any
const parsed = configSchema.parse(config)
return { ...parsed, ...argv }
}
return argv
}

yargs(hideBin(process.argv))
.command(
'run-block',
Expand All @@ -97,7 +121,6 @@ yargs(hideBin(process.argv))
endpoint: {
desc: 'Endpoint to connect to',
string: true,
require: true,
},
block: {
desc: 'Block hash or block number. Default to latest block',
Expand All @@ -111,9 +134,13 @@ yargs(hideBin(process.argv))
desc: 'File path to print output',
string: true,
},
config: {
desc: 'Path to config file',
string: true,
},
}),
(argv) => {
runBlock(argv).catch((err) => {
runBlock(processConfig(argv)).catch((err) => {
console.error(err)
process.exit(1)
})
Expand All @@ -131,7 +158,6 @@ yargs(hideBin(process.argv))
endpoint: {
desc: 'Endpoint to connect to',
string: true,
require: true,
},
block: {
desc: 'Block hash or block number. Default to latest block',
Expand All @@ -153,9 +179,13 @@ yargs(hideBin(process.argv))
desc: 'Mock signature host so any signature starts with 0xdeadbeef and filled by 0xcd is considered valid',
boolean: true,
},
config: {
desc: 'Path to config file',
string: true,
},
}),
(argv) => {
setup(argv).catch((err) => {
setup(processConfig(argv)).catch((err) => {
console.error(err)
process.exit(1)
})
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ __metadata:
wasm-pack: ^0.10.3
ws: ^8.9.0
yargs: ^17.6.0
zod: ^3.19.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -4950,3 +4951,10 @@ __metadata:
checksum: f77b3d8d00310def622123df93d4ee654fc6a0096182af8bd60679ddcdfb3474c56c6c7190817c84a2785648cdee9d721c0154eb45698c62176c322fb46fc700
languageName: node
linkType: hard

"zod@npm:^3.19.1":
version: 3.19.1
resolution: "zod@npm:3.19.1"
checksum: 56e420ea5845912324a8fc61833714a2aec84954e418b52660d76502183c6e62fef9447cbfa64349640c5ce190cf2c24267e006bb80f066183e2f3fa9fe11864
languageName: node
linkType: hard

0 comments on commit 4030860

Please sign in to comment.