New JSON conversion traits.

This commit is contained in:
Gav Wood 2016-01-14 21:24:03 +01:00
parent df1ff2a448
commit 4f53db60ed
9 changed files with 213 additions and 76 deletions

View File

@ -21,6 +21,7 @@ eth-secp256k1 = { git = "https://github.com/arkpar/rust-secp256k1.git" }
rust-crypto = "0.2.34" rust-crypto = "0.2.34"
elastic-array = "0.4" elastic-array = "0.4"
heapsize = "0.2" heapsize = "0.2"
itertools = "0.4"
[dev-dependencies] [dev-dependencies]
json-tests = { path = "json-tests" } json-tests = { path = "json-tests" }

View File

@ -40,7 +40,7 @@ use std::slice;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::error::Error as StdError; use std::error::Error as StdError;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use uint::{U128, U256}; use uint::{Uint, U128, U256};
use hash::FixedHash; use hash::FixedHash;
pub struct PrettySlice<'a> (&'a [u8]); pub struct PrettySlice<'a> (&'a [u8]);

View File

@ -1,4 +1,5 @@
pub use standard::*; pub use standard::*;
pub use from_json::*;
pub use error::*; pub use error::*;
pub use hash::*; pub use hash::*;
pub use uint::*; pub use uint::*;

View File

@ -1,23 +1,18 @@
//! General hash types, a fixed-size raw-data type used as the output of hash functions. //! General hash types, a fixed-size raw-data type used as the output of hash functions.
use std::str::FromStr; use standard::*;
use std::fmt; use math::log2;
use std::ops;
use std::hash::{Hash, Hasher};
use std::ops::{Index, IndexMut, Deref, DerefMut, BitOr, BitOrAssign, BitAnd, BitXor};
use std::cmp::{PartialOrd, Ordering};
use rustc_serialize::hex::*;
use error::UtilError; use error::UtilError;
use rand::Rng; use rand::Rng;
use rand::os::OsRng; use rand::os::OsRng;
use bytes::{BytesConvertable,Populatable}; use bytes::{BytesConvertable,Populatable};
use math::log2; use from_json::*;
use uint::U256; use uint::{Uint, U256};
/// Trait for a fixed-size byte array to be used as the output of hash functions. /// Trait for a fixed-size byte array to be used as the output of hash functions.
/// ///
/// Note: types implementing `FixedHash` must be also `BytesConvertable`. /// Note: types implementing `FixedHash` must be also `BytesConvertable`.
pub trait FixedHash: Sized + BytesConvertable + Populatable { pub trait FixedHash: Sized + BytesConvertable + Populatable + FromStr + Default {
fn new() -> Self; fn new() -> Self;
/// Synonym for `new()`. Prefer to new as it's more readable. /// Synonym for `new()`. Prefer to new as it's more readable.
fn zero() -> Self; fn zero() -> Self;
@ -196,6 +191,20 @@ macro_rules! impl_hash {
} }
} }
impl FromJson for $from {
fn from_json(json: &Json) -> Self {
match json {
&Json::String(ref s) => {
match s.len() % 2 {
0 => FromStr::from_str(clean_0x(s)).unwrap(),
_ => FromStr::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap()
}
},
_ => Default::default(),
}
}
}
impl fmt::Debug for $from { impl fmt::Debug for $from {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in self.0.iter() { for i in self.0.iter() {
@ -381,6 +390,7 @@ macro_rules! impl_hash {
&self ^ &rhs &self ^ &rhs
} }
} }
impl $from { impl $from {
pub fn hex(&self) -> String { pub fn hex(&self) -> String {
format!("{:?}", self) format!("{:?}", self)
@ -389,6 +399,10 @@ macro_rules! impl_hash {
pub fn from_bloomed<T>(b: &T) -> Self where T: FixedHash { b.bloom_part($size) } pub fn from_bloomed<T>(b: &T) -> Self where T: FixedHash { b.bloom_part($size) }
} }
impl Default for $from {
fn default() -> Self { $from::new() }
}
impl From<u64> for $from { impl From<u64> for $from {
fn from(mut value: u64) -> $from { fn from(mut value: u64) -> $from {
let mut ret = $from::new(); let mut ret = $from::new();

View File

@ -8,6 +8,14 @@ pub fn clean(s: &str) -> &str {
} }
} }
pub fn u256_from_str(s: &str) -> U256 {
if s.len() >= 2 && &s[0..2] == "0x" {
U256::from_str(&s[2..]).unwrap_or(U256::from(0))
} else {
U256::from_dec_str(s).unwrap_or(U256::from(0))
}
}
pub fn bytes_from_json(json: &Json) -> Bytes { pub fn bytes_from_json(json: &Json) -> Bytes {
let s = json.as_string().unwrap_or(""); let s = json.as_string().unwrap_or("");
if s.len() % 2 == 1 { if s.len() % 2 == 1 {
@ -34,34 +42,97 @@ pub fn vec_h256_from_json(json: &Json) -> Vec<H256> {
json.as_array().unwrap().iter().map(&h256_from_json).collect() json.as_array().unwrap().iter().map(&h256_from_json).collect()
} }
pub fn u256_from_str(s: &str) -> U256 { pub fn map_h256_h256_from_json(json: &Json) -> BTreeMap<H256, H256> {
if s.len() >= 2 && &s[0..2] == "0x" { json.as_object().unwrap().iter().fold(BTreeMap::new(), |mut m, (key, value)| {
U256::from_str(&s[2..]).unwrap_or(U256::from(0)) m.insert(H256::from(&u256_from_str(key)), H256::from(&U256::from_json(value)));
} else { m
U256::from_dec_str(s).unwrap_or(U256::from(0)) })
}
}
pub fn u256_from_json(json: &Json) -> U256 {
u256_from_str(json.as_string().unwrap_or(""))
} }
pub fn usize_from_json(json: &Json) -> usize { pub fn usize_from_json(json: &Json) -> usize {
u256_from_json(json).low_u64() as usize U256::from_json(json).low_u64() as usize
} }
pub fn u64_from_json(json: &Json) -> u64 { pub fn u64_from_json(json: &Json) -> u64 {
u256_from_json(json).low_u64() U256::from_json(json).low_u64()
} }
pub fn u32_from_json(json: &Json) -> u32 { pub fn u32_from_json(json: &Json) -> u32 {
u256_from_json(json).low_u32() U256::from_json(json).low_u32()
} }
pub fn u16_from_json(json: &Json) -> u16 { pub fn u16_from_json(json: &Json) -> u16 {
u256_from_json(json).low_u32() as u16 U256::from_json(json).low_u32() as u16
} }
pub fn u8_from_json(json: &Json) -> u8 { pub fn u8_from_json(json: &Json) -> u8 {
u256_from_json(json).low_u32() as u8 U256::from_json(json).low_u32() as u8
}
impl<T> FromJson for Vec<T> where T: FromJson {
fn from_json(json: &Json) -> Self {
json.as_array().unwrap().iter().map(|x|T::from_json(x)).collect()
}
}
impl FromJson for u64 {
fn from_json(json: &Json) -> Self {
U256::from_json(json).low_u64()
}
}
impl FromJson for u32 {
fn from_json(json: &Json) -> Self {
U256::from_json(json).low_u64() as u32
}
}
impl FromJson for u16 {
fn from_json(json: &Json) -> Self {
U256::from_json(json).low_u64() as u16
}
}
impl FromJson for u8 {
fn from_json(json: &Json) -> Self {
U256::from_json(json).low_u64() as u8
}
}
#[test]
fn u256_from_json() {
let j = Json::from_str("{ \"dec\": \"10\", \"hex\": \"0x0a\", \"int\": 10 }").unwrap();
let v: U256 = xjson!(&j["dec"]);
assert_eq!(U256::from(10), v);
let v: U256 = xjson!(&j["hex"]);
assert_eq!(U256::from(10), v);
let v: U256 = xjson!(&j["int"]);
assert_eq!(U256::from(10), v);
}
#[test]
fn h256_from_json_() {
let j = Json::from_str("{ \"with\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"without\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\" }").unwrap();
let v: H256 = xjson!(&j["with"]);
assert_eq!(H256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap(), v);
let v: H256 = xjson!(&j["without"]);
assert_eq!(H256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap(), v);
}
#[test]
fn vec_u256_from_json() {
let j = Json::from_str("{ \"array\": [ \"10\", \"0x0a\", 10] }").unwrap();
let v: Vec<U256> = xjson!(&j["array"]);
assert_eq!(vec![U256::from(10); 3], v);
}
#[test]
fn vec_h256_from_json_() {
let j = Json::from_str("{ \"array\": [ \"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\"] }").unwrap();
let v: Vec<H256> = xjson!(&j["array"]);
assert_eq!(vec![H256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap(); 2], v);
} }

View File

@ -34,16 +34,15 @@ extern crate mio;
extern crate rand; extern crate rand;
extern crate rocksdb; extern crate rocksdb;
extern crate tiny_keccak; extern crate tiny_keccak;
#[macro_use] #[macro_use]
extern crate heapsize; extern crate heapsize;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use]
extern crate itertools;
extern crate env_logger; extern crate env_logger;
extern crate time; extern crate time;
extern crate crypto as rcrypto; extern crate crypto as rcrypto;
extern crate secp256k1; extern crate secp256k1;
@ -51,12 +50,16 @@ extern crate arrayvec;
extern crate elastic_array; extern crate elastic_array;
pub mod standard; pub mod standard;
#[macro_use]
pub mod from_json;
#[macro_use]
pub mod common; pub mod common;
pub mod error; pub mod error;
pub mod hash; pub mod hash;
pub mod uint; pub mod uint;
pub mod bytes; pub mod bytes;
pub mod rlp; pub mod rlp;
pub mod misc;
pub mod json_aid; pub mod json_aid;
pub mod vector; pub mod vector;
pub mod sha3; pub mod sha3;
@ -75,6 +78,7 @@ pub mod semantic_version;
pub mod network; pub mod network;
pub use common::*; pub use common::*;
pub use misc::*;
pub use json_aid::*; pub use json_aid::*;
pub use rlp::*; pub use rlp::*;
pub use hashdb::*; pub use hashdb::*;

View File

@ -5,6 +5,8 @@ use tiny_keccak::Keccak;
use bytes::{BytesConvertable,Populatable}; use bytes::{BytesConvertable,Populatable};
use hash::{H256, FixedHash}; use hash::{H256, FixedHash};
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
/// Types implementing this trait are sha3able. /// Types implementing this trait are sha3able.
/// ///
/// ``` /// ```
@ -43,12 +45,10 @@ impl<T> Hashable for T where T: BytesConvertable {
#[test] #[test]
fn sha3_empty() { fn sha3_empty() {
use std::str::FromStr; assert_eq!([0u8; 0].sha3(), SHA3_EMPTY);
assert_eq!([0u8; 0].sha3(), H256::from_str("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap());
} }
#[test] #[test]
fn sha3_as() { fn sha3_as() {
use std::str::FromStr; assert_eq!([0x41u8; 32].sha3(), From::from("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8"));
assert_eq!([0x41u8; 32].sha3(), H256::from_str("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8").unwrap());
} }

View File

@ -7,6 +7,7 @@ pub use std::ptr;
pub use std::result; pub use std::result;
pub use std::option; pub use std::option;
pub use std::mem; pub use std::mem;
pub use std::ops;
pub use std::path::Path; pub use std::path::Path;
pub use std::str::{FromStr}; pub use std::str::{FromStr};
@ -22,6 +23,7 @@ pub use std::collections::*;
pub use rustc_serialize::json::Json; pub use rustc_serialize::json::Json;
pub use rustc_serialize::base64::FromBase64; pub use rustc_serialize::base64::FromBase64;
pub use rustc_serialize::hex::FromHex; pub use rustc_serialize::hex::{FromHex, FromHexError};
pub use heapsize::HeapSizeOf; pub use heapsize::HeapSizeOf;
pub use itertools::Itertools;

View File

@ -21,17 +21,8 @@
///! The functions here are designed to be fast. ///! The functions here are designed to be fast.
///! ///!
use std::fmt; use standard::*;
use std::cmp::*; use from_json::*;
use std::ops::*;
use std::str::FromStr;
use std::hash::{Hash, Hasher};
use rustc_serialize::hex::{FromHex, FromHexError};
pub trait FromDecStr: Sized {
type Err;
fn from_dec_str(value: &str) -> Result<Self, Self::Err>;
}
macro_rules! impl_map_from { macro_rules! impl_map_from {
($thing:ident, $from:ty, $to:ty) => { ($thing:ident, $from:ty, $to:ty) => {
@ -43,32 +34,77 @@ macro_rules! impl_map_from {
} }
} }
pub trait Uint: Sized + Default + FromStr + From<u64> + FromJson + fmt::Debug + fmt::Display + PartialOrd + Ord + PartialEq + Eq + Hash {
/// Size of this type.
const SIZE: usize;
fn zero() -> Self;
fn one() -> Self;
type FromDecStrErr;
fn from_dec_str(value: &str) -> Result<Self, Self::FromDecStrErr>;
/// Conversion to u32
fn low_u32(&self) -> u32;
/// Conversion to u64
fn low_u64(&self) -> u64;
/// Conversion to u32 with overflow checking
fn as_u32(&self) -> u32;
/// Conversion to u64 with overflow checking
fn as_u64(&self) -> u64;
/// Return the least number of bits needed to represent the number
fn bits(&self) -> usize;
fn bit(&self, index: usize) -> bool;
fn byte(&self, index: usize) -> u8;
fn to_bytes(&self, bytes: &mut[u8]);
fn exp10(n: usize) -> Self;
}
macro_rules! construct_uint { macro_rules! construct_uint {
($name:ident, $n_words:expr) => ( ($name:ident, $n_words:expr) => (
/// Little-endian large integer type /// Little-endian large integer type
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Copy, Clone, Eq, PartialEq)]
pub struct $name(pub [u64; $n_words]); pub struct $name(pub [u64; $n_words]);
impl $name { impl Uint for $name {
pub const SIZE: usize = $n_words * 8; const SIZE: usize = $n_words * 8;
type FromDecStrErr = FromHexError;
/// TODO: optimize, throw appropriate err
fn from_dec_str(value: &str) -> Result<Self, Self::FromDecStrErr> {
Ok(value.bytes()
.map(|b| b - 48)
.fold($name::from(0u64), | acc, c |
// fast multiplication by 10
// (acc << 3) + (acc << 1) => acc * 10
(acc << 3) + (acc << 1) + $name::from(c)
))
}
/// Conversion to u32 /// Conversion to u32
#[inline] #[inline]
pub fn low_u32(&self) -> u32 { fn low_u32(&self) -> u32 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
arr[0] as u32 arr[0] as u32
} }
/// Conversion to u64 /// Conversion to u64
#[inline] #[inline]
pub fn low_u64(&self) -> u64 { fn low_u64(&self) -> u64 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
arr[0] arr[0]
} }
/// Conversion to u32 with overflow checking /// Conversion to u32 with overflow checking
#[inline] #[inline]
pub fn as_u32(&self) -> u32 { fn as_u32(&self) -> u32 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
if (arr[0] & (0xffffffffu64 << 32)) != 0 { if (arr[0] & (0xffffffffu64 << 32)) != 0 {
panic!("Integer overflow when casting U256") panic!("Integer overflow when casting U256")
@ -78,7 +114,7 @@ macro_rules! construct_uint {
/// Conversion to u64 with overflow checking /// Conversion to u64 with overflow checking
#[inline] #[inline]
pub fn as_u64(&self) -> u64 { fn as_u64(&self) -> u64 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
for i in 1..$n_words { for i in 1..$n_words {
if arr[i] != 0 { if arr[i] != 0 {
@ -89,7 +125,7 @@ macro_rules! construct_uint {
} }
/// Return the least number of bits needed to represent the number /// Return the least number of bits needed to represent the number
#[inline] #[inline]
pub fn bits(&self) -> usize { fn bits(&self) -> usize {
let &$name(ref arr) = self; let &$name(ref arr) = self;
for i in 1..$n_words { for i in 1..$n_words {
if arr[$n_words - i] > 0 { return (0x40 * ($n_words - i + 1)) - arr[$n_words - i].leading_zeros() as usize; } if arr[$n_words - i] > 0 { return (0x40 * ($n_words - i + 1)) - arr[$n_words - i].leading_zeros() as usize; }
@ -98,18 +134,18 @@ macro_rules! construct_uint {
} }
#[inline] #[inline]
pub fn bit(&self, index: usize) -> bool { fn bit(&self, index: usize) -> bool {
let &$name(ref arr) = self; let &$name(ref arr) = self;
arr[index / 64] & (1 << (index % 64)) != 0 arr[index / 64] & (1 << (index % 64)) != 0
} }
#[inline] #[inline]
pub fn byte(&self, index: usize) -> u8 { fn byte(&self, index: usize) -> u8 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
(arr[index / 8] >> ((index % 8)) * 8) as u8 (arr[index / 8] >> ((index % 8)) * 8) as u8
} }
pub fn to_bytes(&self, bytes: &mut[u8]) { fn to_bytes(&self, bytes: &mut[u8]) {
assert!($n_words * 8 == bytes.len()); assert!($n_words * 8 == bytes.len());
let &$name(ref arr) = self; let &$name(ref arr) = self;
for i in 0..bytes.len() { for i in 0..bytes.len() {
@ -120,7 +156,7 @@ macro_rules! construct_uint {
} }
#[inline] #[inline]
pub fn exp10(n: usize) -> $name { fn exp10(n: usize) -> $name {
match n { match n {
0 => $name::from(1u64), 0 => $name::from(1u64),
_ => $name::exp10(n - 1) * $name::from(10u64) _ => $name::exp10(n - 1) * $name::from(10u64)
@ -128,15 +164,17 @@ macro_rules! construct_uint {
} }
#[inline] #[inline]
pub fn zero() -> $name { fn zero() -> $name {
From::from(0u64) From::from(0u64)
} }
#[inline] #[inline]
pub fn one() -> $name { fn one() -> $name {
From::from(1u64) From::from(1u64)
} }
}
impl $name {
/// Multiplication by u32 /// Multiplication by u32
fn mul_u32(self, other: u32) -> $name { fn mul_u32(self, other: u32) -> $name {
let $name(ref arr) = self; let $name(ref arr) = self;
@ -154,6 +192,12 @@ macro_rules! construct_uint {
} }
} }
impl Default for $name {
fn default() -> Self {
$name::zero()
}
}
impl From<u64> for $name { impl From<u64> for $name {
fn from(value: u64) -> $name { fn from(value: u64) -> $name {
let mut ret = [0; $n_words]; let mut ret = [0; $n_words];
@ -162,6 +206,23 @@ macro_rules! construct_uint {
} }
} }
impl FromJson for $name {
fn from_json(json: &Json) -> Self {
match json {
&Json::String(ref s) => {
if s.len() >= 2 && &s[0..2] == "0x" {
FromStr::from_str(&s[2..]).unwrap_or(Default::default())
} else {
Uint::from_dec_str(s).unwrap_or(Default::default())
}
},
&Json::U64(u) => From::from(u),
&Json::I64(i) => From::from(i as u64),
_ => Uint::zero(),
}
}
}
impl_map_from!($name, u8, u64); impl_map_from!($name, u8, u64);
impl_map_from!($name, u16, u64); impl_map_from!($name, u16, u64);
impl_map_from!($name, u32, u64); impl_map_from!($name, u32, u64);
@ -443,22 +504,6 @@ macro_rules! construct_uint {
state.finish(); state.finish();
} }
} }
impl FromDecStr for $name {
type Err = FromHexError;
/// TODO: optimize, throw appropriate err
fn from_dec_str(value: &str) -> Result<Self, Self::Err> {
Ok(value.bytes()
.map(|b| b - 48)
.fold($name::from(0u64), | acc, c |
// fast multiplication by 10
// (acc << 3) + (acc << 1) => acc * 10
(acc << 3) + (acc << 1) + $name::from(c)
))
}
}
); );
} }
@ -557,8 +602,7 @@ pub const BAD_U256: U256 = U256([0xffffffffffffffffu64; 4]);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use uint::U256; use uint::{Uint, U256};
use uint::FromDecStr;
use std::str::FromStr; use std::str::FromStr;
#[test] #[test]