Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(bolt-sidecar): extract consts and validation #595

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions bolt-sidecar/src/state/pricing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/// Gas limit constants
pub const DEFAULT_BLOCK_GAS_LIMIT: u64 = 30_000_000;

/// Fee calculation constants
estensen marked this conversation as resolved.
Show resolved Hide resolved
const BASE_MULTIPLIER: f64 = 0.019;
const GAS_SCALAR: f64 = 1.02e-6;

/// Handles pricing calculations for preconfirmations
#[derive(Debug)]
pub struct PreconfPricing {
Expand Down Expand Up @@ -31,14 +38,14 @@ pub enum PricingError {

impl Default for PreconfPricing {
fn default() -> Self {
Self::new(30_000_000)
Self::new(DEFAULT_BLOCK_GAS_LIMIT)
}
}

impl PreconfPricing {
/// Initializes a new PreconfPricing with default parameters.
pub fn new(block_gas_limit: u64) -> Self {
Self { block_gas_limit, base_multiplier: 0.019, gas_scalar: 1.02e-6 }
Self { block_gas_limit, base_multiplier: BASE_MULTIPLIER, gas_scalar: GAS_SCALAR }
}

/// Calculate the minimum priority fee for a preconfirmation based on
Expand All @@ -56,31 +63,14 @@ impl PreconfPricing {
incoming_gas: u64,
preconfirmed_gas: u64,
) -> Result<u64, PricingError> {
// Check if preconfirmed gas exceeds block limit
if preconfirmed_gas >= self.block_gas_limit {
return Err(PricingError::ExceedsBlockLimit(preconfirmed_gas, self.block_gas_limit));
}

// Validate incoming gas
if incoming_gas == 0 {
return Err(PricingError::InvalidGasLimit { incoming_gas });
}

// Check if there is enough gas remaining in the block
let remaining_gas = self.block_gas_limit - preconfirmed_gas;
if incoming_gas > remaining_gas {
return Err(PricingError::InsufficientGas {
requested: incoming_gas,
available: remaining_gas,
});
}

validate_fee_inputs(incoming_gas, preconfirmed_gas, self.block_gas_limit)?;
// T(IG,UG) = 0.019 * ln(1.02⋅10^-6(30M-UG)+1 / 1.02⋅10^-6(30M-UG-IG)+1) / IG
// where
// IG = Gas used by the incoming transaction
// UG = Gas already preconfirmed
// T = Inclusion tip per gas
// 30M = Current gas limit (36M soon?)
let remaining_gas = self.block_gas_limit - preconfirmed_gas;
let after_gas = remaining_gas - incoming_gas;

// Calculate numerator and denominator for the logarithm
Expand All @@ -98,6 +88,32 @@ impl PreconfPricing {
}
}

fn validate_fee_inputs(
incoming_gas: u64,
preconfirmed_gas: u64,
gas_limit: u64,
) -> Result<(), PricingError> {
// Check if preconfirmed gas exceeds block limit
if preconfirmed_gas >= gas_limit {
return Err(PricingError::ExceedsBlockLimit(preconfirmed_gas, gas_limit));
}

// Validate incoming gas
if incoming_gas == 0 {
return Err(PricingError::InvalidGasLimit { incoming_gas });
}

// Check if there is enough gas remaining in the block
let remaining_gas = gas_limit - preconfirmed_gas;
if incoming_gas > remaining_gas {
return Err(PricingError::InsufficientGas {
requested: incoming_gas,
available: remaining_gas,
});
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading