Skip to content

Commit

Permalink
update run block result format (#495)
Browse files Browse the repository at this point in the history
* update the format so it is easier to use

* remove console.log
  • Loading branch information
xlc committed Oct 31, 2023
1 parent 6191b00 commit ab3dd00
Showing 1 changed file with 35 additions and 22 deletions.
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

0 comments on commit ab3dd00

Please sign in to comment.