|
| 1 | +extern crate alloc; |
| 2 | + |
| 3 | +use alloc::format; |
| 4 | + |
| 5 | +use frame_support::dispatch::{GetDispatchInfo, Pays, PostDispatchInfo}; |
| 6 | +use frame_system::RawOrigin; |
| 7 | +use pallet_evm::{ |
| 8 | + AddressMapping, BalanceConverter, ExitError, GasWeightMapping, PrecompileFailure, |
| 9 | + PrecompileHandle, |
| 10 | +}; |
| 11 | +use precompile_utils::EvmResult; |
| 12 | +use sp_core::{H160, U256, blake2_256}; |
| 13 | +use sp_runtime::traits::Dispatchable; |
| 14 | +use sp_std::vec::Vec; |
| 15 | + |
| 16 | +pub(crate) trait PrecompileHandleExt: PrecompileHandle { |
| 17 | + fn caller_account_id<R>(&self) -> R::AccountId |
| 18 | + where |
| 19 | + R: frame_system::Config + pallet_evm::Config, |
| 20 | + <R as pallet_evm::Config>::AddressMapping: AddressMapping<R::AccountId>, |
| 21 | + { |
| 22 | + <R as pallet_evm::Config>::AddressMapping::into_account_id(self.context().caller) |
| 23 | + } |
| 24 | + |
| 25 | + fn try_convert_apparent_value<R>(&self) -> EvmResult<U256> |
| 26 | + where |
| 27 | + R: pallet_evm::Config, |
| 28 | + { |
| 29 | + let amount = self.context().apparent_value; |
| 30 | + <R as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount).ok_or( |
| 31 | + PrecompileFailure::Error { |
| 32 | + exit_status: ExitError::Other( |
| 33 | + "error converting balance from ETH to subtensor".into(), |
| 34 | + ), |
| 35 | + }, |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + /// Dispatches a runtime call, but also checks and records the gas costs. |
| 40 | + fn try_dispatch_runtime_call<R, Call>( |
| 41 | + &mut self, |
| 42 | + call: Call, |
| 43 | + origin: RawOrigin<R::AccountId>, |
| 44 | + ) -> EvmResult<()> |
| 45 | + where |
| 46 | + R: frame_system::Config + pallet_evm::Config, |
| 47 | + R::RuntimeCall: From<Call>, |
| 48 | + R::RuntimeCall: GetDispatchInfo + Dispatchable<PostInfo = PostDispatchInfo>, |
| 49 | + R::RuntimeOrigin: From<RawOrigin<R::AccountId>>, |
| 50 | + { |
| 51 | + let call = R::RuntimeCall::from(call); |
| 52 | + let info = GetDispatchInfo::get_dispatch_info(&call); |
| 53 | + |
| 54 | + let target_gas = self.gas_limit(); |
| 55 | + if let Some(gas) = target_gas { |
| 56 | + let valid_weight = |
| 57 | + <R as pallet_evm::Config>::GasWeightMapping::gas_to_weight(gas, false).ref_time(); |
| 58 | + if info.weight.ref_time() > valid_weight { |
| 59 | + return Err(PrecompileFailure::Error { |
| 60 | + exit_status: ExitError::OutOfGas, |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + self.record_external_cost( |
| 66 | + Some(info.weight.ref_time()), |
| 67 | + Some(info.weight.proof_size()), |
| 68 | + None, |
| 69 | + )?; |
| 70 | + |
| 71 | + match call.dispatch(R::RuntimeOrigin::from(origin)) { |
| 72 | + Ok(post_info) => { |
| 73 | + if post_info.pays_fee(&info) == Pays::Yes { |
| 74 | + let actual_weight = post_info.actual_weight.unwrap_or(info.weight); |
| 75 | + let cost = |
| 76 | + <R as pallet_evm::Config>::GasWeightMapping::weight_to_gas(actual_weight); |
| 77 | + self.record_cost(cost)?; |
| 78 | + |
| 79 | + self.refund_external_cost( |
| 80 | + Some( |
| 81 | + info.weight |
| 82 | + .ref_time() |
| 83 | + .saturating_sub(actual_weight.ref_time()), |
| 84 | + ), |
| 85 | + Some( |
| 86 | + info.weight |
| 87 | + .proof_size() |
| 88 | + .saturating_sub(actual_weight.proof_size()), |
| 89 | + ), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + log::info!("Dispatch succeeded. Post info: {:?}", post_info); |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | + Err(e) => { |
| 98 | + log::error!("Dispatch failed. Error: {:?}", e); |
| 99 | + log::warn!("Returning error PrecompileFailure::Error"); |
| 100 | + Err(PrecompileFailure::Error { |
| 101 | + exit_status: ExitError::Other( |
| 102 | + format!("dispatch execution failed: {}", <&'static str>::from(e)).into(), |
| 103 | + ), |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<T> PrecompileHandleExt for T where T: PrecompileHandle {} |
| 111 | + |
| 112 | +pub(crate) trait PrecompileExt<AccountId: From<[u8; 32]>> { |
| 113 | + const INDEX: u64; |
| 114 | + |
| 115 | + // ss58 public key i.e., the contract sends funds it received to the destination address from |
| 116 | + // the method parameter. |
| 117 | + fn account_id() -> AccountId { |
| 118 | + let hash = H160::from_low_u64_be(Self::INDEX); |
| 119 | + let prefix = b"evm:"; |
| 120 | + |
| 121 | + // Concatenate prefix and ethereum address |
| 122 | + let mut combined = Vec::new(); |
| 123 | + combined.extend_from_slice(prefix); |
| 124 | + combined.extend_from_slice(hash.as_bytes()); |
| 125 | + |
| 126 | + let hash = blake2_256(&combined); |
| 127 | + |
| 128 | + hash.into() |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(test)] |
| 133 | +mod test { |
| 134 | + use super::*; |
| 135 | + |
| 136 | + use sp_core::crypto::AccountId32; |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn ss58_address_from_index_works() { |
| 140 | + assert_eq!( |
| 141 | + TestPrecompile::account_id(), |
| 142 | + AccountId32::from([ |
| 143 | + 0x3a, 0x86, 0x18, 0xfb, 0xbb, 0x1b, 0xbc, 0x47, 0x86, 0x64, 0xff, 0x53, 0x46, 0x18, |
| 144 | + 0x0c, 0x35, 0xd0, 0x9f, 0xac, 0x26, 0xf2, 0x02, 0x70, 0x85, 0xb3, 0x1c, 0x56, 0xc1, |
| 145 | + 0x06, 0x3c, 0x1c, 0xd3, |
| 146 | + ]) |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + struct TestPrecompile; |
| 151 | + |
| 152 | + impl PrecompileExt<AccountId32> for TestPrecompile { |
| 153 | + const INDEX: u64 = 2051; |
| 154 | + } |
| 155 | +} |
0 commit comments