From 93525269f9d28961bd6aca27d3213dfcc981cef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Mon, 18 Mar 2024 17:10:57 +0100 Subject: [PATCH 1/5] evm-tracing should replay on_idle --- runtime/common/src/apis.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs index c2f237ba4f..a9036ca09a 100644 --- a/runtime/common/src/apis.rs +++ b/runtime/common/src/apis.rs @@ -138,6 +138,12 @@ macro_rules! impl_runtime_apis_plus_common { return Ok(()); } } + + // Replay on_idle + // Somme XCM messages with eth-xcm transaction might be executed at on_idle + Executive::on_idle(); + + // The transaction was not-found Err(sp_runtime::DispatchError::Other( "Failed to find Ethereum transaction among the extrinsics.", )) @@ -187,6 +193,10 @@ macro_rules! impl_runtime_apis_plus_common { }; } + // Replay on_idle + // Somme XCM messages with eth-xcm transaction might be executed at on_idle + Executive::on_idle(); + Ok(()) } #[cfg(not(feature = "evm-tracing"))] From 0750ccc16a0d85872eacbb0cfa373771e41c6b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Mon, 18 Mar 2024 19:34:03 +0100 Subject: [PATCH 2/5] fix compilation --- runtime/common/src/apis.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs index a9036ca09a..78a9495356 100644 --- a/runtime/common/src/apis.rs +++ b/runtime/common/src/apis.rs @@ -139,14 +139,25 @@ macro_rules! impl_runtime_apis_plus_common { } } - // Replay on_idle - // Somme XCM messages with eth-xcm transaction might be executed at on_idle - Executive::on_idle(); + if let Some(EthereumXcmTracingStatus::Transaction(_)) = unhashed::get( + ETHEREUM_XCM_TRACING_STORAGE_KEY + ) { + // If the transaction was not found, it might be + // an eth-xcm transaction that was executed at on_idle + Executive::finalize_block(); + } - // The transaction was not-found - Err(sp_runtime::DispatchError::Other( - "Failed to find Ethereum transaction among the extrinsics.", - )) + if let Some(EthereumXcmTracingStatus::TransactionExited) = unhashed::get( + ETHEREUM_XCM_TRACING_STORAGE_KEY + ) { + // The transaction was found + Ok(()) + } else { + // The transaction was not-found + Err(sp_runtime::DispatchError::Other( + "Failed to find Ethereum transaction among the extrinsics.", + )) + } } #[cfg(not(feature = "evm-tracing"))] Err(sp_runtime::DispatchError::Other( @@ -195,7 +206,7 @@ macro_rules! impl_runtime_apis_plus_common { // Replay on_idle // Somme XCM messages with eth-xcm transaction might be executed at on_idle - Executive::on_idle(); + Executive::finalize_block(); Ok(()) } From feac84f69f491291cb1173ffa0a58105e5c1e95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Mon, 18 Mar 2024 20:01:17 +0100 Subject: [PATCH 3/5] fix rust tests --- runtime/moonbase/tests/evm_tracing.rs | 4 ++++ runtime/moonbeam/tests/evm_tracing.rs | 4 ++++ runtime/moonriver/tests/evm_tracing.rs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/runtime/moonbase/tests/evm_tracing.rs b/runtime/moonbase/tests/evm_tracing.rs index 85fffcbf9e..fabcf760e6 100644 --- a/runtime/moonbase/tests/evm_tracing.rs +++ b/runtime/moonbase/tests/evm_tracing.rs @@ -84,6 +84,10 @@ mod tests { let eth_uxt = unchecked_eth_tx(VALID_ETH_TX); let eth_tx = ethereum_transaction(VALID_ETH_TX); let eth_extrinsic_hash = eth_tx.hash(); + + // trace block finalize the block, so we need to set the timestamp to avoid panic + pallet_timestamp::Call::::set(6_000); + assert!(Runtime::trace_block( vec![non_eth_uxt.clone(), eth_uxt.clone(), non_eth_uxt, eth_uxt], vec![eth_extrinsic_hash, eth_extrinsic_hash] diff --git a/runtime/moonbeam/tests/evm_tracing.rs b/runtime/moonbeam/tests/evm_tracing.rs index 5d241a8850..723f02f487 100644 --- a/runtime/moonbeam/tests/evm_tracing.rs +++ b/runtime/moonbeam/tests/evm_tracing.rs @@ -84,6 +84,10 @@ mod tests { let eth_uxt = unchecked_eth_tx(VALID_ETH_TX); let eth_tx = ethereum_transaction(VALID_ETH_TX); let eth_extrinsic_hash = eth_tx.hash(); + + // trace block finalize the block, so we need to set the timestamp to avoid panic + pallet_timestamp::Call::::set(6_000); + assert!(Runtime::trace_block( vec![non_eth_uxt.clone(), eth_uxt.clone(), non_eth_uxt, eth_uxt], vec![eth_extrinsic_hash, eth_extrinsic_hash] diff --git a/runtime/moonriver/tests/evm_tracing.rs b/runtime/moonriver/tests/evm_tracing.rs index c8db27d0ff..8cc44bf868 100644 --- a/runtime/moonriver/tests/evm_tracing.rs +++ b/runtime/moonriver/tests/evm_tracing.rs @@ -84,6 +84,10 @@ mod tests { let eth_uxt = unchecked_eth_tx(VALID_ETH_TX); let eth_tx = ethereum_transaction(VALID_ETH_TX); let eth_extrinsic_hash = eth_tx.hash(); + + // trace block finalize the block, so we need to set the timestamp to avoid panic + pallet_timestamp::Call::::set(6_000); + assert!(Runtime::trace_block( vec![non_eth_uxt.clone(), eth_uxt.clone(), non_eth_uxt, eth_uxt], vec![eth_extrinsic_hash, eth_extrinsic_hash] From 16957167f919422bf08866b69be47f21b8187215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Mon, 18 Mar 2024 20:47:04 +0100 Subject: [PATCH 4/5] not call on_finalize --- runtime/common/src/apis.rs | 24 +++++++++++++++++++++--- runtime/moonbase/tests/evm_tracing.rs | 3 --- runtime/moonbeam/tests/evm_tracing.rs | 3 --- runtime/moonriver/tests/evm_tracing.rs | 3 --- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs index 78a9495356..1469921e3d 100644 --- a/runtime/common/src/apis.rs +++ b/runtime/common/src/apis.rs @@ -111,6 +111,7 @@ macro_rules! impl_runtime_apis_plus_common { EthereumXcmTracingStatus }; use frame_support::storage::unhashed; + use frame_system::pallet_prelude::BlockNumberFor; // Tell the CallDispatcher we are tracing a specific Transaction. unhashed::put::( @@ -144,7 +145,15 @@ macro_rules! impl_runtime_apis_plus_common { ) { // If the transaction was not found, it might be // an eth-xcm transaction that was executed at on_idle - Executive::finalize_block(); + let weight = >::block_weight(); + let max_weight = <::BlockWeights as frame_support::traits::Get<_>>::get().max_block; + let remaining_weight = max_weight.saturating_sub(weight.total()); + if remaining_weight.all_gt(Weight::zero()) { + let used_weight = >>::on_idle( + >::block_number(), + remaining_weight, + ); + } } if let Some(EthereumXcmTracingStatus::TransactionExited) = unhashed::get( @@ -175,6 +184,7 @@ macro_rules! impl_runtime_apis_plus_common { #[cfg(feature = "evm-tracing")] { use moonbeam_evm_tracer::tracer::EvmTracer; + use frame_system::pallet_prelude::BlockNumberFor; use xcm_primitives::EthereumXcmTracingStatus; // Tell the CallDispatcher we are tracing a full Block. @@ -205,8 +215,16 @@ macro_rules! impl_runtime_apis_plus_common { } // Replay on_idle - // Somme XCM messages with eth-xcm transaction might be executed at on_idle - Executive::finalize_block(); + // Some XCM messages with eth-xcm transaction might be executed at on_idle + let weight = >::block_weight(); + let max_weight = <::BlockWeights as frame_support::traits::Get<_>>::get().max_block; + let remaining_weight = max_weight.saturating_sub(weight.total()); + if remaining_weight.all_gt(Weight::zero()) { + let used_weight = >>::on_idle( + >::block_number(), + remaining_weight, + ); + } Ok(()) } diff --git a/runtime/moonbase/tests/evm_tracing.rs b/runtime/moonbase/tests/evm_tracing.rs index fabcf760e6..89106ec6c7 100644 --- a/runtime/moonbase/tests/evm_tracing.rs +++ b/runtime/moonbase/tests/evm_tracing.rs @@ -85,9 +85,6 @@ mod tests { let eth_tx = ethereum_transaction(VALID_ETH_TX); let eth_extrinsic_hash = eth_tx.hash(); - // trace block finalize the block, so we need to set the timestamp to avoid panic - pallet_timestamp::Call::::set(6_000); - assert!(Runtime::trace_block( vec![non_eth_uxt.clone(), eth_uxt.clone(), non_eth_uxt, eth_uxt], vec![eth_extrinsic_hash, eth_extrinsic_hash] diff --git a/runtime/moonbeam/tests/evm_tracing.rs b/runtime/moonbeam/tests/evm_tracing.rs index 723f02f487..f7fe777829 100644 --- a/runtime/moonbeam/tests/evm_tracing.rs +++ b/runtime/moonbeam/tests/evm_tracing.rs @@ -85,9 +85,6 @@ mod tests { let eth_tx = ethereum_transaction(VALID_ETH_TX); let eth_extrinsic_hash = eth_tx.hash(); - // trace block finalize the block, so we need to set the timestamp to avoid panic - pallet_timestamp::Call::::set(6_000); - assert!(Runtime::trace_block( vec![non_eth_uxt.clone(), eth_uxt.clone(), non_eth_uxt, eth_uxt], vec![eth_extrinsic_hash, eth_extrinsic_hash] diff --git a/runtime/moonriver/tests/evm_tracing.rs b/runtime/moonriver/tests/evm_tracing.rs index 8cc44bf868..9acf4de3b8 100644 --- a/runtime/moonriver/tests/evm_tracing.rs +++ b/runtime/moonriver/tests/evm_tracing.rs @@ -85,9 +85,6 @@ mod tests { let eth_tx = ethereum_transaction(VALID_ETH_TX); let eth_extrinsic_hash = eth_tx.hash(); - // trace block finalize the block, so we need to set the timestamp to avoid panic - pallet_timestamp::Call::::set(6_000); - assert!(Runtime::trace_block( vec![non_eth_uxt.clone(), eth_uxt.clone(), non_eth_uxt, eth_uxt], vec![eth_extrinsic_hash, eth_extrinsic_hash] From 5da4b4aba663627d5c78c9dfcf8dc09907174132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lo=C3=AFs?= Date: Tue, 19 Mar 2024 11:46:19 +0100 Subject: [PATCH 5/5] create helper function replay_on_idle --- runtime/common/src/apis.rs | 45 +++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs index 1469921e3d..368c97f53f 100644 --- a/runtime/common/src/apis.rs +++ b/runtime/common/src/apis.rs @@ -17,6 +17,31 @@ #[macro_export] macro_rules! impl_runtime_apis_plus_common { {$($custom:tt)*} => { + + #[cfg(feature = "evm-tracing")] + // Helper function to replay the "on_idle" hook for all pallets, we need this for + // evm-tracing because some ethereum-xcm transactions might be executed at on_idle. + // + // We need to make sure that we replay on_idle exactly the same way as the + // original block execution, but unfortunatly frame executive diosn't provide a function + // to replay only on_idle, so we need to copy here some code inside frame executive. + fn replay_on_idle() { + use frame_system::pallet_prelude::BlockNumberFor; + + let weight = >::block_weight(); + let max_weight = < + ::BlockWeights as + frame_support::traits::Get<_> + >::get().max_block; + let remaining_weight = max_weight.saturating_sub(weight.total()); + if remaining_weight.all_gt(Weight::zero()) { + let _ = >>::on_idle( + >::block_number(), + remaining_weight, + ); + } + } + impl_runtime_apis! { $($custom)* @@ -145,15 +170,7 @@ macro_rules! impl_runtime_apis_plus_common { ) { // If the transaction was not found, it might be // an eth-xcm transaction that was executed at on_idle - let weight = >::block_weight(); - let max_weight = <::BlockWeights as frame_support::traits::Get<_>>::get().max_block; - let remaining_weight = max_weight.saturating_sub(weight.total()); - if remaining_weight.all_gt(Weight::zero()) { - let used_weight = >>::on_idle( - >::block_number(), - remaining_weight, - ); - } + replay_on_idle(); } if let Some(EthereumXcmTracingStatus::TransactionExited) = unhashed::get( @@ -216,15 +233,7 @@ macro_rules! impl_runtime_apis_plus_common { // Replay on_idle // Some XCM messages with eth-xcm transaction might be executed at on_idle - let weight = >::block_weight(); - let max_weight = <::BlockWeights as frame_support::traits::Get<_>>::get().max_block; - let remaining_weight = max_weight.saturating_sub(weight.total()); - if remaining_weight.all_gt(Weight::zero()) { - let used_weight = >>::on_idle( - >::block_number(), - remaining_weight, - ); - } + replay_on_idle(); Ok(()) }