Skip to content

Commit

Permalink
Add channel_id to SpendableOutputs event
Browse files Browse the repository at this point in the history
    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
  • Loading branch information
jbesraa committed Aug 22, 2023
1 parent d4ad826 commit 87abb91
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lightning/src/chain/channelmonitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
11 changes: 9 additions & 2 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ pub enum Event {
SpendableOutputs {
/// The outputs which you should store as spendable by you.
outputs: Vec<SpendableOutputDescriptor>,
/// Channel id to help you locate the corresponding `ChannelMonitor`,
/// We define this as an option so we dont break backwards compatibility.
/// This will always be `Some` for events generated by LDK versions 0.0.XX 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.
Expand Down Expand Up @@ -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 } => {
Expand Down Expand Up @@ -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()
},
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions lightning/src/ln/monitor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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_eq!(channel_id, &Some(vec![chan_a.2, chan_b.2][idx]));
let spend_tx = nodes[0].keys_manager.backing.spend_spendable_outputs(
&[&outputs[0]], Vec::new(), Script::new_op_return(&[]), 253, None, &Secp256k1::new(),
).unwrap();
Expand Down
6 changes: 4 additions & 2 deletions lightning/src/ln/reorg_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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]);
Expand Down

0 comments on commit 87abb91

Please sign in to comment.