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	evm::{Bytes, PrestateTrace, PrestateTraceInfo, PrestateTracerConfig},
19	tracing::Tracing,
20	AccountInfo, Code, Config, ExecReturnValue, Key, Pallet, PristineCode,
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_hash = AccountInfo::<T>::load_contract(address)?.code_hash;
186		let code: Vec<u8> = PristineCode::<T>::get(&code_hash)?.into();
187		return Some(code.into())
188	}
189
190	/// Update the prestate info for the given address.
191	fn update_prestate_info(entry: &mut PrestateTraceInfo, addr: &H160, code: Option<Bytes>) {
192		let info = Self::prestate_info(addr, Pallet::<T>::evm_balance(addr), code);
193		entry.balance = info.balance;
194		entry.nonce = info.nonce;
195		entry.code = info.code;
196	}
197
198	/// Set the PrestateTraceInfo for the given address.
199	fn prestate_info(addr: &H160, balance: U256, code: Option<Bytes>) -> PrestateTraceInfo {
200		let mut info = PrestateTraceInfo::default();
201		info.balance = Some(balance);
202		info.code = code;
203		let nonce = Pallet::<T>::evm_nonce(addr);
204		info.nonce = if nonce > 0 { Some(nonce) } else { None };
205		info
206	}
207
208	/// Record a read
209	fn read_account(&mut self, addr: H160) {
210		let include_code = !self.config.disable_code;
211		get_entry!(self, addr).or_insert_with_key(|addr| {
212			Self::prestate_info(
213				addr,
214				Pallet::<T>::evm_balance(addr),
215				include_code.then(|| Self::bytecode(addr)).flatten(),
216			)
217		});
218	}
219}
220
221impl<T: Config> Tracing for PrestateTracer<T>
222where
223	T::Nonce: Into<u32>,
224{
225	fn watch_address(&mut self, addr: &H160) {
226		let include_code = !self.config.disable_code;
227		self.trace.0.entry(*addr).or_insert_with_key(|addr| {
228			Self::prestate_info(
229				addr,
230				Pallet::<T>::evm_balance(addr),
231				include_code.then(|| Self::bytecode(addr)).flatten(),
232			)
233		});
234	}
235
236	fn instantiate_code(&mut self, code: &crate::Code, _salt: Option<&[u8; 32]>) {
237		self.create_code = Some(code.clone());
238	}
239
240	fn terminate(
241		&mut self,
242		contract_address: H160,
243		beneficiary_address: H160,
244		_gas_left: U256,
245		_value: U256,
246	) {
247		self.destructed_addrs.insert(contract_address);
248		self.trace.0.entry(beneficiary_address).or_insert_with_key(|addr| {
249			Self::prestate_info(addr, Pallet::<T>::evm_balance(addr), None)
250		});
251	}
252
253	fn enter_child_span(
254		&mut self,
255		from: H160,
256		to: H160,
257		delegate_call: Option<H160>,
258		_is_read_only: bool,
259		_value: U256,
260		_input: &[u8],
261		_gas_limit: U256,
262	) {
263		if let Some(delegate_call) = delegate_call {
264			self.calls.push(self.current_addr());
265			self.read_account(delegate_call);
266		} else {
267			self.calls.push(to);
268			self.read_account(from);
269		}
270
271		if self.create_code.take().is_some() {
272			self.created_addrs.insert(to);
273		} else {
274			self.read_account(to);
275		}
276	}
277
278	fn exit_child_span_with_error(&mut self, _error: crate::DispatchError, _gas_used: U256) {
279		self.calls.pop();
280	}
281
282	fn exit_child_span(&mut self, output: &ExecReturnValue, _gas_used: U256) {
283		let current_addr = self.calls.pop().unwrap_or_default();
284		if output.did_revert() {
285			return
286		}
287
288		let code = if self.config.disable_code { None } else { Self::bytecode(&current_addr) };
289
290		Self::update_prestate_info(
291			self.trace.1.entry(current_addr).or_default(),
292			&current_addr,
293			code,
294		);
295	}
296
297	fn storage_write(&mut self, key: &Key, old_value: Option<Vec<u8>>, new_value: Option<&[u8]>) {
298		let current_addr = self.current_addr();
299		let key = Bytes::from(key.unhashed().to_vec());
300
301		let old_value = get_entry!(self, current_addr)
302			.or_default()
303			.storage
304			.entry(key.clone())
305			.or_insert_with(|| old_value.map(Into::into));
306
307		if !self.config.diff_mode {
308			return
309		}
310
311		if old_value.as_ref().map(|v| v.0.as_ref()) != new_value {
312			self.trace
313				.1
314				.entry(current_addr)
315				.or_default()
316				.storage
317				.insert(key, new_value.map(|v| v.to_vec().into()));
318		} else {
319			self.trace.1.entry(self.current_addr()).or_default().storage.remove(&key);
320		}
321	}
322
323	fn storage_read(&mut self, key: &Key, value: Option<&[u8]>) {
324		let current_addr = self.current_addr();
325
326		get_entry!(self, current_addr)
327			.or_default()
328			.storage
329			.entry(key.unhashed().to_vec().into())
330			.or_insert_with(|| value.map(|v| v.to_vec().into()));
331	}
332
333	fn balance_read(&mut self, addr: &H160, value: U256) {
334		let include_code = !self.config.disable_code;
335		get_entry!(self, *addr).or_insert_with_key(|addr| {
336			Self::prestate_info(addr, value, include_code.then(|| Self::bytecode(addr)).flatten())
337		});
338	}
339}