Skip to content

Commit

Permalink
try-runtime cli (#775)
Browse files Browse the repository at this point in the history
* fix try-runtime cli

* add docs
  • Loading branch information
ermalkaleci authored Jun 5, 2024
1 parent ea65882 commit d3f813c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 31 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,6 @@ The subcommand arguments could be:

Please note that for both ways, fetched storages will be saved in the sqlite file specified by `--db` option (`db: ./acala.sqlite` in a config file), if not provided, it will default to `./db-{network}-{block}.sqlite`.

## Try-Runtime CLI

Documentation can be found [here](packages/chopsticks/src/plugins/try-runtime/README.md)
4 changes: 2 additions & 2 deletions executor/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
}
}

RuntimeCall::ClosestDescendantMerkleValue(_req) => {
unreachable!()
RuntimeCall::ClosestDescendantMerkleValue(req) => {
req.resume_unknown()
}

RuntimeCall::NextKey(req) => {
Expand Down
21 changes: 21 additions & 0 deletions packages/chopsticks/src/plugins/try-runtime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Try-Runtime CLI

🚧 EXPERIMENTAL FEATURE 🚧

You can use Chopsticks to perform runtime migration checks. It doesn't support PoV measure yet, only weight check is support.

```bash
# try-runtime print help
npx @acala-network/chopsticks try-runtime --help
```

Basic example:

```bash
npx @acala-network/chopsticks try-runtime \
--endpoint <wss://remote.endpoint> \
--runtime <wasm_runtime_path> \
--checks PreAndPost
```

__NOTE__: You can also use `--config` to pass arguments
87 changes: 58 additions & 29 deletions packages/chopsticks/src/plugins/try-runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,98 @@
import { BuildBlockMode } from '@acala-network/chopsticks-core'
import { writeFileSync } from 'node:fs'
import { z } from 'zod'
import type { Argv } from 'yargs'

import { configSchema, getYargsOptions } from '../../schema/index.js'
import { generateHtmlDiffPreviewFile } from '../../utils/generate-html-diff.js'
import { openHtml } from '../../utils/open-html.js'
import { overrideWasm } from '../../utils/override.js'
import { setupContext } from '../../context.js'

const schema = z.object({
endpoint: configSchema.shape.endpoint,
port: configSchema.shape.port,
['build-block-mode']: configSchema.shape['build-block-mode'],
block: configSchema.shape.block,
db: configSchema.shape.db,
['runtime-log-level']: configSchema.shape['runtime-log-level'],
['wasm-override']: z.string({
['runtime-log-level']: configSchema.shape['runtime-log-level'].default(5),
['runtime']: z.string({
description: 'Path to WASM built with feature `try-runtime` enabled',
}),
'import-storage': configSchema.shape['import-storage'],
checks: z.enum(['None', 'All', 'PreAndPost', 'TryState']),
'disable-spec-check': z.boolean({ description: 'Disable spec name/version check' }).optional(),
['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(
'try-runtime',
'Runs runtime upgrade',
'🚧 EXPERIMENTAL: Check upgrade migrations 🚧',
(yargs) => yargs.options(getYargsOptions(schema.shape)),
async (argv) => {
const context = await setupContext(schema.parse(argv))
console.log('🚧 EXPERIMENTAL FEATURE 🚧')

const config = schema.parse(argv)
if (!config.db) {
console.log('⚠️ Make sure to provide db, it will speed up the process')
}
const context = await setupContext({ ...config, port: 8000, 'build-block-mode': BuildBlockMode.Manual })
const block = context.chain.head
const registry = await block.registry
registry.register({
UpgradeCheckSelect: {
_enum: {
None: null,
All: null,
PreAndPost: null,
TryState: null,
},
},
})

const select_none = registry.createType('UpgradeCheckSelect', 'None')
const result = await block.call('TryRuntime_on_runtime_upgrade', [select_none.toHex()])
const oldVersion = await block.runtimeVersion
// set new runtime
await overrideWasm(block.chain, config.runtime)
const newVersion = await block.runtimeVersion
console.log('\n')
console.log(new Array(80).fill('-').join(''))
console.log(`\tCurrent runtime spec_name: ${oldVersion.specName}, spec_version: ${oldVersion.specVersion}`)
console.log(`\tNew runtime spec_name: ${newVersion.specName}, spec_version: ${newVersion.specVersion}`)
console.log(new Array(80).fill('-').join(''))
console.log('\n')

if (argv.html) {
const filePath = await generateHtmlDiffPreviewFile(block, result.storageDiff, block.hash)
console.log(`Generated preview ${filePath}`)
if (argv.open) {
openHtml(filePath)
}
} else if (argv.outputPath) {
writeFileSync(argv.outputPath, JSON.stringify(result, null, 2))
if (!config['disable-spec-check'] && oldVersion.specName !== newVersion.specName) {
console.log('❌ Spec name does not match. Use --disable-spec-check to disable this check')
process.exit(1)
}

if (!config['disable-spec-check'] && oldVersion.specVersion >= newVersion.specVersion) {
console.log('❌ Spec version must increase. Use --disable-spec-check to disable this check')
process.exit(1)
}

const select_none = registry.createType('UpgradeCheckSelect', config.checks)
const response = await block.call('TryRuntime_on_runtime_upgrade', [select_none.toHex()])

if (argv.outputPath) {
writeFileSync(argv.outputPath, JSON.stringify(response, null, 2))
} else {
console.dir(result, { depth: null, colors: false })
const [actual, max] = registry.createType('(Weight, Weight)', response.result)
const consumedWeight = actual.refTime.toBn()
const maxWeight = max.refTime.toBn()

console.log('\n🚧 EXPERIMENTAL FEATURE 🚧')
console.log('⚠️ PoV measure is not supported, consider using https://crates.io/crates/try-runtime-cli')

console.log(
`\nConsumed weight: ${consumedWeight.toNumber()} of max: ${maxWeight.toNumber()} ( ${((consumedWeight.toNumber() / maxWeight.toNumber()) * 100).toFixed(2)}% )`,
)

if (consumedWeight.gt(maxWeight)) {
console.log('❌ Weight limit is exceeded ❌')
process.exit(1)
}
}

process.exit(0)
Expand Down

0 comments on commit d3f813c

Please sign in to comment.