Add channel_id to SpendableOutputs event
authorjbesraa <jbesraa@gmail.com>
Mon, 21 Aug 2023 19:45:02 +0000 (22:45 +0300)
committerjbesraa <jbesraa@gmail.com>
Tue, 22 Aug 2023 14:07:13 +0000 (17:07 +0300)
    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 <dev@tnull.de>
Update lightning/src/events/mod.rs

Co-authored-by: Elias Rohrer <dev@tnull.de>
lightning/src/chain/channelmonitor.rs
lightning/src/events/mod.rs
lightning/src/ln/functional_tests.rs
lightning/src/ln/monitor_tests.rs
lightning/src/ln/reorg_tests.rs

index 5e3d49c0f9f0c5004cb0bad485ed216cc3a70737..bde5c12abd90891beae05e6d8f91d03b19444707 100644 (file)
@@ -3365,7 +3365,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
                                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);
                                },
index 418f8d5459a18e7fe3711327ba119ab87d2de5ef..da687ad1b9aa5bb11cd385a3faa0d99e973d7aba 100644 (file)
@@ -673,6 +673,10 @@ pub enum Event {
        SpendableOutputs {
                /// The outputs which you should store as spendable by you.
                outputs: Vec<SpendableOutputDescriptor>,
+               /// 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()
                        },
index d794d122e91882beb165ead2bd06cccd6f907d14..9a4dd3fdf8a8996d99e27d5232ff97807f47db19 100644 (file)
@@ -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);
index 85a1448ec38905dd2c19f84a7d02c5ec238bdaad..5ba65ceeb0af9f26acc74d2205d69019ec995a87 100644 (file)
@@ -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();
index c0720d5319a1afc075d4cb83edb9fff5436936d2..94479f79a2f70ac83975667415a6f13e3762d6ca 100644 (file)
@@ -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]);