pallet_evm_precompile_registry/lib.rs
1// Copyright 2019-2025 PureStake Inc.
2// This file is part of Moonbeam.
3
4// Moonbeam is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Moonbeam is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.
16
17#![cfg_attr(not(feature = "std"), no_std)]
18
19#[cfg(test)]
20mod mock;
21#[cfg(test)]
22mod tests;
23
24use core::marker::PhantomData;
25use fp_evm::{ExitError, IsPrecompileResult, PrecompileFailure};
26use precompile_utils::{
27 precompile_set::{is_precompile_or_fail, IsActivePrecompile},
28 prelude::*,
29};
30use sp_core::Get;
31
32const DUMMY_CODE: [u8; 5] = [0x60, 0x00, 0x60, 0x00, 0xfd];
33
34pub struct PrecompileRegistry<Runtime>(PhantomData<Runtime>);
35
36#[precompile_utils::precompile]
37impl<Runtime> PrecompileRegistry<Runtime>
38where
39 Runtime: pallet_evm::Config,
40 Runtime::PrecompilesType: IsActivePrecompile,
41{
42 #[precompile::public("isPrecompile(address)")]
43 #[precompile::view]
44 fn is_precompile(handle: &mut impl PrecompileHandle, address: Address) -> EvmResult<bool> {
45 // We consider the precompile set is optimized to do at most one storage read.
46 // In the case of moonbeam, the storage item that can be read is pallet_asset::Asset
47 // (TODO make it more generic, maybe add a const generic on PrecompileRegistry type)
48 // Storage item: Asset:
49 // Blake2_128(16) + AssetId(16) + AssetDetails((4 * AccountId(20)) + (3 * Balance(16)) + 15)
50 handle.record_db_read::<Runtime>(175)?;
51 is_precompile_or_fail::<Runtime>(address.0, handle.remaining_gas())
52 }
53
54 #[precompile::public("isActivePrecompile(address)")]
55 #[precompile::view]
56 fn is_active_precompile(
57 handle: &mut impl PrecompileHandle,
58 address: Address,
59 ) -> EvmResult<bool> {
60 // We consider the precompile set is optimized to do at most one storage read.
61 // In the case of moonbeam, the storage item that can be read is pallet_asset::Asset
62 // (TODO make it more generic, maybe add a const generic on PrecompileRegistry type)
63 // Storage item: Asset:
64 // Blake2_128(16) + AssetId(16) + AssetDetails((4 * AccountId(20)) + (3 * Balance(16)) + 15)
65 handle.record_db_read::<Runtime>(175)?;
66 match <Runtime::PrecompilesValue>::get()
67 .is_active_precompile(address.0, handle.remaining_gas())
68 {
69 IsPrecompileResult::Answer { is_precompile, .. } => Ok(is_precompile),
70 IsPrecompileResult::OutOfGas => Err(PrecompileFailure::Error {
71 exit_status: ExitError::OutOfGas,
72 }),
73 }
74 }
75
76 #[precompile::public("updateAccountCode(address)")]
77 fn update_account_code(handle: &mut impl PrecompileHandle, address: Address) -> EvmResult<()> {
78 // Prevent touching addresses that are not precompiles.
79 //
80 // We consider the precompile set is optimized to do at most one storage read.
81 // In the case of moonbeam, the storage item that can be read is pallet_asset::Asset
82 // (TODO make it more generic, maybe add a const generic on PrecompileRegistry type)
83 // Storage item: Asset:
84 // Blake2_128(16) + AssetId(16) + AssetDetails((4 * AccountId(20)) + (3 * Balance(16)) + 15)
85 handle.record_db_read::<Runtime>(175)?;
86 if !is_precompile_or_fail::<Runtime>(address.0, handle.remaining_gas())? {
87 return Err(revert("provided address is not a precompile"));
88 }
89
90 // pallet_evm::create_account read storage item pallet_evm::AccountCodes
91 //
92 // AccountCodes: Blake2128(16) + H160(20) + Vec(5)
93 // We asume an existing precompile can hold at most 5 bytes worth of dummy code.
94 handle.record_db_read::<Runtime>(41)?;
95 pallet_evm::Pallet::<Runtime>::create_account(address.0, DUMMY_CODE.to_vec(), None)
96 .map_err(|e| PrecompileFailure::Error { exit_status: e })?;
97
98 Ok(())
99 }
100}