Skip to content

Commit e937351

Browse files
committed
Add ChannelPending event emitted upon funding_signed
Currently, users don't have good way of being notified when channel open negotiations have succeeded and new channels are pending confirmation on chain. To this end, we add a new `ChannelPending` event that is emitted when send or receive a `funding_signed` message, i.e., at the last moment before waiting for the confirmation period.
1 parent 23c1b46 commit e937351

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,7 +4564,10 @@ where
45644564
msg: funding_msg,
45654565
});
45664566

4567-
let monitor_res = self.chain_monitor.watch_channel(monitor.get_funding_txo().0, monitor);
4567+
let funding_txo = monitor.get_funding_txo().0;
4568+
let user_channel_id = chan.get_user_id();
4569+
4570+
let monitor_res = self.chain_monitor.watch_channel(funding_txo, monitor);
45684571

45694572
let chan = e.insert(chan);
45704573
let mut res = handle_new_monitor_update!(self, monitor_res, 0, peer_state_lock, peer_state,
@@ -4579,6 +4582,14 @@ where
45794582
// with the funding_signed so the channel can never go on chain).
45804583
if let Err(MsgHandleErrInternal { shutdown_finish: Some((res, _)), .. }) = &mut res {
45814584
res.0 = None;
4585+
} else {
4586+
let mut pending_events = self.pending_events.lock().unwrap();
4587+
pending_events.push(events::Event::ChannelPending {
4588+
channel_id: new_channel_id,
4589+
counterparty_node_id: *counterparty_node_id,
4590+
user_channel_id,
4591+
funding_txo: funding_txo.into_bitcoin_outpoint(),
4592+
});
45824593
}
45834594
res
45844595
}
@@ -4600,7 +4611,9 @@ where
46004611
hash_map::Entry::Occupied(mut chan) => {
46014612
let monitor = try_chan_entry!(self,
46024613
chan.get_mut().funding_signed(&msg, best_block, &self.signer_provider, &self.logger), chan);
4603-
let update_res = self.chain_monitor.watch_channel(chan.get().get_funding_txo().unwrap(), monitor);
4614+
let funding_txo = chan.get().get_funding_txo().unwrap();
4615+
let user_channel_id = chan.get().get_user_id();
4616+
let update_res = self.chain_monitor.watch_channel(funding_txo, monitor);
46044617
let mut res = handle_new_monitor_update!(self, update_res, 0, peer_state_lock, peer_state, per_peer_state, chan);
46054618
if let Err(MsgHandleErrInternal { ref mut shutdown_finish, .. }) = res {
46064619
// We weren't able to watch the channel to begin with, so no updates should be made on
@@ -4609,6 +4622,14 @@ where
46094622
if let Some((ref mut shutdown_finish, _)) = shutdown_finish {
46104623
shutdown_finish.0.take();
46114624
}
4625+
} else {
4626+
let mut pending_events = self.pending_events.lock().unwrap();
4627+
pending_events.push(events::Event::ChannelPending {
4628+
channel_id: msg.channel_id,
4629+
counterparty_node_id: *counterparty_node_id,
4630+
user_channel_id,
4631+
funding_txo: funding_txo.into_bitcoin_outpoint(),
4632+
});
46124633
}
46134634
res
46144635
},

lightning/src/util/events.rs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use crate::util::errors::APIError;
2727
use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReadable, Readable, RequiredWrapper, UpgradableRequired, WithoutLength};
2828
use crate::routing::router::{RouteHop, RouteParameters};
2929

30-
use bitcoin::{PackedLockTime, Transaction};
30+
use bitcoin::{PackedLockTime, Transaction, OutPoint};
3131
#[cfg(anchors)]
32-
use bitcoin::{OutPoint, Txid, TxIn, TxOut, Witness};
32+
use bitcoin::{Txid, TxIn, TxOut, Witness};
3333
use bitcoin::blockdata::script::Script;
3434
use bitcoin::hashes::Hash;
3535
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -821,12 +821,33 @@ pub enum Event {
821821
/// transaction.
822822
claim_from_onchain_tx: bool,
823823
},
824+
/// Used to indicate that a channel with the given `channel_id` is being opened and pending
825+
/// confirmation on-chain.
826+
///
827+
/// This event is emitted when the funding transaction has been signed.
828+
ChannelPending {
829+
/// The `channel_id` of the channel that is pending confirmation.
830+
channel_id: [u8; 32],
831+
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
832+
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
833+
/// [`UserConfig::manually_accept_inbound_channels`] config flag is set to true. Otherwise
834+
/// `user_channel_id` will be randomized for an inbound channel.
835+
///
836+
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
837+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
838+
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
839+
user_channel_id: u128,
840+
/// The `node_id` of the channel counterparty.
841+
counterparty_node_id: PublicKey,
842+
/// The outpoint of the channel's funding transaction.
843+
funding_txo: OutPoint,
844+
},
824845
/// Used to indicate that a channel with the given `channel_id` is ready to
825846
/// be used. This event is emitted either when the funding transaction has been confirmed
826847
/// on-chain, or, in case of a 0conf channel, when both parties have confirmed the channel
827848
/// establishment.
828849
ChannelReady {
829-
/// The channel_id of the channel that is ready.
850+
/// The `channel_id` of the channel that is ready.
830851
channel_id: [u8; 32],
831852
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
832853
/// channels, or to [`ChannelManager::accept_inbound_channel`] for inbound channels if
@@ -837,15 +858,15 @@ pub enum Event {
837858
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
838859
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
839860
user_channel_id: u128,
840-
/// The node_id of the channel counterparty.
861+
/// The `node_id` of the channel counterparty.
841862
counterparty_node_id: PublicKey,
842863
/// The features that this channel will operate with.
843864
channel_type: ChannelTypeFeatures,
844865
},
845866
/// Used to indicate that a previously opened channel with the given `channel_id` is in the
846867
/// process of closure.
847868
ChannelClosed {
848-
/// The channel_id of the channel which has been closed. Note that on-chain transactions
869+
/// The `channel_id` of the channel which has been closed. Note that on-chain transactions
849870
/// resolving the channel are likely still awaiting confirmation.
850871
channel_id: [u8; 32],
851872
/// The `user_channel_id` value passed in to [`ChannelManager::create_channel`] for outbound
@@ -1140,6 +1161,15 @@ impl Writeable for Event {
11401161
(6, channel_type, required),
11411162
});
11421163
},
1164+
&Event::ChannelPending { ref channel_id, ref user_channel_id, ref counterparty_node_id, ref funding_txo } => {
1165+
31u8.write(writer)?;
1166+
write_tlv_fields!(writer, {
1167+
(0, channel_id, required),
1168+
(2, user_channel_id, required),
1169+
(4, counterparty_node_id, required),
1170+
(6, funding_txo, required),
1171+
});
1172+
},
11431173
// Note that, going forward, all new events must only write data inside of
11441174
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
11451175
// data via `write_tlv_fields`.
@@ -1478,6 +1508,28 @@ impl MaybeReadable for Event {
14781508
};
14791509
f()
14801510
},
1511+
31u8 => {
1512+
let f = || {
1513+
let mut channel_id = [0; 32];
1514+
let mut user_channel_id: u128 = 0;
1515+
let mut counterparty_node_id = RequiredWrapper(None);
1516+
let mut funding_txo = RequiredWrapper(None);
1517+
read_tlv_fields!(reader, {
1518+
(0, channel_id, required),
1519+
(2, user_channel_id, required),
1520+
(4, counterparty_node_id, required),
1521+
(6, funding_txo, required),
1522+
});
1523+
1524+
Ok(Some(Event::ChannelReady {
1525+
channel_id,
1526+
user_channel_id,
1527+
counterparty_node_id: counterparty_node_id.0.unwrap(),
1528+
channel_type: funding_txo.0.unwrap()
1529+
}))
1530+
};
1531+
f()
1532+
},
14811533
// Versions prior to 0.0.100 did not ignore odd types, instead returning InvalidValue.
14821534
// Version 0.0.100 failed to properly ignore odd types, possibly resulting in corrupt
14831535
// reads.

0 commit comments

Comments
 (0)