Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<C, BE>(std::marker::PhantomData<(C, BE)>);

impl<C, BE> fc_rpc::EthConfig<Block, C> for PeaqEthConfig<C, BE>
Expand All @@ -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<BlakeTwo256> for address mapping
type RuntimeStorageOverride =
fc_rpc::frontier_backend_client::SystemAccountId32StorageOverride<Block, C, BE>;
Expand Down
1 change: 1 addition & 0 deletions runtime/peaq/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
60 changes: 60 additions & 0 deletions runtime/peaq/src/test.rs
Original file line number Diff line number Diff line change
@@ -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 =
<Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(gas, false);

// Convert back and check if it's reasonably close
let gas_again =
<Runtime as pallet_evm::Config>::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 = <Runtime as pallet_evm::Config>::GasWeightMapping::weight_to_gas(weight);

// Convert back and check
let weight_again =
<Runtime as pallet_evm::Config>::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
);
}
}
}
Loading