Skip to content
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

add feature for Invoice ser/de #1548

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

NicolaLS
Copy link
Contributor

Add feature "serde_invoice" for conditional dependency on serde to serialize/deserialize invoice

@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2022

Codecov Report

Merging #1548 (6c15de1) into main (c180ddd) will decrease coverage by 0.03%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #1548      +/-   ##
==========================================
- Coverage   90.96%   90.93%   -0.04%     
==========================================
  Files          80       80              
  Lines       43533    43610      +77     
  Branches    43533    43610      +77     
==========================================
+ Hits        39599    39655      +56     
- Misses       3934     3955      +21     
Impacted Files Coverage Δ
lightning-invoice/src/lib.rs 87.39% <ø> (ø)
lightning/src/chain/onchaintx.rs 93.98% <0.00%> (-0.93%) ⬇️
lightning/src/ln/functional_tests.rs 96.89% <0.00%> (-0.26%) ⬇️
lightning/src/routing/scoring.rs 97.08% <0.00%> (ø)
lightning/src/ln/channelmanager.rs 84.38% <0.00%> (+0.04%) ⬆️
lightning/src/routing/gossip.rs 91.94% <0.00%> (+0.25%) ⬆️

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 c180ddd...6c15de1. Read the comment docs.

@TheBlueMatt
Copy link
Collaborator

I'm not sure I fully understand the motivation for this - you're adding methods to do the serialization not implementing serde traits, so it seems like its the same amount of code to just call the existing string-serialization methods?

@NicolaLS
Copy link
Contributor Author

@NicolaLS NicolaLS closed this Jun 17, 2022
@NicolaLS NicolaLS reopened this Jun 17, 2022
@NicolaLS
Copy link
Contributor Author

I'm not sure I fully understand the motivation for this - you're adding methods to do the serialization not implementing serde traits, so it seems like its the same amount of code to just call the existing string-serialization methods?

(accidentally closed) Hi, thank you for the reply, I'm not sure if I understand. You mean to serialize as string I don't need to use serializer.serialize_str(invoice.to_string().as_str()) but can use serializer.collect_str(&format_args!("{}", invoice)) because Invoice already has the Display trait to serialize ? The motivation for this is that people who need to build data structures also containing an Invoice can simply #[serde(with = "serde_invoice")] and don't need to write the module themself.

@NicolaLS NicolaLS force-pushed the serde-module branch 2 times, most recently from bdbdc54 to c7e1c4a Compare June 17, 2022 21:25
@NicolaLS
Copy link
Contributor Author

I'm not sure I fully understand the motivation for this - you're adding methods to do the serialization not implementing serde traits, so it seems like its the same amount of code to just call the existing string-serialization methods?

Ok now someone explained it to me. Does my approach make sense now ? (just force pushed)

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Right, this makes more sense now. I'm still kinda curious what your use-case is - I assume you're storing invoices in some larger struct and want to serde it?


[dev-dependencies]
lightning = { version = "0.0.108", path = "../lightning", default-features = false, features = ["_test_utils"] }
hex = "0.4"
serde = { version = "1.0.118", features = ["derive"]}
serde_json = "1.0.81"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh my bad I used it in the doc tests in the earlier version and forgot to delete it

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

LGTM, basically.

@@ -17,6 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]
default = ["std"]
no-std = ["hashbrown", "lightning/no-std", "core2/alloc"]
std = ["bitcoin_hashes/std", "num-traits/std", "lightning/std", "bech32/std"]
serde_invoice = ["serde"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we bother specifying the feature here? Even if we don't put it here users can use serde as a feature to turn on the dependency.

@@ -35,6 +35,8 @@ extern crate secp256k1;
extern crate alloc;
#[cfg(any(test, feature = "std"))]
extern crate core;
#[cfg(feature = "serde_invoice")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets use the implicit serde feature instead of serde_invoice for the cfg tags.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK (new to rust so I thought there has to be a name)

@TheBlueMatt
Copy link
Collaborator

LGTM, this should get some kinda test, though.

@NicolaLS NicolaLS marked this pull request as ready for review June 20, 2022 07:17
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Generally looks good, mod two nits. 👍

#[cfg(test)]
mod test {
use bitcoin_hashes::hex::FromHex;
use bitcoin_hashes::sha256;
use Invoice;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: No need to import this.

#[test]
fn test_serde() {
let invoice = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\
h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: These lines shouldn't indented as much. :)

let serialized_invoice = serde_json::to_string(&invoice).unwrap();
let deserialized_invoice: Invoice = serde_json::from_str(serialized_invoice.as_str()).unwrap();
assert_eq!(invoice, deserialized_invoice);
assert_eq!(invoice.to_string(), deserialized_invoice.to_string());
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also test here that the serialized invoice actually matches the initial string provided? Otherwise there might be the slight possibility that you make a mistake in the initial parsing and here only ever confirm that the logic is able to deserialize whatever you're serializing from that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But serde_json::to_string(...) serializes as JSON so the str is not "lnbc1..." but ""lnbc1..."" so I can't test that right ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, if it's indeed only about the wrapping "s, then you could just strip them before comparing the strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that works I'll force push again thank you

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

LGTM, mod one nit. Sorry :)

lightning-invoice/src/lib.rs Outdated Show resolved Hide resolved
@TheBlueMatt TheBlueMatt merged commit f512586 into lightningdevkit:main Jun 21, 2022
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.

4 participants