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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use std::fmt::{Debug, Display};

pub use super::fvm_latest::gas::{
    Gas as Gas_latest, GasCharge as GasCharge_latest, GasDuration as GasDuration_latest,
};
use fvm2::gas::{
    price_list_by_network_version as price_list_by_network_version_v2, Gas as GasV2,
    GasCharge as GasChargeV2, PriceList as PriceListV2,
};
use fvm3::gas::{
    price_list_by_network_version as price_list_by_network_version_v3, Gas as GasV3,
    MILLIGAS_PRECISION,
};
pub use fvm3::gas::{GasCharge as GasChargeV3, GasTracker, PriceList as PriceListV3};
use fvm4::gas::price_list_by_network_version as price_list_by_network_version_v4;
pub use fvm4::gas::{
    Gas as GasV4, GasCharge as GasChargeV4, GasDuration as GasDurationV4, PriceList as PriceListV4,
};

use crate::shim::version::NetworkVersion;

#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Default)]
pub struct Gas(Gas_latest);

#[derive(Clone, Default)]
pub struct GasDuration(GasDuration_latest);

impl Debug for Gas {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0.as_milligas() == 0 {
            f.debug_tuple("Gas").field(&0 as &dyn Debug).finish()
        } else {
            let integral = self.0.as_milligas() / MILLIGAS_PRECISION;
            let fractional = self.0.as_milligas() % MILLIGAS_PRECISION;
            f.debug_tuple("Gas")
                .field(&format_args!("{integral}.{fractional:03}"))
                .finish()
        }
    }
}

impl Display for Gas {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0.as_milligas() == 0 {
            f.write_str("0")
        } else {
            let integral = self.0.as_milligas() / MILLIGAS_PRECISION;
            let fractional = self.0.as_milligas() % MILLIGAS_PRECISION;
            write!(f, "{integral}.{fractional:03}")
        }
    }
}

impl Gas {
    pub fn new(gas: u64) -> Self {
        Self(Gas_latest::new(gas))
    }

    pub fn round_up(&self) -> u64 {
        self.0.round_up()
    }
}

impl GasDuration {
    pub fn as_nanos(&self) -> u64 {
        if let Some(duration) = self.0.get() {
            duration.as_nanos().clamp(0, u64::MAX as u128) as u64
        } else {
            0
        }
    }
}

impl From<GasV2> for Gas {
    fn from(value: GasV2) -> Self {
        Gas(Gas_latest::from_milligas(value.as_milligas() as _))
    }
}

impl From<Gas> for GasV2 {
    fn from(value: Gas) -> Self {
        GasV2::from_milligas(value.0.as_milligas() as _)
    }
}

impl From<Gas> for GasV3 {
    fn from(value: Gas) -> Self {
        GasV3::from_milligas(value.0.as_milligas())
    }
}

impl From<GasV3> for Gas {
    fn from(value: GasV3) -> Self {
        Gas(Gas_latest::from_milligas(value.as_milligas() as _))
    }
}

impl From<Gas> for GasV4 {
    fn from(value: Gas) -> Self {
        GasV4::from_milligas(value.0.as_milligas())
    }
}

impl From<GasV4> for Gas {
    fn from(value: GasV4) -> Self {
        Gas(value)
    }
}

#[derive(Debug, Clone)]
pub struct GasCharge(GasCharge_latest);

impl GasCharge {
    /// Calculates total gas charge (in `milligas`) by summing compute and
    /// storage gas associated with this charge.
    pub fn total(&self) -> Gas {
        self.0.total().into()
    }
    pub fn name(&self) -> &str {
        &self.0.name
    }
    pub fn compute_gas(&self) -> Gas {
        self.0.compute_gas.into()
    }
    pub fn other_gas(&self) -> Gas {
        self.0.other_gas.into()
    }
    pub fn elapsed(&self) -> GasDuration {
        self.0.elapsed.clone().into()
    }
}

impl From<GasChargeV2> for GasCharge {
    fn from(value: GasChargeV2) -> Self {
        GasChargeV3 {
            name: value.name,
            compute_gas: GasV3::from_milligas(value.compute_gas.as_milligas() as u64),
            other_gas: GasV3::from_milligas(value.storage_gas.as_milligas() as u64),
            elapsed: Default::default(),
        }
        .into()
    }
}

impl From<GasChargeV3> for GasCharge {
    fn from(value: GasChargeV3) -> Self {
        GasChargeV4 {
            name: value.name,
            compute_gas: GasV4::from_milligas(value.compute_gas.as_milligas()),
            other_gas: GasV4::from_milligas(value.other_gas.as_milligas()),
            elapsed: value.elapsed.get().map(|&d| d.into()).unwrap_or_default(),
        }
        .into()
    }
}

impl From<GasChargeV4> for GasCharge {
    fn from(value: GasChargeV4) -> Self {
        GasCharge(value)
    }
}

impl From<GasCharge> for GasChargeV2 {
    fn from(value: GasCharge) -> Self {
        Self {
            name: value.0.name,
            compute_gas: GasV2::from_milligas(value.0.compute_gas.as_milligas() as _),
            storage_gas: GasV2::from_milligas(value.0.other_gas.as_milligas() as _),
        }
    }
}

impl From<GasCharge> for GasChargeV3 {
    fn from(value: GasCharge) -> Self {
        Self {
            name: value.0.name,
            compute_gas: GasV3::from_milligas(value.0.compute_gas.as_milligas() as _),
            other_gas: GasV3::from_milligas(value.0.other_gas.as_milligas() as _),
            // TODO(hanabi1224): https://github.com/ChainSafe/forest/issues/3524
            elapsed: Default::default(),
        }
    }
}

impl From<GasCharge> for GasChargeV4 {
    fn from(value: GasCharge) -> Self {
        value.0
    }
}

impl From<GasDurationV4> for GasDuration {
    fn from(value: GasDurationV4) -> Self {
        GasDuration(value)
    }
}

pub enum PriceList {
    V2(&'static PriceListV2),
    V3(&'static PriceListV3),
    V4(&'static PriceListV4),
}

impl PriceList {
    pub fn on_block_open_base(&self) -> GasCharge {
        match self {
            PriceList::V2(list) => list.on_block_open_base().into(),
            PriceList::V3(list) => list.on_block_open_base().into(),
            PriceList::V4(list) => list.on_block_open_base().into(),
        }
    }

    pub fn on_block_link(&self, data_size: usize) -> GasCharge {
        match self {
            PriceList::V2(list) => list.on_block_link(data_size).into(),
            PriceList::V3(list) => list
                .on_block_link(fvm3::kernel::SupportedHashes::Blake2b256, data_size)
                .into(),
            PriceList::V4(list) => list
                .on_block_link(fvm4::kernel::SupportedHashes::Blake2b256, data_size)
                .into(),
        }
    }

    pub fn on_chain_message(&self, msg_size: usize) -> GasCharge {
        match self {
            PriceList::V2(list) => list.on_chain_message(msg_size).into(),
            PriceList::V3(list) => list.on_chain_message(msg_size).into(),
            PriceList::V4(list) => list.on_chain_message(msg_size).into(),
        }
    }
}

impl From<&'static PriceListV2> for PriceList {
    fn from(value: &'static PriceListV2) -> Self {
        PriceList::V2(value)
    }
}

impl From<&'static PriceListV3> for PriceList {
    fn from(value: &'static PriceListV3) -> Self {
        PriceList::V3(value)
    }
}

impl From<&'static PriceListV4> for PriceList {
    fn from(value: &'static PriceListV4) -> Self {
        PriceList::V4(value)
    }
}

pub fn price_list_by_network_version(network_version: NetworkVersion) -> PriceList {
    if network_version < NetworkVersion::V18 {
        price_list_by_network_version_v2(network_version.into()).into()
    } else if network_version < NetworkVersion::V21 {
        price_list_by_network_version_v3(network_version.into()).into()
    } else {
        price_list_by_network_version_v4(network_version.into()).into()
    }
}

// TODO(elmattic): https://github.com/ChainSafe/forest/issues/4759
use crate::shim::econ::TokenAmount;

#[derive(Clone, Default)]
pub(crate) struct GasOutputs {
    pub base_fee_burn: TokenAmount,
    pub over_estimation_burn: TokenAmount,
    pub miner_penalty: TokenAmount,
    pub miner_tip: TokenAmount,
    pub refund: TokenAmount,

    // In whole gas units.
    pub gas_refund: u64,
    pub gas_burned: u64,
}

impl GasOutputs {
    pub fn compute(
        // In whole gas units.
        gas_used: u64,
        gas_limit: u64,
        base_fee: &TokenAmount,
        fee_cap: &TokenAmount,
        gas_premium: &TokenAmount,
    ) -> Self {
        let mut base_fee_to_pay = base_fee;

        let mut out = GasOutputs::default();

        if base_fee > fee_cap {
            base_fee_to_pay = fee_cap;
            out.miner_penalty = (base_fee - fee_cap.clone()) * gas_used
        }

        out.base_fee_burn = base_fee_to_pay * gas_used;

        let mut miner_tip = gas_premium.clone();
        if &(base_fee_to_pay + &miner_tip) > fee_cap {
            miner_tip = fee_cap - base_fee_to_pay.clone();
        }
        out.miner_tip = &miner_tip * gas_limit;

        let (out_gas_refund, out_gas_burned) = compute_gas_overestimation_burn(gas_used, gas_limit);
        out.gas_refund = out_gas_refund;
        out.gas_burned = out_gas_burned;

        if out.gas_burned != 0 {
            out.over_estimation_burn = base_fee_to_pay * out.gas_burned;
            out.miner_penalty += (base_fee - base_fee_to_pay.clone()) * out.gas_burned;
        }
        let required_funds = fee_cap * gas_limit;
        let refund =
            required_funds - &out.base_fee_burn - &out.miner_tip - &out.over_estimation_burn;
        out.refund = refund;

        out
    }
}

fn compute_gas_overestimation_burn(gas_used: u64, gas_limit: u64) -> (u64, u64) {
    const GAS_OVERUSE_NUM: u128 = 11;
    const GAS_OVERUSE_DENOM: u128 = 10;

    if gas_used == 0 {
        return (0, gas_limit);
    }

    // Convert to u128 to prevent overflow on multiply.
    let gas_used = gas_used as u128;
    let gas_limit = gas_limit as u128;

    // This burns (N-10)% (clamped at 0% and 100%) of the remaining gas where N is the
    // overestimation percentage.
    let over = gas_limit
        .saturating_sub((GAS_OVERUSE_NUM * gas_used) / GAS_OVERUSE_DENOM)
        .min(gas_used);

    // We handle the case where the gas used exceeds the gas limit, just in case.
    let gas_remaining = gas_limit.saturating_sub(gas_used);

    // This computes the fraction of the "remaining" gas to burn and will never be greater than 100%
    // of the remaining gas.
    let gas_to_burn = (gas_remaining * over) / gas_used;

    // But... we use saturating sub, just in case.
    let refund = gas_remaining.saturating_sub(gas_to_burn);

    (refund as u64, gas_to_burn as u64)
}