pallet_evm_precompile_preimage/
lib.rs1#![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
37pub(crate) const SELECTOR_LOG_PREIMAGE_NOTED: [u8; 32] = keccak256!("PreimageNoted(bytes32)");
39
40pub(crate) const SELECTOR_LOG_PREIMAGE_UNNOTED: [u8; 32] = keccak256!("PreimageUnnoted(bytes32)");
42
43pub 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 #[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 #[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}