Skip to content

Commit

Permalink
fix: support runtime api Core::initialize_block for old runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
librelois committed Aug 6, 2024
1 parent 20ba7fe commit 184ca37
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
48 changes: 41 additions & 7 deletions client/rpc/debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<dyn Core<B>>(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)
Expand Down Expand Up @@ -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::<dyn Core<B>>(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
Expand Down
19 changes: 16 additions & 3 deletions client/rpc/trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<dyn Core<B>>(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)
Expand Down

0 comments on commit 184ca37

Please sign in to comment.