From 416fc7512a656d1fa51e15fc91747a211105cb13 Mon Sep 17 00:00:00 2001 From: Ivan Kalinin Date: Tue, 21 Jan 2025 21:47:33 +0100 Subject: [PATCH] feat(models): add helpers for prices/limits --- src/models/config/params.rs | 48 ++++++++++++++++++++++++++++++++++++ src/models/vm/out_actions.rs | 15 +++++++---- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/models/config/params.rs b/src/models/config/params.rs index fa2a336..7434db6 100644 --- a/src/models/config/params.rs +++ b/src/models/config/params.rs @@ -307,6 +307,26 @@ pub struct StoragePrices { pub mc_cell_price_ps: u64, } +impl StoragePrices { + /// Computes the amount of fees for storing `stats` data for `delta` seconds. + pub fn compute_storage_fee( + &self, + is_masterchain: bool, + delta: u64, + stats: CellTreeStats, + ) -> Tokens { + let mut res = if is_masterchain { + (stats.cell_count as u128 * self.mc_cell_price_ps as u128) + .saturating_add(stats.bit_count as u128 * self.mc_bit_price_ps as u128) + } else { + (stats.cell_count as u128 * self.cell_price_ps as u128) + .saturating_add(stats.bit_count as u128 * self.bit_price_ps as u128) + }; + res = res.saturating_mul(delta as u128); + Tokens::new(shift_ceil_price(res)) + } +} + /// Gas limits and prices. #[derive(Default, Debug, Clone, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -333,6 +353,17 @@ pub struct GasLimitsPrices { pub flat_gas_price: u64, } +impl GasLimitsPrices { + /// Converts gas units into tokens. + pub fn compute_gas_fee(&self, gas_used: u64) -> Tokens { + let mut res = self.flat_gas_price as u128; + if let Some(extra_gas) = gas_used.checked_sub(self.flat_gas_limit) { + res = res.saturating_add(shift_ceil_price(self.gas_price as u128 * extra_gas as u128)); + } + Tokens::new(res) + } +} + impl GasLimitsPrices { const TAG_BASE: u8 = 0xdd; const TAG_EXT: u8 = 0xde; @@ -442,6 +473,18 @@ pub struct MsgForwardPrices { pub next_frac: u16, } +impl MsgForwardPrices { + /// Computes fees for forwarding the specified amount of data. + pub fn compute_fwd_fee(&self, stats: CellTreeStats) -> Tokens { + let lump = self.lump_price as u128; + let extra = shift_ceil_price( + (stats.cell_count as u128 * self.cell_price as u128) + .saturating_add(stats.bit_count as u128 * self.bit_price as u128), + ); + Tokens::new(lump.saturating_add(extra)) + } +} + /// Catchain configuration params. #[cfg(not(feature = "tycho"))] #[derive(Debug, Copy, Clone, Eq, PartialEq)] @@ -1457,6 +1500,11 @@ pub struct SizeLimitsConfig { pub defer_out_queue_size_limit: u32, } +const fn shift_ceil_price(value: u128) -> u128 { + let r = value & 0xffff != 0; + (value >> 16) + r as u128 +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/models/vm/out_actions.rs b/src/models/vm/out_actions.rs index 5d820f0..8091b07 100644 --- a/src/models/vm/out_actions.rs +++ b/src/models/vm/out_actions.rs @@ -187,11 +187,16 @@ pub enum OutAction { } impl OutAction { - const TAG_SEND_MSG: u32 = 0x0ec3c86d; - const TAG_SET_CODE: u32 = 0xad4de08e; - const TAG_RESERVE: u32 = 0x36e6b809; - const TAG_CHANGE_LIB: u32 = 0x26fa1dd4; - const TAG_COPYLEFT: u32 = 0x24486f7a; + /// Tag for [`OutAction::SendMsg`]. + pub const TAG_SEND_MSG: u32 = 0x0ec3c86d; + /// Tag for [`OutAction::SetCode`]. + pub const TAG_SET_CODE: u32 = 0xad4de08e; + /// Tag for [`OutAction::ReserveCurrency`]. + pub const TAG_RESERVE: u32 = 0x36e6b809; + /// Tag for [`OutAction::ChangeLibrary`]. + pub const TAG_CHANGE_LIB: u32 = 0x26fa1dd4; + /// Tag for [`OutAction::CopyLeft`]. + pub const TAG_COPYLEFT: u32 = 0x24486f7a; } impl Store for OutAction {