astar_primitives/
precompiles.rs

1// This file is part of Astar.
2
3// Copyright (C) Stake Technologies Pte.Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6// Astar is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// Astar is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with Astar. If not, see <http://www.gnu.org/licenses/>.
18
19use core::marker::PhantomData;
20
21use fp_evm::{ExitError, PrecompileFailure};
22use frame_support::{
23    dispatch::{DispatchClass, GetDispatchInfo, Pays},
24    traits::Contains,
25};
26use pallet_evm_precompile_dispatch::DispatchValidateT;
27
28/// Struct that allows only calls based on `Filter` to pass through.
29pub struct DispatchFilterValidate<RuntimeCall, Filter: Contains<RuntimeCall>>(
30    PhantomData<(RuntimeCall, Filter)>,
31);
32
33impl<AccountId, RuntimeCall: GetDispatchInfo, Filter: Contains<RuntimeCall>>
34    DispatchValidateT<AccountId, RuntimeCall> for DispatchFilterValidate<RuntimeCall, Filter>
35{
36    fn validate_before_dispatch(
37        _origin: &AccountId,
38        call: &RuntimeCall,
39    ) -> Option<PrecompileFailure> {
40        let info = call.get_dispatch_info();
41        let paid_normal_call = info.pays_fee == Pays::Yes && info.class == DispatchClass::Normal;
42        if !paid_normal_call {
43            return Some(PrecompileFailure::Error {
44                exit_status: ExitError::Other("invalid call".into()),
45            });
46        }
47        if Filter::contains(call) {
48            None
49        } else {
50            Some(PrecompileFailure::Error {
51                exit_status: ExitError::Other("call filtered out".into()),
52            })
53        }
54    }
55}