]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Don't construct OnionMessage while holding peer lock
authorValentine Wallace <vwallace@protonmail.com>
Fri, 2 Sep 2022 17:41:41 +0000 (13:41 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Fri, 2 Sep 2022 20:27:38 +0000 (16:27 -0400)
lightning/src/onion_message/messenger.rs

index b9da9246cb103bcf44eb82050bd5870f45521ae5..a3320f10d21091a0faa3131f3125384cc51b871f 100644 (file)
@@ -255,6 +255,28 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
                                        hop_data: new_packet_bytes,
                                        hmac: next_hop_hmac,
                                };
+                               let onion_message = msgs::OnionMessage {
+                                       blinding_point: match next_blinding_override {
+                                               Some(blinding_point) => blinding_point,
+                                               None => {
+                                                       let blinding_factor = {
+                                                               let mut sha = Sha256::engine();
+                                                               sha.input(&msg.blinding_point.serialize()[..]);
+                                                               sha.input(control_tlvs_ss.as_ref());
+                                                               Sha256::from_engine(sha).into_inner()
+                                                       };
+                                                       let next_blinding_point = msg.blinding_point;
+                                                       match next_blinding_point.mul_tweak(&self.secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap()) {
+                                                               Ok(bp) => bp,
+                                                               Err(e) => {
+                                                                       log_trace!(self.logger, "Failed to compute next blinding point: {}", e);
+                                                                       return
+                                                               }
+                                                       }
+                                               },
+                                       },
+                                       onion_routing_packet: outgoing_packet,
+                               };
 
                                let mut pending_per_peer_msgs = self.pending_messages.lock().unwrap();
 
@@ -267,30 +289,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
                                                return
                                        },
                                        hash_map::Entry::Occupied(mut e) => {
-                                               e.get_mut().push_back(
-                                                       msgs::OnionMessage {
-                                                               blinding_point: match next_blinding_override {
-                                                                       Some(blinding_point) => blinding_point,
-                                                                       None => {
-                                                                               let blinding_factor = {
-                                                                                       let mut sha = Sha256::engine();
-                                                                                       sha.input(&msg.blinding_point.serialize()[..]);
-                                                                                       sha.input(control_tlvs_ss.as_ref());
-                                                                                       Sha256::from_engine(sha).into_inner()
-                                                                               };
-                                                                               let next_blinding_point = msg.blinding_point;
-                                                                               match next_blinding_point.mul_tweak(&self.secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap()) {
-                                                                                       Ok(bp) => bp,
-                                                                                       Err(e) => {
-                                                                                               log_trace!(self.logger, "Failed to compute next blinding point: {}", e);
-                                                                                               return
-                                                                                       }
-                                                                               }
-                                                                       },
-                                                               },
-                                                               onion_routing_packet: outgoing_packet,
-                                                       },
-                                               );
+                                               e.get_mut().push_back(onion_message);
                                                log_trace!(self.logger, "Forwarding an onion message to peer {}", next_node_id);
                                        }
                                };