Skip to content

Commit d0db6d3

Browse files
event: store the outpoint when is_manual_broadcast
With [1], it's possible to specify `manual_broadcast` for the channel funding transaction. When `is_manual_broadcast` is set to true, the transaction in the `DiscardFunding` event is replaced with a dummy empty transaction. This commit checks if `is_manual_broadcast` is true and stores the funding OutPoint in the DiscardFunding event instead. [1] #3024 Link: #3164 Suggested-by: TheBlueMatt Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent b2cdc2c commit d0db6d3

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

lightning/src/events/mod.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReada
3636
use crate::util::string::UntrustedString;
3737

3838
use bitcoin::{Transaction, OutPoint};
39-
use bitcoin::locktime::absolute::LockTime;
4039
use bitcoin::script::ScriptBuf;
4140
use bitcoin::hashes::Hash;
4241
use bitcoin::hashes::sha256::Hash as Sha256;
4342
use bitcoin::secp256k1::PublicKey;
44-
use bitcoin::transaction::Version;
4543
use crate::io;
4644
use core::time::Duration;
4745
use core::ops::Deref;
@@ -50,6 +48,35 @@ use crate::sync::Arc;
5048
#[allow(unused_imports)]
5149
use crate::prelude::*;
5250

51+
/// `FundingInfo` holds information about a channel's funding transaction.
52+
///
53+
/// When ldk is set to manual propagation of the tx, the funding transaction info
54+
/// inside the channel struct has a dummy value (See [`ChannelManager::unsafe_manual_funding_transaction_generated`]).
55+
/// In such cases, `FundingInfo` contains the `OutPoint` of the channel.
56+
#[derive(Debug, PartialEq, Eq, Clone)]
57+
pub enum FundingInfo {
58+
/// The full funding `Transaction`.
59+
Tx {
60+
/// The funding transaction
61+
transaction: Transaction
62+
},
63+
/// The `OutPoint` of the funding.
64+
OutPoint {
65+
/// The outpoint of the funding
66+
outpoint: transaction::OutPoint
67+
},
68+
}
69+
70+
impl_writeable_tlv_based_enum!(FundingInfo,
71+
(0, Tx) => {
72+
(0, transaction, required)
73+
},
74+
(1, OutPoint) => {
75+
(1, outpoint, required)
76+
}
77+
);
78+
79+
5380
/// Some information provided on receipt of payment depends on whether the payment received is a
5481
/// spontaneous payment or a "conventional" lightning payment that's paying an invoice.
5582
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -1257,7 +1284,7 @@ pub enum Event {
12571284
/// The channel_id of the channel which has been closed.
12581285
channel_id: ChannelId,
12591286
/// The full transaction received from the user
1260-
transaction: Transaction
1287+
funding_info: FundingInfo,
12611288
},
12621289
/// Indicates a request to open a new channel by a peer.
12631290
///
@@ -1541,11 +1568,18 @@ impl Writeable for Event {
15411568
(9, channel_funding_txo, option),
15421569
});
15431570
},
1544-
&Event::DiscardFunding { ref channel_id, ref transaction } => {
1571+
&Event::DiscardFunding { ref channel_id, ref funding_info } => {
15451572
11u8.write(writer)?;
1573+
1574+
let transaction = if let FundingInfo::Tx { transaction } = funding_info {
1575+
Some(transaction)
1576+
} else {
1577+
None
1578+
};
15461579
write_tlv_fields!(writer, {
15471580
(0, channel_id, required),
1548-
(2, transaction, required)
1581+
(2, transaction, option),
1582+
(4, funding_info, required),
15491583
})
15501584
},
15511585
&Event::PaymentPathSuccessful { ref payment_id, ref payment_hash, ref path } => {
@@ -1924,12 +1958,20 @@ impl MaybeReadable for Event {
19241958
11u8 => {
19251959
let mut f = || {
19261960
let mut channel_id = ChannelId::new_zero();
1927-
let mut transaction = Transaction{ version: Version::TWO, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
1961+
let mut transaction: Option<Transaction> = None;
1962+
let mut funding_info: Option<FundingInfo> = None;
19281963
read_tlv_fields!(reader, {
19291964
(0, channel_id, required),
1930-
(2, transaction, required),
1965+
(2, transaction, option),
1966+
(4, funding_info, option),
19311967
});
1932-
Ok(Some(Event::DiscardFunding { channel_id, transaction } ))
1968+
1969+
let funding_info = if let Some(tx) = transaction {
1970+
FundingInfo::Tx { transaction: tx }
1971+
} else {
1972+
funding_info.ok_or(msgs::DecodeError::InvalidValue)?
1973+
};
1974+
Ok(Some(Event::DiscardFunding { channel_id, funding_info } ))
19331975
};
19341976
f()
19351977
},

lightning/src/ln/channelmanager.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use bitcoin::secp256k1::{SecretKey,PublicKey};
3232
use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::{secp256k1, Sequence};
3434

35+
use crate::events::FundingInfo;
3536
use crate::blinded_path::message::{MessageContext, OffersContext};
3637
use crate::blinded_path::NodeIdLookUp;
3738
use crate::blinded_path::message::{BlindedMessagePath, ForwardNode};
@@ -3509,6 +3510,7 @@ where
35093510
let _ = self.chain_monitor.update_channel(funding_txo, &monitor_update);
35103511
}
35113512
let mut shutdown_results = Vec::new();
3513+
let mut is_manual_broadcast = false;
35123514
if let Some(txid) = shutdown_res.unbroadcasted_batch_funding_txid {
35133515
let mut funding_batch_states = self.funding_batch_states.lock().unwrap();
35143516
let affected_channels = funding_batch_states.remove(&txid).into_iter().flatten();
@@ -3518,6 +3520,10 @@ where
35183520
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
35193521
let mut peer_state = peer_state_mutex.lock().unwrap();
35203522
if let Some(mut chan) = peer_state.channel_by_id.remove(&channel_id) {
3523+
// We override the previous value, so we could change from true -> false,
3524+
// but this is fine because if a channel has manual_broadcast set to false
3525+
// we should choose the safier condition.
3526+
is_manual_broadcast = chan.context().is_manual_broadcast();
35213527
update_maps_on_chan_removal!(self, &chan.context());
35223528
shutdown_results.push(chan.context_mut().force_shutdown(false, ClosureReason::FundingBatchClosure));
35233529
}
@@ -3541,9 +3547,14 @@ where
35413547
channel_funding_txo: shutdown_res.channel_funding_txo,
35423548
}, None));
35433549

3544-
if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
3550+
let funding_info = if is_manual_broadcast {
3551+
shutdown_res.channel_funding_txo.map(|outpoint| FundingInfo::OutPoint{ outpoint })
3552+
} else {
3553+
shutdown_res.unbroadcasted_funding_tx.map(|transaction| FundingInfo::Tx{ transaction })
3554+
};
3555+
if let Some(funding_info) = funding_info {
35453556
pending_events.push_back((events::Event::DiscardFunding {
3546-
channel_id: shutdown_res.channel_id, transaction
3557+
channel_id: shutdown_res.channel_id, funding_info
35473558
}, None));
35483559
}
35493560
}

0 commit comments

Comments
 (0)