Skip to content

Commit 95bb27a

Browse files
authored
Merge pull request #1689 from wpaulino/anchors-bump-channel-close-event
2 parents e61f3a2 + abe85a1 commit 95bb27a

File tree

8 files changed

+545
-121
lines changed

8 files changed

+545
-121
lines changed

lightning/src/chain/chainmonitor.rs

+24
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
707707
L::Target: Logger,
708708
P::Target: Persist<ChannelSigner>,
709709
{
710+
#[cfg(not(anchors))]
710711
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
711712
///
712713
/// An [`EventHandler`] may safely call back to the provider, though this shouldn't be needed in
@@ -722,6 +723,29 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
722723
handler.handle_event(&event);
723724
}
724725
}
726+
#[cfg(anchors)]
727+
/// Processes [`SpendableOutputs`] events produced from each [`ChannelMonitor`] upon maturity.
728+
///
729+
/// For channels featuring anchor outputs, this method will also process [`BumpTransaction`]
730+
/// events produced from each [`ChannelMonitor`] while there is a balance to claim onchain
731+
/// within each channel. As the confirmation of a commitment transaction may be critical to the
732+
/// safety of funds, this method must be invoked frequently, ideally once for every chain tip
733+
/// update (block connected or disconnected).
734+
///
735+
/// An [`EventHandler`] may safely call back to the provider, though this shouldn't be needed in
736+
/// order to handle these events.
737+
///
738+
/// [`SpendableOutputs`]: events::Event::SpendableOutputs
739+
/// [`BumpTransaction`]: events::Event::BumpTransaction
740+
fn process_pending_events<H: Deref>(&self, handler: H) where H::Target: EventHandler {
741+
let mut pending_events = Vec::new();
742+
for monitor_state in self.monitors.read().unwrap().values() {
743+
pending_events.append(&mut monitor_state.monitor.get_and_clear_pending_events());
744+
}
745+
for event in pending_events.drain(..) {
746+
handler.handle_event(&event);
747+
}
748+
}
725749
}
726750

727751
#[cfg(test)]

lightning/src/chain/channelmonitor.rs

+84-15
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
//! ChannelMonitors to get out of the HSM and onto monitoring devices.
2222
2323
use bitcoin::blockdata::block::BlockHeader;
24-
use bitcoin::blockdata::transaction::{TxOut,Transaction};
25-
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
24+
use bitcoin::blockdata::transaction::{OutPoint as BitcoinOutPoint, TxOut, Transaction};
2625
use bitcoin::blockdata::script::{Script, Builder};
2726
use bitcoin::blockdata::opcodes;
2827

@@ -44,13 +43,17 @@ use chain::{BestBlock, WatchedOutput};
4443
use chain::chaininterface::{BroadcasterInterface, FeeEstimator, LowerBoundedFeeEstimator};
4544
use chain::transaction::{OutPoint, TransactionData};
4645
use chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
46+
#[cfg(anchors)]
47+
use chain::onchaintx::ClaimEvent;
4748
use chain::onchaintx::OnchainTxHandler;
4849
use chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput};
4950
use chain::Filter;
5051
use util::logger::Logger;
5152
use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48, OptionDeserWrapper};
5253
use util::byte_utils;
5354
use util::events::Event;
55+
#[cfg(anchors)]
56+
use util::events::{AnchorDescriptor, BumpTransactionEvent};
5457

5558
use prelude::*;
5659
use core::{cmp, mem};
@@ -263,6 +266,20 @@ impl_writeable_tlv_based!(HolderSignedTx, {
263266
(14, htlc_outputs, vec_type)
264267
});
265268

269+
#[cfg(anchors)]
270+
impl HolderSignedTx {
271+
fn non_dust_htlcs(&self) -> Vec<HTLCOutputInCommitment> {
272+
self.htlc_outputs.iter().filter_map(|(htlc, _, _)| {
273+
if let Some(_) = htlc.transaction_output_index {
274+
Some(htlc.clone())
275+
} else {
276+
None
277+
}
278+
})
279+
.collect()
280+
}
281+
}
282+
266283
/// We use this to track static counterparty commitment transaction data and to generate any
267284
/// justice or 2nd-stage preimage/timeout transactions.
268285
#[derive(PartialEq, Eq)]
@@ -1221,7 +1238,7 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
12211238
B::Target: BroadcasterInterface,
12221239
L::Target: Logger,
12231240
{
1224-
self.inner.lock().unwrap().broadcast_latest_holder_commitment_txn(broadcaster, logger)
1241+
self.inner.lock().unwrap().broadcast_latest_holder_commitment_txn(broadcaster, logger);
12251242
}
12261243

12271244
/// Updates a ChannelMonitor on the basis of some new information provided by the Channel
@@ -2222,6 +2239,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22222239
panic!("Attempted to apply ChannelMonitorUpdates out of order, check the update_id before passing an update to update_monitor!");
22232240
}
22242241
let mut ret = Ok(());
2242+
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&*fee_estimator);
22252243
for update in updates.updates.iter() {
22262244
match update {
22272245
ChannelMonitorUpdateStep::LatestHolderCommitmentTXInfo { commitment_tx, htlc_outputs } => {
@@ -2239,7 +2257,6 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22392257
},
22402258
ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage } => {
22412259
log_trace!(logger, "Updating ChannelMonitor with payment preimage");
2242-
let bounded_fee_estimator = LowerBoundedFeeEstimator::new(&*fee_estimator);
22432260
self.provide_payment_preimage(&PaymentHash(Sha256::hash(&payment_preimage.0[..]).into_inner()), &payment_preimage, broadcaster, &bounded_fee_estimator, logger)
22442261
},
22452262
ChannelMonitorUpdateStep::CommitmentSecret { idx, secret } => {
@@ -2255,6 +2272,25 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22552272
self.lockdown_from_offchain = true;
22562273
if *should_broadcast {
22572274
self.broadcast_latest_holder_commitment_txn(broadcaster, logger);
2275+
// If the channel supports anchor outputs, we'll need to emit an external
2276+
// event to be consumed such that a child transaction is broadcast with a
2277+
// high enough feerate for the parent commitment transaction to confirm.
2278+
if self.onchain_tx_handler.opt_anchors() {
2279+
let funding_output = HolderFundingOutput::build(
2280+
self.funding_redeemscript.clone(), self.channel_value_satoshis,
2281+
self.onchain_tx_handler.opt_anchors(),
2282+
);
2283+
let best_block_height = self.best_block.height();
2284+
let commitment_package = PackageTemplate::build_package(
2285+
self.funding_info.0.txid.clone(), self.funding_info.0.index as u32,
2286+
PackageSolvingData::HolderFundingOutput(funding_output),
2287+
best_block_height, false, best_block_height,
2288+
);
2289+
self.onchain_tx_handler.update_claims_view(
2290+
&[], vec![commitment_package], best_block_height, best_block_height,
2291+
broadcaster, &bounded_fee_estimator, logger,
2292+
);
2293+
}
22582294
} else if !self.holder_tx_signed {
22592295
log_error!(logger, "WARNING: You have a potentially-unsafe holder commitment transaction available to broadcast");
22602296
log_error!(logger, " in channel monitor for channel {}!", log_bytes!(self.funding_info.0.to_channel_id()));
@@ -2309,6 +2345,34 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23092345
pub fn get_and_clear_pending_events(&mut self) -> Vec<Event> {
23102346
let mut ret = Vec::new();
23112347
mem::swap(&mut ret, &mut self.pending_events);
2348+
#[cfg(anchors)]
2349+
for claim_event in self.onchain_tx_handler.get_and_clear_pending_claim_events().drain(..) {
2350+
match claim_event {
2351+
ClaimEvent::BumpCommitment {
2352+
package_target_feerate_sat_per_1000_weight, commitment_tx, anchor_output_idx,
2353+
} => {
2354+
let commitment_txid = commitment_tx.txid();
2355+
debug_assert_eq!(self.current_holder_commitment_tx.txid, commitment_txid);
2356+
let pending_htlcs = self.current_holder_commitment_tx.non_dust_htlcs();
2357+
let commitment_tx_fee_satoshis = self.channel_value_satoshis -
2358+
commitment_tx.output.iter().fold(0u64, |sum, output| sum + output.value);
2359+
ret.push(Event::BumpTransaction(BumpTransactionEvent::ChannelClose {
2360+
package_target_feerate_sat_per_1000_weight,
2361+
commitment_tx,
2362+
commitment_tx_fee_satoshis,
2363+
anchor_descriptor: AnchorDescriptor {
2364+
channel_keys_id: self.channel_keys_id,
2365+
channel_value_satoshis: self.channel_value_satoshis,
2366+
outpoint: BitcoinOutPoint {
2367+
txid: commitment_txid,
2368+
vout: anchor_output_idx,
2369+
},
2370+
},
2371+
pending_htlcs,
2372+
}));
2373+
},
2374+
}
2375+
}
23122376
ret
23132377
}
23142378

@@ -2521,13 +2585,13 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
25212585
CounterpartyOfferedHTLCOutput::build(*per_commitment_point,
25222586
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
25232587
self.counterparty_commitment_params.counterparty_htlc_base_key,
2524-
preimage.unwrap(), htlc.clone()))
2588+
preimage.unwrap(), htlc.clone(), self.onchain_tx_handler.opt_anchors()))
25252589
} else {
25262590
PackageSolvingData::CounterpartyReceivedHTLCOutput(
25272591
CounterpartyReceivedHTLCOutput::build(*per_commitment_point,
25282592
self.counterparty_commitment_params.counterparty_delayed_payment_base_key,
25292593
self.counterparty_commitment_params.counterparty_htlc_base_key,
2530-
htlc.clone()))
2594+
htlc.clone(), self.onchain_tx_handler.opt_anchors()))
25312595
};
25322596
let aggregation = if !htlc.offered { false } else { true };
25332597
let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry,aggregation, 0);
@@ -2884,21 +2948,26 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
28842948

28852949
let should_broadcast = self.should_broadcast_holder_commitment_txn(logger);
28862950
if should_broadcast {
2887-
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone());
2951+
let funding_outp = HolderFundingOutput::build(self.funding_redeemscript.clone(), self.channel_value_satoshis, self.onchain_tx_handler.opt_anchors());
28882952
let commitment_package = PackageTemplate::build_package(self.funding_info.0.txid.clone(), self.funding_info.0.index as u32, PackageSolvingData::HolderFundingOutput(funding_outp), self.best_block.height(), false, self.best_block.height());
28892953
claimable_outpoints.push(commitment_package);
28902954
self.pending_monitor_events.push(MonitorEvent::CommitmentTxConfirmed(self.funding_info.0));
28912955
let commitment_tx = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript);
28922956
self.holder_tx_signed = true;
2893-
// Because we're broadcasting a commitment transaction, we should construct the package
2894-
// assuming it gets confirmed in the next block. Sadly, we have code which considers
2895-
// "not yet confirmed" things as discardable, so we cannot do that here.
2896-
let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, self.best_block.height());
2897-
let new_outputs = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, &commitment_tx);
2898-
if !new_outputs.is_empty() {
2899-
watch_outputs.push((self.current_holder_commitment_tx.txid.clone(), new_outputs));
2957+
// We can't broadcast our HTLC transactions while the commitment transaction is
2958+
// unconfirmed. We'll delay doing so until we detect the confirmed commitment in
2959+
// `transactions_confirmed`.
2960+
if !self.onchain_tx_handler.opt_anchors() {
2961+
// Because we're broadcasting a commitment transaction, we should construct the package
2962+
// assuming it gets confirmed in the next block. Sadly, we have code which considers
2963+
// "not yet confirmed" things as discardable, so we cannot do that here.
2964+
let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx, self.best_block.height());
2965+
let new_outputs = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, &commitment_tx);
2966+
if !new_outputs.is_empty() {
2967+
watch_outputs.push((self.current_holder_commitment_tx.txid.clone(), new_outputs));
2968+
}
2969+
claimable_outpoints.append(&mut new_outpoints);
29002970
}
2901-
claimable_outpoints.append(&mut new_outpoints);
29022971
}
29032972

29042973
// Find which on-chain events have reached their confirmation threshold.

lightning/src/chain/keysinterface.rs

+18
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use util::crypto::{hkdf_extract_expand_twice, sign};
3636
use util::ser::{Writeable, Writer, Readable, ReadableArgs};
3737

3838
use chain::transaction::OutPoint;
39+
use ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
3940
use ln::{chan_utils, PaymentPreimage};
4041
use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
4142
use ln::msgs::UnsignedChannelAnnouncement;
@@ -348,6 +349,12 @@ pub trait BaseSign {
348349
/// chosen to forgo their output as dust.
349350
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
350351

352+
/// Computes the signature for a commitment transaction's anchor output used as an
353+
/// input within `anchor_tx`, which spends the commitment transaction, at index `input`.
354+
fn sign_holder_anchor_input(
355+
&self, anchor_tx: &mut Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
356+
) -> Result<Signature, ()>;
357+
351358
/// Signs a channel announcement message with our funding key and our node secret key (aka
352359
/// node_id or network_key), proving it comes from one of the channel participants.
353360
///
@@ -645,6 +652,7 @@ impl InMemorySigner {
645652
witness.push(witness_script.clone().into_bytes());
646653
Ok(witness)
647654
}
655+
648656
}
649657

650658
impl BaseSign for InMemorySigner {
@@ -762,6 +770,16 @@ impl BaseSign for InMemorySigner {
762770
Ok(closing_tx.trust().sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
763771
}
764772

773+
fn sign_holder_anchor_input(
774+
&self, anchor_tx: &mut Transaction, input: usize, secp_ctx: &Secp256k1<secp256k1::All>,
775+
) -> Result<Signature, ()> {
776+
let witness_script = chan_utils::get_anchor_redeemscript(&self.holder_channel_pubkeys.funding_pubkey);
777+
let sighash = sighash::SighashCache::new(&*anchor_tx).segwit_signature_hash(
778+
input, &witness_script, ANCHOR_OUTPUT_VALUE_SATOSHI, EcdsaSighashType::All,
779+
).unwrap();
780+
Ok(sign(secp_ctx, &hash_to_message!(&sighash[..]), &self.funding_key))
781+
}
782+
765783
fn sign_channel_announcement(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>)
766784
-> Result<(Signature, Signature), ()> {
767785
let msghash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);

0 commit comments

Comments
 (0)