Skip to content

Return owned Strings for onion message message types #3273

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

Returning a reference from a trait method is relatively difficult to map in bindings and is currently handled by storing the object in the trait instance, returning a reference to the local field.

This is fine when the object we're returning only needs to live as long as the trait, but when it needs to be 'static (as is the case for onion message msg_types), there's not really a good way to map them at all.

Instead, here, condition on #[cfg(c_bindings)] we return a fully owned String. This is obviously relatively less effecient, but the extra allocation and memcpy isn't the end of the world, especially given it should be released relatively quickly.

@TheBlueMatt TheBlueMatt added this to the 0.0.124 milestone Aug 26, 2024
Copy link

codecov bot commented Aug 26, 2024

Codecov Report

Attention: Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.84%. Comparing base (ff0874a) to head (feffaf8).
Report is 13 commits behind head on main.

Files Patch % Lines
lightning/src/onion_message/offers.rs 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3273      +/-   ##
==========================================
+ Coverage   89.82%   89.84%   +0.01%     
==========================================
  Files         126      126              
  Lines      102988   103142     +154     
  Branches   102988   103142     +154     
==========================================
+ Hits        92513    92669     +156     
+ Misses       7757     7752       -5     
- Partials     2718     2721       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

jkczyz
jkczyz previously approved these changes Aug 26, 2024
@TheBlueMatt TheBlueMatt dismissed stale reviews from jkczyz and valentinewallace via da43a84 August 26, 2024 23:47
@TheBlueMatt TheBlueMatt force-pushed the 2024-08-bindings-no-static branch from 9136597 to da43a84 Compare August 26, 2024 23:47
@TheBlueMatt
Copy link
Collaborator Author

TheBlueMatt commented Aug 26, 2024

Grr, tests with c_bindings failed:

$ git diff-tree -U2 9136597f7 feffaf8bb
diff --git a/ci/ci-tests.sh b/ci/ci-tests.sh
index 22d0783a9..aff1a46c4 100755
--- a/ci/ci-tests.sh
+++ b/ci/ci-tests.sh
@@ -88,5 +88,7 @@ cargo test -p lightning --verbose --color always --features no-std

 echo -e "\n\nTesting c_bindings builds"
-RUSTFLAGS="$RUSTFLAGS --cfg=c_bindings" cargo test --verbose --color always
+# Note that because `$RUSTFLAGS` is not passed through to doctest builds we cannot selectively
+# disable doctests in `c_bindings` so we skip doctests entirely here.
+RUSTFLAGS="$RUSTFLAGS --cfg=c_bindings" cargo test --verbose --color always --lib --bins --tests

 for DIR in lightning-invoice lightning-rapid-gossip-sync; do
@@ -96,7 +98,7 @@ done

 # Note that because `$RUSTFLAGS` is not passed through to doctest builds we cannot selectively
-# disable tests in `c_bindings` so we skip doctests entirely here.
+# disable doctests in `c_bindings` so we skip doctests entirely here.
 RUSTFLAGS="$RUSTFLAGS --cfg=c_bindings" cargo test -p lightning-background-processor --verbose --color always --features futures --no-default-features --lib --bins --tests
-RUSTFLAGS="$RUSTFLAGS --cfg=c_bindings" cargo test -p lightning --verbose --color always --no-default-features --features=no-std
+RUSTFLAGS="$RUSTFLAGS --cfg=c_bindings" cargo test -p lightning --verbose --color always --no-default-features --features=no-std --lib --bins --tests

 echo -e "\n\nTesting other crate-specific builds"
diff --git a/lightning/src/onion_message/functional_tests.rs b/lightning/src/onion_message/functional_tests.rs
index 700f275e5..56c540057 100644
--- a/lightning/src/onion_message/functional_tests.rs
+++ b/lightning/src/onion_message/functional_tests.rs
@@ -109,4 +109,9 @@ impl OnionMessageContents for TestCustomMessage {
 		}
 	}
+	#[cfg(c_bindings)]
+	fn msg_type(&self) -> String {
+		"Custom Message".to_string()
+	}
+	#[cfg(not(c_bindings))]
 	fn msg_type(&self) -> &'static str {
 		"Custom Message"
@@ -657,4 +662,9 @@ fn invalid_custom_message_type() {
 			63
 		}
+		#[cfg(c_bindings)]
+		fn msg_type(&self) -> String {
+			"Invalid Message".to_string()
+		}
+		#[cfg(not(c_bindings))]
 		fn msg_type(&self) -> &'static str {
 			"Invalid Message"
$ 

Returning a reference from a trait method is relatively difficult
to map in bindings and is currently handled by storing the object
in the trait instance, returning a reference to the local field.

This is fine when the object we're returning only needs to live as
long as the trait, but when it needs to be `'static` (as is the
case for onion message `msg_type`s), there's not really a good way
to map them at all.

Instead, here, condition on `#[cfg(c_bindings)]` we return a fully
owned `String`. This is obviously relatively less effecient, but
the extra allocation and `memcpy` isn't the end of the world,
especially given it should be released relatively quickly.

Note that this breaks doctests in with `c_bindings`.
@TheBlueMatt TheBlueMatt force-pushed the 2024-08-bindings-no-static branch from da43a84 to feffaf8 Compare August 27, 2024 00:05
Copy link
Contributor

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@dunxen dunxen left a comment

Choose a reason for hiding this comment

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

Does what it says on the tin. LGTM.

@TheBlueMatt TheBlueMatt merged commit fd2f3dc into lightningdevkit:main Aug 27, 2024
17 of 19 checks passed
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.

5 participants