referrerpolicy=no-referrer-when-downgrade

pallet_revive/evm/tracing/
prestate_tracing.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17use crate::{
18	Code, Config, ExecReturnValue, Key, Pallet, Weight,
19	evm::{Bytes, PrestateTrace, PrestateTraceInfo, PrestateTracerConfig},
20	tracing::Tracing,
21};
22use alloc::{
23	collections::{BTreeMap, BTreeSet},
24	vec::Vec,
25};
26use sp_core::{H160, U256};
27
28/// A tracer that traces the prestate.
29#[derive(frame_support::DefaultNoBound, Debug, Clone, PartialEq)]
30pub struct PrestateTracer<T> {
31	/// The tracer configuration.
32	config: PrestateTracerConfig,
33
34	/// Stack of calls.
35	calls: Vec<H160>,
36
37	/// The code used by create transaction
38	create_code: Option<Code>,
39
40	/// List of created contracts addresses.
41	created_addrs: BTreeSet<H160>,
42
43	/// List of destructed contracts addresses.
44	destructed_addrs: BTreeSet<H160>,
45
46	// pre / post state
47	trace: (BTreeMap<H160, PrestateTraceInfo>, BTreeMap<H160, PrestateTraceInfo>),
48
49	_phantom: core::marker::PhantomData<T>,
50}
51
52impl<T: Config> PrestateTracer<T>
53where
54	T::Nonce: Into<u32>,
55{
56	/// Create a new [`PrestateTracer`] instance.
57	pub fn new(config: PrestateTracerConfig) -> Self {
58		Self { config, ..Default::default() }
59	}
60
61	fn current_addr(&self) -> H160 {
62		self.calls.last().copied().unwrap_or_default()
63	}
64
65	/// Returns an empty trace.
66	pub fn empty_trace(&self) -> PrestateTrace {
67		if self.config.diff_mode {
68			PrestateTrace::DiffMode { pre: Default::default(), post: Default::default() }
69		} else {
70			PrestateTrace::Prestate(Default::default())
71		}
72	}
73
74	/// Collect the traces and return them.
75	pub fn collect_trace(self) -> PrestateTrace {
76		let (mut pre, mut post) = self.trace;
77		let include_code = !self.config.disable_code;
78
79		let is_empty = |info: &PrestateTraceInfo| {
80			!info.storage.values().any(|v| v.is_some()) &&
81				info.balance.is_none() &&
82				info.nonce.is_none() &&
83				info.code.is_none()
84		};
85
86		if self.config.diff_mode {
87			if include_code {
88				for addr in &self.created_addrs {
89					if let Some(info) = post.get_mut(addr) {
90						info.code = Self::bytecode(addr);
91					}
92				}
93			}
94
95			// collect destructed contracts info in post state
96			for addr in &self.destructed_addrs {
97				Self::update_prestate_info(post.entry(*addr).or_default(), addr, None);
98			}
99
100			// clean up the storage that are in pre but not in post these are just read
101			pre.iter_mut().for_each(|(addr, info)| {
102				if let Some(post_info) = post.get(addr) {
103					info.storage.retain(|k, _| post_info.storage.contains_key(k));
104				} else {
105					info.storage.clear();
106				}
107			});
108
109			// If the address was created and destructed we do not trace it
110			post.retain(|addr, _| {
111				if self.created_addrs.contains(addr) && self.destructed_addrs.contains(addr) {
112					return false;
113				}
114				true
115			});
116
117			pre.retain(|addr, pre_info| {
118				if is_empty(&pre_info) {
119					return false;
120				}
121
122				let post_info = post.entry(*addr).or_insert_with_key(|addr| {
123					Self::prestate_info(
124						addr,
125						Pallet::<T>::evm_balance(addr),
126						include_code.then(|| Self::bytecode(addr)).flatten(),
127					)
128				});
129
130				if post_info == pre_info {
131					post.remove(addr);
132					return false;
133				}
134
135				if post_info.code == pre_info.code {
136					post_info.code = None;
137				}
138
139				if post_info.balance == pre_info.balance {
140					post_info.balance = None;
141				}
142
143				if post_info.nonce == pre_info.nonce {
144					post_info.nonce = None;
145				}
146
147				if post_info == &Default::default() {
148					post.remove(addr);
149				}
150
151				true
152			});
153
154			post.retain(|_, info| !is_empty(&info));
155			PrestateTrace::DiffMode { pre, post }
156		} else {
157			pre.retain(|_, info| !is_empty(&info));
158			PrestateTrace::Prestate(pre)
159		}
160	}
161}
162
163/// Get the appropriate trace entry (pre or post) based on whether
164/// the address was created.
165/// Returns early if the address is created and diff_mode is false.
166macro_rules! get_entry {
167	($self:expr, $addr:expr) => {
168		if $self.created_addrs.contains(&$addr) {
169			if !$self.config.diff_mode {
170				return;
171			}
172			$self.trace.1.entry($addr)
173		} else {
174			$self.trace.0.entry($addr)
175		}
176	};
177}
178
179impl<T: Config> PrestateTracer<T>
180where
181	T::Nonce: Into<u32>,
182{
183	/// Get the code of the contract.
184	fn bytecode(address: &H160) -> Option<Bytes> {
185		let code = Pallet::<T>::code(address);
186		(!code.is_empty()).then(|| code.into())
187	}
188
189	/// Update the prestate info for the given address.
190	fn update_prestate_info(entry: &mut PrestateTraceInfo, addr: &H160, code: Option<Bytes>) {
191		let info = Self::prestate_info(addr, Pallet::<T>::evm_balance(addr), code);
192		entry.balance = info.balance;
193		entry.nonce = info.nonce;
194		entry.code = info.code;
195	}
196
197	/// Set the PrestateTraceInfo for the given address.
198	fn prestate_info(addr: &H160, balance: U256, code: Option<Bytes>) -> PrestateTraceInfo {
199		let mut info = PrestateTraceInfo::default();
200		info.balance = Some(balance);
201		info.code = code;
202		let nonce = Pallet::<T>::evm_nonce(addr);
203		info.nonce = if nonce > 0 { Some(nonce) } else { None };
204		info
205	}
206
207	/// Record a read
208	fn read_account(&mut self, addr: H160) {
209		let include_code = !self.config.disable_code;
210		get_entry!(self, addr).or_insert_with_key(|addr| {
211			Self::prestate_info(
212				addr,
213				Pallet::<T>::evm_balance(addr),
214				include_code.then(|| Self::bytecode(addr)).flatten(),
215			)
216		});
217	}
218}
219
220impl<T: Config> Tracing for PrestateTracer<T>
221where
222	T::Nonce: Into<u32>,
223{
224	fn watch_address(&mut self, addr: &H160) {
225		let include_code = !self.config.disable_code;
226		self.trace.0.entry(*addr).or_insert_with_key(|addr| {
227			Self::prestate_info(
228				addr,
229				Pallet::<T>::evm_balance(addr),
230				include_code.then(|| Self::bytecode(addr)).flatten(),
231			)
232		});
233	}
234
235	fn instantiate_code(&mut self, code: &crate::Code, _salt: Option<&[u8; 32]>) {
236		self.create_code = Some(code.clone());
237	}
238
239	fn terminate(
240		&mut self,
241		contract_address: H160,
242		beneficiary_address: H160,
243		_gas_left: u64,
244		_value: U256,
245	) {
246		self.destructed_addrs.insert(contract_address);
247		self.trace.0.entry(beneficiary_address).or_insert_with_key(|addr| {
248			Self::prestate_info(addr, Pallet::<T>::evm_balance(addr), None)
249		});
250	}
251
252	fn enter_child_span(
253		&mut self,
254		from: H160,
255		to: H160,
256		code_address: Option<H160>,
257		is_delegate_call: bool,
258		_is_read_only: bool,
259		_value: U256,
260		_input: &[u8],
261		_gas_limit: u64,
262	) {
263		if is_delegate_call {
264			self.calls.push(self.current_addr());
265		} else {
266			self.calls.push(to);
267		}
268
269		if let Some(code_address) = code_address {
270			self.read_account(code_address);
271		}
272		self.read_account(from);
273
274		if self.create_code.take().is_some() {
275			self.created_addrs.insert(to);
276		} else {
277			self.read_account(to);
278		}
279	}
280
281	fn exit_child_span_with_error(
282		&mut self,
283		_error: crate::DispatchError,
284		_gas_used: u64,
285		_weight_consumed: Weight,
286	) {
287		self.calls.pop();
288	}
289
290	fn exit_child_span(
291		&mut self,
292		output: &ExecReturnValue,
293		_gas_used: u64,
294		_weight_consumed: Weight,
295	) {
296		let current_addr = self.calls.pop().unwrap_or_default();
297		if output.did_revert() {
298			return;
299		}
300
301		let code = if self.config.disable_code { None } else { Self::bytecode(&current_addr) };
302
303		Self::update_prestate_info(
304			self.trace.1.entry(current_addr).or_default(),
305			&current_addr,
306			code,
307		);
308	}
309
310	fn storage_write(&mut self, key: &Key, old_value: Option<Vec<u8>>, new_value: Option<&[u8]>) {
311		let current_addr = self.current_addr();
312		let key = Bytes::from(key.unhashed().to_vec());
313
314		let old_value = get_entry!(self, current_addr)
315			.or_default()
316			.storage
317			.entry(key.clone())
318			.or_insert_with(|| old_value.map(Into::into));
319
320		if !self.config.diff_mode {
321			return;
322		}
323
324		if old_value.as_ref().map(|v| v.0.as_ref()) != new_value {
325			self.trace
326				.1
327				.entry(current_addr)
328				.or_default()
329				.storage
330				.insert(key, new_value.map(|v| v.to_vec().into()));
331		} else {
332			self.trace.1.entry(self.current_addr()).or_default().storage.remove(&key);
333		}
334	}
335
336	fn storage_read(&mut self, key: &Key, value: Option<&[u8]>) {
337		let current_addr = self.current_addr();
338
339		get_entry!(self, current_addr)
340			.or_default()
341			.storage
342			.entry(key.unhashed().to_vec().into())
343			.or_insert_with(|| value.map(|v| v.to_vec().into()));
344	}
345
346	fn balance_read(&mut self, addr: &H160, value: U256) {
347		let include_code = !self.config.disable_code;
348		get_entry!(self, *addr).or_insert_with_key(|addr| {
349			Self::prestate_info(addr, value, include_code.then(|| Self::bytecode(addr)).flatten())
350		});
351	}
352}