Skip to content

[rlp] Add no_std support#167

Closed
koushiro wants to merge 8 commits into
paritytech:masterfrom
koushiro:rlp_no_std
Closed

[rlp] Add no_std support#167
koushiro wants to merge 8 commits into
paritytech:masterfrom
koushiro:rlp_no_std

Conversation

@koushiro
Copy link
Copy Markdown
Contributor

Signed-off-by: koushiro koushiro.cqx@gmail.com

What have I changed?

  • Add no_std support for rlp crate
  • Migrate rlp crate to 2018 edition
  • Remove some test cases that depend on primitive-types (These test cases should be added to primitive-types instead of rlp)

koushiro added 4 commits May 27, 2019 14:22
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
@koushiro koushiro changed the title Support no_std for rlp crate [rlp] Add no_std support May 27, 2019
Copy link
Copy Markdown
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

In general it looks good – we definitely want to move to the 2018 edition and no_std capability is always welcome. I don't understand why tests&benchmarks need to be no_std though, can you elaborate on that?

Comment thread rlp/Cargo.toml Outdated
rustc-hex = {version = "2.0", default-features = false }

[dev-dependencies]
hex-literal = "0.1"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why prefer rustc-hex to hex-literal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think rustc-hex is enough to encode/decode hex. There is no need to introduce more dependencies, even if it is just dev-dependencies.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tests need to be as readable as possible, which is why I thinkhex-literal is better.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(The new 0.2 of hex-literal is edition compatible too!)

Comment thread rlp/benches/rlp.rs
let mut stream = RlpStream::new_list(1000);
for _ in 0..1000 {
stream.append(&U256::from(1));
stream.append(&1u64);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This changes the amount of data tested, making it impossible to compare tests before/after this change.

In general I'm not sure why the dev dependencies can't depend on primitive-types (or any other code, be it no_std or not). I think the benchmark use the big uint types because it is such a common use case for the crate. What's the point of having the benchmarks be no_std?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use the same data length as before here.

Comment thread rlp/src/rlpin.rs Outdated
@koushiro
Copy link
Copy Markdown
Contributor Author

@dvdplm Thanks for reviewing my code!

I removed the primitive-types dependency because H160 and U256 are custom types in primitive-types crate. If we want to test their rlp codec, I think we should write tests in primitive-types instead of in rlp crate.

For example, if you define a structure that implements a Serialize/Deserialize trait and you want to know if the serialization and deserialization of the structure is as expected, you should write tests in the crate of the structure you defined, instead of submitting a PR to request the serde-rs maintainers to add a dev-dependencies to test your code.

@koushiro
Copy link
Copy Markdown
Contributor Author

I just make tests are no_std compatible, which is a good indication that the code is no problem in the no_std environment. As for the benchmarks of U256 type, I think it should also be written in primitive-types crate for the same reason above.

Copy link
Copy Markdown
Contributor

@sorpaas sorpaas left a comment

Choose a reason for hiding this comment

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

@koushiro Would you mind to move the removed tests to primitve-types crate? This PR looks good to me but in general I think it would be a good idea not to remove working tests.

@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented May 28, 2019

@koushiro no, thank you for contributing! :)

So the context for my hesitation about moving the tests is that we've seen several instances of good PRs come in that look perfectly justified and that seem like an easy win and so they are merged but then when someone actually tries to use the new versions in production code, issues pop up. Most of the time it's ergonomic quirks but sometimes in the past we've merged code that turned out to be impossible to use higher up in the stack. I don't actually know what the best way to tackle this is – we can hardly expect all contributor to go through the hassle of upgrading depending crates/projects. The tests you remove here – and I totally understand your reasoning – makes me wonder if we're not removing one barrier to such unfortunate merges: the tests exercise a portion of the stack above itself so we know that part is sort of ok.

The obvious objection is that these are unit tests, not integration tests. And of course you and @sorpaas are right: these tests logically belong in primitive-types.

Maybe we can meet half-way:

  1. revert the test removals here and keep only the no_std parts
  2. in a separate PR, move the unit tests to where they belong and at the same time add a doc-test here that proves that rlp + primitive-types still work

Would that work for you?

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
@koushiro
Copy link
Copy Markdown
Contributor Author

@dvdplm @sorpaas I will add more tests (include removed tests in this PR) into primitive-types in another PR. PTAL again.

Comment thread rlp/benches/rlp.rs
let mut stream = RlpStream::new_list(1000);
for _ in 0..1000 {
stream.append(&U256::from(1));
stream.append(&1u64);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use the same data length as before here.

Comment thread rlp/benches/rlp.rs
let rlp = Rlp::new(&data);
for i in 0..1000 {
let _: U256 = rlp.val_at(i).unwrap();
let _: u64 = rlp.val_at(i).unwrap();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please use the same data length as before here.

Comment thread rlp/src/impls.rs Outdated
use byteorder::{ByteOrder, BigEndian};
use crate::traits::{Encodable, Decodable};

use crate::error::DecoderError;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider using

use crate::{
  …
  …
}

Comment thread rlp/src/rlpin.rs
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
koushiro added 2 commits May 31, 2019 16:47
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented Jun 26, 2019

I will add more tests (include removed tests in this PR) into primitive-types in another PR

Let us know when this other PR is ready so we can unblock this one. :)

@dvdplm
Copy link
Copy Markdown
Contributor

dvdplm commented Aug 12, 2019

@koushiro I'll close this for now. Feel free to re-open at any time with the last changes requested.

@dvdplm dvdplm closed this Aug 12, 2019
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.

3 participants