From 85906357272e8004d04c3d579b89abc000460db4 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Sat, 16 Sep 2023 18:41:39 -0500 Subject: [PATCH] Anchors support --- mutiny-core/src/event.rs | 7 ++++++- mutiny-core/src/fees.rs | 4 ++-- mutiny-core/src/node.rs | 17 +++++++++++++++++ mutiny-core/src/onchain.rs | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/mutiny-core/src/event.rs b/mutiny-core/src/event.rs index 4b428f19a..5dad451f7 100644 --- a/mutiny-core/src/event.rs +++ b/mutiny-core/src/event.rs @@ -2,6 +2,7 @@ use crate::fees::MutinyFeeEstimator; use crate::keymanager::PhantomKeysManager; use crate::ldkstorage::{MutinyNodePersister, PhantomChannelManager}; use crate::logging::MutinyLogger; +use crate::node::BumpTxEventHandler; use crate::nodemanager::ChannelClosure; use crate::onchain::OnChainWallet; use crate::redshift::RedshiftStorage; @@ -81,17 +82,20 @@ pub struct EventHandler { wallet: Arc>, keys_manager: Arc>, persister: Arc>, + bump_tx_event_handler: Arc>, lsp_client_pubkey: Option, logger: Arc, } impl EventHandler { + #[allow(clippy::too_many_arguments)] pub(crate) fn new( channel_manager: Arc>, fee_estimator: Arc>, wallet: Arc>, keys_manager: Arc>, persister: Arc>, + bump_tx_event_handler: Arc>, lsp_client_pubkey: Option, logger: Arc, ) -> Self { @@ -102,6 +106,7 @@ impl EventHandler { keys_manager, lsp_client_pubkey, persister, + bump_tx_event_handler, logger, } } @@ -563,7 +568,7 @@ impl EventHandler { } } Event::HTLCIntercepted { .. } => {} - Event::BumpTransaction(_) => {} // we do not support anchors + Event::BumpTransaction(event) => self.bump_tx_event_handler.handle_event(&event), } } diff --git a/mutiny-core/src/fees.rs b/mutiny-core/src/fees.rs index 0bcc89dd2..fc445b3d9 100644 --- a/mutiny-core/src/fees.rs +++ b/mutiny-core/src/fees.rs @@ -174,8 +174,8 @@ impl FeeEstimator for MutinyFeeEstimator { fn num_blocks_from_conf_target(confirmation_target: ConfirmationTarget) -> usize { match confirmation_target { - // MempoolMinimum is only used for anchor channels which we don't support. - // Just setting it to the max confirmation target for now. + // 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 diff --git a/mutiny-core/src/node.rs b/mutiny-core/src/node.rs index 5b45098b5..862d08b2e 100644 --- a/mutiny-core/src/node.rs +++ b/mutiny-core/src/node.rs @@ -40,6 +40,7 @@ use lightning::{ use crate::multiesplora::MultiEsploraClient; use bitcoin::util::bip32::ExtendedPrivKey; +use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet}; use lightning::ln::PaymentSecret; use lightning::sign::{EntropySource, InMemorySigner, NodeSigner, Recipient}; use lightning::util::config::MaxDustHTLCExposure; @@ -81,6 +82,13 @@ const DEFAULT_PAYMENT_TIMEOUT: u64 = 30; const INITIAL_RECONNECTION_DELAY: u64 = 5; const MAX_RECONNECTION_DELAY: u64 = 60; +pub(crate) type BumpTxEventHandler = BumpTransactionEventHandler< + Arc>, + Arc>, Arc>>, + Arc>, + Arc, +>; + pub(crate) type RapidGossipSync = lightning_rapid_gossip_sync::RapidGossipSync, Arc>; @@ -328,6 +336,13 @@ impl Node { Some(ref lsp) => lsp_clients.iter().find(|c| &c.url == lsp).cloned(), }; + let bump_tx_event_handler = Arc::new(BumpTransactionEventHandler::new( + Arc::clone(&chain), + Arc::new(Wallet::new(Arc::clone(&wallet), Arc::clone(&logger))), + Arc::clone(&keys_manager), + Arc::clone(&logger), + )); + let lsp_client_pubkey = lsp_client.clone().map(|lsp| lsp.pubkey); // init event handler @@ -337,6 +352,7 @@ impl Node { wallet.clone(), keys_manager.clone(), persister.clone(), + bump_tx_event_handler, lsp_client_pubkey, logger.clone(), ); @@ -1764,6 +1780,7 @@ pub(crate) fn default_user_config() -> UserConfig { announced_channel: false, negotiate_scid_privacy: true, commit_upfront_shutdown_pubkey: false, + negotiate_anchors_zero_fee_htlc_tx: true, max_inbound_htlc_value_in_flight_percent_of_channel: 100, ..Default::default() }, diff --git a/mutiny-core/src/onchain.rs b/mutiny-core/src/onchain.rs index d4adadafa..95fa15665 100644 --- a/mutiny-core/src/onchain.rs +++ b/mutiny-core/src/onchain.rs @@ -7,6 +7,7 @@ use std::sync::{Arc, RwLock}; use bdk::chain::{BlockId, ConfirmationTime}; use bdk::psbt::PsbtUtils; use bdk::template::DescriptorTemplateOut; +use bdk::wallet::AddressIndex; use bdk::{FeeRate, LocalUtxo, SignOptions, TransactionDetails, Wallet}; use bdk_esplora::EsploraAsyncExt; use bitcoin::consensus::serialize; @@ -15,6 +16,7 @@ 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}; @@ -629,6 +631,39 @@ pub(crate) fn get_esplora_url(network: Network, user_provided_url: Option WalletSource for OnChainWallet { + fn list_confirmed_utxos(&self) -> Result, ()> { + let wallet = self.wallet.try_read().map_err(|_| ())?; + let utxos = wallet + .list_unspent() + .map(|u| Utxo { + outpoint: u.outpoint, + output: u.txout, + satisfaction_weight: 4 + 2 + 64, + }) + .collect(); + + Ok(utxos) + } + + fn get_change_script(&self) -> Result { + let mut wallet = self.wallet.try_write().map_err(|_| ())?; + let addr = wallet.get_internal_address(AddressIndex::New).address; + Ok(addr.script_pubkey()) + } + + fn sign_tx(&self, tx: Transaction) -> Result { + let wallet = self.wallet.try_read().map_err(|_| ())?; + // fixme will this work? + let mut psbt = PartiallySignedTransaction::from_unsigned_tx(tx).map_err(|_| ())?; + wallet + .sign(&mut psbt, SignOptions::default()) + .map_err(|_| ())?; + + Ok(psbt.extract_tx()) + } +} + #[cfg(test)] mod tests { use super::*;