Skip to content

Commit 8a500f2

Browse files
committed
Handle splice_locked message
When receiving a splice_locked message from a counterparty, promote the corresponding FundingScope if the funding txid matches the one sent by us in splice_locked.
1 parent a92a416 commit 8a500f2

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9845,6 +9845,76 @@ where
98459845
Ok(())
98469846
}
98479847

9848+
#[cfg(splicing)]
9849+
pub fn splice_locked<NS: Deref, L: Deref>(
9850+
&mut self, msg: &msgs::SpliceLocked, node_signer: &NS, chain_hash: ChainHash,
9851+
user_config: &UserConfig, best_block: &BestBlock, logger: &L,
9852+
) -> Result<Option<msgs::AnnouncementSignatures>, ChannelError>
9853+
where
9854+
NS::Target: NodeSigner,
9855+
L::Target: Logger,
9856+
{
9857+
log_info!(
9858+
logger,
9859+
"Received splice_locked txid {} from our peer for channel {}",
9860+
msg.splice_txid,
9861+
&self.context.channel_id,
9862+
);
9863+
9864+
let pending_splice = match self.pending_splice.as_mut() {
9865+
Some(pending_splice) => pending_splice,
9866+
None => {
9867+
return Err(ChannelError::Ignore(format!("Channel is not in pending splice")));
9868+
},
9869+
};
9870+
9871+
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
9872+
if sent_funding_txid == msg.splice_txid {
9873+
if let Some(funding) = self
9874+
.pending_funding
9875+
.iter_mut()
9876+
.find(|funding| funding.get_funding_txid() == Some(sent_funding_txid))
9877+
{
9878+
log_info!(
9879+
logger,
9880+
"Promoting splice funding txid {} for channel {}",
9881+
msg.splice_txid,
9882+
&self.context.channel_id,
9883+
);
9884+
promote_splice_funding!(self, funding);
9885+
return Ok(self.get_announcement_sigs(
9886+
node_signer,
9887+
chain_hash,
9888+
user_config,
9889+
best_block.height,
9890+
logger,
9891+
));
9892+
}
9893+
9894+
let err = "unknown splice funding txid";
9895+
return Err(ChannelError::close(err.to_string()));
9896+
} else {
9897+
log_warn!(
9898+
logger,
9899+
"Mismatched splice_locked txid for channel {}; sent txid {}; received txid {}",
9900+
&self.context.channel_id,
9901+
sent_funding_txid,
9902+
msg.splice_txid,
9903+
);
9904+
}
9905+
} else {
9906+
log_info!(
9907+
logger,
9908+
"Waiting for enough confirmations to send splice_locked txid {} for channel {}",
9909+
msg.splice_txid,
9910+
&self.context.channel_id,
9911+
);
9912+
}
9913+
9914+
pending_splice.received_funding_txid = Some(msg.splice_txid);
9915+
Ok(None)
9916+
}
9917+
98489918
// Send stuff to our remote peers:
98499919

98509920
/// Queues up an outbound HTLC to send by placing it in the holding cell. You should call

lightning/src/ln/channelmanager.rs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10121,6 +10121,61 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1012110121
Err(MsgHandleErrInternal::send_err_msg_no_close("TODO(splicing): Splicing is not implemented (splice_ack)".to_owned(), msg.channel_id))
1012210122
}
1012310123

10124+
#[cfg(splicing)]
10125+
fn internal_splice_locked(
10126+
&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceLocked,
10127+
) -> Result<(), MsgHandleErrInternal> {
10128+
let per_peer_state = self.per_peer_state.read().unwrap();
10129+
let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
10130+
debug_assert!(false);
10131+
MsgHandleErrInternal::send_err_msg_no_close(
10132+
format!(
10133+
"Can't find a peer matching the passed counterparty node_id {}",
10134+
counterparty_node_id
10135+
),
10136+
msg.channel_id,
10137+
)
10138+
})?;
10139+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10140+
let peer_state = &mut *peer_state_lock;
10141+
10142+
// Look for the channel
10143+
match peer_state.channel_by_id.entry(msg.channel_id) {
10144+
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
10145+
"Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
10146+
counterparty_node_id
10147+
), msg.channel_id)),
10148+
hash_map::Entry::Occupied(mut chan_entry) => {
10149+
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
10150+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
10151+
let announcement_sigs_opt = try_channel_entry!(
10152+
self, peer_state, chan.splice_locked(
10153+
msg, &self.node_signer, self.chain_hash, &self.default_configuration,
10154+
&self.best_block.read().unwrap(), &&logger,
10155+
), chan_entry
10156+
);
10157+
10158+
if !chan.has_pending_splice() {
10159+
let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
10160+
insert_short_channel_id!(short_to_chan_info, chan);
10161+
}
10162+
10163+
if let Some(announcement_sigs) = announcement_sigs_opt {
10164+
log_trace!(logger, "Sending announcement_signatures for channel {}", chan.context.channel_id());
10165+
peer_state.pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
10166+
node_id: counterparty_node_id.clone(),
10167+
msg: announcement_sigs,
10168+
});
10169+
}
10170+
} else {
10171+
return Err(MsgHandleErrInternal::send_err_msg_no_close("Channel is not funded, cannot splice".to_owned(), msg.channel_id));
10172+
}
10173+
},
10174+
};
10175+
10176+
Ok(())
10177+
}
10178+
1012410179
/// Process pending events from the [`chain::Watch`], returning whether any events were processed.
1012510180
#[rustfmt::skip]
1012610181
fn process_pending_monitor_events(&self) -> bool {
@@ -12612,9 +12667,16 @@ where
1261212667
#[cfg(splicing)]
1261312668
#[rustfmt::skip]
1261412669
fn handle_splice_locked(&self, counterparty_node_id: PublicKey, msg: &msgs::SpliceLocked) {
12615-
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
12616-
"Splicing not supported (splice_locked)".to_owned(),
12617-
msg.channel_id)), counterparty_node_id);
12670+
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
12671+
let res = self.internal_splice_locked(&counterparty_node_id, msg);
12672+
let persist = match &res {
12673+
Err(e) if e.closes_channel() => NotifyOption::DoPersist,
12674+
Err(_) => NotifyOption::SkipPersistHandleEvents,
12675+
Ok(()) => NotifyOption::DoPersist,
12676+
};
12677+
let _ = handle_error!(self, res, counterparty_node_id);
12678+
persist
12679+
});
1261812680
}
1261912681

1262012682
fn handle_shutdown(&self, counterparty_node_id: PublicKey, msg: &msgs::Shutdown) {

0 commit comments

Comments
 (0)