Merge branch 'master' into new-jsonrpc
This commit is contained in:
@@ -28,32 +28,6 @@ use rustc_serialize::hex::{FromHex, FromHexError};
|
||||
use bigint::{Uint, U256};
|
||||
use libc::{c_void, memcmp};
|
||||
|
||||
/// Trait for a fixed-size byte array to be used as the output of hash functions.
|
||||
pub trait FixedHash: Sized {
|
||||
/// Create a new, zero-initialised, instance.
|
||||
fn new() -> Self;
|
||||
/// Synonym for `new()`. Prefer to new as it's more readable.
|
||||
fn zero() -> Self;
|
||||
/// Create a new, cryptographically random, instance.
|
||||
fn random() -> Self;
|
||||
/// Assign self have a cryptographically random value.
|
||||
fn randomize(&mut self);
|
||||
/// Get the size of this object in bytes.
|
||||
fn len() -> usize;
|
||||
/// Convert a slice of bytes of length `len()` to an instance of this type.
|
||||
fn from_slice(src: &[u8]) -> Self;
|
||||
/// Assign self to be of the same value as a slice of bytes of length `len()`.
|
||||
fn clone_from_slice(&mut self, src: &[u8]) -> usize;
|
||||
/// Copy the data of this object into some mutable slice of length `len()`.
|
||||
fn copy_to(&self, dest: &mut [u8]);
|
||||
/// Returns `true` if all bits set in `b` are also set in `self`.
|
||||
fn contains<'a>(&'a self, b: &'a Self) -> bool;
|
||||
/// Returns `true` if no bits are set.
|
||||
fn is_zero(&self) -> bool;
|
||||
/// Returns the lowest 8 bytes interpreted as a BigEndian integer.
|
||||
fn low_u64(&self) -> u64;
|
||||
}
|
||||
|
||||
/// Return `s` without the `0x` at the beginning of it, if any.
|
||||
pub fn clean_0x(s: &str) -> &str {
|
||||
if s.starts_with("0x") {
|
||||
@@ -105,57 +79,68 @@ macro_rules! impl_hash {
|
||||
}
|
||||
}
|
||||
|
||||
impl FixedHash for $from {
|
||||
fn new() -> $from {
|
||||
impl $from {
|
||||
/// Create a new, zero-initialised, instance.
|
||||
pub fn new() -> $from {
|
||||
$from([0; $size])
|
||||
}
|
||||
|
||||
fn zero() -> $from {
|
||||
/// Synonym for `new()`. Prefer to new as it's more readable.
|
||||
pub fn zero() -> $from {
|
||||
$from([0; $size])
|
||||
}
|
||||
|
||||
fn random() -> $from {
|
||||
/// Create a new, cryptographically random, instance.
|
||||
pub fn random() -> $from {
|
||||
let mut hash = $from::new();
|
||||
hash.randomize();
|
||||
hash
|
||||
}
|
||||
|
||||
fn randomize(&mut self) {
|
||||
/// Assign self have a cryptographically random value.
|
||||
pub fn randomize(&mut self) {
|
||||
let mut rng = OsRng::new().unwrap();
|
||||
rng.fill_bytes(&mut self.0);
|
||||
}
|
||||
|
||||
fn len() -> usize {
|
||||
/// Get the size of this object in bytes.
|
||||
pub fn len() -> usize {
|
||||
$size
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn clone_from_slice(&mut self, src: &[u8]) -> usize {
|
||||
/// Assign self to be of the same value as a slice of bytes of length `len()`.
|
||||
pub fn clone_from_slice(&mut self, src: &[u8]) -> usize {
|
||||
let min = cmp::min($size, src.len());
|
||||
self.0[..min].copy_from_slice(&src[..min]);
|
||||
min
|
||||
}
|
||||
|
||||
fn from_slice(src: &[u8]) -> Self {
|
||||
/// Convert a slice of bytes of length `len()` to an instance of this type.
|
||||
pub fn from_slice(src: &[u8]) -> Self {
|
||||
let mut r = Self::new();
|
||||
r.clone_from_slice(src);
|
||||
r
|
||||
}
|
||||
|
||||
fn copy_to(&self, dest: &mut[u8]) {
|
||||
/// Copy the data of this object into some mutable slice of length `len()`.
|
||||
pub fn copy_to(&self, dest: &mut[u8]) {
|
||||
let min = cmp::min($size, dest.len());
|
||||
dest[..min].copy_from_slice(&self.0[..min]);
|
||||
}
|
||||
|
||||
fn contains<'a>(&'a self, b: &'a Self) -> bool {
|
||||
/// Returns `true` if all bits set in `b` are also set in `self`.
|
||||
pub fn contains<'a>(&'a self, b: &'a Self) -> bool {
|
||||
&(b & self) == b
|
||||
}
|
||||
|
||||
fn is_zero(&self) -> bool {
|
||||
/// Returns `true` if no bits are set.
|
||||
pub fn is_zero(&self) -> bool {
|
||||
self.eq(&Self::new())
|
||||
}
|
||||
|
||||
fn low_u64(&self) -> u64 {
|
||||
/// Returns the lowest 8 bytes interpreted as a BigEndian integer.
|
||||
pub fn low_u64(&self) -> u64 {
|
||||
let mut ret = 0u64;
|
||||
for i in 0..min($size, 8) {
|
||||
ret |= (self.0[$size - 1 - i] as u64) << (i * 8);
|
||||
|
||||
@@ -556,7 +556,6 @@ impl Discovery {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::net::{SocketAddr};
|
||||
use util::FixedHash;
|
||||
use node_table::{Node, NodeId, NodeEndpoint};
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -333,7 +333,7 @@ mod test {
|
||||
use std::sync::Arc;
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use super::*;
|
||||
use util::hash::{H256, FixedHash};
|
||||
use util::hash::H256;
|
||||
use io::*;
|
||||
use mio::tcp::TcpStream;
|
||||
use stats::NetworkStats;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
|
||||
use std::mem;
|
||||
use std::ops::DerefMut;
|
||||
use {H64, H160, H256, H512, H520, H2048, FixedHash};
|
||||
use {H64, H160, H256, H512, H520, H2048};
|
||||
|
||||
/// Returns log2.
|
||||
pub fn log2(x: usize) -> u32 {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
//! Utils common types and macros global reexport.
|
||||
|
||||
pub use standard::*;
|
||||
pub use from_json::*;
|
||||
pub use error::*;
|
||||
pub use bytes::*;
|
||||
pub use vector::*;
|
||||
|
||||
@@ -1,50 +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/>.
|
||||
|
||||
//! Coversion from json.
|
||||
|
||||
use standard::*;
|
||||
use bigint::prelude::*;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! xjson {
|
||||
( $x:expr ) => {
|
||||
FromJson::from_json($x)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait allowing conversion from a JSON value.
|
||||
pub trait FromJson {
|
||||
/// Convert a JSON value to an instance of this type.
|
||||
fn from_json(json: &Json) -> Self;
|
||||
}
|
||||
|
||||
impl FromJson for U256 {
|
||||
fn from_json(json: &Json) -> Self {
|
||||
match *json {
|
||||
Json::String(ref s) => {
|
||||
if s.len() >= 2 && &s[0..2] == "0x" {
|
||||
FromStr::from_str(&s[2..]).unwrap_or_else(|_| Default::default())
|
||||
} else {
|
||||
Uint::from_dec_str(s).unwrap_or_else(|_| Default::default())
|
||||
}
|
||||
},
|
||||
Json::U64(u) => From::from(u),
|
||||
Json::I64(i) => From::from(i as u64),
|
||||
_ => Uint::zero(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,8 +120,6 @@ pub extern crate table;
|
||||
pub mod bloom;
|
||||
pub mod standard;
|
||||
#[macro_use]
|
||||
pub mod from_json;
|
||||
#[macro_use]
|
||||
pub mod common;
|
||||
pub mod error;
|
||||
pub mod bytes;
|
||||
|
||||
@@ -19,7 +19,7 @@ extern crate sha3 as sha3_ext;
|
||||
|
||||
use std::io;
|
||||
use tiny_keccak::Keccak;
|
||||
use hash::{H256, FixedHash};
|
||||
use hash::H256;
|
||||
use self::sha3_ext::*;
|
||||
|
||||
/// Get the SHA3 (i.e. Keccak) hash of the empty bytes string.
|
||||
|
||||
@@ -23,7 +23,6 @@ const SNAPPY_OK: c_int = 0;
|
||||
const SNAPPY_INVALID_INPUT: c_int = 1;
|
||||
const SNAPPY_BUFFER_TOO_SMALL: c_int = 2;
|
||||
|
||||
#[link(name = "snappy", kind = "static")]
|
||||
extern {
|
||||
fn snappy_compress(
|
||||
input: *const c_char,
|
||||
|
||||
Reference in New Issue
Block a user