sp_evm_tracker/
lib.rs

1//! Inherents for EVM tracker
2#![cfg_attr(not(feature = "std"), no_std)]
3
4use domain_runtime_primitives::{maximum_domain_block_weight, Balance, EthereumAccountId};
5use frame_support::parameter_types;
6use frame_support::sp_runtime::app_crypto::sp_core::U256;
7use frame_support::sp_runtime::Perbill;
8use parity_scale_codec::{Decode, Encode};
9use sp_domains::PermissionedActionAllowedBy;
10#[cfg(feature = "std")]
11use sp_inherents::{Error, InherentData};
12use sp_inherents::{InherentIdentifier, IsFatalError};
13use sp_weights::constants::WEIGHT_REF_TIME_PER_SECOND;
14use sp_weights::Weight;
15
16/// Current approximation of the gas/s consumption considering
17/// EVM execution over compiled WASM (on 4.4Ghz CPU).
18pub const GAS_PER_SECOND: u64 = 40_000_000;
19
20/// Approximate ratio of the amount of Weight per Gas.
21/// u64 works for approximations because Weight is a very small unit compared to gas.
22pub const WEIGHT_PER_GAS: u64 = WEIGHT_REF_TIME_PER_SECOND.div_ceil(GAS_PER_SECOND);
23
24parameter_types! {
25    pub const GasLimitPovSizeRatio: u64 = 4;
26    /// Gas per byte
27    /// Ethereum’s Yellow Paper states that it costs 20,000 gas to store one 256-bit word.
28    /// 1 Byte costs 20_000/32 = 625
29    pub const GasPerByte: Balance = 625;
30    /// Proportion of final (gas_price * gas_used) given as storage fee.
31    pub const StorageFeeRatio: Perbill = Perbill::from_percent(30);
32    /// EVM block gas limit is set to maximum to allow all the transaction stored on Consensus chain.
33    pub BlockGasLimit: U256 = U256::from(
34        maximum_domain_block_weight().ref_time() / WEIGHT_PER_GAS
35    );
36    pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
37}
38
39/// Executive inherent identifier.
40pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"dmnevmtr";
41
42#[derive(Debug, Encode)]
43#[cfg_attr(feature = "std", derive(Decode))]
44pub enum InherentError {
45    MissingRuntimeCall,
46    InvalidRuntimeCall,
47    IncorrectRuntimeCall,
48}
49
50impl IsFatalError for InherentError {
51    fn is_fatal_error(&self) -> bool {
52        true
53    }
54}
55
56/// The type of the Subspace inherent data.
57#[derive(Debug, Encode, Decode)]
58pub struct InherentType {
59    /// EVM tracker "set contract creation allowed by" call
60    pub maybe_call: Option<PermissionedActionAllowedBy<EthereumAccountId>>,
61}
62
63/// Provides the set code inherent data.
64#[cfg(feature = "std")]
65pub struct InherentDataProvider {
66    data: InherentType,
67}
68
69#[cfg(feature = "std")]
70impl InherentDataProvider {
71    /// Create new inherent data provider from the given `data`.
72    pub fn new(maybe_call: Option<PermissionedActionAllowedBy<EthereumAccountId>>) -> Self {
73        Self {
74            data: InherentType { maybe_call },
75        }
76    }
77
78    /// Returns the `data` of this inherent data provider.
79    pub fn data(&self) -> &InherentType {
80        &self.data
81    }
82}
83
84#[cfg(feature = "std")]
85#[async_trait::async_trait]
86impl sp_inherents::InherentDataProvider for InherentDataProvider {
87    async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
88        inherent_data.put_data(INHERENT_IDENTIFIER, &self.data)
89    }
90
91    async fn try_handle_error(
92        &self,
93        identifier: &InherentIdentifier,
94        error: &[u8],
95    ) -> Option<Result<(), Error>> {
96        if *identifier != INHERENT_IDENTIFIER {
97            return None;
98        }
99
100        let error = InherentError::decode(&mut &*error).ok()?;
101
102        Some(Err(Error::Application(Box::from(format!("{error:?}")))))
103    }
104}
105
106sp_api::decl_runtime_apis! {
107    /// Api to check and verify the evm-tracker extrinsic calls
108    pub trait EvmTrackerApi {
109        /// Returns an encoded extrinsic for domain "set contract creation allowed by" call.
110        fn construct_evm_contract_creation_allowed_by_extrinsic(decoded_argument: PermissionedActionAllowedBy<EthereumAccountId>) -> Block::Extrinsic;
111    }
112}