1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

use parity_scale_codec::{Decode, Encode};

#[derive(Debug, Default, Copy, Clone, Encode, Decode, PartialEq, Eq)]
pub struct Snapshot {
    pub gas_limit: u64,
    pub memory_gas: u64,
    pub used_gas: u64,
    pub refunded_gas: i64,
}

impl Snapshot {
    pub fn gas(&self) -> u64 {
        self.gas_limit - self.used_gas - self.memory_gas
    }
}

#[cfg(feature = "evm-tracing")]
impl From<Option<evm_gasometer::Snapshot>> for Snapshot {
    fn from(i: Option<evm_gasometer::Snapshot>) -> Self {
        if let Some(i) = i {
            Self {
                gas_limit: i.gas_limit,
                memory_gas: i.memory_gas,
                used_gas: i.used_gas,
                refunded_gas: i.refunded_gas,
            }
        } else {
            Default::default()
        }
    }
}

#[derive(Debug, Copy, Clone, Encode, Decode, PartialEq, Eq)]
pub enum GasometerEvent {
    RecordCost {
        cost: u64,
        snapshot: Snapshot,
    },
    RecordRefund {
        refund: i64,
        snapshot: Snapshot,
    },
    RecordStipend {
        stipend: u64,
        snapshot: Snapshot,
    },
    RecordDynamicCost {
        gas_cost: u64,
        memory_gas: u64,
        gas_refund: i64,
        snapshot: Snapshot,
    },
    RecordTransaction {
        cost: u64,
        snapshot: Snapshot,
    },
}

#[cfg(feature = "evm-tracing")]
impl From<evm_gasometer::tracing::Event> for GasometerEvent {
    fn from(i: evm_gasometer::tracing::Event) -> Self {
        match i {
            evm_gasometer::tracing::Event::RecordCost { cost, snapshot } => Self::RecordCost {
                cost,
                snapshot: snapshot.into(),
            },
            evm_gasometer::tracing::Event::RecordRefund { refund, snapshot } => {
                Self::RecordRefund {
                    refund,
                    snapshot: snapshot.into(),
                }
            }
            evm_gasometer::tracing::Event::RecordStipend { stipend, snapshot } => {
                Self::RecordStipend {
                    stipend,
                    snapshot: snapshot.into(),
                }
            }
            evm_gasometer::tracing::Event::RecordDynamicCost {
                gas_cost,
                memory_gas,
                gas_refund,
                snapshot,
            } => Self::RecordDynamicCost {
                gas_cost,
                memory_gas,
                gas_refund,
                snapshot: snapshot.into(),
            },
            evm_gasometer::tracing::Event::RecordTransaction { cost, snapshot } => {
                Self::RecordTransaction {
                    cost,
                    snapshot: snapshot.into(),
                }
            }
        }
    }
}