From 97c034d78f88fbfbd1d40392f0aa99340be1f218 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 16:36:20 +0800 Subject: [PATCH 01/30] Implement all uint features --- Cargo.toml | 3 +- primitive-types/Cargo.toml | 21 ++++++ primitive-types/src/lib.rs | 27 ++++++++ rlp/Cargo.toml | 2 +- rlp/src/impls.rs | 37 ----------- uint/codec/Cargo.toml | 14 ++++ uint/codec/src/lib.rs | 22 +++++++ uint/rlp/Cargo.toml | 10 +++ uint/rlp/src/lib.rs | 30 +++++++++ uint/serde/Cargo.toml | 11 ++++ uint/serde/src/lib.rs | 28 ++++++++ uint/serde/src/serialize.rs | 126 ++++++++++++++++++++++++++++++++++++ uint/src/lib.rs | 2 +- 13 files changed, 293 insertions(+), 40 deletions(-) create mode 100644 primitive-types/Cargo.toml create mode 100644 primitive-types/src/lib.rs create mode 100644 uint/codec/Cargo.toml create mode 100644 uint/codec/src/lib.rs create mode 100644 uint/rlp/Cargo.toml create mode 100644 uint/rlp/src/lib.rs create mode 100644 uint/serde/Cargo.toml create mode 100644 uint/serde/src/lib.rs create mode 100644 uint/serde/src/serialize.rs diff --git a/Cargo.toml b/Cargo.toml index a8796ce87..33d864075 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,5 +17,6 @@ members = [ "trace-time", "trie-standardmap", "triehash", - "uint" + "uint", + "primitive-types", ] diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml new file mode 100644 index 000000000..112350bce --- /dev/null +++ b/primitive-types/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "primitive-types" +version = "0.1.0" +authors = ["Parity Technologies "] +license = "MIT" +homepage = "https://github.com/paritytech/parity-common" +description = "Primitive types shared by Ethereum and Substrate" + +[dependencies] +fixed-hash = { version = "0.3", path = "../fixed-hash", default-features = false } +uint = { version = "0.5", path = "../uint", default-features = false } +uint-serde = { version = "0.1", path = "../uint/serde", optional = true } +uint-codec = { version = "0.1", path = "../uint/codec", optional = true } +uint-rlp = { version = "0.1", path = "../uint/rlp", optional = true } + +[features] +default = ["std"] +std = ["uint/std", "fixed-hash/std"] +serde = ["uint-serde"] +codec = ["uint-codec"] +rlp = ["uint-rlp"] diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs new file mode 100644 index 000000000..eeabbc973 --- /dev/null +++ b/primitive-types/src/lib.rs @@ -0,0 +1,27 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(all(feature = "uint-serde", not(feature = "std")))] +compile_error!("Feature \"uint-serde\" requires feature \"std\" to build."); + +#[macro_use] +extern crate uint; + +#[cfg(feature = "uint-serde")] +#[macro_use] +extern crate uint_serde; + +#[cfg(feature = "uint-codec")] +#[macro_use] +extern crate uint_codec; + +#[cfg(feature = "uint-rlp")] +#[macro_use] +extern crate uint_rlp; + +#[macro_use] +extern crate fixed_hash; + +construct_uint!(U256, 4); +#[cfg(feature = "uint-serde")] impl_uint_serde!(U256, 4); +#[cfg(feature = "uint-codec")] impl_uint_codec!(U256, 4); +#[cfg(feature = "uint-rlp")] impl_uint_rlp!(U256, 4); diff --git a/rlp/Cargo.toml b/rlp/Cargo.toml index d486847fd..20babfd54 100644 --- a/rlp/Cargo.toml +++ b/rlp/Cargo.toml @@ -9,7 +9,7 @@ authors = ["Parity Technologies "] [dependencies] byteorder = "1.0" ethereum-types = { version = "0.4", optional = true } -rustc-hex = {version = "2.0", default-features = false } +rustc-hex = { version = "2.0", default-features = false } [dev-dependencies] hex-literal = "0.1" diff --git a/rlp/src/impls.rs b/rlp/src/impls.rs index 4abc6d4e0..308a05224 100644 --- a/rlp/src/impls.rs +++ b/rlp/src/impls.rs @@ -209,37 +209,6 @@ mod ethereum_traits { } } - macro_rules! impl_encodable_for_uint { - ($name: ident, $size: expr) => { - impl Encodable for $name { - fn rlp_append(&self, s: &mut RlpStream) { - let leading_empty_bytes = $size - (self.bits() + 7) / 8; - let mut buffer = [0u8; $size]; - self.to_big_endian(&mut buffer); - s.encoder().encode_value(&buffer[leading_empty_bytes..]); - } - } - } - } - - macro_rules! impl_decodable_for_uint { - ($name: ident, $size: expr) => { - impl Decodable for $name { - fn decode(rlp: &Rlp) -> Result { - rlp.decoder().decode_value(|bytes| { - if !bytes.is_empty() && bytes[0] == 0 { - Err(DecoderError::RlpInvalidIndirection) - } else if bytes.len() <= $size { - Ok($name::from(bytes)) - } else { - Err(DecoderError::RlpIsTooBig) - } - }) - } - } - } - } - impl_encodable_for_hash!(H64); impl_encodable_for_hash!(H128); impl_encodable_for_hash!(H160); @@ -255,12 +224,6 @@ mod ethereum_traits { impl_decodable_for_hash!(H512, 64); impl_decodable_for_hash!(H520, 65); impl_decodable_for_hash!(Bloom, 256); - - impl_encodable_for_uint!(U256, 32); - impl_encodable_for_uint!(U128, 16); - - impl_decodable_for_uint!(U256, 32); - impl_decodable_for_uint!(U128, 16); } impl<'a> Encodable for &'a str { diff --git a/uint/codec/Cargo.toml b/uint/codec/Cargo.toml new file mode 100644 index 000000000..4a5d5c15b --- /dev/null +++ b/uint/codec/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "uint-codec" +version = "0.1.0" +authors = ["Parity Technologies "] +license = "MIT" +homepage = "https://github.com/paritytech/parity-common" +description = "Parity Codec serialization support for uint." + +[dependencies] +parity-codec = { version = "2.1", default-features = false } + +[features] +default = ["std"] +std = ["parity-codec/std"] diff --git a/uint/codec/src/lib.rs b/uint/codec/src/lib.rs new file mode 100644 index 000000000..34f9cfc53 --- /dev/null +++ b/uint/codec/src/lib.rs @@ -0,0 +1,22 @@ +#[doc(hidden)] +pub extern crate parity_codec as codec; + +#[macro_export] +macro_rules! impl_uint_codec { + ($name: ident, $len: expr) => { + impl $crate::codec::Encode for $name { + fn using_encoded R>(&self, f: F) -> R { + let mut bytes = [0u8; $len * 8]; + self.to_little_endian(&mut bytes); + bytes.using_encoded(f) + } + } + + impl $crate::codec::Decode for $name { + fn decode(input: &mut I) -> Option { + <[u8; $len * 8] as $crate::codec::Decode>::decode(input) + .map(|b| $name::from_little_endian(&b)) + } + } + } +} diff --git a/uint/rlp/Cargo.toml b/uint/rlp/Cargo.toml new file mode 100644 index 000000000..fa80b5846 --- /dev/null +++ b/uint/rlp/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "uint-rlp" +version = "0.1.0" +authors = ["Parity Technologies "] +license = "MIT" +homepage = "https://github.com/paritytech/parity-common" +description = "RLP serialization support for uint." + +[dependencies] +rlp = { version = "0.3", path = "../../rlp" } diff --git a/uint/rlp/src/lib.rs b/uint/rlp/src/lib.rs new file mode 100644 index 000000000..43027fbbf --- /dev/null +++ b/uint/rlp/src/lib.rs @@ -0,0 +1,30 @@ +#[doc(hidden)] +pub extern crate rlp; + +#[macro_export] +macro_rules! impl_uint_rlp { + ($name: ident, $size: expr) => { + impl $crate::rlp::Encodable for $name { + fn rlp_append(&self, s: &mut $crate::rlp::RlpStream) { + let leading_empty_bytes = $size - (self.bits() + 7) / 8; + let mut buffer = [0u8; $size]; + self.to_big_endian(&mut buffer); + s.encoder().encode_value(&buffer[leading_empty_bytes..]); + } + } + + impl $crate::rlp::Decodable for $name { + fn decode(rlp: &$crate::rlp::Rlp) -> Result { + rlp.decoder().decode_value(|bytes| { + if !bytes.is_empty() && bytes[0] == 0 { + Err($crate::rlp::DecoderError::RlpInvalidIndirection) + } else if bytes.len() <= $size { + Ok($name::from(bytes)) + } else { + Err($crate::rlp::DecoderError::RlpIsTooBig) + } + }) + } + } + } +} diff --git a/uint/serde/Cargo.toml b/uint/serde/Cargo.toml new file mode 100644 index 000000000..dc10dc855 --- /dev/null +++ b/uint/serde/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "uint-serde" +version = "0.1.0" +authors = ["Parity Technologies "] +license = "MIT" +homepage = "https://github.com/paritytech/parity-common" +description = "Serde serialization support for uint." + +[dependencies] +serde = "1.0" +rustc-hex = "2.0.1" diff --git a/uint/serde/src/lib.rs b/uint/serde/src/lib.rs new file mode 100644 index 000000000..ae552a88e --- /dev/null +++ b/uint/serde/src/lib.rs @@ -0,0 +1,28 @@ +#[doc(hidden)] +pub extern crate serde; + +#[doc(hidden)] +pub extern crate rustc_hex; + +#[doc(hidden)] +pub mod serialize; + +#[macro_export] +macro_rules! impl_uint_serde { + ($name: ident, $len: expr) => { + impl $crate::serde::Serialize for $name { + fn serialize(&self, serializer: S) -> Result where S: $crate::serde::Serializer { + let mut bytes = [0u8; $len * 8]; + self.to_big_endian(&mut bytes); + $crate::serialize::serialize_uint(&bytes, serializer) + } + } + + impl<'de> $crate::serde::Deserialize<'de> for $name { + fn deserialize(deserializer: D) -> Result where D: $crate::serde::Deserializer<'de> { + $crate::serialize::deserialize_check_len(deserializer, $crate::serialize::ExpectedLen::Between(0, $len * 8)) + .map(|x| (&*x).into()) + } + } + } +} diff --git a/uint/serde/src/serialize.rs b/uint/serde/src/serialize.rs new file mode 100644 index 000000000..422f5ca1d --- /dev/null +++ b/uint/serde/src/serialize.rs @@ -0,0 +1,126 @@ +use std::fmt; + +use serde::{de, Serializer, Deserializer}; + +/// Serializes a slice of bytes. +pub fn serialize(bytes: &[u8], serializer: S) -> Result where + S: Serializer, +{ + let hex: String = ::rustc_hex::ToHex::to_hex(bytes); + serializer.serialize_str(&format!("0x{}", hex)) +} + +/// Serialize a slice of bytes as uint. +/// +/// The representation will have all leading zeros trimmed. +pub fn serialize_uint(bytes: &[u8], serializer: S) -> Result where + S: Serializer, +{ + let non_zero = bytes.iter().take_while(|b| **b == 0).count(); + let bytes = &bytes[non_zero..]; + if bytes.is_empty() { + return serializer.serialize_str("0x0"); + } + + let hex: String = ::rustc_hex::ToHex::to_hex(bytes); + let has_leading_zero = !hex.is_empty() && &hex[0..1] == "0"; + serializer.serialize_str( + &format!("0x{}", if has_leading_zero { &hex[1..] } else { &hex }) + ) +} + +/// Expected length of bytes vector. +#[derive(PartialEq, Eq)] +#[cfg_attr(feature = "std", derive(Debug))] +pub enum ExpectedLen { + /// Any length in bytes. + #[cfg_attr(not(feature = "std"), allow(unused))] + Any, + /// Exact length in bytes. + Exact(usize), + /// A bytes length between (min; max]. + Between(usize, usize), +} + +impl fmt::Display for ExpectedLen { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match *self { + ExpectedLen::Any => write!(fmt, "even length"), + ExpectedLen::Exact(v) => write!(fmt, "length of {}", v * 2), + ExpectedLen::Between(min, max) => write!(fmt, "length between ({}; {}]", min * 2, max * 2), + } + } +} + +/// Deserialize into vector of bytes. +#[cfg(feature = "std")] +pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where + D: Deserializer<'de>, +{ + deserialize_check_len(deserializer, ExpectedLen::Any) +} + +/// Deserialize into vector of bytes with additional size check. +pub fn deserialize_check_len<'de, D>(deserializer: D, len: ExpectedLen) -> Result, D::Error> where + D: Deserializer<'de>, +{ + struct Visitor { + len: ExpectedLen, + } + + impl<'a> de::Visitor<'a> for Visitor { + type Value = Vec; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a 0x-prefixed hex string with {}", self.len) + } + + fn visit_str(self, v: &str) -> Result { + if v.len() < 2 || &v[0..2] != "0x" { + return Err(E::custom("prefix is missing")) + } + + let is_len_valid = match self.len { + // just make sure that we have all nibbles + ExpectedLen::Any => v.len() % 2 == 0, + ExpectedLen::Exact(len) => v.len() == 2 * len + 2, + ExpectedLen::Between(min, max) => v.len() <= 2 * max + 2 && v.len() > 2 * min + 2, + }; + + if !is_len_valid { + return Err(E::invalid_length(v.len() - 2, &self)) + } + + let bytes = match self.len { + ExpectedLen::Between(..) if v.len() % 2 != 0 => { + ::rustc_hex::FromHex::from_hex(&*format!("0{}", &v[2..])) + }, + _ => ::rustc_hex::FromHex::from_hex(&v[2..]) + }; + + #[cfg(feature = "std")] + fn format_err(e: ::rustc_hex::FromHexError) -> String { + format!("invalid hex value: {:?}", e) + } + + #[cfg(not(feature = "std"))] + fn format_err(e: ::rustc_hex::FromHexError) -> String { + match e { + ::rustc_hex::InvalidHexLength => format!("invalid hex value: invalid length"), + ::rustc_hex::InvalidHexCharacter(c, p) => + format!("invalid hex value: invalid character {} at position {}", c, p), + } + } + + bytes.map_err(|e| E::custom(format_err(e))) + } + + #[cfg(feature = "std")] + fn visit_string(self, v: String) -> Result { + self.visit_str(&v) + } + } + // TODO [ToDr] Use raw bytes if we switch to RLP / binencoding + // (visit_bytes, visit_bytes_buf) + deserializer.deserialize_str(Visitor { len }) +} diff --git a/uint/src/lib.rs b/uint/src/lib.rs index d41f55c96..321ccaf24 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -29,8 +29,8 @@ pub extern crate rustc_hex; #[doc(hidden)] pub extern crate quickcheck; -#[macro_use] extern crate crunchy; +pub use crunchy::unroll; #[macro_use] mod uint; From b2423235e13bbb68150d3af8aa5fb64d0146e733 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 17:24:26 +0800 Subject: [PATCH 02/30] Improve uint defs and finish fixed hash impls --- Cargo.toml | 3 ++ {uint => impls}/codec/Cargo.toml | 4 +- {uint => impls}/codec/src/lib.rs | 16 ++++++++ {uint => impls}/rlp/Cargo.toml | 4 +- {uint => impls}/rlp/src/lib.rs | 28 +++++++++++++ {uint => impls}/serde/Cargo.toml | 4 +- {uint => impls}/serde/src/lib.rs | 18 ++++++++ {uint => impls}/serde/src/serialize.rs | 0 primitive-types/Cargo.toml | 12 +++--- primitive-types/src/lib.rs | 57 ++++++++++++++++++++------ rlp/Cargo.toml | 2 +- rlp/src/impls.rs | 37 +++++++++++++++++ uint/src/lib.rs | 13 +++++- uint/src/uint.rs | 9 ++-- 14 files changed, 174 insertions(+), 33 deletions(-) rename {uint => impls}/codec/Cargo.toml (74%) rename {uint => impls}/codec/src/lib.rs (58%) rename {uint => impls}/rlp/Cargo.toml (71%) rename {uint => impls}/rlp/src/lib.rs (50%) rename {uint => impls}/serde/Cargo.toml (69%) rename {uint => impls}/serde/src/lib.rs (56%) rename {uint => impls}/serde/src/serialize.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 33d864075..d5d4aff25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,5 +18,8 @@ members = [ "trie-standardmap", "triehash", "uint", + "impls/codec", + "impls/rlp", + "impls/serde", "primitive-types", ] diff --git a/uint/codec/Cargo.toml b/impls/codec/Cargo.toml similarity index 74% rename from uint/codec/Cargo.toml rename to impls/codec/Cargo.toml index 4a5d5c15b..1e13c20c0 100644 --- a/uint/codec/Cargo.toml +++ b/impls/codec/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "uint-codec" +name = "impl-codec" version = "0.1.0" authors = ["Parity Technologies "] license = "MIT" homepage = "https://github.com/paritytech/parity-common" -description = "Parity Codec serialization support for uint." +description = "Parity Codec serialization support for uint and fixed hash." [dependencies] parity-codec = { version = "2.1", default-features = false } diff --git a/uint/codec/src/lib.rs b/impls/codec/src/lib.rs similarity index 58% rename from uint/codec/src/lib.rs rename to impls/codec/src/lib.rs index 34f9cfc53..24108e63f 100644 --- a/uint/codec/src/lib.rs +++ b/impls/codec/src/lib.rs @@ -20,3 +20,19 @@ macro_rules! impl_uint_codec { } } } + +#[macro_export] +macro_rules! impl_fixed_hash_codec { + ($name: ident, $len: expr) => { + impl $crate::codec::Encode for $name { + fn using_encoded R>(&self, f: F) -> R { + self.0.using_encoded(f) + } + } + impl $crate::codec::Decode for $name { + fn decode(input: &mut I) -> Option { + <[u8; $len] as $crate::codec::Decode>::decode(input).map($name) + } + } + } +} diff --git a/uint/rlp/Cargo.toml b/impls/rlp/Cargo.toml similarity index 71% rename from uint/rlp/Cargo.toml rename to impls/rlp/Cargo.toml index fa80b5846..a42762ad6 100644 --- a/uint/rlp/Cargo.toml +++ b/impls/rlp/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "uint-rlp" +name = "impl-rlp" version = "0.1.0" authors = ["Parity Technologies "] license = "MIT" homepage = "https://github.com/paritytech/parity-common" -description = "RLP serialization support for uint." +description = "RLP serialization support for uint and fixed hash." [dependencies] rlp = { version = "0.3", path = "../../rlp" } diff --git a/uint/rlp/src/lib.rs b/impls/rlp/src/lib.rs similarity index 50% rename from uint/rlp/src/lib.rs rename to impls/rlp/src/lib.rs index 43027fbbf..779350b7b 100644 --- a/uint/rlp/src/lib.rs +++ b/impls/rlp/src/lib.rs @@ -1,6 +1,9 @@ #[doc(hidden)] pub extern crate rlp; +#[doc(hidden)] +pub extern crate core as core_; + #[macro_export] macro_rules! impl_uint_rlp { ($name: ident, $size: expr) => { @@ -28,3 +31,28 @@ macro_rules! impl_uint_rlp { } } } + +#[macro_export] +macro_rules! impl_fixed_hash_rlp { + ($name: ident, $size: expr) => { + impl $crate::rlp::Encodable for $name { + fn rlp_append(&self, s: &mut $crate::rlp::RlpStream) { + s.encoder().encode_value(self.as_ref()); + } + } + + impl $crate::rlp::Decodable for $name { + fn decode(rlp: &$crate::rlp::Rlp) -> Result { + rlp.decoder().decode_value(|bytes| match bytes.len().cmp(&$size) { + $crate::core_::cmp::Ordering::Less => Err($crate::rlp::DecoderError::RlpIsTooShort), + $crate::core_::cmp::Ordering::Greater => Err($crate::rlp::DecoderError::RlpIsTooBig), + $crate::core_::cmp::Ordering::Equal => { + let mut t = [0u8; $size]; + t.copy_from_slice(bytes); + Ok($name(t)) + } + }) + } + } + } +} diff --git a/uint/serde/Cargo.toml b/impls/serde/Cargo.toml similarity index 69% rename from uint/serde/Cargo.toml rename to impls/serde/Cargo.toml index dc10dc855..24bb3525b 100644 --- a/uint/serde/Cargo.toml +++ b/impls/serde/Cargo.toml @@ -1,10 +1,10 @@ [package] -name = "uint-serde" +name = "impl-serde" version = "0.1.0" authors = ["Parity Technologies "] license = "MIT" homepage = "https://github.com/paritytech/parity-common" -description = "Serde serialization support for uint." +description = "Serde serialization support for uint and fixed hash." [dependencies] serde = "1.0" diff --git a/uint/serde/src/lib.rs b/impls/serde/src/lib.rs similarity index 56% rename from uint/serde/src/lib.rs rename to impls/serde/src/lib.rs index ae552a88e..c70812066 100644 --- a/uint/serde/src/lib.rs +++ b/impls/serde/src/lib.rs @@ -26,3 +26,21 @@ macro_rules! impl_uint_serde { } } } + +#[macro_export] +macro_rules! impl_fixed_hash_serde { + ($name: ident, $len: expr) => { + impl $crate::serde::Serialize for $name { + fn serialize(&self, serializer: S) -> Result where S: $crate::serde::Serializer { + $crate::serialize::serialize(&self.0, serializer) + } + } + + impl<'de> $crate::serde::Deserialize<'de> for $name { + fn deserialize(deserializer: D) -> Result where D: $crate::serde::Deserializer<'de> { + $crate::serialize::deserialize_check_len(deserializer, $crate::serialize::ExpectedLen::Exact($len)) + .map(|x| $name::from_slice(&x)) + } + } + } +} diff --git a/uint/serde/src/serialize.rs b/impls/serde/src/serialize.rs similarity index 100% rename from uint/serde/src/serialize.rs rename to impls/serde/src/serialize.rs diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 112350bce..0f9a438d3 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -9,13 +9,13 @@ description = "Primitive types shared by Ethereum and Substrate" [dependencies] fixed-hash = { version = "0.3", path = "../fixed-hash", default-features = false } uint = { version = "0.5", path = "../uint", default-features = false } -uint-serde = { version = "0.1", path = "../uint/serde", optional = true } -uint-codec = { version = "0.1", path = "../uint/codec", optional = true } -uint-rlp = { version = "0.1", path = "../uint/rlp", optional = true } +impl-serde = { version = "0.1", path = "../impls/serde", optional = true } +impl-codec = { version = "0.1", path = "../impls/codec", optional = true } +impl-rlp = { version = "0.1", path = "../impls/rlp", optional = true } [features] default = ["std"] std = ["uint/std", "fixed-hash/std"] -serde = ["uint-serde"] -codec = ["uint-codec"] -rlp = ["uint-rlp"] +serde = ["impl-serde"] +codec = ["impl-codec"] +rlp = ["impl-rlp"] diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index eeabbc973..b14404140 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -1,27 +1,58 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(all(feature = "uint-serde", not(feature = "std")))] -compile_error!("Feature \"uint-serde\" requires feature \"std\" to build."); +#[cfg(all(feature = "impl-serde", not(feature = "std")))] +compile_error!("Feature \"impl-serde\" requires feature \"std\" to build."); + +#[cfg(all(feature = "impl-rlp", not(feature = "std")))] +compile_error!("Feature \"impl-rlp\" requires feature \"std\" to build."); #[macro_use] extern crate uint; -#[cfg(feature = "uint-serde")] #[macro_use] -extern crate uint_serde; +extern crate fixed_hash; -#[cfg(feature = "uint-codec")] +#[cfg(feature = "impl-serde")] #[macro_use] -extern crate uint_codec; +extern crate impl_serde; -#[cfg(feature = "uint-rlp")] +#[cfg(feature = "impl-codec")] #[macro_use] -extern crate uint_rlp; +extern crate impl_codec; +#[cfg(feature = "impl-rlp")] #[macro_use] -extern crate fixed_hash; +extern crate impl_rlp; + +construct_uint! { + /// Little-endian 256-bit integer type. + #[derive(Copy, Clone, Eq, PartialEq, Hash)] + pub struct U256(4); +} +#[cfg(feature = "impl-serde")] impl_uint_serde!(U256, 4); +#[cfg(feature = "impl-codec")] impl_uint_codec!(U256, 4); +#[cfg(feature = "impl-rlp")] impl_uint_rlp!(U256, 4); + +construct_fixed_hash!{ + /// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size. + pub struct H160(20); +} +#[cfg(feature = "impl-serde")] impl_fixed_hash_serde!(H160, 20); +#[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H160, 20); +#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20); + +construct_fixed_hash!{ + /// 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); +#[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H256, 32); +#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H256, 32); -construct_uint!(U256, 4); -#[cfg(feature = "uint-serde")] impl_uint_serde!(U256, 4); -#[cfg(feature = "uint-codec")] impl_uint_codec!(U256, 4); -#[cfg(feature = "uint-rlp")] impl_uint_rlp!(U256, 4); +construct_fixed_hash!{ + /// 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); +#[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H512, 64); +#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H512, 64); diff --git a/rlp/Cargo.toml b/rlp/Cargo.toml index 20babfd54..d486847fd 100644 --- a/rlp/Cargo.toml +++ b/rlp/Cargo.toml @@ -9,7 +9,7 @@ authors = ["Parity Technologies "] [dependencies] byteorder = "1.0" ethereum-types = { version = "0.4", optional = true } -rustc-hex = { version = "2.0", default-features = false } +rustc-hex = {version = "2.0", default-features = false } [dev-dependencies] hex-literal = "0.1" diff --git a/rlp/src/impls.rs b/rlp/src/impls.rs index 308a05224..4abc6d4e0 100644 --- a/rlp/src/impls.rs +++ b/rlp/src/impls.rs @@ -209,6 +209,37 @@ mod ethereum_traits { } } + macro_rules! impl_encodable_for_uint { + ($name: ident, $size: expr) => { + impl Encodable for $name { + fn rlp_append(&self, s: &mut RlpStream) { + let leading_empty_bytes = $size - (self.bits() + 7) / 8; + let mut buffer = [0u8; $size]; + self.to_big_endian(&mut buffer); + s.encoder().encode_value(&buffer[leading_empty_bytes..]); + } + } + } + } + + macro_rules! impl_decodable_for_uint { + ($name: ident, $size: expr) => { + impl Decodable for $name { + fn decode(rlp: &Rlp) -> Result { + rlp.decoder().decode_value(|bytes| { + if !bytes.is_empty() && bytes[0] == 0 { + Err(DecoderError::RlpInvalidIndirection) + } else if bytes.len() <= $size { + Ok($name::from(bytes)) + } else { + Err(DecoderError::RlpIsTooBig) + } + }) + } + } + } + } + impl_encodable_for_hash!(H64); impl_encodable_for_hash!(H128); impl_encodable_for_hash!(H160); @@ -224,6 +255,12 @@ mod ethereum_traits { impl_decodable_for_hash!(H512, 64); impl_decodable_for_hash!(H520, 65); impl_decodable_for_hash!(Bloom, 256); + + impl_encodable_for_uint!(U256, 32); + impl_encodable_for_uint!(U128, 16); + + impl_decodable_for_uint!(U256, 32); + impl_decodable_for_uint!(U128, 16); } impl<'a> Encodable for &'a str { diff --git a/uint/src/lib.rs b/uint/src/lib.rs index 321ccaf24..c51ac7dcf 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -38,8 +38,17 @@ pub use uint::*; #[cfg(feature = "common")] mod common { - construct_uint!(U256, 4); - construct_uint!(U512, 8); + construct_uint! { + /// Little-endian 256-bit integer type. + #[derive(Copy, Clone, Eq, PartialEq, Hash)] + pub struct U256(4); + } + + construct_uint! { + /// Little-endian 512-bit integer type. + #[derive(Copy, Clone, Eq, PartialEq, Hash)] + pub struct U512(8); + } #[doc(hidden)] impl U256 { diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 6f1d68254..79a63ae0e 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -346,11 +346,10 @@ pub fn split_u128(a: u128) -> (u64, u64) { #[macro_export] macro_rules! construct_uint { - ($name:ident, $n_words: tt) => ( - /// Little-endian large integer type + ( $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => { #[repr(C)] - #[derive(Copy, Clone, Eq, PartialEq, Hash)] - pub struct $name(pub [u64; $n_words]); + $(#[$attr])* + $visibility struct $name ([u64; $n_words]); impl AsRef<$name> for $name { fn as_ref(&self) -> &$name { @@ -1271,7 +1270,7 @@ macro_rules! construct_uint { // `$n_words * 8` because macro expects bytes and // uints use 64 bit (8 byte) words impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8)); - ); + } } #[cfg(feature = "std")] From 84b1856ffd1df93a569dbcfb7f48bee2c9c7c5da Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 17:25:46 +0800 Subject: [PATCH 03/30] Add license header to everything --- impls/codec/src/lib.rs | 8 ++++++++ impls/rlp/src/lib.rs | 8 ++++++++ impls/serde/src/lib.rs | 8 ++++++++ impls/serde/src/serialize.rs | 8 ++++++++ primitive-types/src/lib.rs | 8 ++++++++ 5 files changed, 40 insertions(+) diff --git a/impls/codec/src/lib.rs b/impls/codec/src/lib.rs index 24108e63f..b5587befc 100644 --- a/impls/codec/src/lib.rs +++ b/impls/codec/src/lib.rs @@ -1,3 +1,11 @@ +// Copyright 2015-2017 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + #[doc(hidden)] pub extern crate parity_codec as codec; diff --git a/impls/rlp/src/lib.rs b/impls/rlp/src/lib.rs index 779350b7b..0c14e68f2 100644 --- a/impls/rlp/src/lib.rs +++ b/impls/rlp/src/lib.rs @@ -1,3 +1,11 @@ +// Copyright 2015-2017 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + #[doc(hidden)] pub extern crate rlp; diff --git a/impls/serde/src/lib.rs b/impls/serde/src/lib.rs index c70812066..0e47dbfc9 100644 --- a/impls/serde/src/lib.rs +++ b/impls/serde/src/lib.rs @@ -1,3 +1,11 @@ +// Copyright 2015-2017 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + #[doc(hidden)] pub extern crate serde; diff --git a/impls/serde/src/serialize.rs b/impls/serde/src/serialize.rs index 422f5ca1d..73b4c6e94 100644 --- a/impls/serde/src/serialize.rs +++ b/impls/serde/src/serialize.rs @@ -1,3 +1,11 @@ +// Copyright 2015-2017 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use std::fmt; use serde::{de, Serializer, Deserializer}; diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index b14404140..3ebbf9139 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -1,3 +1,11 @@ +// Copyright 2015-2017 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + #![cfg_attr(not(feature = "std"), no_std)] #[cfg(all(feature = "impl-serde", not(feature = "std")))] From 41aa1078f927440f6cea768ff98b1c7f120fc092 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 17:26:14 +0800 Subject: [PATCH 04/30] Fix new crate license field --- impls/codec/Cargo.toml | 2 +- impls/rlp/Cargo.toml | 2 +- impls/serde/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/impls/codec/Cargo.toml b/impls/codec/Cargo.toml index 1e13c20c0..7b0e342eb 100644 --- a/impls/codec/Cargo.toml +++ b/impls/codec/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-codec" version = "0.1.0" authors = ["Parity Technologies "] -license = "MIT" +license = "MIT/Apache-2" homepage = "https://github.com/paritytech/parity-common" description = "Parity Codec serialization support for uint and fixed hash." diff --git a/impls/rlp/Cargo.toml b/impls/rlp/Cargo.toml index a42762ad6..49e8d43c1 100644 --- a/impls/rlp/Cargo.toml +++ b/impls/rlp/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-rlp" version = "0.1.0" authors = ["Parity Technologies "] -license = "MIT" +license = "MIT/Apache-2" homepage = "https://github.com/paritytech/parity-common" description = "RLP serialization support for uint and fixed hash." diff --git a/impls/serde/Cargo.toml b/impls/serde/Cargo.toml index 24bb3525b..d76b8c2aa 100644 --- a/impls/serde/Cargo.toml +++ b/impls/serde/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-serde" version = "0.1.0" authors = ["Parity Technologies "] -license = "MIT" +license = "MIT/Apache-2" homepage = "https://github.com/paritytech/parity-common" description = "Serde serialization support for uint and fixed hash." From 96ee827b0b1fc04640fcabd2c73235a32f7fc760 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 17:29:33 +0800 Subject: [PATCH 05/30] Add docs --- impls/codec/src/lib.rs | 4 ++++ impls/rlp/src/lib.rs | 4 ++++ impls/serde/src/lib.rs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/impls/codec/src/lib.rs b/impls/codec/src/lib.rs index b5587befc..bd73b615b 100644 --- a/impls/codec/src/lib.rs +++ b/impls/codec/src/lib.rs @@ -6,9 +6,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +//! Parity Codec serialization support for uint and fixed hash. + #[doc(hidden)] pub extern crate parity_codec as codec; +/// Add Parity Codec serialization support to an integer created by `construct_uint!`. #[macro_export] macro_rules! impl_uint_codec { ($name: ident, $len: expr) => { @@ -29,6 +32,7 @@ macro_rules! impl_uint_codec { } } +/// Add Parity Codec serialization support to a fixed-sized hash type created by `construct_fixed_hash!`. #[macro_export] macro_rules! impl_fixed_hash_codec { ($name: ident, $len: expr) => { diff --git a/impls/rlp/src/lib.rs b/impls/rlp/src/lib.rs index 0c14e68f2..89380acf2 100644 --- a/impls/rlp/src/lib.rs +++ b/impls/rlp/src/lib.rs @@ -6,12 +6,15 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +//! RLP serialization support for uint and fixed hash. + #[doc(hidden)] pub extern crate rlp; #[doc(hidden)] pub extern crate core as core_; +/// Add RLP serialization support to an integer created by `construct_uint!`. #[macro_export] macro_rules! impl_uint_rlp { ($name: ident, $size: expr) => { @@ -40,6 +43,7 @@ macro_rules! impl_uint_rlp { } } +/// Add RLP serialization support to a fixed-sized hash type created by `construct_fixed_hash!`. #[macro_export] macro_rules! impl_fixed_hash_rlp { ($name: ident, $size: expr) => { diff --git a/impls/serde/src/lib.rs b/impls/serde/src/lib.rs index 0e47dbfc9..509f36c47 100644 --- a/impls/serde/src/lib.rs +++ b/impls/serde/src/lib.rs @@ -6,6 +6,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +//! Serde serialization support for uint and fixed hash. + #[doc(hidden)] pub extern crate serde; @@ -15,6 +17,7 @@ pub extern crate rustc_hex; #[doc(hidden)] pub mod serialize; +/// Add Serde serialization support to an integer created by `construct_uint!`. #[macro_export] macro_rules! impl_uint_serde { ($name: ident, $len: expr) => { @@ -35,6 +38,7 @@ macro_rules! impl_uint_serde { } } +/// Add Serde serialization support to a fixed-sized hash type created by `construct_fixed_hash!`. #[macro_export] macro_rules! impl_fixed_hash_serde { ($name: ident, $len: expr) => { From 84f56d8ec92a0146d956444971d0083653e73926 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 17:31:34 +0800 Subject: [PATCH 06/30] Move basic derive to macro --- primitive-types/src/lib.rs | 3 ++- uint/src/lib.rs | 2 -- uint/src/uint.rs | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 3ebbf9139..6f6138baa 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -6,6 +6,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +//! Primitive types shared by Substrate and Parity Ethereum. + #![cfg_attr(not(feature = "std"), no_std)] #[cfg(all(feature = "impl-serde", not(feature = "std")))] @@ -34,7 +36,6 @@ extern crate impl_rlp; construct_uint! { /// Little-endian 256-bit integer type. - #[derive(Copy, Clone, Eq, PartialEq, Hash)] pub struct U256(4); } #[cfg(feature = "impl-serde")] impl_uint_serde!(U256, 4); diff --git a/uint/src/lib.rs b/uint/src/lib.rs index c51ac7dcf..c18bebadd 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -40,13 +40,11 @@ pub use uint::*; mod common { construct_uint! { /// Little-endian 256-bit integer type. - #[derive(Copy, Clone, Eq, PartialEq, Hash)] pub struct U256(4); } construct_uint! { /// Little-endian 512-bit integer type. - #[derive(Copy, Clone, Eq, PartialEq, Hash)] pub struct U512(8); } diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 79a63ae0e..c8f780a70 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -349,6 +349,7 @@ macro_rules! construct_uint { ( $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => { #[repr(C)] $(#[$attr])* + #[derive(Copy, Clone, Eq, PartialEq, Hash)] $visibility struct $name ([u64; $n_words]); impl AsRef<$name> for $name { From 2eb6b4e534d4d4b77376bb242fc8e5312ea5ea2c Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 21:58:46 +0800 Subject: [PATCH 07/30] Revert uint convenience improvements so that dep PR works --- primitive-types/Cargo.toml | 3 ++- primitive-types/src/lib.rs | 32 +++++++++++++++++++++++++------- uint/src/lib.rs | 13 +++---------- uint/src/uint.rs | 8 ++++---- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 0f9a438d3..7fdb78f87 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -9,13 +9,14 @@ description = "Primitive types shared by Ethereum and Substrate" [dependencies] fixed-hash = { version = "0.3", path = "../fixed-hash", default-features = false } uint = { version = "0.5", path = "../uint", default-features = false } +crunchy = { version = "0.2", default-features = false } impl-serde = { version = "0.1", path = "../impls/serde", optional = true } impl-codec = { version = "0.1", path = "../impls/codec", optional = true } impl-rlp = { version = "0.1", path = "../impls/rlp", optional = true } [features] default = ["std"] -std = ["uint/std", "fixed-hash/std"] +std = ["uint/std", "fixed-hash/std", "crunchy/std"] serde = ["impl-serde"] codec = ["impl-codec"] rlp = ["impl-rlp"] diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 6f6138baa..4386331d4 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -22,6 +22,9 @@ extern crate uint; #[macro_use] extern crate fixed_hash; +#[macro_use] +extern crate crunchy; + #[cfg(feature = "impl-serde")] #[macro_use] extern crate impl_serde; @@ -34,15 +37,12 @@ extern crate impl_codec; #[macro_use] extern crate impl_rlp; -construct_uint! { - /// Little-endian 256-bit integer type. - pub struct U256(4); -} +construct_uint!(U256, 4); #[cfg(feature = "impl-serde")] impl_uint_serde!(U256, 4); #[cfg(feature = "impl-codec")] impl_uint_codec!(U256, 4); #[cfg(feature = "impl-rlp")] impl_uint_rlp!(U256, 4); -construct_fixed_hash!{ +construct_fixed_hash! { /// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size. pub struct H160(20); } @@ -50,7 +50,13 @@ construct_fixed_hash!{ #[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H160, 20); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20); -construct_fixed_hash!{ +impl From for H160 { + fn from(val: u64) -> Self { + Self::from_low_u64_be(val) + } +} + +construct_fixed_hash! { /// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size. pub struct H256(32); } @@ -58,10 +64,22 @@ construct_fixed_hash!{ #[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H256, 32); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H256, 32); -construct_fixed_hash!{ +impl From for H256 { + fn from(val: u64) -> Self { + Self::from_low_u64_be(val) + } +} + +construct_fixed_hash! { /// 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); #[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H512, 64); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H512, 64); + +impl From for H512 { + fn from(val: u64) -> Self { + Self::from_low_u64_be(val) + } +} diff --git a/uint/src/lib.rs b/uint/src/lib.rs index c18bebadd..d41f55c96 100644 --- a/uint/src/lib.rs +++ b/uint/src/lib.rs @@ -29,8 +29,8 @@ pub extern crate rustc_hex; #[doc(hidden)] pub extern crate quickcheck; +#[macro_use] extern crate crunchy; -pub use crunchy::unroll; #[macro_use] mod uint; @@ -38,15 +38,8 @@ pub use uint::*; #[cfg(feature = "common")] mod common { - construct_uint! { - /// Little-endian 256-bit integer type. - pub struct U256(4); - } - - construct_uint! { - /// Little-endian 512-bit integer type. - pub struct U512(8); - } + construct_uint!(U256, 4); + construct_uint!(U512, 8); #[doc(hidden)] impl U256 { diff --git a/uint/src/uint.rs b/uint/src/uint.rs index c8f780a70..6f1d68254 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -346,11 +346,11 @@ pub fn split_u128(a: u128) -> (u64, u64) { #[macro_export] macro_rules! construct_uint { - ( $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => { + ($name:ident, $n_words: tt) => ( + /// Little-endian large integer type #[repr(C)] - $(#[$attr])* #[derive(Copy, Clone, Eq, PartialEq, Hash)] - $visibility struct $name ([u64; $n_words]); + pub struct $name(pub [u64; $n_words]); impl AsRef<$name> for $name { fn as_ref(&self) -> &$name { @@ -1271,7 +1271,7 @@ macro_rules! construct_uint { // `$n_words * 8` because macro expects bytes and // uints use 64 bit (8 byte) words impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8)); - } + ); } #[cfg(feature = "std")] From 524363938f4fb97bf637c7dbc088fe71e8e19f88 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 22:02:43 +0800 Subject: [PATCH 08/30] Fix license of impls --- impls/codec/Cargo.toml | 2 +- impls/rlp/Cargo.toml | 2 +- impls/serde/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/impls/codec/Cargo.toml b/impls/codec/Cargo.toml index 7b0e342eb..4ee8ab126 100644 --- a/impls/codec/Cargo.toml +++ b/impls/codec/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-codec" version = "0.1.0" authors = ["Parity Technologies "] -license = "MIT/Apache-2" +license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" description = "Parity Codec serialization support for uint and fixed hash." diff --git a/impls/rlp/Cargo.toml b/impls/rlp/Cargo.toml index 49e8d43c1..7cfd34d02 100644 --- a/impls/rlp/Cargo.toml +++ b/impls/rlp/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-rlp" version = "0.1.0" authors = ["Parity Technologies "] -license = "MIT/Apache-2" +license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" description = "RLP serialization support for uint and fixed hash." diff --git a/impls/serde/Cargo.toml b/impls/serde/Cargo.toml index d76b8c2aa..c646ef941 100644 --- a/impls/serde/Cargo.toml +++ b/impls/serde/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-serde" version = "0.1.0" authors = ["Parity Technologies "] -license = "MIT/Apache-2" +license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" description = "Serde serialization support for uint and fixed hash." From e61e1c255d9c22f3ba1a4be804a357c4b7068744 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 22:03:05 +0800 Subject: [PATCH 09/30] Fix license of primitive-types --- primitive-types/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 7fdb78f87..476204d7b 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -2,7 +2,7 @@ name = "primitive-types" version = "0.1.0" authors = ["Parity Technologies "] -license = "MIT" +license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" description = "Primitive types shared by Ethereum and Substrate" From d93f59bcf896e6778bc41de4e790ae6b69c96872 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 23:21:02 +0800 Subject: [PATCH 10/30] Add U512 in primitive-types --- primitive-types/src/lib.rs | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 4386331d4..a08c92a10 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -42,6 +42,74 @@ construct_uint!(U256, 4); #[cfg(feature = "impl-codec")] impl_uint_codec!(U256, 4); #[cfg(feature = "impl-rlp")] impl_uint_rlp!(U256, 4); +impl U256 { + /// Multiplies two 256-bit integers to produce full 512-bit integer + /// No overflow possible + #[inline(always)] + pub fn full_mul(self, other: U256) -> U512 { + U512(uint_full_mul_reg!(U256, 4, self, other)) + } +} + +impl From for U512 { + fn from(value: U256) -> U512 { + let U256(ref arr) = value; + let mut ret = [0; 8]; + ret[0] = arr[0]; + ret[1] = arr[1]; + ret[2] = arr[2]; + ret[3] = arr[3]; + U512(ret) + } +} + +impl From for U256 { + fn from(value: U512) -> U256 { + let U512(ref arr) = value; + if arr[4] | arr[5] | arr[6] | arr[7] != 0 { + panic!("From for U256: encountered overflow") + } + let mut ret = [0; 4]; + ret[0] = arr[0]; + ret[1] = arr[1]; + ret[2] = arr[2]; + ret[3] = arr[3]; + U256(ret) + } +} + +impl<'a> From<&'a U256> for U512 { + fn from(value: &'a U256) -> U512 { + let U256(ref arr) = *value; + let mut ret = [0; 8]; + ret[0] = arr[0]; + ret[1] = arr[1]; + ret[2] = arr[2]; + ret[3] = arr[3]; + U512(ret) + } +} + +impl<'a> From<&'a U512> for U256 { + fn from(value: &'a U512) -> U256 { + let U512(ref arr) = *value; + if arr[4] | arr[5] | arr[6] | arr[7] != 0 { + panic!("From<&U512> for U256: encountered overflow") + } + let mut ret = [0; 4]; + ret[0] = arr[0]; + ret[1] = arr[1]; + ret[2] = arr[2]; + ret[3] = arr[3]; + U256(ret) + } +} + +construct_uint!(U512, 8); +#[cfg(feature = "impl-serde")] impl_uint_serde!(U512, 8); +#[cfg(feature = "impl-codec")] impl_uint_codec!(U512, 8); +#[cfg(feature = "impl-rlp")] impl_uint_rlp!(U512, 8); + construct_fixed_hash! { /// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size. pub struct H160(20); @@ -56,6 +124,22 @@ impl From for H160 { } } +impl From for H256 { + fn from(value: H160) -> H256 { + let mut ret = H256::zero(); + ret.0[12..32].copy_from_slice(value.as_bytes()); + ret + } +} + +impl<'a> From<&'a H160> for H256 { + fn from(value: &'a H160) -> H256 { + let mut ret = H256::zero(); + ret.0[12..32].copy_from_slice(value.as_bytes()); + ret + } +} + construct_fixed_hash! { /// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size. pub struct H256(32); From 765aeb3c4e43632ee5b7dc35eda108ab90e7023e Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 23:21:25 +0800 Subject: [PATCH 11/30] Release primitive-types 0.1.1 so things are not blocked here --- primitive-types/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 476204d7b..40dafc284 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "primitive-types" -version = "0.1.0" +version = "0.1.1" authors = ["Parity Technologies "] license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" From 52c6847a13dccc0c854a3a0cc7feae7c91e410ed Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 2 Dec 2018 23:43:00 +0800 Subject: [PATCH 12/30] Remove unused std feature --- impls/serde/Cargo.toml | 2 +- impls/serde/src/serialize.rs | 16 +--------------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/impls/serde/Cargo.toml b/impls/serde/Cargo.toml index c646ef941..22c1ce126 100644 --- a/impls/serde/Cargo.toml +++ b/impls/serde/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "impl-serde" -version = "0.1.0" +version = "0.1.1" authors = ["Parity Technologies "] license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" diff --git a/impls/serde/src/serialize.rs b/impls/serde/src/serialize.rs index 73b4c6e94..b3e3d95f2 100644 --- a/impls/serde/src/serialize.rs +++ b/impls/serde/src/serialize.rs @@ -38,11 +38,9 @@ pub fn serialize_uint(bytes: &[u8], serializer: S) -> Result } /// Expected length of bytes vector. -#[derive(PartialEq, Eq)] -#[cfg_attr(feature = "std", derive(Debug))] +#[derive(PartialEq, Eq, Debug)] pub enum ExpectedLen { /// Any length in bytes. - #[cfg_attr(not(feature = "std"), allow(unused))] Any, /// Exact length in bytes. Exact(usize), @@ -61,7 +59,6 @@ impl fmt::Display for ExpectedLen { } /// Deserialize into vector of bytes. -#[cfg(feature = "std")] pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { @@ -106,24 +103,13 @@ pub fn deserialize_check_len<'de, D>(deserializer: D, len: ExpectedLen) -> Resul _ => ::rustc_hex::FromHex::from_hex(&v[2..]) }; - #[cfg(feature = "std")] fn format_err(e: ::rustc_hex::FromHexError) -> String { format!("invalid hex value: {:?}", e) } - #[cfg(not(feature = "std"))] - fn format_err(e: ::rustc_hex::FromHexError) -> String { - match e { - ::rustc_hex::InvalidHexLength => format!("invalid hex value: invalid length"), - ::rustc_hex::InvalidHexCharacter(c, p) => - format!("invalid hex value: invalid character {} at position {}", c, p), - } - } - bytes.map_err(|e| E::custom(format_err(e))) } - #[cfg(feature = "std")] fn visit_string(self, v: String) -> Result { self.visit_str(&v) } From 7803bc45ae7f3aa85d20210af4b38f2add3e4cd1 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 3 Dec 2018 00:09:30 +0800 Subject: [PATCH 13/30] Fix nostd compile for primitive-types --- primitive-types/Cargo.toml | 2 +- primitive-types/src/lib.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 40dafc284..546fb56df 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "primitive-types" -version = "0.1.1" +version = "0.1.2" authors = ["Parity Technologies "] license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index a08c92a10..7ed04d34c 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -118,6 +118,7 @@ construct_fixed_hash! { #[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H160, 20); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20); +#[cfg(feature = "std")] impl From for H160 { fn from(val: u64) -> Self { Self::from_low_u64_be(val) @@ -148,6 +149,7 @@ construct_fixed_hash! { #[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H256, 32); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H256, 32); +#[cfg(feature = "std")] impl From for H256 { fn from(val: u64) -> Self { Self::from_low_u64_be(val) @@ -162,6 +164,7 @@ construct_fixed_hash! { #[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H512, 64); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H512, 64); +#[cfg(feature = "std")] impl From for H512 { fn from(val: u64) -> Self { Self::from_low_u64_be(val) From 79898165c4ef3444a3b00087839fbb63b1c63f39 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 3 Dec 2018 00:18:17 +0800 Subject: [PATCH 14/30] Disable std on impl-serde/codec/rlp crate --- primitive-types/Cargo.toml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 546fb56df..04aed77ab 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "primitive-types" -version = "0.1.2" +version = "0.1.3" authors = ["Parity Technologies "] license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" @@ -10,13 +10,13 @@ description = "Primitive types shared by Ethereum and Substrate" fixed-hash = { version = "0.3", path = "../fixed-hash", default-features = false } uint = { version = "0.5", path = "../uint", default-features = false } crunchy = { version = "0.2", default-features = false } -impl-serde = { version = "0.1", path = "../impls/serde", optional = true } -impl-codec = { version = "0.1", path = "../impls/codec", optional = true } -impl-rlp = { version = "0.1", path = "../impls/rlp", optional = true } +impl-serde = { version = "0.1", path = "../impls/serde", default-features = false, optional = true } +impl-codec = { version = "0.1", path = "../impls/codec", default-features = false, optional = true } +impl-rlp = { version = "0.1", path = "../impls/rlp", default-features = false, optional = true } [features] default = ["std"] -std = ["uint/std", "fixed-hash/std", "crunchy/std"] +std = ["uint/std", "fixed-hash/std", "crunchy/std", "impl-codec/std"] serde = ["impl-serde"] codec = ["impl-codec"] rlp = ["impl-rlp"] From 37ea789ca83a646e00595b97605b759bb6c1b32d Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 3 Dec 2018 00:24:55 +0800 Subject: [PATCH 15/30] Missed nostd attr in impl-codec --- impls/codec/Cargo.toml | 2 +- impls/codec/src/lib.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/impls/codec/Cargo.toml b/impls/codec/Cargo.toml index 4ee8ab126..69d3e3d4a 100644 --- a/impls/codec/Cargo.toml +++ b/impls/codec/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "impl-codec" -version = "0.1.0" +version = "0.1.1" authors = ["Parity Technologies "] license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" diff --git a/impls/codec/src/lib.rs b/impls/codec/src/lib.rs index bd73b615b..d7e7bfb60 100644 --- a/impls/codec/src/lib.rs +++ b/impls/codec/src/lib.rs @@ -8,6 +8,8 @@ //! Parity Codec serialization support for uint and fixed hash. +#![cfg_attr(not(feature = "std"), no_std)] + #[doc(hidden)] pub extern crate parity_codec as codec; From ee3f55d68cb6de7f09688078311d816f4a88ff02 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 3 Dec 2018 10:50:10 +0800 Subject: [PATCH 16/30] Remove rlp default features for impl-rlp --- impls/rlp/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/impls/rlp/Cargo.toml b/impls/rlp/Cargo.toml index 7cfd34d02..c49b44698 100644 --- a/impls/rlp/Cargo.toml +++ b/impls/rlp/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "impl-rlp" -version = "0.1.0" +version = "0.1.1" authors = ["Parity Technologies "] license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" description = "RLP serialization support for uint and fixed hash." [dependencies] -rlp = { version = "0.3", path = "../../rlp" } +rlp = { version = "0.3", path = "../../rlp", default-features = false } From 59d9672df718cf42316b4445c641aa977461c028 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 3 Dec 2018 11:02:24 +0800 Subject: [PATCH 17/30] Add all feature gates from fixed-hash and uint --- primitive-types/Cargo.toml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 04aed77ab..dc1654301 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "primitive-types" -version = "0.1.3" +version = "0.1.4" authors = ["Parity Technologies "] license = "Apache-2.0" homepage = "https://github.com/paritytech/parity-common" @@ -17,6 +17,10 @@ impl-rlp = { version = "0.1", path = "../impls/rlp", default-features = false, o [features] default = ["std"] std = ["uint/std", "fixed-hash/std", "crunchy/std", "impl-codec/std"] +heapsize = ["uint/heapsize", "fixed-hash/heapsize"] +byteorder = ["fixed-hash/byteorder"] +libc = ["fixed-hash/libc"] +rustc-hex = ["fixed-hash/rustc-hex"] serde = ["impl-serde"] codec = ["impl-codec"] rlp = ["impl-rlp"] From c264679463b755c0cd2ac052a308b45774404d67 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:33:11 +0800 Subject: [PATCH 18/30] Update license --- impls/codec/Cargo.toml | 2 +- impls/rlp/Cargo.toml | 2 +- impls/serde/Cargo.toml | 2 +- primitive-types/Cargo.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/impls/codec/Cargo.toml b/impls/codec/Cargo.toml index 69d3e3d4a..61044c41d 100644 --- a/impls/codec/Cargo.toml +++ b/impls/codec/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-codec" version = "0.1.1" authors = ["Parity Technologies "] -license = "Apache-2.0" +license = "Apache-2.0/MIT" homepage = "https://github.com/paritytech/parity-common" description = "Parity Codec serialization support for uint and fixed hash." diff --git a/impls/rlp/Cargo.toml b/impls/rlp/Cargo.toml index c49b44698..f3b44ef77 100644 --- a/impls/rlp/Cargo.toml +++ b/impls/rlp/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-rlp" version = "0.1.1" authors = ["Parity Technologies "] -license = "Apache-2.0" +license = "Apache-2.0/MIT" homepage = "https://github.com/paritytech/parity-common" description = "RLP serialization support for uint and fixed hash." diff --git a/impls/serde/Cargo.toml b/impls/serde/Cargo.toml index 22c1ce126..0ebe2c655 100644 --- a/impls/serde/Cargo.toml +++ b/impls/serde/Cargo.toml @@ -2,7 +2,7 @@ name = "impl-serde" version = "0.1.1" authors = ["Parity Technologies "] -license = "Apache-2.0" +license = "Apache-2.0/MIT" homepage = "https://github.com/paritytech/parity-common" description = "Serde serialization support for uint and fixed hash." diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index dc1654301..a1da0e917 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -2,7 +2,7 @@ name = "primitive-types" version = "0.1.4" authors = ["Parity Technologies "] -license = "Apache-2.0" +license = "Apache-2.0/MIT" homepage = "https://github.com/paritytech/parity-common" description = "Primitive types shared by Ethereum and Substrate" From 7714a2a58cf35e50e1002197a984dbca35836934 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:36:27 +0800 Subject: [PATCH 19/30] Move impls to primitive-types subfolder --- Cargo.toml | 3 --- primitive-types/Cargo.toml | 6 +++--- {impls => primitive-types/impls}/codec/Cargo.toml | 0 {impls => primitive-types/impls}/codec/src/lib.rs | 0 {impls => primitive-types/impls}/rlp/Cargo.toml | 0 {impls => primitive-types/impls}/rlp/src/lib.rs | 0 {impls => primitive-types/impls}/serde/Cargo.toml | 0 {impls => primitive-types/impls}/serde/src/lib.rs | 0 {impls => primitive-types/impls}/serde/src/serialize.rs | 0 9 files changed, 3 insertions(+), 6 deletions(-) rename {impls => primitive-types/impls}/codec/Cargo.toml (100%) rename {impls => primitive-types/impls}/codec/src/lib.rs (100%) rename {impls => primitive-types/impls}/rlp/Cargo.toml (100%) rename {impls => primitive-types/impls}/rlp/src/lib.rs (100%) rename {impls => primitive-types/impls}/serde/Cargo.toml (100%) rename {impls => primitive-types/impls}/serde/src/lib.rs (100%) rename {impls => primitive-types/impls}/serde/src/serialize.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index d5d4aff25..33d864075 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,8 +18,5 @@ members = [ "trie-standardmap", "triehash", "uint", - "impls/codec", - "impls/rlp", - "impls/serde", "primitive-types", ] diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index a1da0e917..39818cd09 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -10,9 +10,9 @@ description = "Primitive types shared by Ethereum and Substrate" fixed-hash = { version = "0.3", path = "../fixed-hash", default-features = false } uint = { version = "0.5", path = "../uint", default-features = false } crunchy = { version = "0.2", default-features = false } -impl-serde = { version = "0.1", path = "../impls/serde", default-features = false, optional = true } -impl-codec = { version = "0.1", path = "../impls/codec", default-features = false, optional = true } -impl-rlp = { version = "0.1", path = "../impls/rlp", default-features = false, optional = true } +impl-serde = { version = "0.1", path = "impls/serde", default-features = false, optional = true } +impl-codec = { version = "0.1", path = "impls/codec", default-features = false, optional = true } +impl-rlp = { version = "0.1", path = "impls/rlp", default-features = false, optional = true } [features] default = ["std"] diff --git a/impls/codec/Cargo.toml b/primitive-types/impls/codec/Cargo.toml similarity index 100% rename from impls/codec/Cargo.toml rename to primitive-types/impls/codec/Cargo.toml diff --git a/impls/codec/src/lib.rs b/primitive-types/impls/codec/src/lib.rs similarity index 100% rename from impls/codec/src/lib.rs rename to primitive-types/impls/codec/src/lib.rs diff --git a/impls/rlp/Cargo.toml b/primitive-types/impls/rlp/Cargo.toml similarity index 100% rename from impls/rlp/Cargo.toml rename to primitive-types/impls/rlp/Cargo.toml diff --git a/impls/rlp/src/lib.rs b/primitive-types/impls/rlp/src/lib.rs similarity index 100% rename from impls/rlp/src/lib.rs rename to primitive-types/impls/rlp/src/lib.rs diff --git a/impls/serde/Cargo.toml b/primitive-types/impls/serde/Cargo.toml similarity index 100% rename from impls/serde/Cargo.toml rename to primitive-types/impls/serde/Cargo.toml diff --git a/impls/serde/src/lib.rs b/primitive-types/impls/serde/src/lib.rs similarity index 100% rename from impls/serde/src/lib.rs rename to primitive-types/impls/serde/src/lib.rs diff --git a/impls/serde/src/serialize.rs b/primitive-types/impls/serde/src/serialize.rs similarity index 100% rename from impls/serde/src/serialize.rs rename to primitive-types/impls/serde/src/serialize.rs From 5bbb03f17bd36aa7e52684e3e4be749874801c1b Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:38:06 +0800 Subject: [PATCH 20/30] typo: 2.0.1 -> 2 --- primitive-types/impls/serde/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitive-types/impls/serde/Cargo.toml b/primitive-types/impls/serde/Cargo.toml index 0ebe2c655..25ce8500d 100644 --- a/primitive-types/impls/serde/Cargo.toml +++ b/primitive-types/impls/serde/Cargo.toml @@ -8,4 +8,4 @@ description = "Serde serialization support for uint and fixed hash." [dependencies] serde = "1.0" -rustc-hex = "2.0.1" +rustc-hex = "2" From 9f2269132b7a6d2a677ac9090f9c1e29b85404d6 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:41:58 +0800 Subject: [PATCH 21/30] Expand primitive types docs --- primitive-types/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 7ed04d34c..c7279be02 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -7,6 +7,10 @@ // except according to those terms. //! Primitive types shared by Substrate and Parity Ethereum. +//! +//! Those are uint types `U256` and `U512`, and fixed hash types `H160`, +//! `H256` and `H512`, with optional serde serialization, parity-codec and +//! rlp encoding. #![cfg_attr(not(feature = "std"), no_std)] From cfc2a5d10b03a5dc7349d9ebe1ee7ecc1b7faeb8 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:42:43 +0800 Subject: [PATCH 22/30] Force std feature in serde and rlp --- primitive-types/Cargo.toml | 4 ++-- primitive-types/src/lib.rs | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 39818cd09..4f668397f 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -21,6 +21,6 @@ heapsize = ["uint/heapsize", "fixed-hash/heapsize"] byteorder = ["fixed-hash/byteorder"] libc = ["fixed-hash/libc"] rustc-hex = ["fixed-hash/rustc-hex"] -serde = ["impl-serde"] +serde = ["std", "impl-serde"] codec = ["impl-codec"] -rlp = ["impl-rlp"] +rlp = ["std", "impl-rlp"] diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index c7279be02..c745835c8 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -14,12 +14,6 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(all(feature = "impl-serde", not(feature = "std")))] -compile_error!("Feature \"impl-serde\" requires feature \"std\" to build."); - -#[cfg(all(feature = "impl-rlp", not(feature = "std")))] -compile_error!("Feature \"impl-rlp\" requires feature \"std\" to build."); - #[macro_use] extern crate uint; From 8a06e416974af780731bb2023bdccce01fae348f Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:43:38 +0800 Subject: [PATCH 23/30] Fix style nit --- primitive-types/src/lib.rs | 45 +++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index c745835c8..141bbd43d 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -36,9 +36,12 @@ extern crate impl_codec; extern crate impl_rlp; construct_uint!(U256, 4); -#[cfg(feature = "impl-serde")] impl_uint_serde!(U256, 4); -#[cfg(feature = "impl-codec")] impl_uint_codec!(U256, 4); -#[cfg(feature = "impl-rlp")] impl_uint_rlp!(U256, 4); +#[cfg(feature = "impl-serde")] +impl_uint_serde!(U256, 4); +#[cfg(feature = "impl-codec")] +impl_uint_codec!(U256, 4); +#[cfg(feature = "impl-rlp")] +impl_uint_rlp!(U256, 4); impl U256 { /// Multiplies two 256-bit integers to produce full 512-bit integer @@ -104,17 +107,23 @@ impl<'a> From<&'a U512> for U256 { } construct_uint!(U512, 8); -#[cfg(feature = "impl-serde")] impl_uint_serde!(U512, 8); -#[cfg(feature = "impl-codec")] impl_uint_codec!(U512, 8); -#[cfg(feature = "impl-rlp")] impl_uint_rlp!(U512, 8); +#[cfg(feature = "impl-serde")] +impl_uint_serde!(U512, 8); +#[cfg(feature = "impl-codec")] +impl_uint_codec!(U512, 8); +#[cfg(feature = "impl-rlp")] +impl_uint_rlp!(U512, 8); construct_fixed_hash! { /// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size. pub struct H160(20); } -#[cfg(feature = "impl-serde")] impl_fixed_hash_serde!(H160, 20); -#[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H160, 20); -#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20); +#[cfg(feature = "impl-serde")] +impl_fixed_hash_serde!(H160, 20); +#[cfg(feature = "impl-codec")] +impl_fixed_hash_codec!(H160, 20); +#[cfg(feature = "impl-rlp")] +impl_fixed_hash_rlp!(H160, 20); #[cfg(feature = "std")] impl From for H160 { @@ -143,9 +152,12 @@ construct_fixed_hash! { /// 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); -#[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H256, 32); -#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H256, 32); +#[cfg(feature = "impl-serde")] +impl_fixed_hash_serde!(H256, 32); +#[cfg(feature = "impl-codec")] +impl_fixed_hash_codec!(H256, 32); +#[cfg(feature = "impl-rlp")] +impl_fixed_hash_rlp!(H256, 32); #[cfg(feature = "std")] impl From for H256 { @@ -158,9 +170,12 @@ construct_fixed_hash! { /// 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); -#[cfg(feature = "impl-codec")] impl_fixed_hash_codec!(H512, 64); -#[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H512, 64); +#[cfg(feature = "impl-serde")] +impl_fixed_hash_serde!(H512, 64); +#[cfg(feature = "impl-codec")] +impl_fixed_hash_codec!(H512, 64); +#[cfg(feature = "impl-rlp")] +impl_fixed_hash_rlp!(H512, 64); #[cfg(feature = "std")] impl From for H512 { From 8d6d787009c3d1603c43be4a0b662cc5fc7b0dac Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:54:35 +0800 Subject: [PATCH 24/30] Fix unnecessary std dep --- primitive-types/impls/rlp/Cargo.toml | 2 +- primitive-types/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/primitive-types/impls/rlp/Cargo.toml b/primitive-types/impls/rlp/Cargo.toml index f3b44ef77..cc84f2b9b 100644 --- a/primitive-types/impls/rlp/Cargo.toml +++ b/primitive-types/impls/rlp/Cargo.toml @@ -7,4 +7,4 @@ homepage = "https://github.com/paritytech/parity-common" description = "RLP serialization support for uint and fixed hash." [dependencies] -rlp = { version = "0.3", path = "../../rlp", default-features = false } +rlp = { version = "0.3", path = "../../../rlp", default-features = false } diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 141bbd43d..6a7d74f68 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -159,7 +159,7 @@ impl_fixed_hash_codec!(H256, 32); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H256, 32); -#[cfg(feature = "std")] +#[cfg(feature = "fixed-hash/byteorder")] impl From for H256 { fn from(val: u64) -> Self { Self::from_low_u64_be(val) @@ -177,7 +177,7 @@ impl_fixed_hash_codec!(H512, 64); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H512, 64); -#[cfg(feature = "std")] +#[cfg(feature = "fixed-hash/byteorder")] impl From for H512 { fn from(val: u64) -> Self { Self::from_low_u64_be(val) From 5adf9f165185c2c1cdb56a7fd95e393590724b7d Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:55:58 +0800 Subject: [PATCH 25/30] Remove `From` from primitive types --- primitive-types/src/lib.rs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 6a7d74f68..868063c33 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -125,13 +125,6 @@ impl_fixed_hash_codec!(H160, 20); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20); -#[cfg(feature = "std")] -impl From for H160 { - fn from(val: u64) -> Self { - Self::from_low_u64_be(val) - } -} - impl From for H256 { fn from(value: H160) -> H256 { let mut ret = H256::zero(); @@ -159,13 +152,6 @@ impl_fixed_hash_codec!(H256, 32); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H256, 32); -#[cfg(feature = "fixed-hash/byteorder")] -impl From for H256 { - fn from(val: u64) -> Self { - Self::from_low_u64_be(val) - } -} - construct_fixed_hash! { /// Fixed-size uninterpreted hash type with 64 bytes (512 bits) size. pub struct H512(64); @@ -176,10 +162,3 @@ impl_fixed_hash_serde!(H512, 64); impl_fixed_hash_codec!(H512, 64); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H512, 64); - -#[cfg(feature = "fixed-hash/byteorder")] -impl From for H512 { - fn from(val: u64) -> Self { - Self::from_low_u64_be(val) - } -} From 559e5a2923341359a49c1902cc8c0a4b2b51c1a8 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Tue, 4 Dec 2018 11:57:56 +0800 Subject: [PATCH 26/30] Copyright year 2015-2018 --- primitive-types/impls/codec/src/lib.rs | 2 +- primitive-types/impls/rlp/src/lib.rs | 2 +- primitive-types/impls/serde/src/lib.rs | 2 +- primitive-types/impls/serde/src/serialize.rs | 2 +- primitive-types/src/lib.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/primitive-types/impls/codec/src/lib.rs b/primitive-types/impls/codec/src/lib.rs index d7e7bfb60..0861ee086 100644 --- a/primitive-types/impls/codec/src/lib.rs +++ b/primitive-types/impls/codec/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Parity Technologies +// Copyright 2015-2018 Parity Technologies // // Licensed under the Apache License, Version 2.0 or the MIT license diff --git a/primitive-types/impls/rlp/src/lib.rs b/primitive-types/impls/rlp/src/lib.rs index 89380acf2..8f01d054c 100644 --- a/primitive-types/impls/rlp/src/lib.rs +++ b/primitive-types/impls/rlp/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Parity Technologies +// Copyright 2015-2018 Parity Technologies // // Licensed under the Apache License, Version 2.0 or the MIT license diff --git a/primitive-types/impls/serde/src/lib.rs b/primitive-types/impls/serde/src/lib.rs index 509f36c47..ad6e53d75 100644 --- a/primitive-types/impls/serde/src/lib.rs +++ b/primitive-types/impls/serde/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Parity Technologies +// Copyright 2015-2018 Parity Technologies // // Licensed under the Apache License, Version 2.0 or the MIT license diff --git a/primitive-types/impls/serde/src/serialize.rs b/primitive-types/impls/serde/src/serialize.rs index b3e3d95f2..040255a86 100644 --- a/primitive-types/impls/serde/src/serialize.rs +++ b/primitive-types/impls/serde/src/serialize.rs @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Parity Technologies +// Copyright 2015-2018 Parity Technologies // // Licensed under the Apache License, Version 2.0 or the MIT license diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 868063c33..e93d03ced 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2015-2017 Parity Technologies +// Copyright 2015-2018 Parity Technologies // // Licensed under the Apache License, Version 2.0 or the MIT license From dfc65a174f5afc1291a95031452e1f91c912de1e Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Wed, 5 Dec 2018 12:43:58 +0800 Subject: [PATCH 27/30] Use impl_fixed_hash_conversions --- primitive-types/src/lib.rs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index e93d03ced..dadcb5660 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -125,21 +125,7 @@ impl_fixed_hash_codec!(H160, 20); #[cfg(feature = "impl-rlp")] impl_fixed_hash_rlp!(H160, 20); -impl From for H256 { - fn from(value: H160) -> H256 { - let mut ret = H256::zero(); - ret.0[12..32].copy_from_slice(value.as_bytes()); - ret - } -} - -impl<'a> From<&'a H160> for H256 { - fn from(value: &'a H160) -> H256 { - let mut ret = H256::zero(); - ret.0[12..32].copy_from_slice(value.as_bytes()); - ret - } -} +impl_fixed_hash_conversions!(H256, H160); construct_fixed_hash! { /// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size. From 88efc9b0605f5d6dae3f20ac874295de0712356b Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 21 Dec 2018 22:24:59 +0800 Subject: [PATCH 28/30] Use new construct int style --- primitive-types/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index dadcb5660..2e4d9dd99 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -35,7 +35,10 @@ extern crate impl_codec; #[macro_use] extern crate impl_rlp; -construct_uint!(U256, 4); +construct_uint! { + /// 256-bit unsigned integer. + pub struct U256(4); +} #[cfg(feature = "impl-serde")] impl_uint_serde!(U256, 4); #[cfg(feature = "impl-codec")] @@ -106,7 +109,10 @@ impl<'a> From<&'a U512> for U256 { } } -construct_uint!(U512, 8); +construct_uint! { + /// 512-bits unsigned integer. + pub struct U512(8); +} #[cfg(feature = "impl-serde")] impl_uint_serde!(U512, 8); #[cfg(feature = "impl-codec")] From 42f557d3424c0ade968f866869f8e90b2206922b Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 21 Dec 2018 22:26:41 +0800 Subject: [PATCH 29/30] Remove crunchy and update uint version --- primitive-types/Cargo.toml | 5 ++--- primitive-types/src/lib.rs | 3 --- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 4f668397f..03781d9b2 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -8,15 +8,14 @@ description = "Primitive types shared by Ethereum and Substrate" [dependencies] fixed-hash = { version = "0.3", path = "../fixed-hash", default-features = false } -uint = { version = "0.5", path = "../uint", default-features = false } -crunchy = { version = "0.2", default-features = false } +uint = { version = "0.6", path = "../uint", default-features = false } impl-serde = { version = "0.1", path = "impls/serde", default-features = false, optional = true } impl-codec = { version = "0.1", path = "impls/codec", default-features = false, optional = true } impl-rlp = { version = "0.1", path = "impls/rlp", default-features = false, optional = true } [features] default = ["std"] -std = ["uint/std", "fixed-hash/std", "crunchy/std", "impl-codec/std"] +std = ["uint/std", "fixed-hash/std", "impl-codec/std"] heapsize = ["uint/heapsize", "fixed-hash/heapsize"] byteorder = ["fixed-hash/byteorder"] libc = ["fixed-hash/libc"] diff --git a/primitive-types/src/lib.rs b/primitive-types/src/lib.rs index 2e4d9dd99..0be1bb63b 100644 --- a/primitive-types/src/lib.rs +++ b/primitive-types/src/lib.rs @@ -20,9 +20,6 @@ extern crate uint; #[macro_use] extern crate fixed_hash; -#[macro_use] -extern crate crunchy; - #[cfg(feature = "impl-serde")] #[macro_use] extern crate impl_serde; From d632b4c1c88fc9260ddcb2fec5c07d016eefee55 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 21 Dec 2018 22:30:26 +0800 Subject: [PATCH 30/30] Bump primitive-types patch version --- primitive-types/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index 03781d9b2..b95736353 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "primitive-types" -version = "0.1.4" +version = "0.1.5" authors = ["Parity Technologies "] license = "Apache-2.0/MIT" homepage = "https://github.com/paritytech/parity-common"