evm_tracing_events/
gasometer.rs

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
17use parity_scale_codec::{Decode, Encode};
18
19#[derive(Debug, Default, Copy, Clone, Encode, Decode, PartialEq, Eq)]
20pub struct Snapshot {
21	pub gas_limit: u64,
22	pub memory_gas: u64,
23	pub used_gas: u64,
24	pub refunded_gas: i64,
25}
26
27impl Snapshot {
28	pub fn gas(&self) -> u64 {
29		self.gas_limit - self.used_gas - self.memory_gas
30	}
31}
32
33#[cfg(feature = "evm-tracing")]
34impl From<Option<evm_gasometer::Snapshot>> for Snapshot {
35	fn from(i: Option<evm_gasometer::Snapshot>) -> Self {
36		if let Some(i) = i {
37			Self {
38				gas_limit: i.gas_limit,
39				memory_gas: i.memory_gas,
40				used_gas: i.used_gas,
41				refunded_gas: i.refunded_gas,
42			}
43		} else {
44			Default::default()
45		}
46	}
47}
48
49#[derive(Debug, Copy, Clone, Encode, Decode, PartialEq, Eq)]
50pub enum GasometerEvent {
51	RecordCost {
52		cost: u64,
53		snapshot: Snapshot,
54	},
55	RecordRefund {
56		refund: i64,
57		snapshot: Snapshot,
58	},
59	RecordStipend {
60		stipend: u64,
61		snapshot: Snapshot,
62	},
63	RecordDynamicCost {
64		gas_cost: u64,
65		memory_gas: u64,
66		gas_refund: i64,
67		snapshot: Snapshot,
68	},
69	RecordTransaction {
70		cost: u64,
71		snapshot: Snapshot,
72	},
73}
74
75#[cfg(feature = "evm-tracing")]
76impl From<evm_gasometer::tracing::Event> for GasometerEvent {
77	fn from(i: evm_gasometer::tracing::Event) -> Self {
78		match i {
79			evm_gasometer::tracing::Event::RecordCost { cost, snapshot } => Self::RecordCost {
80				cost,
81				snapshot: snapshot.into(),
82			},
83			evm_gasometer::tracing::Event::RecordRefund { refund, snapshot } => {
84				Self::RecordRefund {
85					refund,
86					snapshot: snapshot.into(),
87				}
88			}
89			evm_gasometer::tracing::Event::RecordStipend { stipend, snapshot } => {
90				Self::RecordStipend {
91					stipend,
92					snapshot: snapshot.into(),
93				}
94			}
95			evm_gasometer::tracing::Event::RecordDynamicCost {
96				gas_cost,
97				memory_gas,
98				gas_refund,
99				snapshot,
100			} => Self::RecordDynamicCost {
101				gas_cost,
102				memory_gas,
103				gas_refund,
104				snapshot: snapshot.into(),
105			},
106			evm_gasometer::tracing::Event::RecordTransaction { cost, snapshot } => {
107				Self::RecordTransaction {
108					cost,
109					snapshot: snapshot.into(),
110				}
111			}
112		}
113	}
114}