Merge branch 'master' into mul_fix
Conflicts: src/uint.rs
This commit is contained in:
commit
6252549bda
@ -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" }
|
||||||
|
@ -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]);
|
||||||
|
@ -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::*;
|
||||||
|
12
src/from_json.rs
Normal file
12
src/from_json.rs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
use standard::*;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! xjson {
|
||||||
|
( $x:expr ) => {
|
||||||
|
FromJson::from_json($x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait FromJson {
|
||||||
|
fn from_json(json: &Json) -> Self;
|
||||||
|
}
|
35
src/hash.rs
35
src/hash.rs
@ -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,21 @@ macro_rules! impl_hash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromJson for $from {
|
||||||
|
fn from_json(json: &Json) -> Self {
|
||||||
|
match json {
|
||||||
|
&Json::String(ref s) => {
|
||||||
|
println!("s: {}", 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 +391,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 +400,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();
|
||||||
|
148
src/json_aid.rs
148
src/json_aid.rs
@ -8,33 +8,7 @@ pub fn clean(s: &str) -> &str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bytes_from_json(json: &Json) -> Bytes {
|
fn u256_from_str(s: &str) -> U256 {
|
||||||
let s = json.as_string().unwrap_or("");
|
|
||||||
if s.len() % 2 == 1 {
|
|
||||||
FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![])
|
|
||||||
} else {
|
|
||||||
FromHex::from_hex(clean(s)).unwrap_or(vec![])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn address_from_json(json: &Json) -> Address {
|
|
||||||
From::from(json.as_string().unwrap_or("0000000000000000000000000000000000000000"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn h256_from_json(json: &Json) -> H256 {
|
|
||||||
let s = json.as_string().unwrap_or("0000000000000000000000000000000000000000000000000000000000000000");
|
|
||||||
if s.len() % 2 == 1 {
|
|
||||||
h256_from_hex(&("0".to_string() + &(clean(s).to_string()))[..])
|
|
||||||
} else {
|
|
||||||
h256_from_hex(clean(s))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn vec_h256_from_json(json: &Json) -> Vec<H256> {
|
|
||||||
json.as_array().unwrap().iter().map(&h256_from_json).collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn u256_from_str(s: &str) -> U256 {
|
|
||||||
if s.len() >= 2 && &s[0..2] == "0x" {
|
if s.len() >= 2 && &s[0..2] == "0x" {
|
||||||
U256::from_str(&s[2..]).unwrap_or(U256::from(0))
|
U256::from_str(&s[2..]).unwrap_or(U256::from(0))
|
||||||
} else {
|
} else {
|
||||||
@ -42,26 +16,122 @@ pub fn u256_from_str(s: &str) -> U256 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn u256_from_json(json: &Json) -> U256 {
|
impl FromJson for Bytes {
|
||||||
u256_from_str(json.as_string().unwrap_or(""))
|
fn from_json(json: &Json) -> Self {
|
||||||
|
match json {
|
||||||
|
&Json::String(ref s) => match s.len() % 2 {
|
||||||
|
0 => FromHex::from_hex(clean(s)).unwrap_or(vec![]),
|
||||||
|
_ => FromHex::from_hex(&("0".to_string() + &(clean(s).to_string()))[..]).unwrap_or(vec![]),
|
||||||
|
},
|
||||||
|
_ => vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn usize_from_json(json: &Json) -> usize {
|
impl FromJson for BTreeMap<H256, H256> {
|
||||||
u256_from_json(json).low_u64() as usize
|
fn from_json(json: &Json) -> Self {
|
||||||
|
match json {
|
||||||
|
&Json::Object(ref o) => o.iter().map(|(key, value)| (x!(&u256_from_str(key)), x!(&U256::from_json(value)))).collect(),
|
||||||
|
_ => BTreeMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn u64_from_json(json: &Json) -> u64 {
|
impl<T> FromJson for Vec<T> where T: FromJson {
|
||||||
u256_from_json(json).low_u64()
|
fn from_json(json: &Json) -> Self {
|
||||||
|
match json {
|
||||||
|
&Json::Array(ref o) => o.iter().map(|x|T::from_json(x)).collect(),
|
||||||
|
_ => Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn u32_from_json(json: &Json) -> u32 {
|
impl<T> FromJson for Option<T> where T: FromJson {
|
||||||
u256_from_json(json).low_u32()
|
fn from_json(json: &Json) -> Self {
|
||||||
|
match json {
|
||||||
|
&Json::String(ref o) if o.is_empty() => None,
|
||||||
|
&Json::Null => None,
|
||||||
|
_ => Some(FromJson::from_json(json)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn u16_from_json(json: &Json) -> u16 {
|
impl FromJson for u64 {
|
||||||
u256_from_json(json).low_u32() as u16
|
fn from_json(json: &Json) -> Self {
|
||||||
|
U256::from_json(json).low_u64()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn u8_from_json(json: &Json) -> u8 {
|
impl FromJson for u32 {
|
||||||
u256_from_json(json).low_u32() as u8
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple_types() {
|
||||||
|
let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap();
|
||||||
|
let v: u16 = xjson!(&j["int"]);
|
||||||
|
assert_eq!(42u16, v);
|
||||||
|
let v: u32 = xjson!(&j["dec"]);
|
||||||
|
assert_eq!(42u32, v);
|
||||||
|
let v: u64 = xjson!(&j["hex"]);
|
||||||
|
assert_eq!(42u64, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn option_types() {
|
||||||
|
let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap();
|
||||||
|
let v: Option<u16> = xjson!(&j["int"]);
|
||||||
|
assert_eq!(Some(42u16), v);
|
||||||
|
let v: Option<u16> = xjson!(&j["dec"]);
|
||||||
|
assert_eq!(Some(42u16), v);
|
||||||
|
let v: Option<u16> = xjson!(&j["null"]);
|
||||||
|
assert_eq!(None, v);
|
||||||
|
let v: Option<u16> = xjson!(&j["empty"]);
|
||||||
|
assert_eq!(None, v);
|
||||||
|
}
|
10
src/lib.rs
10
src/lib.rs
@ -35,16 +35,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;
|
||||||
@ -52,12 +51,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;
|
||||||
@ -76,6 +79,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::*;
|
||||||
|
31
src/misc.rs
Normal file
31
src/misc.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
use common::*;
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,PartialEq,Eq)]
|
||||||
|
/// Diff type for specifying a change (or not).
|
||||||
|
pub enum Diff<T> where T: Eq {
|
||||||
|
Same,
|
||||||
|
Born(T),
|
||||||
|
Changed(T, T),
|
||||||
|
Died(T),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Diff<T> where T: Eq {
|
||||||
|
/// Construct new object with given `pre` and `post`.
|
||||||
|
pub fn new(pre: T, post: T) -> Self { if pre == post { Diff::Same } else { Diff::Changed(pre, post) } }
|
||||||
|
|
||||||
|
/// Get the before value, if there is one.
|
||||||
|
pub fn pre(&self) -> Option<&T> { match self { &Diff::Died(ref x) | &Diff::Changed(ref x, _) => Some(x), _ => None } }
|
||||||
|
|
||||||
|
/// Get the after value, if there is one.
|
||||||
|
pub fn post(&self) -> Option<&T> { match self { &Diff::Born(ref x) | &Diff::Changed(_, ref x) => Some(x), _ => None } }
|
||||||
|
|
||||||
|
/// Determine whether there was a change or not.
|
||||||
|
pub fn is_same(&self) -> bool { match self { &Diff::Same => true, _ => false }}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq,Eq,Clone,Copy)]
|
||||||
|
/// Boolean type for clean/dirty status.
|
||||||
|
pub enum Filth {
|
||||||
|
Clean,
|
||||||
|
Dirty,
|
||||||
|
}
|
@ -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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
@ -71,6 +71,9 @@ impl<'db> TrieDBMut<'db> {
|
|||||||
/// Create a new trie with the backing database `db` and `root`
|
/// Create a new trie with the backing database `db` and `root`
|
||||||
/// Panics, if `root` does not exist
|
/// Panics, if `root` does not exist
|
||||||
pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self {
|
pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self {
|
||||||
|
if !db.exists(root) && root == &SHA3_NULL_RLP {
|
||||||
|
*root = db.insert(&NULL_RLP);
|
||||||
|
}
|
||||||
assert!(db.exists(root));
|
assert!(db.exists(root));
|
||||||
TrieDBMut {
|
TrieDBMut {
|
||||||
db: db,
|
db: db,
|
||||||
|
129
src/uint.rs
129
src/uint.rs
@ -21,18 +21,9 @@
|
|||||||
///! 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 std::num::wrapping::OverflowingOps;
|
use std::num::wrapping::OverflowingOps;
|
||||||
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) => {
|
||||||
@ -51,6 +42,37 @@ macro_rules! panic_on_overflow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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) => (
|
||||||
@ -58,26 +80,39 @@ macro_rules! construct_uint {
|
|||||||
#[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")
|
||||||
@ -87,7 +122,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 {
|
||||||
@ -98,7 +133,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; }
|
||||||
@ -107,18 +142,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() {
|
||||||
@ -129,7 +164,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)
|
||||||
@ -137,15 +172,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;
|
||||||
@ -193,6 +230,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];
|
||||||
@ -201,6 +244,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);
|
||||||
@ -561,22 +621,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)
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -675,10 +719,7 @@ pub const BAD_U256: U256 = U256([0xffffffffffffffffu64; 4]);
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use uint::U128;
|
use uint::{Uint, U128, U256, U512};
|
||||||
use uint::U256;
|
|
||||||
use uint::U512;
|
|
||||||
use uint::FromDecStr;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::num::wrapping::OverflowingOps;
|
use std::num::wrapping::OverflowingOps;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user