use core::{cmp, ops::Deref};
use crate::ln::funding::FundingContribution;
use crate::ln::types::ChannelId;
use crate::prelude::*;
use bitcoin::hash_types::Txid;
use bitcoin::secp256k1::PublicKey;
use bitcoin::transaction::Transaction;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum TransactionType {
Funding {
channels: Vec<(PublicKey, ChannelId)>,
},
CooperativeClose {
counterparty_node_id: PublicKey,
channel_id: ChannelId,
},
UnilateralClose {
counterparty_node_id: PublicKey,
channel_id: ChannelId,
},
AnchorBump {
counterparty_node_id: PublicKey,
channel_id: ChannelId,
},
Claim {
counterparty_node_id: PublicKey,
channel_id: ChannelId,
},
Sweep {
channels: Vec<(PublicKey, ChannelId)>,
},
InteractiveFunding {
candidates: Vec<FundingCandidate>,
},
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct FundingCandidate {
pub txid: Txid,
pub channels: Vec<ChannelFunding>,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct ChannelFunding {
pub counterparty_node_id: PublicKey,
pub channel_id: ChannelId,
pub purpose: FundingPurpose,
pub contribution: Option<FundingContribution>,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum FundingPurpose {
Establishment,
Splice,
}
impl_writeable_tlv_based!(FundingCandidate, {
(1, txid, required),
(3, channels, required_vec),
});
impl_writeable_tlv_based!(ChannelFunding, {
(1, counterparty_node_id, required),
(3, channel_id, required),
(5, purpose, required),
(7, contribution, option),
});
impl_writeable_tlv_based_enum!(FundingPurpose,
(0, Establishment) => {},
(2, Splice) => {},
);
pub(crate) fn compute_feerate_sat_per_1000_weight(fee_sat: u64, weight: u64) -> u32 {
(fee_sat * 1000 / weight).try_into().unwrap_or(u32::max_value())
}
pub(crate) const fn fee_for_weight(feerate_sat_per_1000_weight: u32, weight: u64) -> u64 {
(feerate_sat_per_1000_weight as u64 * weight).div_ceil(1000)
}
pub trait BroadcasterInterface {
fn broadcast_transactions(&self, txs: &[(&Transaction, TransactionType)]);
}
impl<T: BroadcasterInterface + ?Sized, B: Deref<Target = T>> BroadcasterInterface for B {
fn broadcast_transactions(&self, txs: &[(&Transaction, TransactionType)]) {
self.deref().broadcast_transactions(txs)
}
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum ConfirmationTarget {
MaximumFeeEstimate,
UrgentOnChainSweep,
MinAllowedAnchorChannelRemoteFee,
MinAllowedNonAnchorChannelRemoteFee,
AnchorChannelFee,
NonAnchorChannelFee,
ChannelCloseMinimum,
OutputSpendingFee,
}
pub trait FeeEstimator {
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32;
}
impl<T: FeeEstimator + ?Sized, F: Deref<Target = T>> FeeEstimator for F {
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
self.deref().get_est_sat_per_1000_weight(confirmation_target)
}
}
pub const INCREMENTAL_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 253;
pub const FEERATE_FLOOR_SATS_PER_KW: u32 = 253;
pub(crate) struct LowerBoundedFeeEstimator<F: FeeEstimator>(pub F);
impl<F: FeeEstimator> LowerBoundedFeeEstimator<F> {
pub fn new(fee_estimator: F) -> Self {
LowerBoundedFeeEstimator(fee_estimator)
}
pub fn bounded_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
cmp::max(self.0.get_est_sat_per_1000_weight(confirmation_target), FEERATE_FLOOR_SATS_PER_KW)
}
}
#[cfg(test)]
mod tests {
use super::{
ConfirmationTarget, FeeEstimator, LowerBoundedFeeEstimator, FEERATE_FLOOR_SATS_PER_KW,
};
struct TestFeeEstimator {
sat_per_kw: u32,
}
impl FeeEstimator for TestFeeEstimator {
fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u32 {
self.sat_per_kw
}
}
#[test]
fn test_fee_estimator_less_than_floor() {
let sat_per_kw = FEERATE_FLOOR_SATS_PER_KW - 1;
let test_fee_estimator = &TestFeeEstimator { sat_per_kw };
let fee_estimator = LowerBoundedFeeEstimator::new(test_fee_estimator);
assert_eq!(
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee),
FEERATE_FLOOR_SATS_PER_KW
);
}
#[test]
fn test_fee_estimator_greater_than_floor() {
let sat_per_kw = FEERATE_FLOOR_SATS_PER_KW + 1;
let test_fee_estimator = &TestFeeEstimator { sat_per_kw };
let fee_estimator = LowerBoundedFeeEstimator::new(test_fee_estimator);
assert_eq!(
fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee),
sat_per_kw
);
}
}