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

LDK 0.0.118 #804

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
uuid = { version = "1.1.2", features = ["v4"] }
esplora-client = { version = "0.5", default-features = false }
lightning = { version = "0.0.117", default-features = false, features = ["max_level_trace", "grind_signatures", "no-std"] }
lightning-invoice = { version = "0.25.0", default-features = false, features = ["no-std", "serde"] }
lightning-rapid-gossip-sync = { version = "0.0.117", default-features = false, features = ["no-std"] }
lightning = { version = "0.0.118", default-features = false, features = ["max_level_trace", "grind_signatures", "no-std"] }
lightning-invoice = { version = "0.26.0", default-features = false, features = ["no-std", "serde"] }
lightning-rapid-gossip-sync = { version = "0.0.118", default-features = false, features = ["no-std"] }
chrono = "0.4.22"
futures-util = { version = "0.3", default-features = false }
reqwest = { version = "0.11", default-features = false, features = ["json"] }
Expand Down
13 changes: 6 additions & 7 deletions mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use core::fmt;
use lightning::events::{Event, PaymentPurpose};
use lightning::sign::SpendableOutputDescriptor;
use lightning::{
chain::chaininterface::{ConfirmationTarget, FeeEstimator},
log_debug, log_error, log_info, log_warn,
util::errors::APIError,
util::logger::Logger,
log_debug, log_error, log_info, log_warn, util::errors::APIError, util::logger::Logger,
};
use lightning_invoice::Bolt11Invoice;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -571,6 +568,10 @@ impl<S: MutinyStorage> EventHandler<S> {
}
Event::HTLCIntercepted { .. } => {}
Event::BumpTransaction(event) => self.bump_tx_event_handler.handle_event(&event),
Event::InvoiceRequestFailed { payment_id } => {
// we don't support bolt 12 yet
log_warn!(self.logger, "EVENT: InvoiceRequestFailed: {payment_id}");
}
}
}

Expand Down Expand Up @@ -604,9 +605,7 @@ impl<S: MutinyStorage> EventHandler<S> {
output_descriptors.len()
);

let tx_feerate = self
.fee_estimator
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let tx_feerate = self.fee_estimator.get_normal_fee_rate();

// We set nLockTime to the current height to discourage fee sniping.
// Occasionally randomly pick a nLockTime even further back, so
Expand Down
82 changes: 51 additions & 31 deletions mutiny-core/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ impl<S: MutinyStorage> MutinyFeeEstimator<S> {
sats_per_kw: Option<u32>,
) -> u64 {
// if no fee rate is provided, use the normal confirmation target
let sats_per_kw = sats_per_kw
.unwrap_or_else(|| self.get_est_sat_per_1000_weight(ConfirmationTarget::Normal));
let sats_per_kw = sats_per_kw.unwrap_or_else(|| self.get_normal_fee_rate());
let expected_weight = {
// Calculate the non-witness and witness data sizes
let non_witness_size = TX_OVERHEAD
Expand Down Expand Up @@ -144,14 +143,29 @@ impl<S: MutinyStorage> MutinyFeeEstimator<S> {

Ok(())
}

pub fn get_low_fee_rate(&self) -> u32 {
// MinAllowedNonAnchorChannelRemoteFee is a fee rate we expect to get slowly
self.get_est_sat_per_1000_weight(ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee)
}

pub fn get_normal_fee_rate(&self) -> u32 {
// NonAnchorChannelFee is a fee rate we expect to be confirmed in 6 blocks
self.get_est_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee)
}

pub fn get_high_fee_rate(&self) -> u32 {
// OnChainSweep is the highest fee rate we have, so use that
self.get_est_sat_per_1000_weight(ConfirmationTarget::OnChainSweep)
}
}

impl<S: MutinyStorage> FeeEstimator for MutinyFeeEstimator<S> {
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
let num_blocks = num_blocks_from_conf_target(confirmation_target);
let fallback_fee = fallback_fee_from_conf_target(confirmation_target);

match self.storage.get_fee_estimates() {
let fee = match self.storage.get_fee_estimates() {
Err(_) | Ok(None) => fallback_fee,
Ok(Some(estimates)) => {
let found = estimates.get(&num_blocks.to_string());
Expand All @@ -160,43 +174,46 @@ impl<S: MutinyStorage> FeeEstimator for MutinyFeeEstimator<S> {
log_trace!(self.logger, "Got fee rate from saved cache!");
let sats_vbyte = num.to_owned();
// convert to sats per kw
let mut fee_rate = sats_vbyte * 250.0;

// if we're using the high priority target, multiply by 3
// this should help prevent force closures from fee disputes
if confirmation_target == ConfirmationTarget::HighPriority {
fee_rate *= 3.0;
}
let fee_rate = sats_vbyte * 250.0;

// return the fee rate, but make sure it's not lower than the floor
(fee_rate as u32).max(FEERATE_FLOOR_SATS_PER_KW)
}
None => fallback_fee,
}
}
};

// any post processing we do after the we get the fee rate from the cache
match confirmation_target {
ConfirmationTarget::MaxAllowedNonAnchorChannelRemoteFee => fee * 30, // multiply by 30 to help prevent force closes
ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => fee - 250, // helps with rounding errors
_ => fee,
}
}
}

fn num_blocks_from_conf_target(confirmation_target: ConfirmationTarget) -> usize {
match confirmation_target {
// MempoolMinimum is only used for anchor channels, we just set the target to 1008
// as that is esplora's highest block target available
ConfirmationTarget::MempoolMinimum => 1008,
// Background is VERY lax and may never confirm if used directly
// it is only meant for lower ranges of transaction to enter mempool
ConfirmationTarget::Background => 1008,
ConfirmationTarget::Normal => 6,
ConfirmationTarget::HighPriority => 1,
ConfirmationTarget::AnchorChannelFee => 1008,
ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => 1008,
ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 1008,
ConfirmationTarget::ChannelCloseMinimum => 1008,
ConfirmationTarget::NonAnchorChannelFee => 6,
ConfirmationTarget::MaxAllowedNonAnchorChannelRemoteFee => 1,
ConfirmationTarget::OnChainSweep => 1,
}
}

fn fallback_fee_from_conf_target(confirmation_target: ConfirmationTarget) -> u32 {
match confirmation_target {
ConfirmationTarget::MempoolMinimum => 3 * 250,
ConfirmationTarget::Background => 10 * 250,
ConfirmationTarget::Normal => 20 * 250,
ConfirmationTarget::HighPriority => 50 * 250,
ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => 3 * 250,
ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => 3 * 250,
ConfirmationTarget::ChannelCloseMinimum => 10 * 250,
ConfirmationTarget::AnchorChannelFee => 10 * 250,
ConfirmationTarget::NonAnchorChannelFee => 20 * 250,
ConfirmationTarget::MaxAllowedNonAnchorChannelRemoteFee => 50 * 250,
ConfirmationTarget::OnChainSweep => 50 * 250,
}
}

Expand Down Expand Up @@ -229,28 +246,31 @@ mod test {
#[test]
fn test_num_blocks_from_conf_target() {
assert_eq!(
num_blocks_from_conf_target(ConfirmationTarget::Background),
num_blocks_from_conf_target(ConfirmationTarget::ChannelCloseMinimum),
1008
);
assert_eq!(num_blocks_from_conf_target(ConfirmationTarget::Normal), 6);
assert_eq!(
num_blocks_from_conf_target(ConfirmationTarget::HighPriority),
num_blocks_from_conf_target(ConfirmationTarget::NonAnchorChannelFee),
6
);
assert_eq!(
num_blocks_from_conf_target(ConfirmationTarget::OnChainSweep),
1
);
}

#[test]
fn test_fallback_fee_from_conf_target() {
assert_eq!(
fallback_fee_from_conf_target(ConfirmationTarget::Background),
fallback_fee_from_conf_target(ConfirmationTarget::ChannelCloseMinimum),
2_500
);
assert_eq!(
fallback_fee_from_conf_target(ConfirmationTarget::Normal),
fallback_fee_from_conf_target(ConfirmationTarget::NonAnchorChannelFee),
5_000
);
assert_eq!(
fallback_fee_from_conf_target(ConfirmationTarget::HighPriority),
fallback_fee_from_conf_target(ConfirmationTarget::OnChainSweep),
12_500
);
}
Expand Down Expand Up @@ -288,17 +308,17 @@ mod test {

// test that we get the fee rate from the cache
assert_eq!(
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal),
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee),
2500
);

// test that we get the fallback fee rate
assert_eq!(
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background),
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::ChannelCloseMinimum),
2_500
);
assert_eq!(
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority),
fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::OnChainSweep),
12_500
);
}
Expand Down
12 changes: 3 additions & 9 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use core::time::Duration;
use lightning::chain::channelmonitor::ChannelMonitor;
use lightning::util::ser::{ReadableArgs, Writeable};
use lightning::{
chain::chaininterface::{ConfirmationTarget, FeeEstimator},
ln::channelmanager::{RecipientOnionFields, RetryableSendFailure},
routing::scoring::ProbabilisticScoringFeeParameters,
util::config::ChannelConfig,
Expand Down Expand Up @@ -1409,10 +1408,7 @@ impl<S: MutinyStorage> Node<S> {
let sats_per_vbyte = if let Some(sats_vbyte) = fee_rate {
sats_vbyte
} else {
let sats_per_kw = self
.wallet
.fees
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let sats_per_kw = self.wallet.fees.get_normal_fee_rate();

FeeRate::from_sat_per_kwu(sats_per_kw as f32).as_sat_per_vb()
};
Expand Down Expand Up @@ -1483,10 +1479,8 @@ impl<S: MutinyStorage> Node<S> {
total
};

let sats_per_kw = self
.wallet
.fees
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let sats_per_kw = self.wallet.fees.get_normal_fee_rate();

// Calculate the expected transaction fee
let expected_fee = self.wallet.fees.calculate_expected_fee(
utxos.len(),
Expand Down
17 changes: 4 additions & 13 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ use bitcoin::{Address, Network, OutPoint, Transaction, Txid};
use core::time::Duration;
use esplora_client::Builder;
use futures::{future::join_all, lock::Mutex};
use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator};
use lightning::chain::Confirm;
use lightning::events::ClosureReason;
use lightning::io::Read;
Expand Down Expand Up @@ -1460,25 +1459,19 @@ impl<S: MutinyStorage> NodeManager<S> {
/// Gets a fee estimate for a very low priority transaction.
/// Value is in sat/vbyte.
pub fn estimate_fee_low(&self) -> u32 {
self.fee_estimator
.get_est_sat_per_1000_weight(ConfirmationTarget::Background)
/ 250
self.fee_estimator.get_low_fee_rate() / 250
}

/// Gets a fee estimate for an average priority transaction.
/// Value is in sat/vbyte.
pub fn estimate_fee_normal(&self) -> u32 {
self.fee_estimator
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal)
/ 250
self.fee_estimator.get_normal_fee_rate() / 250
}

/// Gets a fee estimate for an high priority transaction.
/// Value is in sat/vbyte.
pub fn estimate_fee_high(&self) -> u32 {
self.fee_estimator
.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority)
/ 250
self.fee_estimator.get_high_fee_rate() / 250
}

/// Creates a new lightning node and adds it to the manager.
Expand Down Expand Up @@ -2077,9 +2070,7 @@ impl<S: MutinyStorage> NodeManager<S> {

// ldk uses background fee rate for closing channels which can be very slow
// so we use normal fee rate instead
let fee_rate = self
.fee_estimator
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let fee_rate = self.wallet.fees.get_normal_fee_rate();

node.channel_manager
.close_channel_with_feerate_and_script(
Expand Down
9 changes: 2 additions & 7 deletions mutiny-core/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use bitcoin::hashes::hex::ToHex;
use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey};
use bitcoin::{Address, Network, OutPoint, Script, Transaction, Txid};
use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator};
use lightning::events::bump_transaction::{Utxo, WalletSource};
use lightning::util::logger::Logger;
use lightning::{log_debug, log_error, log_info, log_warn};
Expand Down Expand Up @@ -447,9 +446,7 @@ impl<S: MutinyStorage> OnChainWallet<S> {
let fee_rate = if let Some(rate) = fee_rate {
FeeRate::from_sat_per_vb(rate)
} else {
let sat_per_kwu = self
.fees
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let sat_per_kwu = self.fees.get_normal_fee_rate();
FeeRate::from_sat_per_kwu(sat_per_kwu as f32)
};
let (mut psbt, details) = {
Expand Down Expand Up @@ -495,9 +492,7 @@ impl<S: MutinyStorage> OnChainWallet<S> {
let fee_rate = if let Some(rate) = fee_rate {
FeeRate::from_sat_per_vb(rate)
} else {
let sat_per_kwu = self
.fees
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let sat_per_kwu = self.fees.get_normal_fee_rate();
FeeRate::from_sat_per_kwu(sat_per_kwu as f32)
};
let (mut psbt, details) = {
Expand Down
12 changes: 0 additions & 12 deletions mutiny-core/src/peermanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::{net::SocketAddr, sync::atomic::AtomicBool};

use crate::networking::socket::{schedule_descriptor_read, MutinySocketDescriptor};
use crate::scb::message_handler::SCBMessageHandler;
use bitcoin::BlockHash;
use lightning::events::{MessageSendEvent, MessageSendEventsProvider};
use lightning::ln::features::{InitFeatures, NodeFeatures};
use lightning::ln::msgs;
Expand All @@ -22,7 +21,6 @@ use lightning::ln::peer_handler::PeerHandleError;
use lightning::ln::peer_handler::{IgnoringMessageHandler, PeerManager as LdkPeerManager};
use lightning::log_warn;
use lightning::routing::gossip::NodeId;
use lightning::routing::utxo::{UtxoLookup, UtxoLookupError, UtxoResult};
use lightning::util::logger::Logger;
use std::sync::Arc;

Expand Down Expand Up @@ -179,16 +177,6 @@ impl<S: MutinyStorage> MessageSendEventsProvider for GossipMessageHandler<S> {
}
}

// I needed some type to implement RoutingMessageHandler, but I don't want to implement it
// we don't need to lookup UTXOs, so we can just return an error
// This should never actually be called because we are passing in None for the UTXO lookup
struct ErroringUtxoLookup {}
impl UtxoLookup for ErroringUtxoLookup {
fn get_utxo(&self, _genesis_hash: &BlockHash, _short_channel_id: u64) -> UtxoResult {
UtxoResult::Sync(Err(UtxoLookupError::UnknownTx))
}
}

impl<S: MutinyStorage> RoutingMessageHandler for GossipMessageHandler<S> {
fn handle_node_announcement(
&self,
Expand Down
4 changes: 2 additions & 2 deletions mutiny-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ wasm-bindgen-futures = "0.4.33"
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
bitcoin = { version = "0.29.2", default-features = false, features = ["serde", "secp-recovery", "rand"] }
lightning = { version = "0.0.117", default-features = false, features = ["no-std"] }
lightning-invoice = { version = "0.25.0", default-features = false, features = ["no-std"] }
lightning = { version = "0.0.118", default-features = false, features = ["no-std"] }
lightning-invoice = { version = "0.26.0", default-features = false, features = ["no-std"] }
thiserror = "1.0"
instant = { version = "0.1", features = ["wasm-bindgen"] }
lnurl-rs = { version = "0.2.6", default-features = false }
Expand Down
Loading