cairo_lang_sierra_to_casm/environment/
gas_wallet.rs

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
use std::fmt::Display;

use cairo_lang_sierra::extensions::gas::CostTokenType;
use cairo_lang_utils::collection_arithmetics::add_maps;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use thiserror::Error;

#[derive(Error, Debug, Eq, PartialEq)]
pub enum GasWalletError {
    #[error(
        "Ran out of gas ({token_type:?}) in the wallet, requested {request:?} while state is \
         {state}."
    )]
    OutOfGas {
        state: GasWallet,
        request: Box<OrderedHashMap<CostTokenType, i64>>,
        token_type: CostTokenType,
    },
}

/// Environment tracking the amount of gas available in a statement's context.
#[derive(Clone, Debug, Eq)]
pub enum GasWallet {
    /// A known value.
    Value(OrderedHashMap<CostTokenType, i64>),
    /// If gas tracking is disabled, this value should be used for all the statements.
    Disabled,
}
impl GasWallet {
    /// Updates the value in the wallet by `request`. Can be both negative (for most libfuncs) and
    /// positive (for gas acquisition libfuncs).
    pub fn update(
        &self,
        request: OrderedHashMap<CostTokenType, i64>,
    ) -> Result<Self, GasWalletError> {
        match &self {
            Self::Value(existing) => {
                let new_value = add_maps(existing.clone(), request.iter().map(|(k, v)| (*k, *v)));
                for (token_type, val) in new_value.iter() {
                    if *val < 0 {
                        return Err(GasWalletError::OutOfGas {
                            state: self.clone(),
                            request: Box::new(request),
                            token_type: *token_type,
                        });
                    }
                }
                Ok(GasWallet::Value(new_value))
            }
            Self::Disabled => Ok(Self::Disabled),
        }
    }
}

impl Display for GasWallet {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Value(value) => write!(f, "GasWallet::Value({value:?})"),
            Self::Disabled => write!(f, "GasWallet::Disabled"),
        }
    }
}

impl PartialEq for GasWallet {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Value(l), Self::Value(r)) => l.eq_unordered(r),
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}