From 3439c06a1c2ce58d2336c39065e4b3780b78ec5f Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Thu, 1 Sep 2016 14:04:19 +0400 Subject: [PATCH 1/9] Synchronization tweaks for IPC services (#2028) * using sequentally consistent checks * making shutdown method synchronous * redndant line --- ipc/hypervisor/src/service.rs.in | 2 +- parity/boot.rs | 2 +- parity/sync.rs | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ipc/hypervisor/src/service.rs.in b/ipc/hypervisor/src/service.rs.in index 69585ee6c..938cea345 100644 --- a/ipc/hypervisor/src/service.rs.in +++ b/ipc/hypervisor/src/service.rs.in @@ -42,7 +42,7 @@ pub struct ModuleState { #[derive(Ipc)] pub trait ControlService { - fn shutdown(&self); + fn shutdown(&self) -> bool; } #[derive(Ipc)] diff --git a/parity/boot.rs b/parity/boot.rs index 1614317b8..aa0e4b82b 100644 --- a/parity/boot.rs +++ b/parity/boot.rs @@ -42,7 +42,7 @@ pub fn host_service(addr: &str, stop_guard: A let mut worker = nanoipc::Worker::::new(&service); worker.add_reqrep(&socket_url).unwrap(); - while !stop_guard.load(Ordering::Relaxed) { + while !stop_guard.load(Ordering::SeqCst) { worker.poll(); } }); diff --git a/parity/sync.rs b/parity/sync.rs index 95c9924c6..27e9d5a6a 100644 --- a/parity/sync.rs +++ b/parity/sync.rs @@ -31,9 +31,10 @@ struct SyncControlService { } impl ControlService for SyncControlService { - fn shutdown(&self) { + fn shutdown(&self) -> bool { trace!(target: "hypervisor", "Received shutdown from control service"); - self.stop.store(true, ::std::sync::atomic::Ordering::Relaxed); + self.stop.store(true, ::std::sync::atomic::Ordering::SeqCst); + true } } @@ -75,14 +76,15 @@ pub fn main() { let control_service = Arc::new(SyncControlService::default()); let as_control = control_service.clone() as Arc; let mut worker = nanoipc::Worker::::new(&as_control); + let thread_stop = control_service.stop.clone(); worker.add_reqrep( &service_urls::with_base(&service_config.io_path, service_urls::SYNC_CONTROL) ).unwrap(); - while !control_service.stop.load(::std::sync::atomic::Ordering::Relaxed) { + while !thread_stop.load(::std::sync::atomic::Ordering::SeqCst) { worker.poll(); } - service_stop.store(true, ::std::sync::atomic::Ordering::Relaxed); + service_stop.store(true, ::std::sync::atomic::Ordering::SeqCst); hypervisor.module_shutdown(SYNC_MODULE_ID); trace!(target: "hypervisor", "Sync process terminated gracefully"); From 9a5668f8023c578411eb98387b4e750621e679bd Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 1 Sep 2016 12:23:31 +0200 Subject: [PATCH 2/9] get rid of populatable and bytesconvertable traits (#2019) --- ethcore/src/builtin.rs | 4 +- ethcore/src/error.rs | 6 +- ethcore/src/evm/interpreter/gasometer.rs | 2 +- ethcore/src/evm/interpreter/mod.rs | 10 +- ethcore/src/pod_account.rs | 4 +- ethcore/src/spec/spec.rs | 2 +- ethcore/src/state/account.rs | 2 +- ethcore/src/types/account_diff.rs | 8 +- ethcore/src/types/transaction_import.rs | 3 +- ipc/rpc/src/binary.rs | 22 +-- sync/src/api.rs | 4 +- sync/src/chain.rs | 2 +- util/src/bytes.rs | 162 +---------------------- util/src/error.rs | 4 +- util/src/journaldb/earlymergedb.rs | 2 +- util/src/memorydb.rs | 4 +- util/src/sha3.rs | 16 +-- 17 files changed, 54 insertions(+), 203 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index bcd02cbf3..2825a2a12 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -127,14 +127,14 @@ impl Impl for EcRecover { let s = H256::from_slice(&input[96..128]); let bit = match v[31] { - 27 | 28 if &v.as_slice()[..31] == &[0; 31] => v[31] - 27, + 27 | 28 if &v.0[..31] == &[0; 31] => v[31] - 27, _ => return, }; let s = Signature::from_rsv(&r, &s, bit); if s.is_valid() { if let Ok(p) = ec_recover(&s, &hash) { - let r = p.as_slice().sha3(); + let r = p.sha3(); let out_len = min(output.len(), 32); diff --git a/ethcore/src/error.rs b/ethcore/src/error.rs index 5c26e2f78..57fe25c44 100644 --- a/ethcore/src/error.rs +++ b/ethcore/src/error.rs @@ -28,7 +28,7 @@ use ethkey::Error as EthkeyError; pub use types::executed::{ExecutionError, CallError}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Copy)] /// Errors concerning transaction processing. pub enum TransactionError { /// Transaction is already imported to the queue @@ -87,7 +87,7 @@ impl fmt::Display for TransactionError { } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Clone, Copy, Eq)] /// Errors concerning block processing. pub enum BlockError { /// Block has too many uncles. @@ -185,7 +185,7 @@ impl fmt::Display for BlockError { } } -#[derive(Debug, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq)] /// Import to the block queue result pub enum ImportError { /// Already in the block chain. diff --git a/ethcore/src/evm/interpreter/gasometer.rs b/ethcore/src/evm/interpreter/gasometer.rs index ffa2c41a0..d7022be7c 100644 --- a/ethcore/src/evm/interpreter/gasometer.rs +++ b/ethcore/src/evm/interpreter/gasometer.rs @@ -76,7 +76,7 @@ impl Gasometer { instructions::SSTORE => { let address = H256::from(stack.peek(0)); let newval = stack.peek(1); - let val = U256::from(ext.storage_at(&address).as_slice()); + let val = U256::from(&*ext.storage_at(&address)); let gas = if val.is_zero() && !newval.is_zero() { schedule.sstore_set_gas diff --git a/ethcore/src/evm/interpreter/mod.rs b/ethcore/src/evm/interpreter/mod.rs index df3ca2b4a..d1b9b18bc 100644 --- a/ethcore/src/evm/interpreter/mod.rs +++ b/ethcore/src/evm/interpreter/mod.rs @@ -403,18 +403,18 @@ impl Interpreter { let offset = stack.pop_back(); let size = stack.pop_back(); let sha3 = self.mem.read_slice(offset, size).sha3(); - stack.push(U256::from(sha3.as_slice())); + stack.push(U256::from(&*sha3)); }, instructions::SLOAD => { let key = H256::from(&stack.pop_back()); - let word = U256::from(ext.storage_at(&key).as_slice()); + let word = U256::from(&*ext.storage_at(&key)); stack.push(word); }, instructions::SSTORE => { let address = H256::from(&stack.pop_back()); let val = stack.pop_back(); - let current_val = U256::from(ext.storage_at(&address).as_slice()); + let current_val = U256::from(&*ext.storage_at(&address)); // Increase refund for clear if !self.is_zero(¤t_val) && self.is_zero(&val) { ext.inc_sstore_clears(); @@ -491,7 +491,7 @@ impl Interpreter { instructions::BLOCKHASH => { let block_number = stack.pop_back(); let block_hash = ext.blockhash(&block_number); - stack.push(U256::from(block_hash.as_slice())); + stack.push(U256::from(&*block_hash)); }, instructions::COINBASE => { stack.push(address_to_u256(ext.env_info().author.clone())); @@ -807,7 +807,7 @@ fn u256_to_address(value: &U256) -> Address { #[inline] fn address_to_u256(value: Address) -> U256 { - U256::from(H256::from(value).as_slice()) + U256::from(&*H256::from(value)) } #[test] diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index 797d1a265..a65d0249b 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -57,7 +57,7 @@ impl PodAccount { let mut stream = RlpStream::new_list(4); stream.append(&self.nonce); stream.append(&self.balance); - stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), encode(&U256::from(v.as_slice())).to_vec())).collect())); + stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), encode(&U256::from(&**v)).to_vec())).collect())); stream.append(&self.code.as_ref().unwrap_or(&vec![]).sha3()); stream.out() } @@ -71,7 +71,7 @@ impl PodAccount { let mut r = H256::new(); let mut t = SecTrieDBMut::new(db, &mut r); for (k, v) in &self.storage { - if let Err(e) = t.insert(k, &encode(&U256::from(v.as_slice()))) { + if let Err(e) = t.insert(k, &encode(&U256::from(&**v))) { warn!("Encountered potential DB corruption: {}", e); } } diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 7a03a2acd..87a329740 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -231,7 +231,7 @@ impl Spec { { let mut t = SecTrieDBMut::new(db, &mut root); for (address, account) in self.genesis_state.get().iter() { - try!(t.insert(address.as_slice(), &account.rlp())); + try!(t.insert(&**address, &account.rlp())); } } for (address, account) in self.genesis_state.get().iter() { diff --git a/ethcore/src/state/account.rs b/ethcore/src/state/account.rs index 72e2bb0ed..e34be3e61 100644 --- a/ethcore/src/state/account.rs +++ b/ethcore/src/state/account.rs @@ -287,7 +287,7 @@ impl Account { // so we can call overloaded `to_bytes` method let res = match v.is_zero() { true => t.remove(k), - false => t.insert(k, &encode(&U256::from(v.as_slice()))), + false => t.insert(k, &encode(&U256::from(&*v))), }; if let Err(e) = res { diff --git a/ethcore/src/types/account_diff.rs b/ethcore/src/types/account_diff.rs index a33c9ddfe..e4766d7f1 100644 --- a/ethcore/src/types/account_diff.rs +++ b/ethcore/src/types/account_diff.rs @@ -98,12 +98,10 @@ impl AccountDiff { // TODO: refactor into something nicer. fn interpreted_hash(u: &H256) -> String { - use util::bytes::*; - if u <= &H256::from(0xffffffff) { - format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u32(), U256::from(u.as_slice()).low_u32()) + format!("{} = 0x{:x}", U256::from(&**u).low_u32(), U256::from(&**u).low_u32()) } else if u <= &H256::from(u64::max_value()) { - format!("{} = 0x{:x}", U256::from(u.as_slice()).low_u64(), U256::from(u.as_slice()).low_u64()) + format!("{} = 0x{:x}", U256::from(&**u).low_u64(), U256::from(&**u).low_u64()) // } else if u <= &H256::from("0xffffffffffffffffffffffffffffffffffffffff") { // format!("@{}", Address::from(u)) } else { @@ -113,7 +111,7 @@ fn interpreted_hash(u: &H256) -> String { impl fmt::Display for AccountDiff { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use util::bytes::*; + use util::bytes::ToPretty; match self.nonce { Diff::Born(ref x) => try!(write!(f, " non {}", x)), diff --git a/ethcore/src/types/transaction_import.rs b/ethcore/src/types/transaction_import.rs index c993632b9..e53313df7 100644 --- a/ethcore/src/types/transaction_import.rs +++ b/ethcore/src/types/transaction_import.rs @@ -18,9 +18,8 @@ use ipc::binary::{BinaryConvertError, BinaryConvertable}; use error::{TransactionError, Error}; -use util::Populatable; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq)] /// Represents the result of importing transaction. pub enum TransactionImportResult { /// Transaction was imported to current queue. diff --git a/ipc/rpc/src/binary.rs b/ipc/rpc/src/binary.rs index a1f1ed0bb..3908992d1 100644 --- a/ipc/rpc/src/binary.rs +++ b/ipc/rpc/src/binary.rs @@ -16,7 +16,6 @@ //! Binary representation of types -use util::bytes::Populatable; use util::{U256, U512, H256, H2048, Address}; use std::mem; use std::collections::{VecDeque, BTreeMap}; @@ -706,14 +705,21 @@ pub fn serialize(t: &T) -> Result, BinaryError> { #[macro_export] macro_rules! binary_fixed_size { ($target_ty: ty) => { - impl BinaryConvertable for $target_ty { + impl BinaryConvertable for $target_ty where $target_ty: Copy { fn from_bytes(bytes: &[u8], _length_stack: &mut ::std::collections::VecDeque) -> Result { - match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) { + let size = ::std::mem::size_of::<$target_ty>(); + match bytes.len().cmp(&size) { ::std::cmp::Ordering::Equal => (), - _ => return Err(BinaryConvertError::size(::std::mem::size_of::<$target_ty>(), bytes.len())), + _ => return Err(BinaryConvertError::size(size, bytes.len())), }; - let mut res: Self = unsafe { ::std::mem::uninitialized() }; - res.copy_raw(bytes); + let res: Self = unsafe { + let mut temp = ::std::mem::zeroed(); + let temp_ptr = &mut temp as *mut _ as *mut u8; + ::std::ptr::copy_nonoverlapping(bytes.as_ptr(), temp_ptr, size); + + temp + }; + Ok(res) } @@ -731,14 +737,14 @@ macro_rules! binary_fixed_size { } /// Fixed-sized version of Handshake struct -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct BinHandshake { api_version: BinVersion, protocol_version: BinVersion, } /// Shorten version of semver Version without `pre` and `build` information -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct BinVersion { pub major: u64, pub minor: u64, diff --git a/sync/src/api.rs b/sync/src/api.rs index 921eb1007..7b866d2bf 100644 --- a/sync/src/api.rs +++ b/sync/src/api.rs @@ -17,7 +17,7 @@ use std::sync::Arc; use network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, NetworkError}; -use util::{U256, H256, Populatable}; +use util::{U256, H256}; use io::{TimerToken}; use ethcore::client::{BlockChainClient, ChainNotify}; use ethcore::header::BlockNumber; @@ -32,7 +32,7 @@ use parking_lot::RwLock; pub const ETH_PROTOCOL: &'static str = "eth"; /// Sync configuration -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct SyncConfig { /// Max blocks to download ahead pub max_download_ahead_blocks: usize, diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 9abd05398..f5a54a0ba 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -156,7 +156,7 @@ pub enum SyncState { } /// Syncing status and statistics -#[derive(Clone)] +#[derive(Clone, Copy)] pub struct SyncStatus { /// State pub state: SyncState, diff --git a/util/src/bytes.rs b/util/src/bytes.rs index b9064acd1..7c5e929f4 100644 --- a/util/src/bytes.rs +++ b/util/src/bytes.rs @@ -14,26 +14,12 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -//! Unified interfaces for bytes operations on basic types +//! General bytes-related utilities. //! -//! # Examples -//! ```rust -//! extern crate ethcore_util as util; -//! -//! fn bytes_convertable() { -//! use util::bytes::BytesConvertable; -//! -//! let arr = [0; 5]; -//! let slice: &[u8] = arr.as_slice(); -//! } -//! -//! fn main() { -//! bytes_convertable(); -//! } -//! ``` +//! Includes a pretty-printer for bytes, in the form of `ToPretty` and `PrettySlice` +//! as use std::fmt; -use std::slice; use std::ops::{Deref, DerefMut}; /// Slice pretty print helper @@ -71,20 +57,9 @@ pub trait ToPretty { } } -impl<'a> ToPretty for &'a [u8] { +impl> ToPretty for T { fn pretty(&self) -> PrettySlice { - PrettySlice(self) - } -} - -impl<'a> ToPretty for &'a Bytes { - fn pretty(&self) -> PrettySlice { - PrettySlice(self.as_slice()) - } -} -impl ToPretty for Bytes { - fn pretty(&self) -> PrettySlice { - PrettySlice(self.as_slice()) + PrettySlice(self.as_ref()) } } @@ -116,128 +91,5 @@ impl <'a> DerefMut for BytesRef<'a> { } } -/// Vector of bytes -pub type Bytes = Vec; - -/// Slice of bytes to underlying memory -pub trait BytesConvertable { - /// Get the underlying byte-wise representation of the value. - fn as_slice(&self) -> &[u8]; - /// Get a copy of the underlying byte-wise representation. - fn to_bytes(&self) -> Bytes { self.as_slice().to_vec() } -} - -impl BytesConvertable for T where T: AsRef<[u8]> { - fn as_slice(&self) -> &[u8] { self.as_ref() } -} - -#[test] -fn bytes_convertable() { - assert_eq!(vec![0x12u8, 0x34].as_slice(), &[0x12u8, 0x34]); - assert!([0u8; 0].as_slice().is_empty()); -} - -/// Simple trait to allow for raw population of a Sized object from a byte slice. -pub trait Populatable { - /// Copies a bunch of bytes `d` to `self`, overwriting as necessary. - /// - /// If `d` is smaller, zero-out the remaining bytes. - fn populate_raw(&mut self, d: &[u8]) { - let mut s = self.as_slice_mut(); - for i in 0..s.len() { - s[i] = if i < d.len() {d[i]} else {0}; - } - } - - /// Copies a bunch of bytes `d` to `self`, overwriting as necessary. - /// - /// If `d` is smaller, will leave some bytes untouched. - fn copy_raw(&mut self, d: &[u8]) { - use std::io::Write; - self.as_slice_mut().write(d).unwrap(); - } - - /// Copies the raw representation of an object `d` to `self`, overwriting as necessary. - /// - /// If `d` is smaller, zero-out the remaining bytes. - fn populate_raw_from(&mut self, d: &BytesConvertable) { self.populate_raw(d.as_slice()); } - - /// Copies the raw representation of an object `d` to `self`, overwriting as necessary. - /// - /// If `d` is smaller, will leave some bytes untouched. - fn copy_raw_from(&mut self, d: &BytesConvertable) { self.copy_raw(d.as_slice()); } - - /// Get the raw slice for this object. - fn as_slice_mut(&mut self) -> &mut [u8]; -} - -impl Populatable for T where T: Sized { - fn as_slice_mut(&mut self) -> &mut [u8] { - use std::mem; - unsafe { - slice::from_raw_parts_mut(self as *mut T as *mut u8, mem::size_of::()) - } - } -} - -impl Populatable for [T] where T: Sized { - fn as_slice_mut(&mut self) -> &mut [u8] { - use std::mem; - unsafe { - slice::from_raw_parts_mut(self.as_mut_ptr() as *mut u8, mem::size_of::() * self.len()) - } - } -} - -#[test] -fn fax_raw() { - let mut x = [255u8; 4]; - x.copy_raw(&[1u8; 2][..]); - assert_eq!(x, [1u8, 1, 255, 255]); - let mut x = [255u8; 4]; - x.copy_raw(&[1u8; 6][..]); - assert_eq!(x, [1u8, 1, 1, 1]); -} - -#[test] -fn populate_raw() { - let mut x = [255u8; 4]; - x.populate_raw(&[1u8; 2][..]); - assert_eq!(x, [1u8, 1, 0, 0]); - let mut x = [255u8; 4]; - x.populate_raw(&[1u8; 6][..]); - assert_eq!(x, [1u8, 1, 1, 1]); -} - -#[test] -fn populate_raw_dyn() { - let mut x = [255u8; 4]; - x.populate_raw(&[1u8; 2][..]); - assert_eq!(&x[..], [1u8, 1, 0, 0]); - let mut x = [255u8; 4]; - x.populate_raw(&[1u8; 6][..]); - assert_eq!(&x[..], [1u8, 1, 1, 1]); -} - -#[test] -fn fax_raw_dyn() { - let mut x = [255u8; 4]; - x.copy_raw(&[1u8; 2][..]); - assert_eq!(&x[..], [1u8, 1, 255, 255]); - let mut x = [255u8; 4]; - x.copy_raw(&[1u8; 6][..]); - assert_eq!(&x[..], [1u8, 1, 1, 1]); -} - -#[test] -fn populate_big_types() { - use hash::*; - let a: H160 = "ffffffffffffffffffffffffffffffffffffffff".into(); - let mut h: H256 = 0x69.into(); - h.populate_raw_from(&a); - assert_eq!(h, "ffffffffffffffffffffffffffffffffffffffff000000000000000000000000".into()); - - let mut h: H256 = 0x69.into(); - h.copy_raw_from(&a); - assert_eq!(h, "ffffffffffffffffffffffffffffffffffffffff000000000000000000000069".into()); -} +/// Vector of bytes. +pub type Bytes = Vec; \ No newline at end of file diff --git a/util/src/error.rs b/util/src/error.rs index e1bf7b8a7..1c8180e88 100644 --- a/util/src/error.rs +++ b/util/src/error.rs @@ -74,7 +74,7 @@ impl fmt::Display for UtilError { } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] /// Error indicating an expected value was not found. pub struct Mismatch { /// Value expected. @@ -89,7 +89,7 @@ impl fmt::Display for Mismatch { } } -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] /// Error indicating value found is outside of a valid range. pub struct OutOfBounds { /// Minimum allowed value. diff --git a/util/src/journaldb/earlymergedb.rs b/util/src/journaldb/earlymergedb.rs index 4e53202c1..bbb4ed827 100644 --- a/util/src/journaldb/earlymergedb.rs +++ b/util/src/journaldb/earlymergedb.rs @@ -95,7 +95,7 @@ impl EarlyMergeDB { } fn morph_key(key: &H256, index: u8) -> Bytes { - let mut ret = key.to_bytes(); + let mut ret = (&**key).to_owned(); ret.push(index); ret } diff --git a/util/src/memorydb.rs b/util/src/memorydb.rs index 468ad2ec3..36e3fad1b 100644 --- a/util/src/memorydb.rs +++ b/util/src/memorydb.rs @@ -296,8 +296,8 @@ fn memorydb_denote() { for _ in 0..1000 { let r = H256::random(); let k = r.sha3(); - let (v, rc) = m.denote(&k, r.to_bytes()); - assert_eq!(v, r.as_slice()); + let (v, rc) = m.denote(&k, r.to_vec()); + assert_eq!(v, &*r); assert_eq!(rc, 0); } diff --git a/util/src/sha3.rs b/util/src/sha3.rs index 0dcde2ccb..7cb730223 100644 --- a/util/src/sha3.rs +++ b/util/src/sha3.rs @@ -18,16 +18,13 @@ extern crate sha3 as sha3_ext; use std::io; -use std::mem::uninitialized; use tiny_keccak::Keccak; -use bytes::{BytesConvertable, Populatable}; use hash::{H256, FixedHash}; use self::sha3_ext::*; /// Get the SHA3 (i.e. Keccak) hash of the empty bytes string. pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] ); - /// Types implementing this trait are sha3able. /// /// ``` @@ -50,17 +47,16 @@ pub trait Hashable { } } -impl Hashable for T where T: BytesConvertable { +impl Hashable for T where T: AsRef<[u8]> { fn sha3(&self) -> H256 { - unsafe { - let mut ret: H256 = uninitialized(); - self.sha3_into(ret.as_slice_mut()); - ret - } + let mut ret: H256 = H256::zero(); + self.sha3_into(&mut *ret); + ret } fn sha3_into(&self, dest: &mut [u8]) { + let input: &[u8] = self.as_ref(); + unsafe { - let input: &[u8] = self.as_slice(); sha3_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len()); } } From eb7b62a61c969993b97a2d10ac19a48822216f24 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 1 Sep 2016 13:36:32 +0200 Subject: [PATCH 3/9] move rlp to separate crate, port ethcore-util to it --- Cargo.lock | 18 +++++- ethcore/Cargo.toml | 1 + ethcore/src/account_db.rs | 1 + ethcore/src/lib.rs | 24 +++++-- scripts/targets.sh | 1 + util/Cargo.toml | 3 +- util/rlp/Cargo.toml | 12 ++++ util/{src/rlp => rlp/src}/bytes.rs | 2 +- util/{src/rlp => rlp/src}/commonrlps.rs | 60 +---------------- util/{src/rlp/mod.rs => rlp/src/lib.rs} | 27 ++++---- util/{src/rlp => rlp/src}/rlpcompression.rs | 30 ++------- util/{src/rlp => rlp/src}/rlperrors.rs | 2 +- util/{src/rlp => rlp/src}/rlpin.rs | 8 ++- util/{src/rlp => rlp/src}/rlpstream.rs | 7 +- util/{src/rlp => rlp/src}/rlptraits.rs | 72 ++++++++++----------- util/{src/rlp => rlp/src}/tests.rs | 31 ++++----- util/{src/rlp => rlp/src}/untrusted_rlp.rs | 7 +- util/src/lib.rs | 25 +++---- util/src/sha3.rs | 7 +- util/src/trie/mod.rs | 2 +- util/src/trie/triedb.rs | 2 +- util/src/trie/triedbmut.rs | 9 +-- 22 files changed, 159 insertions(+), 192 deletions(-) create mode 100644 util/rlp/Cargo.toml rename util/{src/rlp => rlp/src}/bytes.rs (99%) rename util/{src/rlp => rlp/src}/commonrlps.rs (76%) rename util/{src/rlp/mod.rs => rlp/src/lib.rs} (77%) rename util/{src/rlp => rlp/src}/rlpcompression.rs (91%) rename util/{src/rlp => rlp/src}/rlperrors.rs (98%) rename util/{src/rlp => rlp/src}/rlpin.rs (91%) rename util/{src/rlp => rlp/src}/rlpstream.rs (98%) rename util/{src/rlp => rlp/src}/rlptraits.rs (88%) rename util/{src/rlp => rlp/src}/tests.rs (92%) rename util/{src/rlp => rlp/src}/untrusted_rlp.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index 79bb6dba9..86db0c97a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -205,8 +205,8 @@ dependencies = [ [[package]] name = "elastic-array" -version = "0.4.0" -source = "git+https://github.com/ethcore/elastic-array#9a9bebd6ea291c58e4d6b44dd5dc18368638fefe" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "env_logger" @@ -278,6 +278,7 @@ dependencies = [ "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -492,7 +493,7 @@ dependencies = [ "arrayvec 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "bigint 0.1.0", "clippy 0.0.85 (registry+https://github.com/rust-lang/crates.io-index)", - "elastic-array 0.4.0 (git+https://github.com/ethcore/elastic-array)", + "elastic-array 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "eth-secp256k1 0.5.4 (git+https://github.com/ethcore/rust-secp256k1)", "ethcore-devtools 1.4.0", @@ -503,6 +504,7 @@ dependencies = [ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "rocksdb 0.4.5 (git+https://github.com/ethcore/rust-rocksdb)", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1265,6 +1267,16 @@ name = "regex-syntax" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rlp" +version = "0.1.0" +dependencies = [ + "bigint 0.1.0", + "elastic-array 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rocksdb" version = "0.4.5" diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index a34116df5..8acba2266 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -35,6 +35,7 @@ ethcore-ipc = { path = "../ipc/rpc" } ethstore = { path = "../ethstore" } ethkey = { path = "../ethkey" } ethcore-ipc-nano = { path = "../ipc/nano" } +rlp = { path = "../util/rlp" } rand = "0.3" [dependencies.hyper] diff --git a/ethcore/src/account_db.rs b/ethcore/src/account_db.rs index 15042403d..32c7acbb7 100644 --- a/ethcore/src/account_db.rs +++ b/ethcore/src/account_db.rs @@ -16,6 +16,7 @@ //! DB backend wrapper for Account trie use util::*; +use util::sha3::SHA3_NULL_RLP; static NULL_RLP_STATIC: [u8; 1] = [0x80; 1]; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index b8233ea26..5344381f0 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -79,12 +79,9 @@ //! cargo build --release //! ``` -#[macro_use] extern crate log; -#[macro_use] extern crate ethcore_util as util; + extern crate ethcore_io as io; -#[macro_use] extern crate lazy_static; extern crate rustc_serialize; -#[macro_use] extern crate heapsize; extern crate crypto; extern crate time; extern crate env_logger; @@ -92,19 +89,32 @@ extern crate num_cpus; extern crate crossbeam; extern crate ethjson; extern crate bloomchain; -#[macro_use] extern crate ethcore_ipc as ipc; extern crate rayon; extern crate hyper; extern crate ethash; extern crate ethkey; -pub extern crate ethstore; extern crate semver; extern crate ethcore_ipc_nano as nanoipc; extern crate ethcore_devtools as devtools; extern crate rand; extern crate bit_set; +extern crate rlp; -#[cfg(feature = "jit" )] extern crate evmjit; +#[macro_use] +extern crate log; +#[macro_use] +extern crate ethcore_util as util; +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate heapsize; +#[macro_use] +extern crate ethcore_ipc as ipc; + +#[cfg(feature = "jit" )] +extern crate evmjit; + +pub extern crate ethstore; pub mod account_provider; pub mod engines; diff --git a/scripts/targets.sh b/scripts/targets.sh index 5a8d41a3b..009b8ad1d 100644 --- a/scripts/targets.sh +++ b/scripts/targets.sh @@ -2,6 +2,7 @@ export TARGETS=" -p bigint\ + -p rlp\ -p ethash \ -p ethcore \ -p ethcore-dapps \ diff --git a/util/Cargo.toml b/util/Cargo.toml index 719e4c255..106251a3f 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -18,7 +18,8 @@ rocksdb = { git = "https://github.com/ethcore/rust-rocksdb" } lazy_static = "0.2" eth-secp256k1 = { git = "https://github.com/ethcore/rust-secp256k1" } rust-crypto = "0.2.34" -elastic-array = { git = "https://github.com/ethcore/elastic-array" } +elastic-array = "0.5" +rlp = { path = "rlp" } heapsize = { version = "0.3", features = ["unstable"] } itertools = "0.4" sha3 = { path = "sha3" } diff --git a/util/rlp/Cargo.toml b/util/rlp/Cargo.toml new file mode 100644 index 000000000..eba3a2842 --- /dev/null +++ b/util/rlp/Cargo.toml @@ -0,0 +1,12 @@ +[package] +description = "Recursive-length prefix encoding, decoding, and compression" +license = "GPL-3.0" +name = "rlp" +version = "0.1.0" +authors = ["Ethcore "] + +[dependencies] +elastic-array = "0.5" +bigint = { path = "../bigint" } +lazy_static = "0.2" +rustc-serialize = "0.3" \ No newline at end of file diff --git a/util/src/rlp/bytes.rs b/util/rlp/src/bytes.rs similarity index 99% rename from util/src/rlp/bytes.rs rename to util/rlp/src/bytes.rs index 9b98be6c6..5940d21d2 100644 --- a/util/src/rlp/bytes.rs +++ b/util/rlp/src/bytes.rs @@ -22,7 +22,7 @@ use std::fmt; use std::cmp::Ordering; use std::error::Error as StdError; use bigint::uint::{Uint, U128, U256}; -use hash::{H64, H128, H160, H256, H512, H520, H2048}; +use bigint::hash::{H64, H128, H160, H256, H512, H520, H2048}; use elastic_array::*; /// Vector like object diff --git a/util/src/rlp/commonrlps.rs b/util/rlp/src/commonrlps.rs similarity index 76% rename from util/src/rlp/commonrlps.rs rename to util/rlp/src/commonrlps.rs index 856c2fc3a..239117245 100644 --- a/util/src/rlp/commonrlps.rs +++ b/util/rlp/src/commonrlps.rs @@ -16,7 +16,7 @@ //! Contains RLPs used for compression. -use rlp::rlpcompression::InvalidRlpSwapper; +use rlpcompression::InvalidRlpSwapper; lazy_static! { /// Swapper for snapshot compression. @@ -47,60 +47,4 @@ static COMMON_RLPS: &'static [&'static [u8]] = &[ &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; -static INVALID_RLPS: &'static [&'static [u8]] = &[&[0x81, 0x0], &[0x81, 0x1], &[0x81, 0x2], &[0x81, 0x3], &[0x81, 0x4], &[0x81, 0x5], &[0x81, 0x6], &[0x81, 0x7], &[0x81, 0x8], &[0x81, 0x9], &[0x81, 0xa], &[0x81, 0xb], &[0x81, 0xc], &[0x81, 0xd], &[0x81, 0xe], &[0x81, 0xf], &[0x81, 0x10], &[0x81, 0x11], &[0x81, 0x12], &[0x81, 0x13], &[0x81, 0x14], &[0x81, 0x15], &[0x81, 0x16], &[0x81, 0x17], &[0x81, 0x18], &[0x81, 0x19], &[0x81, 0x1a], &[0x81, 0x1b], &[0x81, 0x1c], &[0x81, 0x1d], &[0x81, 0x1e], &[0x81, 0x1f], &[0x81, 0x20], &[0x81, 0x21], &[0x81, 0x22], &[0x81, 0x23], &[0x81, 0x24], &[0x81, 0x25], &[0x81, 0x26], &[0x81, 0x27], &[0x81, 0x28], &[0x81, 0x29], &[0x81, 0x2a], &[0x81, 0x2b], &[0x81, 0x2c], &[0x81, 0x2d], &[0x81, 0x2e], &[0x81, 0x2f], &[0x81, 0x30], &[0x81, 0x31], &[0x81, 0x32], &[0x81, 0x33], &[0x81, 0x34], &[0x81, 0x35], &[0x81, 0x36], &[0x81, 0x37], &[0x81, 0x38], &[0x81, 0x39], &[0x81, 0x3a], &[0x81, 0x3b], &[0x81, 0x3c], &[0x81, 0x3d], &[0x81, 0x3e], &[0x81, 0x3f], &[0x81, 0x40], &[0x81, 0x41], &[0x81, 0x42], &[0x81, 0x43], &[0x81, 0x44], &[0x81, 0x45], &[0x81, 0x46], &[0x81, 0x47], &[0x81, 0x48], &[0x81, 0x49], &[0x81, 0x4a], &[0x81, 0x4b], &[0x81, 0x4c], &[0x81, 0x4d], &[0x81, 0x4e], &[0x81, 0x4f], &[0x81, 0x50], &[0x81, 0x51], &[0x81, 0x52], &[0x81, 0x53], &[0x81, 0x54], &[0x81, 0x55], &[0x81, 0x56], &[0x81, 0x57], &[0x81, 0x58], &[0x81, 0x59], &[0x81, 0x5a], &[0x81, 0x5b], &[0x81, 0x5c], &[0x81, 0x5d], &[0x81, 0x5e], &[0x81, 0x5f], &[0x81, 0x60], &[0x81, 0x61], &[0x81, 0x62], &[0x81, 0x63], &[0x81, 0x64], &[0x81, 0x65], &[0x81, 0x66], &[0x81, 0x67], &[0x81, 0x68], &[0x81, 0x69], &[0x81, 0x6a], &[0x81, 0x6b], &[0x81, 0x6c], &[0x81, 0x6d], &[0x81, 0x6e], &[0x81, 0x6f], &[0x81, 0x70], &[0x81, 0x71], &[0x81, 0x72], &[0x81, 0x73], &[0x81, 0x74], &[0x81, 0x75], &[0x81, 0x76], &[0x81, 0x77], &[0x81, 0x78], &[0x81, 0x79], &[0x81, 0x7a], &[0x81, 0x7b], &[0x81, 0x7c], &[0x81, 0x7d], &[0x81, 0x7e]]; - -#[cfg(test)] -mod tests { - #[test] - #[ignore] - fn analyze_db() { - use rlp::{UntrustedRlp, View}; - use std::collections::HashMap; - use kvdb::*; - - let path = "db path".to_string(); - let values: Vec<_> = Database::open_default(&path).unwrap().iter(Some(2)).map(|(_, v)| v).collect(); - let mut rlp_counts: HashMap<_, u32> = HashMap::new(); - let mut rlp_sizes: HashMap<_, u32> = HashMap::new(); - - fn flat_rlp<'a>(acc: &mut Vec>, rlp: UntrustedRlp<'a>) { - match rlp.is_data() { - true => if rlp.size()>=70 { - match rlp.data() { - Ok(x) => flat_rlp(acc, UntrustedRlp::new(x)), - _ => acc.push(rlp), - } - } else { - acc.push(rlp); - }, - false => for r in rlp.iter() { flat_rlp(acc, r); }, - } - } - - fn space_saving(bytes: &[u8]) -> u32 { - let l = bytes.len() as u32; - match l >= 2 { - true => l-2, - false => 0, - } - } - - for v in &values { - let rlp = UntrustedRlp::new(v); - let mut flat = Vec::new(); - flat_rlp(&mut flat, rlp); - for r in &flat { - *rlp_counts.entry(r.as_raw()).or_insert(0) += 1; - *rlp_sizes.entry(r.as_raw()).or_insert(0) += space_saving(r.as_raw()); - } - } - let mut size_vec: Vec<_> = rlp_sizes.iter().collect(); - size_vec.sort_by(|a, b| b.1.cmp(a.1)); - - // Exclude rare large RLPs. - for v in size_vec.iter().filter(|v| rlp_counts.get(v.0).unwrap()>&100).take(20) { - println!("{:?}, {:?}", v, rlp_counts.get(v.0).unwrap()); - } - println!("DONE"); - } -} +static INVALID_RLPS: &'static [&'static [u8]] = &[&[0x81, 0x0], &[0x81, 0x1], &[0x81, 0x2], &[0x81, 0x3], &[0x81, 0x4], &[0x81, 0x5], &[0x81, 0x6], &[0x81, 0x7], &[0x81, 0x8], &[0x81, 0x9], &[0x81, 0xa], &[0x81, 0xb], &[0x81, 0xc], &[0x81, 0xd], &[0x81, 0xe], &[0x81, 0xf], &[0x81, 0x10], &[0x81, 0x11], &[0x81, 0x12], &[0x81, 0x13], &[0x81, 0x14], &[0x81, 0x15], &[0x81, 0x16], &[0x81, 0x17], &[0x81, 0x18], &[0x81, 0x19], &[0x81, 0x1a], &[0x81, 0x1b], &[0x81, 0x1c], &[0x81, 0x1d], &[0x81, 0x1e], &[0x81, 0x1f], &[0x81, 0x20], &[0x81, 0x21], &[0x81, 0x22], &[0x81, 0x23], &[0x81, 0x24], &[0x81, 0x25], &[0x81, 0x26], &[0x81, 0x27], &[0x81, 0x28], &[0x81, 0x29], &[0x81, 0x2a], &[0x81, 0x2b], &[0x81, 0x2c], &[0x81, 0x2d], &[0x81, 0x2e], &[0x81, 0x2f], &[0x81, 0x30], &[0x81, 0x31], &[0x81, 0x32], &[0x81, 0x33], &[0x81, 0x34], &[0x81, 0x35], &[0x81, 0x36], &[0x81, 0x37], &[0x81, 0x38], &[0x81, 0x39], &[0x81, 0x3a], &[0x81, 0x3b], &[0x81, 0x3c], &[0x81, 0x3d], &[0x81, 0x3e], &[0x81, 0x3f], &[0x81, 0x40], &[0x81, 0x41], &[0x81, 0x42], &[0x81, 0x43], &[0x81, 0x44], &[0x81, 0x45], &[0x81, 0x46], &[0x81, 0x47], &[0x81, 0x48], &[0x81, 0x49], &[0x81, 0x4a], &[0x81, 0x4b], &[0x81, 0x4c], &[0x81, 0x4d], &[0x81, 0x4e], &[0x81, 0x4f], &[0x81, 0x50], &[0x81, 0x51], &[0x81, 0x52], &[0x81, 0x53], &[0x81, 0x54], &[0x81, 0x55], &[0x81, 0x56], &[0x81, 0x57], &[0x81, 0x58], &[0x81, 0x59], &[0x81, 0x5a], &[0x81, 0x5b], &[0x81, 0x5c], &[0x81, 0x5d], &[0x81, 0x5e], &[0x81, 0x5f], &[0x81, 0x60], &[0x81, 0x61], &[0x81, 0x62], &[0x81, 0x63], &[0x81, 0x64], &[0x81, 0x65], &[0x81, 0x66], &[0x81, 0x67], &[0x81, 0x68], &[0x81, 0x69], &[0x81, 0x6a], &[0x81, 0x6b], &[0x81, 0x6c], &[0x81, 0x6d], &[0x81, 0x6e], &[0x81, 0x6f], &[0x81, 0x70], &[0x81, 0x71], &[0x81, 0x72], &[0x81, 0x73], &[0x81, 0x74], &[0x81, 0x75], &[0x81, 0x76], &[0x81, 0x77], &[0x81, 0x78], &[0x81, 0x79], &[0x81, 0x7a], &[0x81, 0x7b], &[0x81, 0x7c], &[0x81, 0x7d], &[0x81, 0x7e]]; \ No newline at end of file diff --git a/util/src/rlp/mod.rs b/util/rlp/src/lib.rs similarity index 77% rename from util/src/rlp/mod.rs rename to util/rlp/src/lib.rs index 4dc14c8a3..2cc8c3bd8 100644 --- a/util/src/rlp/mod.rs +++ b/util/rlp/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -//! Rlp serialization module +//! Recursive Length Prefix serialization crate. //! //! Allows encoding, decoding, and view onto rlp-slice //! @@ -64,27 +64,29 @@ pub use self::untrusted_rlp::{UntrustedRlp, UntrustedRlpIterator, PayloadInfo, P pub use self::rlpin::{Rlp, RlpIterator}; pub use self::rlpstream::RlpStream; pub use self::rlpcompression::RlpType; -pub use elastic_array::ElasticArray1024; -use super::hash::H256; + +extern crate bigint; +extern crate elastic_array; +extern crate rustc_serialize; + +#[macro_use] +extern crate lazy_static; + +use elastic_array::ElasticArray1024; /// The RLP encoded empty data (used to mean "null value"). pub const NULL_RLP: [u8; 1] = [0x80; 1]; /// The RLP encoded empty list. pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1]; -/// The SHA3 of the RLP encoding of empty data. -pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] ); -/// The SHA3 of the RLP encoding of empty list. -pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] ); /// Shortcut function to decode trusted rlp /// /// ```rust -/// extern crate ethcore_util as util; -/// use util::rlp::*; +/// extern crate rlp; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; -/// let animals: Vec = decode(&data); +/// let animals: Vec = rlp::decode(&data); /// assert_eq!(animals, vec!["cat".to_string(), "dog".to_string()]); /// } /// ``` @@ -96,12 +98,11 @@ pub fn decode(bytes: &[u8]) -> T where T: RlpDecodable { /// Shortcut function to encode structure into rlp. /// /// ```rust -/// extern crate ethcore_util as util; -/// use util::rlp::*; +/// extern crate rlp; /// /// fn main () { /// let animal = "cat"; -/// let out = encode(&animal).to_vec(); +/// let out = rlp::encode(&animal).to_vec(); /// assert_eq!(out, vec![0x83, b'c', b'a', b't']); /// } /// ``` diff --git a/util/src/rlp/rlpcompression.rs b/util/rlp/src/rlpcompression.rs similarity index 91% rename from util/src/rlp/rlpcompression.rs rename to util/rlp/src/rlpcompression.rs index 64f2aac39..3113d8b83 100644 --- a/util/src/rlp/rlpcompression.rs +++ b/util/rlp/src/rlpcompression.rs @@ -14,9 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use rlp::{UntrustedRlp, View, Compressible, encode, ElasticArray1024, Stream, RlpStream}; -use rlp::commonrlps::{BLOCKS_RLP_SWAPPER, SNAPSHOT_RLP_SWAPPER}; +use ::{UntrustedRlp, View, Compressible, encode, Stream, RlpStream}; +use commonrlps::{BLOCKS_RLP_SWAPPER, SNAPSHOT_RLP_SWAPPER}; + use std::collections::HashMap; +use elastic_array::ElasticArray1024; /// Stores RLPs used for compression pub struct InvalidRlpSwapper<'a> { @@ -169,8 +171,8 @@ impl<'a> Compressible for UntrustedRlp<'a> { #[cfg(test)] mod tests { - use rlp::{UntrustedRlp, Compressible, View, RlpType}; - use rlp::rlpcompression::InvalidRlpSwapper; + use ::{UntrustedRlp, Compressible, View, RlpType}; + use rlpcompression::InvalidRlpSwapper; #[test] fn invalid_rlp_swapper() { @@ -222,24 +224,4 @@ mod tests { let malformed_rlp = UntrustedRlp::new(&malformed); assert_eq!(malformed_rlp.decompress(RlpType::Blocks).to_vec(), malformed); } - - #[test] - #[ignore] - fn test_compression() { - use kvdb::*; - let path = "db to test".to_string(); - let values: Vec<_> = Database::open_default(&path).unwrap().iter(Some(2)).map(|(_, v)| v).collect(); - let mut decomp_size = 0; - let mut comp_size = 0; - - for v in &values { - let rlp = UntrustedRlp::new(v); - let compressed = rlp.compress(RlpType::Blocks).to_vec(); - comp_size += compressed.len(); - let decompressed = rlp.decompress(RlpType::Blocks).to_vec(); - decomp_size += decompressed.len(); - } - println!("Decompressed bytes {:?}, compressed bytes: {:?}", decomp_size, comp_size); - assert!(decomp_size > comp_size); - } } diff --git a/util/src/rlp/rlperrors.rs b/util/rlp/src/rlperrors.rs similarity index 98% rename from util/src/rlp/rlperrors.rs rename to util/rlp/src/rlperrors.rs index 93a82187e..dc60afc9a 100644 --- a/util/src/rlp/rlperrors.rs +++ b/util/rlp/src/rlperrors.rs @@ -16,7 +16,7 @@ use std::fmt; use std::error::Error as StdError; -use rlp::bytes::FromBytesError; +use bytes::FromBytesError; #[derive(Debug, PartialEq, Eq)] /// Error concerning the RLP decoder. diff --git a/util/src/rlp/rlpin.rs b/util/rlp/src/rlpin.rs similarity index 91% rename from util/src/rlp/rlpin.rs rename to util/rlp/src/rlpin.rs index fb8395c0c..a096cdbe8 100644 --- a/util/src/rlp/rlpin.rs +++ b/util/rlp/src/rlpin.rs @@ -16,7 +16,7 @@ use std::fmt; use rustc_serialize::hex::ToHex; -use rlp::{View, DecoderError, UntrustedRlp, PayloadInfo, Prototype, RlpDecodable}; +use ::{View, DecoderError, UntrustedRlp, PayloadInfo, Prototype, RlpDecodable}; impl<'a> From> for Rlp<'a> { fn from(rlp: UntrustedRlp<'a>) -> Rlp<'a> { @@ -162,8 +162,10 @@ impl<'a, 'view> Iterator for RlpIterator<'a, 'view> { #[test] fn break_it() { - use common::*; - let h: Bytes = FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap(); + use rustc_serialize::hex::FromHex; + use bigint::uint::U256; + + let h: Vec = FromHex::from_hex("f84d0589010efbef67941f79b2a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap(); let r: Rlp = Rlp::new(&h); let u: U256 = r.val_at(1); assert_eq!(format!("{}", u), "19526463837540678066"); diff --git a/util/src/rlp/rlpstream.rs b/util/rlp/src/rlpstream.rs similarity index 98% rename from util/src/rlp/rlpstream.rs rename to util/rlp/src/rlpstream.rs index eb70a7c13..ec85e8ee5 100644 --- a/util/src/rlp/rlpstream.rs +++ b/util/rlp/src/rlpstream.rs @@ -15,9 +15,10 @@ // along with Parity. If not, see . use elastic_array::*; -use rlp::bytes::{ToBytes, VecLike}; -use rlp::{Stream, Encoder, Encodable}; -use rlp::rlptraits::{ByteEncodable, RlpEncodable}; + +use ::{Stream, Encoder, Encodable}; +use bytes::{ToBytes, VecLike}; +use rlptraits::{ByteEncodable, RlpEncodable}; #[derive(Debug, Copy, Clone)] struct ListInfo { diff --git a/util/src/rlp/rlptraits.rs b/util/rlp/src/rlptraits.rs similarity index 88% rename from util/src/rlp/rlptraits.rs rename to util/rlp/src/rlptraits.rs index 5eca7ca03..0f3fb35d7 100644 --- a/util/src/rlp/rlptraits.rs +++ b/util/rlp/src/rlptraits.rs @@ -15,12 +15,11 @@ // along with Parity. If not, see . //! Common RLP traits -use rlp::bytes::VecLike; -use rlp::{DecoderError, UntrustedRlp}; -use rlp::rlpstream::RlpStream; +use ::{DecoderError, UntrustedRlp}; +use bytes::VecLike; +use rlpstream::RlpStream; + use elastic_array::ElasticArray1024; -use hash::H256; -use sha3::*; /// Type is able to decode RLP. pub trait Decoder: Sized { @@ -65,8 +64,8 @@ pub trait View<'a, 'view>: Sized { /// The raw data of the RLP as slice. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -89,8 +88,8 @@ pub trait View<'a, 'view>: Sized { /// Returns number of RLP items. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -105,8 +104,8 @@ pub trait View<'a, 'view>: Sized { /// Returns the number of bytes in the data, or zero if it isn't data. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -124,8 +123,8 @@ pub trait View<'a, 'view>: Sized { /// slices is faster. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -138,8 +137,8 @@ pub trait View<'a, 'view>: Sized { /// No value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![]; @@ -152,8 +151,8 @@ pub trait View<'a, 'view>: Sized { /// Contains a zero-length string or zero-length list. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc0]; @@ -166,8 +165,8 @@ pub trait View<'a, 'view>: Sized { /// List value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -180,8 +179,8 @@ pub trait View<'a, 'view>: Sized { /// String value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -194,8 +193,8 @@ pub trait View<'a, 'view>: Sized { /// Int value /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc1, 0x10]; @@ -209,8 +208,8 @@ pub trait View<'a, 'view>: Sized { /// Get iterator over rlp-slices /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; @@ -254,9 +253,6 @@ pub trait Encodable { self.rlp_append(&mut s); s.drain() } - - /// Get the hash or RLP encoded representation - fn rlp_sha3(&self) -> H256 { (&*self.rlp_bytes()).sha3() } } /// Encodable wrapper trait required to handle special case of encoding a &[u8] as string and not as list @@ -277,8 +273,8 @@ pub trait Stream: Sized { /// Apends value to the end of stream, chainable. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); @@ -292,8 +288,8 @@ pub trait Stream: Sized { /// Declare appending the list of given size, chainable. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); @@ -308,8 +304,8 @@ pub trait Stream: Sized { /// Apends null to the end of stream, chainable. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); @@ -326,8 +322,8 @@ pub trait Stream: Sized { /// Clear the output stream so far. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(3); @@ -342,8 +338,8 @@ pub trait Stream: Sized { /// Returns true if stream doesnt expect any more items. /// /// ```rust - /// extern crate ethcore_util as util; - /// use util::rlp::*; + /// extern crate rlp; + /// use rlp::*; /// /// fn main () { /// let mut stream = RlpStream::new_list(2); diff --git a/util/src/rlp/tests.rs b/util/rlp/src/tests.rs similarity index 92% rename from util/src/rlp/tests.rs rename to util/rlp/src/tests.rs index 5cb8094c6..1d98e4972 100644 --- a/util/src/rlp/tests.rs +++ b/util/rlp/src/tests.rs @@ -16,8 +16,7 @@ use std::{fmt, cmp}; use std::str::FromStr; -use rlp; -use rlp::{UntrustedRlp, RlpStream, View, Stream, DecoderError}; +use ::{Encodable, RlpDecodable, UntrustedRlp, RlpStream, View, Stream, DecoderError}; use bigint::uint::U256; #[test] @@ -26,26 +25,22 @@ fn rlp_at() { { let rlp = UntrustedRlp::new(&data); assert!(rlp.is_list()); - //let animals = as rlp::RlpDecodable>::decode_untrusted(&rlp).unwrap(); let animals: Vec = rlp.as_val().unwrap(); assert_eq!(animals, vec!["cat".to_owned(), "dog".to_owned()]); let cat = rlp.at(0).unwrap(); assert!(cat.is_data()); assert_eq!(cat.as_raw(), &[0x83, b'c', b'a', b't']); - //assert_eq!(String::decode_untrusted(&cat).unwrap(), "cat".to_owned()); assert_eq!(cat.as_val::().unwrap(), "cat".to_owned()); let dog = rlp.at(1).unwrap(); assert!(dog.is_data()); assert_eq!(dog.as_raw(), &[0x83, b'd', b'o', b'g']); - //assert_eq!(String::decode_untrusted(&dog).unwrap(), "dog".to_owned()); assert_eq!(dog.as_val::().unwrap(), "dog".to_owned()); let cat_again = rlp.at(0).unwrap(); assert!(cat_again.is_data()); assert_eq!(cat_again.as_raw(), &[0x83, b'c', b'a', b't']); - //assert_eq!(String::decode_untrusted(&cat_again).unwrap(), "cat".to_owned()); assert_eq!(cat_again.as_val::().unwrap(), "cat".to_owned()); } } @@ -58,10 +53,10 @@ fn rlp_at_err() { assert!(rlp.is_list()); let cat_err = rlp.at(0).unwrap_err(); - assert_eq!(cat_err, rlp::DecoderError::RlpIsTooShort); + assert_eq!(cat_err, DecoderError::RlpIsTooShort); let dog_err = rlp.at(1).unwrap_err(); - assert_eq!(dog_err, rlp::DecoderError::RlpIsTooShort); + assert_eq!(dog_err, DecoderError::RlpIsTooShort); } } @@ -89,13 +84,13 @@ fn rlp_iter() { } } -struct ETestPair(T, Vec) where T: rlp::Encodable; +struct ETestPair(T, Vec) where T: Encodable; fn run_encode_tests(tests: Vec>) - where T: rlp::Encodable + where T: Encodable { for t in &tests { - let res = rlp::encode(&t.0); + let res = super::encode(&t.0); assert_eq!(&res[..], &t.1[..]); } } @@ -165,7 +160,7 @@ fn encode_str() { #[test] fn encode_address() { - use hash::*; + use bigint::hash::H160; let tests = vec![ ETestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"), @@ -206,11 +201,11 @@ fn encode_vector_str() { run_encode_tests(tests); } -struct DTestPair(T, Vec) where T: rlp::RlpDecodable + fmt::Debug + cmp::Eq; +struct DTestPair(T, Vec) where T: RlpDecodable + fmt::Debug + cmp::Eq; -fn run_decode_tests(tests: Vec>) where T: rlp::RlpDecodable + fmt::Debug + cmp::Eq { +fn run_decode_tests(tests: Vec>) where T: RlpDecodable + fmt::Debug + cmp::Eq { for t in &tests { - let res: T = rlp::decode(&t.1); + let res: T = super::decode(&t.1); assert_eq!(res, t.0); } } @@ -301,7 +296,7 @@ fn decode_untrusted_str() { #[test] fn decode_untrusted_address() { - use hash::*; + use bigint::hash::H160; let tests = vec![ DTestPair(H160::from("ef2d6d194084c2de36e0dabfce45d046b37d1106"), @@ -340,8 +335,8 @@ fn decode_untrusted_vector_of_vectors_str() { #[test] fn test_decoding_array() { let v = vec![5u16, 2u16]; - let res = rlp::encode(&v); - let arr: [u16; 2] = rlp::decode(&res); + let res = super::encode(&v); + let arr: [u16; 2] = super::decode(&res); assert_eq!(arr[0], 5); assert_eq!(arr[1], 2); } diff --git a/util/src/rlp/untrusted_rlp.rs b/util/rlp/src/untrusted_rlp.rs similarity index 98% rename from util/src/rlp/untrusted_rlp.rs rename to util/rlp/src/untrusted_rlp.rs index fdf584211..8f954e3e6 100644 --- a/util/src/rlp/untrusted_rlp.rs +++ b/util/rlp/src/untrusted_rlp.rs @@ -17,8 +17,9 @@ use std::cell::Cell; use std::fmt; use rustc_serialize::hex::ToHex; -use rlp::bytes::{FromBytes, FromBytesResult, FromBytesError}; -use rlp::{View, Decoder, Decodable, DecoderError, RlpDecodable}; + +use bytes::{FromBytes, FromBytesResult, FromBytesError}; +use ::{View, Decoder, Decodable, DecoderError, RlpDecodable}; /// rlp offset #[derive(Copy, Clone, Debug)] @@ -490,7 +491,7 @@ impl RlpDecodable for u8 { #[cfg(test)] mod tests { - use rlp::{UntrustedRlp, View}; + use ::{UntrustedRlp, View}; #[test] fn test_rlp_display() { use rustc_serialize::hex::FromHex; diff --git a/util/src/lib.rs b/util/src/lib.rs index f7459615e..57ea9c152 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -90,29 +90,32 @@ extern crate rustc_serialize; extern crate rand; extern crate rocksdb; -#[macro_use] -extern crate heapsize; -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate itertools; extern crate env_logger; extern crate crypto as rcrypto; extern crate secp256k1; extern crate arrayvec; extern crate elastic_array; -#[macro_use] -extern crate log as rlog; extern crate time; extern crate ethcore_devtools as devtools; extern crate libc; extern crate target_info; extern crate bigint; extern crate parking_lot; -pub extern crate using_queue; -pub extern crate table; extern crate ansi_term; extern crate tiny_keccak; +extern crate rlp; + +#[macro_use] +extern crate heapsize; +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate itertools; +#[macro_use] +extern crate log as rlog; + +pub extern crate using_queue; +pub extern crate table; pub mod bloom; pub mod standard; @@ -122,7 +125,6 @@ pub mod from_json; pub mod common; pub mod error; pub mod bytes; -pub mod rlp; pub mod misc; pub mod vector; pub mod sha3; @@ -144,7 +146,6 @@ mod timer; pub use common::*; pub use misc::*; -pub use rlp::*; pub use hashdb::*; pub use memorydb::*; pub use overlaydb::*; diff --git a/util/src/sha3.rs b/util/src/sha3.rs index 0dcde2ccb..5ab55257d 100644 --- a/util/src/sha3.rs +++ b/util/src/sha3.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -//! Wrapper around tiny-keccak crate. +//! Wrapper around tiny-keccak crate as well as common hash constants. extern crate sha3 as sha3_ext; use std::io; @@ -27,6 +27,11 @@ use self::sha3_ext::*; /// Get the SHA3 (i.e. Keccak) hash of the empty bytes string. pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] ); +/// The SHA3 of the RLP encoding of empty data. +pub const SHA3_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] ); + +/// The SHA3 of the RLP encoding of empty list. +pub const SHA3_EMPTY_LIST_RLP: H256 = H256( [0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47] ); /// Types implementing this trait are sha3able. /// diff --git a/util/src/trie/mod.rs b/util/src/trie/mod.rs index 886ccd724..b71f6a5e2 100644 --- a/util/src/trie/mod.rs +++ b/util/src/trie/mod.rs @@ -84,7 +84,7 @@ pub trait Trie { fn root(&self) -> &H256; /// Is the trie empty? - fn is_empty(&self) -> bool { *self.root() == ::rlp::SHA3_NULL_RLP } + fn is_empty(&self) -> bool { *self.root() == ::sha3::SHA3_NULL_RLP } /// Does the trie contain a given key? fn contains(&self, key: &[u8]) -> Result { diff --git a/util/src/trie/triedb.rs b/util/src/trie/triedb.rs index 8e6cb98fa..88a3399e7 100644 --- a/util/src/trie/triedb.rs +++ b/util/src/trie/triedb.rs @@ -32,11 +32,11 @@ use super::{Trie, TrieItem, TrieError}; /// # Example /// ``` /// extern crate ethcore_util as util; +/// /// use util::trie::*; /// use util::hashdb::*; /// use util::memorydb::*; /// use util::hash::*; -/// use util::rlp::*; /// /// fn main() { /// let mut memdb = MemoryDB::new(); diff --git a/util/src/trie/triedbmut.rs b/util/src/trie/triedbmut.rs index 8d3d7d3d3..5e2ef6b79 100644 --- a/util/src/trie/triedbmut.rs +++ b/util/src/trie/triedbmut.rs @@ -19,10 +19,11 @@ use super::{TrieError, TrieMut}; use super::node::Node as RlpNode; -use ::{Bytes, HashDB, H256, SHA3_NULL_RLP}; +use ::{Bytes, HashDB, H256}; use ::bytes::ToPretty; use ::nibbleslice::NibbleSlice; use ::rlp::{Rlp, RlpStream, View, Stream}; +use ::sha3::SHA3_NULL_RLP; use elastic_array::ElasticArray1024; @@ -261,18 +262,18 @@ impl<'a> Index<&'a StorageHandle> for NodeStorage { /// # Example /// ``` /// extern crate ethcore_util as util; +/// /// use util::trie::*; /// use util::hashdb::*; /// use util::memorydb::*; /// use util::hash::*; -/// use util::rlp::*; /// /// fn main() { /// let mut memdb = MemoryDB::new(); /// let mut root = H256::new(); /// let mut t = TrieDBMut::new(&mut memdb, &mut root); /// assert!(t.is_empty()); -/// assert_eq!(*t.root(), SHA3_NULL_RLP); +/// assert_eq!(*t.root(), ::util::sha3::SHA3_NULL_RLP); /// t.insert(b"foo", b"bar").unwrap(); /// assert!(t.contains(b"foo").unwrap()); /// assert_eq!(t.get(b"foo").unwrap().unwrap(), b"bar"); @@ -951,8 +952,8 @@ mod tests { use hashdb::*; use memorydb::*; use super::*; - use rlp::*; use bytes::ToPretty; + use sha3::SHA3_NULL_RLP; use super::super::TrieMut; use super::super::standardmap::*; From 5dd56aa070600f0b591764b822c68c779a0770ad Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 1 Sep 2016 14:29:59 +0200 Subject: [PATCH 4/9] port ethcore --- ethcore/src/account_db.rs | 2 +- ethcore/src/block.rs | 1 + ethcore/src/blockchain/blockchain.rs | 1 + ethcore/src/blockchain/extras.rs | 1 + ethcore/src/blockchain/generator/block.rs | 2 +- ethcore/src/blooms/bloom.rs | 2 +- ethcore/src/blooms/bloom_group.rs | 2 +- ethcore/src/client/client.rs | 11 ++++++----- ethcore/src/client/test_client.rs | 11 ++++++----- ethcore/src/db.rs | 21 +++++++++++---------- ethcore/src/engines/basic_authority.rs | 8 +++++--- ethcore/src/engines/instant_seal.rs | 2 +- ethcore/src/error.rs | 4 ++-- ethcore/src/ethereum/ethash.rs | 8 +++++--- ethcore/src/executive.rs | 2 ++ ethcore/src/header.rs | 9 +++++---- ethcore/src/json_tests/transaction.rs | 1 + ethcore/src/migrations/blocks/v8.rs | 2 +- ethcore/src/migrations/state/v7.rs | 4 +++- ethcore/src/migrations/v9.rs | 2 +- ethcore/src/miner/transaction_queue.rs | 4 +++- ethcore/src/pod_account.rs | 5 +++-- ethcore/src/snapshot/account.rs | 11 ++++++----- ethcore/src/snapshot/block.rs | 6 +++--- ethcore/src/snapshot/error.rs | 2 +- ethcore/src/snapshot/io.rs | 2 +- ethcore/src/snapshot/mod.rs | 4 ++-- ethcore/src/snapshot/tests/helpers.rs | 2 +- ethcore/src/spec/genesis.rs | 2 +- ethcore/src/spec/seal.rs | 2 +- ethcore/src/spec/spec.rs | 1 + ethcore/src/state/account.rs | 2 ++ ethcore/src/tests/client.rs | 1 + ethcore/src/tests/helpers.rs | 3 ++- ethcore/src/trace/bloom.rs | 2 +- ethcore/src/types/executed.rs | 5 +++-- ethcore/src/types/log_entry.rs | 3 ++- ethcore/src/types/receipt.rs | 3 ++- ethcore/src/types/trace_types/flat.rs | 7 +++---- ethcore/src/types/trace_types/trace.rs | 3 ++- ethcore/src/types/transaction.rs | 4 ++-- ethcore/src/verification/verification.rs | 6 +++++- ethcore/src/views/block.rs | 1 + ethcore/src/views/body.rs | 1 + ethcore/src/views/header.rs | 3 ++- ethcore/src/views/transaction.rs | 3 ++- util/src/sha3.rs | 6 +++--- 47 files changed, 113 insertions(+), 77 deletions(-) diff --git a/ethcore/src/account_db.rs b/ethcore/src/account_db.rs index 32c7acbb7..a02b670d0 100644 --- a/ethcore/src/account_db.rs +++ b/ethcore/src/account_db.rs @@ -16,7 +16,7 @@ //! DB backend wrapper for Account trie use util::*; -use util::sha3::SHA3_NULL_RLP; +use rlp::NULL_RLP; static NULL_RLP_STATIC: [u8; 1] = [0x80; 1]; diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index d3b4ac168..0b42d6838 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -22,6 +22,7 @@ use state::*; use verification::PreverifiedBlock; use trace::FlatTrace; use factory::Factories; +use rlp::*; /// A block, encoded as it is on the block chain. #[derive(Default, Debug, Clone, PartialEq)] diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index 943dc04c5..ef26a6b08 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -18,6 +18,7 @@ use bloomchain as bc; use util::*; +use rlp::*; use header::*; use super::extras::*; use transaction::*; diff --git a/ethcore/src/blockchain/extras.rs b/ethcore/src/blockchain/extras.rs index e2b4e89b3..6dc7aef98 100644 --- a/ethcore/src/blockchain/extras.rs +++ b/ethcore/src/blockchain/extras.rs @@ -18,6 +18,7 @@ use bloomchain; use util::*; +use rlp::*; use header::BlockNumber; use receipt::Receipt; use db::Key; diff --git a/ethcore/src/blockchain/generator/block.rs b/ethcore/src/blockchain/generator/block.rs index 1fb9c13a1..6b8aa78f2 100644 --- a/ethcore/src/blockchain/generator/block.rs +++ b/ethcore/src/blockchain/generator/block.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use util::rlp::*; +use rlp::*; use util::{H256, H2048}; use util::bytes::Bytes; use header::Header; diff --git a/ethcore/src/blooms/bloom.rs b/ethcore/src/blooms/bloom.rs index 83d898ab5..b535bcb21 100644 --- a/ethcore/src/blooms/bloom.rs +++ b/ethcore/src/blooms/bloom.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use bloomchain as bc; -use util::rlp::*; +use rlp::*; use util::HeapSizeOf; use basic_types::LogBloom; diff --git a/ethcore/src/blooms/bloom_group.rs b/ethcore/src/blooms/bloom_group.rs index 8e2b87189..9e3b53f18 100644 --- a/ethcore/src/blooms/bloom_group.rs +++ b/ethcore/src/blooms/bloom_group.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use bloomchain::group as bc; -use util::rlp::*; +use rlp::*; use util::HeapSizeOf; use super::Bloom; diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index edd671b70..554afab38 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -22,11 +22,11 @@ use std::time::{Instant}; use time::precise_time_ns; // util -use util::{journaldb, rlp, Bytes, View, PerfTimer, Itertools, Mutex, RwLock}; -use util::journaldb::JournalDB; -use util::rlp::{UntrustedRlp}; +use util::{Bytes, PerfTimer, Itertools, Mutex, RwLock}; +use util::journaldb::{self, JournalDB}; use util::{U256, H256, Address, H2048, Uint}; use util::sha3::*; +use util::TrieFactory; use util::kvdb::*; // other @@ -63,9 +63,10 @@ use trace; use trace::FlatTransactionTraces; use evm::Factory as EvmFactory; use miner::{Miner, MinerService}; -use util::TrieFactory; use snapshot::{self, io as snapshot_io}; use factory::Factories; +use rlp::{View, UntrustedRlp}; + // re-export pub use types::blockchain_info::BlockChainInfo; @@ -877,7 +878,7 @@ impl BlockChainClient for Client { } fn block_receipts(&self, hash: &H256) -> Option { - self.chain.block_receipts(hash).map(|receipts| rlp::encode(&receipts).to_vec()) + self.chain.block_receipts(hash).map(|receipts| ::rlp::encode(&receipts).to_vec()) } fn import_block(&self, bytes: Bytes) -> Result { diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index fb7f9083e..aa45ed985 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -18,6 +18,7 @@ use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder}; use util::*; +use rlp::*; use ethkey::{Generator, Random}; use devtools::*; use transaction::{Transaction, LocalizedTransaction, SignedTransaction, Action}; @@ -204,7 +205,7 @@ impl TestBlockChainClient { txs.append(&signed_tx); txs.out() }, - _ => rlp::EMPTY_LIST_RLP.to_vec() + _ => ::rlp::EMPTY_LIST_RLP.to_vec() }; let mut rlp = RlpStream::new_list(3); @@ -222,8 +223,8 @@ impl TestBlockChainClient { header.set_extra_data(b"This extra data is way too long to be considered valid".to_vec()); let mut rlp = RlpStream::new_list(3); rlp.append(&header); - rlp.append_raw(&rlp::NULL_RLP, 1); - rlp.append_raw(&rlp::NULL_RLP, 1); + rlp.append_raw(&::rlp::NULL_RLP, 1); + rlp.append_raw(&::rlp::NULL_RLP, 1); self.blocks.write().insert(hash, rlp.out()); } @@ -234,8 +235,8 @@ impl TestBlockChainClient { header.set_parent_hash(H256::from(42)); let mut rlp = RlpStream::new_list(3); rlp.append(&header); - rlp.append_raw(&rlp::NULL_RLP, 1); - rlp.append_raw(&rlp::NULL_RLP, 1); + rlp.append_raw(&::rlp::NULL_RLP, 1); + rlp.append_raw(&::rlp::NULL_RLP, 1); self.blocks.write().insert(hash, rlp.out()); } diff --git a/ethcore/src/db.rs b/ethcore/src/db.rs index 7fcf30de3..61cd41bd6 100644 --- a/ethcore/src/db.rs +++ b/ethcore/src/db.rs @@ -20,7 +20,8 @@ use std::ops::Deref; use std::hash::Hash; use std::collections::HashMap; use util::{DBTransaction, Database, RwLock}; -use util::rlp::{encode, Encodable, decode, Decodable}; + +use rlp; // database columns /// Column for State @@ -83,12 +84,12 @@ pub trait Key { /// Should be used to write value into database. pub trait Writable { /// Writes the value into the database. - fn write(&mut self, col: Option, key: &Key, value: &T) where T: Encodable, R: Deref; + fn write(&mut self, col: Option, key: &Key, value: &T) where T: rlp::Encodable, R: Deref; /// Writes the value into the database and updates the cache. fn write_with_cache(&mut self, col: Option, cache: &mut Cache, key: K, value: T, policy: CacheUpdatePolicy) where K: Key + Hash + Eq, - T: Encodable, + T: rlp::Encodable, R: Deref { self.write(col, &key, &value); match policy { @@ -104,7 +105,7 @@ pub trait Writable { /// Writes the values into the database and updates the cache. fn extend_with_cache(&mut self, col: Option, cache: &mut Cache, values: HashMap, policy: CacheUpdatePolicy) where K: Key + Hash + Eq, - T: Encodable, + T: rlp::Encodable, R: Deref { match policy { CacheUpdatePolicy::Overwrite => { @@ -127,13 +128,13 @@ pub trait Writable { pub trait Readable { /// Returns value for given key. fn read(&self, col: Option, key: &Key) -> Option where - T: Decodable, + T: rlp::Decodable, R: Deref; /// Returns value for given key either in cache or in database. fn read_with_cache(&self, col: Option, cache: &RwLock, key: &K) -> Option where K: Key + Eq + Hash + Clone, - T: Clone + Decodable, + T: Clone + rlp::Decodable, C: Cache { { let read = cache.read(); @@ -169,17 +170,17 @@ pub trait Readable { } impl Writable for DBTransaction { - fn write(&mut self, col: Option, key: &Key, value: &T) where T: Encodable, R: Deref { - self.put(col, &key.key(), &encode(value)); + fn write(&mut self, col: Option, key: &Key, value: &T) where T: rlp::Encodable, R: Deref { + self.put(col, &key.key(), &rlp::encode(value)); } } impl Readable for Database { - fn read(&self, col: Option, key: &Key) -> Option where T: Decodable, R: Deref { + fn read(&self, col: Option, key: &Key) -> Option where T: rlp::Decodable, R: Deref { let result = self.get(col, &key.key()); match result { - Ok(option) => option.map(|v| decode(&v)), + Ok(option) => option.map(|v| rlp::decode(&v)), Err(err) => { panic!("db get failed, key: {:?}, err: {:?}", &key.key() as &[u8], err); } diff --git a/ethcore/src/engines/basic_authority.rs b/ethcore/src/engines/basic_authority.rs index 332d947c3..eef3df6b1 100644 --- a/ethcore/src/engines/basic_authority.rs +++ b/ethcore/src/engines/basic_authority.rs @@ -109,7 +109,7 @@ impl Engine for BasicAuthority { let message = header.bare_hash(); // account should be pernamently unlocked, otherwise sealing will fail if let Ok(signature) = ap.sign(*block.header().author(), message) { - return Some(vec![encode(&(&*signature as &[u8])).to_vec()]); + return Some(vec![::rlp::encode(&(&*signature as &[u8])).to_vec()]); } else { trace!(target: "basicauthority", "generate_seal: FAIL: accounts secret key unavailable"); } @@ -131,6 +131,8 @@ impl Engine for BasicAuthority { } fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> result::Result<(), Error> { + use rlp::{UntrustedRlp, View}; + // check the signature is legit. let sig = try!(UntrustedRlp::new(&header.seal()[0]).as_val::()); let signer = public_to_address(&try!(recover(&sig.into(), &header.bare_hash()))); @@ -172,7 +174,7 @@ impl Engine for BasicAuthority { impl Header { /// Get the none field of the header. pub fn signature(&self) -> H520 { - decode(&self.seal()[0]) + ::rlp::decode(&self.seal()[0]) } } @@ -228,7 +230,7 @@ mod tests { fn can_do_signature_verification_fail() { let engine = new_test_authority().engine; let mut header: Header = Header::default(); - header.set_seal(vec![rlp::encode(&H520::default()).to_vec()]); + header.set_seal(vec![::rlp::encode(&H520::default()).to_vec()]); let verify_result = engine.verify_block_unordered(&header, None); assert!(verify_result.is_err()); diff --git a/ethcore/src/engines/instant_seal.rs b/ethcore/src/engines/instant_seal.rs index e98e87bf5..bdb882ee7 100644 --- a/ethcore/src/engines/instant_seal.rs +++ b/ethcore/src/engines/instant_seal.rs @@ -100,7 +100,7 @@ mod tests { assert!(engine.verify_block_basic(&header, None).is_ok()); - header.set_seal(vec![rlp::encode(&H520::default()).to_vec()]); + header.set_seal(vec![::rlp::encode(&H520::default()).to_vec()]); assert!(engine.verify_block_unordered(&header, None).is_ok()); } diff --git a/ethcore/src/error.rs b/ethcore/src/error.rs index 5c26e2f78..cb3a1cf52 100644 --- a/ethcore/src/error.rs +++ b/ethcore/src/error.rs @@ -302,8 +302,8 @@ impl From for Error { } } -impl From for Error { - fn from(err: DecoderError) -> Error { +impl From<::rlp::DecoderError> for Error { + fn from(err: ::rlp::DecoderError) -> Error { Error::Util(UtilError::Decoder(err)) } } diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index 82a74d9ea..4d25f812a 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -21,6 +21,7 @@ use spec::CommonParams; use engines::Engine; use evm::Schedule; use ethjson; +use rlp::{self, UntrustedRlp, View}; /// Ethash params. #[derive(Debug, PartialEq)] @@ -328,17 +329,17 @@ impl Ethash { impl Header { /// Get the none field of the header. pub fn nonce(&self) -> H64 { - decode(&self.seal()[1]) + rlp::decode(&self.seal()[1]) } /// Get the mix hash field of the header. pub fn mix_hash(&self) -> H256 { - decode(&self.seal()[0]) + rlp::decode(&self.seal()[0]) } /// Set the nonce and mix hash fields of the header. pub fn set_nonce_and_mix_hash(&mut self, nonce: &H64, mix_hash: &H256) { - self.set_seal(vec![encode(mix_hash).to_vec(), encode(nonce).to_vec()]); + self.set_seal(vec![rlp::encode(mix_hash).to_vec(), rlp::encode(nonce).to_vec()]); } } @@ -349,6 +350,7 @@ mod tests { use tests::helpers::*; use super::super::new_morden; use super::Ethash; + use rlp; #[test] fn on_close_block() { diff --git a/ethcore/src/executive.rs b/ethcore/src/executive.rs index 53d5460ad..64771f95d 100644 --- a/ethcore/src/executive.rs +++ b/ethcore/src/executive.rs @@ -32,6 +32,8 @@ const MAX_VM_DEPTH_FOR_THREAD: usize = 64; /// Returns new address created from address and given nonce. pub fn contract_address(address: &Address, nonce: &U256) -> Address { + use rlp::{RlpStream, Stream}; + let mut stream = RlpStream::new_list(2); stream.append(address); stream.append(nonce); diff --git a/ethcore/src/header.rs b/ethcore/src/header.rs index 9b0e155f4..04581fac9 100644 --- a/ethcore/src/header.rs +++ b/ethcore/src/header.rs @@ -19,6 +19,7 @@ use util::*; use basic_types::*; use time::get_time; +use rlp::*; use std::cell::RefCell; @@ -297,7 +298,7 @@ impl Encodable for Header { #[cfg(test)] mod tests { use rustc_serialize::hex::FromHex; - use util::rlp::{decode, encode}; + use rlp; use super::Header; #[test] @@ -307,7 +308,7 @@ mod tests { let mix_hash = "a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd".from_hex().unwrap(); let nonce = "88ab4e252a7e8c2a23".from_hex().unwrap(); - let header: Header = decode(&header_rlp); + let header: Header = rlp::decode(&header_rlp); let seal_fields = header.seal; assert_eq!(seal_fields.len(), 2); assert_eq!(seal_fields[0], mix_hash); @@ -319,8 +320,8 @@ mod tests { // that's rlp of block header created with ethash engine. let header_rlp = "f901f9a0d405da4e66f1445d455195229624e133f5baafe72b5cf7b3c36c12c8146e98b7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a088d2ec6b9860aae1a2c3b299f72b6a5d70d7f7ba4722c78f2c49ba96273c2158a007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefba82524d84568e932a80a0a0349d8c3df71f1a48a9df7d03fd5f14aeee7d91332c009ecaff0a71ead405bd88ab4e252a7e8c2a23".from_hex().unwrap(); - let header: Header = decode(&header_rlp); - let encoded_header = encode(&header).to_vec(); + let header: Header = rlp::decode(&header_rlp); + let encoded_header = rlp::encode(&header).to_vec(); assert_eq!(header_rlp, encoded_header); } diff --git a/ethcore/src/json_tests/transaction.rs b/ethcore/src/json_tests/transaction.rs index 7c9a3327e..a06e3b5dc 100644 --- a/ethcore/src/json_tests/transaction.rs +++ b/ethcore/src/json_tests/transaction.rs @@ -17,6 +17,7 @@ use super::test_common::*; use evm; use ethjson; +use rlp::{UntrustedRlp, View}; fn do_json_test(json_data: &[u8]) -> Vec { let tests = ethjson::transaction::Test::load(json_data).unwrap(); diff --git a/ethcore/src/migrations/blocks/v8.rs b/ethcore/src/migrations/blocks/v8.rs index 798be0790..21024f93d 100644 --- a/ethcore/src/migrations/blocks/v8.rs +++ b/ethcore/src/migrations/blocks/v8.rs @@ -17,7 +17,7 @@ //! This migration compresses the state db. use util::migration::{SimpleMigration, Progress}; -use util::rlp::{Compressible, UntrustedRlp, View, RlpType}; +use rlp::{Compressible, UntrustedRlp, View, RlpType}; /// Compressing migration. #[derive(Default)] diff --git a/ethcore/src/migrations/state/v7.rs b/ethcore/src/migrations/state/v7.rs index 5395cec09..9327decef 100644 --- a/ethcore/src/migrations/state/v7.rs +++ b/ethcore/src/migrations/state/v7.rs @@ -23,9 +23,11 @@ use util::Bytes; use util::{Address, FixedHash, H256}; use util::kvdb::Database; use util::migration::{Batch, Config, Error, Migration, SimpleMigration, Progress}; -use util::rlp::{decode, Rlp, RlpStream, Stream, View}; use util::sha3::Hashable; +use rlp::{decode, Rlp, RlpStream, Stream, View}; + + // attempt to migrate a key, value pair. None if migration not possible. fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Option { let val_hash = val.sha3(); diff --git a/ethcore/src/migrations/v9.rs b/ethcore/src/migrations/v9.rs index 0c8e77588..d4070d0c0 100644 --- a/ethcore/src/migrations/v9.rs +++ b/ethcore/src/migrations/v9.rs @@ -17,7 +17,7 @@ //! This migration consolidates all databases into single one using Column Families. -use util::{Rlp, RlpStream, View, Stream}; +use rlp::{Rlp, RlpStream, View, Stream}; use util::kvdb::Database; use util::migration::{Batch, Config, Error, Migration, Progress}; diff --git a/ethcore/src/miner/transaction_queue.rs b/ethcore/src/miner/transaction_queue.rs index 5e610da24..fb00a8aa8 100644 --- a/ethcore/src/miner/transaction_queue.rs +++ b/ethcore/src/miner/transaction_queue.rs @@ -1229,6 +1229,8 @@ mod test { #[test] fn should_reject_incorectly_signed_transaction() { + use rlp::{self, RlpStream, Stream}; + // given let mut txq = TransactionQueue::new(); let tx = new_unsigned_tx(123.into(), 1.into()); @@ -1243,7 +1245,7 @@ mod test { s.append(&0u64); // v s.append(&U256::zero()); // r s.append(&U256::zero()); // s - decode(s.as_raw()) + rlp::decode(s.as_raw()) }; // when let res = txq.add(stx, &default_account_details, TransactionOrigin::External); diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index 797d1a265..2596c5fca 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -19,6 +19,7 @@ use state::Account; use account_db::AccountDBMut; use ethjson; use types::account_diff::*; +use rlp::{self, RlpStream, Stream}; #[derive(Debug, Clone, PartialEq, Eq)] /// An account, expressed as Plain-Old-Data (hence the name). @@ -57,7 +58,7 @@ impl PodAccount { let mut stream = RlpStream::new_list(4); stream.append(&self.nonce); stream.append(&self.balance); - stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), encode(&U256::from(v.as_slice())).to_vec())).collect())); + stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), rlp::encode(&U256::from(v.as_slice())).to_vec())).collect())); stream.append(&self.code.as_ref().unwrap_or(&vec![]).sha3()); stream.out() } @@ -71,7 +72,7 @@ impl PodAccount { let mut r = H256::new(); let mut t = SecTrieDBMut::new(db, &mut r); for (k, v) in &self.storage { - if let Err(e) = t.insert(k, &encode(&U256::from(v.as_slice()))) { + if let Err(e) = t.insert(k, &rlp::encode(&U256::from(v.as_slice()))) { warn!("Encountered potential DB corruption: {}", e); } } diff --git a/ethcore/src/snapshot/account.rs b/ethcore/src/snapshot/account.rs index fcd7b6abc..16c59db2e 100644 --- a/ethcore/src/snapshot/account.rs +++ b/ethcore/src/snapshot/account.rs @@ -17,11 +17,12 @@ //! Account state encoding and decoding use account_db::{AccountDB, AccountDBMut}; -use util::{U256, FixedHash, H256, Bytes, HashDB, SHA3_EMPTY}; -use util::rlp::{Rlp, RlpStream, Stream, UntrustedRlp, View}; -use util::trie::{TrieDB, Trie}; use snapshot::Error; +use util::{U256, FixedHash, H256, Bytes, HashDB, SHA3_EMPTY}; +use util::trie::{TrieDB, Trie}; +use rlp::{Rlp, RlpStream, Stream, UntrustedRlp, View}; + use std::collections::{HashMap, HashSet}; // whether an encoded account has code and how it is referred to. @@ -206,9 +207,9 @@ mod tests { use tests::helpers::get_temp_journal_db; use snapshot::tests::helpers::fill_storage; - use util::{SHA3_NULL_RLP, SHA3_EMPTY}; + use util::sha3::{SHA3_EMPTY, SHA3_NULL_RLP}; use util::{Address, FixedHash, H256, HashDB}; - use util::rlp::{UntrustedRlp, View}; + use rlp::{UntrustedRlp, View}; use std::collections::{HashSet, HashMap}; diff --git a/ethcore/src/snapshot/block.rs b/ethcore/src/snapshot/block.rs index 66fc06066..05b3281c8 100644 --- a/ethcore/src/snapshot/block.rs +++ b/ethcore/src/snapshot/block.rs @@ -20,8 +20,8 @@ use block::Block; use header::Header; use views::BlockView; -use util::rlp::{DecoderError, RlpStream, Stream, UntrustedRlp, View}; -use util::rlp::{Compressible, RlpType}; +use rlp::{DecoderError, RlpStream, Stream, UntrustedRlp, View}; +use rlp::{Compressible, RlpType}; use util::{Bytes, Hashable, H256}; const HEADER_FIELDS: usize = 10; @@ -103,7 +103,7 @@ impl AbridgedBlock { header.set_gas_used(try!(rlp.val_at(7))); header.set_timestamp(try!(rlp.val_at(8))); header.set_extra_data(try!(rlp.val_at(9))); - + let transactions = try!(rlp.val_at(10)); let uncles: Vec
= try!(rlp.val_at(11)); diff --git a/ethcore/src/snapshot/error.rs b/ethcore/src/snapshot/error.rs index d4587fdba..acd9409f7 100644 --- a/ethcore/src/snapshot/error.rs +++ b/ethcore/src/snapshot/error.rs @@ -22,7 +22,7 @@ use ids::BlockID; use util::H256; use util::trie::TrieError; -use util::rlp::DecoderError; +use rlp::DecoderError; /// Snapshot-related errors. #[derive(Debug)] diff --git a/ethcore/src/snapshot/io.rs b/ethcore/src/snapshot/io.rs index 7179b97ef..ae8a83b21 100644 --- a/ethcore/src/snapshot/io.rs +++ b/ethcore/src/snapshot/io.rs @@ -27,7 +27,7 @@ use std::path::{Path, PathBuf}; use util::Bytes; use util::hash::H256; -use util::rlp::{self, Encodable, RlpStream, UntrustedRlp, Stream, View}; +use rlp::{self, Encodable, RlpStream, UntrustedRlp, Stream, View}; use super::ManifestData; diff --git a/ethcore/src/snapshot/mod.rs b/ethcore/src/snapshot/mod.rs index d1ad077fe..89e4ed8ba 100644 --- a/ethcore/src/snapshot/mod.rs +++ b/ethcore/src/snapshot/mod.rs @@ -32,9 +32,9 @@ use util::Mutex; use util::hash::{FixedHash, H256}; use util::journaldb::{self, Algorithm, JournalDB}; use util::kvdb::Database; -use util::rlp::{DecoderError, RlpStream, Stream, UntrustedRlp, View, Compressible, RlpType}; -use util::rlp::SHA3_NULL_RLP; +use util::sha3::SHA3_NULL_RLP; use util::trie::{TrieDB, TrieDBMut, Trie, TrieMut}; +use rlp::{DecoderError, RlpStream, Stream, UntrustedRlp, View, Compressible, RlpType}; use self::account::Account; use self::block::AbridgedBlock; diff --git a/ethcore/src/snapshot/tests/helpers.rs b/ethcore/src/snapshot/tests/helpers.rs index aa055e8fb..bdf6a6a6a 100644 --- a/ethcore/src/snapshot/tests/helpers.rs +++ b/ethcore/src/snapshot/tests/helpers.rs @@ -25,7 +25,7 @@ use util::hash::{FixedHash, H256}; use util::hashdb::HashDB; use util::trie::{Alphabet, StandardMap, SecTrieDBMut, TrieMut, ValueMode}; use util::trie::{TrieDB, TrieDBMut, Trie}; -use util::rlp::SHA3_NULL_RLP; +use util::sha3::SHA3_NULL_RLP; // the proportion of accounts we will alter each tick. const ACCOUNT_CHURN: f32 = 0.01; diff --git a/ethcore/src/spec/genesis.rs b/ethcore/src/spec/genesis.rs index 8f472e996..380c48aba 100644 --- a/ethcore/src/spec/genesis.rs +++ b/ethcore/src/spec/genesis.rs @@ -14,8 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use util::rlp::*; use util::{Address, H256, Uint, U256}; +use util::sha3::SHA3_NULL_RLP; use ethjson; use super::seal::Seal; diff --git a/ethcore/src/spec/seal.rs b/ethcore/src/spec/seal.rs index 600de701a..e25f174d2 100644 --- a/ethcore/src/spec/seal.rs +++ b/ethcore/src/spec/seal.rs @@ -16,7 +16,7 @@ //! Spec seal. -use util::rlp::*; +use rlp::*; use util::hash::{H64, H256}; use ethjson; diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 7a03a2acd..82cede922 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -24,6 +24,7 @@ use super::genesis::Genesis; use super::seal::Generic as GenericSeal; use ethereum; use ethjson; +use rlp::{Rlp, RlpStream, View, Stream}; /// Parameters common to all engines. #[derive(Debug, PartialEq, Clone)] diff --git a/ethcore/src/state/account.rs b/ethcore/src/state/account.rs index 72e2bb0ed..eb4bea833 100644 --- a/ethcore/src/state/account.rs +++ b/ethcore/src/state/account.rs @@ -19,6 +19,7 @@ use std::collections::hash_map::Entry; use util::*; use pod_account::*; +use rlp::*; use std::cell::{Ref, RefCell, Cell}; @@ -333,6 +334,7 @@ mod tests { use util::*; use super::*; use account_db::*; + use rlp::*; #[test] fn account_compress() { diff --git a/ethcore/src/tests/client.rs b/ethcore/src/tests/client.rs index 26256a760..99aae1078 100644 --- a/ethcore/src/tests/client.rs +++ b/ethcore/src/tests/client.rs @@ -22,6 +22,7 @@ use tests::helpers::*; use common::*; use devtools::*; use miner::Miner; +use rlp::{Rlp, View}; #[test] fn imports_from_empty() { diff --git a/ethcore/src/tests/helpers.rs b/ethcore/src/tests/helpers.rs index f3975a0d0..e05c82c55 100644 --- a/ethcore/src/tests/helpers.rs +++ b/ethcore/src/tests/helpers.rs @@ -27,6 +27,7 @@ use engines::Engine; use ethereum; use devtools::*; use miner::Miner; +use rlp::{self, RlpStream, Stream}; #[cfg(feature = "json-tests")] pub enum ChainEra { @@ -116,7 +117,7 @@ pub fn create_test_block_with_data(header: &Header, transactions: &[SignedTransa rlp.append(header); rlp.begin_list(transactions.len()); for t in transactions { - rlp.append_raw(&encode::(t).to_vec(), 1); + rlp.append_raw(&rlp::encode::(t).to_vec(), 1); } rlp.append(&uncles); rlp.out() diff --git a/ethcore/src/trace/bloom.rs b/ethcore/src/trace/bloom.rs index 8640240df..9196453fd 100644 --- a/ethcore/src/trace/bloom.rs +++ b/ethcore/src/trace/bloom.rs @@ -1,6 +1,6 @@ use bloomchain::Bloom; use bloomchain::group::{BloomGroup, GroupPosition}; -use util::rlp::*; +use rlp::*; use basic_types::LogBloom; /// Helper structure representing bloom of the trace. diff --git a/ethcore/src/types/executed.rs b/ethcore/src/types/executed.rs index b81707581..b4328b75d 100644 --- a/ethcore/src/types/executed.rs +++ b/ethcore/src/types/executed.rs @@ -17,7 +17,7 @@ //! Transaction execution format module. use util::{Bytes, U256, Address, U512}; -use util::rlp::*; +use rlp::*; use trace::{VMTrace, FlatTrace}; use types::log_entry::LogEntry; use types::state_diff::StateDiff; @@ -203,7 +203,8 @@ pub type ExecutionResult = Result; #[test] fn should_encode_and_decode_call_type() { - use util::rlp; + use rlp; + let original = CallType::Call; let encoded = rlp::encode(&original); let decoded = rlp::decode(&encoded); diff --git a/ethcore/src/types/log_entry.rs b/ethcore/src/types/log_entry.rs index 5267686bc..0e5f7d531 100644 --- a/ethcore/src/types/log_entry.rs +++ b/ethcore/src/types/log_entry.rs @@ -18,8 +18,9 @@ use std::ops::Deref; use util::{H256, Address, Bytes, HeapSizeOf, Hashable}; -use util::rlp::*; use util::bloom::Bloomable; +use rlp::*; + use basic_types::LogBloom; use header::BlockNumber; use ethjson; diff --git a/ethcore/src/types/receipt.rs b/ethcore/src/types/receipt.rs index c6d413381..52e6747e8 100644 --- a/ethcore/src/types/receipt.rs +++ b/ethcore/src/types/receipt.rs @@ -17,8 +17,9 @@ //! Receipt use util::{H256, U256, Address}; -use util::rlp::*; use util::HeapSizeOf; +use rlp::*; + use basic_types::LogBloom; use header::BlockNumber; use log_entry::{LogEntry, LocalizedLogEntry}; diff --git a/ethcore/src/types/trace_types/flat.rs b/ethcore/src/types/trace_types/flat.rs index e5dd9dd8b..58983558d 100644 --- a/ethcore/src/types/trace_types/flat.rs +++ b/ethcore/src/types/trace_types/flat.rs @@ -17,7 +17,7 @@ //! Flat trace module use std::collections::VecDeque; -use util::rlp::*; +use rlp::*; use util::HeapSizeOf; use basic_types::LogBloom; use super::trace::{Action, Res}; @@ -167,7 +167,6 @@ mod tests { #[test] fn test_trace_serialization() { - use util::rlp; // block #51921 let flat_trace = FlatTrace { @@ -220,8 +219,8 @@ mod tests { FlatTransactionTraces(vec![flat_trace1, flat_trace2]) ]); - let encoded = rlp::encode(&block_traces); - let decoded = rlp::decode(&encoded); + let encoded = ::rlp::encode(&block_traces); + let decoded = ::rlp::decode(&encoded); assert_eq!(block_traces, decoded); } } diff --git a/ethcore/src/types/trace_types/trace.rs b/ethcore/src/types/trace_types/trace.rs index f594dd4bb..7261107e6 100644 --- a/ethcore/src/types/trace_types/trace.rs +++ b/ethcore/src/types/trace_types/trace.rs @@ -17,9 +17,10 @@ //! Tracing datatypes. use util::{U256, Bytes, Address}; -use util::rlp::*; use util::sha3::Hashable; use util::bloom::Bloomable; +use rlp::*; + use action_params::ActionParams; use basic_types::LogBloom; use types::executed::CallType; diff --git a/ethcore/src/types/transaction.rs b/ethcore/src/types/transaction.rs index 02d3da30a..f7e582f11 100644 --- a/ethcore/src/types/transaction.rs +++ b/ethcore/src/types/transaction.rs @@ -18,7 +18,7 @@ use std::ops::Deref; use std::cell::*; -use util::rlp::*; +use rlp::*; use util::sha3::Hashable; use util::{H256, Address, U256, Bytes}; use ethkey::{Signature, sign, Secret, recover, public_to_address, Error as EthkeyError}; @@ -275,7 +275,7 @@ impl SignedTransaction { match hash { Some(h) => h, None => { - let h = self.rlp_sha3(); + let h = (&*self.rlp_bytes()).sha3(); self.hash.set(Some(h)); h } diff --git a/ethcore/src/verification/verification.rs b/ethcore/src/verification/verification.rs index aa719cf23..db3a9314c 100644 --- a/ethcore/src/verification/verification.rs +++ b/ethcore/src/verification/verification.rs @@ -24,6 +24,7 @@ use common::*; use engines::Engine; use blockchain::*; +use rlp::{UntrustedRlp, View}; /// Preprocessed block data gathered in `verify_block_unordered` call pub struct PreverifiedBlock { @@ -240,6 +241,7 @@ mod tests { use spec::*; use transaction::*; use tests::helpers::*; + use rlp::View; fn check_ok(result: Result<(), Error>) { result.unwrap_or_else(|e| panic!("Block verification failed: {:?}", e)); @@ -346,6 +348,8 @@ mod tests { #[test] #[cfg_attr(feature="dev", allow(similar_names))] fn test_verify_block() { + use rlp::{RlpStream, Stream}; + // Test against morden let mut good = Header::new(); let spec = Spec::new_test(); @@ -411,7 +415,7 @@ mod tests { let mut uncles_rlp = RlpStream::new(); uncles_rlp.append(&good_uncles); let good_uncles_hash = uncles_rlp.as_raw().sha3(); - let good_transactions_root = ordered_trie_root(good_transactions.iter().map(|t| encode::(t).to_vec()).collect()); + let good_transactions_root = ordered_trie_root(good_transactions.iter().map(|t| ::rlp::encode::(t).to_vec()).collect()); let mut parent = good.clone(); parent.set_number(9); diff --git a/ethcore/src/views/block.rs b/ethcore/src/views/block.rs index fdcae383b..97cf26441 100644 --- a/ethcore/src/views/block.rs +++ b/ethcore/src/views/block.rs @@ -20,6 +20,7 @@ use util::*; use header::*; use transaction::*; use super::{TransactionView, HeaderView}; +use rlp::{Rlp, View}; /// View onto block rlp. pub struct BlockView<'a> { diff --git a/ethcore/src/views/body.rs b/ethcore/src/views/body.rs index 8f1295f31..989acd465 100644 --- a/ethcore/src/views/body.rs +++ b/ethcore/src/views/body.rs @@ -20,6 +20,7 @@ use util::*; use header::*; use transaction::*; use super::{TransactionView, HeaderView}; +use rlp::{Rlp, View}; /// View onto block rlp. pub struct BodyView<'a> { diff --git a/ethcore/src/views/header.rs b/ethcore/src/views/header.rs index 70b59fbfa..6c3b14b15 100644 --- a/ethcore/src/views/header.rs +++ b/ethcore/src/views/header.rs @@ -16,7 +16,8 @@ //! View onto block header rlp -use util::{Rlp, U256, Bytes, Hashable, H256, Address, H2048, View}; +use util::{U256, Bytes, Hashable, H256, Address, H2048}; +use rlp::{Rlp, View}; use header::BlockNumber; /// View onto block header rlp. diff --git a/ethcore/src/views/transaction.rs b/ethcore/src/views/transaction.rs index d83290909..df861d9fd 100644 --- a/ethcore/src/views/transaction.rs +++ b/ethcore/src/views/transaction.rs @@ -15,7 +15,8 @@ // along with Parity. If not, see . //! View onto transaction rlp -use util::{Rlp, U256, Bytes, Hashable, H256, View}; +use util::{U256, Bytes, Hashable, H256}; +use rlp::{Rlp, View}; /// View onto transaction rlp. pub struct TransactionView<'a> { diff --git a/util/src/sha3.rs b/util/src/sha3.rs index 5ab55257d..29c66f8dc 100644 --- a/util/src/sha3.rs +++ b/util/src/sha3.rs @@ -20,7 +20,7 @@ extern crate sha3 as sha3_ext; use std::io; use std::mem::uninitialized; use tiny_keccak::Keccak; -use bytes::{BytesConvertable, Populatable}; +use bytes::{Populatable}; use hash::{H256, FixedHash}; use self::sha3_ext::*; @@ -55,7 +55,7 @@ pub trait Hashable { } } -impl Hashable for T where T: BytesConvertable { +impl Hashable for T where T: AsRef<[u8]> { fn sha3(&self) -> H256 { unsafe { let mut ret: H256 = uninitialized(); @@ -65,7 +65,7 @@ impl Hashable for T where T: BytesConvertable { } fn sha3_into(&self, dest: &mut [u8]) { unsafe { - let input: &[u8] = self.as_slice(); + let input: &[u8] = self.as_ref(); sha3_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len()); } } From d763664d168f6bc06c6427c3b95e891be3cbf7b2 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 1 Sep 2016 14:49:12 +0200 Subject: [PATCH 5/9] port network, sync, rpc --- Cargo.lock | 3 +++ rpc/Cargo.toml | 1 + rpc/src/lib.rs | 11 +++++++---- rpc/src/v1/helpers/dispatch.rs | 5 ++--- rpc/src/v1/impls/eth.rs | 10 +++++----- rpc/src/v1/impls/traces.rs | 2 +- rpc/src/v1/tests/mocked/eth.rs | 4 ++-- rpc/src/v1/types/transaction.rs | 5 ++--- sync/Cargo.toml | 1 + sync/src/blocks.rs | 8 +++++--- sync/src/chain.rs | 6 ++++-- sync/src/lib.rs | 15 ++++++++------- util/network/Cargo.toml | 1 + util/network/src/connection.rs | 2 +- util/network/src/discovery.rs | 2 +- util/network/src/error.rs | 2 +- util/network/src/handshake.rs | 2 +- util/network/src/host.rs | 2 +- util/network/src/lib.rs | 8 +++++--- util/network/src/node_table.rs | 2 +- util/network/src/session.rs | 2 +- 21 files changed, 54 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 86db0c97a..e9d7a757a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -416,6 +416,7 @@ dependencies = [ "mio 0.5.1 (git+https://github.com/ethcore/mio?branch=v0.5.x)", "parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -442,6 +443,7 @@ dependencies = [ "jsonrpc-core 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-http-server 6.1.0 (git+https://github.com/ethcore/jsonrpc-http-server.git)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", "serde_codegen 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -588,6 +590,7 @@ dependencies = [ "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index ac1fd96c5..0d5bfc62c 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -23,6 +23,7 @@ ethash = { path = "../ethash" } ethsync = { path = "../sync" } ethjson = { path = "../json" } ethcore-devtools = { path = "../devtools" } +rlp = { path = "../util/rlp" } rustc-serialize = "0.3" transient-hashmap = "0.1" serde_macros = { version = "0.7.0", optional = true } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 6edc72d41..17d1837ae 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -19,15 +19,12 @@ #![cfg_attr(feature="nightly", feature(custom_derive, custom_attribute, plugin))] #![cfg_attr(feature="nightly", plugin(serde_macros, clippy))] -#[macro_use] -extern crate log; extern crate rustc_serialize; extern crate serde; extern crate serde_json; extern crate jsonrpc_core; extern crate jsonrpc_http_server; -#[macro_use] -extern crate ethcore_util as util; + extern crate ethcore_io as io; extern crate ethcore; extern crate ethkey; @@ -37,6 +34,12 @@ extern crate transient_hashmap; extern crate json_ipc_server as ipc; extern crate ethcore_ipc; extern crate time; +extern crate rlp; + +#[macro_use] +extern crate log; +#[macro_use] +extern crate ethcore_util as util; #[cfg(test)] extern crate ethjson; diff --git a/rpc/src/v1/helpers/dispatch.rs b/rpc/src/v1/helpers/dispatch.rs index b26dca8dc..bf8442a85 100644 --- a/rpc/src/v1/helpers/dispatch.rs +++ b/rpc/src/v1/helpers/dispatch.rs @@ -15,7 +15,6 @@ // along with Parity. If not, see . use util::{Address, H256, U256, Uint}; -use util::rlp::encode; use util::bytes::ToPretty; use ethcore::miner::MinerService; use ethcore::client::MiningBlockChainClient; @@ -70,7 +69,7 @@ pub fn unlock_sign_and_dispatch(client: &C, miner: &M, request: Transactio t.with_signature(signature) }; - trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty()); + trace!(target: "miner", "send_transaction: dispatching tx: {}", ::rlp::encode(&signed_transaction).to_vec().pretty()); dispatch_transaction(&*client, &*miner, signed_transaction) } @@ -84,7 +83,7 @@ pub fn sign_and_dispatch(client: &C, miner: &M, request: TransactionReques t.with_signature(signature) }; - trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty()); + trace!(target: "miner", "send_transaction: dispatching tx: {}", ::rlp::encode(&signed_transaction).to_vec().pretty()); dispatch_transaction(&*client, &*miner, signed_transaction) } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index d805d57ee..8ec1904ea 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -29,8 +29,8 @@ use ethcore::miner::{MinerService, ExternalMinerService}; use jsonrpc_core::*; use util::{H256, Address, FixedHash, U256, H64, Uint}; use util::sha3::*; -use util::rlp::{encode, decode, UntrustedRlp, View}; use util::{FromHex, Mutex}; +use rlp::{self, UntrustedRlp, View}; use ethcore::account_provider::AccountProvider; use ethcore::client::{MiningBlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::header::Header as BlockHeader; @@ -123,7 +123,7 @@ impl EthClient where timestamp: view.timestamp().into(), difficulty: view.difficulty().into(), total_difficulty: total_difficulty.into(), - seal_fields: view.seal().into_iter().map(|f| decode(&f)).map(Bytes::new).collect(), + seal_fields: view.seal().into_iter().map(|f| rlp::decode(&f)).map(Bytes::new).collect(), uncles: block_view.uncle_hashes().into_iter().map(Into::into).collect(), transactions: match include_txs { true => BlockTransactions::Full(block_view.localized_transactions().into_iter().map(Into::into).collect()), @@ -147,7 +147,7 @@ impl EthClient where fn uncle(&self, id: UncleID) -> Result { let client = take_weak!(self.client); let uncle: BlockHeader = match client.uncle(id) { - Some(rlp) => decode(&rlp), + Some(rlp) => rlp::decode(&rlp), None => { return Ok(Value::Null); } }; let parent_difficulty = match client.block_total_difficulty(BlockID::Hash(uncle.parent_hash().clone())) { @@ -173,7 +173,7 @@ impl EthClient where total_difficulty: (uncle.difficulty().clone() + parent_difficulty).into(), receipts_root: uncle.receipts_root().clone().into(), extra_data: uncle.extra_data().clone().into(), - seal_fields: uncle.seal().clone().into_iter().map(|f| decode(&f)).map(Bytes::new).collect(), + seal_fields: uncle.seal().clone().into_iter().map(|f| rlp::decode(&f)).map(Bytes::new).collect(), uncles: vec![], transactions: BlockTransactions::Hashes(vec![]), }; @@ -566,7 +566,7 @@ impl Eth for EthClient where trace!(target: "miner", "submit_work: Decoded: nonce={}, pow_hash={}, mix_hash={}", nonce, pow_hash, mix_hash); let miner = take_weak!(self.miner); let client = take_weak!(self.client); - let seal = vec![encode(&mix_hash).to_vec(), encode(&nonce).to_vec()]; + let seal = vec![rlp::encode(&mix_hash).to_vec(), rlp::encode(&nonce).to_vec()]; let r = miner.submit_seal(&*client, pow_hash, seal); to_value(&r.is_ok()) }) diff --git a/rpc/src/v1/impls/traces.rs b/rpc/src/v1/impls/traces.rs index e5f84d9ef..0189fbaee 100644 --- a/rpc/src/v1/impls/traces.rs +++ b/rpc/src/v1/impls/traces.rs @@ -18,7 +18,7 @@ use std::sync::{Weak, Arc}; use jsonrpc_core::*; -use util::rlp::{UntrustedRlp, View}; +use rlp::{UntrustedRlp, View}; use ethcore::client::{BlockChainClient, CallAnalytics, TransactionID, TraceId}; use ethcore::miner::MinerService; use ethcore::transaction::{Transaction as EthTransaction, SignedTransaction, Action}; diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index d7d93137f..1ff15dd0d 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -364,7 +364,7 @@ fn rpc_eth_pending_transaction_by_hash() { let tester = EthTester::default(); { - let tx: SignedTransaction = decode(&FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap()); + let tx: SignedTransaction = ::rlp::decode(&FromHex::from_hex("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap()); tester.miner.pending_transactions.lock().insert(H256::zero(), tx); } @@ -705,7 +705,7 @@ fn rpc_eth_send_raw_transaction() { let signature = tester.accounts_provider.sign(address, t.hash()).unwrap(); let t = t.with_signature(signature); - let rlp = ::util::rlp::encode(&t).to_vec().to_hex(); + let rlp = ::rlp::encode(&t).to_vec().to_hex(); let req = r#"{ "jsonrpc": "2.0", diff --git a/rpc/src/v1/types/transaction.rs b/rpc/src/v1/types/transaction.rs index 812c006e4..6ce9eafc3 100644 --- a/rpc/src/v1/types/transaction.rs +++ b/rpc/src/v1/types/transaction.rs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use util::rlp::encode; use ethcore::contract_address; use ethcore::transaction::{LocalizedTransaction, Action, SignedTransaction}; use v1::types::{Bytes, H160, H256, U256}; @@ -75,7 +74,7 @@ impl From for Transaction { Action::Create => Some(contract_address(&t.sender().unwrap(), &t.nonce).into()), Action::Call(_) => None, }, - raw: encode(&t.signed).to_vec().into(), + raw: ::rlp::encode(&t.signed).to_vec().into(), } } } @@ -101,7 +100,7 @@ impl From for Transaction { Action::Create => Some(contract_address(&t.sender().unwrap(), &t.nonce).into()), Action::Call(_) => None, }, - raw: encode(&t).to_vec().into(), + raw: ::rlp::encode(&t).to_vec().into(), } } } diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 3a2e8aec2..a73077c9c 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -16,6 +16,7 @@ ethcore-util = { path = "../util" } ethcore-network = { path = "../util/network" } ethcore-io = { path = "../util/io" } ethcore = { path = "../ethcore" } +rlp = { path = "../util/rlp" } clippy = { version = "0.0.85", optional = true} log = "0.3" env_logger = "0.3" diff --git a/sync/src/blocks.rs b/sync/src/blocks.rs index 498b35f43..753ba7111 100644 --- a/sync/src/blocks.rs +++ b/sync/src/blocks.rs @@ -15,6 +15,7 @@ // along with Parity. If not, see . use util::*; +use rlp::*; use network::NetworkError; use ethcore::header::{ Header as BlockHeader}; @@ -283,11 +284,11 @@ impl BlockCollection { transactions_root: info.transactions_root().clone(), uncles: info.uncles_hash().clone(), }; - if header_id.transactions_root == rlp::SHA3_NULL_RLP && header_id.uncles == rlp::SHA3_EMPTY_LIST_RLP { + if header_id.transactions_root == sha3::SHA3_NULL_RLP && header_id.uncles == sha3::SHA3_EMPTY_LIST_RLP { // empty body, just mark as downloaded let mut body_stream = RlpStream::new_list(2); - body_stream.append_raw(&rlp::EMPTY_LIST_RLP, 1); - body_stream.append_raw(&rlp::EMPTY_LIST_RLP, 1); + body_stream.append_raw(&::rlp::EMPTY_LIST_RLP, 1); + body_stream.append_raw(&::rlp::EMPTY_LIST_RLP, 1); block.body = Some(body_stream.out()); } else { @@ -337,6 +338,7 @@ mod test { use ethcore::views::HeaderView; use ethcore::header::BlockNumber; use util::*; + use rlp::*; fn is_empty(bc: &BlockCollection) -> bool { bc.heads.is_empty() && diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 9abd05398..feac90741 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -88,6 +88,7 @@ /// use util::*; +use rlp::*; use network::*; use std::mem::{replace}; use ethcore::views::{HeaderView, BlockView}; @@ -1531,6 +1532,7 @@ mod tests { use super::*; use ::SyncConfig; use util::*; + use rlp::*; use super::{PeerInfo, PeerAsking}; use ethcore::views::BlockView; use ethcore::header::*; @@ -1548,8 +1550,8 @@ mod tests { let mut rlp = RlpStream::new_list(3); rlp.append(&header); - rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); - rlp.append_raw(&rlp::EMPTY_LIST_RLP, 1); + rlp.append_raw(&::rlp::EMPTY_LIST_RLP, 1); + rlp.append_raw(&::rlp::EMPTY_LIST_RLP, 1); rlp.out() } diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 69dd03a2a..c0c240f9d 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -34,7 +34,6 @@ //! extern crate ethcore; //! extern crate ethsync; //! use std::env; -//! use std::sync::Arc; //! use io::IoChannel; //! use ethcore::client::{Client, ClientConfig}; //! use ethsync::{EthSync, SyncConfig, ManageNetwork, NetworkConfiguration}; @@ -62,22 +61,24 @@ //! } //! ``` -#[macro_use] -extern crate log; -#[macro_use] -extern crate ethcore_util as util; extern crate ethcore_network as network; extern crate ethcore_io as io; extern crate ethcore; extern crate env_logger; extern crate time; extern crate rand; +extern crate semver; +extern crate parking_lot; +extern crate rlp; + +#[macro_use] +extern crate log; +#[macro_use] +extern crate ethcore_util as util; #[macro_use] extern crate heapsize; #[macro_use] extern crate ethcore_ipc as ipc; -extern crate semver; -extern crate parking_lot; mod chain; mod blocks; diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml index caad5c0d5..50ef6a5e4 100644 --- a/util/network/Cargo.toml +++ b/util/network/Cargo.toml @@ -25,6 +25,7 @@ ethcore-util = { path = ".." } ethcore-devtools = { path = "../../devtools" } ethkey = { path = "../../ethkey" } ethcrypto = { path = "../../ethcrypto" } +rlp = { path = "../rlp" } [features] default = [] diff --git a/util/network/src/connection.rs b/util/network/src/connection.rs index d3e54393b..4a7b5a3c7 100644 --- a/util/network/src/connection.rs +++ b/util/network/src/connection.rs @@ -23,7 +23,7 @@ use mio::tcp::*; use util::hash::*; use util::sha3::*; use util::bytes::*; -use util::rlp::*; +use rlp::*; use std::io::{self, Cursor, Read, Write}; use error::*; use io::{IoContext, StreamToken}; diff --git a/util/network/src/discovery.rs b/util/network/src/discovery.rs index 48d16be48..61eaf4094 100644 --- a/util/network/src/discovery.rs +++ b/util/network/src/discovery.rs @@ -24,7 +24,7 @@ use mio::udp::*; use util::sha3::*; use time; use util::hash::*; -use util::rlp::*; +use rlp::*; use node_table::*; use error::NetworkError; use io::{StreamToken, IoContext}; diff --git a/util/network/src/error.rs b/util/network/src/error.rs index 99bfb4500..a1c4d70a1 100644 --- a/util/network/src/error.rs +++ b/util/network/src/error.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use io::IoError; -use util::rlp::*; +use rlp::*; use util::UtilError; use std::fmt; use ethkey::Error as KeyError; diff --git a/util/network/src/handshake.rs b/util/network/src/handshake.rs index 403079de4..1456cf801 100644 --- a/util/network/src/handshake.rs +++ b/util/network/src/handshake.rs @@ -18,9 +18,9 @@ use std::sync::Arc; use rand::random; use mio::tcp::*; use util::hash::*; -use util::rlp::*; use util::sha3::Hashable; use util::bytes::Bytes; +use rlp::*; use connection::{Connection}; use host::{HostInfo}; use node_table::NodeId; diff --git a/util/network/src/host.rs b/util/network/src/host.rs index a1d320c58..359f54f1a 100644 --- a/util/network/src/host.rs +++ b/util/network/src/host.rs @@ -29,8 +29,8 @@ use mio::*; use mio::tcp::*; use util::hash::*; use util::Hashable; -use util::rlp::*; use util::version; +use rlp::*; use session::{Session, SessionData}; use error::*; use io::*; diff --git a/util/network/src/lib.rs b/util/network/src/lib.rs index e36861f37..bfcd49cea 100644 --- a/util/network/src/lib.rs +++ b/util/network/src/lib.rs @@ -54,9 +54,6 @@ //! } //! ``` - -#[macro_use] -extern crate log; extern crate ethcore_io as io; extern crate ethcore_util as util; extern crate parking_lot; @@ -72,6 +69,11 @@ extern crate libc; extern crate slab; extern crate ethkey; extern crate ethcrypto as crypto; +extern crate rlp; + +#[macro_use] +extern crate log; + #[cfg(test)] extern crate ethcore_devtools as devtools; diff --git a/util/network/src/node_table.rs b/util/network/src/node_table.rs index 26f6d9f8c..073e9ab76 100644 --- a/util/network/src/node_table.rs +++ b/util/network/src/node_table.rs @@ -26,8 +26,8 @@ use std::fmt; use std::fs; use std::io::{Read, Write}; use util::hash::*; -use util::rlp::*; use util::UtilError; +use rlp::*; use time::Tm; use error::NetworkError; use discovery::{TableUpdates, NodeEntry}; diff --git a/util/network/src/session.rs b/util/network/src/session.rs index 4e6dd1c8a..8ebd37090 100644 --- a/util/network/src/session.rs +++ b/util/network/src/session.rs @@ -19,8 +19,8 @@ use std::io; use std::sync::*; use mio::*; use mio::tcp::*; -use util::rlp::*; use util::hash::*; +use rlp::*; use connection::{EncryptedConnection, Packet, Connection}; use handshake::Handshake; use io::{IoContext, StreamToken}; From 74ffcff99c9aaab2550fd339bf527ebcb081b8dc Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 1 Sep 2016 14:55:07 +0200 Subject: [PATCH 6/9] port parity itself --- Cargo.lock | 1 + Cargo.toml | 1 + parity/blockchain.rs | 3 ++- parity/main.rs | 20 ++++++++++++-------- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e9d7a757a..46932e594 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,7 @@ dependencies = [ "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.68 (registry+https://github.com/rust-lang/crates.io-index)", + "rlp 0.1.0", "rpassword 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 661544465..d8e42bf3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ ethcore-ipc-nano = { path = "ipc/nano" } ethcore-ipc = { path = "ipc/rpc" } ethcore-ipc-hypervisor = { path = "ipc/hypervisor" } ethcore-logger = { path = "logger" } +rlp = { path = "util/rlp" } json-ipc-server = { git = "https://github.com/ethcore/json-ipc-server.git" } ethcore-dapps = { path = "dapps", optional = true } clippy = { version = "0.0.85", optional = true} diff --git a/parity/blockchain.rs b/parity/blockchain.rs index 06e03ae72..0dd11b976 100644 --- a/parity/blockchain.rs +++ b/parity/blockchain.rs @@ -24,7 +24,8 @@ use std::sync::Arc; use rustc_serialize::hex::FromHex; use ethcore_logger::{setup_log, Config as LogConfig}; use io::{PanicHandler, ForwardPanic}; -use util::{PayloadInfo, ToPretty}; +use util::ToPretty; +use rlp::PayloadInfo; use ethcore::service::ClientService; use ethcore::client::{Mode, DatabaseCompactionProfile, Switch, VMType, BlockImportError, BlockChainClient, BlockID}; use ethcore::error::ImportError; diff --git a/parity/main.rs b/parity/main.rs index 406645f06..86844baa9 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -26,12 +26,8 @@ extern crate docopt; extern crate num_cpus; extern crate rustc_serialize; extern crate ethcore_devtools as devtools; -#[macro_use] -extern crate ethcore_util as util; extern crate ethcore; extern crate ethsync; -#[macro_use] -extern crate log as rlog; extern crate env_logger; extern crate ethcore_logger; extern crate ctrlc; @@ -43,8 +39,8 @@ extern crate semver; extern crate ethcore_io as io; extern crate ethcore_ipc as ipc; extern crate ethcore_ipc_nano as nanoipc; -#[macro_use] -extern crate hyper; // for price_info.rs +extern crate rlp; + extern crate json_ipc_server as jsonipc; extern crate ethcore_ipc_hypervisor as hypervisor; @@ -52,11 +48,19 @@ extern crate ethcore_rpc; extern crate ethcore_signer; extern crate ansi_term; -#[macro_use] -extern crate lazy_static; + extern crate regex; extern crate isatty; +#[macro_use] +extern crate ethcore_util as util; +#[macro_use] +extern crate log as rlog; +#[macro_use] +extern crate hyper; // for price_info.rs +#[macro_use] +extern crate lazy_static; + #[cfg(feature="stratum")] extern crate ethcore_stratum; From cabb028c1ce67b5e6f4d9fb3c224dc73ee100e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 1 Sep 2016 19:07:58 +0200 Subject: [PATCH 7/9] Propagating transactions to peers on timer. (#2035) --- ethcore/src/client/test_client.rs | 20 ++++- sync/src/api.rs | 1 + sync/src/chain.rs | 124 ++++++++++++++++++++++++------ 3 files changed, 119 insertions(+), 26 deletions(-) diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index fb7f9083e..9ee7f0933 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -33,7 +33,7 @@ use receipt::{Receipt, LocalizedReceipt}; use blockchain::extras::BlockReceipts; use error::{ImportResult}; use evm::{Factory as EvmFactory, VMType}; -use miner::{Miner, MinerService}; +use miner::{Miner, MinerService, TransactionImportResult}; use spec::Spec; use block_queue::BlockQueueInfo; @@ -254,6 +254,24 @@ impl TestBlockChainClient { BlockID::Latest | BlockID::Pending => self.numbers.read().get(&(self.numbers.read().len() - 1)).cloned() } } + + /// Inserts a transaction to miners transactions queue. + pub fn insert_transaction_to_queue(&self) { + let keypair = Random.generate().unwrap(); + let tx = Transaction { + action: Action::Create, + value: U256::from(100), + data: "3331600055".from_hex().unwrap(), + gas: U256::from(100_000), + gas_price: U256::one(), + nonce: U256::zero() + }; + let signed_tx = tx.sign(keypair.secret()); + self.set_balance(signed_tx.sender().unwrap(), 10_000_000.into()); + let res = self.miner.import_external_transactions(self, vec![signed_tx]); + let res = res.into_iter().next().unwrap().expect("Successful import"); + assert_eq!(res, TransactionImportResult::Current); + } } pub fn get_temp_journal_db() -> GuardedTempResult> { diff --git a/sync/src/api.rs b/sync/src/api.rs index 7b866d2bf..a19004642 100644 --- a/sync/src/api.rs +++ b/sync/src/api.rs @@ -119,6 +119,7 @@ impl NetworkProtocolHandler for SyncProtocolHandler { fn timeout(&self, io: &NetworkContext, _timer: TimerToken) { self.sync.write().maintain_peers(&mut NetSyncIo::new(io, &*self.chain)); self.sync.write().maintain_sync(&mut NetSyncIo::new(io, &*self.chain)); + self.sync.write().propagate_new_transactions(&mut NetSyncIo::new(io, &*self.chain)); } } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index f5a54a0ba..10d90707a 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -241,7 +241,9 @@ struct PeerInfo { asking_hash: Option, /// Request timestamp ask_time: f64, - /// Pending request is expird and result should be ignored + /// Holds a set of transactions recently sent to this peer to avoid spamming. + last_sent_transactions: HashSet, + /// Pending request is expired and result should be ignored expired: bool, /// Peer fork confirmation status confirmation: ForkConfirmation, @@ -406,6 +408,7 @@ impl ChainSync { asking_blocks: Vec::new(), asking_hash: None, ask_time: 0f64, + last_sent_transactions: HashSet::new(), expired: false, confirmation: if self.fork_block.is_none() { ForkConfirmation::Confirmed } else { ForkConfirmation::Unconfirmed }, }; @@ -1447,42 +1450,65 @@ impl ChainSync { } /// propagates new transactions to all peers - fn propagate_new_transactions(&mut self, io: &mut SyncIo) -> usize { + pub fn propagate_new_transactions(&mut self, io: &mut SyncIo) -> usize { // Early out of nobody to send to. if self.peers.is_empty() { return 0; } - let mut transactions = io.chain().pending_transactions(); + let transactions = io.chain().pending_transactions(); if transactions.is_empty() { return 0; } - let mut packet = RlpStream::new_list(transactions.len()); - let tx_count = transactions.len(); - for tx in transactions.drain(..) { - packet.append(&tx); - } - let rlp = packet.out(); - - let lucky_peers = { - // sqrt(x)/x scaled to max u32 - let fraction = (self.peers.len() as f64).powf(-0.5).mul(u32::max_value() as f64).round() as u32; - let small = self.peers.len() < MIN_PEERS_PROPAGATION; - let lucky_peers = self.peers.iter() - .filter_map(|(&p, _)| if small || ::rand::random::() < fraction { Some(p.clone()) } else { None }) - .collect::>(); - - // taking at max of MAX_PEERS_PROPAGATION - lucky_peers.iter().cloned().take(min(lucky_peers.len(), MAX_PEERS_PROPAGATION)).collect::>() + let all_transactions_hashes = transactions.iter().map(|ref tx| tx.hash()).collect::>(); + let all_transactions_rlp = { + let mut packet = RlpStream::new_list(transactions.len()); + for tx in &transactions { packet.append(tx); } + packet.out() }; + // sqrt(x)/x scaled to max u32 + let fraction = (self.peers.len() as f64).powf(-0.5).mul(u32::max_value() as f64).round() as u32; + let small = self.peers.len() < MIN_PEERS_PROPAGATION; + + let lucky_peers = self.peers.iter_mut() + .filter(|_| small || ::rand::random::() < fraction) + .take(MAX_PEERS_PROPAGATION) + .filter_map(|(peer_id, mut peer_info)| { + // Send all transactions + if peer_info.last_sent_transactions.is_empty() { + peer_info.last_sent_transactions = all_transactions_hashes.clone(); + return Some((*peer_id, all_transactions_rlp.clone())); + } + + // Get hashes of all transactions to send to this peer + let to_send = all_transactions_hashes.difference(&peer_info.last_sent_transactions).cloned().collect::>(); + if to_send.is_empty() { + return None; + } + + // Construct RLP + let mut packet = RlpStream::new_list(to_send.len()); + for tx in &transactions { + if to_send.contains(&tx.hash()) { + packet.append(tx); + } + } + + peer_info.last_sent_transactions = to_send; + Some((*peer_id, packet.out())) + }) + .collect::>(); + + // Send RLPs let sent = lucky_peers.len(); - for peer_id in lucky_peers { - self.send_packet(io, peer_id, TRANSACTIONS_PACKET, rlp.clone()); + for (peer_id, rlp) in lucky_peers.into_iter() { + self.send_packet(io, peer_id, TRANSACTIONS_PACKET, rlp); } - trace!(target: "sync", "Sent {} transactions to {} peers.", tx_count, sent); + + trace!(target: "sync", "Sent up to {} transactions to {} peers.", transactions.len(), sent); sent } @@ -1512,16 +1538,18 @@ impl ChainSync { self.check_resume(io); } - /// called when block is imported to chain, updates transactions queue and propagates the blocks + /// called when block is imported to chain - propagates the blocks and updates transactions sent to peers pub fn chain_new_blocks(&mut self, io: &mut SyncIo, _imported: &[H256], invalid: &[H256], _enacted: &[H256], _retracted: &[H256], sealed: &[H256]) { if io.is_chain_queue_empty() { - // Propagate latests blocks self.propagate_latest_blocks(io, sealed); } if !invalid.is_empty() { trace!(target: "sync", "Bad blocks in the queue, restarting"); self.restart_on_bad_block(io); } + for peer_info in self.peers.values_mut() { + peer_info.last_sent_transactions.clear(); + } } } @@ -1723,6 +1751,7 @@ mod tests { asking_blocks: Vec::new(), asking_hash: None, ask_time: 0f64, + last_sent_transactions: HashSet::new(), expired: false, confirmation: super::ForkConfirmation::Confirmed, }); @@ -1819,6 +1848,51 @@ mod tests { assert_eq!(0x07, io.queue[0].packet_id); } + #[test] + fn propagates_transactions() { + let mut client = TestBlockChainClient::new(); + client.add_blocks(100, EachBlockWith::Uncle); + client.insert_transaction_to_queue(); + let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(1), &client); + let mut queue = VecDeque::new(); + let mut io = TestIo::new(&mut client, &mut queue, None); + let peer_count = sync.propagate_new_transactions(&mut io); + // Try to propagate same transactions for the second time + let peer_count2 = sync.propagate_new_transactions(&mut io); + + // 1 message should be send + assert_eq!(1, io.queue.len()); + // 1 peer should be updated but only once + assert_eq!(1, peer_count); + assert_eq!(0, peer_count2); + // TRANSACTIONS_PACKET + assert_eq!(0x02, io.queue[0].packet_id); + } + + #[test] + fn propagates_transactions_again_after_new_block() { + let mut client = TestBlockChainClient::new(); + client.add_blocks(100, EachBlockWith::Uncle); + client.insert_transaction_to_queue(); + let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(1), &client); + let mut queue = VecDeque::new(); + let mut io = TestIo::new(&mut client, &mut queue, None); + let peer_count = sync.propagate_new_transactions(&mut io); + sync.chain_new_blocks(&mut io, &[], &[], &[], &[], &[]); + // Try to propagate same transactions for the second time + let peer_count2 = sync.propagate_new_transactions(&mut io); + + // 1 message should be send + assert_eq!(2, io.queue.len()); + // 1 peer should be updated but only once + assert_eq!(1, peer_count); + assert_eq!(1, peer_count2); + // TRANSACTIONS_PACKET + assert_eq!(0x02, io.queue[0].packet_id); + assert_eq!(0x02, io.queue[1].packet_id); + } + + #[test] fn handles_peer_new_block_malformed() { let mut client = TestBlockChainClient::new(); From 5b27ea3b4d2277417923526d6bf979817dab2477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Fri, 2 Sep 2016 11:37:45 +0200 Subject: [PATCH 8/9] Fixing new transactions without alternating (#2039) --- sync/src/chain.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 10d90707a..6254e08f4 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -1497,7 +1497,7 @@ impl ChainSync { } } - peer_info.last_sent_transactions = to_send; + peer_info.last_sent_transactions = all_transactions_hashes.clone(); Some((*peer_id, packet.out())) }) .collect::>(); @@ -1882,9 +1882,9 @@ mod tests { // Try to propagate same transactions for the second time let peer_count2 = sync.propagate_new_transactions(&mut io); - // 1 message should be send + // 2 message should be send assert_eq!(2, io.queue.len()); - // 1 peer should be updated but only once + // 1 peer should be updated twice assert_eq!(1, peer_count); assert_eq!(1, peer_count2); // TRANSACTIONS_PACKET @@ -1892,6 +1892,38 @@ mod tests { assert_eq!(0x02, io.queue[1].packet_id); } + #[test] + fn propagates_transactions_without_alternating() { + let mut client = TestBlockChainClient::new(); + client.add_blocks(100, EachBlockWith::Uncle); + client.insert_transaction_to_queue(); + let mut sync = dummy_sync_with_peer(client.block_hash_delta_minus(1), &client); + let mut queue = VecDeque::new(); + // should sent some + { + + let mut io = TestIo::new(&mut client, &mut queue, None); + let peer_count = sync.propagate_new_transactions(&mut io); + assert_eq!(1, io.queue.len()); + assert_eq!(1, peer_count); + } + // Insert some more + client.insert_transaction_to_queue(); + let mut io = TestIo::new(&mut client, &mut queue, None); + // Propagate new transactions + let peer_count2 = sync.propagate_new_transactions(&mut io); + // And now the peer should have all transactions + let peer_count3 = sync.propagate_new_transactions(&mut io); + + // 2 message should be send (in total) + assert_eq!(2, io.queue.len()); + // 1 peer should be updated but only once after inserting new transaction + assert_eq!(1, peer_count2); + assert_eq!(0, peer_count3); + // TRANSACTIONS_PACKET + assert_eq!(0x02, io.queue[0].packet_id); + assert_eq!(0x02, io.queue[1].packet_id); + } #[test] fn handles_peer_new_block_malformed() { From e2e6b11dc7c64630bf39e997d2e4af7246316cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Fri, 2 Sep 2016 11:38:16 +0200 Subject: [PATCH 9/9] Fixing uint serialization (#2037) --- rpc/src/v1/tests/eth.rs | 18 ++++----- rpc/src/v1/tests/mocked/eth.rs | 28 +++++++------- rpc/src/v1/tests/mocked/eth_signing.rs | 2 +- rpc/src/v1/tests/mocked/ethcore.rs | 2 +- rpc/src/v1/tests/mocked/personal_signer.rs | 12 +++--- rpc/src/v1/types/block.rs | 4 +- rpc/src/v1/types/confirmations.rs | 6 +-- rpc/src/v1/types/log.rs | 2 +- rpc/src/v1/types/receipt.rs | 2 +- rpc/src/v1/types/sync.rs | 4 +- rpc/src/v1/types/trace.rs | 8 ++-- rpc/src/v1/types/transaction.rs | 2 +- rpc/src/v1/types/uint.rs | 43 +++++++++++++++++++++- 13 files changed, 87 insertions(+), 46 deletions(-) diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 117b783db..b7ad5b943 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -156,7 +156,7 @@ fn eth_get_balance() { "params": ["0xaaaf5374fce5edbc8e2a8697c15331677e6ebaaa", "latest"], "id": 1 }"#; - let res_latest = r#"{"jsonrpc":"2.0","result":"0x09","id":1}"#.to_owned(); + let res_latest = r#"{"jsonrpc":"2.0","result":"0x9","id":1}"#.to_owned(); assert_eq!(tester.handler.handle_request_sync(req_latest).unwrap(), res_latest); // non-existant account @@ -167,7 +167,7 @@ fn eth_get_balance() { "id": 3 }"#; - let res_new_acc = r#"{"jsonrpc":"2.0","result":"0x00","id":3}"#.to_owned(); + let res_new_acc = r#"{"jsonrpc":"2.0","result":"0x0","id":3}"#.to_owned(); assert_eq!(tester.handler.handle_request_sync(req_new_acc).unwrap(), res_new_acc); } @@ -297,7 +297,7 @@ fn eth_transaction_count() { "id": 15 }"#; - let res_before = r#"{"jsonrpc":"2.0","result":"0x00","id":15}"#; + let res_before = r#"{"jsonrpc":"2.0","result":"0x0","id":15}"#; assert_eq!(tester.handler.handle_request_sync(&req_before).unwrap(), res_before); @@ -308,7 +308,7 @@ fn eth_transaction_count() { "from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567", "gas": "0x30000", - "gasPrice": "0x01", + "gasPrice": "0x1", "value": "0x9184e72a" }], "id": 16 @@ -325,7 +325,7 @@ fn eth_transaction_count() { "id": 17 }"#; - let res_after_latest = r#"{"jsonrpc":"2.0","result":"0x00","id":17}"#; + let res_after_latest = r#"{"jsonrpc":"2.0","result":"0x0","id":17}"#; assert_eq!(&tester.handler.handle_request_sync(&req_after_latest).unwrap(), res_after_latest); @@ -337,7 +337,7 @@ fn eth_transaction_count() { "id": 18 }"#; - let res_after_pending = r#"{"jsonrpc":"2.0","result":"0x01","id":18}"#; + let res_after_pending = r#"{"jsonrpc":"2.0","result":"0x1","id":18}"#; assert_eq!(&tester.handler.handle_request_sync(&req_after_pending).unwrap(), res_after_pending); } @@ -365,7 +365,7 @@ fn verify_transaction_counts(name: String, chain: BlockChain) { }"#; let res = r#"{"jsonrpc":"2.0","result":""#.to_owned() - + format!("0x{:02x}", count).as_ref() + + format!("0x{:x}", count).as_ref() + r#"","id":"# + format!("{}", *id).as_ref() + r#"}"#; *id += 1; @@ -383,7 +383,7 @@ fn verify_transaction_counts(name: String, chain: BlockChain) { }"#; let res = r#"{"jsonrpc":"2.0","result":""#.to_owned() - + format!("0x{:02x}", count).as_ref() + + format!("0x{:x}", count).as_ref() + r#"","id":"# + format!("{}", *id).as_ref() + r#"}"#; *id += 1; @@ -425,7 +425,7 @@ fn starting_nonce_test() { "#) ).unwrap(); - assert_eq!(r#"{"jsonrpc":"2.0","result":"0x0100","id":15}"#, &sample); + assert_eq!(r#"{"jsonrpc":"2.0","result":"0x100","id":15}"#, &sample); } register_test!(eth_transaction_count_1, verify_transaction_counts, "BlockchainTests/bcWalletTest"); diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index a6b5a1ef7..0f68d0733 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -122,7 +122,7 @@ fn rpc_eth_syncing() { } } - let true_res = r#"{"jsonrpc":"2.0","result":{"currentBlock":"0x03e8","highestBlock":"0x09c4","startingBlock":"0x00"},"id":1}"#; + let true_res = r#"{"jsonrpc":"2.0","result":{"currentBlock":"0x3e8","highestBlock":"0x9c4","startingBlock":"0x0"},"id":1}"#; assert_eq!(tester.io.handle_request_sync(request), Some(true_res.to_owned())); { @@ -226,7 +226,7 @@ fn rpc_eth_mining() { #[test] fn rpc_eth_gas_price() { let request = r#"{"jsonrpc": "2.0", "method": "eth_gasPrice", "params": [], "id": 1}"#; - let response = r#"{"jsonrpc":"2.0","result":"0x04a817c800","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x4a817c800","id":1}"#; assert_eq!(EthTester::default().io.handle_request_sync(request), Some(response.to_owned())); } @@ -248,7 +248,7 @@ fn rpc_eth_block_number() { tester.client.add_blocks(10, EachBlockWith::Nothing); let request = r#"{"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 1}"#; - let response = r#"{"jsonrpc":"2.0","result":"0x0a","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0xa","id":1}"#; assert_eq!(tester.io.handle_request_sync(request), Some(response.to_owned())); } @@ -264,7 +264,7 @@ fn rpc_eth_balance() { "params": ["0x0000000000000000000000000000000000000001", "latest"], "id": 1 }"#; - let response = r#"{"jsonrpc":"2.0","result":"0x05","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x5","id":1}"#; assert_eq!(tester.io.handle_request_sync(request), Some(response.to_owned())); } @@ -284,7 +284,7 @@ fn rpc_eth_balance_pending() { // the TestMinerService doesn't communicate with the the TestBlockChainClient in any way. // if this returns zero, we know that the "pending" call is being properly forwarded to the // miner. - let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x0","id":1}"#; assert_eq!(tester.io.handle_request_sync(request), Some(response.to_owned())); } @@ -313,7 +313,7 @@ fn rpc_eth_transaction_count() { "params": ["0x0000000000000000000000000000000000000001", "latest"], "id": 1 }"#; - let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x0","id":1}"#; assert_eq!(EthTester::default().io.handle_request_sync(request), Some(response.to_owned())); } @@ -339,7 +339,7 @@ fn rpc_eth_transaction_count_by_number() { "params": ["latest"], "id": 1 }"#; - let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x0","id":1}"#; assert_eq!(EthTester::default().io.handle_request_sync(request), Some(response.to_owned())); } @@ -352,7 +352,7 @@ fn rpc_eth_transaction_count_by_number_pending() { "params": ["pending"], "id": 1 }"#; - let response = r#"{"jsonrpc":"2.0","result":"0x01","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x1","id":1}"#; assert_eq!(EthTester::default().io.handle_request_sync(request), Some(response.to_owned())); } @@ -368,7 +368,7 @@ fn rpc_eth_pending_transaction_by_hash() { tester.miner.pending_transactions.lock().insert(H256::zero(), tx); } - let response = r#"{"jsonrpc":"2.0","result":{"blockHash":null,"blockNumber":null,"creates":null,"from":"0x0f65fe9276bc9a24ae7083ae28e2660ef72df99e","gas":"0x5208","gasPrice":"0x01","hash":"0x41df922fd0d4766fcc02e161f8295ec28522f329ae487f14d811e4b64c8d6e31","input":"0x","nonce":"0x00","raw":"0xf85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804","to":"0x095e7baea6a6c7c4c2dfeb977efac326af552d87","transactionIndex":null,"value":"0x0a"},"id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":{"blockHash":null,"blockNumber":null,"creates":null,"from":"0x0f65fe9276bc9a24ae7083ae28e2660ef72df99e","gas":"0x5208","gasPrice":"0x1","hash":"0x41df922fd0d4766fcc02e161f8295ec28522f329ae487f14d811e4b64c8d6e31","input":"0x","nonce":"0x0","raw":"0xf85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804","to":"0x095e7baea6a6c7c4c2dfeb977efac326af552d87","transactionIndex":null,"value":"0xa"},"id":1}"#; let request = r#"{ "jsonrpc": "2.0", "method": "eth_getTransactionByHash", @@ -400,7 +400,7 @@ fn rpc_eth_uncle_count_by_block_number() { "params": ["latest"], "id": 1 }"#; - let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x0","id":1}"#; assert_eq!(EthTester::default().io.handle_request_sync(request), Some(response.to_owned())); } @@ -758,7 +758,7 @@ fn rpc_eth_transaction_receipt() { "params": ["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"], "id": 1 }"#; - let response = r#"{"jsonrpc":"2.0","result":{"blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","contractAddress":null,"cumulativeGasUsed":"0x20","gasUsed":"0x10","logs":[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","data":"0x","logIndex":"0x01","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00","type":"mined"}],"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00"},"id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":{"blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x4510c","contractAddress":null,"cumulativeGasUsed":"0x20","gasUsed":"0x10","logs":[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x4510c","data":"0x","logIndex":"0x1","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","type":"mined"}],"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0"},"id":1}"#; assert_eq!(tester.io.handle_request_sync(request), Some(response.to_owned())); } @@ -835,7 +835,7 @@ fn rpc_get_work_returns_correct_work_package() { eth_tester.miner.set_author(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()); let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": [], "id": 1}"#; - let response = r#"{"jsonrpc":"2.0","result":["0x3bbe93f74e7b97ae00784aeff8819c5cb600dd87e8b282a5d3446f3f871f0347","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000800000000000000000000000000000000000000000000000000000000000","0x01"],"id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":["0x3bbe93f74e7b97ae00784aeff8819c5cb600dd87e8b282a5d3446f3f871f0347","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000800000000000000000000000000000000000000000000000000000000000","0x1"],"id":1}"#; assert_eq!(eth_tester.io.handle_request_sync(request), Some(response.to_owned())); } @@ -864,7 +864,7 @@ fn rpc_get_work_should_timeout() { // Request without providing timeout. This should work since we're disabling timeout. let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": [], "id": 1}"#; let work_response = format!( - r#"{{"jsonrpc":"2.0","result":["0x{:?}","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000800000000000000000000000000000000000000000000000000000000000","0x01"],"id":1}}"#, + r#"{{"jsonrpc":"2.0","result":["0x{:?}","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000800000000000000000000000000000000000000000000000000000000000","0x1"],"id":1}}"#, hash, ); assert_eq!(eth_tester.io.handle_request_sync(request), Some(work_response.to_owned())); @@ -872,7 +872,7 @@ fn rpc_get_work_should_timeout() { // Request with timeout of 0 seconds. This should work since we're disabling timeout. let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": ["0"], "id": 1}"#; let work_response = format!( - r#"{{"jsonrpc":"2.0","result":["0x{:?}","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000800000000000000000000000000000000000000000000000000000000000","0x01"],"id":1}}"#, + r#"{{"jsonrpc":"2.0","result":["0x{:?}","0x0000000000000000000000000000000000000000000000000000000000000000","0x0000800000000000000000000000000000000000000000000000000000000000","0x1"],"id":1}}"#, hash, ); assert_eq!(eth_tester.io.handle_request_sync(request), Some(work_response.to_owned())); diff --git a/rpc/src/v1/tests/mocked/eth_signing.rs b/rpc/src/v1/tests/mocked/eth_signing.rs index e9316da61..f06d4027a 100644 --- a/rpc/src/v1/tests/mocked/eth_signing.rs +++ b/rpc/src/v1/tests/mocked/eth_signing.rs @@ -104,7 +104,7 @@ fn should_post_sign_to_queue() { ], "id": 1 }"#; - let response = r#"{"jsonrpc":"2.0","result":"0x01","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x1","id":1}"#; // then assert_eq!(tester.io.handle_request_sync(&request), Some(response.to_owned())); diff --git a/rpc/src/v1/tests/mocked/ethcore.rs b/rpc/src/v1/tests/mocked/ethcore.rs index 056bdf25d..d8121b6d6 100644 --- a/rpc/src/v1/tests/mocked/ethcore.rs +++ b/rpc/src/v1/tests/mocked/ethcore.rs @@ -126,7 +126,7 @@ fn rpc_ethcore_min_gas_price() { io.add_delegate(ethcore_client(&client, &miner, &sync, &net).to_delegate()); let request = r#"{"jsonrpc": "2.0", "method": "ethcore_minGasPrice", "params": [], "id": 1}"#; - let response = r#"{"jsonrpc":"2.0","result":"0x01312d00","id":1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x1312d00","id":1}"#; assert_eq!(io.handle_request_sync(request), Some(response.to_owned())); } diff --git a/rpc/src/v1/tests/mocked/personal_signer.rs b/rpc/src/v1/tests/mocked/personal_signer.rs index eacbf2404..976b232cc 100644 --- a/rpc/src/v1/tests/mocked/personal_signer.rs +++ b/rpc/src/v1/tests/mocked/personal_signer.rs @@ -86,8 +86,8 @@ fn should_return_list_of_items_to_confirm() { let request = r#"{"jsonrpc":"2.0","method":"personal_requestsToConfirm","params":[],"id":1}"#; let response = concat!( r#"{"jsonrpc":"2.0","result":["#, - r#"{"id":"0x01","payload":{"transaction":{"data":"0x","from":"0x0000000000000000000000000000000000000001","gas":"0x989680","gasPrice":"0x2710","nonce":null,"to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","value":"0x01"}}},"#, - r#"{"id":"0x02","payload":{"sign":{"address":"0x0000000000000000000000000000000000000001","hash":"0x0000000000000000000000000000000000000000000000000000000000000005"}}}"#, + r#"{"id":"0x1","payload":{"transaction":{"data":"0x","from":"0x0000000000000000000000000000000000000001","gas":"0x989680","gasPrice":"0x2710","nonce":null,"to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","value":"0x1"}}},"#, + r#"{"id":"0x2","payload":{"sign":{"address":"0x0000000000000000000000000000000000000001","hash":"0x0000000000000000000000000000000000000000000000000000000000000005"}}}"#, r#"],"id":1}"# ); @@ -112,7 +112,7 @@ fn should_reject_transaction_from_queue_without_dispatching() { assert_eq!(tester.queue.requests().len(), 1); // when - let request = r#"{"jsonrpc":"2.0","method":"personal_rejectRequest","params":["0x01"],"id":1}"#; + let request = r#"{"jsonrpc":"2.0","method":"personal_rejectRequest","params":["0x1"],"id":1}"#; let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#; // then @@ -137,7 +137,7 @@ fn should_not_remove_transaction_if_password_is_invalid() { assert_eq!(tester.queue.requests().len(), 1); // when - let request = r#"{"jsonrpc":"2.0","method":"personal_confirmRequest","params":["0x01",{},"xxx"],"id":1}"#; + let request = r#"{"jsonrpc":"2.0","method":"personal_confirmRequest","params":["0x1",{},"xxx"],"id":1}"#; let response = r#"{"jsonrpc":"2.0","error":{"code":-32021,"message":"Account password is invalid or account does not exist.","data":"SStore(InvalidAccount)"},"id":1}"#; // then @@ -153,7 +153,7 @@ fn should_not_remove_sign_if_password_is_invalid() { assert_eq!(tester.queue.requests().len(), 1); // when - let request = r#"{"jsonrpc":"2.0","method":"personal_confirmRequest","params":["0x01",{},"xxx"],"id":1}"#; + let request = r#"{"jsonrpc":"2.0","method":"personal_confirmRequest","params":["0x1",{},"xxx"],"id":1}"#; let response = r#"{"jsonrpc":"2.0","error":{"code":-32021,"message":"Account password is invalid or account does not exist.","data":"SStore(InvalidAccount)"},"id":1}"#; // then @@ -195,7 +195,7 @@ fn should_confirm_transaction_and_dispatch() { let request = r#"{ "jsonrpc":"2.0", "method":"personal_confirmRequest", - "params":["0x01", {"gasPrice":"0x1000"}, "test"], + "params":["0x1", {"gasPrice":"0x1000"}, "test"], "id":1 }"#; let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#; diff --git a/rpc/src/v1/types/block.rs b/rpc/src/v1/types/block.rs index bbdd8d696..21459d026 100644 --- a/rpc/src/v1/types/block.rs +++ b/rpc/src/v1/types/block.rs @@ -103,7 +103,7 @@ mod tests { fn test_serialize_block_transactions() { let t = BlockTransactions::Full(vec![Transaction::default()]); let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"[{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x00","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x00","gasPrice":"0x00","gas":"0x00","input":"0x","creates":null,"raw":"0x"}]"#); + assert_eq!(serialized, r#"[{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x"}]"#); let t = BlockTransactions::Hashes(vec![H256::default().into()]); let serialized = serde_json::to_string(&t).unwrap(); @@ -136,6 +136,6 @@ mod tests { }; let serialized = serde_json::to_string(&block).unwrap(); - assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","author":"0x0000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","number":"0x00","gasUsed":"0x00","gasLimit":"0x00","extraData":"0x","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x00","difficulty":"0x00","totalDifficulty":"0x00","sealFields":["0x","0x"],"uncles":[],"transactions":[],"size":"0x45"}"#); + assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","author":"0x0000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","number":"0x0","gasUsed":"0x0","gasLimit":"0x0","extraData":"0x","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","difficulty":"0x0","totalDifficulty":"0x0","sealFields":["0x","0x"],"uncles":[],"transactions":[],"size":"0x45"}"#); } } diff --git a/rpc/src/v1/types/confirmations.rs b/rpc/src/v1/types/confirmations.rs index e2cea32de..c074df443 100644 --- a/rpc/src/v1/types/confirmations.rs +++ b/rpc/src/v1/types/confirmations.rs @@ -96,7 +96,7 @@ mod tests { // when let res = serde_json::to_string(&ConfirmationRequest::from(request)); - let expected = r#"{"id":"0x0f","payload":{"sign":{"address":"0x0000000000000000000000000000000000000001","hash":"0x0000000000000000000000000000000000000000000000000000000000000005"}}}"#; + let expected = r#"{"id":"0xf","payload":{"sign":{"address":"0x0000000000000000000000000000000000000001","hash":"0x0000000000000000000000000000000000000000000000000000000000000005"}}}"#; // then assert_eq!(res.unwrap(), expected.to_owned()); @@ -120,7 +120,7 @@ mod tests { // when let res = serde_json::to_string(&ConfirmationRequest::from(request)); - let expected = r#"{"id":"0x0f","payload":{"transaction":{"from":"0x0000000000000000000000000000000000000000","to":null,"gasPrice":"0x2710","gas":"0x3a98","value":"0x0186a0","data":"0x010203","nonce":"0x01"}}}"#; + let expected = r#"{"id":"0xf","payload":{"transaction":{"from":"0x0000000000000000000000000000000000000000","to":null,"gasPrice":"0x2710","gas":"0x3a98","value":"0x186a0","data":"0x010203","nonce":"0x1"}}}"#; // then assert_eq!(res.unwrap(), expected.to_owned()); @@ -130,7 +130,7 @@ mod tests { fn should_deserialize_modification() { // given let s1 = r#"{ - "gasPrice":"0x0ba43b7400" + "gasPrice":"0xba43b7400" }"#; let s2 = r#"{}"#; diff --git a/rpc/src/v1/types/log.rs b/rpc/src/v1/types/log.rs index 9d9261d94..b945de6ea 100644 --- a/rpc/src/v1/types/log.rs +++ b/rpc/src/v1/types/log.rs @@ -86,7 +86,7 @@ mod tests { #[test] fn log_serialization() { - let s = r#"{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00","logIndex":"0x01","type":"mined"}"#; + let s = r#"{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x4510c","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","logIndex":"0x1","type":"mined"}"#; let log = Log { address: H160::from_str("33990122638b9132ca29c723bdf037f1a891a70c").unwrap(), diff --git a/rpc/src/v1/types/receipt.rs b/rpc/src/v1/types/receipt.rs index a91d38c87..066195cbf 100644 --- a/rpc/src/v1/types/receipt.rs +++ b/rpc/src/v1/types/receipt.rs @@ -98,7 +98,7 @@ mod tests { #[test] fn receipt_serialization() { - let s = r#"{"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","cumulativeGasUsed":"0x20","gasUsed":"0x10","contractAddress":null,"logs":[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00","logIndex":"0x01","type":"mined"}]}"#; + let s = r#"{"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x4510c","cumulativeGasUsed":"0x20","gasUsed":"0x10","contractAddress":null,"logs":[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x4510c","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","logIndex":"0x1","type":"mined"}]}"#; let receipt = Receipt { transaction_hash: Some(H256::from(0)), diff --git a/rpc/src/v1/types/sync.rs b/rpc/src/v1/types/sync.rs index cf94120ec..28ef3101d 100644 --- a/rpc/src/v1/types/sync.rs +++ b/rpc/src/v1/types/sync.rs @@ -70,7 +70,7 @@ mod tests { fn test_serialize_sync_info() { let t = SyncInfo::default(); let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"{"startingBlock":"0x00","currentBlock":"0x00","highestBlock":"0x00"}"#); + assert_eq!(serialized, r#"{"startingBlock":"0x0","currentBlock":"0x0","highestBlock":"0x0"}"#); } #[test] @@ -88,6 +88,6 @@ mod tests { let t = SyncStatus::Info(SyncInfo::default()); let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"{"startingBlock":"0x00","currentBlock":"0x00","highestBlock":"0x00"}"#); + assert_eq!(serialized, r#"{"startingBlock":"0x0","currentBlock":"0x0","highestBlock":"0x0"}"#); } } diff --git a/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index cee96e0c7..e401a6cee 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -549,7 +549,7 @@ mod tests { block_hash: H256::from(14), }; let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"{"action":{"call":{"from":"0x0000000000000000000000000000000000000004","to":"0x0000000000000000000000000000000000000005","value":"0x06","gas":"0x07","input":"0x1234","callType":"call"}},"result":{"call":{"gasUsed":"0x08","output":"0x5678"}},"traceAddress":["0x0a"],"subtraces":"0x01","transactionPosition":"0x0b","transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":"0x0d","blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); + assert_eq!(serialized, r#"{"action":{"call":{"from":"0x0000000000000000000000000000000000000004","to":"0x0000000000000000000000000000000000000005","value":"0x6","gas":"0x7","input":"0x1234","callType":"call"}},"result":{"call":{"gasUsed":"0x8","output":"0x5678"}},"traceAddress":["0xa"],"subtraces":"0x1","transactionPosition":"0xb","transactionHash":"0x000000000000000000000000000000000000000000000000000000000000000c","blockNumber":"0xd","blockHash":"0x000000000000000000000000000000000000000000000000000000000000000e"}"#); } #[test] @@ -614,7 +614,7 @@ mod tests { } ]); let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"{"0x000000000000000000000000000000000000002a":{"balance":"=","nonce":{"+":"0x01"},"code":"=","storage":{"0x000000000000000000000000000000000000000000000000000000000000002a":"="}},"0x0000000000000000000000000000000000000045":{"balance":"=","nonce":{"*":{"from":"0x01","to":"0x00"}},"code":{"-":"0x60"},"storage":{}}}"#); + assert_eq!(serialized, r#"{"0x000000000000000000000000000000000000002a":{"balance":"=","nonce":{"+":"0x1"},"code":"=","storage":{"0x000000000000000000000000000000000000000000000000000000000000002a":"="}},"0x0000000000000000000000000000000000000045":{"balance":"=","nonce":{"*":{"from":"0x1","to":"0x0"}},"code":{"-":"0x60"},"storage":{}}}"#); } #[test] @@ -634,7 +634,7 @@ mod tests { })]; let serialized = serde_json::to_string(&actions).unwrap(); - assert_eq!(serialized, r#"[{"call":{"from":"0x0000000000000000000000000000000000000001","to":"0x0000000000000000000000000000000000000002","value":"0x03","gas":"0x04","input":"0x1234","callType":"call"}},{"create":{"from":"0x0000000000000000000000000000000000000005","value":"0x06","gas":"0x07","init":"0x5678"}}]"#); + assert_eq!(serialized, r#"[{"call":{"from":"0x0000000000000000000000000000000000000001","to":"0x0000000000000000000000000000000000000002","value":"0x3","gas":"0x4","input":"0x1234","callType":"call"}},{"create":{"from":"0x0000000000000000000000000000000000000005","value":"0x6","gas":"0x7","init":"0x5678"}}]"#); } #[test] @@ -654,6 +654,6 @@ mod tests { ]; let serialized = serde_json::to_string(&results).unwrap(); - assert_eq!(serialized, r#"[{"call":{"gasUsed":"0x01","output":"0x1234"}},{"create":{"gasUsed":"0x02","code":"0x4556","address":"0x0000000000000000000000000000000000000003"}},"failedCall","failedCreate"]"#); + assert_eq!(serialized, r#"[{"call":{"gasUsed":"0x1","output":"0x1234"}},{"create":{"gasUsed":"0x2","code":"0x4556","address":"0x0000000000000000000000000000000000000003"}},"failedCall","failedCreate"]"#); } } diff --git a/rpc/src/v1/types/transaction.rs b/rpc/src/v1/types/transaction.rs index 812c006e4..a2d8abc3e 100644 --- a/rpc/src/v1/types/transaction.rs +++ b/rpc/src/v1/types/transaction.rs @@ -115,7 +115,7 @@ mod tests { fn test_transaction_serialize() { let t = Transaction::default(); let serialized = serde_json::to_string(&t).unwrap(); - assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x00","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x00","gasPrice":"0x00","gas":"0x00","input":"0x","creates":null,"raw":"0x"}"#); + assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":"0x","creates":null,"raw":"0x"}"#); } } diff --git a/rpc/src/v1/types/uint.rs b/rpc/src/v1/types/uint.rs index b1cb0521f..bcd874a18 100644 --- a/rpc/src/v1/types/uint.rs +++ b/rpc/src/v1/types/uint.rs @@ -52,7 +52,13 @@ macro_rules! impl_uint { let mut bytes = [0u8; 8 * $size]; self.0.to_big_endian(&mut bytes); let len = cmp::max((self.0.bits() + 7) / 8, 1); - hex.push_str(&bytes[bytes.len() - len..].to_hex()); + let bytes_hex = bytes[bytes.len() - len..].to_hex(); + + if bytes_hex.starts_with('0') { + hex.push_str(&bytes_hex[1..]); + } else { + hex.push_str(&bytes_hex); + } serializer.serialize_str(&hex) } } @@ -87,3 +93,38 @@ macro_rules! impl_uint { } impl_uint!(U256, EthU256, 4); + + +#[cfg(test)] +mod tests { + use super::U256; + use serde_json; + + #[test] + fn should_serialize_u256() { + let serialized1 = serde_json::to_string(&U256(0.into())).unwrap(); + let serialized2 = serde_json::to_string(&U256(1.into())).unwrap(); + let serialized3 = serde_json::to_string(&U256(16.into())).unwrap(); + let serialized4 = serde_json::to_string(&U256(256.into())).unwrap(); + + assert_eq!(serialized1, r#""0x0""#); + assert_eq!(serialized2, r#""0x1""#); + assert_eq!(serialized3, r#""0x10""#); + assert_eq!(serialized4, r#""0x100""#); + } + + #[test] + fn should_deserialize_u256() { + let deserialized1: U256 = serde_json::from_str(r#""0x""#).unwrap(); + let deserialized2: U256 = serde_json::from_str(r#""0x0""#).unwrap(); + let deserialized3: U256 = serde_json::from_str(r#""0x1""#).unwrap(); + let deserialized4: U256 = serde_json::from_str(r#""0x01""#).unwrap(); + let deserialized5: U256 = serde_json::from_str(r#""0x100""#).unwrap(); + + assert_eq!(deserialized1, U256(0.into())); + assert_eq!(deserialized2, U256(0.into())); + assert_eq!(deserialized3, U256(1.into())); + assert_eq!(deserialized4, U256(1.into())); + assert_eq!(deserialized5, U256(256.into())); + } +}