Skip to content

Commit

Permalink
add rpc plugin schema check (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii authored Nov 10, 2023
1 parent 3488064 commit 5ef86e8
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
4 changes: 1 addition & 3 deletions packages/chopsticks/src/plugins/dry-run/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { HexString } from '@polkadot/util/types'
import { z } from 'zod'

import { Context, ResponseError } from '@acala-network/chopsticks-core'
import { decodeStorageDiff } from '../../utils/decoder.js'
import { generateHtmlDiff } from '../../utils/generate-html-diff.js'
import { zHash, zHex } from '../../schema/index.js'

const zHex = z.custom<HexString>((val: any) => /^0x\w+$/.test(val))
const zHash = z.string().length(66).and(zHex)
const zParaId = z.string().regex(/^\d+$/).transform(Number)

const schema = z.object({
Expand Down
62 changes: 47 additions & 15 deletions packages/chopsticks/src/plugins/new-block/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
import { Context, DownwardMessage, HorizontalMessage, ResponseError } from '@acala-network/chopsticks-core'
import { HexString } from '@polkadot/util/types'
import { Context, ResponseError } from '@acala-network/chopsticks-core'
import { z } from 'zod'

import { defaultLogger } from '../../logger.js'
import { zHex } from '../../schema/index.js'

const schema = z.object({
count: z.number().optional(),
to: z.number().optional(),
dmp: z
.array(
z.object({
sentAt: z.number(),
msg: zHex,
}),
)
.min(1)
.optional(),
ump: z.record(z.number(), z.array(zHex).min(1)).optional(),
hrmp: z
.record(
z.number(),
z
.array(
z.object({
sentAt: z.number(),
data: zHex,
}),
)
.min(1),
)
.optional(),
transactions: z.array(zHex).min(1).optional(),
unsafeBlockHeight: z.number().optional(),
})

type Params = z.infer<typeof schema>

export interface NewBlockParams {
/**
* The number of blocks to build
*/
count: number
count: Params['count']
/**
* The block number to build to
*/
to: number
to: Params['to']
/**
* The downward messages to include in the block
*/
dmp: DownwardMessage[]
dmp: Params['dmp']
/**
* The upward messages to include in the block
*/
ump: Record<number, HexString[]>
ump: Params['ump']
/**
* The horizontal messages to include in the block
*/
hrmp: Record<number, HorizontalMessage[]>
hrmp: Params['hrmp']
/**
* The transactions to include in the block
*/
transactions: HexString[]
transactions: Params['transactions']
/**
* Build block using a specific block height (unsafe)
*/
unsafeBlockHeight: number
unsafeBlockHeight: Params['unsafeBlockHeight']
}

/**
Expand Down Expand Up @@ -71,16 +105,14 @@ export interface NewBlockParams {
* await ws.send('dev_newBlock', [{ count: 2, unsafeBlockHeight: 100000001 }])
* ```
*/
export const rpc = async (context: Context, params: [NewBlockParams]) => {
const [param] = params
const { count, to, hrmp, ump, dmp, transactions, unsafeBlockHeight } = param || {}
export const rpc = async (context: Context, [params]: [NewBlockParams]) => {
const { count, to, hrmp, ump, dmp, transactions, unsafeBlockHeight } = schema.parse(params || {})
const now = context.chain.head.number
const diff = to ? to - now : count
const finalCount = diff > 0 ? diff : 1
const finalCount = diff !== undefined ? Math.max(diff, 1) : 1

let finalHash: string | undefined

if (unsafeBlockHeight < now) {
if (unsafeBlockHeight !== undefined && unsafeBlockHeight <= now) {
throw new ResponseError(1, 'unsafeBlockHeight must be greater than current block height')
}

Expand Down
6 changes: 3 additions & 3 deletions packages/chopsticks/src/plugins/run-block/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {
runTask,
taskHandler,
} from '@acala-network/chopsticks-core'

const zHex = z.custom<HexString>((val: any) => /^0x\w+$/.test(val))
const zHash = z.string().length(66).and(zHex)
import { zHash, zHex } from '../../schema/index.js'

const schema = z.object({
includeRaw: z.boolean().optional(),
Expand Down Expand Up @@ -116,6 +114,8 @@ export interface RunBlockResponse {
}
}

export const name = 'runBlock'

/**
* Run a set of extrinsics on top of a block and get the storage diff
* and optionally the parsed storage diff and block details.
Expand Down
4 changes: 3 additions & 1 deletion packages/chopsticks/src/plugins/set-block-build-mode/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { defaultLogger } from '../../logger.js'
/**
* Set a build block mode. See [BuildBlockMode](../core/enums/BuildBlockMode).
*
* 1 - Batch, 2 - Instant, 3 - Manual
*
* This function is a dev rpc handler. Use `dev_setBlockBuildMode` as the method name when calling it.
*
* @param context - The context object of the rpc handler
Expand All @@ -18,7 +20,7 @@ import { defaultLogger } from '../../logger.js'
* ```
*/
export const rpc = async (context: Context, [mode]: [BuildBlockMode]) => {
defaultLogger.debug({ mode }, 'dev_setBlockBuildMode')
defaultLogger.debug({ mode: BuildBlockMode[mode] }, 'dev_setBlockBuildMode')

if (BuildBlockMode[mode] === undefined) {
throw new ResponseError(1, `Invalid mode ${mode}`)
Expand Down
10 changes: 8 additions & 2 deletions packages/chopsticks/src/plugins/set-head/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Block, Context, ResponseError } from '@acala-network/chopsticks-core'
import { HexString } from '@polkadot/util/types'
import { z } from 'zod'

import { zHash } from '../../schema/index.js'

const schema = zHash.or(z.number())
type Params = z.infer<typeof schema>

/**
* Set head.
Expand All @@ -16,7 +21,8 @@ import { HexString } from '@polkadot/util/types'
* await ws.send('dev_setHead', [1000000])
* ```
*/
export const rpc = async (context: Context, [hashOrNumber]: [HexString | number]) => {
export const rpc = async (context: Context, [params]: [Params]) => {
const hashOrNumber = schema.parse(params)
let block: Block | undefined
if (typeof hashOrNumber === 'number') {
const blockNumber = hashOrNumber > 0 ? hashOrNumber : context.chain.head.number + hashOrNumber
Expand Down
4 changes: 4 additions & 0 deletions packages/chopsticks/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { BuildBlockMode, defaultLogger, genesisSchema, isUrl } from '@acala-network/chopsticks-core'
import { HexString } from '@polkadot/util/types'
import { basename, extname } from 'node:path'
import { readFileSync } from 'node:fs'
import { z } from 'zod'
import _ from 'lodash'
import axios from 'axios'
import yaml from 'js-yaml'

export const zHex = z.custom<HexString>((val: any) => /^0x\w+$/.test(val))
export const zHash = z.string().length(66).and(zHex)

export const configSchema = z
.object({
port: z.number().optional(),
Expand Down

0 comments on commit 5ef86e8

Please sign in to comment.