Skip to content

Concretize bindings templates #788

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

Merged

Conversation

TheBlueMatt
Copy link
Collaborator

(based on #787 because I'm too lazy to rebase).

This concertizes the templates in the bindings, making the bindings Rust code a little closer to what we end up with in C, leaning a little less on cbindgen. It also significantly reduces the template-specific type resolution logic, which is a big win.

Finally, while we're at it, we add _clone methods for template types, making downstream usage much simpler.

@codecov
Copy link

codecov bot commented Feb 3, 2021

Codecov Report

Merging #788 (c2fd683) into main (c35002f) will increase coverage by 0.04%.
The diff coverage is 33.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #788      +/-   ##
==========================================
+ Coverage   90.75%   90.80%   +0.04%     
==========================================
  Files          44       44              
  Lines       24119    24073      -46     
==========================================
- Hits        21890    21859      -31     
+ Misses       2229     2214      -15     
Impacted Files Coverage Δ
lightning/src/ln/channelmanager.rs 84.99% <0.00%> (+<0.01%) ⬆️
lightning/src/ln/msgs.rs 89.50% <0.00%> (+0.04%) ⬆️
lightning/src/ln/peer_handler.rs 49.72% <0.00%> (-0.07%) ⬇️
lightning/src/util/errors.rs 71.42% <0.00%> (-9.97%) ⬇️
lightning/src/util/ser.rs 90.61% <0.00%> (+0.29%) ⬆️
lightning/src/chain/channelmonitor.rs 95.67% <100.00%> (+0.03%) ⬆️
lightning/src/routing/network_graph.rs 90.96% <100.00%> (+0.07%) ⬆️
lightning/src/chain/keysinterface.rs 92.40% <0.00%> (-0.92%) ⬇️
lightning/src/util/events.rs 23.42% <0.00%> (-0.27%) ⬇️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c35002f...c2fd683. Read the comment docs.

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not my most meticulous review, but this LGTM 🥇

Exciting to get closer to support for wasm_bindgen + nice code simplifications

While the type aliasing trick works great for cbindgen,
wasm_bindgen doesn't support it and requires fully-concrete types.
In order to better support wasm_bindgen in the future, we do so
here, adding a function which manually writes out almost the exact
thing which was templated previously in concrete form.

As a nice side-effect, we no longer have to allocate and free a u8
for generic parameters which were `()` (though we still do in some
conversion functions, which we can get rid of when we similarly
concretize all generics fully).
Previously we'd segfault trying to deref the NULL page, but there
is no reason to not simply clone by creating another opaque instance
with a null inner. This comes up specifically when cloning
ChannelSigners as the pubkeys instance is NULL on construction
before get_pubkeys is called.
@TheBlueMatt TheBlueMatt force-pushed the 2020-02-concrete-bindings branch from 26a2260 to 1d089ca Compare February 11, 2021 03:28
@TheBlueMatt
Copy link
Collaborator Author

TheBlueMatt commented Feb 11, 2021

Rebased with no changes and squashed the binindgs update commit into one. Moved from 26a2260 to c2fd683, which diff-tree shows the only changes are upstream merges, plus one fuzz fix and updating the binindgs to latest upstream.

The only API change outside of additional derives is to change
the inner field in `DecodeError::Io()` to an `std::io::ErrorKind`
instead of an `std::io::Error`. While `std::io::Error` obviously
makes more sense in context, it doesn't support Clone, and the
inner error largely doesn't have a lot of value on its own.
@TheBlueMatt TheBlueMatt force-pushed the 2020-02-concrete-bindings branch from 1d089ca to 0816cf4 Compare February 11, 2021 03:34
@TheBlueMatt TheBlueMatt force-pushed the 2020-02-concrete-bindings branch from 0816cf4 to c2fd683 Compare February 11, 2021 03:39
@TheBlueMatt
Copy link
Collaborator Author

No reason to sit on this given its Basically Just Bindings (tm). Gonna merge. Non-bindings diff is below.

$ git diff-tree -U3 upstream/main c2fd68377c15ff923981439bd2815df6b65e6c0c lightning
diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs
index b46a2df1..95495d1b 100644
--- a/lightning/src/chain/channelmonitor.rs
+++ b/lightning/src/chain/channelmonitor.rs
@@ -174,7 +174,7 @@ pub enum ChannelMonitorUpdateErr {
 /// means you tried to update a monitor for a different channel or the ChannelMonitorUpdate was
 /// corrupted.
 /// Contains a developer-readable error message.
-#[derive(Debug)]
+#[derive(Clone, Debug)]
 pub struct MonitorUpdateError(pub &'static str);
 
 /// An event to be processed by the ChannelManager.
diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs
index 62101372..3dcc74f9 100644
--- a/lightning/src/ln/channelmanager.rs
+++ b/lightning/src/ln/channelmanager.rs
@@ -514,7 +514,7 @@ pub struct ChannelDetails {
 /// If a payment fails to send, it can be in one of several states. This enum is returned as the
 /// Err() type describing which state the payment is in, see the description of individual enum
 /// states for more.
-#[derive(Debug)]
+#[derive(Clone, Debug)]
 pub enum PaymentSendFailure {
 	/// A parameter which was passed to send_payment was invalid, preventing us from attempting to
 	/// send the payment at all. No channel state has been changed or messages sent to peers, and
diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs
index 289cdae4..798764dc 100644
--- a/lightning/src/ln/msgs.rs
+++ b/lightning/src/ln/msgs.rs
@@ -33,6 +33,7 @@ use bitcoin::hash_types::{Txid, BlockHash};
 use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
 
 use std::{cmp, fmt};
+use std::fmt::Debug;
 use std::io::Read;
 
 use util::events::MessageSendEventsProvider;
@@ -44,7 +45,7 @@ use ln::channelmanager::{PaymentPreimage, PaymentHash, PaymentSecret};
 pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
 
 /// An error in decoding a message or struct.
-#[derive(Debug)]
+#[derive(Clone, Debug)]
 pub enum DecodeError {
 	/// A version byte specified something we don't know how to handle.
 	/// Includes unknown realm byte in an OnionHopData packet
@@ -60,7 +61,7 @@ pub enum DecodeError {
 	/// A length descriptor in the packet didn't describe the later data correctly
 	BadLengthDescriptor,
 	/// Error from std::io
-	Io(::std::io::Error),
+	Io(::std::io::ErrorKind),
 }
 
 /// An init message to be sent or received from a peer
@@ -674,6 +675,7 @@ pub enum ErrorAction {
 }
 
 /// An Err type for failure to process messages.
+#[derive(Clone)]
 pub struct LightningError {
 	/// A human-readable message describing the error
 	pub err: String,
@@ -949,7 +951,7 @@ impl From<::std::io::Error> for DecodeError {
 		if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
 			DecodeError::ShortRead
 		} else {
-			DecodeError::Io(e)
+			DecodeError::Io(e.kind())
 		}
 	}
 }
diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs
index a3c22921..0e9facb8 100644
--- a/lightning/src/ln/peer_handler.rs
+++ b/lightning/src/ln/peer_handler.rs
@@ -90,6 +90,7 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
 /// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
 /// generate no further read_event/write_buffer_space_avail/socket_disconnected calls for the
 /// descriptor.
+#[derive(Clone)]
 pub struct PeerHandleError {
 	/// Used to indicate that we probably can't make any future connections to this peer, implying
 	/// we should go ahead and force-close any channels we have with it.
diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs
index bba99244..040d3617 100644
--- a/lightning/src/routing/network_graph.rs
+++ b/lightning/src/routing/network_graph.rs
@@ -329,7 +329,7 @@ where
 	}
 }
 
-#[derive(PartialEq, Debug)]
+#[derive(Clone, PartialEq, Debug)]
 /// Details about one direction of a channel. Received
 /// within a channel update.
 pub struct DirectionalChannelInfo {
@@ -441,7 +441,7 @@ impl Writeable for RoutingFees {
 	}
 }
 
-#[derive(PartialEq, Debug)]
+#[derive(Clone, PartialEq, Debug)]
 /// Information received in the latest node_announcement from this node.
 pub struct NodeAnnouncementInfo {
 	/// Protocol features the node announced support for
@@ -507,7 +507,7 @@ impl Readable for NodeAnnouncementInfo {
 	}
 }
 
-#[derive(PartialEq)]
+#[derive(Clone, PartialEq)]
 /// Details about a node in the network, known from the network announcement.
 pub struct NodeInfo {
 	/// All valid channels a node has announced
diff --git a/lightning/src/util/errors.rs b/lightning/src/util/errors.rs
index a2a45a7b..f67621c8 100644
--- a/lightning/src/util/errors.rs
+++ b/lightning/src/util/errors.rs
@@ -13,6 +13,7 @@ use std::fmt;
 
 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
 /// too-high values)
+#[derive(Clone)]
 pub enum APIError {
 	/// Indicates the API was wholly misused (see err for more). Cases where these can be returned
 	/// are documented, but generally indicates some precondition of a function was violated.
diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs
index 581f228b..1d24e264 100644
--- a/lightning/src/util/ser.rs
+++ b/lightning/src/util/ser.rs
@@ -718,7 +718,7 @@ macro_rules! impl_consensus_ser {
 				match consensus::encode::Decodable::consensus_decode(r) {
 					Ok(t) => Ok(t),
 					Err(consensus::encode::Error::Io(ref e)) if e.kind() == ::std::io::ErrorKind::UnexpectedEof => Err(DecodeError::ShortRead),
-					Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e)),
+					Err(consensus::encode::Error::Io(e)) => Err(DecodeError::Io(e.kind())),
 					Err(_) => Err(DecodeError::InvalidValue),
 				}
 			}

@TheBlueMatt TheBlueMatt merged commit 879e309 into lightningdevkit:main Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants