selection.rs (2118B)
1 use super::{BlindedTransaction, Transaction}; 2 3 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 4 pub(super) enum TransactionKind { 5 Burn, 6 } 7 8 #[derive(Clone, Debug, Default, Eq, PartialEq)] 9 pub(super) struct BlockSelection { 10 pub(super) transactions: Vec<Transaction>, 11 pub(super) blinded_transactions: Vec<BlindedTransaction>, 12 } 13 14 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 15 pub(super) enum SelectableItem { 16 Plain(usize, u128), 17 Blinded(usize, u128), 18 } 19 20 pub(super) fn fee_rate_key(transaction: &Transaction) -> u128 { 21 let size = transaction.economic_size_bytes(); 22 if size == 0 { 23 return 0; 24 } 25 u128::from(transaction.fee()) * 1_000_000 / size as u128 26 } 27 28 pub(super) fn blinded_fee_rate_key(transaction: &BlindedTransaction) -> u128 { 29 let size = transaction.fee_rate_size_bytes(); 30 if size == 0 { 31 return 0; 32 } 33 u128::from(transaction.fee) * 1_000_000 / size as u128 34 } 35 36 pub(super) fn best_selectable_item( 37 plain: Option<SelectableItem>, 38 blinded: Option<SelectableItem>, 39 ) -> Option<SelectableItem> { 40 match (plain, blinded) { 41 ( 42 Some(SelectableItem::Plain(_, plain_rate)), 43 Some(SelectableItem::Blinded(_, blind_rate)), 44 ) => { 45 if blind_rate > plain_rate { 46 blinded 47 } else { 48 plain 49 } 50 } 51 (Some(item), None) | (None, Some(item)) => Some(item), 52 (None, None) => None, 53 _ => None, 54 } 55 } 56 57 #[cfg(test)] 58 mod tests { 59 use super::{SelectableItem, best_selectable_item}; 60 61 #[test] 62 fn selectable_item_prefers_blinded_only_when_fee_rate_is_higher() { 63 assert_eq!( 64 best_selectable_item( 65 Some(SelectableItem::Plain(1, 10)), 66 Some(SelectableItem::Blinded(2, 11)) 67 ), 68 Some(SelectableItem::Blinded(2, 11)) 69 ); 70 assert_eq!( 71 best_selectable_item( 72 Some(SelectableItem::Plain(1, 10)), 73 Some(SelectableItem::Blinded(2, 10)) 74 ), 75 Some(SelectableItem::Plain(1, 10)) 76 ); 77 } 78 }