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 result format #495

Merged
merged 3 commits into from
Oct 31, 2023
Merged
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
57 changes: 35 additions & 22 deletions packages/chopsticks/src/plugins/run-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import { z } from 'zod'
import _ from 'lodash'
import type yargs from 'yargs'

import { Block, Context, decodeKeyValue, printRuntimeLogs, runTask, taskHandler } from '@acala-network/chopsticks-core'
import {
Block,
Context,
compactHex,
decodeKeyValue,
printRuntimeLogs,
runTask,
taskHandler,
} from '@acala-network/chopsticks-core'

import { Config } from '../../schema'
import { defaultOptions, mockOptions } from '../../cli-options'
Expand Down Expand Up @@ -139,13 +147,18 @@ export interface RunBlockResponse {
*/
phase: Phase
/**
* Parsed storage diff. Only available when `includeParsed` is true.
* The modified storages of this phase.
*/
parsed?: Record<string, Record<string, any>>
/**
* Raw storage diff. Only available when `includeRaw` is true.
*/
raw?: [HexString, HexString | null][]
storageDiff: {
/**
* Raw storage diff in bytes. Only available when `includeRaw` is true.
*/
raw?: { key: HexString; value: HexString | null }
/**
* Decoded storage diff. Only available when `includeParsed` is true.
*/
parsed?: any
}[]
/**
* Runtime logs.
*/
Expand Down Expand Up @@ -198,6 +211,7 @@ export const rpc = async ({ chain }: Context, [params]: [RunBlockParams]): Promi
const header = registry.createType<Header>('Header', block.header)

const wasm = await parentBlock.wasm
const meta = await parentBlock.meta

const blockNumber = parentBlock.number + 1
const hash: HexString = `0x${Math.round(Math.random() * 100000000)
Expand All @@ -214,6 +228,9 @@ export const rpc = async ({ chain }: Context, [params]: [RunBlockParams]): Promi
phases: [],
} as RunBlockResponse

// exclude system events because it can be stupidly large and redudant
const systemEventsKey = compactHex(meta.query.system.events())

const run = async (fn: string, args: HexString[]) => {
const result = await runTask(
{
Expand All @@ -230,28 +247,24 @@ export const rpc = async ({ chain }: Context, [params]: [RunBlockParams]): Promi
throw new Error(result.Error)
}

const resp = {} as any
const resp = { storageDiff: [] } as Omit<RunBlockResponse['phases'][number], 'phase'>
const raw = result.Call.storageDiff

newBlock.pushStorageLayer().setAll(raw)

if (includeRawStorage) {
resp.raw = raw
}

if (includeParsed) {
const meta = await newBlock.meta
const parsed = {}
for (const [key, value] of raw) {
_.merge(parsed, decodeKeyValue(meta, newBlock, key, value, false))
for (const [key, value] of raw) {
if (key === systemEventsKey) {
continue
}

// clear events because it can be stupidly large and redudant
if (parsed['system']?.['events']) {
delete parsed['system']['events']
const obj = {} as (typeof resp)['storageDiff'][number]
if (includeRawStorage) {
obj.raw = { key, value }
}

resp.parsed = parsed
if (includeParsed) {
obj.parsed = decodeKeyValue(await newBlock.meta, newBlock, key, value, false)
}
resp.storageDiff.push(obj)
}

resp.logs = result.Call.runtimeLogs
Expand Down