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

support runtime log level #508

Merged
merged 4 commits into from
Nov 2, 2023
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
264 changes: 136 additions & 128 deletions executor/Cargo.lock

Large diffs are not rendered by default.

51 changes: 45 additions & 6 deletions executor/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use smoldot::{
calculate_root::{root_merkle_value, RootMerkleValueCalculation},
nibbles_to_bytes_suffix_extend, HashFunction, TrieEntryVersion,
},
verify::body_only::LogEmitInfo,
};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -68,13 +69,21 @@ pub struct TaskCall {
runtime_log_level: u32,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LogInfo {
message: String,
level: Option<u32>,
target: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CallResponse {
result: HexString,
storage_diff: Vec<(HexString, Option<HexString>)>,
offchain_storage_diff: Vec<(HexString, Option<HexString>)>,
runtime_logs: Vec<String>,
runtime_logs: Vec<LogInfo>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -123,7 +132,7 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
})
.unwrap();
let mut ret: Result<Vec<u8>, String> = Ok(Vec::new());
let mut runtime_logs: Vec<String> = vec![];
let mut runtime_logs: Vec<LogInfo> = vec![];

for (call, params) in task.calls {
let mut vm = runtime_host::run(runtime_host::Config {
Expand Down Expand Up @@ -287,6 +296,40 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
req.resume(success)
}
},

RuntimeHostVm::LogEmit(req) => {
{
let info = req.info();
let log = match info {
LogEmitInfo::Num(v) => LogInfo {
qiweiii marked this conversation as resolved.
Show resolved Hide resolved
message: v.to_string(),
level: None,
target: None,
},
LogEmitInfo::Utf8(v) => LogInfo {
message: v.to_string(),
level: None,
target: None,
},
LogEmitInfo::Hex(v) => LogInfo {
message: v.to_string(),
level: None,
target: None,
},
LogEmitInfo::Log {
log_level,
target,
message,
} => LogInfo {
message: message.to_string(),
level: Some(log_level),
target: Some(target.to_string()),
},
};
runtime_logs.push(log);
}
req.resume()
}
}
};

Expand All @@ -312,10 +355,6 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
});

storage_main_trie_changes = success.storage_changes.into_main_trie_diff();

if !success.logs.is_empty() {
runtime_logs.push(success.logs);
}
}
Err(err) => {
ret = Err(err.to_string());
Expand Down
3 changes: 2 additions & 1 deletion packages/chopsticks/src/plugins/run-block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type yargs from 'yargs'
import {
Block,
Context,
RuntimeLog,
compactHex,
decodeKeyValue,
printRuntimeLogs,
Expand Down Expand Up @@ -167,7 +168,7 @@ export interface RunBlockResponse {
/**
* Runtime logs.
*/
logs?: string[]
logs?: RuntimeLog[]
}[]
/**
* Block details. Only available when `includeBlockDetails` is true.
Expand Down
2 changes: 1 addition & 1 deletion packages/chopsticks/src/utils/generate-html-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'node:path'

export const generateHtmlDiff = async (block: Block, diff: [HexString, HexString | null][]) => {
const { oldState, delta } = await decodeStorageDiff(block, diff)
const htmlTemplate = readFileSync(path.join(__dirname, '../../../template/diff.html'), 'utf-8')
const htmlTemplate = readFileSync(path.join(__dirname, '../../template/diff.html'), 'utf-8')
return template(htmlTemplate)({ left: JSON.stringify(oldState), delta: JSON.stringify(delta) })
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/blockchain/block-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
RawBabePreDigest,
TransactionValidityError,
} from '@polkadot/types/interfaces'
import { Block, TaskCallResponse } from './block'
import { Block } from './block'
import { GenericExtrinsic } from '@polkadot/types'
import { HexString } from '@polkadot/util/types'
import { StorageLayer, StorageValueKind } from './storage-layer'
import { TaskCallResponse } from '../wasm-executor'
import { compactAddLength, hexToU8a, stringToHex, u8aConcat } from '@polkadot/util'
import { compactHex } from '../utils'
import { defaultLogger, truncate } from '../logger'
Expand Down
9 changes: 1 addition & 8 deletions packages/core/src/blockchain/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ import { Blockchain } from '.'
import { RemoteStorageLayer, StorageLayer, StorageLayerProvider, StorageValue, StorageValueKind } from './storage-layer'
import { compactHex, printRuntimeLogs } from '../utils'
import { getRuntimeVersion, runTask, taskHandler } from '../wasm-executor'
import type { RuntimeVersion } from '../wasm-executor'

export type TaskCallResponse = {
result: HexString
storageDiff: [HexString, HexString | null][]
offchainStorageDiff: [HexString, HexString | null][]
runtimeLogs: string[]
}
import type { RuntimeVersion, TaskCallResponse } from '../wasm-executor'

/**
* Block class.
Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { compactStripLength, u8aToHex } from '@polkadot/util'
import { hexAddPrefix, hexStripPrefix } from '@polkadot/util/hex'

import { Blockchain } from '../blockchain'
import { RuntimeLog } from '../wasm-executor'

export * from './set-storage'
export * from './time-travel'
Expand Down Expand Up @@ -108,12 +109,26 @@ export const stripChildPrefix = (key: HexString) => {
return storageKey
}

export const printRuntimeLogs = (logs: string[]) => {
const logLevels = ['OFF', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE']

export const formatRuntimeLog = (log: RuntimeLog) => {
let msg = ''
if (log.level != null) {
msg += `${logLevels[log.level]}\t `
}
if (log.target) {
msg += `[${log.target}]:\t `
}
msg += log.message
return msg
}

export const printRuntimeLogs = (logs: RuntimeLog[]) => {
if (!logs.length) return

console.group('RuntimeLogs:')
for (const log of logs) {
console.log(log)
console.log(formatRuntimeLog(log))
}
console.groupEnd()
}
20 changes: 14 additions & 6 deletions packages/core/src/wasm-executor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ export type RuntimeVersion = {
stateVersion: number
}

export type RuntimeLog = {
message: string
level?: number
target?: string
}

export type TaskCallResponse = {
result: HexString
storageDiff: [HexString, HexString | null][]
offchainStorageDiff: [HexString, HexString | null][]
runtimeLogs: RuntimeLog[]
}

export type TaskResponse =
| {
Call: {
result: HexString
storageDiff: [HexString, HexString | null][]
offchainStorageDiff: [HexString, HexString | null][]
runtimeLogs: string[]
}
Call: TaskCallResponse
}
| {
Error: string
Expand Down
2 changes: 1 addition & 1 deletion vendor/smoldot
Submodule smoldot updated 73 files
+13 −2 .github/workflows/ci.yml
+4 −4 .github/workflows/deploy.yml
+273 −193 Cargo.lock
+2 −0 README.md
+62 −0 demo-chain-specs/polkadot-asset-hub.json
+3 −3 full-node/Cargo.toml
+21 −14 full-node/src/consensus_service.rs
+6 −3 full-node/src/json_rpc_service.rs
+11 −4 full-node/src/json_rpc_service/requests_handler.rs
+22 −18 full-node/src/lib.rs
+716 −396 full-node/src/network_service.rs
+8 −37 full-node/src/network_service/tasks.rs
+0 −20 full-node/src/util.rs
+117 −109 fuzz/Cargo.lock
+4 −3 lib/Cargo.toml
+4 −0 lib/src/chain/chain_information/build.rs
+6 −0 lib/src/chain_spec.rs
+6 −1 lib/src/chain_spec/structs.rs
+274 −124 lib/src/executor/host.rs
+82 −51 lib/src/executor/runtime_host.rs
+1 −0 lib/src/executor/runtime_host/tests.rs
+9 −15 lib/src/libp2p.rs
+18 −11 lib/src/libp2p/collection.rs
+3 −3 lib/src/libp2p/collection/multi_stream.rs
+3 −3 lib/src/libp2p/collection/single_stream.rs
+1 −0 lib/src/libp2p/connection/established/substream.rs
+0 −2,512 lib/src/libp2p/peers.rs
+142 −108 lib/src/libp2p/with_buffers.rs
+1 −0 lib/src/network.rs
+826 −0 lib/src/network/basic_peering_strategy.rs
+257 −0 lib/src/network/protocol.rs
+1 −1 lib/src/network/protocol/block_announces.rs
+3,535 −915 lib/src/network/service.rs
+0 −223 lib/src/network/service/addresses.rs
+0 −870 lib/src/network/service/notifications.rs
+0 −1,236 lib/src/network/service/requests_responses.rs
+70 −88 lib/src/sync/all_forks.rs
+2 −0 lib/src/sync/optimistic.rs
+10 −0 lib/src/transactions/validate.rs
+107 −36 lib/src/verify/babe.rs
+39 −1 lib/src/verify/body_only.rs
+6 −6 light-base/Cargo.toml
+57 −16 light-base/examples/basic.rs
+2 −1 light-base/src/database.rs
+7 −7 light-base/src/json_rpc_service.rs
+9 −9 light-base/src/json_rpc_service/background.rs
+38 −14 light-base/src/json_rpc_service/background/chain_head.rs
+1 −7 light-base/src/json_rpc_service/background/getters.rs
+150 −122 light-base/src/json_rpc_service/background/legacy_state_sub.rs
+32 −11 light-base/src/json_rpc_service/background/state_chain.rs
+4 −1 light-base/src/json_rpc_service/background/transactions.rs
+24 −38 light-base/src/lib.rs
+855 −665 light-base/src/network_service.rs
+46 −198 light-base/src/network_service/tasks.rs
+13 −10 light-base/src/platform.rs
+25 −24 light-base/src/platform/default.rs
+150 −49 light-base/src/runtime_service.rs
+13 −9 light-base/src/sync_service.rs
+18 −15 light-base/src/sync_service/parachain.rs
+23 −24 light-base/src/sync_service/standalone.rs
+238 −135 light-base/src/transactions_service.rs
+6 −0 wasm-node/CHANGELOG.md
+18 −18 wasm-node/javascript/package-lock.json
+34 −37 wasm-node/javascript/src/internals/client.ts
+15 −25 wasm-node/javascript/src/internals/local-instance.ts
+9 −9 wasm-node/javascript/src/internals/remote-instance.ts
+26 −97 wasm-node/javascript/src/no-auto-bytecode-browser.ts
+2 −2 wasm-node/javascript/src/no-auto-bytecode-deno.ts
+2 −5 wasm-node/javascript/src/no-auto-bytecode-nodejs.ts
+14 −13 wasm-node/javascript/src/public-types.ts
+1 −1 wasm-node/rust/Cargo.toml
+20 −52 wasm-node/rust/src/bindings.rs
+112 −142 wasm-node/rust/src/platform.rs