Skip to main content

thruput_storage_cost/
model.rs

1use thruput_xdr::storage_cost::StorageCostModelXdr;
2
3pub struct StorageCostModel {
4    cost_model: StorageCostModelXdr,
5}
6
7impl StorageCostModel {
8    pub fn new(cost_model: StorageCostModelXdr) -> Self {
9        Self { cost_model }
10    }
11
12    pub fn no_cost_model() -> Self {
13        Self {
14            cost_model: StorageCostModelXdr {
15                ext: thruput_xdr::types::ExtensionPoint::_0,
16                base_data_item_cost: 0u64,
17                bytestring_cost_per_byte: 0u32,
18                base_contract_deployment_cost: 0u64,
19                contract_deployment_cost_per_byte: 0u64,
20                auth_per_key_cost: 0u64,
21                rcq_cost_per_entry: 0u64,
22                replay_cache_cost_per_entry: 0u64,
23                balance_cost: 0u64,
24            },
25        }
26    }
27
28    pub fn serialize(&self) -> xdrs::WriteResult<xdrs::Vec<u8>> {
29        xdrs::txs::encode(&self.cost_model)
30    }
31
32    fn base_data_item_cost(&self) -> i128 {
33        self.cost_model.base_data_item_cost.into()
34    }
35
36    fn bytestring_cost_per_byte(&self) -> i128 {
37        self.cost_model.bytestring_cost_per_byte.into()
38    }
39
40    /// Cost of a bytestring of the given length
41    pub fn bytestring_cost(&self, length: usize) -> i128 {
42        self.base_data_item_cost()
43            + self.bytestring_cost_per_byte() * length as i128
44    }
45
46    /// Cost of a non-negative integer
47    pub fn nnint_cost(&self) -> i128 {
48        self.base_data_item_cost()
49    }
50
51    fn base_contract_deployment_cost(&self) -> i128 {
52        self.cost_model.base_contract_deployment_cost.into()
53    }
54
55    fn contract_deployment_cost_per_byte(&self) -> i128 {
56        self.cost_model.contract_deployment_cost_per_byte.into()
57    }
58
59    /// Cost of a contract deployment of the given length
60    pub fn contract_deployment_cost(&self, length: usize) -> i128 {
61        self.base_contract_deployment_cost()
62            + self.contract_deployment_cost_per_byte() * length as i128
63    }
64
65    /// Cost of a native token balance
66    pub fn balance_cost(&self) -> i128 {
67        self.cost_model.balance_cost.into()
68    }
69
70    pub fn as_xdr(&self) -> StorageCostModelXdr {
71        self.cost_model.clone()
72    }
73}