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
use crate::mock::*;
17
use crate::*;
18
use precompile_utils::testing::*;
19

            
20
use fp_evm::MAX_TRANSACTION_GAS_LIMIT;
21
use frame_support::assert_ok;
22
use pallet_evm::{Call as EvmCall, Event as EvmEvent};
23

            
24
use sp_core::{Hasher, U256};
25
use sp_runtime::traits::Dispatchable;
26

            
27
2
fn evm_call(input: Vec<u8>) -> EvmCall<Runtime> {
28
2
	EvmCall::call {
29
2
		source: Alice.into(),
30
2
		target: Precompile1.into(),
31
2
		input,
32
2
		value: U256::zero(),
33
2
		gas_limit: MAX_TRANSACTION_GAS_LIMIT.low_u64(),
34
2
		max_fee_per_gas: U256::zero(),
35
2
		max_priority_fee_per_gas: Some(U256::zero()),
36
2
		nonce: None,
37
2
		access_list: Vec::new(),
38
2
		authorization_list: Vec::new(),
39
2
	}
40
2
}
41

            
42
1
fn precompiles() -> Precompiles<Runtime> {
43
1
	PrecompilesValue::get()
44
1
}
45

            
46
#[test]
47
1
fn test_solidity_interface_has_all_function_selectors_documented_and_implemented() {
48
1
	check_precompile_implements_solidity_interfaces(&["Preimage.sol"], PCall::supports_selector)
49
1
}
50

            
51
#[test]
52
1
fn note_unnote_preimage_logs_work() {
53
1
	ExtBuilder::default()
54
1
		.with_balances(vec![(Alice.into(), 100_000)])
55
1
		.build()
56
1
		.execute_with(|| {
57
1
			let bytes = vec![1, 2, 3];
58
1
			let expected_hash = sp_runtime::traits::BlakeTwo256::hash(&bytes);
59

            
60
			// Note preimage
61
1
			let input = PCall::note_preimage {
62
1
				encoded_proposal: bytes.into(),
63
1
			}
64
1
			.into();
65
1
			assert_ok!(RuntimeCall::Evm(evm_call(input)).dispatch(RuntimeOrigin::root()));
66

            
67
			// Assert note preimage event is emitted and matching frame event preimage hash.
68
1
			assert!(vec![
69
1
				EvmEvent::Log {
70
1
					log: log1(
71
1
						Precompile1,
72
1
						SELECTOR_LOG_PREIMAGE_NOTED,
73
1
						solidity::encode_event_data(expected_hash)
74
1
					),
75
1
				}
76
1
				.into(),
77
1
				RuntimeEvent::Preimage(pallet_preimage::pallet::Event::Noted {
78
1
					hash: expected_hash
79
1
				})
80
1
			]
81
1
			.iter()
82
2
			.all(|log| events().contains(log)));
83

            
84
			// Unnote preimage
85
1
			let input = PCall::unnote_preimage {
86
1
				hash: expected_hash,
87
1
			}
88
1
			.into();
89
1
			assert_ok!(RuntimeCall::Evm(evm_call(input)).dispatch(RuntimeOrigin::root()));
90

            
91
			// Assert unnote preimage is emitted
92
1
			assert!(events().contains(
93
1
				&EvmEvent::Log {
94
1
					log: log1(
95
1
						Precompile1,
96
1
						SELECTOR_LOG_PREIMAGE_UNNOTED,
97
1
						solidity::encode_event_data(expected_hash)
98
1
					),
99
1
				}
100
1
				.into()
101
1
			));
102
1
		})
103
1
}
104

            
105
#[test]
106
1
fn note_preimage_returns_preimage_hash() {
107
1
	ExtBuilder::default()
108
1
		.with_balances(vec![(Alice.into(), 40)])
109
1
		.build()
110
1
		.execute_with(|| {
111
1
			let preimage = [1u8; 32];
112
1
			let preimage_hash = <mock::Runtime as frame_system::Config>::Hashing::hash(&preimage);
113

            
114
1
			precompiles()
115
1
				.prepare_test(
116
1
					Alice,
117
1
					Precompile1,
118
1
					PCall::note_preimage {
119
1
						encoded_proposal: BoundedBytes::from(preimage),
120
1
					},
121
				)
122
1
				.execute_returns(preimage_hash);
123
1
		})
124
1
}