From 184ca3743baca8b7c43ae33f4d584d21bae82d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Tue, 6 Aug 2024 19:51:06 +0200 Subject: [PATCH] fix: support runtime api Core::initialize_block for old runtimes --- client/rpc/debug/src/lib.rs | 48 +++++++++++++++++++++++++++++++------ client/rpc/trace/src/lib.rs | 19 ++++++++++++--- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/client/rpc/debug/src/lib.rs b/client/rpc/debug/src/lib.rs index 3bb402644f..96bd61736b 100644 --- a/client/rpc/debug/src/lib.rs +++ b/client/rpc/debug/src/lib.rs @@ -433,15 +433,30 @@ where // The block is initialized inside "trace_block" api.trace_block(parent_block_hash, exts, eth_tx_hashes, &header) } else { - // Pre pallet-message-queue + // Get core runtime api version + let core_api_version = if let Ok(Some(api_version)) = + api.api_version::>(parent_block_hash) + { + api_version + } else { + return Err(internal_err( + "Runtime api version call failed (core)".to_string(), + )); + }; // Initialize block: calls the "on_initialize" hook on every pallet // in AllPalletsWithSystem // This was fine before pallet-message-queue because the XCM messages // were processed by the "setValidationData" inherent call and not on an // "on_initialize" hook, which runs before enabling XCM tracing - api.initialize_block(parent_block_hash, &header) - .map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?; + if core_api_version >= 5 { + api.initialize_block(parent_block_hash, &header) + .map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?; + } else { + #[allow(deprecated)] + api.initialize_block_before_version_5(parent_block_hash, &header) + .map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?; + } #[allow(deprecated)] api.trace_block_before_version_5(parent_block_hash, exts, eth_tx_hashes) @@ -573,15 +588,34 @@ where // The block is initialized inside "trace_transaction" api.trace_transaction(parent_block_hash, exts, &transaction, &header) } else { + // Get core runtime api version + let core_api_version = if let Ok(Some(api_version)) = + api.api_version::>(parent_block_hash) + { + api_version + } else { + return Err(internal_err( + "Runtime api version call failed (core)".to_string(), + )); + }; + // Initialize block: calls the "on_initialize" hook on every pallet // in AllPalletsWithSystem // This was fine before pallet-message-queue because the XCM messages // were processed by the "setValidationData" inherent call and not on an // "on_initialize" hook, which runs before enabling XCM tracing - api.initialize_block(parent_block_hash, &header) - .map_err(|e| { - internal_err(format!("Runtime api access error: {:?}", e)) - })?; + if core_api_version >= 5 { + api.initialize_block(parent_block_hash, &header) + .map_err(|e| { + internal_err(format!("Runtime api access error: {:?}", e)) + })?; + } else { + #[allow(deprecated)] + api.initialize_block_before_version_5(parent_block_hash, &header) + .map_err(|e| { + internal_err(format!("Runtime api access error: {:?}", e)) + })?; + } if trace_api_version == 4 { // Pre pallet-message-queue diff --git a/client/rpc/trace/src/lib.rs b/client/rpc/trace/src/lib.rs index 50e6b8acda..8d5cd635f1 100644 --- a/client/rpc/trace/src/lib.rs +++ b/client/rpc/trace/src/lib.rs @@ -859,15 +859,28 @@ where &block_header, ) } else { - // Pre pallet-message-queue + // Get core runtime api version + let core_api_version = if let Ok(Some(api_version)) = + api.api_version::>(substrate_parent_hash) + { + api_version + } else { + return Err("Runtime api version call failed (core)".to_string()); + }; // Initialize block: calls the "on_initialize" hook on every pallet // in AllPalletsWithSystem // This was fine before pallet-message-queue because the XCM messages // were processed by the "setValidationData" inherent call and not on an // "on_initialize" hook, which runs before enabling XCM tracing - api.initialize_block(substrate_parent_hash, &block_header) - .map_err(|e| format!("Runtime api access error: {:?}", e))?; + if core_api_version >= 5 { + api.initialize_block(substrate_parent_hash, &block_header) + .map_err(|e| format!("Runtime api access error: {:?}", e))?; + } else { + #[allow(deprecated)] + api.initialize_block_before_version_5(substrate_parent_hash, &block_header) + .map_err(|e| format!("Runtime api access error: {:?}", e))?; + } #[allow(deprecated)] api.trace_block_before_version_5(substrate_parent_hash, extrinsics, eth_tx_hashes)