pallet_evm_precompile_preimage/
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
19use fp_evm::PrecompileHandle;
20use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
21use frame_support::traits::ConstU32;
22use pallet_evm::AddressMapping;
23use pallet_preimage::Call as PreimageCall;
24use precompile_utils::prelude::*;
25use sp_core::{Hasher, H256};
26use sp_runtime::traits::Dispatchable;
27use sp_std::{marker::PhantomData, vec::Vec};
28
29#[cfg(test)]
30mod mock;
31#[cfg(test)]
32mod tests;
33
34pub const ENCODED_PROPOSAL_SIZE_LIMIT: u32 = 2u32.pow(16);
35type GetEncodedProposalSizeLimit = ConstU32<ENCODED_PROPOSAL_SIZE_LIMIT>;
36
37/// Solidity selector of the PreimageNoted log, which is the Keccak of the Log signature.
38pub(crate) const SELECTOR_LOG_PREIMAGE_NOTED: [u8; 32] = keccak256!("PreimageNoted(bytes32)");
39
40/// Solidity selector of the PreimageUnnoted log, which is the Keccak of the Log signature.
41pub(crate) const SELECTOR_LOG_PREIMAGE_UNNOTED: [u8; 32] = keccak256!("PreimageUnnoted(bytes32)");
42
43/// A precompile to wrap the functionality from pallet-preimage.
44pub struct PreimagePrecompile<Runtime>(PhantomData<Runtime>);
45
46#[precompile_utils::precompile]
47impl<Runtime> PreimagePrecompile<Runtime>
48where
49	Runtime: pallet_preimage::Config + pallet_evm::Config + frame_system::Config,
50	<Runtime as frame_system::Config>::Hash: TryFrom<H256> + Into<H256>,
51	<Runtime as frame_system::Config>::RuntimeCall:
52		Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
53	<<Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
54		From<Option<Runtime::AccountId>>,
55	<Runtime as frame_system::Config>::Hash: Into<H256>,
56	<Runtime as frame_system::Config>::RuntimeCall: From<PreimageCall<Runtime>>,
57	<Runtime as pallet_evm::Config>::AddressMapping: AddressMapping<Runtime::AccountId>,
58{
59	/// Register a preimage on-chain.
60	///
61	/// Parameters:
62	/// * encoded_proposal: The preimage registered on-chain
63	#[precompile::public("notePreimage(bytes)")]
64	fn note_preimage(
65		handle: &mut impl PrecompileHandle,
66		encoded_proposal: BoundedBytes<GetEncodedProposalSizeLimit>,
67	) -> EvmResult<H256> {
68		let bytes: Vec<u8> = encoded_proposal.into();
69		let hash: H256 = Runtime::Hashing::hash(&bytes).into();
70
71		let event = log1(
72			handle.context().address,
73			SELECTOR_LOG_PREIMAGE_NOTED,
74			solidity::encode_arguments(H256::from(hash)),
75		);
76		handle.record_log_costs(&[&event])?;
77		let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
78
79		let call = PreimageCall::<Runtime>::note_preimage { bytes }.into();
80
81		<RuntimeHelper<Runtime>>::try_dispatch(handle, Some(origin).into(), call, 0)?;
82
83		event.record(handle)?;
84		Ok(hash)
85	}
86
87	/// Clear an unrequested preimage from the runtime storage.
88	///
89	/// Parameters:
90	/// * hash: The preimage cleared from storage
91	#[precompile::public("unnotePreimage(bytes32)")]
92	fn unnote_preimage(handle: &mut impl PrecompileHandle, hash: H256) -> EvmResult {
93		let event = log1(
94			handle.context().address,
95			SELECTOR_LOG_PREIMAGE_UNNOTED,
96			solidity::encode_arguments(H256::from(hash)),
97		);
98		handle.record_log_costs(&[&event])?;
99
100		let hash: Runtime::Hash = hash
101			.try_into()
102			.map_err(|_| RevertReason::custom("H256 is Runtime::Hash").in_field("hash"))?;
103		let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
104
105		let call = PreimageCall::<Runtime>::unnote_preimage { hash }.into();
106
107		<RuntimeHelper<Runtime>>::try_dispatch(handle, Some(origin).into(), call, 0)?;
108
109		event.record(handle)?;
110
111		Ok(())
112	}
113}