diff --git a/crates/transaction-pool/src/pool/blob.rs b/crates/transaction-pool/src/pool/blob.rs index 15f337ecc..15677bf55 100644 --- a/crates/transaction-pool/src/pool/blob.rs +++ b/crates/transaction-pool/src/pool/blob.rs @@ -284,12 +284,19 @@ impl Ord for BlobTransaction { /// /// This is supposed to get the number of fee jumps required to get from the current fee to the fee /// cap, or where the transaction would not be executable any more. -fn fee_delta(max_tx_fee: u128, current_fee: u128) -> f64 { +fn fee_delta(max_tx_fee: u128, current_fee: u128) -> i64 { // jumps = log1.125(txfee) - log1.125(basefee) - // TODO: should we do this without f64? let jumps = (max_tx_fee as f64).log(1.125) - (current_fee as f64).log(1.125); + // delta = sign(jumps) * log(abs(jumps)) - jumps.signum() * jumps.abs().log2() + match (jumps as i64).cmp(&0) { + Ordering::Equal => { + // can't take ilog2 of 0 + 0 + } + Ordering::Greater => (jumps.ceil() as i64).ilog2() as i64, + Ordering::Less => -((-jumps.floor() as i64).ilog2() as i64), + } } /// Returns the priority for the transaction, based on the "delta" blob fee and priority fee. @@ -298,12 +305,12 @@ fn blob_tx_priority( blob_fee: u128, max_priority_fee: u128, base_fee: u128, -) -> f64 { +) -> i64 { let delta_blob_fee = fee_delta(blob_fee_cap, blob_fee); let delta_priority_fee = fee_delta(max_priority_fee, base_fee); // priority = min(delta-basefee, delta-blobfee, 0) - delta_blob_fee.min(delta_priority_fee).min(0.0) + delta_blob_fee.min(delta_priority_fee).min(0) } #[derive(Debug, Clone)] @@ -312,7 +319,7 @@ struct BlobOrd { pub(crate) submission_id: u64, // The priority for this transaction, calculated using the [`blob_tx_priority`] function, // taking into account both the blob and priority fee. - pub(crate) priority: f64, + pub(crate) priority: i64, } impl Eq for BlobOrd {} @@ -331,7 +338,7 @@ impl PartialOrd for BlobOrd { impl Ord for BlobOrd { fn cmp(&self, other: &Self) -> Ordering { - let ord = other.priority.total_cmp(&self.priority); + let ord = other.priority.cmp(&self.priority); // use submission_id to break ties if ord == Ordering::Equal { @@ -565,4 +572,27 @@ mod tests { ); } } + + #[test] + fn priority_tests() { + // Test vectors from: + // + let vectors = vec![ + (7u128, 10u128, 2i64), + (17_200_000_000, 17_200_000_000, 0), + (9_853_941_692, 11_085_092_510, 0), + (11_544_106_391, 10_356_781_100, 0), + (17_200_000_000, 7, -7), + (7, 17_200_000_000, 7), + ]; + + for (base_fee, tx_fee, expected) in vectors { + let actual = fee_delta(tx_fee, base_fee); + assert_eq!( + actual, expected, + "fee_delta({}, {}) = {}, expected: {}", + tx_fee, base_fee, actual, expected + ); + } + } }