Skip to content

Commit 1802b6e

Browse files
committed
Send 0conf splice_locked upon tx_signatures exchange
Splices negotiated with 0 confirmations require that we immediately lock it after exchanging `tx_signatures`.
1 parent 1726047 commit 1802b6e

File tree

3 files changed

+209
-79
lines changed

3 files changed

+209
-79
lines changed

lightning/src/ln/channel.rs

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6846,6 +6846,9 @@ pub struct FundingTxSigned {
68466846

68476847
/// Information about the completed funding negotiation.
68486848
pub splice_negotiated: Option<SpliceFundingNegotiated>,
6849+
6850+
/// A `splice_locked` to send to the counterparty when the splice requires 0 confirmations.
6851+
pub splice_locked: Option<msgs::SpliceLocked>,
68496852
}
68506853

68516854
/// Information about a splice funding negotiation that has been completed.
@@ -8877,9 +8880,13 @@ where
88778880
}
88788881
}
88798882

8880-
fn on_tx_signatures_exchange(
8881-
&mut self, funding_tx: Transaction,
8882-
) -> Option<SpliceFundingNegotiated> {
8883+
fn on_tx_signatures_exchange<'a, L: Deref>(
8884+
&mut self, funding_tx: Transaction, best_block_height: u32,
8885+
logger: &WithChannelContext<'a, L>,
8886+
) -> (Option<SpliceFundingNegotiated>, Option<msgs::SpliceLocked>)
8887+
where
8888+
L::Target: Logger,
8889+
{
88838890
debug_assert!(!self.context.channel_state.is_monitor_update_in_progress());
88848891
debug_assert!(!self.context.channel_state.is_awaiting_remote_revoke());
88858892

@@ -8901,22 +8908,42 @@ where
89018908
channel_type,
89028909
};
89038910

8904-
Some(splice_negotiated)
8911+
let splice_locked = pending_splice.check_get_splice_locked(
8912+
&self.context,
8913+
pending_splice.negotiated_candidates.len() - 1,
8914+
best_block_height,
8915+
);
8916+
if let Some(splice_txid) =
8917+
splice_locked.as_ref().map(|splice_locked| splice_locked.splice_txid)
8918+
{
8919+
log_info!(
8920+
logger,
8921+
"Sending 0conf splice_locked txid {} to our peer for channel {}",
8922+
splice_txid,
8923+
&self.context.channel_id
8924+
);
8925+
}
8926+
8927+
(Some(splice_negotiated), splice_locked)
89058928
} else {
89068929
debug_assert!(false);
8907-
None
8930+
(None, None)
89088931
}
89098932
} else {
89108933
self.funding.funding_transaction = Some(funding_tx);
89118934
self.context.channel_state =
89128935
ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
8913-
None
8936+
(None, None)
89148937
}
89158938
}
89168939

8917-
pub fn funding_transaction_signed(
8918-
&mut self, funding_txid_signed: Txid, witnesses: Vec<Witness>,
8919-
) -> Result<FundingTxSigned, APIError> {
8940+
pub fn funding_transaction_signed<L: Deref>(
8941+
&mut self, funding_txid_signed: Txid, witnesses: Vec<Witness>, best_block_height: u32,
8942+
logger: &L,
8943+
) -> Result<FundingTxSigned, APIError>
8944+
where
8945+
L::Target: Logger,
8946+
{
89208947
let signing_session =
89218948
if let Some(signing_session) = self.context.interactive_tx_signing_session.as_mut() {
89228949
if let Some(pending_splice) = self.pending_splice.as_ref() {
@@ -8937,6 +8964,7 @@ where
89378964
tx_signatures: None,
89388965
funding_tx: None,
89398966
splice_negotiated: None,
8967+
splice_locked: None,
89408968
});
89418969
}
89428970

@@ -8949,6 +8977,7 @@ where
89498977
tx_signatures: None,
89508978
funding_tx: None,
89518979
splice_negotiated: None,
8980+
splice_locked: None,
89528981
});
89538982
}
89548983
let err =
@@ -8991,19 +9020,30 @@ where
89919020
.provide_holder_witnesses(tx_signatures, &self.context.secp_ctx)
89929021
.map_err(|err| APIError::APIMisuseError { err })?;
89939022

8994-
let splice_negotiated = if let Some(funding_tx) = funding_tx.clone() {
9023+
let logger = WithChannelContext::from(logger, &self.context, None);
9024+
if tx_signatures.is_some() {
9025+
log_info!(
9026+
logger,
9027+
"Sending tx_signatures for interactive funding transaction {funding_txid_signed}"
9028+
);
9029+
}
9030+
9031+
let (splice_negotiated, splice_locked) = if let Some(funding_tx) = funding_tx.clone() {
89959032
debug_assert!(tx_signatures.is_some());
8996-
self.on_tx_signatures_exchange(funding_tx)
9033+
self.on_tx_signatures_exchange(funding_tx, best_block_height, &logger)
89979034
} else {
8998-
None
9035+
(None, None)
89999036
};
90009037

9001-
Ok(FundingTxSigned { tx_signatures, funding_tx, splice_negotiated })
9038+
Ok(FundingTxSigned { tx_signatures, funding_tx, splice_negotiated, splice_locked })
90029039
}
90039040

9004-
pub fn tx_signatures(
9005-
&mut self, msg: &msgs::TxSignatures,
9006-
) -> Result<FundingTxSigned, ChannelError> {
9041+
pub fn tx_signatures<L: Deref>(
9042+
&mut self, msg: &msgs::TxSignatures, best_block_height: u32, logger: &L,
9043+
) -> Result<FundingTxSigned, ChannelError>
9044+
where
9045+
L::Target: Logger,
9046+
{
90079047
let signing_session = if let Some(signing_session) =
90089048
self.context.interactive_tx_signing_session.as_mut()
90099049
{
@@ -9049,13 +9089,25 @@ where
90499089
let (holder_tx_signatures, funding_tx) =
90509090
signing_session.received_tx_signatures(msg).map_err(|msg| ChannelError::Warn(msg))?;
90519091

9052-
let splice_negotiated = if let Some(funding_tx) = funding_tx.clone() {
9053-
self.on_tx_signatures_exchange(funding_tx)
9092+
let logger = WithChannelContext::from(logger, &self.context, None);
9093+
log_info!(
9094+
logger,
9095+
"Received tx_signatures for interactive funding transaction {}",
9096+
msg.tx_hash
9097+
);
9098+
9099+
let (splice_negotiated, splice_locked) = if let Some(funding_tx) = funding_tx.clone() {
9100+
self.on_tx_signatures_exchange(funding_tx, best_block_height, &logger)
90549101
} else {
9055-
None
9102+
(None, None)
90569103
};
90579104

9058-
Ok(FundingTxSigned { tx_signatures: holder_tx_signatures, funding_tx, splice_negotiated })
9105+
Ok(FundingTxSigned {
9106+
tx_signatures: holder_tx_signatures,
9107+
funding_tx,
9108+
splice_negotiated,
9109+
splice_locked,
9110+
})
90599111
}
90609112

90619113
/// Queues up an outbound update fee by placing it in the holding cell. You should call
@@ -11362,7 +11414,11 @@ where
1136211414
confirmed_funding_index,
1136311415
height,
1136411416
) {
11365-
log_info!(logger, "Sending a splice_locked to our peer for channel {}", &self.context.channel_id);
11417+
log_info!(
11418+
logger, "Sending splice_locked txid {} to our peer for channel {}",
11419+
splice_locked.splice_txid,
11420+
&self.context.channel_id
11421+
);
1136611422

1136711423
let (funding_txo, monitor_update, announcement_sigs, discarded_funding) = chain_node_signer
1136811424
.and_then(|(chain_hash, node_signer, user_config)| {

lightning/src/ln/channelmanager.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6431,11 +6431,18 @@ where
64316431
.map(|input| input.witness)
64326432
.filter(|witness| !witness.is_empty())
64336433
.collect();
6434-
match chan.funding_transaction_signed(txid, witnesses) {
6434+
let best_block_height = self.best_block.read().unwrap().height;
6435+
match chan.funding_transaction_signed(
6436+
txid,
6437+
witnesses,
6438+
best_block_height,
6439+
&self.logger,
6440+
) {
64356441
Ok(FundingTxSigned {
64366442
tx_signatures: Some(tx_signatures),
64376443
funding_tx,
64386444
splice_negotiated,
6445+
splice_locked,
64396446
}) => {
64406447
if let Some(funding_tx) = funding_tx {
64416448
self.broadcast_interactive_funding(
@@ -6462,6 +6469,14 @@ where
64626469
msg: tx_signatures,
64636470
},
64646471
);
6472+
if let Some(splice_locked) = splice_locked {
6473+
peer_state.pending_msg_events.push(
6474+
MessageSendEvent::SendSpliceLocked {
6475+
node_id: *counterparty_node_id,
6476+
msg: splice_locked,
6477+
},
6478+
);
6479+
}
64656480
return NotifyOption::DoPersist;
64666481
},
64676482
Err(err) => {
@@ -6472,9 +6487,11 @@ where
64726487
tx_signatures: None,
64736488
funding_tx,
64746489
splice_negotiated,
6490+
splice_locked,
64756491
}) => {
64766492
debug_assert!(funding_tx.is_none());
64776493
debug_assert!(splice_negotiated.is_none());
6494+
debug_assert!(splice_locked.is_none());
64786495
return NotifyOption::SkipPersistNoEvents;
64796496
},
64806497
}
@@ -9578,8 +9595,14 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
95789595
}
95799596
} else {
95809597
let txid = signing_session.unsigned_tx().compute_txid();
9581-
match channel.funding_transaction_signed(txid, vec![]) {
9582-
Ok(FundingTxSigned { tx_signatures: Some(tx_signatures), funding_tx, splice_negotiated }) => {
9598+
let best_block_height = self.best_block.read().unwrap().height;
9599+
match channel.funding_transaction_signed(txid, vec![], best_block_height, &self.logger) {
9600+
Ok(FundingTxSigned {
9601+
tx_signatures: Some(tx_signatures),
9602+
funding_tx,
9603+
splice_negotiated,
9604+
splice_locked,
9605+
}) => {
95839606
if let Some(funding_tx) = funding_tx {
95849607
self.broadcast_interactive_funding(channel, &funding_tx, &self.logger);
95859608
}
@@ -9602,6 +9625,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96029625
node_id: counterparty_node_id,
96039626
msg: tx_signatures,
96049627
});
9628+
if let Some(splice_locked) = splice_locked {
9629+
pending_msg_events.push(MessageSendEvent::SendSpliceLocked {
9630+
node_id: counterparty_node_id,
9631+
msg: splice_locked,
9632+
});
9633+
}
96059634
}
96069635
},
96079636
Ok(FundingTxSigned { tx_signatures: None, .. }) => {
@@ -10580,14 +10609,30 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1058010609
hash_map::Entry::Occupied(mut chan_entry) => {
1058110610
match chan_entry.get_mut().as_funded_mut() {
1058210611
Some(chan) => {
10583-
let FundingTxSigned { tx_signatures, funding_tx, splice_negotiated } =
10584-
try_channel_entry!(self, peer_state, chan.tx_signatures(msg), chan_entry);
10612+
let best_block_height = self.best_block.read().unwrap().height;
10613+
let FundingTxSigned {
10614+
tx_signatures,
10615+
funding_tx,
10616+
splice_negotiated,
10617+
splice_locked,
10618+
} = try_channel_entry!(
10619+
self,
10620+
peer_state,
10621+
chan.tx_signatures(msg, best_block_height, &self.logger),
10622+
chan_entry
10623+
);
1058510624
if let Some(tx_signatures) = tx_signatures {
1058610625
peer_state.pending_msg_events.push(MessageSendEvent::SendTxSignatures {
1058710626
node_id: *counterparty_node_id,
1058810627
msg: tx_signatures,
1058910628
});
1059010629
}
10630+
if let Some(splice_locked) = splice_locked {
10631+
peer_state.pending_msg_events.push(MessageSendEvent::SendSpliceLocked {
10632+
node_id: *counterparty_node_id,
10633+
msg: splice_locked,
10634+
});
10635+
}
1059110636
if let Some(ref funding_tx) = funding_tx {
1059210637
self.broadcast_interactive_funding(chan, funding_tx, &self.logger);
1059310638
}

0 commit comments

Comments
 (0)