Skip to content

Commit e96eb52

Browse files
committed
Send funding_signed messages out-of-band to ensure ordered delivery
1 parent 0036735 commit e96eb52

File tree

6 files changed

+41
-24
lines changed

6 files changed

+41
-24
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ mod tests {
757757

758758
let log_entries = logger.lines.lock().unwrap();
759759
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendAcceptChannel event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel ff4f00f805273c1b203bb5ebf8436bfde57b3be8c2f5e95d9491dbb181909679".to_string())), Some(&1)); // 1
760-
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 35 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 2
760+
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingSigned event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 2
761761
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 3
762762
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling SendFundingLocked event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 4
763763
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Encoding and sending message of type 133 to 030000000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&4)); // 5

src/ln/channelmanager.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ impl ChannelManager {
16931693
Ok(())
16941694
}
16951695

1696-
fn internal_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, MsgHandleErrInternal> {
1696+
fn internal_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
16971697
let (chan, funding_msg, monitor_update) = {
16981698
let mut channel_state = self.channel_state.lock().unwrap();
16991699
match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
@@ -1719,16 +1719,21 @@ impl ChannelManager {
17191719
if let Err(_e) = self.monitor.add_update_monitor(monitor_update.get_funding_txo().unwrap(), monitor_update) {
17201720
unimplemented!();
17211721
}
1722-
let mut channel_state = self.channel_state.lock().unwrap();
1722+
let mut channel_state_lock = self.channel_state.lock().unwrap();
1723+
let channel_state = channel_state_lock.borrow_parts();
17231724
match channel_state.by_id.entry(funding_msg.channel_id) {
17241725
hash_map::Entry::Occupied(_) => {
17251726
return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id", funding_msg.channel_id))
17261727
},
17271728
hash_map::Entry::Vacant(e) => {
1729+
channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
1730+
node_id: their_node_id.clone(),
1731+
msg: funding_msg,
1732+
});
17281733
e.insert(chan);
17291734
}
17301735
}
1731-
Ok(funding_msg)
1736+
Ok(())
17321737
}
17331738

17341739
fn internal_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), MsgHandleErrInternal> {
@@ -2494,7 +2499,7 @@ impl ChannelMessageHandler for ChannelManager {
24942499
handle_error!(self, self.internal_accept_channel(their_node_id, msg), their_node_id)
24952500
}
24962501

2497-
fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
2502+
fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), HandleError> {
24982503
handle_error!(self, self.internal_funding_created(their_node_id, msg), their_node_id)
24992504
}
25002505

@@ -2918,22 +2923,15 @@ mod tests {
29182923
_ => panic!("Unexpected event"),
29192924
}
29202925

2921-
let events_3 = node_a.node.get_and_clear_pending_msg_events();
2922-
assert_eq!(events_3.len(), 1);
2923-
let funding_signed = match events_3[0] {
2924-
MessageSendEvent::SendFundingCreated { ref node_id, ref msg } => {
2925-
assert_eq!(*node_id, node_b.node.get_our_node_id());
2926-
let res = node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), msg).unwrap();
2927-
let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
2928-
assert_eq!(added_monitors.len(), 1);
2929-
assert_eq!(added_monitors[0].0, funding_output);
2930-
added_monitors.clear();
2931-
res
2932-
},
2933-
_ => panic!("Unexpected event"),
2934-
};
2926+
node_b.node.handle_funding_created(&node_a.node.get_our_node_id(), &get_event_msg!(node_a, MessageSendEvent::SendFundingCreated, node_b.node.get_our_node_id())).unwrap();
2927+
{
2928+
let mut added_monitors = node_b.chan_monitor.added_monitors.lock().unwrap();
2929+
assert_eq!(added_monitors.len(), 1);
2930+
assert_eq!(added_monitors[0].0, funding_output);
2931+
added_monitors.clear();
2932+
}
29352933

2936-
node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &funding_signed).unwrap();
2934+
node_a.node.handle_funding_signed(&node_b.node.get_our_node_id(), &get_event_msg!(node_b, MessageSendEvent::SendFundingSigned, node_a.node.get_our_node_id())).unwrap();
29372935
{
29382936
let mut added_monitors = node_a.chan_monitor.added_monitors.lock().unwrap();
29392937
assert_eq!(added_monitors.len(), 1);

src/ln/msgs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub struct AcceptChannel {
212212
}
213213

214214
/// A funding_created message to be sent or received from a peer
215+
#[derive(Clone)]
215216
pub struct FundingCreated {
216217
pub(crate) temporary_channel_id: [u8; 32],
217218
pub(crate) funding_txid: Sha256dHash,
@@ -220,6 +221,7 @@ pub struct FundingCreated {
220221
}
221222

222223
/// A funding_signed message to be sent or received from a peer
224+
#[derive(Clone)]
223225
pub struct FundingSigned {
224226
pub(crate) channel_id: [u8; 32],
225227
pub(crate) signature: Signature,
@@ -530,7 +532,7 @@ pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Syn
530532
/// Handle an incoming accept_channel message from the given peer.
531533
fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &AcceptChannel) -> Result<(), HandleError>;
532534
/// Handle an incoming funding_created message from the given peer.
533-
fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<FundingSigned, HandleError>;
535+
fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &FundingCreated) -> Result<(), HandleError>;
534536
/// Handle an incoming funding_signed message from the given peer.
535537
fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &FundingSigned) -> Result<(), HandleError>;
536538
/// Handle an incoming funding_locked message from the given peer.

src/ln/peer_handler.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
568568

569569
34 => {
570570
let msg = try_potential_decodeerror!(msgs::FundingCreated::read(&mut reader));
571-
let resp = try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg));
572-
encode_and_send_msg!(resp, 35);
571+
try_potential_handleerror!(self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg));
573572
},
574573
35 => {
575574
let msg = try_potential_decodeerror!(msgs::FundingSigned::read(&mut reader));
@@ -818,6 +817,17 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
818817
peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 34)));
819818
Self::do_attempt_write_data(&mut descriptor, peer);
820819
},
820+
MessageSendEvent::SendFundingSigned { ref node_id, ref msg } => {
821+
log_trace!(self, "Handling SendFundingSigned event in peer_handler for node {} for channel {}",
822+
log_pubkey!(node_id),
823+
log_bytes!(msg.channel_id));
824+
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
825+
//TODO: generate a DiscardFunding event indicating to the wallet that
826+
//they should just throw away this funding transaction
827+
});
828+
peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 35)));
829+
Self::do_attempt_write_data(&mut descriptor, peer);
830+
},
821831
MessageSendEvent::SendFundingLocked { ref node_id, ref msg, ref announcement_sigs } => {
822832
log_trace!(self, "Handling SendFundingLocked event in peer_handler for node {}{} for channel {}",
823833
log_pubkey!(node_id),

src/util/events.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ pub enum MessageSendEvent {
114114
/// The message which should be sent.
115115
msg: msgs::FundingCreated,
116116
},
117+
/// Used to indicate that a funding_signed message should be sent to the peer with the given node_id.
118+
SendFundingSigned {
119+
/// The node_id of the node which should receive this message
120+
node_id: PublicKey,
121+
/// The message which should be sent.
122+
msg: msgs::FundingSigned,
123+
},
117124
/// Used to indicate that a funding_locked message should be sent to the peer with the given node_id.
118125
SendFundingLocked {
119126
/// The node_id of the node which should receive these message(s)

src/util/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
9292
fn handle_accept_channel(&self, _their_node_id: &PublicKey, _msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
9393
Err(HandleError { err: "", action: None })
9494
}
95-
fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
95+
fn handle_funding_created(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingCreated) -> Result<(), HandleError> {
9696
Err(HandleError { err: "", action: None })
9797
}
9898
fn handle_funding_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::FundingSigned) -> Result<(), HandleError> {

0 commit comments

Comments
 (0)