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
use crate::mock::*;
18
use fp_evm::{ExitRevert, PrecompileFailure};
19
use precompile_utils::{solidity::revert::revert_as_bytes, testing::*};
20

            
21
3
fn precompiles() -> Precompiles<Runtime> {
22
3
	PrecompilesValue::get()
23
3
}
24

            
25
#[test]
26
1
fn contract_disabling_default_value_is_false() {
27
1
	ExtBuilder::default()
28
1
		.with_balances(vec![(Alice.into(), 100_000)])
29
1
		.build()
30
1
		.execute_with(|| {
31
1
			// default should be false
32
1
			assert_eq!(crate::storage::PrecompileEnabled::get(), None);
33
1
			assert_eq!(crate::is_enabled(), false);
34
1
			assert_eq!(
35
1
				crate::ensure_enabled(),
36
1
				Err(PrecompileFailure::Revert {
37
1
					exit_status: ExitRevert::Reverted,
38
1
					output: revert_as_bytes("GMP Precompile is not enabled"),
39
1
				})
40
1
			);
41

            
42
1
			precompiles()
43
1
				.prepare_test(
44
1
					CryptoAlith,
45
1
					Precompile1,
46
1
					PCall::wormhole_transfer_erc20 {
47
1
						wormhole_vaa: Vec::new().into(),
48
1
					},
49
1
				)
50
1
				.execute_reverts(|output| output == b"GMP Precompile is not enabled");
51
1
		})
52
1
}
53

            
54
#[test]
55
1
fn contract_enabling_works() {
56
1
	ExtBuilder::default()
57
1
		.with_balances(vec![(Alice.into(), 100_000)])
58
1
		.build()
59
1
		.execute_with(|| {
60
1
			crate::storage::PrecompileEnabled::set(Some(true));
61
1
			assert_eq!(crate::storage::PrecompileEnabled::get(), Some(true));
62
1
			assert_eq!(crate::is_enabled(), true);
63
1
			assert_eq!(crate::ensure_enabled(), Ok(()));
64

            
65
			// should fail at a later point since contract addresses are not set
66
1
			precompiles()
67
1
				.prepare_test(
68
1
					CryptoAlith,
69
1
					Precompile1,
70
1
					PCall::wormhole_transfer_erc20 {
71
1
						wormhole_vaa: Vec::new().into(),
72
1
					},
73
1
				)
74
1
				.execute_reverts(|output| output == b"invalid wormhole core address");
75
1
		})
76
1
}
77

            
78
#[test]
79
1
fn contract_disabling_works() {
80
1
	ExtBuilder::default()
81
1
		.with_balances(vec![(Alice.into(), 100_000)])
82
1
		.build()
83
1
		.execute_with(|| {
84
1
			crate::storage::PrecompileEnabled::set(Some(false));
85
1
			assert_eq!(crate::storage::PrecompileEnabled::get(), Some(false));
86
1
			assert_eq!(crate::is_enabled(), false);
87
1
			assert_eq!(
88
1
				crate::ensure_enabled(),
89
1
				Err(PrecompileFailure::Revert {
90
1
					exit_status: ExitRevert::Reverted,
91
1
					output: revert_as_bytes("GMP Precompile is not enabled"),
92
1
				})
93
1
			);
94

            
95
1
			precompiles()
96
1
				.prepare_test(
97
1
					CryptoAlith,
98
1
					Precompile1,
99
1
					PCall::wormhole_transfer_erc20 {
100
1
						wormhole_vaa: Vec::new().into(),
101
1
					},
102
1
				)
103
1
				.execute_reverts(|output| output == b"GMP Precompile is not enabled");
104
1
		})
105
1
}
106

            
107
#[test]
108
1
fn test_solidity_interface_has_all_function_selectors_documented_and_implemented() {
109
1
	check_precompile_implements_solidity_interfaces(&["Gmp.sol"], PCall::supports_selector)
110
1
}