From 13808724687465e0759f4aa678c0f86844b110e2 Mon Sep 17 00:00:00 2001 From: DrPing Date: Wed, 18 Jun 2025 11:14:52 +0900 Subject: [PATCH 1/2] feat(EGA): Implement gas to weight conversion tests and add EstimateGasAdapter for precompile handling (WIP) --- node/src/rpc.rs | 25 +++++++++++++++-- runtime/peaq/src/lib.rs | 1 + runtime/peaq/src/test.rs | 60 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 runtime/peaq/src/test.rs diff --git a/node/src/rpc.rs b/node/src/rpc.rs index 8b6ac30cd..8e2ad35a1 100644 --- a/node/src/rpc.rs +++ b/node/src/rpc.rs @@ -4,7 +4,7 @@ use cumulus_primitives_core::ParaId; use cumulus_primitives_parachain_inherent::ParachainInherentData; use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder; use fc_rpc::{EthBlockDataCacheTask, OverrideHandle}; -use fc_rpc_core::types::{FeeHistoryCache, FilterPool}; +use fc_rpc_core::types::{CallRequest, FeeHistoryCache, FilterPool}; use jsonrpsee::RpcModule; use peaq_primitives_xcm::*; use polkadot_primitives::PersistedValidationData; @@ -34,6 +34,27 @@ use zenlink_protocol::AssetId as ZenlinkAssetId; pub mod tracing; use crate::cli_opt::EthApi as EthApiCmd; +pub struct PeaqEGA; + +impl fc_rpc::EstimateGasAdapter for PeaqEGA { + fn adapt_request(mut request: CallRequest) -> CallRequest { + use sp_core::H160; + const BATCH_PRECOMPILE_ADDRESS: H160 = + H160(hex_literal::hex!("0000000000000000000000000000000000000805")); + const BATCH_PRECOMPILE_BATCH_ALL_SELECTOR: [u8; 4] = hex_literal::hex!("79df4b9c"); + if request.to == Some(BATCH_PRECOMPILE_ADDRESS) { + match &mut request.data { + Some(ref mut data) => + if data.0.len() >= 4 { + data.0[..4].copy_from_slice(&BATCH_PRECOMPILE_BATCH_ALL_SELECTOR); + }, + None => {}, + } + } + request + } +} + pub struct PeaqEthConfig(std::marker::PhantomData<(C, BE)>); impl fc_rpc::EthConfig for PeaqEthConfig @@ -43,7 +64,7 @@ where { // Use to override (adapt) evm call to precompiles for proper gas estimation. // We are not aware of any of our precompile that require this. - type EstimateGasAdapter = (); + type EstimateGasAdapter = PeaqEGA; // This assumes the use of HashedMapping for address mapping type RuntimeStorageOverride = fc_rpc::frontier_backend_client::SystemAccountId32StorageOverride; diff --git a/runtime/peaq/src/lib.rs b/runtime/peaq/src/lib.rs index e5b66e9cc..23d70865f 100644 --- a/runtime/peaq/src/lib.rs +++ b/runtime/peaq/src/lib.rs @@ -65,6 +65,7 @@ use sp_version::NativeVersion; use sp_version::RuntimeVersion; use zenlink_protocol::{AssetBalance, MultiAssetsHandler, PairInfo, ZenlinkMultiAssets}; +mod test; mod weights; pub mod xcm_config; diff --git a/runtime/peaq/src/test.rs b/runtime/peaq/src/test.rs new file mode 100644 index 000000000..8985bd5dd --- /dev/null +++ b/runtime/peaq/src/test.rs @@ -0,0 +1,60 @@ +#[cfg(test)] +mod tests { + use crate::Runtime; + use frame_support::weights::Weight; + use pallet_evm::GasWeightMapping; + + #[test] + fn test_gas_to_weight_conversion() { + // Test with various values + let gas_values = vec![100_000, 1_000_000, 10_000_000]; + + for gas in gas_values { + let weight = + ::GasWeightMapping::gas_to_weight(gas, false); + + // Convert back and check if it's reasonably close + let gas_again = + ::GasWeightMapping::weight_to_gas(weight); + + // Should be reasonably close, allowing for some precision loss + let tolerance = (gas as f64 * 0.01) as u64; // 1% tolerance + assert!( + (gas as i64 - gas_again as i64).abs() < tolerance as i64, + "Gas conversion roundtrip failed: {} -> {} -> {}", + gas, + weight, + gas_again + ); + } + } + + #[test] + fn test_weight_to_gas_conversion() { + // Test various weights + let weights = vec![ + Weight::from_parts(100_000, 0), + Weight::from_parts(1_000_000, 0), + Weight::from_parts(10_000_000, 0), + ]; + + for weight in weights { + let gas = ::GasWeightMapping::weight_to_gas(weight); + + // Convert back and check + let weight_again = + ::GasWeightMapping::gas_to_weight(gas, false); + + // Should be reasonably close (allowing for precision loss) + let tolerance = (weight.ref_time() as f64 * 0.01) as u64; // 1% tolerance + assert!( + (weight.ref_time() as i64 - weight_again.ref_time() as i64).abs() < + tolerance as i64, + "Weight conversion roundtrip failed: {:?} -> {} -> {:?}", + weight, + gas, + weight_again + ); + } + } +} From 9baa4bf88639e0af172ef5da17e5607861bd4554 Mon Sep 17 00:00:00 2001 From: DrPing <23166653+DocteurPing@users.noreply.github.com> Date: Fri, 27 Jun 2025 14:50:24 +0700 Subject: [PATCH 2/2] fix(EGA): Make match statement into if let for better readability Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- node/src/rpc.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/node/src/rpc.rs b/node/src/rpc.rs index 8e2ad35a1..57612f6c8 100644 --- a/node/src/rpc.rs +++ b/node/src/rpc.rs @@ -43,12 +43,10 @@ impl fc_rpc::EstimateGasAdapter for PeaqEGA { H160(hex_literal::hex!("0000000000000000000000000000000000000805")); const BATCH_PRECOMPILE_BATCH_ALL_SELECTOR: [u8; 4] = hex_literal::hex!("79df4b9c"); if request.to == Some(BATCH_PRECOMPILE_ADDRESS) { - match &mut request.data { - Some(ref mut data) => - if data.0.len() >= 4 { - data.0[..4].copy_from_slice(&BATCH_PRECOMPILE_BATCH_ALL_SELECTOR); - }, - None => {}, + if let Some(ref mut data) = &mut request.data { + if data.0.len() >= 4 { + data.0[..4].copy_from_slice(&BATCH_PRECOMPILE_BATCH_ALL_SELECTOR); + } } } request