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

Anchors support #762

Merged
merged 1 commit into from
Sep 27, 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
7 changes: 6 additions & 1 deletion mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,17 +82,20 @@ pub struct EventHandler<S: MutinyStorage> {
wallet: Arc<OnChainWallet<S>>,
keys_manager: Arc<PhantomKeysManager<S>>,
persister: Arc<MutinyNodePersister<S>>,
bump_tx_event_handler: Arc<BumpTxEventHandler<S>>,
lsp_client_pubkey: Option<PublicKey>,
logger: Arc<MutinyLogger>,
}

impl<S: MutinyStorage> EventHandler<S> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
channel_manager: Arc<PhantomChannelManager<S>>,
fee_estimator: Arc<MutinyFeeEstimator<S>>,
wallet: Arc<OnChainWallet<S>>,
keys_manager: Arc<PhantomKeysManager<S>>,
persister: Arc<MutinyNodePersister<S>>,
bump_tx_event_handler: Arc<BumpTxEventHandler<S>>,
lsp_client_pubkey: Option<PublicKey>,
logger: Arc<MutinyLogger>,
) -> Self {
Expand All @@ -102,6 +106,7 @@ impl<S: MutinyStorage> EventHandler<S> {
keys_manager,
lsp_client_pubkey,
persister,
bump_tx_event_handler,
logger,
}
}
Expand Down Expand Up @@ -563,7 +568,7 @@ impl<S: MutinyStorage> EventHandler<S> {
}
}
Event::HTLCIntercepted { .. } => {}
Event::BumpTransaction(_) => {} // we do not support anchors
Event::BumpTransaction(event) => self.bump_tx_event_handler.handle_event(&event),
}
}

Expand Down
4 changes: 2 additions & 2 deletions mutiny-core/src/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ impl<S: MutinyStorage> FeeEstimator for MutinyFeeEstimator<S> {

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
Expand Down
17 changes: 17 additions & 0 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<S: MutinyStorage> = BumpTransactionEventHandler<
Arc<MutinyChain<S>>,
Arc<Wallet<Arc<OnChainWallet<S>>, Arc<MutinyLogger>>>,
Arc<PhantomKeysManager<S>>,
Arc<MutinyLogger>,
>;

pub(crate) type RapidGossipSync =
lightning_rapid_gossip_sync::RapidGossipSync<Arc<NetworkGraph>, Arc<MutinyLogger>>;

Expand Down Expand Up @@ -328,6 +336,13 @@ impl<S: MutinyStorage> Node<S> {
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
Expand All @@ -337,6 +352,7 @@ impl<S: MutinyStorage> Node<S> {
wallet.clone(),
keys_manager.clone(),
persister.clone(),
bump_tx_event_handler,
lsp_client_pubkey,
logger.clone(),
);
Expand Down Expand Up @@ -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()
},
Expand Down
35 changes: 35 additions & 0 deletions mutiny-core/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};

Expand Down Expand Up @@ -629,6 +631,39 @@ pub(crate) fn get_esplora_url(network: Network, user_provided_url: Option<String
}
}

impl<S: MutinyStorage> WalletSource for OnChainWallet<S> {
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
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<Script, ()> {
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<Transaction, ()> {
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::*;
Expand Down