Skip to content

Commit

Permalink
f: Update trait bounds
Browse files Browse the repository at this point in the history
1. Updated handle_message trait bound
2. Update handle_custom_message trait bound
  • Loading branch information
shaavan committed Mar 6, 2024
1 parent 2806122 commit 9699624
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 20 deletions.
4 changes: 2 additions & 2 deletions fuzz/src/onion_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl MessageRouter for TestMessageRouter {
struct TestOffersMessageHandler {}

impl OffersMessageHandler for TestOffersMessageHandler {
fn handle_message<F: Fn(OffersMessage, BlindedPath)>(&self, _message: ReceivedOnionMessage<OMH, OffersMessage>) {}
fn handle_message<R: RespondFunction<OffersMessage>>(&self, _message: ReceivedOnionMessage<R, OffersMessage>) {}
}

#[derive(Debug)]
Expand All @@ -121,7 +121,7 @@ struct TestCustomMessageHandler {}

impl CustomOnionMessageHandler for TestCustomMessageHandler {
type CustomMessage = TestCustomMessage;
fn handle_custom_message<F: Fn(Self::CustomMessage, BlindedPath), T: OnionMessageContents>(&self, message: ReceivedOnionMessage<F, T>) {
fn handle_custom_message<R: RespondFunction<Self::CustomMessage>>(&self, message: ReceivedOnionMessage<R, Self::CustomMessage>) {
if let ReceivedOnionMessage::WithReplyPath({_, responder}) = message {
responder.respond(TestCustomMessage {})
}
Expand Down
5 changes: 3 additions & 2 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use crate::offers::merkle::SignError;
use crate::offers::offer::{DerivedMetadata, Offer, OfferBuilder};
use crate::offers::parse::Bolt12SemanticError;
use crate::offers::refund::{Refund, RefundBuilder};
use crate::onion_message::messenger::{new_pending_onion_message, Destination, MessageRouter, PendingOnionMessage, ReceivedOnionMessage};
use crate::onion_message::messenger::{new_pending_onion_message, Destination, MessageRouter, PendingOnionMessage, ReceivedOnionMessage, RespondFunction};
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
use crate::sign::ecdsa::WriteableEcdsaChannelSigner;
Expand Down Expand Up @@ -9274,7 +9274,8 @@ where
R::Target: Router,
L::Target: Logger,
{
fn handle_message<F: Fn(OffersMessage, BlindedPath)>(&self, message: ReceivedOnionMessage<F, OffersMessage>) {
fn handle_message<RF: RespondFunction<OffersMessage>>(&self, message: ReceivedOnionMessage<RF, OffersMessage>)
{
let secp_ctx = &self.secp_ctx;
let expanded_key = &self.inbound_payment_key;

Expand Down
2 changes: 0 additions & 2 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ use bitcoin::blockdata::script::ScriptBuf;
use bitcoin::hash_types::Txid;

use crate::blinded_path::payment::{BlindedPaymentTlvs, ForwardTlvs, ReceiveTlvs};
use crate::blinded_path::BlindedPath;
use crate::ln::{ChannelId, PaymentPreimage, PaymentHash, PaymentSecret};
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
use crate::ln::onion_utils;
use crate::onion_message;
use crate::onion_message::packet::OnionMessageContents;
use crate::sign::{NodeSigner, Recipient};

use crate::prelude::*;
Expand Down
7 changes: 3 additions & 4 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use bitcoin::blockdata::constants::ChainHash;
use bitcoin::secp256k1::{self, Secp256k1, SecretKey, PublicKey};

use crate::blinded_path::BlindedPath;
use crate::sign::{NodeSigner, Recipient};
use crate::events::{EventHandler, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
use crate::ln::ChannelId;
Expand All @@ -29,7 +28,7 @@ use crate::util::ser::{VecWriter, Writeable, Writer};
use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor, NextNoiseStep, MessageBuf, MSG_BUF_ALLOC_SIZE};
use crate::ln::wire;
use crate::ln::wire::{Encode, Type};
use crate::onion_message::messenger::{CustomOnionMessageHandler, PendingOnionMessage, ReceivedOnionMessage};
use crate::onion_message::messenger::{CustomOnionMessageHandler, PendingOnionMessage, ReceivedOnionMessage, RespondFunction};
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
use crate::onion_message::packet::OnionMessageContents;
use crate::routing::gossip::{NodeId, NodeAlias};
Expand Down Expand Up @@ -135,11 +134,11 @@ impl OnionMessageHandler for IgnoringMessageHandler {
}

impl OffersMessageHandler for IgnoringMessageHandler {
fn handle_message<F: Fn(OffersMessage, BlindedPath)>(&self, _message: ReceivedOnionMessage<F, OffersMessage>) {}
fn handle_message<R: RespondFunction<OffersMessage>>(&self, _message: ReceivedOnionMessage<R, OffersMessage>) {}
}
impl CustomOnionMessageHandler for IgnoringMessageHandler {
type CustomMessage = Infallible;
fn handle_custom_message<F: Fn(Self::CustomMessage, BlindedPath)>(&self, _message: ReceivedOnionMessage<F, Self::CustomMessage>) {
fn handle_custom_message<R: RespondFunction<Self::CustomMessage>>(&self, _message: ReceivedOnionMessage<R, Self::CustomMessage>) {
// Since we always return `None` in the read the handle method should never be called.
unreachable!();
}
Expand Down
6 changes: 3 additions & 3 deletions lightning/src/onion_message/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::ln::msgs::{self, DecodeError, OnionMessageHandler, SocketAddress};
use crate::sign::{NodeSigner, Recipient};
use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer};
use crate::util::test_utils;
use super::messenger::{CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, PendingOnionMessage, ReceivedOnionMessage, SendError};
use super::messenger::{CustomOnionMessageHandler, Destination, MessageRouter, OnionMessagePath, OnionMessenger, PendingOnionMessage, ReceivedOnionMessage, RespondFunction, SendError};
use super::offers::{OffersMessage, OffersMessageHandler};
use super::packet::{OnionMessageContents, Packet};

Expand Down Expand Up @@ -70,7 +70,7 @@ impl MessageRouter for TestMessageRouter {
struct TestOffersMessageHandler {}

impl OffersMessageHandler for TestOffersMessageHandler {
fn handle_message<F: Fn(OffersMessage, BlindedPath)>(&self, _message: ReceivedOnionMessage<F, OffersMessage>) {}
fn handle_message<R: RespondFunction<OffersMessage>>(&self, _message: ReceivedOnionMessage<R, OffersMessage>) {}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Drop for TestCustomMessageHandler {

impl CustomOnionMessageHandler for TestCustomMessageHandler {
type CustomMessage = TestCustomMessage;
fn handle_custom_message<F: Fn(Self::CustomMessage, BlindedPath)>(&self, message: ReceivedOnionMessage<F, Self::CustomMessage>) {
fn handle_custom_message<R: RespondFunction<Self::CustomMessage>>(&self, message: ReceivedOnionMessage<R, Self::CustomMessage>) {
if let ReceivedOnionMessage::WithReplyPath{responder, message, path_id: _} = message {
match self.expected_messages.lock().unwrap().pop_front() {
Some(expected_msg) => assert_eq!(expected_msg, message),
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ pub trait CustomOnionMessageHandler {
/// Called with the custom message that was received, returning a response to send, if any.
///
/// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
fn handle_custom_message<F: Fn(Self::CustomMessage, BlindedPath)>(&self, message: ReceivedOnionMessage<F, Self::CustomMessage>);
fn handle_custom_message<R: RespondFunction<Self::CustomMessage>>(&self, message: ReceivedOnionMessage<R, Self::CustomMessage>);

/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
/// message type is unknown.
Expand Down
9 changes: 3 additions & 6 deletions lightning/src/onion_message/offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

use core::convert::TryFrom;
use core::fmt;
use crate::blinded_path::BlindedPath;
use crate::io::{self, Read};
use crate::ln::msgs::{DecodeError, OnionMessageHandler};
use crate::ln::msgs::DecodeError;
use crate::offers::invoice_error::InvoiceError;
use crate::offers::invoice_request::InvoiceRequest;
use crate::offers::invoice::Bolt12Invoice;
Expand All @@ -22,7 +21,7 @@ use crate::onion_message::packet::OnionMessageContents;
use crate::util::logger::Logger;
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
#[cfg(not(c_bindings))]
use crate::onion_message::messenger::PendingOnionMessage;
use crate::onion_message::messenger::{PendingOnionMessage, ReceivedOnionMessage, RespondFunction};

use crate::prelude::*;

Expand All @@ -31,8 +30,6 @@ const INVOICE_REQUEST_TLV_TYPE: u64 = 64;
const INVOICE_TLV_TYPE: u64 = 66;
const INVOICE_ERROR_TLV_TYPE: u64 = 68;

use crate::onion_message::messenger::ReceivedOnionMessage;

/// A handler for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload.
///
/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
Expand All @@ -43,7 +40,7 @@ pub trait OffersMessageHandler {
/// The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
///
/// [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
fn handle_message<F: Fn(OffersMessage, BlindedPath)>(&self, message: ReceivedOnionMessage<F, OffersMessage>);
fn handle_message<R: RespondFunction<OffersMessage>>(&self, message: ReceivedOnionMessage<R, OffersMessage>);

/// Releases any [`OffersMessage`]s that need to be sent.
///
Expand Down

0 comments on commit 9699624

Please sign in to comment.