precompile_utils/testing/
mod.rs

1// This file is part of Frontier.
2
3// Copyright (c) Moonsong Labs.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0
6
7// Licensed under the Apache License, Version 2.0 (the "License");
8// you may not use this file except in compliance with the License.
9// You may obtain a copy of the License at
10//
11// 	http://www.apache.org/licenses/LICENSE-2.0
12//
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16// See the License for the specific language governing permissions and
17// limitations under the License.
18
19pub mod account;
20pub mod execution;
21pub mod handle;
22pub mod modifier;
23mod solidity;
24
25pub use account::*;
26pub use execution::*;
27pub use handle::*;
28pub use modifier::*;
29pub use solidity::{check_precompile_implements_solidity_interfaces, compute_selector};
30
31use fp_evm::Log;
32
33pub fn decode_revert_message(encoded: &[u8]) -> &[u8] {
34	let encoded_len = encoded.len();
35	// selector 4 + offset 32 + string length 32
36	if encoded_len > 68 {
37		let message_len = encoded[36..68].iter().sum::<u8>();
38		if encoded_len >= 68 + message_len as usize {
39			return &encoded[68..68 + message_len as usize];
40		}
41	}
42	b"decode_revert_message: error"
43}
44
45#[derive(Clone, PartialEq, Eq)]
46pub struct PrettyLog(Log);
47
48impl core::fmt::Debug for PrettyLog {
49	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
50		let bytes = self
51			.0
52			.data
53			.iter()
54			.map(|b| format!("{b:02X}"))
55			.collect::<Vec<String>>()
56			.join("");
57
58		let message = String::from_utf8(self.0.data.clone()).ok();
59
60		f.debug_struct("Log")
61			.field("address", &self.0.address)
62			.field("topics", &self.0.topics)
63			.field("data", &bytes)
64			.field("data_utf8", &message)
65			.finish()
66	}
67}
68
69/// Panics if an event is not found in the system log of events
70#[macro_export]
71macro_rules! assert_event_emitted {
72	($event:expr) => {
73		match &$event {
74			e => {
75				assert!(
76					$crate::mock::events().iter().find(|x| *x == e).is_some(),
77					"Event {:?} was not found in events: \n {:?}",
78					e,
79					$crate::mock::events()
80				);
81			}
82		}
83	};
84}
85
86// Panics if an event is found in the system log of events
87#[macro_export]
88macro_rules! assert_event_not_emitted {
89	($event:expr) => {
90		match &$event {
91			e => {
92				assert!(
93					$crate::mock::events().iter().find(|x| *x == e).is_none(),
94					"Event {:?} was found in events: \n {:?}",
95					e,
96					$crate::mock::events()
97				);
98			}
99		}
100	};
101}