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
// 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/>.

//! The Local Network EVM precompiles. This can be compiled with ``#[no_std]`, ready for Wasm.

use crate::{RuntimeCall, UnifiedAccounts};
use astar_primitives::precompiles::DispatchFilterValidate;
use frame_support::traits::ConstU32;
use frame_support::{parameter_types, traits::Contains};
use pallet_evm_precompile_assets_erc20::Erc20AssetsPrecompileSet;
use pallet_evm_precompile_blake2::Blake2F;
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
use pallet_evm_precompile_dapp_staking::DappStakingV3Precompile;
use pallet_evm_precompile_dispatch::Dispatch;
use pallet_evm_precompile_dispatch_lockdrop::DispatchLockdrop;
use pallet_evm_precompile_ed25519::Ed25519Verify;
use pallet_evm_precompile_modexp::Modexp;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
use pallet_evm_precompile_sr25519::Sr25519Precompile;
use pallet_evm_precompile_substrate_ecdsa::SubstrateEcdsaPrecompile;
use pallet_evm_precompile_unified_accounts::UnifiedAccountsPrecompile;
use precompile_utils::precompile_set::*;
use sp_std::fmt::Debug;

/// The asset precompile address prefix. Addresses that match against this prefix will be routed
/// to Erc20AssetsPrecompileSet
pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
parameter_types! {
    pub AssetPrefix: &'static [u8] = ASSET_PRECOMPILE_ADDRESS_PREFIX;
}

/// Precompile checks for ethereum spec precompiles
/// We allow DELEGATECALL to stay compliant with Ethereum behavior.
type EthereumPrecompilesChecks = (AcceptDelegateCall, CallableByContract, CallableByPrecompile);

/// Filter that only allows whitelisted runtime call to pass through dispatch precompile
pub struct WhitelistedCalls;

impl Contains<RuntimeCall> for WhitelistedCalls {
    fn contains(t: &RuntimeCall) -> bool {
        match t {
            RuntimeCall::Utility(pallet_utility::Call::batch { calls })
            | RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => {
                calls.iter().all(|call| WhitelistedCalls::contains(call))
            }
            RuntimeCall::DappStaking(_) => true,
            RuntimeCall::Assets(pallet_assets::Call::transfer { .. }) => true,
            RuntimeCall::Democracy(_)
            | RuntimeCall::Treasury(_)
            | RuntimeCall::CommunityTreasury(_)
            | RuntimeCall::Preimage(_) => true,
            _ => false,
        }
    }
}

/// Filter that only allows whitelisted runtime call to pass through dispatch-lockdrop precompile
pub struct WhitelistedLockdropCalls;

impl Contains<RuntimeCall> for WhitelistedLockdropCalls {
    fn contains(t: &RuntimeCall) -> bool {
        match t {
            RuntimeCall::Utility(pallet_utility::Call::batch { calls })
            | RuntimeCall::Utility(pallet_utility::Call::batch_all { calls }) => calls
                .iter()
                .all(|call| WhitelistedLockdropCalls::contains(call)),
            RuntimeCall::DappStaking(pallet_dapp_staking::Call::unbond_and_unstake { .. }) => true,
            RuntimeCall::DappStaking(pallet_dapp_staking::Call::withdraw_unbonded { .. }) => true,
            RuntimeCall::Balances(pallet_balances::Call::transfer_all { .. }) => true,
            RuntimeCall::Balances(pallet_balances::Call::transfer_keep_alive { .. }) => true,
            RuntimeCall::Balances(pallet_balances::Call::transfer_allow_death { .. }) => true,
            RuntimeCall::Assets(pallet_assets::Call::transfer { .. }) => true,
            _ => false,
        }
    }
}

/// The PrecompileSet installed in the Local runtime.
#[precompile_utils::precompile_name_from_address]
pub type LocalPrecompilesSetAt<R> = (
    // Ethereum precompiles:
    // We allow DELEGATECALL to stay compliant with Ethereum behavior.
    PrecompileAt<AddressU64<1>, ECRecover, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<2>, Sha256, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<3>, Ripemd160, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<4>, Identity, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<5>, Modexp, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<6>, Bn128Add, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<7>, Bn128Mul, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<8>, Bn128Pairing, EthereumPrecompilesChecks>,
    PrecompileAt<AddressU64<9>, Blake2F, EthereumPrecompilesChecks>,
    // Non-Local specific nor Ethereum precompiles :
    PrecompileAt<AddressU64<1024>, Sha3FIPS256, (CallableByContract, CallableByPrecompile)>,
    PrecompileAt<
        AddressU64<1025>,
        Dispatch<R, DispatchFilterValidate<RuntimeCall, WhitelistedCalls>>,
        // Not callable from smart contract nor precompiles, only EOA accounts
        (),
    >,
    PrecompileAt<AddressU64<1026>, ECRecoverPublicKey, (CallableByContract, CallableByPrecompile)>,
    PrecompileAt<AddressU64<1027>, Ed25519Verify, (CallableByContract, CallableByPrecompile)>,
    // Local specific precompiles:
    PrecompileAt<
        AddressU64<20481>,
        DappStakingV3Precompile<R>,
        (CallableByContract, CallableByPrecompile),
    >,
    PrecompileAt<
        AddressU64<20482>,
        Sr25519Precompile<R>,
        (CallableByContract, CallableByPrecompile),
    >,
    PrecompileAt<
        AddressU64<20483>,
        SubstrateEcdsaPrecompile<R>,
        (CallableByContract, CallableByPrecompile),
    >,
    // skip 20484 for xcm precompile
    // Skipping 20485 to make sure all network have consistent precompiles address
    PrecompileAt<
        AddressU64<20486>,
        UnifiedAccountsPrecompile<R, UnifiedAccounts>,
        (CallableByContract, CallableByPrecompile),
    >,
    PrecompileAt<
        AddressU64<20487>,
        DispatchLockdrop<
            R,
            DispatchFilterValidate<RuntimeCall, WhitelistedLockdropCalls>,
            ConstU32<8>,
        >,
        // Not callable from smart contract nor precompiled, only EOA accounts
        (),
    >,
);

pub type LocalPrecompiles<R> = PrecompileSetBuilder<
    R,
    (
        // Skip precompiles if out of range.
        PrecompilesInRangeInclusive<
            // We take range as last precompile index, UPDATE this once new precompile is added
            (AddressU64<1>, AddressU64<20487>),
            LocalPrecompilesSetAt<R>,
        >,
        // Prefixed precompile sets (XC20)
        PrecompileSetStartingWith<AssetPrefix, Erc20AssetsPrecompileSet<R>, CallableByContract>,
    ),
>;