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
use fp_evm::PrecompileHandle;
20
use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
21
use frame_support::traits::ConstU32;
22
use pallet_evm::AddressMapping;
23
use pallet_preimage::Call as PreimageCall;
24
use precompile_utils::prelude::*;
25
use sp_core::{Hasher, H256};
26
use sp_runtime::traits::Dispatchable;
27
use sp_std::{marker::PhantomData, vec::Vec};
28

            
29
#[cfg(test)]
30
mod mock;
31
#[cfg(test)]
32
mod tests;
33

            
34
pub const ENCODED_PROPOSAL_SIZE_LIMIT: u32 = 2u32.pow(16);
35
type GetEncodedProposalSizeLimit = ConstU32<ENCODED_PROPOSAL_SIZE_LIMIT>;
36

            
37
/// Solidity selector of the PreimageNoted log, which is the Keccak of the Log signature.
38
pub(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.
41
pub(crate) const SELECTOR_LOG_PREIMAGE_UNNOTED: [u8; 32] = keccak256!("PreimageUnnoted(bytes32)");
42

            
43
/// A precompile to wrap the functionality from pallet-preimage.
44
pub struct PreimagePrecompile<Runtime>(PhantomData<Runtime>);
45

            
46
27
#[precompile_utils::precompile]
47
impl<Runtime> PreimagePrecompile<Runtime>
48
where
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
2
	fn note_preimage(
65
2
		handle: &mut impl PrecompileHandle,
66
2
		encoded_proposal: BoundedBytes<GetEncodedProposalSizeLimit>,
67
2
	) -> EvmResult<H256> {
68
2
		let bytes: Vec<u8> = encoded_proposal.into();
69
2
		let hash: H256 = Runtime::Hashing::hash(&bytes).into();
70
2

            
71
2
		let event = log1(
72
2
			handle.context().address,
73
2
			SELECTOR_LOG_PREIMAGE_NOTED,
74
2
			solidity::encode_arguments(H256::from(hash)),
75
2
		);
76
2
		handle.record_log_costs(&[&event])?;
77
2
		let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
78
2

            
79
2
		let call = PreimageCall::<Runtime>::note_preimage { bytes }.into();
80
2

            
81
2
		<RuntimeHelper<Runtime>>::try_dispatch(handle, Some(origin).into(), call, 0)?;
82

            
83
2
		event.record(handle)?;
84
2
		Ok(hash)
85
2
	}
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
1
	fn unnote_preimage(handle: &mut impl PrecompileHandle, hash: H256) -> EvmResult {
93
1
		let event = log1(
94
1
			handle.context().address,
95
1
			SELECTOR_LOG_PREIMAGE_UNNOTED,
96
1
			solidity::encode_arguments(H256::from(hash)),
97
1
		);
98
1
		handle.record_log_costs(&[&event])?;
99

            
100
1
		let hash: Runtime::Hash = hash
101
1
			.try_into()
102
1
			.map_err(|_| RevertReason::custom("H256 is Runtime::Hash").in_field("hash"))?;
103
1
		let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
104
1

            
105
1
		let call = PreimageCall::<Runtime>::unnote_preimage { hash }.into();
106
1

            
107
1
		<RuntimeHelper<Runtime>>::try_dispatch(handle, Some(origin).into(), call, 0)?;
108

            
109
1
		event.record(handle)?;
110

            
111
1
		Ok(())
112
1
	}
113
}