Merge branch 'master' of github.com:gavofyork/ethcore-util
This commit is contained in:
commit
cfd858668f
@ -16,7 +16,6 @@ rand = "0.3.12"
|
|||||||
time = "0.1.34"
|
time = "0.1.34"
|
||||||
tiny-keccak = "1.0"
|
tiny-keccak = "1.0"
|
||||||
rocksdb = "0.2.1"
|
rocksdb = "0.2.1"
|
||||||
num = "0.1"
|
|
||||||
lazy_static = "0.1.*"
|
lazy_static = "0.1.*"
|
||||||
secp256k1 = "0.5.1"
|
secp256k1 = "0.5.1"
|
||||||
rust-crypto = "0.2.34"
|
rust-crypto = "0.2.34"
|
||||||
|
@ -41,7 +41,6 @@
|
|||||||
use std::collections::{HashMap};
|
use std::collections::{HashMap};
|
||||||
use hash::*;
|
use hash::*;
|
||||||
use sha3::*;
|
use sha3::*;
|
||||||
use num::pow;
|
|
||||||
|
|
||||||
/// Represents bloom index in cache
|
/// Represents bloom index in cache
|
||||||
///
|
///
|
||||||
@ -119,13 +118,21 @@ impl<'a, D> ChainFilter<'a, D> where D: FilterDataSource
|
|||||||
let mut filter = ChainFilter {
|
let mut filter = ChainFilter {
|
||||||
data_source: data_source,
|
data_source: data_source,
|
||||||
index_size: index_size,
|
index_size: index_size,
|
||||||
level_sizes: vec![]
|
// 0 level has always a size of 1
|
||||||
|
level_sizes: vec![1]
|
||||||
};
|
};
|
||||||
|
|
||||||
// cache level sizes, so we do not have to calculate them all the time
|
// cache level sizes, so we do not have to calculate them all the time
|
||||||
for i in 0..levels {
|
// eg. if levels == 3, index_size = 16
|
||||||
filter.level_sizes.push(pow(index_size, i as usize));
|
// level_sizes = [1, 16, 256]
|
||||||
}
|
let additional: Vec<usize> = (1..).into_iter()
|
||||||
|
.scan(1, |acc, _| {
|
||||||
|
*acc = *acc * index_size;
|
||||||
|
Some(*acc)
|
||||||
|
})
|
||||||
|
.take(levels as usize - 1)
|
||||||
|
.collect();
|
||||||
|
filter.level_sizes.extend(additional);
|
||||||
|
|
||||||
filter
|
filter
|
||||||
}
|
}
|
||||||
|
11
src/hash.rs
11
src/hash.rs
@ -9,6 +9,7 @@ use rand::Rng;
|
|||||||
use rand::os::OsRng;
|
use rand::os::OsRng;
|
||||||
use bytes::BytesConvertable;
|
use bytes::BytesConvertable;
|
||||||
use math::log2;
|
use math::log2;
|
||||||
|
use uint::U256;
|
||||||
|
|
||||||
/// types implementing FixedHash must be also BytesConvertable
|
/// types implementing FixedHash must be also BytesConvertable
|
||||||
pub trait FixedHash: Sized + BytesConvertable {
|
pub trait FixedHash: Sized + BytesConvertable {
|
||||||
@ -307,6 +308,16 @@ macro_rules! impl_hash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a U256> for H256 {
|
||||||
|
fn from(value: &'a U256) -> H256 {
|
||||||
|
unsafe {
|
||||||
|
let mut ret: H256 = ::std::mem::uninitialized();
|
||||||
|
value.to_bytes(&mut ret);
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl_hash!(H32, 4);
|
impl_hash!(H32, 4);
|
||||||
impl_hash!(H64, 8);
|
impl_hash!(H64, 8);
|
||||||
impl_hash!(H128, 16);
|
impl_hash!(H128, 16);
|
||||||
|
37
src/lib.rs
37
src/lib.rs
@ -1,13 +1,37 @@
|
|||||||
//! Ethcore-util library
|
//! Ethcore-util library
|
||||||
//!
|
//!
|
||||||
//! TODO: check reexports
|
//! ### Rust version:
|
||||||
|
//! - beta
|
||||||
|
//! - nightly
|
||||||
|
//!
|
||||||
|
//! ### Supported platforms:
|
||||||
|
//! - OSX
|
||||||
|
//! - Linux
|
||||||
|
//!
|
||||||
|
//! ### Dependencies:
|
||||||
|
//! - RocksDB 3.13
|
||||||
|
//!
|
||||||
|
//! ### Dependencies Installation:
|
||||||
|
//!
|
||||||
|
//! - OSX:
|
||||||
|
//!
|
||||||
|
//! ```bash
|
||||||
|
//! brew install rocksdb
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! - From source:
|
||||||
|
//!
|
||||||
|
//! ```bash
|
||||||
|
//! wget https://github.com/facebook/rocksdb/archive/rocksdb-3.13.tar.gz
|
||||||
|
//! tar xvf rocksdb-3.13.tar.gz && cd rocksdb-rocksdb-3.13 && make shared_lib
|
||||||
|
//! sudo make install
|
||||||
|
//! ```
|
||||||
|
|
||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
extern crate mio;
|
extern crate mio;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
extern crate rocksdb;
|
extern crate rocksdb;
|
||||||
extern crate tiny_keccak;
|
extern crate tiny_keccak;
|
||||||
extern crate num;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@ -39,12 +63,3 @@ pub mod trie;
|
|||||||
pub mod nibbleslice;
|
pub mod nibbleslice;
|
||||||
|
|
||||||
//pub mod network;
|
//pub mod network;
|
||||||
|
|
||||||
// reexports
|
|
||||||
pub use std::str::FromStr;
|
|
||||||
pub use hash::*;
|
|
||||||
pub use sha3::*;
|
|
||||||
pub use bytes::*;
|
|
||||||
pub use hashdb::*;
|
|
||||||
pub use memorydb::*;
|
|
||||||
|
|
||||||
|
136
src/uint.rs
136
src/uint.rs
@ -22,7 +22,7 @@
|
|||||||
///!
|
///!
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::cmp::{Ord, PartialOrd, Ordering};
|
use std::cmp::*;
|
||||||
use std::ops::*;
|
use std::ops::*;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use rustc_serialize::hex::{FromHex, FromHexError};
|
use rustc_serialize::hex::{FromHex, FromHexError};
|
||||||
@ -37,124 +37,11 @@ macro_rules! impl_map_from {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_array_newtype {
|
|
||||||
($thing:ident, $ty:ty, $len:expr) => {
|
|
||||||
impl $thing {
|
|
||||||
#[inline]
|
|
||||||
/// Converts the object to a raw pointer
|
|
||||||
pub fn as_ptr(&self) -> *const $ty {
|
|
||||||
let &$thing(ref dat) = self;
|
|
||||||
dat.as_ptr()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Converts the object to a mutable raw pointer
|
|
||||||
pub fn as_mut_ptr(&mut self) -> *mut $ty {
|
|
||||||
let &mut $thing(ref mut dat) = self;
|
|
||||||
dat.as_mut_ptr()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Returns the length of the object as an array
|
|
||||||
pub fn len(&self) -> usize { $len }
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
/// Returns whether the object, as an array, is empty. Always false.
|
|
||||||
pub fn is_empty(&self) -> bool { false }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> From<&'a [$ty]> for $thing {
|
|
||||||
fn from(data: &'a [$ty]) -> $thing {
|
|
||||||
assert_eq!(data.len(), $len);
|
|
||||||
unsafe {
|
|
||||||
use std::intrinsics::copy_nonoverlapping;
|
|
||||||
use std::mem;
|
|
||||||
let mut ret: $thing = mem::uninitialized();
|
|
||||||
copy_nonoverlapping(data.as_ptr(),
|
|
||||||
ret.as_mut_ptr(),
|
|
||||||
mem::size_of::<$thing>());
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<usize> for $thing {
|
|
||||||
type Output = $ty;
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn index(&self, index: usize) -> &$ty {
|
|
||||||
let &$thing(ref dat) = self;
|
|
||||||
&dat[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_index_newtype!($thing, $ty);
|
|
||||||
|
|
||||||
impl PartialEq for $thing {
|
|
||||||
#[inline]
|
|
||||||
fn eq(&self, other: &$thing) -> bool {
|
|
||||||
&self[..] == &other[..]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for $thing {}
|
|
||||||
|
|
||||||
impl Clone for $thing {
|
|
||||||
#[inline]
|
|
||||||
fn clone(&self) -> $thing {
|
|
||||||
$thing::from(&self[..])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Copy for $thing {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! impl_index_newtype {
|
|
||||||
($thing:ident, $ty:ty) => {
|
|
||||||
impl Index<Range<usize>> for $thing {
|
|
||||||
type Output = [$ty];
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn index(&self, index: Range<usize>) -> &[$ty] {
|
|
||||||
&self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<RangeTo<usize>> for $thing {
|
|
||||||
type Output = [$ty];
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn index(&self, index: RangeTo<usize>) -> &[$ty] {
|
|
||||||
&self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<RangeFrom<usize>> for $thing {
|
|
||||||
type Output = [$ty];
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn index(&self, index: RangeFrom<usize>) -> &[$ty] {
|
|
||||||
&self.0[index]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Index<RangeFull> for $thing {
|
|
||||||
type Output = [$ty];
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn index(&self, _: RangeFull) -> &[$ty] {
|
|
||||||
&self.0[..]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)]
|
||||||
pub struct $name(pub [u64; $n_words]);
|
pub struct $name(pub [u64; $n_words]);
|
||||||
impl_array_newtype!($name, u64, $n_words);
|
|
||||||
|
|
||||||
impl $name {
|
impl $name {
|
||||||
/// Conversion to u32
|
/// Conversion to u32
|
||||||
@ -186,6 +73,15 @@ macro_rules! construct_uint {
|
|||||||
(arr[index / 8] >> ((index % 8)) * 8) as u8
|
(arr[index / 8] >> ((index % 8)) * 8) as u8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_bytes(&self, bytes: &mut[u8]) {
|
||||||
|
assert!($n_words * 8 == bytes.len());
|
||||||
|
let &$name(ref arr) = self;
|
||||||
|
for i in 0..bytes.len() {
|
||||||
|
let rev = bytes.len() - 1 - i;
|
||||||
|
let pos = rev / 8;
|
||||||
|
bytes[i] = (arr[pos] >> ((rev % 8) * 8)) as u8;
|
||||||
|
}
|
||||||
|
}
|
||||||
/// 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;
|
||||||
@ -514,6 +410,16 @@ mod tests {
|
|||||||
assert_eq!(U256([0x12f0, 1 , 0x0910203040506077, 0x8090a0b0c0d0e0f0]), U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").unwrap());
|
assert_eq!(U256([0x12f0, 1 , 0x0910203040506077, 0x8090a0b0c0d0e0f0]), U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn uint256_to() {
|
||||||
|
let hex = "8090a0b0c0d0e0f00910203040506077583a2cf8264910e1436bda32571012f0";
|
||||||
|
let uint = U256::from_str(hex).unwrap();
|
||||||
|
let mut bytes = [0u8; 32];
|
||||||
|
uint.to_bytes(&mut bytes);
|
||||||
|
let uint2 = U256::from(&bytes[..]);
|
||||||
|
assert_eq!(uint, uint2);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn uint256_bits_test() {
|
pub fn uint256_bits_test() {
|
||||||
assert_eq!(U256::from(0u64).bits(), 0);
|
assert_eq!(U256::from(0u64).bits(), 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user