Skip to content

Commit 6286ec2

Browse files
Don't construct OnionMessage while holding peer lock
1 parent 3047dbb commit 6286ec2

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

lightning/src/onion_message/messenger.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,28 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
254254
hop_data: new_packet_bytes,
255255
hmac: next_hop_hmac,
256256
};
257+
let onion_message = msgs::OnionMessage {
258+
blinding_point: match next_blinding_override {
259+
Some(blinding_point) => blinding_point,
260+
None => {
261+
let blinding_factor = {
262+
let mut sha = Sha256::engine();
263+
sha.input(&msg.blinding_point.serialize()[..]);
264+
sha.input(control_tlvs_ss.as_ref());
265+
Sha256::from_engine(sha).into_inner()
266+
};
267+
let next_blinding_point = msg.blinding_point;
268+
match next_blinding_point.mul_tweak(&self.secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap()) {
269+
Ok(bp) => bp,
270+
Err(e) => {
271+
log_trace!(self.logger, "Failed to compute next blinding point: {}", e);
272+
return
273+
}
274+
}
275+
},
276+
},
277+
onion_routing_packet: outgoing_packet,
278+
};
257279

258280
let mut pending_per_peer_msgs = self.pending_messages.lock().unwrap();
259281

@@ -266,30 +288,7 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessageHandler for OnionMessenger<Si
266288
return
267289
},
268290
hash_map::Entry::Occupied(mut e) => {
269-
e.get_mut().push_back(
270-
msgs::OnionMessage {
271-
blinding_point: match next_blinding_override {
272-
Some(blinding_point) => blinding_point,
273-
None => {
274-
let blinding_factor = {
275-
let mut sha = Sha256::engine();
276-
sha.input(&msg.blinding_point.serialize()[..]);
277-
sha.input(control_tlvs_ss.as_ref());
278-
Sha256::from_engine(sha).into_inner()
279-
};
280-
let next_blinding_point = msg.blinding_point;
281-
match next_blinding_point.mul_tweak(&self.secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap()) {
282-
Ok(bp) => bp,
283-
Err(e) => {
284-
log_trace!(self.logger, "Failed to compute next blinding point: {}", e);
285-
return
286-
}
287-
}
288-
},
289-
},
290-
onion_routing_packet: outgoing_packet,
291-
},
292-
);
291+
e.get_mut().push_back(onion_message);
293292
log_trace!(self.logger, "Forwarding an onion message to peer {}", next_node_id);
294293
}
295294
};

0 commit comments

Comments
 (0)