Skip to content

Commit a0e1d78

Browse files
committed
Handle splice_locked message
1 parent bd5f608 commit a0e1d78

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9502,6 +9502,69 @@ impl<SP: Deref> FundedChannel<SP> where
95029502
Ok(())
95039503
}
95049504

9505+
#[cfg(splicing)]
9506+
pub fn splice_locked<NS: Deref, L: Deref>(
9507+
&mut self, msg: &msgs::SpliceLocked, node_signer: &NS, chain_hash: ChainHash,
9508+
user_config: &UserConfig, best_block: &BestBlock, logger: &L,
9509+
) -> Result<Option<msgs::AnnouncementSignatures>, ChannelError>
9510+
where
9511+
NS::Target: NodeSigner,
9512+
L::Target: Logger
9513+
{
9514+
log_info!(
9515+
logger,
9516+
"Received splice_locked txid {} from our peer for channel {}",
9517+
msg.splice_txid,
9518+
&self.context.channel_id,
9519+
);
9520+
9521+
let pending_splice = match self.pending_splice.as_mut() {
9522+
Some(pending_splice) => pending_splice,
9523+
None => {
9524+
return Err(ChannelError::Ignore(format!("Channel is not in pending splice")));
9525+
},
9526+
};
9527+
9528+
if let Some(sent_funding_txid) = pending_splice.sent_funding_txid {
9529+
if sent_funding_txid == msg.splice_txid {
9530+
if let Some(funding) = self.pending_funding
9531+
.iter_mut()
9532+
.find(|funding| funding.get_funding_txid() == Some(sent_funding_txid))
9533+
{
9534+
log_info!(
9535+
logger,
9536+
"Promoting splice funding txid {} for channel {}",
9537+
msg.splice_txid,
9538+
&self.context.channel_id,
9539+
);
9540+
promote_splice_funding!(self, funding);
9541+
return Ok(self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block.height, logger));
9542+
}
9543+
9544+
let err = "unknown splice funding txid";
9545+
return Err(ChannelError::close(err.to_string()));
9546+
} else {
9547+
log_warn!(
9548+
logger,
9549+
"Mismatched splice_locked txid for channel {}; sent txid {}; received txid {}",
9550+
&self.context.channel_id,
9551+
sent_funding_txid,
9552+
msg.splice_txid,
9553+
);
9554+
}
9555+
} else {
9556+
log_info!(
9557+
logger,
9558+
"Waiting for enough confirmations to send splice_locked txid {} for channel {}",
9559+
msg.splice_txid,
9560+
&self.context.channel_id,
9561+
);
9562+
}
9563+
9564+
pending_splice.received_funding_txid = Some(msg.splice_txid);
9565+
Ok(None)
9566+
}
9567+
95059568
// Send stuff to our remote peers:
95069569

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

lightning/src/ln/channelmanager.rs

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

10005+
#[cfg(splicing)]
10006+
fn internal_splice_locked(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceLocked) -> Result<(), MsgHandleErrInternal> {
10007+
let per_peer_state = self.per_peer_state.read().unwrap();
10008+
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
10009+
.ok_or_else(|| {
10010+
debug_assert!(false);
10011+
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.channel_id)
10012+
})?;
10013+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
10014+
let peer_state = &mut *peer_state_lock;
10015+
10016+
// Look for the channel
10017+
match peer_state.channel_by_id.entry(msg.channel_id) {
10018+
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!(
10019+
"Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
10020+
counterparty_node_id
10021+
), msg.channel_id)),
10022+
hash_map::Entry::Occupied(mut chan_entry) => {
10023+
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
10024+
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
10025+
let announcement_sigs_opt = try_channel_entry!(
10026+
self, peer_state, chan.splice_locked(
10027+
msg, &self.node_signer, self.chain_hash, &self.default_configuration,
10028+
&self.best_block.read().unwrap(), &&logger,
10029+
), chan_entry
10030+
);
10031+
10032+
if !chan.has_pending_splice() {
10033+
let mut short_to_chan_info = self.short_to_chan_info.write().unwrap();
10034+
insert_short_channel_id!(short_to_chan_info, chan);
10035+
}
10036+
10037+
if let Some(announcement_sigs) = announcement_sigs_opt {
10038+
log_trace!(logger, "Sending announcement_signatures for channel {}", chan.context.channel_id());
10039+
peer_state.pending_msg_events.push(MessageSendEvent::SendAnnouncementSignatures {
10040+
node_id: counterparty_node_id.clone(),
10041+
msg: announcement_sigs,
10042+
});
10043+
}
10044+
} else {
10045+
return Err(MsgHandleErrInternal::send_err_msg_no_close("Channel is not funded, cannot splice".to_owned(), msg.channel_id));
10046+
}
10047+
},
10048+
};
10049+
10050+
Ok(())
10051+
}
10052+
1000510053
/// Process pending events from the [`chain::Watch`], returning whether any events were processed.
1000610054
#[rustfmt::skip]
1000710055
fn process_pending_monitor_events(&self) -> bool {
@@ -12455,9 +12503,16 @@ where
1245512503
#[cfg(splicing)]
1245612504
#[rustfmt::skip]
1245712505
fn handle_splice_locked(&self, counterparty_node_id: PublicKey, msg: &msgs::SpliceLocked) {
12458-
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
12459-
"Splicing not supported (splice_locked)".to_owned(),
12460-
msg.channel_id)), counterparty_node_id);
12506+
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
12507+
let res = self.internal_splice_locked(&counterparty_node_id, msg);
12508+
let persist = match &res {
12509+
Err(e) if e.closes_channel() => NotifyOption::DoPersist,
12510+
Err(_) => NotifyOption::SkipPersistHandleEvents,
12511+
Ok(()) => NotifyOption::DoPersist,
12512+
};
12513+
let _ = handle_error!(self, res, counterparty_node_id);
12514+
persist
12515+
});
1246112516
}
1246212517

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

0 commit comments

Comments
 (0)