merge accountdb migration
This commit is contained in:
commit
33c1a41847
|
@ -12,7 +12,6 @@ rustc_version = "0.1"
|
|||
|
||||
[dependencies]
|
||||
rustc-serialize = "0.3"
|
||||
serde = "0.7.0"
|
||||
heapsize = "0.3"
|
||||
|
||||
[features]
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
#![cfg_attr(asm_available, feature(asm))]
|
||||
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
#[macro_use] extern crate heapsize;
|
||||
|
||||
pub mod uint;
|
||||
|
|
91
src/uint.rs
91
src/uint.rs
|
@ -39,22 +39,22 @@
|
|||
#[cfg(all(asm_available, target_arch="x86_64"))]
|
||||
use std::mem;
|
||||
use std::fmt;
|
||||
use std::cmp;
|
||||
|
||||
use std::str::{FromStr};
|
||||
use std::convert::From;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::hash::Hash;
|
||||
use std::ops::*;
|
||||
use std::cmp::*;
|
||||
|
||||
use serde;
|
||||
use rustc_serialize::hex::{FromHex, FromHexError, ToHex};
|
||||
use rustc_serialize::hex::{FromHex, FromHexError};
|
||||
|
||||
/// Conversion from decimal string error
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum FromDecStrErr {
|
||||
/// Char not from range 0-9
|
||||
InvalidCharacter,
|
||||
/// Value does not fit into type
|
||||
InvalidLength
|
||||
InvalidLength,
|
||||
}
|
||||
|
||||
macro_rules! impl_map_from {
|
||||
|
@ -562,8 +562,11 @@ macro_rules! construct_uint {
|
|||
|
||||
impl Uint for $name {
|
||||
|
||||
/// TODO: optimize, throw appropriate err
|
||||
fn from_dec_str(value: &str) -> Result<Self, FromDecStrErr> {
|
||||
if value.bytes().any(|b| b < 48 && b > 57) {
|
||||
return Err(FromDecStrErr::InvalidCharacter)
|
||||
}
|
||||
|
||||
let mut res = Self::default();
|
||||
for b in value.bytes().map(|b| b - 48) {
|
||||
let (r, overflow) = res.overflowing_mul_u32(10);
|
||||
|
@ -649,7 +652,7 @@ macro_rules! construct_uint {
|
|||
fn exp10(n: usize) -> Self {
|
||||
match n {
|
||||
0 => Self::from(1u64),
|
||||
_ => Self::exp10(n - 1) * Self::from(10u64)
|
||||
_ => Self::exp10(n - 1).mul_u32(10)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -757,16 +760,16 @@ macro_rules! construct_uint {
|
|||
}
|
||||
|
||||
impl $name {
|
||||
#[allow(dead_code)] // not used when multiplied with inline assembly
|
||||
/// Multiplication by u32
|
||||
#[allow(dead_code)] // not used when multiplied with inline assembly
|
||||
fn mul_u32(self, other: u32) -> Self {
|
||||
let (ret, overflow) = self.overflowing_mul_u32(other);
|
||||
panic_on_overflow!(overflow);
|
||||
ret
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // not used when multiplied with inline assembly
|
||||
/// Overflowing multiplication by u32
|
||||
#[allow(dead_code)] // not used when multiplied with inline assembly
|
||||
fn overflowing_mul_u32(self, other: u32) -> (Self, bool) {
|
||||
let $name(ref arr) = self;
|
||||
let mut ret = [0u64; $n_words];
|
||||
|
@ -789,44 +792,6 @@ macro_rules! construct_uint {
|
|||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for $name {
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: serde::Serializer {
|
||||
let mut hex = "0x".to_owned();
|
||||
let mut bytes = [0u8; 8 * $n_words];
|
||||
self.to_raw_bytes(&mut bytes);
|
||||
let len = cmp::max((self.bits() + 7) / 8, 1);
|
||||
hex.push_str(bytes[bytes.len() - len..].to_hex().as_ref());
|
||||
serializer.serialize_str(hex.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Deserialize for $name {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<$name, D::Error>
|
||||
where D: serde::Deserializer {
|
||||
struct UintVisitor;
|
||||
|
||||
impl serde::de::Visitor for UintVisitor {
|
||||
type Value = $name;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: serde::Error {
|
||||
// 0x + len
|
||||
if value.len() > 2 + $n_words * 16 || value.len() < 2 {
|
||||
return Err(serde::Error::custom("Invalid length."));
|
||||
}
|
||||
|
||||
$name::from_str(&value[2..]).map_err(|_| serde::Error::custom("Invalid hex value."))
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: serde::Error {
|
||||
self.visit_str(value.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize(UintVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for $name {
|
||||
fn from(value: u64) -> $name {
|
||||
let mut ret = [0; $n_words];
|
||||
|
@ -959,8 +924,6 @@ macro_rules! construct_uint {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: optimise and traitify.
|
||||
|
||||
impl BitAnd<$name> for $name {
|
||||
type Output = $name;
|
||||
|
||||
|
@ -1031,7 +994,7 @@ macro_rules! construct_uint {
|
|||
|
||||
// shift
|
||||
for i in word_shift..$n_words {
|
||||
ret[i] += original[i - word_shift] << bit_shift;
|
||||
ret[i] = original[i - word_shift] << bit_shift;
|
||||
}
|
||||
// carry
|
||||
if bit_shift > 0 {
|
||||
|
@ -1052,14 +1015,18 @@ macro_rules! construct_uint {
|
|||
let word_shift = shift / 64;
|
||||
let bit_shift = shift % 64;
|
||||
|
||||
// shift
|
||||
for i in word_shift..$n_words {
|
||||
// Shift
|
||||
ret[i - word_shift] += original[i] >> bit_shift;
|
||||
// Carry
|
||||
if bit_shift > 0 && i < $n_words - 1 {
|
||||
ret[i - word_shift] += original[i + 1] << (64 - bit_shift);
|
||||
ret[i - word_shift] = original[i] >> bit_shift;
|
||||
}
|
||||
|
||||
// Carry
|
||||
if bit_shift > 0 {
|
||||
for i in word_shift+1..$n_words {
|
||||
ret[i - word_shift - 1] += original[i] << (64 - bit_shift);
|
||||
}
|
||||
}
|
||||
|
||||
$name(ret)
|
||||
}
|
||||
}
|
||||
|
@ -1430,12 +1397,6 @@ impl From<U256> for u32 {
|
|||
}
|
||||
}
|
||||
|
||||
/// Constant value of `U256::zero()` that can be used for a reference saving an additional instance creation.
|
||||
pub const ZERO_U256: U256 = U256([0x00u64; 4]);
|
||||
/// Constant value of `U256::one()` that can be used for a reference saving an additional instance creation.
|
||||
pub const ONE_U256: U256 = U256([0x01u64, 0x00u64, 0x00u64, 0x00u64]);
|
||||
|
||||
|
||||
known_heap_size!(0, U128, U256);
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -1578,7 +1539,13 @@ mod tests {
|
|||
assert_eq!(U256::from(105u8) / U256::from(5u8), U256::from(21u8));
|
||||
let div = mult / U256::from(300u16);
|
||||
assert_eq!(div, U256([0x9F30411021524112u64, 0x0001BD5B7DDFBD5A, 0, 0]));
|
||||
//// TODO: bit inversion
|
||||
|
||||
let a = U256::from_str("ff000000000000000000000000000000000000000000000000000000000000d1").unwrap();
|
||||
let b = U256::from_str("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2e").unwrap();
|
||||
println!("{:x}", a);
|
||||
println!("{:x}", b);
|
||||
assert_eq!(!a, b);
|
||||
assert_eq!(a, !b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in New Issue