diff --git a/node/src/rpc.rs b/node/src/rpc.rs index 8b6ac30c..57612f6c 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,25 @@ 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) { + 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 + } +} + pub struct PeaqEthConfig(std::marker::PhantomData<(C, BE)>); impl fc_rpc::EthConfig for PeaqEthConfig @@ -43,7 +62,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 1165709f..e6ef307b 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 00000000..8985bd5d --- /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 + ); + } + } +}