Merge branch 'master' of github.com:gavofyork/ethcore-util
This commit is contained in:
		
						commit
						d05435f4c4
					
				
							
								
								
									
										7
									
								
								src/bloom.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/bloom.rs
									
									
									
									
									
										Normal 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; | ||||
| } | ||||
| @ -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! { | ||||
|         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 | ||||
|  | ||||
							
								
								
									
										90
									
								
								src/hash.rs
									
									
									
									
									
								
							
							
						
						
									
										90
									
								
								src/hash.rs
									
									
									
									
									
								
							| @ -1,34 +1,29 @@ | ||||
| use std::str::FromStr; | ||||
| use std::fmt; | ||||
| use std::hash::{Hash, Hasher}; | ||||
| use std::ops::{Index, IndexMut}; | ||||
| use std::ops::{Index, IndexMut, BitOr}; | ||||
| use rustc_serialize::hex::*; | ||||
| use error::EthcoreError; | ||||
| use rand::Rng; | ||||
| use rand::os::OsRng; | ||||
| 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 { | ||||
| 	($from: ident, $size: expr) => { | ||||
| 		#[derive(Eq)] | ||||
| 		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] { | ||||
| 				&mut self.0 | ||||
| 		impl $from { | ||||
| 			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 { | ||||
| 			type Err = EthcoreError; | ||||
| 
 | ||||
| @ -68,10 +80,14 @@ macro_rules! impl_hash { | ||||
| 
 | ||||
| 		impl Clone for $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 { | ||||
| 			fn eq(&self, other: &Self) -> bool { | ||||
| @ -103,6 +119,30 @@ macro_rules! impl_hash { | ||||
| 				&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!(H1024, 128); | ||||
| impl_hash!(H2048, 256); | ||||
| impl_hash!(H4096, 512); | ||||
| 
 | ||||
| #[test] | ||||
| fn hash() { | ||||
| @ -126,3 +165,16 @@ fn hash() { | ||||
| 	assert!(h != H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xee])); | ||||
| 	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); | ||||
| } | ||||
|  | ||||
| @ -16,6 +16,7 @@ pub mod db; | ||||
| pub mod sha3; | ||||
| pub mod hashdb; | ||||
| pub mod memorydb; | ||||
| pub mod bloom; | ||||
| 
 | ||||
| //pub mod network;
 | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										1176
									
								
								src/rlp.rs
									
									
									
									
									
								
							
							
						
						
									
										1176
									
								
								src/rlp.rs
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @ -1,7 +1,7 @@ | ||||
| use std::mem::uninitialized; | ||||
| use tiny_keccak::keccak_256; | ||||
| use bytes::BytesConvertable; | ||||
| use hash::H256; | ||||
| use hash::{FixedHash, H256}; | ||||
| 
 | ||||
| pub trait Hashable { | ||||
| 	fn sha3(&self) -> H256; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user