-
Notifications
You must be signed in to change notification settings - Fork 377
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
implement fromstr trait to netaddress #2134
Conversation
@TheBlueMatt @tnull any recommendation on how to decode the onionv3 address? I could potentially use https://crates.io/crates/base32 but im not sure about adding a new package just for that? |
Hmm, yea, that's a good question - I took a brief glance at the base32 code, it looks fine, though could use one or two trivial changes (and a fuzzer, it has an obvious panic in the decode!). Luckily its the same license so we'd have to add a notice that it includes code which is "Copyright (c) 2015 The base32 Developers" but that's nbd. We should pull it in by coping their lib.rs, though, not taking the dependency, there's no need for a dep here. |
made some changes(main functionality is implemented and tests are passing) to the code and updated the top comment accordingly |
362a9b4
to
d807a53
Compare
25250ca
to
3e46d4c
Compare
b6f5ae6
to
471c5d7
Compare
lightning/src/util/base32.rs
Outdated
use crate::prelude::*; | ||
|
||
/// Alphabet used for encoding and decoding. | ||
#[derive(Copy, Clone)] |
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.
(a) we can drop the crockford encoding, we don't use it. (b) we should move the zbase32 stuff we already have to here - its the same code anyway, but just a different alphabet.
a092195
to
cf72e89
Compare
Let me know when you're ready for another look here, looks like you're in the midst of merging the two base32s. |
cf72e89
to
ea1b296
Compare
Codecov ReportPatch coverage:
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the GitHub App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## main #2134 +/- ##
==========================================
+ Coverage 90.58% 90.60% +0.01%
==========================================
Files 110 110
Lines 57526 57555 +29
Branches 57526 57555 +29
==========================================
+ Hits 52112 52149 +37
+ Misses 5414 5406 -8
☔ View full report in Codecov by Sentry. |
@TheBlueMatt can u please give this another round of review? also, any tips on how to debug the fuzzer locally would be much appreciated |
Re: fuzzer reproduction, the |
359e099
to
d74c87f
Compare
apologies for the slow execution, life got in the middle (: ...and ill cleanup commits |
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.
Okay, really just one nit left!
56b168a
to
dbded6e
Compare
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.
A few minor nits, but nothing blocking.
lightning/src/util/base32.rs
Outdated
]; | ||
|
||
const RFC4648_TEST_VECTORS: &[(&[u8], &[u8])] = &[ | ||
(b"", b""), |
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.
data type for the second element in the tuple should be a str, which you're currently doing in the tests with std::str::from_utf8...unwrap
.
lightning/src/util/base32.rs
Outdated
|
||
for (_, encoded) in RFC4648_TEST_VECTORS { | ||
let decoded = &Alphabet::RFC4648 { padding: true }.decode(std::str::from_utf8(encoded).unwrap()).unwrap(); | ||
assert_eq!(&Alphabet::RFC4648 { padding: true }.encode(decoded).as_bytes(), encoded); |
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.
This test is redundant - we already test that if we decode we get the expected value and if we encode we get the expected value, there's no need for an additional round-trip test.
@@ -3296,10 +3403,10 @@ mod tests { | |||
let shutdown = msgs::Shutdown { | |||
channel_id: [2; 32], | |||
scriptpubkey: | |||
if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey() } | |||
if script_type == 1 { Address::p2pkh(&::bitcoin::PublicKey{compressed: true, inner: pubkey_1}, Network::Testnet).script_pubkey() } |
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.
Why did you remove the alignment here and below?
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.
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.
Indeed, it was a space to align the opening { nicely no matter the users' configured tabstop.
lightning/src/ln/msgs.rs
Outdated
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
NetAddressParseError::SocketAddrParse => write!(f, "Socket address (IPv4/IPv6) parsing error"), | ||
NetAddressParseError::InvalidInput => write!(f, "{}", "Invalid input format. \ |
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.
You can drop the , "{}"
part.
Oh, if you push again, please fix the commit title on the last commit - commit description text must include a blank line between the title and the first non-title line (otherwise git treats it as a really long title, which breaks lots of things). |
Also, the description in the first commit doesn't tell me anything " restructure encode function restructure decode function" doesn't tell me what the commit is doing or why its doing that, though the title itself is pretty clear. Commit descriptions shouldn't include the history of that commit, by the time we merge it we don't care too much about the history of the PR's review. |
dbded6e
to
529a09f
Compare
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.
Mostly looks good from my side, but have yet to go over the base32 logic stuff one last time.
Also needs a minor rebase now it seems. |
Any update here, would really love to land this for 0.0.117 but its gonna get over the line in the next week or two. |
c9750ce
to
03e4ac8
Compare
@tnull split the indentation to a separate commit and added short docs for the numbers |
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.
Generally looks good, mod some nits/minor comments.
lightning/src/util/mod.rs
Outdated
@@ -58,3 +58,4 @@ pub mod test_utils; | |||
#[cfg(any(test, feature = "_test_utils"))] | |||
pub mod test_channel_signer; | |||
|
|||
pub mod enforcing_trait_impls; |
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.
This file was removed on main and we shouldn't re-add the module here.
@@ -143,4 +143,4 @@ mod test { | |||
assert!(verify(c[1].as_bytes(), c[2], &PublicKey::from_str(c[3]).unwrap())) | |||
} | |||
} | |||
} | |||
} |
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.
Don't remove the trailing newline.
lightning/src/util/base32.rs
Outdated
impl Alphabet { | ||
/// Encode bytes into a base32 string. | ||
pub fn encode(&self, data: &[u8]) -> String { | ||
// This line calculates the length of the output string, which is equal to the original data length * 8 / 5, rounded up. The + 4 at the end is to account for the extra padding characters that may be added. |
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.
nit: might want to wrap the comment lines also.
lightning/src/util/base32.rs
Outdated
if *padding { | ||
let len = ret.len(); | ||
let num_extra = len - output_length; | ||
for i in 1..num_extra + 1 { |
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.
Isn't this just the same as the following, which is a lot less confusing?:
for i in output_length..len {
ret[i] = b'=';
}
lightning/src/util/base32.rs
Outdated
// If the string has more characters than are required to alphabet_encode the number of bytes | ||
// decodable, treat the string as invalid. | ||
match data.len() % 8 { 1|3|6 => return Err(()), _ => {} } | ||
let mut ret = Self::decode_data(data, alphabet)?; |
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.
nit: Doesn't need to be mut
.
lightning/src/util/base32.rs
Outdated
let cap = (data.len() + 4) / 5 * 8; | ||
let mut ret = Vec::with_capacity(cap); | ||
for chunk in data.chunks(5) { | ||
let buf = { |
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.
I don't think we need this additional let
binding? Why not just use the mut buf
below?
lightning/src/util/base32.rs
Outdated
let cap = (data.len() + 7) / 8 * 5; | ||
let mut ret = Vec::with_capacity(cap); | ||
for chunk in data.chunks(8) { | ||
let buf = { |
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.
Same here, why do we need this additional let
binding?
03e4ac8
to
53db4ca
Compare
Fixed Up:
|
53db4ca
to
5f354fa
Compare
lightning/src/util/base32.rs
Outdated
pub fn encode(&self, data: &[u8]) -> String { | ||
// This line calculates the length of the output string, | ||
// which is equal to the original data length * 8 / 5, rounded up. | ||
// The + 4 at the end is to account for the extra padding characters that may be added. |
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.
No, the +4 is the "rounded up" part - rounding up with integer division is generally done by doing (numerator + divisor - 1) / divisor
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.
wdyt about this?
1 pub fn encode(&self, data: &[u8]) -> String {
| 45 // output_length is calculated as follows:
| 1 // / 5 divides the data length by the number of bits per chunk (5),
| 2 // * 8 multiplies the result by the number of characters per chunk (8).
| 3 // + 4 rounds up to the nearest character.
4 let output_length = (data.len() * 8 + 4) / 5;
```
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.
Sure.
lightning/src/util/base32.rs
Outdated
|
||
/// Encode a byte slice into a base32 string. | ||
fn encode_data(data: &[u8], alphabet: &'static [u8]) -> Vec<u8> { | ||
// The + 4 at the end is to account for the padding characters that may be needed, |
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.
No, the +5 is to round up.
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.
wdyt about this?
102 fn encode_data(data: &[u8], alphabet: &'static [u8]) -> Vec<u8> {
| 1 // cap is calculated as follows:
| 2 // / 5 divides the data length by the number of bits per chunk (5),
| 3 // * 8 multiplies the result by the number of characters per chunk (8).
| 4 // + 4 rounds up to the nearest character.
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.
Sure
lightning/src/util/base32.rs
Outdated
|
||
|
||
fn decode_data(data: &[u8], alphabet: [i8; 43]) -> Result<Vec<u8>, ()> { | ||
// The + 7 at the end is to account for the padding characters that may be needed, |
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.
No, the +7 is to round up.
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.
wdyt about this?
129 fn decode_data(data: &[u8], alphabet: [i8; 43]) -> Result<Vec<u8>, ()> {
| 1 // cap is calculated as follows:
| 2 // / 8 divides the data length by the number of characters per chunk (8),
| 3 // * 5 multiplies the result by the number of bits per chunk (5),
| 4 // + 7 rounds up to the nearest byte.
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.
Sure
lightning/src/util/base32.rs
Outdated
|
||
fn decode_data(data: &[u8], alphabet: [i8; 43]) -> Result<Vec<u8>, ()> { | ||
// The + 7 at the end is to account for the padding characters that may be needed, | ||
// The / 8 divides the Base64 string length by the number of characters per chunk (8), |
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.
nit: here, and a few places, you have excess whitespace at the end of your line. A local git show
should highlight these depending on your terminal settings.
lightning/src/ln/msgs.rs
Outdated
match { | ||
addr | ||
} { |
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.
match { | |
addr | |
} { | |
match addr { |
- Add fuzz test for `NetAddress` `from_str` function
5f354fa
to
4f45cdc
Compare
resolves #2056