-
Notifications
You must be signed in to change notification settings - Fork 436
refactor: merge lightning-custom-message into lightning crate #4386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
okekefrancis112
wants to merge
2
commits into
lightningdevkit:main
Choose a base branch
from
okekefrancis112:drop_crate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−68
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| // This file is Copyright its original authors, visible in version control | ||
| // history. | ||
| // | ||
| // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE | ||
| // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. | ||
| // You may not use this file except in accordance with one or both of these | ||
| // licenses. | ||
|
|
||
| //! Utilities for supporting custom peer-to-peer messages in LDK. | ||
| //! | ||
| //! [BOLT 1] specifies a custom message type range for use with experimental or application-specific | ||
| //! messages. While a [`CustomMessageHandler`] can be defined to support more than one message type, | ||
| //! defining such a handler requires a significant amount of boilerplate and can be error prone. | ||
| //! | ||
| //! This crate provides the [`composite_custom_message_handler`] macro for easily composing | ||
| //! This module provides the [`composite_custom_message_handler!`](crate::composite_custom_message_handler!) macro for easily composing | ||
| //! pre-defined custom message handlers into one handler. The resulting handler can be further | ||
| //! composed with other custom message handlers using the same macro. | ||
| //! | ||
|
|
@@ -13,14 +22,10 @@ | |
| //! | ||
| //!``` | ||
| //! # fn main() {} // Avoid #[macro_export] generating an in-function warning | ||
| //! # extern crate bitcoin; | ||
| //! extern crate lightning; | ||
| //! #[macro_use] | ||
| //! extern crate lightning_custom_message; | ||
| //! | ||
| //! # use bitcoin::secp256k1::PublicKey; | ||
| //! # use lightning::io; | ||
| //! # use lightning::ln::msgs::{DecodeError, Init, LightningError}; | ||
| //! use lightning::composite_custom_message_handler; | ||
| //! use lightning::ln::peer_handler::CustomMessageHandler; | ||
| //! use lightning::ln::wire::{CustomMessageReader, self}; | ||
| //! # use lightning::types::features::{InitFeatures, NodeFeatures}; | ||
|
|
@@ -228,12 +233,7 @@ | |
| //!``` | ||
| //! | ||
| //! [BOLT 1]: https://github.com/lightning/bolts/blob/master/01-messaging.md | ||
| //! [`CustomMessageHandler`]: crate::lightning::ln::peer_handler::CustomMessageHandler | ||
|
|
||
| #![doc(test(no_crate_inject, attr(deny(warnings))))] | ||
|
|
||
| pub extern crate bitcoin; | ||
| pub extern crate lightning; | ||
| //! [`CustomMessageHandler`]: crate::ln::peer_handler::CustomMessageHandler | ||
|
|
||
| /// Defines a composite type implementing [`CustomMessageHandler`] (and therefore also implementing | ||
| /// [`CustomMessageReader`]), along with a corresponding enumerated custom message [`Type`], from | ||
|
|
@@ -244,13 +244,12 @@ pub extern crate lightning; | |
| /// though using OR-ed literal patterns is preferred in order to catch unreachable code for | ||
| /// conflicting handlers. | ||
| /// | ||
| /// See [crate documentation] for example usage. | ||
| /// See [module documentation](self) for example usage. | ||
| /// | ||
| /// [`CustomMessageHandler`]: crate::lightning::ln::peer_handler::CustomMessageHandler | ||
| /// [`CustomMessageReader`]: crate::lightning::ln::wire::CustomMessageReader | ||
| /// [`Type`]: crate::lightning::ln::wire::Type | ||
| /// [`PeerManager`]: crate::lightning::ln::peer_handler::PeerManager | ||
| /// [crate documentation]: self | ||
| /// [`CustomMessageHandler`]: crate::ln::peer_handler::CustomMessageHandler | ||
| /// [`CustomMessageReader`]: crate::ln::wire::CustomMessageReader | ||
| /// [`Type`]: crate::ln::wire::Type | ||
| /// [`PeerManager`]: crate::ln::peer_handler::PeerManager | ||
| #[macro_export] | ||
| macro_rules! composite_custom_message_handler { | ||
| ( | ||
|
|
@@ -273,26 +272,26 @@ macro_rules! composite_custom_message_handler { | |
| #[derive(Debug)] | ||
| $message_visibility enum $message { | ||
| $( | ||
| $variant(<$type as $crate::lightning::ln::wire::CustomMessageReader>::CustomMessage), | ||
| $variant(<$type as $crate::ln::wire::CustomMessageReader>::CustomMessage), | ||
| )* | ||
| } | ||
|
|
||
| impl $crate::lightning::ln::peer_handler::CustomMessageHandler for $handler { | ||
| impl $crate::ln::peer_handler::CustomMessageHandler for $handler { | ||
| fn handle_custom_message( | ||
| &self, msg: Self::CustomMessage, sender_node_id: $crate::bitcoin::secp256k1::PublicKey | ||
| ) -> Result<(), $crate::lightning::ln::msgs::LightningError> { | ||
| &self, msg: Self::CustomMessage, sender_node_id: bitcoin::secp256k1::PublicKey | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving out $crate might be a problem when this exported macro is used in other crates? |
||
| ) -> Result<(), $crate::ln::msgs::LightningError> { | ||
| match msg { | ||
| $( | ||
| $message::$variant(message) => { | ||
| $crate::lightning::ln::peer_handler::CustomMessageHandler::handle_custom_message( | ||
| $crate::ln::peer_handler::CustomMessageHandler::handle_custom_message( | ||
| &self.$field, message, sender_node_id | ||
| ) | ||
| }, | ||
| )* | ||
| } | ||
| } | ||
|
|
||
| fn get_and_clear_pending_msg(&self) -> Vec<($crate::bitcoin::secp256k1::PublicKey, Self::CustomMessage)> { | ||
| fn get_and_clear_pending_msg(&self) -> Vec<(bitcoin::secp256k1::PublicKey, Self::CustomMessage)> { | ||
| vec![].into_iter() | ||
| $( | ||
| .chain( | ||
|
|
@@ -305,13 +304,13 @@ macro_rules! composite_custom_message_handler { | |
| .collect() | ||
| } | ||
|
|
||
| fn peer_disconnected(&self, their_node_id: $crate::bitcoin::secp256k1::PublicKey) { | ||
| fn peer_disconnected(&self, their_node_id: bitcoin::secp256k1::PublicKey) { | ||
| $( | ||
| self.$field.peer_disconnected(their_node_id); | ||
| )* | ||
| } | ||
|
|
||
| fn peer_connected(&self, their_node_id: $crate::bitcoin::secp256k1::PublicKey, msg: &$crate::lightning::ln::msgs::Init, inbound: bool) -> Result<(), ()> { | ||
| fn peer_connected(&self, their_node_id: bitcoin::secp256k1::PublicKey, msg: &$crate::ln::msgs::Init, inbound: bool) -> Result<(), ()> { | ||
| let mut result = Ok(()); | ||
| $( | ||
| if let Err(e) = self.$field.peer_connected(their_node_id, msg, inbound) { | ||
|
|
@@ -321,28 +320,28 @@ macro_rules! composite_custom_message_handler { | |
| result | ||
| } | ||
|
|
||
| fn provided_node_features(&self) -> $crate::lightning::types::features::NodeFeatures { | ||
| $crate::lightning::types::features::NodeFeatures::empty() | ||
| fn provided_node_features(&self) -> $crate::types::features::NodeFeatures { | ||
| $crate::types::features::NodeFeatures::empty() | ||
| $( | ||
| | self.$field.provided_node_features() | ||
| )* | ||
| } | ||
|
|
||
| fn provided_init_features( | ||
| &self, their_node_id: $crate::bitcoin::secp256k1::PublicKey | ||
| ) -> $crate::lightning::types::features::InitFeatures { | ||
| $crate::lightning::types::features::InitFeatures::empty() | ||
| &self, their_node_id: bitcoin::secp256k1::PublicKey | ||
| ) -> $crate::types::features::InitFeatures { | ||
| $crate::types::features::InitFeatures::empty() | ||
| $( | ||
| | self.$field.provided_init_features(their_node_id) | ||
| )* | ||
| } | ||
| } | ||
|
|
||
| impl $crate::lightning::ln::wire::CustomMessageReader for $handler { | ||
| impl $crate::ln::wire::CustomMessageReader for $handler { | ||
| type CustomMessage = $message; | ||
| fn read<R: $crate::lightning::util::ser::LengthLimitedRead>( | ||
| fn read<R: $crate::util::ser::LengthLimitedRead>( | ||
| &self, message_type: u16, buffer: &mut R | ||
| ) -> Result<Option<Self::CustomMessage>, $crate::lightning::ln::msgs::DecodeError> { | ||
| ) -> Result<Option<Self::CustomMessage>, $crate::ln::msgs::DecodeError> { | ||
| match message_type { | ||
| $( | ||
| $pattern => match <$type>::read(&self.$field, message_type, buffer)? { | ||
|
|
@@ -355,7 +354,7 @@ macro_rules! composite_custom_message_handler { | |
| } | ||
| } | ||
|
|
||
| impl $crate::lightning::ln::wire::Type for $message { | ||
| impl $crate::ln::wire::Type for $message { | ||
| fn type_id(&self) -> u16 { | ||
| match self { | ||
| $( | ||
|
|
@@ -365,8 +364,8 @@ macro_rules! composite_custom_message_handler { | |
| } | ||
| } | ||
|
|
||
| impl $crate::lightning::util::ser::Writeable for $message { | ||
| fn write<W: $crate::lightning::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::lightning::io::Error> { | ||
| impl $crate::util::ser::Writeable for $message { | ||
| fn write<W: $crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> { | ||
| match self { | ||
| $( | ||
| Self::$variant(message) => message.write(writer), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check q: I think the only testing were doc tests, that are now running as part of lightning?