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

update run-block command #240

Merged
merged 1 commit into from
Mar 9, 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
8 changes: 4 additions & 4 deletions packages/chopsticks/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ yargs(hideBin(process.argv))
(yargs) =>
yargs.options({
...defaultOptions,
port: {
desc: 'Port to listen on',
number: true,
},
'output-path': {
desc: 'File path to print output',
string: true,
},
'import-storage': {
desc: 'Pre-defined JSON/YAML storage file path',
string: true,
},
html: {
desc: 'Generate html with storage diff',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/chopsticks/src/run-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { runTask, taskHandler } from './executor'
import { setup } from './setup'

export const runBlock = async (argv: Config) => {
const context = await setup(argv)
const context = await setup(argv, true)

const header = await context.chain.head.header
const wasm = await context.chain.head.wasm
Expand Down
14 changes: 11 additions & 3 deletions packages/chopsticks/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { importStorage, overrideWasm } from './utils/import-storage'
import { openDb } from './db'
import { timeTravel } from './utils/time-travel'

export const setup = async (argv: Config) => {
export const setup = async (argv: Config, runBlock = false) => {
let provider: ProviderInterface
if (argv.genesis) {
if (typeof argv.genesis === 'string') {
Expand Down Expand Up @@ -78,10 +78,18 @@ export const setup = async (argv: Config) => {

if (argv.timestamp) await timeTravel(chain, argv.timestamp)

let at: HexString | undefined
if (runBlock) {
// in case of run block we need to apply wasm-override and import-storage to parent block
const block = await chain.head.parentBlock
if (!block) throw new Error('Cannot find parent block')
at = block.hash
}

// override wasm before importing storage, in case new pallets have been
// added that have storage imports
await overrideWasm(chain, argv['wasm-override'])
await importStorage(chain, argv['import-storage'])
await overrideWasm(chain, argv['wasm-override'], at)
await importStorage(chain, argv['import-storage'], at)

return { chain, api, ws: provider }
}
14 changes: 10 additions & 4 deletions packages/chopsticks/src/utils/import-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Blockchain } from '../blockchain'
import { StorageValues, setStorage } from './set-storage'
import { defaultLogger } from '../logger'

export const importStorage = async (chain: Blockchain, storage?: string | StorageValues) => {
export const importStorage = async (chain: Blockchain, storage?: string | StorageValues, at?: HexString) => {
if (storage == null) {
return
}
Expand All @@ -17,11 +17,11 @@ export const importStorage = async (chain: Blockchain, storage?: string | Storag
} else {
storageValue = storage
}
const blockHash = await setStorage(chain, storageValue)
const blockHash = await setStorage(chain, storageValue, at)
defaultLogger.trace({ blockHash, storage }, 'ImportStorage')
}

export const overrideWasm = async (chain: Blockchain, wasmPath?: string) => {
export const overrideWasm = async (chain: Blockchain, wasmPath?: string, at?: HexString) => {
if (wasmPath == null) {
return
}
Expand All @@ -33,5 +33,11 @@ export const overrideWasm = async (chain: Blockchain, wasmPath?: string) => {
} else {
wasmHex = '0x' + wasm.toString('hex')
}
chain.head.setWasm(wasmHex as HexString)
if (at) {
const block = await chain.getBlock(at)
if (!block) throw new Error(`Cannot find block ${at}`)
block.setWasm(wasmHex as HexString)
} else {
chain.head.setWasm(wasmHex as HexString)
}
}