shiden_runtime/
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
19//! The Shiden Network EVM precompiles. This can be compiled with ``#[no_std]`, ready for Wasm.
20
21use crate::{Runtime, RuntimeCall};
22use astar_primitives::precompiles::DispatchFilterValidate;
23use frame_support::pallet_prelude::ConstU32;
24use frame_support::{parameter_types, traits::Contains};
25use pallet_evm_precompile_assets_erc20::Erc20AssetsPrecompileSet;
26use pallet_evm_precompile_blake2::Blake2F;
27use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
28use pallet_evm_precompile_dapp_staking::DappStakingV3Precompile;
29use pallet_evm_precompile_dispatch::Dispatch;
30use pallet_evm_precompile_dispatch_lockdrop::DispatchLockdrop;
31use pallet_evm_precompile_ed25519::Ed25519Verify;
32use pallet_evm_precompile_modexp::Modexp;
33use pallet_evm_precompile_sha3fips::Sha3FIPS256;
34use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
35use pallet_evm_precompile_sr25519::Sr25519Precompile;
36use pallet_evm_precompile_substrate_ecdsa::SubstrateEcdsaPrecompile;
37use pallet_evm_precompile_xcm::XcmPrecompile;
38use precompile_utils::precompile_set::*;
39use sp_std::fmt::Debug;
40
41/// The asset precompile address prefix. Addresses that match against this prefix will be routed
42/// to Erc20AssetsPrecompileSet
43pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
44parameter_types! {
45    pub AssetPrefix: &'static [u8] = ASSET_PRECOMPILE_ADDRESS_PREFIX;
46}
47
48/// Precompile checks for ethereum spec precompiles
49/// We allow DELEGATECALL to stay compliant with Ethereum behavior.
50type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);
51
52/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
53pub struct WhitelistedCalls;
54
55impl Contains<RuntimeCall> for WhitelistedCalls {
56    fn contains(t: &RuntimeCall) -> bool {
57        match t {
58            RuntimeCall::Utility(pallet_utility::Call::batch { calls })
59            | RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
60                calls.iter().all(|call| WhitelistedCalls::contains(call))
61            }
62            RuntimeCall::DappStaking(_) => true,
63            RuntimeCall::Assets(pallet_assets::Call::transfer { .. }) => true,
64            _ => false,
65        }
66    }
67}
68/// Filter that only allows whitelisted runtime call to pass through dispatch-lockdrop precompile
69pub struct WhitelistedLockdropCalls;
70
71impl Contains<RuntimeCall> for WhitelistedLockdropCalls {
72    fn contains(t: &RuntimeCall) -> bool {
73        match t {
74            RuntimeCall::Utility(pallet_utility::Call::batch { calls })
75            | RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => calls
76                .iter()
77                .all(|call| WhitelistedLockdropCalls::contains(call)),
78            RuntimeCall::DappStaking(pallet_dapp_staking::Call::unbond_and_unstake { .. }) => true,
79            RuntimeCall::DappStaking(pallet_dapp_staking::Call::withdraw_unbonded { .. }) => true,
80            RuntimeCall::Balances(pallet_balances::Call::transfer_all { .. }) => true,
81            RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive { .. }) => true,
82            RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death { .. }) => true,
83            RuntimeCall::Assets(pallet_assets::Call::transfer { .. }) => true,
84            _ => false,
85        }
86    }
87}
88
89/// The PrecompileSet installed in the Shiden runtime.
90#[precompile_utils::precompile_name_from_address]
91pub type ShidenPrecompilesSetAt<R, C> = (
92    // Ethereum precompiles:
93    // We allow DELEGATECALL to stay compliant with Ethereum behavior.
94    PrecompileAt<AddressU64<1>, ECRecover, EthereumPrecompilesChecks>,
95    PrecompileAt<AddressU64<2>, Sha256, EthereumPrecompilesChecks>,
96    PrecompileAt<AddressU64<3>, Ripemd160, EthereumPrecompilesChecks>,
97    PrecompileAt<AddressU64<4>, Identity, EthereumPrecompilesChecks>,
98    PrecompileAt<AddressU64<5>, Modexp, EthereumPrecompilesChecks>,
99    PrecompileAt<AddressU64<6>, Bn128Add, EthereumPrecompilesChecks>,
100    PrecompileAt<AddressU64<7>, Bn128Mul, EthereumPrecompilesChecks>,
101    PrecompileAt<AddressU64<8>, Bn128Pairing, EthereumPrecompilesChecks>,
102    PrecompileAt<AddressU64<9>, Blake2F, EthereumPrecompilesChecks>,
103    // Non-Astar specific nor Ethereum precompiles :
104    PrecompileAt<
105        AddressU64<1024>,
106        Sha3FIPS256<Runtime, ()>,
107        (CallableByContract, CallableByPrecompile),
108    >,
109    PrecompileAt<
110        AddressU64<1025>,
111        Dispatch<R, DispatchFilterValidate<RuntimeCall, WhitelistedCalls>>,
112        // Not callable from smart contract nor precompiles, only EOA accounts
113        (),
114    >,
115    PrecompileAt<AddressU64<1026>, ECRecoverPublicKey, (CallableByContract, CallableByPrecompile)>,
116    PrecompileAt<AddressU64<1027>, Ed25519Verify, (CallableByContract, CallableByPrecompile)>,
117    // Astar specific precompiles:
118    PrecompileAt<
119        AddressU64<20481>,
120        DappStakingV3Precompile<R>,
121        (CallableByContract, CallableByPrecompile),
122    >,
123    PrecompileAt<
124        AddressU64<20482>,
125        Sr25519Precompile<R>,
126        (CallableByContract, CallableByPrecompile),
127    >,
128    PrecompileAt<
129        AddressU64<20483>,
130        SubstrateEcdsaPrecompile<R>,
131        (CallableByContract, CallableByPrecompile),
132    >,
133    PrecompileAt<
134        AddressU64<20484>,
135        XcmPrecompile<R, C>,
136        (
137            SubcallWithMaxNesting<1>,
138            CallableByContract,
139            CallableByPrecompile,
140        ),
141    >,
142    // Skipping 20485 and 20486 to make sure all network have consistent
143    // precompiles address
144    PrecompileAt<
145        AddressU64<20487>,
146        DispatchLockdrop<
147            R,
148            DispatchFilterValidate<RuntimeCall, WhitelistedLockdropCalls>,
149            ConstU32<8>,
150        >,
151        // Not callable from smart contract nor precompiled, only EOA accounts
152        (),
153    >,
154);
155
156pub type ShidenPrecompiles<R, C> = PrecompileSetBuilder<
157    R,
158    (
159        // Skip precompiles if out of range.
160        PrecompilesInRangeInclusive<
161            // We take range as last precompile index, UPDATE this once new prcompile is added
162            (AddressU64<1>, AddressU64<20487>),
163            ShidenPrecompilesSetAt<R, C>,
164        >,
165        // Prefixed precompile sets (XC20)
166        PrecompileSetStartingWith<AssetPrefix, Erc20AssetsPrecompileSet<R>, CallableByContract>,
167    ),
168>;