From 5fe97fc980b55daf8a9a0366787a58ae7e6ba95d Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Tue, 5 Sep 2023 14:13:15 +0800 Subject: [PATCH 1/4] fix runtime log level setting --- packages/core/src/setup.ts | 4 ++++ packages/core/src/utils/index.ts | 1 + packages/core/src/utils/log-level.ts | 23 +++++++++++++++++++++++ packages/e2e/src/networks.ts | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/utils/log-level.ts diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index a202b623..ef3835e3 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -19,6 +19,7 @@ import { SetValidationData, } from './blockchain/inherent' import { defaultLogger } from './logger' +import { getLogLevel } from './utils' import { openDb } from './db' type Options = { @@ -36,6 +37,9 @@ type Options = { } export const setup = async (options: Options) => { + defaultLogger.level = getLogLevel(options.runtimeLogLevel) + defaultLogger.info(`Runtime log level set to ${defaultLogger.level}`) + let provider: ProviderInterface if (options.genesis) { if (typeof options.genesis === 'string') { diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index 574dc85a..c624194c 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -7,6 +7,7 @@ import { Blockchain } from '../blockchain' export * from './set-storage' export * from './time-travel' export * from './decoder' +export * from './log-level' export type GetKeys = (startKey?: string) => Promise[]> diff --git a/packages/core/src/utils/log-level.ts b/packages/core/src/utils/log-level.ts new file mode 100644 index 00000000..8c65c737 --- /dev/null +++ b/packages/core/src/utils/log-level.ts @@ -0,0 +1,23 @@ +/** + * Get pino log level from number input, default to 'info'. + * + * [off = 0; error = 1; warn = 2; info = 3; debug = 4; trace = 5] + */ +export const getLogLevel = (level: number | undefined) => { + switch (level) { + case 0: + return 'off' + case 1: + return 'error' + case 2: + return 'warn' + case 3: + return 'info' + case 4: + return 'debug' + case 5: + return 'trace' + default: + return 'info' + } +} diff --git a/packages/e2e/src/networks.ts b/packages/e2e/src/networks.ts index a4237336..e48f7509 100644 --- a/packages/e2e/src/networks.ts +++ b/packages/e2e/src/networks.ts @@ -2,7 +2,7 @@ import { config as dotenvConfig } from 'dotenv' import { SetupOption, setupContext } from '@acala-network/chopsticks-testing' -dotenvConfig +dotenvConfig() const endpoints = { polkadot: 'wss://rpc.polkadot.io', From 5e88097fd0b259fea03ca79c478f0f8906595b8a Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Tue, 5 Sep 2023 16:04:44 +0800 Subject: [PATCH 2/4] Revert "fix runtime log level setting" This reverts commit 5fe97fc980b55daf8a9a0366787a58ae7e6ba95d. --- packages/core/src/setup.ts | 4 ---- packages/core/src/utils/index.ts | 1 - packages/core/src/utils/log-level.ts | 23 ----------------------- packages/e2e/src/networks.ts | 2 +- 4 files changed, 1 insertion(+), 29 deletions(-) delete mode 100644 packages/core/src/utils/log-level.ts diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index ef3835e3..a202b623 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -19,7 +19,6 @@ import { SetValidationData, } from './blockchain/inherent' import { defaultLogger } from './logger' -import { getLogLevel } from './utils' import { openDb } from './db' type Options = { @@ -37,9 +36,6 @@ type Options = { } export const setup = async (options: Options) => { - defaultLogger.level = getLogLevel(options.runtimeLogLevel) - defaultLogger.info(`Runtime log level set to ${defaultLogger.level}`) - let provider: ProviderInterface if (options.genesis) { if (typeof options.genesis === 'string') { diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index c624194c..574dc85a 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -7,7 +7,6 @@ import { Blockchain } from '../blockchain' export * from './set-storage' export * from './time-travel' export * from './decoder' -export * from './log-level' export type GetKeys = (startKey?: string) => Promise[]> diff --git a/packages/core/src/utils/log-level.ts b/packages/core/src/utils/log-level.ts deleted file mode 100644 index 8c65c737..00000000 --- a/packages/core/src/utils/log-level.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Get pino log level from number input, default to 'info'. - * - * [off = 0; error = 1; warn = 2; info = 3; debug = 4; trace = 5] - */ -export const getLogLevel = (level: number | undefined) => { - switch (level) { - case 0: - return 'off' - case 1: - return 'error' - case 2: - return 'warn' - case 3: - return 'info' - case 4: - return 'debug' - case 5: - return 'trace' - default: - return 'info' - } -} diff --git a/packages/e2e/src/networks.ts b/packages/e2e/src/networks.ts index e48f7509..a4237336 100644 --- a/packages/e2e/src/networks.ts +++ b/packages/e2e/src/networks.ts @@ -2,7 +2,7 @@ import { config as dotenvConfig } from 'dotenv' import { SetupOption, setupContext } from '@acala-network/chopsticks-testing' -dotenvConfig() +dotenvConfig const endpoints = { polkadot: 'wss://rpc.polkadot.io', From f5d2ae298c398cc9fa792ad32af13bab603226d6 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Tue, 5 Sep 2023 18:02:04 +0800 Subject: [PATCH 3/4] dev rpc runtime log level --- .../src/plugins/set-runtime-log-level/index.ts | 14 ++++++++++++++ packages/core/src/blockchain/index.ts | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 packages/chopsticks/src/plugins/set-runtime-log-level/index.ts diff --git a/packages/chopsticks/src/plugins/set-runtime-log-level/index.ts b/packages/chopsticks/src/plugins/set-runtime-log-level/index.ts new file mode 100644 index 00000000..6a1c2740 --- /dev/null +++ b/packages/chopsticks/src/plugins/set-runtime-log-level/index.ts @@ -0,0 +1,14 @@ +import { Handler, ResponseError } from '../../rpc/shared' +import { defaultLogger } from '../../logger' + +export const rpc: Handler = async (context, [runtimeLogLevel]) => { + defaultLogger.debug({ runtimeLogLevel }, 'dev_setRuntimeLogLevel') + + if (typeof runtimeLogLevel !== 'number' || runtimeLogLevel < 0 || runtimeLogLevel > 5) { + throw new ResponseError(1, `Invalid runtimeLogLevel ${runtimeLogLevel}`) + } + + context.chain.runtimeLogLevel = runtimeLogLevel + + defaultLogger.debug(`Runtime log level set to ${runtimeLogLevel}`) +} diff --git a/packages/core/src/blockchain/index.ts b/packages/core/src/blockchain/index.ts index 1dfa4b7b..a9508c43 100644 --- a/packages/core/src/blockchain/index.ts +++ b/packages/core/src/blockchain/index.ts @@ -40,7 +40,7 @@ export class Blockchain { readonly db: DataSource | undefined readonly mockSignatureHost: boolean readonly allowUnresolvedImports: boolean - readonly runtimeLogLevel: number + #runtimeLogLevel: number readonly registeredTypes: RegisteredTypes readonly #txpool: TxPool @@ -73,7 +73,7 @@ export class Blockchain { this.db = db this.mockSignatureHost = mockSignatureHost this.allowUnresolvedImports = allowUnresolvedImports - this.runtimeLogLevel = runtimeLogLevel + this.#runtimeLogLevel = runtimeLogLevel this.registeredTypes = registeredTypes this.#head = new Block(this, header.number, header.hash) @@ -109,6 +109,14 @@ export class Blockchain { return this.#txpool } + get runtimeLogLevel(): number { + return this.#runtimeLogLevel + } + + set runtimeLogLevel(level: number) { + this.#runtimeLogLevel = level + } + async saveBlockToDB(block: Block) { if (this.db) { const { hash, number, header, extrinsics } = block From 5f6b94adade93baa637cb462b8f7e40b20777338 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Wed, 6 Sep 2023 18:34:58 +0800 Subject: [PATCH 4/4] fix log level validation and refactor log --- .../chopsticks/src/plugins/set-runtime-log-level/index.ts | 4 +--- packages/core/src/blockchain/index.ts | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/chopsticks/src/plugins/set-runtime-log-level/index.ts b/packages/chopsticks/src/plugins/set-runtime-log-level/index.ts index 6a1c2740..c5e393d5 100644 --- a/packages/chopsticks/src/plugins/set-runtime-log-level/index.ts +++ b/packages/chopsticks/src/plugins/set-runtime-log-level/index.ts @@ -4,11 +4,9 @@ import { defaultLogger } from '../../logger' export const rpc: Handler = async (context, [runtimeLogLevel]) => { defaultLogger.debug({ runtimeLogLevel }, 'dev_setRuntimeLogLevel') - if (typeof runtimeLogLevel !== 'number' || runtimeLogLevel < 0 || runtimeLogLevel > 5) { + if (typeof runtimeLogLevel !== 'number') { throw new ResponseError(1, `Invalid runtimeLogLevel ${runtimeLogLevel}`) } context.chain.runtimeLogLevel = runtimeLogLevel - - defaultLogger.debug(`Runtime log level set to ${runtimeLogLevel}`) } diff --git a/packages/core/src/blockchain/index.ts b/packages/core/src/blockchain/index.ts index a9508c43..c1868004 100644 --- a/packages/core/src/blockchain/index.ts +++ b/packages/core/src/blockchain/index.ts @@ -115,6 +115,7 @@ export class Blockchain { set runtimeLogLevel(level: number) { this.#runtimeLogLevel = level + logger.debug(`Runtime log level set to ${logger.level}`) } async saveBlockToDB(block: Block) {