Unify primitive types for Parity Ethereum and Substrate#86
Conversation
f9c9a23 to
7989816
Compare
|
LGTM! Couldn't even find a minor grumble. |
dvdplm
left a comment
There was a problem hiding this comment.
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.
| name = "impl-codec" | ||
| version = "0.1.1" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| license = "Apache-2.0" |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
| name = "impl-rlp" | ||
| version = "0.1.1" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| license = "Apache-2.0" |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
| name = "impl-serde" | ||
| version = "0.1.1" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| license = "Apache-2.0" |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
|
|
||
| [dependencies] | ||
| serde = "1.0" | ||
| rustc-hex = "2.0.1" |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
| } | ||
| } | ||
|
|
||
| impl From<H160> for H256 { |
There was a problem hiding this comment.
This should use impl_fixed_hash_conversions!(H256, H160) I think.
There was a problem hiding this comment.
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?).
There was a problem hiding this comment.
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.
| #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20); | ||
|
|
||
| #[cfg(feature = "std")] | ||
| impl From<u64> for H160 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This from impl is fairly short so I think it's okay to just be there.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
replace any
From<u64>by the explicit functionfrom_low_u64_b
I like this solution. Let's do that. :)
| /// 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.
This comment was marked as resolved.
Sorry, something went wrong.
| #[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.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Fixed. It shouldn't be std.
There was a problem hiding this comment.
And see #86 (comment). I think we should remove From<u64> defs.
| /// 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.
This comment was marked as resolved.
Sorry, something went wrong.
…into sp-primitive-types-init
|
@dvdplm Would you mind to give a final review? |
rel openethereum/parity-ethereum#9994
This puts five primitive types into a crate
primitive-types. They areU256,U512,H160,H256andH512. This allows us to use parity-ethereum crates on substrate, and vice versa.primitive-typesnow implements RLP serialization for the types defined. It's expected that later we removeethereumfeature fromrlpcrate.impl-serde,impl-codecandimpl-rlp. This allows code reuse when additional types with similar serialization function need to be defined. It also saves some complications of declaring dependencies.