Merge branch 'master' of github.com:gavofyork/ethcore-util

This commit is contained in:
Gav Wood 2015-11-28 00:15:09 +01:00
commit d05435f4c4
6 changed files with 688 additions and 590 deletions

7
src/bloom.rs Normal file
View File

@ -0,0 +1,7 @@
use bytes::BytesConvertable;
// use hash::FixedHash;
pub trait Bloomable {
fn shift_bloom<T>(&mut self, bytes: &T) where T: BytesConvertable;
fn contains_bloom<T>(&self, bytes: &T) -> bool where T: BytesConvertable;
}

View File

@ -34,7 +34,7 @@ macro_rules! impl_bytes_convertable_for_array {
} }
} }
// two -1 at the end is not expanded // -1 at the end is not expanded
impl_bytes_convertable_for_array! { impl_bytes_convertable_for_array! {
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1

View File

@ -1,34 +1,29 @@
use std::str::FromStr; use std::str::FromStr;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut, BitOr};
use rustc_serialize::hex::*; use rustc_serialize::hex::*;
use error::EthcoreError; use error::EthcoreError;
use rand::Rng; use rand::Rng;
use rand::os::OsRng; use rand::os::OsRng;
use bytes::BytesConvertable; use bytes::BytesConvertable;
/// types implementing FixedHash must be also BytesConvertable
pub trait FixedHash: BytesConvertable {
fn random() -> Self;
fn randomize(&mut self);
fn mut_bytes(&mut self) -> &mut [u8];
}
macro_rules! impl_hash { macro_rules! impl_hash {
($from: ident, $size: expr) => { ($from: ident, $size: expr) => {
#[derive(Eq)] #[derive(Eq)]
pub struct $from (pub [u8; $size]); pub struct $from (pub [u8; $size]);
impl $from {
pub fn new() -> $from {
$from([0; $size])
}
pub fn random() -> $from {
let mut hash = $from::new();
hash.randomize();
hash
}
pub fn randomize(&mut self) {
let mut rng = OsRng::new().unwrap();
rng.fill_bytes(&mut self.0);
}
pub fn mut_bytes(&mut self) -> &mut [u8; $size] { impl $from {
&mut self.0 fn new() -> $from {
$from([0; $size])
} }
} }
@ -38,6 +33,23 @@ macro_rules! impl_hash {
} }
} }
impl FixedHash for $from {
fn random() -> $from {
let mut hash = $from::new();
hash.randomize();
hash
}
fn randomize(&mut self) {
let mut rng = OsRng::new().unwrap();
rng.fill_bytes(&mut self.0);
}
fn mut_bytes(&mut self) -> &mut [u8] {
&mut self.0
}
}
impl FromStr for $from { impl FromStr for $from {
type Err = EthcoreError; type Err = EthcoreError;
@ -68,10 +80,14 @@ macro_rules! impl_hash {
impl Clone for $from { impl Clone for $from {
fn clone(&self) -> $from { fn clone(&self) -> $from {
*self unsafe {
use std::{mem, ptr};
let mut ret: $from = mem::uninitialized();
ptr::copy(self.0.as_ptr(), ret.0.as_mut_ptr(), mem::size_of::<$from>());
ret
}
} }
} }
impl Copy for $from {}
impl PartialEq for $from { impl PartialEq for $from {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
@ -103,6 +119,30 @@ macro_rules! impl_hash {
&mut self.0[index] &mut self.0[index]
} }
} }
impl<'a> BitOr for &'a $from {
type Output = $from;
fn bitor(self, rhs: Self) -> Self::Output {
unsafe {
use std::mem;
let mut ret: $from = mem::uninitialized();
for i in 0..$size {
ret.0[i] = self.0[i] | rhs.0[i];
}
ret
}
}
}
impl BitOr for $from {
type Output = $from;
fn bitor(self, rhs: Self) -> Self::Output {
&self | &rhs
}
}
} }
} }
@ -114,7 +154,6 @@ impl_hash!(H512, 64);
impl_hash!(H520, 65); impl_hash!(H520, 65);
impl_hash!(H1024, 128); impl_hash!(H1024, 128);
impl_hash!(H2048, 256); impl_hash!(H2048, 256);
impl_hash!(H4096, 512);
#[test] #[test]
fn hash() { fn hash() {
@ -126,3 +165,16 @@ fn hash() {
assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee])); assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee]));
assert!(h != H64([0; 8])); assert!(h != H64([0; 8]));
} }
#[test]
fn hash_bitor() {
let a = H64([1; 8]);
let b = H64([2; 8]);
let c = H64([3; 8]);
// borrow
assert_eq!(&a | &b, c);
// move
assert_eq!(a | b, c);
}

View File

@ -16,6 +16,7 @@ pub mod db;
pub mod sha3; pub mod sha3;
pub mod hashdb; pub mod hashdb;
pub mod memorydb; pub mod memorydb;
pub mod bloom;
//pub mod network; //pub mod network;

1176
src/rlp.rs

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
use std::mem::uninitialized; use std::mem::uninitialized;
use tiny_keccak::keccak_256; use tiny_keccak::keccak_256;
use bytes::BytesConvertable; use bytes::BytesConvertable;
use hash::H256; use hash::{FixedHash, H256};
pub trait Hashable { pub trait Hashable {
fn sha3(&self) -> H256; fn sha3(&self) -> H256;