Skip to content

Commit

Permalink
fix try-runtime cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Jun 3, 2024
1 parent b8e9f44 commit 5e10a8c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 35 deletions.
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
2 changes: 1 addition & 1 deletion packages/chopsticks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@
"default": "./dist/esm/utils/*.js"
}
}
}
}
85 changes: 57 additions & 28 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']: 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
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@
"./dist/cjs/wasm-executor/node-worker.js": "./dist/cjs/wasm-executor/browser-worker.js",
"./dist/esm/wasm-executor/node-worker.js": "./dist/esm/wasm-executor/browser-worker.js"
}
}
}
2 changes: 1 addition & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
"default": "./dist/esm/*.js"
}
}
}
}
2 changes: 1 addition & 1 deletion packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"default": "./dist/esm/*.js"
}
}
}
}
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
"default": "./dist/esm/*.js"
}
}
}
}

0 comments on commit 5e10a8c

Please sign in to comment.