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

move some RPC methods into core #676

Merged
merged 4 commits into from
Mar 3, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"typescript": "^5.3.3",
"unplugin-swc": "^1.4.4",
"vite-tsconfig-paths": "^4.3.1",
"vitepress": "^1.0.0-rc.40",
"vitepress": "^1.0.0-rc.44",
"vitest": "^1.2.1",
"wasm-pack": "^0.12.1"
}
Expand Down
1 change: 0 additions & 1 deletion packages/chopsticks/src/plugins/new-block/index.ts

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion packages/chopsticks/src/plugins/set-head/index.ts

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion packages/chopsticks/src/plugins/set-storage/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/chopsticks/src/plugins/time-travel/index.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/chopsticks/src/plugins/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
export { rpc as newBlock } from './new-block/index.js'
export { rpc as dryRun } from './dry-run/index.js'
export { rpc as setBlockBuildMode } from './set-block-build-mode/index.js'
export { rpc as setHead } from './set-head/index.js'
export { rpc as setRuntimeLogLevel } from './set-runtime-log-level/index.js'
export { rpc as setStorage } from './set-storage/index.js'
export { rpc as timeTravel } from './time-travel/index.js'
export { rpc as runBlock } from './run-block/index.js'

export type { NewBlockParams } from './new-block/index.js'
export type { DryRunParams } from './dry-run/index.js'
export type { RunBlockParams } from './run-block/index.js'
6 changes: 3 additions & 3 deletions packages/chopsticks/src/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import {
Handlers,
ResponseError,
SubscriptionManager,
allHandlers as coreHandlers,
defaultLogger,
substrate,
} from '@acala-network/chopsticks-core'

import { loadRpcPlugin, rpcPluginMethods } from '../plugins/index.js'

const rpcLogger = defaultLogger.child({ name: 'rpc' })

const allHandlers: Handlers = {
...substrate,
...coreHandlers,
rpc_methods: async () =>
Promise.resolve({
version: 1,
methods: [...Object.keys(allHandlers), ...rpcPluginMethods],
methods: [...Object.keys(allHandlers), ...rpcPluginMethods].sort(),
}),
}

Expand Down
1 change: 1 addition & 0 deletions packages/chopsticks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export type {
Handler,
} from '@acala-network/chopsticks-core'
export * from '@acala-network/chopsticks-core/rpc/substrate/index.js'
export * as DevRPC from '@acala-network/chopsticks-core/rpc/dev/index.js'
export * from './plugins/types.js'
6 changes: 1 addition & 5 deletions packages/core/src/chopsticks-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import {
} from '@polkadot/rpc-provider/types'

import { Blockchain } from './blockchain/index.js'
import { Context, Handlers, allHandlers } from './rpc/index.js'
import { Database } from './database.js'
import { Handlers, allHandlers } from './rpc/index.js'
import { defaultLogger } from './logger.js'
import { setup } from './setup.js'

const providerHandlers: Handlers = {
...allHandlers,
dev_newBlock: async (context: Context, _params: any, _subscriptionManager: any) => {
const block = await context.chain.newBlock()
return block.hash
},
}

const logger = defaultLogger.child({ name: '[Chopsticks provider]' })
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/rpc/dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { dev_newBlock } from './new-block.js'
import { dev_setBlockBuildMode } from './set-block-build-mode.js'
import { dev_setHead } from './set-head.js'
import { dev_setRuntimeLogLevel } from './set-runtime-log-level.js'
import { dev_setStorage } from './set-storage.js'
import { dev_timeTravel } from './time-travel.js'

const handlers = {
dev_newBlock,
dev_setBlockBuildMode,
dev_setHead,
dev_setRuntimeLogLevel,
dev_setStorage,
dev_timeTravel,
}

export default handlers

export * from './new-block.js'
export * from './set-block-build-mode.js'
export * from './set-head.js'
export * from './set-runtime-log-level.js'
export * from './set-storage.js'
export * from './time-travel.js'
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context, ResponseError } from '@acala-network/chopsticks-core'
import { Context, ResponseError, zHex } from '../shared.js'
import { HexString } from '@polkadot/util/types'
import { z } from 'zod'

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

const schema = z.object({
count: z.number().optional(),
Expand Down Expand Up @@ -105,13 +105,13 @@ export interface NewBlockParams {
* await ws.send('dev_newBlock', [{ count: 2, unsafeBlockHeight: 100000001 }])
* ```
*/
export const rpc = async (context: Context, [params]: [NewBlockParams]) => {
export const dev_newBlock = 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 !== undefined ? Math.max(diff, 1) : 1

let finalHash: string | undefined
let finalHash: HexString | undefined
if (unsafeBlockHeight !== undefined && unsafeBlockHeight <= now) {
throw new ResponseError(1, 'unsafeBlockHeight must be greater than current block height')
}
Expand All @@ -132,5 +132,5 @@ export const rpc = async (context: Context, [params]: [NewBlockParams]) => {
finalHash = block.hash
}

return finalHash
return finalHash!
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BuildBlockMode, Context, ResponseError } from '@acala-network/chopsticks-core'
import { BuildBlockMode } from '../../blockchain/txpool.js'
import { Context, ResponseError } from '../shared.js'
import { defaultLogger } from '../../logger.js'

/**
Expand All @@ -19,7 +20,7 @@ import { defaultLogger } from '../../logger.js'
* await ws.send('dev_setBlockBuildMode', [BuildBlockMode.Instant])
* ```
*/
export const rpc = async (context: Context, [mode]: [BuildBlockMode]) => {
export const dev_setBlockBuildMode = async (context: Context, [mode]: [BuildBlockMode]) => {
defaultLogger.debug({ mode: BuildBlockMode[mode] }, 'dev_setBlockBuildMode')

if (BuildBlockMode[mode] === undefined) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Block, Context, ResponseError } from '@acala-network/chopsticks-core'
import { Block } from '../../blockchain/block.js'
import { Context, ResponseError, zHash } from '../shared.js'
import { z } from 'zod'

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

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

Expand All @@ -21,7 +20,7 @@ type Params = z.infer<typeof schema>
* await ws.send('dev_setHead', [1000000])
* ```
*/
export const rpc = async (context: Context, [params]: [Params]) => {
export const dev_setHead = async (context: Context, [params]: [Params]) => {
const hashOrNumber = schema.parse(params)
let block: Block | undefined
if (typeof hashOrNumber === 'number') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, ResponseError } from '@acala-network/chopsticks-core'
import { Context, ResponseError } from '../shared.js'
import { defaultLogger } from '../../logger.js'

/**
Expand All @@ -16,7 +16,7 @@ import { defaultLogger } from '../../logger.js'
* await ws.send('dev_setRuntimeLogLevel', [1])
* ```
*/
export const rpc = async (context: Context, [runtimeLogLevel]: [number]) => {
export const dev_setRuntimeLogLevel = async (context: Context, [runtimeLogLevel]: [number]) => {
defaultLogger.debug({ runtimeLogLevel }, 'dev_setRuntimeLogLevel')

if (typeof runtimeLogLevel !== 'number') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context, ResponseError, StorageValues, setStorage } from '@acala-network/chopsticks-core'
import { Context, ResponseError } from '../shared.js'
import { HexString } from '@polkadot/util/types'

import { StorageValues, setStorage } from '../../utils/set-storage.js'
import { defaultLogger } from '../../logger.js'

/**
Expand Down Expand Up @@ -29,7 +30,7 @@ import { defaultLogger } from '../../logger.js'
* ```
*/

export const rpc = async (context: Context, params: [StorageValues, HexString?]) => {
export const dev_setStorage = async (context: Context, params: [StorageValues, HexString?]) => {
const [values, blockHash] = params
const hash = await setStorage(context.chain, values, blockHash).catch((error) => {
throw new ResponseError(1, error.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, ResponseError, timeTravel } from '@acala-network/chopsticks-core'
import { Context, ResponseError } from '../shared.js'
import { timeTravel } from '../../utils/time-travel.js'

/**
* Travel to a specific time.
Expand All @@ -15,7 +16,7 @@ import { Context, ResponseError, timeTravel } from '@acala-network/chopsticks-co
* await ws.send('dev_timeTravel', ['Jan 1, 2023'])
* ```
*/
export const rpc = async (context: Context, [date]: [string | number]) => {
export const dev_timeTravel = async (context: Context, [date]: [string | number]) => {
const timestamp = typeof date === 'string' ? Date.parse(date) : date
if (Number.isNaN(timestamp)) throw new ResponseError(1, 'Invalid date')
await timeTravel(context.chain, timestamp)
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/rpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Handlers } from './shared.js'
import dev from './dev/index.js'
import substrate from './substrate/index.js'

export const allHandlers: Handlers = {
...substrate,
...dev,
rpc_methods: async () =>
Promise.resolve({
version: 1,
methods: [...Object.keys(allHandlers)],
methods: Object.keys(allHandlers).sort(),
}),
}

export { default as substrate } from './substrate/index.js'
export { substrate, dev }
export { ResponseError } from './shared.js'
export type { Context, SubscriptionManager, Handler, Handlers } from './shared.js'
6 changes: 6 additions & 0 deletions packages/core/src/rpc/shared.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { HexString } from '@polkadot/util/types'
import { z } from 'zod'

import { Blockchain } from '../blockchain/index.js'
import { defaultLogger } from '../logger.js'

export const logger = defaultLogger.child({ name: 'rpc' })

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

export class ResponseError extends Error {
code: number

Expand Down
Loading
Loading