Skip to content

Unify primitive types for Parity Ethereum and Substrate#86

Merged
sorpaas merged 31 commits intomasterfrom
sp-primitive-types-init
Jan 7, 2019
Merged

Unify primitive types for Parity Ethereum and Substrate#86
sorpaas merged 31 commits intomasterfrom
sp-primitive-types-init

Conversation

@sorpaas
Copy link
Copy Markdown
Contributor

@sorpaas sorpaas commented Dec 2, 2018

rel openethereum/parity-ethereum#9994

This puts five primitive types into a crate primitive-types. They are U256, U512, H160, H256 and H512. This allows us to use parity-ethereum crates on substrate, and vice versa.

  • primitive-types now implements RLP serialization for the types defined. It's expected that later we remove ethereum feature from rlp crate.
  • Three new helper crate are added -- impl-serde, impl-codec and impl-rlp. This allows code reuse when additional types with similar serialization function need to be defined. It also saves some complications of declaring dependencies.

@sorpaas sorpaas force-pushed the sp-primitive-types-init branch from f9c9a23 to 7989816 Compare December 2, 2018 16:20
@Robbepop
Copy link
Copy Markdown

Robbepop commented Dec 2, 2018

LGTM!

Couldn't even find a minor grumble.

@Robbepop Robbepop self-requested a review December 2, 2018 21:26
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.

Overall this looks good. I think the ergonomics would be improved a bit if we can move more code to uint/fixed-hash and leave only type construction in primitive-types.
I am not convinced of the impls/* name and I think we should consider moving those crates to a subfolder of primitive-types.

Comment thread impls/codec/Cargo.toml Outdated
name = "impl-codec"
version = "0.1.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"

This comment was marked as resolved.

Comment thread Cargo.toml Outdated
Comment thread impls/rlp/Cargo.toml Outdated
name = "impl-rlp"
version = "0.1.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"

This comment was marked as resolved.

Comment thread impls/serde/Cargo.toml Outdated
name = "impl-serde"
version = "0.1.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"

This comment was marked as resolved.

Comment thread impls/serde/Cargo.toml Outdated

[dependencies]
serde = "1.0"
rustc-hex = "2.0.1"

This comment was marked as resolved.

Comment thread primitive-types/src/lib.rs Outdated
}
}

impl From<H160> for H256 {
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 should use impl_fixed_hash_conversions!(H256, H160) I think.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

+1

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.

We don't have that macro now and I don't think it's a good idea -- converting H160 to H256 is a special operation (that has agreement on Substrate and Parity Ethereum). We cannot apply it directly to any other types (otherwise, we can't answer issues like why is it zero-padded on the front? why not on the end or repeat the bytes?).

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.

I meant this; it does a static assert on the type size and zero-pads in front. I agree this is arbitrary and not something we invented recently, but it is there and it's been used for a long time so I'd argue it's the "standard by convention" in our codebase. As long as we document the behaviour I can live with it.

The reason I'm being difficult here and elsewhere is that I'm taking the POV of a dev that is unfamiliar with the code structure and approaches the code base for the first time: I'm afraid they will grow frustrated with how code is spread out in different crates with a hard-to-grasp logic as to what stuff is found where. If at all possible I'd like logic to be in uint/fixed-hash.

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.

Ah right. Fixed!

Comment thread primitive-types/src/lib.rs Outdated
#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20);

#[cfg(feature = "std")]
impl From<u64> for H160 {
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.

@Robbepop maybe we should have this in uint?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is problematic since in uint you can also define uint that are smaller than u64 and thus making this conversion failing. However, From impls are guaranteed to never fail. So we would need to make this conversion lossy in order to guarantee that it never fails but I am not sure if this is explicitly wanted behaviour since this may introduce unwanted semantics.

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.

This from impl is fairly short so I think it's okay to just be there.

Copy link
Copy Markdown
Contributor Author

@sorpaas sorpaas Dec 4, 2018

Choose a reason for hiding this comment

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

Hmm I'm thinking -- maybe we should remove those From<u64> conversions? It looks like it's both used in parity-ethereum and substrate, but the issue with it is that it assumes big-endian encoding. To fix those in both codebase is trivial -- we just replace any From<u64> by the explicit function from_low_u64_be.

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.

replace any From<u64> by the explicit function from_low_u64_b

I like this solution. Let's do that. :)

Comment thread primitive-types/src/lib.rs Outdated
/// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size.
pub struct H256(32);
}
#[cfg(feature = "impl-serde")] impl_fixed_hash_serde!(H256, 32);

This comment was marked as resolved.

Comment thread primitive-types/src/lib.rs Outdated
#[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H256, 32);
#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H256, 32);

#[cfg(feature = "std")]

This comment was marked as resolved.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this is because of Self::from_low_u64_be which itself requires fixed-hash/byteorder support. As far as I can see this crate tries to simplify crate features so that there is only something like std left which is enabled by default. I can understand this decision, however, one should keep in mind that this makes it impossible to use this API without std.

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.

Fixed. It shouldn't be std.

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.

And see #86 (comment). I think we should remove From<u64> defs.

Comment thread primitive-types/src/lib.rs Outdated
/// Fixed-size uninterpreted hash type with 64 bytes (512 bits) size.
pub struct H512(64);
}
#[cfg(feature = "impl-serde")] impl_fixed_hash_serde!(H512, 64);

This comment was marked as resolved.

Comment thread impls/codec/src/lib.rs Outdated
@sorpaas
Copy link
Copy Markdown
Contributor Author

sorpaas commented Dec 21, 2018

@dvdplm Would you mind to give a final review?

@sorpaas sorpaas merged commit 6e04748 into master Jan 7, 2019
@sorpaas sorpaas deleted the sp-primitive-types-init branch January 7, 2019 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants