-
Notifications
You must be signed in to change notification settings - Fork 80
/
cli.ts
86 lines (74 loc) · 2.4 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { HexString } from '@polkadot/util/types'
import { writeFileSync } from 'node:fs'
import { z } from 'zod'
import _ from 'lodash'
import type { Argv } from 'yargs'
import { runTask, taskHandler } from '@acala-network/chopsticks-core'
import { configSchema, getYargsOptions } from '../../schema/index.js'
import { generateHtmlDiffPreviewFile } from '../../utils/generate-html-diff.js'
import { openHtml } from '../../utils/open-html.js'
import { setupContext } from '../../context.js'
const schema = z.object({
...configSchema.shape,
['output-path']: z
.string({
description: 'File path to print output',
})
.optional(),
html: z
.boolean({
description: 'Generate html with storage diff',
})
.optional(),
open: z
.boolean({
description: 'Open generated html',
})
.optional(),
})
export const cli = (y: Argv) => {
y.command(
'run-block',
'Replay a block',
(yargs) => yargs.options(getYargsOptions(schema.shape)),
async (argv) => {
const parsedArgv = schema.parse(argv)
const context = await setupContext(parsedArgv, true)
const header = await context.chain.head.header
const block = context.chain.head
const parent = await block.parentBlock
if (!parent) throw Error('cant find parent block')
const wasm = await parent.wasm
const calls: [string, HexString[]][] = [['Core_initialize_block', [header.toHex()]]]
for (const extrinsic of await block.extrinsics) {
calls.push(['BlockBuilder_apply_extrinsic', [extrinsic]])
}
calls.push(['BlockBuilder_finalize_block', []])
const result = await runTask(
{
wasm,
calls,
mockSignatureHost: false,
allowUnresolvedImports: false,
runtimeLogLevel: parsedArgv['runtime-log-level'] || 0,
},
taskHandler(parent),
)
if ('Error' in result) {
throw new Error(result.Error)
}
if (argv.html) {
const filePath = await generateHtmlDiffPreviewFile(parent, result.Call.storageDiff, block.hash)
console.log(`Generated preview ${filePath}`)
if (argv.open) {
openHtml(filePath)
}
} else if (argv.outputPath) {
writeFileSync(argv.outputPath as string, JSON.stringify(result, null, 2))
} else {
console.dir(result, { depth: null, colors: false })
}
process.exit(0)
},
)
}