From 14b761283bfa454f2d08439e3442c0e370f07cde Mon Sep 17 00:00:00 2001 From: jbesraa Date: Mon, 21 Aug 2023 22:45:02 +0300 Subject: [PATCH] Add channel_id to SpendableOutputs event This will make it possible to link between SpendableOuts and ChannelMonitor - change channel_id to option so we dont break upgrade - remove unused channel_id - document channel_id - extract channel id dynamically to pass test - use contains to check channel_id in test as the events are not ordered - update docs framing - specify ldk version channel_id will be introduced in Co-authored-by: Elias Rohrer Update lightning/src/events/mod.rs Co-authored-by: Elias Rohrer --- lightning/src/chain/channelmonitor.rs | 3 ++- lightning/src/events/mod.rs | 11 +++++++++-- lightning/src/ln/functional_tests.rs | 2 +- lightning/src/ln/monitor_tests.rs | 5 +++-- lightning/src/ln/reorg_tests.rs | 6 ++++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index 5e3d49c0f..bde5c12ab 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -3365,7 +3365,8 @@ impl ChannelMonitorImpl { OnchainEvent::MaturingOutput { descriptor } => { log_debug!(logger, "Descriptor {} has got enough confirmations to be passed upstream", log_spendable!(descriptor)); self.pending_events.push(Event::SpendableOutputs { - outputs: vec![descriptor] + outputs: vec![descriptor], + channel_id: Some(self.funding_info.0.to_channel_id()), }); self.spendable_txids_confirmed.push(entry.txid); }, diff --git a/lightning/src/events/mod.rs b/lightning/src/events/mod.rs index 418f8d545..da687ad1b 100644 --- a/lightning/src/events/mod.rs +++ b/lightning/src/events/mod.rs @@ -673,6 +673,10 @@ pub enum Event { SpendableOutputs { /// The outputs which you should store as spendable by you. outputs: Vec, + /// The `channel_id` indicating which channel the spendable outputs belong to. + /// + /// This will always be `Some` for events generated by LDK versions 0.0.117 and above. + channel_id: Option<[u8; 32]>, }, /// This event is generated when a payment has been successfully forwarded through us and a /// forwarding fee earned. @@ -958,10 +962,11 @@ impl Writeable for Event { // Note that we now ignore these on the read end as we'll re-generate them in // ChannelManager, we write them here only for backwards compatibility. }, - &Event::SpendableOutputs { ref outputs } => { + &Event::SpendableOutputs { ref outputs, channel_id } => { 5u8.write(writer)?; write_tlv_fields!(writer, { (0, WithoutLength(outputs), required), + (1, channel_id, option), }); }, &Event::HTLCIntercepted { requested_next_hop_scid, payment_hash, inbound_amount_msat, expected_outbound_amount_msat, intercept_id } => { @@ -1230,10 +1235,12 @@ impl MaybeReadable for Event { 5u8 => { let f = || { let mut outputs = WithoutLength(Vec::new()); + let mut channel_id: Option<[u8; 32]> = None; read_tlv_fields!(reader, { (0, outputs, required), + (1, channel_id, option), }); - Ok(Some(Event::SpendableOutputs { outputs: outputs.0 })) + Ok(Some(Event::SpendableOutputs { outputs: outputs.0, channel_id })) }; f() }, diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index d794d122e..9a4dd3fdf 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -4302,7 +4302,7 @@ macro_rules! check_spendable_outputs { let secp_ctx = Secp256k1::new(); for event in events.drain(..) { match event { - Event::SpendableOutputs { mut outputs } => { + Event::SpendableOutputs { mut outputs, channel_id: _ } => { for outp in outputs.drain(..) { txn.push($keysinterface.backing.spend_spendable_outputs(&[&outp], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &secp_ctx).unwrap()); all_outputs.push(outp); diff --git a/lightning/src/ln/monitor_tests.rs b/lightning/src/ln/monitor_tests.rs index 85a1448ec..5ba65ceeb 100644 --- a/lightning/src/ln/monitor_tests.rs +++ b/lightning/src/ln/monitor_tests.rs @@ -95,7 +95,7 @@ fn chanmon_fail_from_stale_commitment() { fn test_spendable_output<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, spendable_tx: &Transaction) { let mut spendable = node.chain_monitor.chain_monitor.get_and_clear_pending_events(); assert_eq!(spendable.len(), 1); - if let Event::SpendableOutputs { outputs } = spendable.pop().unwrap() { + if let Event::SpendableOutputs { outputs, .. } = spendable.pop().unwrap() { assert_eq!(outputs.len(), 1); let spend_tx = node.keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &Secp256k1::new()).unwrap(); @@ -2228,8 +2228,9 @@ fn test_anchors_aggregated_revoked_htlc_tx() { let spendable_output_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events(); assert_eq!(spendable_output_events.len(), 2); for (idx, event) in spendable_output_events.iter().enumerate() { - if let Event::SpendableOutputs { outputs } = event { + if let Event::SpendableOutputs { outputs, channel_id } = event { assert_eq!(outputs.len(), 1); + assert!(vec![chan_b.2, chan_a.2].contains(&channel_id.unwrap())); let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs( &[&outputs[0]], Vec::new(), Script::new_op_return(&[]), 253, None, &Secp256k1::new(), ).unwrap(); diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs index c0720d531..94479f79a 100644 --- a/lightning/src/ln/reorg_tests.rs +++ b/lightning/src/ln/reorg_tests.rs @@ -578,8 +578,9 @@ fn do_test_to_remote_after_local_detection(style: ConnectStyle) { let mut node_a_spendable = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events(); assert_eq!(node_a_spendable.len(), 1); - if let Event::SpendableOutputs { outputs } = node_a_spendable.pop().unwrap() { + if let Event::SpendableOutputs { outputs, channel_id } = node_a_spendable.pop().unwrap() { assert_eq!(outputs.len(), 1); + assert_eq!(channel_id, Some(chan_id)); let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &Secp256k1::new()).unwrap(); check_spends!(spend_tx, remote_txn_b[0]); @@ -598,8 +599,9 @@ fn do_test_to_remote_after_local_detection(style: ConnectStyle) { let mut node_b_spendable = nodes[1].chain_monitor.chain_monitor.get_and_clear_pending_events(); assert_eq!(node_b_spendable.len(), 1); - if let Event::SpendableOutputs { outputs } = node_b_spendable.pop().unwrap() { + if let Event::SpendableOutputs { outputs, channel_id } = node_b_spendable.pop().unwrap() { assert_eq!(outputs.len(), 1); + assert_eq!(channel_id, Some(chan_id)); let spend_tx = nodes[1].keys_manager.backing.spend_spendable_outputs(&[&outputs[0]], Vec::new(), Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script(), 253, None, &Secp256k1::new()).unwrap(); check_spends!(spend_tx, remote_txn_a[0]); -- 2.39.5