util reexports less std

This commit is contained in:
debris
2017-07-29 17:12:07 +02:00
parent e84f308264
commit eecd823d32
74 changed files with 255 additions and 168 deletions

View File

@@ -1,145 +0,0 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Bloom operations.
use std::mem;
use std::ops::DerefMut;
use {H64, H160, H256, H512, H520, H2048};
/// Returns log2.
pub fn log2(x: usize) -> u32 {
if x <= 1 {
return 0;
}
let n = x.leading_zeros();
mem::size_of::<usize>() as u32 * 8 - n
}
/// Bloom operations.
pub trait Bloomable: Sized + Default + DerefMut<Target = [u8]> {
/// When interpreting self as a bloom output, augment (bit-wise OR) with the a bloomed version of `b`.
fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: Bloomable;
/// Same as `shift_bloomed` except that `self` is consumed and a new value returned.
fn with_bloomed<T>(mut self, b: &T) -> Self where T: Bloomable {
self.shift_bloomed(b);
self
}
/// Construct new instance equal to the bloomed value of `b`.
fn from_bloomed<T>(b: &T) -> Self where T: Bloomable;
/// Bloom the current value using the bloom parameter `m`.
fn bloom_part<T>(&self, m: usize) -> T where T: Bloomable;
/// Check to see whether this hash, interpreted as a bloom, contains the value `b` when bloomed.
fn contains_bloomed<T>(&self, b: &T) -> bool where T: Bloomable;
}
macro_rules! impl_bloomable_for_hash {
($name: ident, $size: expr) => {
impl Bloomable for $name {
fn shift_bloomed<'a, T>(&'a mut self, b: &T) -> &'a mut Self where T: Bloomable {
let bp: Self = b.bloom_part($size);
let new_self = &bp | self;
self.0 = new_self.0;
self
}
fn bloom_part<T>(&self, m: usize) -> T where T: Bloomable + Default {
// numbers of bits
// TODO: move it to some constant
let p = 3;
let bloom_bits = m * 8;
let mask = bloom_bits - 1;
let bloom_bytes = (log2(bloom_bits) + 7) / 8;
// must be a power of 2
assert_eq!(m & (m - 1), 0);
// out of range
assert!(p * bloom_bytes <= $size);
// return type
let mut ret = T::default();
// 'ptr' to out slice
let mut ptr = 0;
// set p number of bits,
// p is equal 3 according to yellowpaper
for _ in 0..p {
let mut index = 0 as usize;
for _ in 0..bloom_bytes {
index = (index << 8) | self.0[ptr] as usize;
ptr += 1;
}
index &= mask;
ret[m - 1 - index / 8] |= 1 << (index % 8);
}
ret
}
fn contains_bloomed<T>(&self, b: &T) -> bool where T: Bloomable {
let bp: Self = b.bloom_part($size);
self.contains(&bp)
}
fn from_bloomed<T>(b: &T) -> Self where T: Bloomable {
b.bloom_part($size)
}
}
}
}
impl_bloomable_for_hash!(H64, 8);
impl_bloomable_for_hash!(H160, 20);
impl_bloomable_for_hash!(H256, 32);
impl_bloomable_for_hash!(H512, 64);
impl_bloomable_for_hash!(H520, 65);
impl_bloomable_for_hash!(H2048, 256);
#[cfg(test)]
mod tests {
use {H160, H256, H2048};
use sha3::Hashable;
use super::Bloomable;
#[test]
fn shift_bloomed() {
let bloom: H2048 = "00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000".into();
let address: H160 = "ef2d6d194084c2de36e0dabfce45d046b37d1106".into();
let topic: H256 = "02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc".into();
let mut my_bloom = H2048::default();
assert!(!my_bloom.contains_bloomed(&address.sha3()));
assert!(!my_bloom.contains_bloomed(&topic.sha3()));
my_bloom.shift_bloomed(&address.sha3());
assert!(my_bloom.contains_bloomed(&address.sha3()));
assert!(!my_bloom.contains_bloomed(&topic.sha3()));
my_bloom.shift_bloomed(&topic.sha3());
assert_eq!(my_bloom, bloom);
assert!(my_bloom.contains_bloomed(&address.sha3()));
assert!(my_bloom.contains_bloomed(&topic.sha3()));
}
}

View File

@@ -16,6 +16,7 @@
//! Utils common types and macros global reexport.
use std::io;
pub use standard::*;
pub use error::*;
pub use bytes::*;
@@ -100,8 +101,8 @@ macro_rules! flushln {
#[doc(hidden)]
pub fn flush(s: String) {
let _ = ::std::io::stdout().write(s.as_bytes());
let _ = ::std::io::stdout().flush();
let _ = io::Write::write(&mut io::stdout(), s.as_bytes());
let _ = io::Write::flush(&mut io::stdout());
}
#[test]

View File

@@ -16,6 +16,7 @@
//! Disk-backed `HashDB` implementation.
use std::collections::HashMap;
use common::*;
use rlp::*;
use hashdb::*;

View File

@@ -16,6 +16,8 @@
//! Disk-backed `HashDB` implementation.
use std::fmt;
use std::collections::HashMap;
use common::*;
use rlp::*;
use hashdb::*;

View File

@@ -16,6 +16,7 @@
//! `JournalDB` interface and implementation.
use std::{fmt, str};
use common::*;
/// Export the journaldb module.
@@ -59,7 +60,7 @@ impl Default for Algorithm {
fn default() -> Algorithm { Algorithm::OverlayRecent }
}
impl FromStr for Algorithm {
impl str::FromStr for Algorithm {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {

View File

@@ -16,6 +16,7 @@
//! `JournalDB` over in-memory overlay
use std::collections::HashMap;
use common::*;
use rlp::*;
use hashdb::*;

View File

@@ -16,6 +16,7 @@
//! Disk-backed, ref-counted `JournalDB` implementation.
use std::collections::HashMap;
use common::*;
use rlp::*;
use hashdb::*;

View File

@@ -16,6 +16,8 @@
//! Key-Value store abstraction with `RocksDB` backend.
use std::{mem, fs};
use std::collections::{HashMap, BTreeMap};
use std::io::ErrorKind;
use std::marker::PhantomData;
use std::path::PathBuf;

View File

@@ -112,10 +112,6 @@ extern crate ethcore_logger;
#[macro_use]
extern crate log as rlog;
pub extern crate using_queue;
pub extern crate table;
pub mod bloom;
pub mod standard;
#[macro_use]
pub mod common;
@@ -147,7 +143,6 @@ pub use overlaydb::*;
pub use journaldb::JournalDB;
pub use triehash::*;
pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut};
pub use nibbleslice::*;
pub use semantic_version::*;
pub use kvdb::*;
pub use timer::*;

View File

@@ -18,6 +18,7 @@
//! A random temp directory is created. A database is created within it, and migrations
//! are performed in temp sub-directories.
use std::collections::BTreeMap;
use common::*;
use migration::{Batch, Config, Error, SimpleMigration, Migration, Manager};
use kvdb::Database;

View File

@@ -60,9 +60,9 @@ pub fn version() -> String {
pub fn version_data() -> Bytes {
let mut s = RlpStream::new_list(4);
let v =
(u32::from_str(env!("CARGO_PKG_VERSION_MAJOR")).expect("Environment variables are known to be valid; qed") << 16) +
(u32::from_str(env!("CARGO_PKG_VERSION_MINOR")).expect("Environment variables are known to be valid; qed") << 8) +
u32::from_str(env!("CARGO_PKG_VERSION_PATCH")).expect("Environment variables are known to be valid; qed");
(env!("CARGO_PKG_VERSION_MAJOR").parse::<u32>().expect("Environment variables are known to be valid; qed") << 16) +
(env!("CARGO_PKG_VERSION_MINOR").parse::<u32>().expect("Environment variables are known to be valid; qed") << 8) +
env!("CARGO_PKG_VERSION_PATCH").parse::<u32>().expect("Environment variables are known to be valid; qed");
s.append(&v);
s.append(&"Parity");
s.append(&rustc_version());

View File

@@ -17,7 +17,7 @@
//! An owning, nibble-oriented byte vector.
use ::NibbleSlice;
use nibbleslice::NibbleSlice;
use elastic_array::ElasticArray36;
/// Owning, nibble-oriented byte vector. Counterpart to `NibbleSlice`.

View File

@@ -16,28 +16,10 @@
//! Std lib global reexports.
pub use std::io;
pub use std::fs;
pub use std::str;
pub use std::fmt;
pub use std::cmp;
pub use std::ptr;
pub use std::mem;
pub use std::ops;
pub use std::slice;
pub use std::result;
pub use std::option;
pub use std::path::Path;
pub use std::str::{FromStr};
pub use std::io::{Read,Write};
pub use std::hash::{Hash, Hasher};
pub use std::error::Error as StdError;
pub use std::ops::*;
pub use std::cmp::*;
pub use std::sync::Arc;
pub use std::collections::*;
pub use heapsize::HeapSizeOf;
pub use itertools::Itertools;

View File

@@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::fmt;
use common::*;
use hashdb::*;
use nibbleslice::*;