Skip to content

Commit 933edfe

Browse files
libreloisnoandrea
authored andcommitted
fix: support runtime api Core::initialize_block for old runtimes
1 parent f0404cc commit 933edfe

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

client/rpc/debug/src/lib.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,30 @@ where
442442
// The block is initialized inside "trace_block"
443443
api.trace_block(parent_block_hash, exts, eth_tx_hashes, &header)
444444
} else {
445-
// Pre pallet-message-queue
445+
// Get core runtime api version
446+
let core_api_version = if let Ok(Some(api_version)) =
447+
api.api_version::<dyn Core<B>>(parent_block_hash)
448+
{
449+
api_version
450+
} else {
451+
return Err(internal_err(
452+
"Runtime api version call failed (core)".to_string(),
453+
));
454+
};
446455

447456
// Initialize block: calls the "on_initialize" hook on every pallet
448457
// in AllPalletsWithSystem
449458
// This was fine before pallet-message-queue because the XCM messages
450459
// were processed by the "setValidationData" inherent call and not on an
451460
// "on_initialize" hook, which runs before enabling XCM tracing
452-
api.initialize_block(parent_block_hash, &header)
453-
.map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?;
461+
if core_api_version >= 5 {
462+
api.initialize_block(parent_block_hash, &header)
463+
.map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?;
464+
} else {
465+
#[allow(deprecated)]
466+
api.initialize_block_before_version_5(parent_block_hash, &header)
467+
.map_err(|e| internal_err(format!("Runtime api access error: {:?}", e)))?;
468+
}
454469

455470
#[allow(deprecated)]
456471
api.trace_block_before_version_5(parent_block_hash, exts, eth_tx_hashes)
@@ -583,15 +598,34 @@ where
583598
// The block is initialized inside "trace_transaction"
584599
api.trace_transaction(parent_block_hash, exts, &transaction, &header)
585600
} else {
601+
// Get core runtime api version
602+
let core_api_version = if let Ok(Some(api_version)) =
603+
api.api_version::<dyn Core<B>>(parent_block_hash)
604+
{
605+
api_version
606+
} else {
607+
return Err(internal_err(
608+
"Runtime api version call failed (core)".to_string(),
609+
));
610+
};
611+
586612
// Initialize block: calls the "on_initialize" hook on every pallet
587613
// in AllPalletsWithSystem
588614
// This was fine before pallet-message-queue because the XCM messages
589615
// were processed by the "setValidationData" inherent call and not on an
590616
// "on_initialize" hook, which runs before enabling XCM tracing
591-
api.initialize_block(parent_block_hash, &header)
592-
.map_err(|e| {
593-
internal_err(format!("Runtime api access error: {:?}", e))
594-
})?;
617+
if core_api_version >= 5 {
618+
api.initialize_block(parent_block_hash, &header)
619+
.map_err(|e| {
620+
internal_err(format!("Runtime api access error: {:?}", e))
621+
})?;
622+
} else {
623+
#[allow(deprecated)]
624+
api.initialize_block_before_version_5(parent_block_hash, &header)
625+
.map_err(|e| {
626+
internal_err(format!("Runtime api access error: {:?}", e))
627+
})?;
628+
}
595629

596630
if trace_api_version == 4 {
597631
// Pre pallet-message-queue

client/rpc/trace/src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,15 +859,28 @@ where
859859
&block_header,
860860
)
861861
} else {
862-
// Pre pallet-message-queue
862+
// Get core runtime api version
863+
let core_api_version = if let Ok(Some(api_version)) =
864+
api.api_version::<dyn Core<B>>(substrate_parent_hash)
865+
{
866+
api_version
867+
} else {
868+
return Err("Runtime api version call failed (core)".to_string());
869+
};
863870

864871
// Initialize block: calls the "on_initialize" hook on every pallet
865872
// in AllPalletsWithSystem
866873
// This was fine before pallet-message-queue because the XCM messages
867874
// were processed by the "setValidationData" inherent call and not on an
868875
// "on_initialize" hook, which runs before enabling XCM tracing
869-
api.initialize_block(substrate_parent_hash, &block_header)
870-
.map_err(|e| format!("Runtime api access error: {:?}", e))?;
876+
if core_api_version >= 5 {
877+
api.initialize_block(substrate_parent_hash, &block_header)
878+
.map_err(|e| format!("Runtime api access error: {:?}", e))?;
879+
} else {
880+
#[allow(deprecated)]
881+
api.initialize_block_before_version_5(substrate_parent_hash, &block_header)
882+
.map_err(|e| format!("Runtime api access error: {:?}", e))?;
883+
}
871884

872885
#[allow(deprecated)]
873886
api.trace_block_before_version_5(substrate_parent_hash, extrinsics, eth_tx_hashes)

0 commit comments

Comments
 (0)