1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// This file is part of Astar.

// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

//! # Ethereum Checked Pallet
//!
//! ## Overview
//!
//! A `pallet-ethereum like pallet that execute transactions from checked source,
//! like XCM remote call. Only `Call` transactions are supported
//! (no `Create`).
//!
//! The checked source guarantees that transactions are valid with prior checks, so these
//! transactions are not required to include valid signatures. Instead, `pallet-ethereum-checked`
//! will add the same dummy signature to them. To avoid transaction hash collisions, a global
//! nonce shared with all users are used.
//!
//! ## Interface
//!
//! ### Dispatch-able calls
//!
//! - `transact`: transact an Ethereum transaction. Similar to `pallet_ethereum::Transact`,
//! but is only for XCM remote call.
//!

#![cfg_attr(not(feature = "std"), no_std)]

use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

use ethereum_types::{H160, U256};
use fp_ethereum::{TransactionData, ValidatedTransaction};
use fp_evm::{
    CallInfo, CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, ExitReason,
    ExitSucceed, TransactionValidationError,
};
use pallet_evm::GasWeightMapping;

use frame_support::{
    dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo},
    pallet_prelude::*,
};
use frame_system::pallet_prelude::*;
#[cfg(feature = "runtime-benchmarks")]
use sp_runtime::traits::TrailingZeroInput;
use sp_runtime::traits::UniqueSaturatedInto;
use sp_std::{marker::PhantomData, result::Result};

use astar_primitives::{ethereum_checked::CheckedEthereumTx, evm::UnifiedAddressMapper};

pub use pallet::*;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

// TODO: after integrated into Astar/Shiden runtime, redo benchmarking with them.
// The reason is that `EVMChainId` storage read only happens in Shibuya
pub mod weights;
pub use weights::WeightInfo;

mod mock;
mod tests;

pub type WeightInfoOf<T> = <T as Config>::WeightInfo;

/// Origin for dispatch-able calls.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum RawOrigin<AccountId> {
    XcmEthereumTx(AccountId),
}

/// Ensure the origin is with XCM calls.
pub struct EnsureXcmEthereumTx<AccountId>(PhantomData<AccountId>);
impl<O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>, AccountId: Decode>
    EnsureOrigin<O> for EnsureXcmEthereumTx<AccountId>
{
    type Success = AccountId;

    fn try_origin(o: O) -> Result<Self::Success, O> {
        o.into().map(|o| match o {
            RawOrigin::XcmEthereumTx(account_id) => account_id,
        })
    }

    #[cfg(feature = "runtime-benchmarks")]
    fn try_successful_origin() -> Result<O, ()> {
        let zero_account_id =
            AccountId::decode(&mut TrailingZeroInput::zeroes()).map_err(|_| ())?;
        Ok(O::from(RawOrigin::XcmEthereumTx(zero_account_id)))
    }
}

/// Transaction kind.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub enum CheckedEthereumTxKind {
    /// The tx is from XCM remote call.
    Xcm,
}

#[frame_support::pallet]
pub mod pallet {
    use super::*;

    #[pallet::pallet]
    pub struct Pallet<T>(PhantomData<T>);

    #[pallet::config]
    pub trait Config: frame_system::Config + pallet_evm::Config {
        /// Reserved Xcmp weight for block gas limit calculation.
        type ReservedXcmpWeight: Get<Weight>;

        /// Invalid tx error.
        type InvalidEvmTransactionError: From<TransactionValidationError>;

        /// Validated tx execution.
        type ValidatedTransaction: ValidatedTransaction;

        /// Account mapping.
        type AddressMapper: UnifiedAddressMapper<Self::AccountId>;

        /// Origin for `transact` call.
        type XcmTransactOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Self::AccountId>;

        /// Weight information for extrinsics in this pallet.
        type WeightInfo: WeightInfo;
    }

    #[pallet::origin]
    pub type Origin<T> = RawOrigin<<T as frame_system::Config>::AccountId>;

    /// Global nonce for all transactions to avoid hash collision, which is
    /// caused by the same dummy signatures for all transactions.
    #[pallet::storage]
    pub type Nonce<T: Config> = StorageValue<_, U256, ValueQuery>;

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Transact an Ethereum transaction. Similar to `pallet_ethereum::Transact`,
        /// but is only for XCM remote call.
        #[pallet::call_index(0)]
        #[pallet::weight({
            let weight_limit = T::GasWeightMapping::gas_to_weight(tx.gas_limit.unique_saturated_into(), false);
            weight_limit.saturating_add(WeightInfoOf::<T>::transact_without_apply())
        })]
        pub fn transact(origin: OriginFor<T>, tx: CheckedEthereumTx) -> DispatchResultWithPostInfo {
            let source = T::XcmTransactOrigin::ensure_origin(origin)?;
            Self::do_transact(
                T::AddressMapper::to_h160_or_default(&source).into_address(),
                tx.into(),
                CheckedEthereumTxKind::Xcm,
                false,
            )
            .map(|(post_info, _)| post_info)
        }
    }
}

impl<T: Config> Pallet<T> {
    /// Validate and execute the checked tx. Only `Call` transaction action is allowed.
    fn do_transact(
        source: H160,
        checked_tx: CheckedEthereumTx,
        tx_kind: CheckedEthereumTxKind,
        skip_apply: bool,
    ) -> Result<(PostDispatchInfo, CallInfo), DispatchErrorWithPostInfo> {
        let chain_id = T::ChainId::get();
        let nonce = Nonce::<T>::get();
        let tx = checked_tx.into_ethereum_tx(Nonce::<T>::get(), chain_id);
        let tx_data: TransactionData = (&tx).into();

        let (weight_limit, proof_size_base_cost) =
            match <T as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
                tx_data.gas_limit.unique_saturated_into(),
                true,
            ) {
                weight_limit if weight_limit.proof_size() > 0 => (
                    Some(weight_limit),
                    // measured PoV should be correct to use here
                    Some(WeightInfoOf::<T>::transact_without_apply().proof_size()),
                ),
                _ => (None, None),
            };

        // Validate the tx.
        let _ = CheckEvmTransaction::<T::InvalidEvmTransactionError>::new(
            CheckEvmTransactionConfig {
                evm_config: T::config(),
                block_gas_limit: U256::from(Self::block_gas_limit(&tx_kind)),
                base_fee: U256::zero(),
                chain_id,
                is_transactional: true,
            },
            tx_data.into(),
            weight_limit,
            proof_size_base_cost,
        )
        // Gas limit validation. The fee payment has been validated as the tx is `checked`.
        .validate_common()
        .map_err(|_| DispatchErrorWithPostInfo {
            post_info: PostDispatchInfo {
                // actual_weight = overhead - nonce_write_1
                actual_weight: Some(
                    WeightInfoOf::<T>::transact_without_apply()
                        .saturating_sub(T::DbWeight::get().writes(1)),
                ),
                pays_fee: Pays::Yes,
            },
            error: DispatchError::Other("Failed to validate Ethereum tx"),
        })?;

        Nonce::<T>::put(nonce.saturating_add(U256::one()));

        if skip_apply {
            return Ok((
                PostDispatchInfo {
                    actual_weight: Some(WeightInfoOf::<T>::transact_without_apply()),
                    pays_fee: Pays::Yes,
                },
                CallInfo {
                    exit_reason: ExitReason::Succeed(ExitSucceed::Returned),
                    value: Default::default(),
                    used_gas: fp_evm::UsedGas {
                        standard: checked_tx.gas_limit,
                        effective: checked_tx.gas_limit,
                    },
                    weight_info: None,
                    logs: Default::default(),
                },
            ));
        }

        // Execute the tx.
        let (post_info, apply_info) = T::ValidatedTransaction::apply(source, tx)?;
        match apply_info {
            CallOrCreateInfo::Call(info) => Ok((post_info, info)),
            // It is not possible to have a `Create` transaction via `CheckedEthereumTx`.
            CallOrCreateInfo::Create(_) => {
                unreachable!("Cannot create a 'Create' transaction; qed")
            }
        }
    }

    /// Block gas limit calculation based on the tx kind.
    fn block_gas_limit(tx_kind: &CheckedEthereumTxKind) -> u64 {
        let weight_limit = match tx_kind {
            CheckedEthereumTxKind::Xcm => T::ReservedXcmpWeight::get(),
        };
        T::GasWeightMapping::weight_to_gas(weight_limit)
    }

    /// Similar to `transact` dispatch-able call that transacts an Ethereum transaction,
    /// but not to apply it. This is to benchmark the weight overhead in addition to `gas_limit`.
    #[cfg(feature = "runtime-benchmarks")]
    pub fn transact_without_apply(
        origin: OriginFor<T>,
        tx: CheckedEthereumTx,
    ) -> DispatchResultWithPostInfo {
        let source = T::XcmTransactOrigin::ensure_origin(origin)?;
        Self::do_transact(
            T::AddressMapper::to_h160_or_default(&source).into_address(),
            tx.into(),
            CheckedEthereumTxKind::Xcm,
            true,
        )
        .map(|(post_info, _)| post_info)
    }
}