From 0e088d783d20a1dc104eaf82d600ccdc3b57885d Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 30 Aug 2017 16:20:21 +0200 Subject: [PATCH 01/16] Hashable::sha3 -> fn keccak for ethcore-util --- Cargo.lock | 23 +-- ethash/Cargo.toml | 4 +- util/Cargo.toml | 2 +- util/bloomable/Cargo.toml | 2 +- util/bloomable/tests/test.rs | 24 ++-- util/{sha3 => hash}/Cargo.toml | 9 +- util/{sha3 => hash}/build.rs | 0 util/hash/src/lib.rs | 106 ++++++++++++++ util/{sha3 => hash}/src/tinykeccak.c | 24 ++-- util/sha3/src/lib.rs | 20 --- util/src/hashdb.rs | 10 +- util/src/journaldb/archivedb.rs | 95 ++++++------- util/src/journaldb/earlymergedb.rs | 187 ++++++++++++------------ util/src/journaldb/overlayrecentdb.rs | 195 +++++++++++++------------- util/src/journaldb/refcounteddb.rs | 40 +++--- util/src/lib.rs | 4 +- util/src/memorydb.rs | 19 +-- util/src/sha3.rs | 123 ---------------- util/src/trie/fatdb.rs | 10 +- util/src/trie/fatdbmut.rs | 14 +- util/src/trie/mod.rs | 3 +- util/src/trie/recorder.rs | 11 +- util/src/trie/sectriedb.rs | 8 +- util/src/trie/sectriedbmut.rs | 12 +- util/src/trie/standardmap.rs | 8 +- util/src/trie/triedbmut.rs | 29 ++-- util/src/triehash.rs | 8 +- 27 files changed, 483 insertions(+), 507 deletions(-) rename util/{sha3 => hash}/Cargo.toml (65%) rename util/{sha3 => hash}/build.rs (100%) create mode 100644 util/hash/src/lib.rs rename util/{sha3 => hash}/src/tinykeccak.c (92%) delete mode 100644 util/sha3/src/lib.rs delete mode 100644 util/src/sha3.rs diff --git a/Cargo.lock b/Cargo.lock index e2108d186..c28246791 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,7 +196,7 @@ name = "bloomable" version = "0.1.0" dependencies = [ "ethcore-bigint 0.1.3", - "tiny-keccak 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", ] [[package]] @@ -480,10 +480,10 @@ name = "ethash" version = "1.8.0" dependencies = [ "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "primal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "sha3 0.1.0", ] [[package]] @@ -777,6 +777,7 @@ dependencies = [ "ethcore-bloom-journal 0.1.0", "ethcore-devtools 1.8.0", "ethcore-logger 1.8.0", + "hash 0.1.0", "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -789,7 +790,6 @@ dependencies = [ "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "sha3 0.1.0", "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "tiny-keccak 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1062,6 +1062,16 @@ dependencies = [ "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hash" +version = "0.1.0" +dependencies = [ + "ethcore-bigint 0.1.3", + "gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny-keccak 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "heapsize" version = "0.4.0" @@ -2690,13 +2700,6 @@ name = "sha1" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "sha3" -version = "0.1.0" -dependencies = [ - "gcc 0.3.51 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "shell32-sys" version = "0.1.1" diff --git a/ethash/Cargo.toml b/ethash/Cargo.toml index 87069d6f8..aa415c1f9 100644 --- a/ethash/Cargo.toml +++ b/ethash/Cargo.toml @@ -7,10 +7,10 @@ authors = ["Parity Technologies "] [dependencies] log = "0.3" -sha3 = { path = "../util/sha3" } +hash = { path = "../util/hash" } primal = "0.2.3" parking_lot = "0.4" crunchy = "0.1.0" [features] -benches = [] \ No newline at end of file +benches = [] diff --git a/util/Cargo.toml b/util/Cargo.toml index 0e485431e..d04450053 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -19,7 +19,7 @@ rust-crypto = "0.2.34" elastic-array = "0.9" rlp = { path = "rlp" } heapsize = "0.4" -sha3 = { path = "sha3" } +hash = { path = "hash" } clippy = { version = "0.0.103", optional = true} ethcore-devtools = { path = "../devtools" } libc = "0.2.7" diff --git a/util/bloomable/Cargo.toml b/util/bloomable/Cargo.toml index f85b67943..94ca4856b 100644 --- a/util/bloomable/Cargo.toml +++ b/util/bloomable/Cargo.toml @@ -7,4 +7,4 @@ authors = ["debris "] ethcore-bigint = { path = "../bigint" } [dev-dependencies] -tiny-keccak = "1.3" +hash = { path = "../hash" } diff --git a/util/bloomable/tests/test.rs b/util/bloomable/tests/test.rs index 85ced83e6..f3fa908e2 100644 --- a/util/bloomable/tests/test.rs +++ b/util/bloomable/tests/test.rs @@ -1,14 +1,10 @@ -extern crate tiny_keccak; +extern crate hash; extern crate ethcore_bigint; extern crate bloomable; use ethcore_bigint::hash::{H160, H256, H2048}; use bloomable::Bloomable; -use tiny_keccak::keccak256; - -fn sha3(input: &[u8]) -> H256 { - keccak256(input).into() -} +use hash::keccak; #[test] fn shift_bloomed() { @@ -17,15 +13,15 @@ fn shift_bloomed() { let topic: H256 = "02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc".into(); let mut my_bloom = H2048::default(); - assert!(!my_bloom.contains_bloomed(&sha3(&address))); - assert!(!my_bloom.contains_bloomed(&sha3(&topic))); + assert!(!my_bloom.contains_bloomed(&keccak(&address))); + assert!(!my_bloom.contains_bloomed(&keccak(&topic))); - my_bloom.shift_bloomed(&sha3(&address)); - assert!(my_bloom.contains_bloomed(&sha3(&address))); - assert!(!my_bloom.contains_bloomed(&sha3(&topic))); + my_bloom.shift_bloomed(&keccak(&address)); + assert!(my_bloom.contains_bloomed(&keccak(&address))); + assert!(!my_bloom.contains_bloomed(&keccak(&topic))); - my_bloom.shift_bloomed(&sha3(&topic)); + my_bloom.shift_bloomed(&keccak(&topic)); assert_eq!(my_bloom, bloom); - assert!(my_bloom.contains_bloomed(&sha3(&address))); - assert!(my_bloom.contains_bloomed(&sha3(&topic))); + assert!(my_bloom.contains_bloomed(&keccak(&address))); + assert!(my_bloom.contains_bloomed(&keccak(&topic))); } diff --git a/util/sha3/Cargo.toml b/util/hash/Cargo.toml similarity index 65% rename from util/sha3/Cargo.toml rename to util/hash/Cargo.toml index 8f8d6d160..91a195468 100644 --- a/util/sha3/Cargo.toml +++ b/util/hash/Cargo.toml @@ -2,10 +2,17 @@ description = "Rust bindings for tinykeccak C library" homepage = "http://parity.io" license = "GPL-3.0" -name = "sha3" +name = "hash" version = "0.1.0" authors = ["Parity Technologies "] build = "build.rs" +[dependencies] +ethcore-bigint = { path = "../bigint" } +tiny-keccak = "1.3" + [build-dependencies] gcc = "0.3" + +[dev-dependencies] +tempdir = "0.3" diff --git a/util/sha3/build.rs b/util/hash/build.rs similarity index 100% rename from util/sha3/build.rs rename to util/hash/build.rs diff --git a/util/hash/src/lib.rs b/util/hash/src/lib.rs new file mode 100644 index 000000000..bd0121248 --- /dev/null +++ b/util/hash/src/lib.rs @@ -0,0 +1,106 @@ +// Copyright 2015-2017 Parity Technologies (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +extern crate ethcore_bigint as bigint; +extern crate tiny_keccak; + +use std::io; +use tiny_keccak::Keccak; +pub use bigint::hash::H256; + +/// Get the KECCAK (i.e. Keccak) hash of the empty bytes string. +pub const KECCAK_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 KECCAK of the RLP encoding of empty data. +pub const KECCAK_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 KECCAK of the RLP encoding of empty list. +pub const KECCAK_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] ); + +extern { + pub fn keccak_256(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) -> i32; + pub fn keccak_512(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) -> i32; +} + +pub fn keccak>(s: T) -> H256 { + let mut result = [0u8; 32]; + keccak_into(s, &mut result); + H256(result) +} + +pub fn keccak_into>(s: T, dest: &mut [u8]) { + let input = s.as_ref(); + unsafe { + keccak_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len()); + } +} + +pub fn keccak_buffer(r: &mut io::BufRead) -> Result { + let mut output = [0u8; 32]; + let mut input = [0u8; 1024]; + let mut keccak = Keccak::new_keccak256(); + + // read file + loop { + let some = r.read(&mut input)?; + if some == 0 { + break; + } + keccak.update(&input[0..some]); + } + + keccak.finalize(&mut output); + Ok(output.into()) +} + +#[cfg(test)] +mod tests { + extern crate tempdir; + + use std::fs; + use std::io::{Write, BufReader}; + use self::tempdir::TempDir; + use super::{keccak, keccak_buffer, KECCAK_EMPTY}; + + #[test] + fn keccak_empty() { + assert_eq!(keccak([0u8; 0]), KECCAK_EMPTY); + } + #[test] + fn keccak_as() { + assert_eq!(keccak([0x41u8; 32]), From::from("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8")); + } + + #[test] + fn should_keccak_a_file() { + // given + let tempdir = TempDir::new("keccak").unwrap(); + let mut path = tempdir.path().to_owned(); + path.push("should_keccak_a_file"); + // Prepare file + { + let mut file = fs::File::create(&path).unwrap(); + file.write_all(b"something").unwrap(); + } + + let mut file = BufReader::new(fs::File::open(&path).unwrap()); + // when + let hash = keccak_buffer(&mut file).unwrap(); + + // then + assert_eq!(format!("{:?}", hash), "68371d7e884c168ae2022c82bd837d51837718a7f7dfb7aa3f753074a35e1d87"); + } +} diff --git a/util/sha3/src/tinykeccak.c b/util/hash/src/tinykeccak.c similarity index 92% rename from util/sha3/src/tinykeccak.c rename to util/hash/src/tinykeccak.c index 1f93ce2db..bfe172e5f 100644 --- a/util/sha3/src/tinykeccak.c +++ b/util/hash/src/tinykeccak.c @@ -13,15 +13,15 @@ #define decshake(bits) \ int shake##bits(uint8_t*, size_t, const uint8_t*, size_t); -#define decsha3(bits) \ - int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t); +#define deckeccak(bits) \ + int keccak_##bits(uint8_t*, size_t, const uint8_t*, size_t); decshake(128) decshake(256) -decsha3(224) -decsha3(256) -decsha3(384) -decsha3(512) +deckeccak(224) +deckeccak(256) +deckeccak(384) +deckeccak(512) /******** The Keccak-f[1600] permutation ********/ @@ -154,8 +154,8 @@ static inline int hash(uint8_t* out, size_t outlen, const uint8_t* in, size_t inlen) { \ return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ } -#define defsha3(bits) \ - int sha3_##bits(uint8_t* out, size_t outlen, \ +#define defkeccak(bits) \ + int keccak_##bits(uint8_t* out, size_t outlen, \ const uint8_t* in, size_t inlen) { \ if (outlen > (bits/8)) { \ return -1; \ @@ -168,10 +168,10 @@ defshake(128) defshake(256) /*** FIPS202 SHA3 FOFs ***/ -defsha3(224) -defsha3(256) -defsha3(384) -defsha3(512) +defkeccak(224) +defkeccak(256) +defkeccak(384) +defkeccak(512) diff --git a/util/sha3/src/lib.rs b/util/sha3/src/lib.rs deleted file mode 100644 index 77ecd84c3..000000000 --- a/util/sha3/src/lib.rs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -extern { - pub fn sha3_256(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) -> i32; - pub fn sha3_512(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) -> i32; -} diff --git a/util/src/hashdb.rs b/util/src/hashdb.rs index ca4120702..3b52a2fec 100644 --- a/util/src/hashdb.rs +++ b/util/src/hashdb.rs @@ -48,14 +48,15 @@ pub trait HashDB: AsHashDB + Send + Sync { /// /// # Examples /// ```rust + /// extern crate hash; /// extern crate ethcore_util; /// use ethcore_util::hashdb::*; /// use ethcore_util::memorydb::*; - /// use ethcore_util::sha3::*; + /// use hash::keccak; /// fn main() { /// let mut m = MemoryDB::new(); /// let hello_bytes = "Hello world!".as_bytes(); - /// assert!(!m.contains(&hello_bytes.sha3())); + /// assert!(!m.contains(&keccak(hello_bytes))); /// let key = m.insert(hello_bytes); /// assert!(m.contains(&key)); /// m.remove(&key); @@ -91,13 +92,14 @@ pub trait HashDB: AsHashDB + Send + Sync { /// # Examples /// ```rust /// extern crate ethcore_util; + /// extern crate hash; /// use ethcore_util::hashdb::*; /// use ethcore_util::memorydb::*; - /// use ethcore_util::sha3::*; + /// use hash::keccak; /// fn main() { /// let mut m = MemoryDB::new(); /// let d = "Hello world!".as_bytes(); - /// let key = &d.sha3(); + /// let key = &keccak(d); /// m.remove(key); // OK - we now owe an insertion. /// assert!(!m.contains(key)); /// m.remove(key); // OK - we now owe two insertions. diff --git a/util/src/journaldb/archivedb.rs b/util/src/journaldb/archivedb.rs index 8c87ef623..3f4b948f3 100644 --- a/util/src/journaldb/archivedb.rs +++ b/util/src/journaldb/archivedb.rs @@ -205,11 +205,12 @@ mod tests { #![cfg_attr(feature="dev", allow(similar_names))] use std::path::Path; + use keccak::keccak; use hashdb::{HashDB, DBValue}; use super::*; use journaldb::traits::JournalDB; use kvdb::Database; - use {Hashable, H32}; + use {H32}; #[test] fn insert_same_in_fork() { @@ -217,18 +218,18 @@ mod tests { let mut jdb = ArchiveDB::new_temp(); let x = jdb.insert(b"X"); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); - jdb.commit_batch(3, &b"1002a".sha3(), Some((1, b"1".sha3()))).unwrap(); - jdb.commit_batch(4, &b"1003a".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); + jdb.commit_batch(3, &keccak(b"1002a"), Some((1, keccak(b"1")))).unwrap(); + jdb.commit_batch(4, &keccak(b"1003a"), Some((2, keccak(b"2")))).unwrap(); jdb.remove(&x); - jdb.commit_batch(3, &b"1002b".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"1002b"), Some((1, keccak(b"1")))).unwrap(); let x = jdb.insert(b"X"); - jdb.commit_batch(4, &b"1003b".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"1003b"), Some((2, keccak(b"2")))).unwrap(); - jdb.commit_batch(5, &b"1004a".sha3(), Some((3, b"1002a".sha3()))).unwrap(); - jdb.commit_batch(6, &b"1005a".sha3(), Some((4, b"1003a".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"1004a"), Some((3, keccak(b"1002a")))).unwrap(); + jdb.commit_batch(6, &keccak(b"1005a"), Some((4, keccak(b"1003a")))).unwrap(); assert!(jdb.contains(&x)); } @@ -238,16 +239,16 @@ mod tests { // history is 3 let mut jdb = ArchiveDB::new_temp(); let h = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&h)); jdb.remove(&h); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.contains(&h)); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.contains(&h)); - jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.contains(&h)); - jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.contains(&h)); } @@ -256,13 +257,13 @@ mod tests { fn multiple_owed_removal_not_allowed() { let mut jdb = ArchiveDB::new_temp(); let h = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&h)); jdb.remove(&h); jdb.remove(&h); // commit_batch would call journal_under(), // and we don't allow multiple owned removals. - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); } #[test] @@ -272,29 +273,29 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); jdb.remove(&foo); jdb.remove(&bar); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); assert!(jdb.contains(&baz)); let foo = jdb.insert(b"foo"); jdb.remove(&baz); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&baz)); jdb.remove(&foo); - jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.contains(&foo)); - jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap(); } #[test] @@ -304,22 +305,22 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); jdb.remove(&foo); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); jdb.remove(&bar); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); assert!(jdb.contains(&baz)); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.contains(&foo)); } @@ -329,16 +330,16 @@ mod tests { let mut jdb = ArchiveDB::new_temp(); let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&foo)); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); jdb.insert(b"foo"); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.contains(&foo)); - jdb.commit_batch(3, &b"2".sha3(), Some((0, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"2"), Some((0, keccak(b"2")))).unwrap(); assert!(jdb.contains(&foo)); } @@ -346,16 +347,16 @@ mod tests { fn fork_same_key() { // history is 1 let mut jdb = ArchiveDB::new_temp(); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2a".sha3(), Some((1, b"1a".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2a"), Some((1, keccak(b"1a")))).unwrap(); assert!(jdb.contains(&foo)); } @@ -375,21 +376,21 @@ mod tests { // history is 1 let foo = jdb.insert(b"foo"); jdb.emplace(bar.clone(), DBValue::from_slice(b"bar")); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); foo }; { let mut jdb = new_db(&dir); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); } { let mut jdb = new_db(&dir); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); } } @@ -402,24 +403,24 @@ mod tests { let mut jdb = new_db(&dir); // history is 1 let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); // foo is ancient history. jdb.insert(b"foo"); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); foo }; { let mut jdb = new_db(&dir); jdb.remove(&foo); - jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.contains(&foo)); jdb.remove(&foo); - jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap(); - jdb.commit_batch(5, &b"5".sha3(), Some((4, b"4".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((4, keccak(b"4")))).unwrap(); } } @@ -432,19 +433,19 @@ mod tests { // history is 1 let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); jdb.remove(&foo); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); jdb.remove(&bar); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); (foo, bar, baz) }; { let mut jdb = new_db(&dir); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.contains(&foo)); } } @@ -456,7 +457,7 @@ mod tests { let key = { let mut jdb = new_db(temp.as_path().as_path()); let key = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); key }; diff --git a/util/src/journaldb/earlymergedb.rs b/util/src/journaldb/earlymergedb.rs index 0d0681a03..dce7660f8 100644 --- a/util/src/journaldb/earlymergedb.rs +++ b/util/src/journaldb/earlymergedb.rs @@ -577,12 +577,13 @@ mod tests { #![cfg_attr(feature="dev", allow(similar_names))] use std::path::Path; + use keccak::keccak; use hashdb::{HashDB, DBValue}; use super::*; use super::super::traits::JournalDB; use ethcore_logger::init_log; use kvdb::{DatabaseConfig}; - use {Hashable, H32}; + use {H32}; #[test] fn insert_same_in_fork() { @@ -590,25 +591,25 @@ mod tests { let mut jdb = EarlyMergeDB::new_temp(); let x = jdb.insert(b"X"); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(3, &b"1002a".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"1002a"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(4, &b"1003a".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"1003a"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&x); - jdb.commit_batch(3, &b"1002b".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"1002b"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); let x = jdb.insert(b"X"); - jdb.commit_batch(4, &b"1003b".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"1003b"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(5, &b"1004a".sha3(), Some((3, b"1002a".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"1004a"), Some((3, keccak(b"1002a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(6, &b"1005a".sha3(), Some((4, b"1003a".sha3()))).unwrap(); + jdb.commit_batch(6, &keccak(b"1005a"), Some((4, keccak(b"1003a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&x)); @@ -618,17 +619,17 @@ mod tests { fn insert_older_era() { let mut jdb = EarlyMergeDB::new_temp(); let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0a".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let bar = jdb.insert(b"bar"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0a".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&bar); - jdb.commit_batch(0, &b"0b".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); @@ -639,20 +640,20 @@ mod tests { // history is 3 let mut jdb = EarlyMergeDB::new_temp(); let h = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); jdb.remove(&h); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); - jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); - jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&h)); } @@ -664,7 +665,7 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); @@ -672,7 +673,7 @@ mod tests { jdb.remove(&foo); jdb.remove(&bar); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); @@ -680,20 +681,20 @@ mod tests { let foo = jdb.insert(b"foo"); jdb.remove(&baz); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&bar)); assert!(jdb.contains(&baz)); jdb.remove(&foo); - jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&bar)); assert!(!jdb.contains(&baz)); - jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); assert!(!jdb.contains(&bar)); @@ -707,25 +708,25 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); jdb.remove(&foo); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&bar); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); assert!(jdb.contains(&baz)); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&baz)); @@ -738,19 +739,19 @@ mod tests { let mut jdb = EarlyMergeDB::new_temp(); let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); - jdb.commit_batch(3, &b"2".sha3(), Some((0, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"2"), Some((0, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); } @@ -759,24 +760,24 @@ mod tests { fn fork_same_key_one() { let mut jdb = EarlyMergeDB::new_temp(); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1c".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1c"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2a".sha3(), Some((1, b"1a".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2a"), Some((1, keccak(b"1a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); } @@ -784,24 +785,24 @@ mod tests { #[test] fn fork_same_key_other() { let mut jdb = EarlyMergeDB::new_temp(); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1c".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1c"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); } @@ -809,33 +810,33 @@ mod tests { #[test] fn fork_ins_del_ins() { let mut jdb = EarlyMergeDB::new_temp(); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(2, &b"2a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(2, &b"2b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3a".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3a"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3b".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3b"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(4, &b"4a".sha3(), Some((2, b"2a".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4a"), Some((2, keccak(b"2a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(5, &b"5a".sha3(), Some((3, b"3a".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5a"), Some((3, keccak(b"3a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -856,7 +857,7 @@ mod tests { // history is 1 let foo = jdb.insert(b"foo"); jdb.emplace(bar.clone(), DBValue::from_slice(b"bar")); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); foo }; @@ -864,7 +865,7 @@ mod tests { { let mut jdb = new_db(&dir); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -872,7 +873,7 @@ mod tests { let mut jdb = new_db(&dir); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); } @@ -886,22 +887,22 @@ mod tests { // history is 4 let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(3, &b"3".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(4, &b"4".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // expunge foo - jdb.commit_batch(5, &b"5".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -912,43 +913,43 @@ mod tests { // history is 4 let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(1, &b"1a".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(1, &b"1b".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(2, &b"2a".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(2, &b"2b".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(3, &b"3a".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(3, &b"3b".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(4, &b"4a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(4, &b"4b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // expunge foo - jdb.commit_batch(5, &b"5".sha3(), Some((1, b"1a".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -958,25 +959,25 @@ mod tests { // history is 1 let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // foo is ancient history. jdb.remove(&foo); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap(); // BROKEN + jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); // BROKEN assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); jdb.remove(&foo); - jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(5, &b"5".sha3(), Some((4, b"4".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((4, keccak(b"4")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); } @@ -987,30 +988,30 @@ mod tests { // history is 4 let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(3, &b"3".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(4, &b"4".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // foo is ancient history. jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(5, &b"5".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); jdb.remove(&bar); - jdb.commit_batch(6, &b"6".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(6, &keccak(b"6"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); jdb.insert(b"bar"); - jdb.commit_batch(7, &b"7".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(7, &keccak(b"7"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -1021,26 +1022,26 @@ mod tests { let mut dir = ::std::env::temp_dir(); dir.push(H32::random().hex()); - let foo = b"foo".sha3(); + let foo = keccak(b"foo"); { let mut jdb = new_db(&dir); // history is 1 jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); // foo is ancient history. jdb.remove(&foo); - jdb.commit_batch(2, &b"2".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); @@ -1049,7 +1050,7 @@ mod tests { let mut jdb = new_db(&dir); jdb.remove(&foo); - jdb.commit_batch(4, &b"4".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); @@ -1057,7 +1058,7 @@ mod tests { }; { let mut jdb = new_db(&dir); - jdb.commit_batch(5, &b"5".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); @@ -1065,7 +1066,7 @@ mod tests { }; { let mut jdb = new_db(&dir); - jdb.commit_batch(6, &b"6".sha3(), Some((4, b"4".sha3()))).unwrap(); + jdb.commit_batch(6, &keccak(b"6"), Some((4, keccak(b"4")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); } @@ -1080,22 +1081,22 @@ mod tests { // history is 1 let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&bar); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); (foo, bar, baz) }; { let mut jdb = new_db(&dir); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&baz)); diff --git a/util/src/journaldb/overlayrecentdb.rs b/util/src/journaldb/overlayrecentdb.rs index 975abd8e8..d2f6cc741 100644 --- a/util/src/journaldb/overlayrecentdb.rs +++ b/util/src/journaldb/overlayrecentdb.rs @@ -461,12 +461,13 @@ mod tests { #![cfg_attr(feature="dev", allow(similar_names))] use std::path::Path; + use keccak::keccak; use super::*; use hashdb::{HashDB, DBValue}; use ethcore_logger::init_log; use journaldb::JournalDB; use kvdb::Database; - use {H32, Hashable}; + use {H32}; fn new_db(path: &Path) -> OverlayRecentDB { let backing = Arc::new(Database::open_default(path.to_str().unwrap()).unwrap()); @@ -479,25 +480,25 @@ mod tests { let mut jdb = OverlayRecentDB::new_temp(); let x = jdb.insert(b"X"); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(3, &b"1002a".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"1002a"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(4, &b"1003a".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"1003a"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&x); - jdb.commit_batch(3, &b"1002b".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"1002b"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); let x = jdb.insert(b"X"); - jdb.commit_batch(4, &b"1003b".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"1003b"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(5, &b"1004a".sha3(), Some((3, b"1002a".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"1004a"), Some((3, keccak(b"1002a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(6, &b"1005a".sha3(), Some((4, b"1003a".sha3()))).unwrap(); + jdb.commit_batch(6, &keccak(b"1005a"), Some((4, keccak(b"1003a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&x)); @@ -508,20 +509,20 @@ mod tests { // history is 3 let mut jdb = OverlayRecentDB::new_temp(); let h = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); jdb.remove(&h); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); - jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&h)); - jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&h)); } @@ -533,7 +534,7 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); @@ -541,7 +542,7 @@ mod tests { jdb.remove(&foo); jdb.remove(&bar); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); @@ -549,20 +550,20 @@ mod tests { let foo = jdb.insert(b"foo"); jdb.remove(&baz); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&bar)); assert!(jdb.contains(&baz)); jdb.remove(&foo); - jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&bar)); assert!(!jdb.contains(&baz)); - jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); assert!(!jdb.contains(&bar)); @@ -576,25 +577,25 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); jdb.remove(&foo); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&bar); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); assert!(jdb.contains(&baz)); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&baz)); @@ -607,19 +608,19 @@ mod tests { let mut jdb = OverlayRecentDB::new_temp(); let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); - jdb.commit_batch(3, &b"2".sha3(), Some((0, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"2"), Some((0, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); } @@ -627,24 +628,24 @@ mod tests { #[test] fn fork_same_key_one() { let mut jdb = OverlayRecentDB::new_temp(); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1c".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1c"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2a".sha3(), Some((1, b"1a".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2a"), Some((1, keccak(b"1a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); } @@ -653,24 +654,24 @@ mod tests { fn fork_same_key_other() { let mut jdb = OverlayRecentDB::new_temp(); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1c".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1c"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); } @@ -679,33 +680,33 @@ mod tests { fn fork_ins_del_ins() { let mut jdb = OverlayRecentDB::new_temp(); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(2, &b"2a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(2, &b"2b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3a".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3a"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3b".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3b"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(4, &b"4a".sha3(), Some((2, b"2a".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4a"), Some((2, keccak(b"2a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(5, &b"5a".sha3(), Some((3, b"3a".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5a"), Some((3, keccak(b"3a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -720,7 +721,7 @@ mod tests { // history is 1 let foo = jdb.insert(b"foo"); jdb.emplace(bar.clone(), DBValue::from_slice(b"bar")); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); foo }; @@ -728,7 +729,7 @@ mod tests { { let mut jdb = new_db(&dir); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -736,7 +737,7 @@ mod tests { let mut jdb = new_db(&dir); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); } @@ -749,22 +750,22 @@ mod tests { // history is 4 let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(3, &b"3".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(4, &b"4".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // expunge foo - jdb.commit_batch(5, &b"5".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -775,43 +776,43 @@ mod tests { // history is 4 let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(1, &b"1a".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(1, &b"1b".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(2, &b"2a".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(2, &b"2b".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(3, &b"3a".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); - jdb.commit_batch(3, &b"3b".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(4, &b"4a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(4, &b"4b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // expunge foo - jdb.commit_batch(5, &b"5".sha3(), Some((1, b"1a".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -820,25 +821,25 @@ mod tests { let mut jdb = OverlayRecentDB::new_temp(); let foo = jdb.insert(b"foo"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // foo is ancient history. jdb.remove(&foo); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap(); // BROKEN + jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); // BROKEN assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); jdb.remove(&foo); - jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(5, &b"5".sha3(), Some((4, b"4".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((4, keccak(b"4")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); } @@ -848,30 +849,30 @@ mod tests { let mut jdb = OverlayRecentDB::new_temp(); // history is 4 let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(3, &b"3".sha3(), None).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(4, &b"4".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); // foo is ancient history. jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(5, &b"5".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); jdb.remove(&bar); - jdb.commit_batch(6, &b"6".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(6, &keccak(b"6"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.insert(b"foo"); jdb.insert(b"bar"); - jdb.commit_batch(7, &b"7".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(7, &keccak(b"7"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); } @@ -882,26 +883,26 @@ mod tests { let mut dir = ::std::env::temp_dir(); dir.push(H32::random().hex()); - let foo = b"foo".sha3(); + let foo = keccak(b"foo"); { let mut jdb = new_db(&dir); // history is 1 jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); // foo is ancient history. jdb.remove(&foo); - jdb.commit_batch(2, &b"2".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); jdb.insert(b"foo"); - jdb.commit_batch(3, &b"3".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); @@ -910,7 +911,7 @@ mod tests { let mut jdb = new_db(&dir); jdb.remove(&foo); - jdb.commit_batch(4, &b"4".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); @@ -918,7 +919,7 @@ mod tests { }; { let mut jdb = new_db(&dir); - jdb.commit_batch(5, &b"5".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(5, &keccak(b"5"), Some((3, keccak(b"3")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); @@ -926,7 +927,7 @@ mod tests { }; { let mut jdb = new_db(&dir); - jdb.commit_batch(6, &b"6".sha3(), Some((4, b"4".sha3()))).unwrap(); + jdb.commit_batch(6, &keccak(b"6"), Some((4, keccak(b"4")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(!jdb.contains(&foo)); } @@ -941,22 +942,22 @@ mod tests { // history is 1 let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&foo); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&bar); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.can_reconstruct_refs()); (foo, bar, baz) }; { let mut jdb = new_db(&dir); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.can_reconstruct_refs()); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&baz)); @@ -968,17 +969,17 @@ mod tests { fn insert_older_era() { let mut jdb = OverlayRecentDB::new_temp(); let foo = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0a".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0a"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); let bar = jdb.insert(b"bar"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0a".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0a")))).unwrap(); assert!(jdb.can_reconstruct_refs()); jdb.remove(&bar); - jdb.commit_batch(0, &b"0b".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0b"), None).unwrap(); assert!(jdb.can_reconstruct_refs()); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); @@ -1010,28 +1011,28 @@ mod tests { // single journalled era. let _key = jdb.insert(b"hello!"); let mut batch = jdb.backing().transaction(); - jdb.journal_under(&mut batch, 0, &b"0".sha3()).unwrap(); + jdb.journal_under(&mut batch, 0, &keccak(b"0")).unwrap(); jdb.backing().write_buffered(batch); assert_eq!(jdb.earliest_era(), Some(0)); // second journalled era. let mut batch = jdb.backing().transaction(); - jdb.journal_under(&mut batch, 1, &b"1".sha3()).unwrap(); + jdb.journal_under(&mut batch, 1, &keccak(b"1")).unwrap(); jdb.backing().write_buffered(batch); assert_eq!(jdb.earliest_era(), Some(0)); // single journalled era. let mut batch = jdb.backing().transaction(); - jdb.mark_canonical(&mut batch, 0, &b"0".sha3()).unwrap(); + jdb.mark_canonical(&mut batch, 0, &keccak(b"0")).unwrap(); jdb.backing().write_buffered(batch); assert_eq!(jdb.earliest_era(), Some(1)); // no journalled eras. let mut batch = jdb.backing().transaction(); - jdb.mark_canonical(&mut batch, 1, &b"1".sha3()).unwrap(); + jdb.mark_canonical(&mut batch, 1, &keccak(b"1")).unwrap(); jdb.backing().write_buffered(batch); assert_eq!(jdb.earliest_era(), Some(1)); diff --git a/util/src/journaldb/refcounteddb.rs b/util/src/journaldb/refcounteddb.rs index 1b9c9ded8..d0990b125 100644 --- a/util/src/journaldb/refcounteddb.rs +++ b/util/src/journaldb/refcounteddb.rs @@ -213,26 +213,26 @@ mod tests { #![cfg_attr(feature="dev", allow(blacklisted_name))] #![cfg_attr(feature="dev", allow(similar_names))] + use keccak::keccak; use hashdb::{HashDB, DBValue}; use super::*; use super::super::traits::JournalDB; - use {Hashable}; #[test] fn long_history() { // history is 3 let mut jdb = RefCountedDB::new_temp(); let h = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&h)); jdb.remove(&h); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert!(jdb.contains(&h)); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert!(jdb.contains(&h)); - jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.contains(&h)); - jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1")))).unwrap(); assert!(!jdb.contains(&h)); } @@ -242,16 +242,16 @@ mod tests { let mut jdb = RefCountedDB::new_temp(); assert_eq!(jdb.latest_era(), None); let h = jdb.insert(b"foo"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert_eq!(jdb.latest_era(), Some(0)); jdb.remove(&h); - jdb.commit_batch(1, &b"1".sha3(), None).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), None).unwrap(); assert_eq!(jdb.latest_era(), Some(1)); - jdb.commit_batch(2, &b"2".sha3(), None).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), None).unwrap(); assert_eq!(jdb.latest_era(), Some(2)); - jdb.commit_batch(3, &b"3".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0")))).unwrap(); assert_eq!(jdb.latest_era(), Some(3)); - jdb.commit_batch(4, &b"4".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1")))).unwrap(); assert_eq!(jdb.latest_era(), Some(4)); } @@ -262,32 +262,32 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); jdb.remove(&foo); jdb.remove(&bar); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); assert!(jdb.contains(&baz)); let foo = jdb.insert(b"foo"); jdb.remove(&baz); - jdb.commit_batch(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap(); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&bar)); assert!(jdb.contains(&baz)); jdb.remove(&foo); - jdb.commit_batch(3, &b"3".sha3(), Some((2, b"2".sha3()))).unwrap(); + jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&bar)); assert!(!jdb.contains(&baz)); - jdb.commit_batch(4, &b"4".sha3(), Some((3, b"3".sha3()))).unwrap(); + jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap(); assert!(!jdb.contains(&foo)); assert!(!jdb.contains(&bar)); assert!(!jdb.contains(&baz)); @@ -300,22 +300,22 @@ mod tests { let foo = jdb.insert(b"foo"); let bar = jdb.insert(b"bar"); - jdb.commit_batch(0, &b"0".sha3(), None).unwrap(); + jdb.commit_batch(0, &keccak(b"0"), None).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); jdb.remove(&foo); let baz = jdb.insert(b"baz"); - jdb.commit_batch(1, &b"1a".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap(); jdb.remove(&bar); - jdb.commit_batch(1, &b"1b".sha3(), Some((0, b"0".sha3()))).unwrap(); + jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap(); assert!(jdb.contains(&foo)); assert!(jdb.contains(&bar)); assert!(jdb.contains(&baz)); - jdb.commit_batch(2, &b"2b".sha3(), Some((1, b"1b".sha3()))).unwrap(); + jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap(); assert!(jdb.contains(&foo)); assert!(!jdb.contains(&baz)); assert!(!jdb.contains(&bar)); diff --git a/util/src/lib.rs b/util/src/lib.rs index 46730fe9c..def0b17c8 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -107,6 +107,7 @@ extern crate regex; extern crate lru_cache; extern crate heapsize; extern crate ethcore_logger; +extern crate hash as keccak; #[macro_use] extern crate log as rlog; @@ -117,7 +118,7 @@ pub mod error; pub mod bytes; pub mod misc; pub mod vector; -pub mod sha3; +//pub mod sha3; pub mod hashdb; pub mod memorydb; pub mod migration; @@ -146,7 +147,6 @@ pub use timer::*; pub use error::*; pub use bytes::*; pub use vector::*; -pub use sha3::*; pub use bigint::prelude::*; pub use bigint::hash; diff --git a/util/src/memorydb.rs b/util/src/memorydb.rs index 6c9cb63fe..b3a951569 100644 --- a/util/src/memorydb.rs +++ b/util/src/memorydb.rs @@ -22,7 +22,7 @@ use std::collections::hash_map::Entry; use heapsize::HeapSizeOf; use hash::{H256FastMap, H256}; use rlp::NULL_RLP; -use sha3::*; +use keccak::{KECCAK_NULL_RLP, keccak}; use hashdb::*; /// Reference-counted memory-based `HashDB` implementation. @@ -117,7 +117,7 @@ impl MemoryDB { /// Even when Some is returned, the data is only guaranteed to be useful /// when the refs > 0. pub fn raw(&self, key: &H256) -> Option<(DBValue, i32)> { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return Some((DBValue::from_slice(&NULL_RLP), 1)); } self.data.get(key).cloned() @@ -131,7 +131,7 @@ impl MemoryDB { /// Remove an element and delete it from storage if reference count reaches zero. /// If the value was purged, return the old value. pub fn remove_and_purge(&mut self, key: &H256) -> Option { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return None; } match self.data.entry(key.clone()) { @@ -170,7 +170,7 @@ impl MemoryDB { impl HashDB for MemoryDB { fn get(&self, key: &H256) -> Option { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return Some(DBValue::from_slice(&NULL_RLP)); } @@ -191,7 +191,7 @@ impl HashDB for MemoryDB { } fn contains(&self, key: &H256) -> bool { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return true; } @@ -203,9 +203,9 @@ impl HashDB for MemoryDB { fn insert(&mut self, value: &[u8]) -> H256 { if value == &NULL_RLP { - return SHA3_NULL_RLP.clone(); + return KECCAK_NULL_RLP.clone(); } - let key = value.sha3(); + let key = keccak(value); match self.data.entry(key) { Entry::Occupied(mut entry) => { let &mut (ref mut old_value, ref mut rc) = entry.get_mut(); @@ -241,7 +241,7 @@ impl HashDB for MemoryDB { } fn remove(&mut self, key: &H256) { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return; } @@ -259,12 +259,13 @@ impl HashDB for MemoryDB { #[cfg(test)] mod tests { + use keccak::keccak; use super::*; #[test] fn memorydb_remove_and_purge() { let hello_bytes = b"Hello world!"; - let hello_key = hello_bytes.sha3(); + let hello_key = keccak(hello_bytes); let mut m = MemoryDB::new(); m.remove(&hello_key); diff --git a/util/src/sha3.rs b/util/src/sha3.rs deleted file mode 100644 index 8adcfbfee..000000000 --- a/util/src/sha3.rs +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -//! Wrapper around tiny-keccak crate as well as common hash constants. -extern crate sha3 as sha3_ext; - -use std::io; -use tiny_keccak::Keccak; -use hash::H256; -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. -/// -/// ``` -/// extern crate ethcore_util as util; -/// use std::str::FromStr; -/// use util::sha3::*; -/// use util::hash::*; -/// -/// fn main() { -/// assert_eq!([0u8; 0].sha3(), H256::from_str("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap()); -/// } -/// ``` -pub trait Hashable { - /// Calculate SHA3 of this object. - fn sha3(&self) -> H256; - - /// Calculate SHA3 of this object and place result into dest. - fn sha3_into(&self, dest: &mut [u8]) { - self.sha3().copy_to(dest); - } -} - -impl Hashable for T where T: AsRef<[u8]> { - fn sha3(&self) -> H256 { - 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 { - sha3_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len()); - } - } -} - -/// Calculate SHA3 of given stream. -pub fn sha3(r: &mut io::BufRead) -> Result { - let mut output = [0u8; 32]; - let mut input = [0u8; 1024]; - let mut sha3 = Keccak::new_keccak256(); - - // read file - loop { - let some = r.read(&mut input)?; - if some == 0 { - break; - } - sha3.update(&input[0..some]); - } - - sha3.finalize(&mut output); - Ok(output.into()) -} - -#[cfg(test)] -mod tests { - use std::fs; - use std::io::{Write, BufReader}; - use super::*; - - #[test] - fn sha3_empty() { - assert_eq!([0u8; 0].sha3(), SHA3_EMPTY); - } - #[test] - fn sha3_as() { - assert_eq!([0x41u8; 32].sha3(), From::from("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8")); - } - - #[test] - fn should_sha3_a_file() { - // given - use devtools::RandomTempPath; - let path = RandomTempPath::new(); - // Prepare file - { - let mut file = fs::File::create(&path).unwrap(); - file.write_all(b"something").unwrap(); - } - - let mut file = BufReader::new(fs::File::open(&path).unwrap()); - // when - let hash = sha3(&mut file).unwrap(); - - // then - assert_eq!(format!("{:?}", hash), "68371d7e884c168ae2022c82bd837d51837718a7f7dfb7aa3f753074a35e1d87"); - } -} diff --git a/util/src/trie/fatdb.rs b/util/src/trie/fatdb.rs index f10e6d4bf..0ea5d2d65 100644 --- a/util/src/trie/fatdb.rs +++ b/util/src/trie/fatdb.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use hash::H256; -use sha3::Hashable; +use keccak::keccak; use hashdb::HashDB; use super::{TrieDB, Trie, TrieDBIterator, TrieItem, TrieIterator, Query}; @@ -55,13 +55,13 @@ impl<'db> Trie for FatDB<'db> { } fn contains(&self, key: &[u8]) -> super::Result { - self.raw.contains(&key.sha3()) + self.raw.contains(&keccak(key)) } fn get_with<'a, 'key, Q: Query>(&'a self, key: &'key [u8], query: Q) -> super::Result> where 'a: 'key { - self.raw.get_with(&key.sha3(), query) + self.raw.get_with(&keccak(key), query) } } @@ -83,7 +83,7 @@ impl<'db> FatDBIterator<'db> { impl<'db> TrieIterator for FatDBIterator<'db> { fn seek(&mut self, key: &[u8]) -> super::Result<()> { - self.trie_iterator.seek(&key.sha3()) + self.trie_iterator.seek(&keccak(key)) } } @@ -94,7 +94,7 @@ impl<'db> Iterator for FatDBIterator<'db> { self.trie_iterator.next() .map(|res| res.map(|(hash, value)| { - let aux_hash = hash.sha3(); + let aux_hash = keccak(hash); (self.trie.db().get(&aux_hash).expect("Missing fatdb hash").into_vec(), value) }) ) diff --git a/util/src/trie/fatdbmut.rs b/util/src/trie/fatdbmut.rs index 0f5d6f47e..ebbcc92db 100644 --- a/util/src/trie/fatdbmut.rs +++ b/util/src/trie/fatdbmut.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use hash::H256; -use sha3::Hashable; +use keccak::keccak; use hashdb::{HashDB, DBValue}; use super::{TrieDBMut, TrieMut}; @@ -53,7 +53,7 @@ impl<'db> FatDBMut<'db> { } fn to_aux_key(key: &[u8]) -> H256 { - key.sha3() + keccak(key) } } @@ -67,17 +67,17 @@ impl<'db> TrieMut for FatDBMut<'db> { } fn contains(&self, key: &[u8]) -> super::Result { - self.raw.contains(&key.sha3()) + self.raw.contains(&keccak(key)) } fn get<'a, 'key>(&'a self, key: &'key [u8]) -> super::Result> where 'a: 'key { - self.raw.get(&key.sha3()) + self.raw.get(&keccak(key)) } fn insert(&mut self, key: &[u8], value: &[u8]) -> super::Result> { - let hash = key.sha3(); + let hash = keccak(key); let out = self.raw.insert(&hash, value)?; let db = self.raw.db_mut(); @@ -89,7 +89,7 @@ impl<'db> TrieMut for FatDBMut<'db> { } fn remove(&mut self, key: &[u8]) -> super::Result> { - let hash = key.sha3(); + let hash = keccak(key); let out = self.raw.remove(&hash)?; // don't remove if it already exists. @@ -114,5 +114,5 @@ fn fatdb_to_trie() { t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); } let t = TrieDB::new(&memdb, &root).unwrap(); - assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23])); + assert_eq!(t.get(&keccak(&[0x01u8, 0x23])).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23])); } diff --git a/util/src/trie/mod.rs b/util/src/trie/mod.rs index cae980505..e4e9cd3ef 100644 --- a/util/src/trie/mod.rs +++ b/util/src/trie/mod.rs @@ -18,6 +18,7 @@ use std::fmt; use hash::H256; +use keccak::KECCAK_NULL_RLP; use hashdb::{HashDB, DBValue}; /// Export the standardmap module. @@ -123,7 +124,7 @@ pub trait Trie { fn root(&self) -> &H256; /// Is the trie empty? - fn is_empty(&self) -> bool { *self.root() == ::sha3::SHA3_NULL_RLP } + fn is_empty(&self) -> bool { *self.root() == KECCAK_NULL_RLP } /// Does the trie contain a given key? fn contains(&self, key: &[u8]) -> Result { diff --git a/util/src/trie/recorder.rs b/util/src/trie/recorder.rs index 8d79b4315..1366ccddd 100644 --- a/util/src/trie/recorder.rs +++ b/util/src/trie/recorder.rs @@ -16,7 +16,7 @@ //! Trie query recorder. -use sha3::Hashable; +use keccak::keccak; use {Bytes, H256}; /// A record of a visited node. @@ -62,7 +62,7 @@ impl Recorder { /// Record a visited node, given its hash, data, and depth. pub fn record(&mut self, hash: &H256, data: &[u8], depth: u32) { - debug_assert_eq!(data.sha3(), *hash); + debug_assert_eq!(keccak(data), *hash); if depth >= self.min_depth { self.nodes.push(Record { @@ -82,7 +82,6 @@ impl Recorder { #[cfg(test)] mod tests { use super::*; - use sha3::Hashable; use ::H256; #[test] @@ -92,7 +91,7 @@ mod tests { let node1 = vec![1, 2, 3, 4]; let node2 = vec![4, 5, 6, 7, 8, 9, 10]; - let (hash1, hash2) = (node1.sha3(), node2.sha3()); + let (hash1, hash2) = (keccak(&node1), keccak(&node2)); basic.record(&hash1, &node1, 0); basic.record(&hash2, &node2, 456); @@ -118,8 +117,8 @@ mod tests { let node1 = vec![1, 2, 3, 4]; let node2 = vec![4, 5, 6, 7, 8, 9, 10]; - let hash1 = node1.sha3(); - let hash2 = node2.sha3(); + let hash1 = keccak(&node1); + let hash2 = keccak(&node2); basic.record(&hash1, &node1, 0); basic.record(&hash2, &node2, 456); diff --git a/util/src/trie/sectriedb.rs b/util/src/trie/sectriedb.rs index b2f344794..59dbe5b3a 100644 --- a/util/src/trie/sectriedb.rs +++ b/util/src/trie/sectriedb.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use hash::H256; -use sha3::Hashable; +use keccak::keccak; use hashdb::HashDB; use super::triedb::TrieDB; use super::{Trie, TrieItem, TrieIterator, Query}; @@ -56,13 +56,13 @@ impl<'db> Trie for SecTrieDB<'db> { fn root(&self) -> &H256 { self.raw.root() } fn contains(&self, key: &[u8]) -> super::Result { - self.raw.contains(&key.sha3()) + self.raw.contains(&keccak(key)) } fn get_with<'a, 'key, Q: Query>(&'a self, key: &'key [u8], query: Q) -> super::Result> where 'a: 'key { - self.raw.get_with(&key.sha3(), query) + self.raw.get_with(&keccak(key), query) } } @@ -77,7 +77,7 @@ fn trie_to_sectrie() { let mut root = H256::default(); { let mut t = TrieDBMut::new(&mut memdb, &mut root); - t.insert(&(&[0x01u8, 0x23]).sha3(), &[0x01u8, 0x23]).unwrap(); + t.insert(&keccak(&[0x01u8, 0x23]), &[0x01u8, 0x23]).unwrap(); } let t = SecTrieDB::new(&memdb, &root).unwrap(); assert_eq!(t.get(&[0x01u8, 0x23]).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23])); diff --git a/util/src/trie/sectriedbmut.rs b/util/src/trie/sectriedbmut.rs index 5cfdc4b9f..9e53aaff1 100644 --- a/util/src/trie/sectriedbmut.rs +++ b/util/src/trie/sectriedbmut.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use hash::H256; -use sha3::Hashable; +use keccak::keccak; use hashdb::{HashDB, DBValue}; use super::triedbmut::TrieDBMut; use super::TrieMut; @@ -59,21 +59,21 @@ impl<'db> TrieMut for SecTrieDBMut<'db> { } fn contains(&self, key: &[u8]) -> super::Result { - self.raw.contains(&key.sha3()) + self.raw.contains(&keccak(key)) } fn get<'a, 'key>(&'a self, key: &'key [u8]) -> super::Result> where 'a: 'key { - self.raw.get(&key.sha3()) + self.raw.get(&keccak(key)) } fn insert(&mut self, key: &[u8], value: &[u8]) -> super::Result> { - self.raw.insert(&key.sha3(), value) + self.raw.insert(&keccak(key), value) } fn remove(&mut self, key: &[u8]) -> super::Result> { - self.raw.remove(&key.sha3()) + self.raw.remove(&keccak(key)) } } @@ -90,5 +90,5 @@ fn sectrie_to_trie() { t.insert(&[0x01u8, 0x23], &[0x01u8, 0x23]).unwrap(); } let t = TrieDB::new(&memdb, &root).unwrap(); - assert_eq!(t.get(&(&[0x01u8, 0x23]).sha3()).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23])); + assert_eq!(t.get(&keccak(&[0x01u8, 0x23])).unwrap().unwrap(), DBValue::from_slice(&[0x01u8, 0x23])); } diff --git a/util/src/trie/standardmap.rs b/util/src/trie/standardmap.rs index f839f4f2e..0c7c1537e 100644 --- a/util/src/trie/standardmap.rs +++ b/util/src/trie/standardmap.rs @@ -17,8 +17,8 @@ //! Key-value datastore with a modified Merkle tree. extern crate rand; +use keccak::keccak; use bytes::*; -use sha3::*; use hash::*; use rlp::encode; @@ -63,14 +63,14 @@ impl StandardMap { /// `seed` is mutated pseudoramdonly and used. fn random_bytes(min_count: usize, journal_count: usize, seed: &mut H256) -> Vec { assert!(min_count + journal_count <= 32); - *seed = seed.sha3(); + *seed = keccak(&seed); let r = min_count + (seed[31] as usize % (journal_count + 1)); seed[0..r].to_vec() } /// Get a random value. Equal chance of being 1 byte as of 32. `seed` is mutated pseudoramdonly and used. fn random_value(seed: &mut H256) -> Bytes { - *seed = seed.sha3(); + *seed = keccak(&seed); match seed[0] % 2 { 1 => vec![seed[31];1], _ => seed.to_vec(), @@ -81,7 +81,7 @@ impl StandardMap { /// Each byte is an item from `alphabet`. `seed` is mutated pseudoramdonly and used. fn random_word(alphabet: &[u8], min_count: usize, journal_count: usize, seed: &mut H256) -> Vec { assert!(min_count + journal_count <= 32); - *seed = seed.sha3(); + *seed = keccak(&seed); let r = min_count + (seed[31] as usize % (journal_count + 1)); let mut ret: Vec = Vec::with_capacity(r); for i in 0..r { diff --git a/util/src/trie/triedbmut.rs b/util/src/trie/triedbmut.rs index 89eb574dc..fa23ca756 100644 --- a/util/src/trie/triedbmut.rs +++ b/util/src/trie/triedbmut.rs @@ -25,14 +25,13 @@ use ::{HashDB, H256}; use ::bytes::ToPretty; use ::nibbleslice::NibbleSlice; use ::rlp::{Rlp, RlpStream}; -use ::sha3::SHA3_NULL_RLP; use hashdb::DBValue; -use elastic_array::ElasticArray1024; - use std::collections::{HashSet, VecDeque}; use std::mem; use std::ops::Index; +use elastic_array::ElasticArray1024; +use keccak::{KECCAK_NULL_RLP}; // For lookups into the Node storage buffer. // This is deliberately non-copyable. @@ -262,7 +261,9 @@ impl<'a> Index<&'a StorageHandle> for NodeStorage { /// # Example /// ``` /// extern crate ethcore_util as util; +/// extern crate hash; /// +/// use hash::KECCAK_NULL_RLP; /// use util::trie::*; /// use util::hashdb::*; /// use util::memorydb::*; @@ -273,7 +274,7 @@ impl<'a> Index<&'a StorageHandle> for NodeStorage { /// let mut root = H256::new(); /// let mut t = TrieDBMut::new(&mut memdb, &mut root); /// assert!(t.is_empty()); -/// assert_eq!(*t.root(), ::util::sha3::SHA3_NULL_RLP); +/// assert_eq!(*t.root(), KECCAK_NULL_RLP); /// t.insert(b"foo", b"bar").unwrap(); /// assert!(t.contains(b"foo").unwrap()); /// assert_eq!(t.get(b"foo").unwrap().unwrap(), DBValue::from_slice(b"bar")); @@ -295,8 +296,8 @@ pub struct TrieDBMut<'a> { impl<'a> TrieDBMut<'a> { /// Create a new trie with backing database `db` and empty `root`. pub fn new(db: &'a mut HashDB, root: &'a mut H256) -> Self { - *root = SHA3_NULL_RLP; - let root_handle = NodeHandle::Hash(SHA3_NULL_RLP); + *root = KECCAK_NULL_RLP; + let root_handle = NodeHandle::Hash(KECCAK_NULL_RLP); TrieDBMut { storage: NodeStorage::empty(), @@ -871,7 +872,7 @@ impl<'a> TrieMut for TrieDBMut<'a> { fn is_empty(&self) -> bool { match self.root_handle { - NodeHandle::Hash(h) => h == SHA3_NULL_RLP, + NodeHandle::Hash(h) => h == KECCAK_NULL_RLP, NodeHandle::InMemory(ref h) => match self.storage[h] { Node::Empty => true, _ => false, @@ -919,8 +920,8 @@ impl<'a> TrieMut for TrieDBMut<'a> { } None => { trace!(target: "trie", "remove: obliterated trie"); - self.root_handle = NodeHandle::Hash(SHA3_NULL_RLP); - *self.root = SHA3_NULL_RLP; + self.root_handle = NodeHandle::Hash(KECCAK_NULL_RLP); + *self.root = KECCAK_NULL_RLP; } } @@ -941,7 +942,7 @@ mod tests { use memorydb::*; use super::*; use bytes::ToPretty; - use sha3::SHA3_NULL_RLP; + use keccak::KECCAK_NULL_RLP; use super::super::TrieMut; use super::super::standardmap::*; @@ -996,7 +997,7 @@ mod tests { assert_eq!(*memtrie.root(), real); unpopulate_trie(&mut memtrie, &x); memtrie.commit(); - if *memtrie.root() != SHA3_NULL_RLP { + if *memtrie.root() != KECCAK_NULL_RLP { println!("- TRIE MISMATCH"); println!(""); println!("{:?} vs {:?}", memtrie.root(), real); @@ -1004,7 +1005,7 @@ mod tests { println!("{:?} -> {:?}", i.0.pretty(), i.1.pretty()); } } - assert_eq!(*memtrie.root(), SHA3_NULL_RLP); + assert_eq!(*memtrie.root(), KECCAK_NULL_RLP); } } @@ -1013,7 +1014,7 @@ mod tests { let mut memdb = MemoryDB::new(); let mut root = H256::new(); let mut t = TrieDBMut::new(&mut memdb, &mut root); - assert_eq!(*t.root(), SHA3_NULL_RLP); + assert_eq!(*t.root(), KECCAK_NULL_RLP); } #[test] @@ -1268,7 +1269,7 @@ mod tests { } assert!(t.is_empty()); - assert_eq!(*t.root(), SHA3_NULL_RLP); + assert_eq!(*t.root(), KECCAK_NULL_RLP); } #[test] diff --git a/util/src/triehash.rs b/util/src/triehash.rs index 62d3dbf97..fa790f3f4 100644 --- a/util/src/triehash.rs +++ b/util/src/triehash.rs @@ -21,7 +21,7 @@ use std::collections::BTreeMap; use std::cmp; use hash::*; -use sha3::*; +use keccak::keccak; use rlp; use rlp::RlpStream; use vector::SharedPrefix; @@ -115,7 +115,7 @@ pub fn sec_trie_root(input: Vec<(Vec, Vec)>) -> H256 { let gen_input = input // first put elements into btree to sort them and to remove duplicates .into_iter() - .map(|(k, v)| (k.sha3(), v)) + .map(|(k, v)| (keccak(k), v)) .collect::>() // then move them to a vector .into_iter() @@ -128,7 +128,7 @@ pub fn sec_trie_root(input: Vec<(Vec, Vec)>) -> H256 { fn gen_trie_root(input: Vec<(Vec, Vec)>) -> H256 { let mut stream = RlpStream::new(); hash256rlp(&input, 0, &mut stream); - stream.out().sha3() + keccak(stream.out()) } /// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1. @@ -271,7 +271,7 @@ fn hash256aux(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStre let out = s.out(); match out.len() { 0...31 => stream.append_raw(&out, 1), - _ => stream.append(&out.sha3()) + _ => stream.append(&keccak(out)) }; } From e120c75d174e665c3aca1d791732926b0183bf0b Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 30 Aug 2017 17:14:52 +0200 Subject: [PATCH 02/16] Hashable::sha3 -> fn keccak for ethcore-network --- Cargo.lock | 1 + util/network/Cargo.toml | 1 + util/network/src/connection.rs | 10 +++++----- util/network/src/discovery.rs | 22 +++++++++++----------- util/network/src/handshake.rs | 4 ++-- util/network/src/host.rs | 8 ++++---- util/network/src/lib.rs | 3 ++- util/src/lib.rs | 1 - util/src/trie/triedb.rs | 2 +- 9 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c28246791..bd3469589 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -693,6 +693,7 @@ dependencies = [ "ethcore-util 1.8.0", "ethcrypto 0.1.0", "ethkey 0.2.0", + "hash 0.1.0", "igd 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipnetwork 0.12.6 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/util/network/Cargo.toml b/util/network/Cargo.toml index 2d571d632..86f7867f4 100644 --- a/util/network/Cargo.toml +++ b/util/network/Cargo.toml @@ -31,6 +31,7 @@ rlp = { path = "../rlp" } path = { path = "../path" } ethcore-logger = { path ="../../logger" } ipnetwork = "0.12.6" +hash = { path = "../hash" } [features] default = [] diff --git a/util/network/src/connection.rs b/util/network/src/connection.rs index a9cb05c00..b51813100 100644 --- a/util/network/src/connection.rs +++ b/util/network/src/connection.rs @@ -18,11 +18,11 @@ use std::sync::Arc; use std::collections::VecDeque; use std::net::SocketAddr; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; +use hash::{keccak, keccak_into}; use mio::{Token, Ready, PollOpt}; use mio::deprecated::{Handler, EventLoop, TryRead, TryWrite}; use mio::tcp::*; use util::hash::*; -use util::sha3::*; use util::bytes::*; use rlp::*; use std::io::{self, Cursor, Read, Write}; @@ -312,16 +312,16 @@ impl EncryptedConnection { } let mut key_material = H512::new(); shared.copy_to(&mut key_material[0..32]); - nonce_material.sha3_into(&mut key_material[32..64]); - key_material.sha3().copy_to(&mut key_material[32..64]); - key_material.sha3().copy_to(&mut key_material[32..64]); + keccak_into(&nonce_material, &mut key_material[32..64]); + keccak(&key_material).copy_to(&mut key_material[32..64]); + keccak(&key_material).copy_to(&mut key_material[32..64]); let iv = vec![0u8; 16]; let encoder = CtrMode::new(AesSafe256Encryptor::new(&key_material[32..64]), iv); let iv = vec![0u8; 16]; let decoder = CtrMode::new(AesSafe256Encryptor::new(&key_material[32..64]), iv); - key_material.sha3().copy_to(&mut key_material[32..64]); + keccak(&key_material).copy_to(&mut key_material[32..64]); let mac_encoder = EcbEncryptor::new(AesSafe256Encryptor::new(&key_material[32..64]), NoPadding); let mut egress_mac = Keccak::new_keccak256(); diff --git a/util/network/src/discovery.rs b/util/network/src/discovery.rs index 3dcd8548d..f4025b26f 100644 --- a/util/network/src/discovery.rs +++ b/util/network/src/discovery.rs @@ -22,7 +22,7 @@ use std::default::Default; use mio::*; use mio::deprecated::{Handler, EventLoop}; use mio::udp::*; -use util::sha3::*; +use hash::keccak; use time; use util::hash::*; use rlp::*; @@ -112,7 +112,7 @@ impl Discovery { let socket = UdpSocket::bind(&listen).expect("Error binding UDP socket"); Discovery { id: key.public().clone(), - id_hash: key.public().sha3(), + id_hash: keccak(key.public()), secret: key.secret().clone(), public_endpoint: public, token: token, @@ -154,7 +154,7 @@ impl Discovery { fn update_node(&mut self, e: NodeEntry) { trace!(target: "discovery", "Inserting {:?}", &e); - let id_hash = e.id.sha3(); + let id_hash = keccak(e.id); let ping = { let mut bucket = &mut self.node_buckets[Discovery::distance(&self.id_hash, &id_hash) as usize]; let updated = if let Some(node) = bucket.nodes.iter_mut().find(|n| n.address.id == e.id) { @@ -180,7 +180,7 @@ impl Discovery { } fn clear_ping(&mut self, id: &NodeId) { - let mut bucket = &mut self.node_buckets[Discovery::distance(&self.id_hash, &id.sha3()) as usize]; + let mut bucket = &mut self.node_buckets[Discovery::distance(&self.id_hash, &keccak(id)) as usize]; if let Some(node) = bucket.nodes.iter_mut().find(|n| &n.address.id == id) { node.timeout = None; } @@ -264,7 +264,7 @@ impl Discovery { rlp.append(×tamp); let bytes = rlp.drain(); - let hash = bytes.as_ref().sha3(); + let hash = keccak(bytes.as_ref()); let signature = match sign(&self.secret, &hash) { Ok(s) => s, Err(_) => { @@ -276,7 +276,7 @@ impl Discovery { packet.extend(hash.iter()); packet.extend(signature.iter()); packet.extend(bytes.iter()); - let signed_hash = (&packet[32..]).sha3(); + let signed_hash = keccak(&packet[32..]); packet[0..32].clone_from_slice(&signed_hash); self.send_to(packet, address.clone()); } @@ -285,7 +285,7 @@ impl Discovery { fn nearest_node_entries(target: &NodeId, buckets: &[NodeBucket]) -> Vec { let mut found: BTreeMap> = BTreeMap::new(); let mut count = 0; - let target_hash = target.sha3(); + let target_hash = keccak(target); // Sort nodes by distance to target for bucket in buckets { @@ -368,14 +368,14 @@ impl Discovery { return Err(NetworkError::BadProtocol); } - let hash_signed = (&packet[32..]).sha3(); + let hash_signed = keccak(&packet[32..]); if hash_signed[..] != packet[0..32] { return Err(NetworkError::BadProtocol); } let signed = &packet[(32 + 65)..]; let signature = H520::from_slice(&packet[32..(32 + 65)]); - let node_id = recover(&signature.into(), &signed.sha3())?; + let node_id = recover(&signature.into(), &keccak(signed))?; let packet_id = signed[0]; let rlp = UntrustedRlp::new(&signed[1..]); @@ -419,7 +419,7 @@ impl Discovery { self.update_node(entry.clone()); added_map.insert(node.clone(), entry); } - let hash = rlp.as_raw().sha3(); + let hash = keccak(rlp.as_raw()); let mut response = RlpStream::new_list(2); dest.to_rlp_list(&mut response); response.append(&hash); @@ -636,7 +636,7 @@ mod tests { buckets[0].nodes.push_back(BucketEntry { address: NodeEntry { id: NodeId::new(), endpoint: ep.clone() }, timeout: None, - id_hash: NodeId::new().sha3(), + id_hash: keccak(NodeId::new()), }); } let nearest = Discovery::nearest_node_entries(&NodeId::new(), &buckets); diff --git a/util/network/src/handshake.rs b/util/network/src/handshake.rs index f5c4e118a..ca38d4360 100644 --- a/util/network/src/handshake.rs +++ b/util/network/src/handshake.rs @@ -16,9 +16,9 @@ use std::sync::Arc; use rand::random; +use hash::keccak_into; use mio::tcp::*; use util::hash::*; -use util::sha3::Hashable; use util::bytes::Bytes; use rlp::*; use connection::{Connection}; @@ -273,7 +273,7 @@ impl Handshake { // E(remote-pubk, S(ecdhe-random, ecdh-shared-secret^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x0) let shared = *ecdh::agree(secret, &self.id)?; sig.copy_from_slice(&*sign(self.ecdhe.secret(), &(&shared ^ &self.nonce))?); - self.ecdhe.public().sha3_into(hepubk); + keccak_into(self.ecdhe.public(), hepubk); pubk.copy_from_slice(public); nonce.copy_from_slice(&self.nonce); } diff --git a/util/network/src/host.rs b/util/network/src/host.rs index d74b2fa6e..caefc6738 100644 --- a/util/network/src/host.rs +++ b/util/network/src/host.rs @@ -25,11 +25,11 @@ use std::path::{Path, PathBuf}; use std::io::{Read, Write, ErrorKind}; use std::fs; use ethkey::{KeyPair, Secret, Random, Generator}; +use hash::keccak; use mio::*; use mio::deprecated::{EventLoop}; use mio::tcp::*; use util::hash::*; -use util::Hashable; use util::version; use rlp::*; use session::{Session, SessionInfo, SessionData}; @@ -354,8 +354,8 @@ impl HostInfo { /// Increments and returns connection nonce. pub fn next_nonce(&mut self) -> H256 { - self.nonce = self.nonce.sha3(); - self.nonce.clone() + self.nonce = keccak(&self.nonce); + self.nonce } } @@ -694,7 +694,7 @@ impl Host { let max_handshakes_per_round = max_handshakes / 2; let mut started: usize = 0; - for id in nodes.filter(|id| + for id in nodes.filter(|id| !self.have_session(id) && !self.connecting_to(id) && *id != self_id && diff --git a/util/network/src/lib.rs b/util/network/src/lib.rs index 5695b8196..accd6b0a2 100644 --- a/util/network/src/lib.rs +++ b/util/network/src/lib.rs @@ -78,6 +78,7 @@ extern crate bytes; extern crate path; extern crate ethcore_logger; extern crate ipnetwork; +extern crate hash; #[macro_use] extern crate log; @@ -156,7 +157,7 @@ pub struct IpFilter { pub predefined: AllowIP, pub custom_allow: Vec, pub custom_block: Vec, -} +} impl Default for IpFilter { fn default() -> Self { diff --git a/util/src/lib.rs b/util/src/lib.rs index def0b17c8..911df1cc6 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -118,7 +118,6 @@ pub mod error; pub mod bytes; pub mod misc; pub mod vector; -//pub mod sha3; pub mod hashdb; pub mod memorydb; pub mod migration; diff --git a/util/src/trie/triedb.rs b/util/src/trie/triedb.rs index 3e62550ba..0398aed51 100644 --- a/util/src/trie/triedb.rs +++ b/util/src/trie/triedb.rs @@ -130,7 +130,7 @@ impl<'db> TrieDB<'db> { /// This could be a simple identity operation in the case that the node is sufficiently small, but /// may require a database lookup. fn get_raw_or_lookup(&'db self, node: &'db [u8]) -> super::Result { - // check if its sha3 + len + // check if its keccak + len let r = Rlp::new(node); match r.is_data() && r.size() == 32 { true => { From e04d58f647c3b2bf0bbf7767fb0f79f78c899461 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Thu, 31 Aug 2017 00:38:05 +0800 Subject: [PATCH 03/16] use one hasher in Bloom (#6404) * remove the redundant hasher in Bloom * add the test to check the hash backward compatibility --- util/bloom/src/lib.rs | 49 ++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/util/bloom/src/lib.rs b/util/bloom/src/lib.rs index 91897ed44..4fdb61d40 100644 --- a/util/bloom/src/lib.rs +++ b/util/bloom/src/lib.rs @@ -24,9 +24,6 @@ use std::hash::{Hash, Hasher}; use std::collections::HashSet; use siphasher::sip::SipHasher; -// TODO [ToDr] Both hashers are exactly the same - no point to keep two. -const NUMBER_OF_HASHERS: usize = 2; - /// BitVec structure with journalling /// Every time any of the blocks is getting set it's index is tracked /// and can be then drained by `drain` method @@ -80,8 +77,6 @@ pub struct Bloom { bitmap: BitVecJournal, bitmap_bits: u64, k_num: u32, - // TODO [ToDr] Both hashers are exactly the same - no point to keep two. - sips: [SipHasher; NUMBER_OF_HASHERS], } impl Bloom { @@ -93,12 +88,10 @@ impl Bloom { let bitmap_bits = (bitmap_size as u64) * 8u64; let k_num = Bloom::optimal_k_num(bitmap_bits, items_count); let bitmap = BitVecJournal::new(bitmap_bits as usize); - let sips = [SipHasher::new(), SipHasher::new()]; Bloom { bitmap: bitmap, bitmap_bits: bitmap_bits, k_num: k_num, - sips: sips, } } @@ -107,12 +100,10 @@ impl Bloom { let bitmap_size = parts.len() * 8; let bitmap_bits = (bitmap_size as u64) * 8u64; let bitmap = BitVecJournal::from_parts(parts); - let sips = [SipHasher::new(), SipHasher::new()]; Bloom { bitmap: bitmap, bitmap_bits: bitmap_bits, k_num: k_num, - sips: sips, } } @@ -139,9 +130,9 @@ impl Bloom { pub fn set(&mut self, item: T) where T: Hash { - let mut hashes = [0u64, 0u64]; + let base_hash = Bloom::sip_hash(&item); for k_i in 0..self.k_num { - let bit_offset = (self.bloom_hash(&mut hashes, &item, k_i) % self.bitmap_bits) as usize; + let bit_offset = (Bloom::bloom_hash(base_hash, k_i) % self.bitmap_bits) as usize; self.bitmap.set(bit_offset); } } @@ -151,9 +142,9 @@ impl Bloom { pub fn check(&self, item: T) -> bool where T: Hash { - let mut hashes = [0u64, 0u64]; + let base_hash = Bloom::sip_hash(&item); for k_i in 0..self.k_num { - let bit_offset = (self.bloom_hash(&mut hashes, &item, k_i) % self.bitmap_bits) as usize; + let bit_offset = (Bloom::bloom_hash(base_hash, k_i) % self.bitmap_bits) as usize; if !self.bitmap.get(bit_offset) { return false; } @@ -178,17 +169,20 @@ impl Bloom { cmp::max(k_num, 1) } - fn bloom_hash(&self, hashes: &mut [u64; NUMBER_OF_HASHERS], item: &T, k_i: u32) -> u64 + fn sip_hash(item: &T) -> u64 where T: Hash { - if k_i < NUMBER_OF_HASHERS as u32 { - let mut sip = self.sips[k_i as usize].clone(); - item.hash(&mut sip); - let hash = sip.finish(); - hashes[k_i as usize] = hash; - hash + let mut sip = SipHasher::new(); + item.hash(&mut sip); + let hash = sip.finish(); + hash + } + + fn bloom_hash(base_hash: u64, k_i: u32) -> u64 { + if k_i < 2 { + base_hash } else { - hashes[0].wrapping_add((k_i as u64).wrapping_mul(hashes[1]) % 0xffffffffffffffc5) + base_hash.wrapping_add((k_i as u64).wrapping_mul(base_hash) % 0xffffffffffffffc5) } } @@ -218,6 +212,7 @@ pub struct BloomJournal { #[cfg(test)] mod tests { use super::Bloom; + use std::collections::HashSet; #[test] fn get_set() { @@ -248,4 +243,16 @@ mod tests { // 2/8/64 = 0.00390625 assert!(full >= 0.0039f64 && full <= 0.004f64); } + + #[test] + fn hash_backward_compatibility() { + let ss = vec!["you", "should", "not", "break", "hash", "backward", "compatibility"]; + let mut bloom = Bloom::new(16, 8); + for s in ss.iter() { + bloom.set(&s); + } + let drained_elems: HashSet = bloom.drain_journal().entries.into_iter().map(|t| t.1).collect(); + let expected: HashSet = [2094615114573771027u64, 244675582389208413u64].iter().cloned().collect(); + assert_eq!(drained_elems, expected); + } } From f0e8abb07bca9c6a9f646f71f5cefa8243dd1493 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 30 Aug 2017 19:18:28 +0200 Subject: [PATCH 04/16] Hashable::sha3 -> fn keccak for ethcore --- Cargo.lock | 5 ++ ethash/src/compute.rs | 42 +++++++------- ethash/src/lib.rs | 2 +- ethcore/Cargo.toml | 1 + ethcore/evm/Cargo.toml | 1 + ethcore/evm/src/interpreter/mod.rs | 9 +-- ethcore/evm/src/interpreter/shared_cache.rs | 4 +- ethcore/evm/src/lib.rs | 1 + ethcore/src/account_db.rs | 35 ++++++------ ethcore/src/block.rs | 15 ++--- ethcore/src/blockchain/blockchain.rs | 56 +++++++++---------- ethcore/src/blockchain/generator/complete.rs | 3 +- ethcore/src/blockchain/generator/generator.rs | 9 ++- ethcore/src/builtin.rs | 13 +---- ethcore/src/client/client.rs | 15 ++--- ethcore/src/client/test_client.rs | 3 +- ethcore/src/encoded.rs | 13 ++--- ethcore/src/engines/authority_round/mod.rs | 11 ++-- ethcore/src/engines/basic_authority.rs | 5 +- ethcore/src/engines/tendermint/message.rs | 12 ++-- ethcore/src/engines/tendermint/mod.rs | 25 +++++---- ethcore/src/engines/validator_set/contract.rs | 3 +- ethcore/src/engines/validator_set/multi.rs | 5 +- .../engines/validator_set/safe_contract.rs | 8 ++- ethcore/src/engines/vote_collector.rs | 43 +++++++------- ethcore/src/ethereum/ethash.rs | 3 +- ethcore/src/ethereum/mod.rs | 4 +- ethcore/src/executive.rs | 15 ++--- ethcore/src/header.rs | 21 +++---- ethcore/src/lib.rs | 1 + ethcore/src/migrations/state/v7.rs | 6 +- ethcore/src/miner/banning_queue.rs | 12 ++-- ethcore/src/miner/miner.rs | 5 +- ethcore/src/pod_account.rs | 5 +- ethcore/src/snapshot/account.rs | 43 +++++++------- ethcore/src/snapshot/block.rs | 5 +- ethcore/src/snapshot/io.rs | 22 ++++---- ethcore/src/snapshot/mod.rs | 14 ++--- ethcore/src/snapshot/tests/helpers.rs | 6 +- .../src/snapshot/tests/proof_of_authority.rs | 8 +-- ethcore/src/snapshot/tests/proof_of_work.rs | 4 +- ethcore/src/snapshot/tests/state.rs | 9 ++- ethcore/src/spec/genesis.rs | 6 +- ethcore/src/spec/spec.rs | 25 +++++---- ethcore/src/state/account.rs | 39 ++++++------- ethcore/src/state/mod.rs | 21 +++---- ethcore/src/state_db.rs | 7 ++- ethcore/src/tests/client.rs | 5 +- ethcore/src/tests/evm.rs | 5 +- ethcore/src/tests/helpers.rs | 3 +- ethcore/src/trace/types/filter.rs | 54 +++++++++--------- ethcore/src/trace/types/trace.rs | 14 ++--- ethcore/src/transaction.rs | 17 +++--- ethcore/src/verification/verification.rs | 8 ++- ethcore/src/views/block.rs | 17 ++---- ethcore/src/views/body.rs | 5 +- ethcore/src/views/header.rs | 13 ++--- ethcore/src/views/transaction.rs | 14 ++--- ethcore/types/Cargo.toml | 1 + ethcore/types/src/filter.rs | 7 ++- ethcore/types/src/lib.rs | 1 + ethcore/types/src/log_entry.rs | 5 +- ethcore/vm/Cargo.toml | 3 +- ethcore/vm/src/action_params.rs | 6 +- ethcore/vm/src/env_info.rs | 5 +- ethcore/vm/src/lib.rs | 3 +- stratum/Cargo.toml | 1 + stratum/src/lib.rs | 6 +- util/benches/trie.rs | 14 ++--- 69 files changed, 429 insertions(+), 398 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd3469589..439ba8936 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,6 +301,7 @@ dependencies = [ "bloomable 0.1.0", "ethcore-util 1.8.0", "ethjson 0.1.0", + "hash 0.1.0", "rlp 0.2.0", "rlp_derive 0.1.0", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -516,6 +517,7 @@ dependencies = [ "evm 0.1.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "hardware-wallet 1.8.0", + "hash 0.1.0", "hyper 0.10.0-a.0 (git+https://github.com/paritytech/hyper)", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -756,6 +758,7 @@ dependencies = [ "ethcore-logger 1.8.0", "ethcore-util 1.8.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-macros 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-tcp-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", @@ -921,6 +924,7 @@ dependencies = [ "ethcore-util 1.8.0", "ethjson 0.1.0", "evmjit 1.8.0", + "hash 0.1.0", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "parity-wasm 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3185,6 +3189,7 @@ dependencies = [ "ethcore-util 1.8.0", "ethjson 0.1.0", "evmjit 1.8.0", + "hash 0.1.0", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.0", diff --git a/ethash/src/compute.rs b/ethash/src/compute.rs index ab2c758df..ae680b686 100644 --- a/ethash/src/compute.rs +++ b/ethash/src/compute.rs @@ -23,7 +23,7 @@ use primal::is_prime; use std::cell::Cell; use std::mem; use std::ptr; -use sha3; +use hash; use std::slice; use std::path::{Path, PathBuf}; use std::io::{self, Read, Write}; @@ -200,7 +200,7 @@ impl SeedHashCompute { #[inline] pub fn resume_compute_seedhash(mut hash: H256, start_epoch: u64, end_epoch: u64) -> H256 { for _ in start_epoch..end_epoch { - unsafe { sha3::sha3_256(hash[..].as_mut_ptr(), 32, hash[..].as_ptr(), 32) }; + unsafe { hash::keccak_256(hash[..].as_mut_ptr(), 32, hash[..].as_ptr(), 32) }; } hash } @@ -214,14 +214,14 @@ fn fnv_hash(x: u32, y: u32) -> u32 { return x.wrapping_mul(FNV_PRIME) ^ y; } -fn sha3_512(input: &[u8], output: &mut [u8]) { - unsafe { sha3::sha3_512(output.as_mut_ptr(), output.len(), input.as_ptr(), input.len()) }; +fn keccak_512(input: &[u8], output: &mut [u8]) { + unsafe { hash::keccak_512(output.as_mut_ptr(), output.len(), input.as_ptr(), input.len()) }; } -fn sha3_512_inplace(input: &mut [u8]) { +fn keccak_512_inplace(input: &mut [u8]) { // This is safe since `sha3_*` uses an internal buffer and copies the result to the output. This // means that we can reuse the input buffer for both input and output. - unsafe { sha3::sha3_512(input.as_mut_ptr(), input.len(), input.as_ptr(), input.len()) }; + unsafe { hash::keccak_512(input.as_mut_ptr(), input.len(), input.as_ptr(), input.len()) }; } fn get_cache_size(block_number: u64) -> usize { @@ -250,23 +250,23 @@ fn get_data_size(block_number: u64) -> usize { /// Boundary recovered from mix hash pub fn quick_get_difficulty(header_hash: &H256, nonce: u64, mix_hash: &H256) -> H256 { unsafe { - // This is safe - the `sha3_512` call below reads the first 40 bytes (which we explicitly set + // This is safe - the `keccak_512` call below reads the first 40 bytes (which we explicitly set // with two `copy_nonoverlapping` calls) but writes the first 64, and then we explicitly write - // the next 32 bytes before we read the whole thing with `sha3_256`. + // the next 32 bytes before we read the whole thing with `keccak_256`. // // This cannot be elided by the compiler as it doesn't know the implementation of - // `sha3_512`. + // `keccak_512`. let mut buf: [u8; 64 + 32] = mem::uninitialized(); ptr::copy_nonoverlapping(header_hash.as_ptr(), buf.as_mut_ptr(), 32); ptr::copy_nonoverlapping(mem::transmute(&nonce), buf[32..].as_mut_ptr(), 8); - sha3::sha3_512(buf.as_mut_ptr(), 64, buf.as_ptr(), 40); + hash::keccak_512(buf.as_mut_ptr(), 64, buf.as_ptr(), 40); ptr::copy_nonoverlapping(mix_hash.as_ptr(), buf[64..].as_mut_ptr(), 32); - // This is initialized in `sha3_256` + // This is initialized in `keccak_256` let mut hash: [u8; 32] = mem::uninitialized(); - sha3::sha3_256(hash.as_mut_ptr(), hash.len(), buf.as_ptr(), buf.len()); + hash::keccak_256(hash.as_mut_ptr(), hash.len(), buf.as_ptr(), buf.len()); hash } @@ -320,7 +320,7 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) half_mix: unsafe { // Pack `header_hash` and `nonce` together // We explicitly write the first 40 bytes, leaving the last 24 as uninitialized. Then - // `sha3_512` reads the first 40 bytes (4th parameter) and overwrites the entire array, + // `keccak_512` reads the first 40 bytes (4th parameter) and overwrites the entire array, // leaving it fully initialized. let mut out: [u8; NODE_BYTES] = mem::uninitialized(); @@ -336,7 +336,7 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) ); // compute sha3-512 hash and replicate across mix - sha3::sha3_512( + hash::keccak_512( out.as_mut_ptr(), NODE_BYTES, out.as_ptr(), @@ -427,10 +427,10 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) let value: H256 = unsafe { // We can interpret the buffer as an array of `u8`s, since it's `repr(C)`. let read_ptr: *const u8 = mem::transmute(&buf); - // We overwrite the second half since `sha3_256` has an internal buffer and so allows + // We overwrite the second half since `keccak_256` has an internal buffer and so allows // overlapping arrays as input. let write_ptr: *mut u8 = mem::transmute(&mut buf.compress_bytes); - sha3::sha3_256( + hash::keccak_256( write_ptr, buf.compress_bytes.len(), read_ptr, @@ -450,7 +450,7 @@ fn calculate_dag_item(node_index: u32, cache: &[Node]) -> Node { let mut ret = cache[node_index as usize % num_parent_nodes].clone(); ret.as_words_mut()[0] ^= node_index; - sha3_512_inplace(&mut ret.bytes); + keccak_512_inplace(&mut ret.bytes); debug_assert_eq!(NODE_WORDS, 16); for i in 0..ETHASH_DATASET_PARENTS as u32 { @@ -467,7 +467,7 @@ fn calculate_dag_item(node_index: u32, cache: &[Node]) -> Node { } } - sha3_512_inplace(&mut ret.bytes); + keccak_512_inplace(&mut ret.bytes); ret } @@ -485,9 +485,9 @@ fn light_new>(cache_dir: T, block_number: u64) -> Light { // Use uninit instead of unnecessarily writing `size_of::() * num_nodes` 0s nodes.set_len(num_nodes); - sha3_512(&seedhash[0..32], &mut nodes.get_unchecked_mut(0).bytes); + keccak_512(&seedhash[0..32], &mut nodes.get_unchecked_mut(0).bytes); for i in 1..num_nodes { - sha3::sha3_512(nodes.get_unchecked_mut(i).bytes.as_mut_ptr(), NODE_BYTES, nodes.get_unchecked(i - 1).bytes.as_ptr(), NODE_BYTES); + hash::keccak_512(nodes.get_unchecked_mut(i).bytes.as_mut_ptr(), NODE_BYTES, nodes.get_unchecked(i - 1).bytes.as_ptr(), NODE_BYTES); } debug_assert_eq!(NODE_WORDS, 16); @@ -504,7 +504,7 @@ fn light_new>(cache_dir: T, block_number: u64) -> Light { } } - sha3_512(&data.bytes, &mut nodes.get_unchecked_mut(i).bytes); + keccak_512(&data.bytes, &mut nodes.get_unchecked_mut(i).bytes); } } } diff --git a/ethash/src/lib.rs b/ethash/src/lib.rs index 9112546c4..f88e8a3f5 100644 --- a/ethash/src/lib.rs +++ b/ethash/src/lib.rs @@ -20,7 +20,7 @@ #![cfg_attr(feature = "benches", feature(test))] extern crate primal; -extern crate sha3; +extern crate hash; extern crate parking_lot; #[macro_use] diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index c49e31898..4af0f315d 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -59,6 +59,7 @@ table = { path = "../util/table" } bloomable = { path = "../util/bloomable" } vm = { path = "vm" } wasm = { path = "wasm" } +hash = { path = "../util/hash" } [dev-dependencies] native-contracts = { path = "native_contracts", features = ["test_contracts"] } diff --git a/ethcore/evm/Cargo.toml b/ethcore/evm/Cargo.toml index 2780703da..4dc239e19 100644 --- a/ethcore/evm/Cargo.toml +++ b/ethcore/evm/Cargo.toml @@ -17,6 +17,7 @@ vm = { path = "../vm" } parity-wasm = "0.12" ethcore-logger = { path = "../../logger" } wasm-utils = { git = "https://github.com/paritytech/wasm-utils" } +hash = { path = "../../util/hash" } [dev-dependencies] rustc-hex = "1.0" diff --git a/ethcore/evm/src/interpreter/mod.rs b/ethcore/evm/src/interpreter/mod.rs index cb8fe3a52..edf8bc3f5 100644 --- a/ethcore/evm/src/interpreter/mod.rs +++ b/ethcore/evm/src/interpreter/mod.rs @@ -26,6 +26,7 @@ mod shared_cache; use std::marker::PhantomData; use std::{cmp, mem}; use std::sync::Arc; +use hash::keccak; use vm::{ self, ActionParams, ActionValue, CallType, MessageCallResult, @@ -180,7 +181,7 @@ impl vm::Vm for Interpreter { match result { InstructionResult::JumpToPosition(position) => { if valid_jump_destinations.is_none() { - let code_hash = params.code_hash.clone().unwrap_or_else(|| code.sha3()); + let code_hash = params.code_hash.clone().unwrap_or_else(|| keccak(code.as_ref())); valid_jump_destinations = Some(self.cache.jump_destinations(&code_hash, code)); } let jump_destinations = valid_jump_destinations.as_ref().expect("jump_destinations are initialized on first jump; qed"); @@ -464,8 +465,8 @@ impl Interpreter { instructions::SHA3 => { let offset = stack.pop_back(); let size = stack.pop_back(); - let sha3 = self.mem.read_slice(offset, size).sha3(); - stack.push(U256::from(&*sha3)); + let k = keccak(self.mem.read_slice(offset, size)); + stack.push(U256::from(&*k)); }, instructions::SLOAD => { let key = H256::from(&stack.pop_back()); @@ -880,7 +881,7 @@ mod tests { use rustc_hex::FromHex; use vmtype::VMType; use factory::Factory; - use vm::{self, ActionParams, ActionValue}; + use vm::{ActionParams, ActionValue}; use vm::tests::{FakeExt, test_finalize}; #[test] diff --git a/ethcore/evm/src/interpreter/shared_cache.rs b/ethcore/evm/src/interpreter/shared_cache.rs index a93afa79a..6f6620a3b 100644 --- a/ethcore/evm/src/interpreter/shared_cache.rs +++ b/ethcore/evm/src/interpreter/shared_cache.rs @@ -15,8 +15,8 @@ // along with Parity. If not, see . use std::sync::Arc; +use hash::KECCAK_EMPTY; use util::{H256, HeapSizeOf, Mutex}; -use util::sha3::*; use util::cache::MemoryLruCache; use bit_set::BitSet; use super::super::instructions; @@ -49,7 +49,7 @@ impl SharedCache { /// Get jump destinations bitmap for a contract. pub fn jump_destinations(&self, code_hash: &H256, code: &[u8]) -> Arc { - if code_hash == &SHA3_EMPTY { + if code_hash == &KECCAK_EMPTY { return Self::find_jump_destinations(code); } diff --git a/ethcore/evm/src/lib.rs b/ethcore/evm/src/lib.rs index 833b26664..5cde3a131 100644 --- a/ethcore/evm/src/lib.rs +++ b/ethcore/evm/src/lib.rs @@ -26,6 +26,7 @@ extern crate parity_wasm; extern crate wasm_utils; extern crate ethcore_logger; extern crate vm; +extern crate hash; #[macro_use] extern crate lazy_static; diff --git a/ethcore/src/account_db.rs b/ethcore/src/account_db.rs index 5ce555aef..48bb448dd 100644 --- a/ethcore/src/account_db.rs +++ b/ethcore/src/account_db.rs @@ -16,6 +16,7 @@ //! DB backend wrapper for Account trie use std::collections::HashMap; +use hash::{KECCAK_NULL_RLP, keccak}; use util::*; use rlp::NULL_RLP; @@ -79,7 +80,7 @@ impl<'db> AccountDB<'db> { /// Create a new AccountDB from an address. #[cfg(test)] pub fn new(db: &'db HashDB, address: &Address) -> Self { - Self::from_hash(db, address.sha3()) + Self::from_hash(db, keccak(address)) } /// Create a new AcountDB from an address' hash. @@ -97,14 +98,14 @@ impl<'db> HashDB for AccountDB<'db>{ } fn get(&self, key: &H256) -> Option { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return Some(DBValue::from_slice(&NULL_RLP)); } self.db.get(&combine_key(&self.address_hash, key)) } fn contains(&self, key: &H256) -> bool { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return true; } self.db.contains(&combine_key(&self.address_hash, key)) @@ -133,7 +134,7 @@ impl<'db> AccountDBMut<'db> { /// Create a new AccountDB from an address. #[cfg(test)] pub fn new(db: &'db mut HashDB, address: &Address) -> Self { - Self::from_hash(db, address.sha3()) + Self::from_hash(db, keccak(address)) } /// Create a new AcountDB from an address' hash. @@ -156,14 +157,14 @@ impl<'db> HashDB for AccountDBMut<'db>{ } fn get(&self, key: &H256) -> Option { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return Some(DBValue::from_slice(&NULL_RLP)); } self.db.get(&combine_key(&self.address_hash, key)) } fn contains(&self, key: &H256) -> bool { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return true; } self.db.contains(&combine_key(&self.address_hash, key)) @@ -171,16 +172,16 @@ impl<'db> HashDB for AccountDBMut<'db>{ fn insert(&mut self, value: &[u8]) -> H256 { if value == &NULL_RLP { - return SHA3_NULL_RLP.clone(); + return KECCAK_NULL_RLP.clone(); } - let k = value.sha3(); + let k = keccak(value); let ak = combine_key(&self.address_hash, &k); self.db.emplace(ak, DBValue::from_slice(value)); k } fn emplace(&mut self, key: H256, value: DBValue) { - if key == SHA3_NULL_RLP { + if key == KECCAK_NULL_RLP { return; } let key = combine_key(&self.address_hash, &key); @@ -188,7 +189,7 @@ impl<'db> HashDB for AccountDBMut<'db>{ } fn remove(&mut self, key: &H256) { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return; } let key = combine_key(&self.address_hash, key); @@ -204,14 +205,14 @@ impl<'db> HashDB for Wrapping<'db> { } fn get(&self, key: &H256) -> Option { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return Some(DBValue::from_slice(&NULL_RLP)); } self.0.get(key) } fn contains(&self, key: &H256) -> bool { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return true; } self.0.contains(key) @@ -238,14 +239,14 @@ impl<'db> HashDB for WrappingMut<'db>{ } fn get(&self, key: &H256) -> Option { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return Some(DBValue::from_slice(&NULL_RLP)); } self.0.get(key) } fn contains(&self, key: &H256) -> bool { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return true; } self.0.contains(key) @@ -253,20 +254,20 @@ impl<'db> HashDB for WrappingMut<'db>{ fn insert(&mut self, value: &[u8]) -> H256 { if value == &NULL_RLP { - return SHA3_NULL_RLP.clone(); + return KECCAK_NULL_RLP.clone(); } self.0.insert(value) } fn emplace(&mut self, key: H256, value: DBValue) { - if key == SHA3_NULL_RLP { + if key == KECCAK_NULL_RLP { return; } self.0.emplace(key, value) } fn remove(&mut self, key: &H256) { - if key == &SHA3_NULL_RLP { + if key == &KECCAK_NULL_RLP { return; } self.0.remove(key) diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 64958700e..35eb41a83 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -19,9 +19,10 @@ use std::cmp; use std::sync::Arc; use std::collections::HashSet; +use hash::{keccak, KECCAK_NULL_RLP}; use rlp::{UntrustedRlp, RlpStream, Encodable, Decodable, DecoderError}; -use util::{Bytes, Address, Hashable, U256, H256, ordered_trie_root, SHA3_NULL_RLP}; +use util::{Bytes, Address, U256, H256, ordered_trie_root}; use util::error::{Mismatch, OutOfBounds}; use basic_types::{LogBloom, Seal}; @@ -400,7 +401,7 @@ impl<'x> OpenBlock<'x> { } s.block.header.set_transactions_root(ordered_trie_root(s.block.transactions.iter().map(|e| e.rlp_bytes().into_vec()))); let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append_raw(&u.rlp(Seal::With), 1); s} ).out(); - s.block.header.set_uncles_hash(uncle_bytes.sha3()); + s.block.header.set_uncles_hash(keccak(&uncle_bytes)); s.block.header.set_state_root(s.block.state.root().clone()); s.block.header.set_receipts_root(ordered_trie_root(s.block.receipts.iter().map(|r| r.rlp_bytes().into_vec()))); s.block.header.set_log_bloom(s.block.receipts.iter().fold(LogBloom::zero(), |mut b, r| {b = &b | &r.log_bloom; b})); //TODO: use |= operator @@ -425,14 +426,14 @@ impl<'x> OpenBlock<'x> { if let Err(e) = s.block.state.commit() { warn!("Encountered error on state commit: {}", e); } - if s.block.header.transactions_root().is_zero() || s.block.header.transactions_root() == &SHA3_NULL_RLP { + if s.block.header.transactions_root().is_zero() || s.block.header.transactions_root() == &KECCAK_NULL_RLP { s.block.header.set_transactions_root(ordered_trie_root(s.block.transactions.iter().map(|e| e.rlp_bytes().into_vec()))); } let uncle_bytes = s.block.uncles.iter().fold(RlpStream::new_list(s.block.uncles.len()), |mut s, u| {s.append_raw(&u.rlp(Seal::With), 1); s} ).out(); if s.block.header.uncles_hash().is_zero() { - s.block.header.set_uncles_hash(uncle_bytes.sha3()); + s.block.header.set_uncles_hash(keccak(&uncle_bytes)); } - if s.block.header.receipts_root().is_zero() || s.block.header.receipts_root() == &SHA3_NULL_RLP { + if s.block.header.receipts_root().is_zero() || s.block.header.receipts_root() == &KECCAK_NULL_RLP { s.block.header.set_receipts_root(ordered_trie_root(s.block.receipts.iter().map(|r| r.rlp_bytes().into_vec()))); } @@ -465,7 +466,7 @@ impl<'x> IsBlock for LockedBlock { impl ClosedBlock { /// Get the hash of the header without seal arguments. - pub fn hash(&self) -> H256 { self.header().rlp_sha3(Seal::Without) } + pub fn hash(&self) -> H256 { self.header().rlp_keccak(Seal::Without) } /// Turn this into a `LockedBlock`, unable to be reopened again. pub fn lock(self) -> LockedBlock { @@ -490,7 +491,7 @@ impl ClosedBlock { impl LockedBlock { /// Get the hash of the header without seal arguments. - pub fn hash(&self) -> H256 { self.header().rlp_sha3(Seal::Without) } + pub fn hash(&self) -> H256 { self.header().rlp_keccak(Seal::Without) } /// Provide a valid seal in order to turn this into a `SealedBlock`. /// diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index 001df861c..a39417321 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -511,7 +511,7 @@ impl BlockChain { // we need to insert genesis into the cache let block = BlockView::new(genesis); let header = block.header_view(); - let hash = block.sha3(); + let hash = block.hash(); let details = BlockDetails { number: header.number(), @@ -763,7 +763,7 @@ impl BlockChain { pub fn insert_unordered_block(&self, batch: &mut DBTransaction, bytes: &[u8], receipts: Vec, parent_td: Option, is_best: bool, is_ancient: bool) -> bool { let block = BlockView::new(bytes); let header = block.header_view(); - let hash = header.sha3(); + let hash = header.hash(); if self.is_known(&hash) { return false; @@ -965,7 +965,7 @@ impl BlockChain { // create views onto rlp let block = BlockView::new(bytes); let header = block.header_view(); - let hash = header.sha3(); + let hash = header.hash(); if self.is_known_child(&header.parent_hash(), &hash) { return ImportRoute::none(); @@ -1004,7 +1004,7 @@ impl BlockChain { /// Get inserted block info which is critical to prepare extras updates. fn block_info(&self, header: &HeaderView) -> BlockInfo { - let hash = header.sha3(); + let hash = header.hash(); let number = header.number(); let parent_hash = header.parent_hash(); let parent_details = self.block_details(&parent_hash).unwrap_or_else(|| panic!("Invalid parent hash: {:?}", parent_hash)); @@ -1465,9 +1465,9 @@ mod tests { #![cfg_attr(feature="dev", allow(similar_names))] use std::sync::Arc; use rustc_hex::FromHex; + use hash::keccak; use util::kvdb::KeyValueDB; use util::hash::*; - use util::sha3::Hashable; use receipt::Receipt; use blockchain::{BlockProvider, BlockChain, Config, ImportRoute}; use tests::helpers::*; @@ -1517,8 +1517,8 @@ mod tests { let mut finalizer = BlockFinalizer::default(); let genesis = canon_chain.generate(&mut finalizer).unwrap(); let first = canon_chain.generate(&mut finalizer).unwrap(); - let genesis_hash = BlockView::new(&genesis).header_view().sha3(); - let first_hash = BlockView::new(&first).header_view().sha3(); + let genesis_hash = BlockView::new(&genesis).header_view().hash(); + let first_hash = BlockView::new(&first).header_view().hash(); let db = new_db(); let bc = new_chain(&genesis, db.clone()); @@ -1548,7 +1548,7 @@ mod tests { let mut canon_chain = ChainGenerator::default(); let mut finalizer = BlockFinalizer::default(); let genesis = canon_chain.generate(&mut finalizer).unwrap(); - let genesis_hash = BlockView::new(&genesis).header_view().sha3(); + let genesis_hash = BlockView::new(&genesis).header_view().hash(); let db = new_db(); let bc = new_chain(&genesis, db.clone()); @@ -1557,7 +1557,7 @@ mod tests { let mut batch = db.transaction(); for _ in 0..10 { let block = canon_chain.generate(&mut finalizer).unwrap(); - block_hashes.push(BlockView::new(&block).header_view().sha3()); + block_hashes.push(BlockView::new(&block).header_view().hash()); bc.insert_block(&mut batch, &block, vec![]); bc.commit(); } @@ -1606,14 +1606,14 @@ mod tests { assert_eq!( [&b4b, &b3b, &b2b].iter().map(|b| BlockView::new(b).header()).collect::>(), - bc.find_uncle_headers(&BlockView::new(&b4a).header_view().sha3(), 3).unwrap() + bc.find_uncle_headers(&BlockView::new(&b4a).header_view().hash(), 3).unwrap() ); // TODO: insert block that already includes one of them as an uncle to check it's not allowed. } fn secret() -> Secret { - "".sha3().into() + keccak("").into() } #[test] @@ -1645,8 +1645,8 @@ mod tests { let b2 = fork_chain .generate(&mut fork_finalizer).unwrap(); - let b1a_hash = BlockView::new(&b1a).header_view().sha3(); - let b2_hash = BlockView::new(&b2).header_view().sha3(); + let b1a_hash = BlockView::new(&b1a).header_view().hash(); + let b2_hash = BlockView::new(&b2).header_view().hash(); let t1_hash = t1.hash(); @@ -1729,9 +1729,9 @@ mod tests { .with_transaction(t3.clone()) .generate(&mut fork_finalizer).unwrap(); - let b1a_hash = BlockView::new(&b1a).header_view().sha3(); - let b1b_hash = BlockView::new(&b1b).header_view().sha3(); - let b2_hash = BlockView::new(&b2).header_view().sha3(); + let b1a_hash = BlockView::new(&b1a).header_view().hash(); + let b1b_hash = BlockView::new(&b1b).header_view().hash(); + let b2_hash = BlockView::new(&b2).header_view().hash(); let t1_hash = t1.hash(); let t2_hash = t2.hash(); @@ -1789,11 +1789,11 @@ mod tests { let b3b = canon_chain.fork(1).generate(&mut finalizer.fork()).unwrap(); let b3a = canon_chain.generate(&mut finalizer).unwrap(); - let genesis_hash = BlockView::new(&genesis).header_view().sha3(); - let b1_hash= BlockView::new(&b1).header_view().sha3(); - let b2_hash= BlockView::new(&b2).header_view().sha3(); - let b3a_hash= BlockView::new(&b3a).header_view().sha3(); - let b3b_hash= BlockView::new(&b3b).header_view().sha3(); + let genesis_hash = BlockView::new(&genesis).header_view().hash(); + let b1_hash= BlockView::new(&b1).header_view().hash(); + let b2_hash= BlockView::new(&b2).header_view().hash(); + let b3a_hash= BlockView::new(&b3a).header_view().hash(); + let b3b_hash= BlockView::new(&b3b).header_view().hash(); // b3a is a part of canon chain, whereas b3b is part of sidechain let best_block_hash = b3a_hash.clone(); @@ -1909,8 +1909,8 @@ mod tests { let mut finalizer = BlockFinalizer::default(); let genesis = canon_chain.generate(&mut finalizer).unwrap(); let first = canon_chain.generate(&mut finalizer).unwrap(); - let genesis_hash = BlockView::new(&genesis).header_view().sha3(); - let first_hash = BlockView::new(&first).header_view().sha3(); + let genesis_hash = BlockView::new(&genesis).header_view().hash(); + let first_hash = BlockView::new(&first).header_view().hash(); let db = new_db(); { @@ -2225,9 +2225,9 @@ mod tests { let genesis = canon_chain.generate(&mut finalizer).unwrap(); let first = canon_chain.generate(&mut finalizer).unwrap(); let second = canon_chain.generate(&mut finalizer).unwrap(); - let genesis_hash = BlockView::new(&genesis).header_view().sha3(); - let first_hash = BlockView::new(&first).header_view().sha3(); - let second_hash = BlockView::new(&second).header_view().sha3(); + let genesis_hash = BlockView::new(&genesis).header_view().hash(); + let first_hash = BlockView::new(&first).header_view().hash(); + let second_hash = BlockView::new(&second).header_view().hash(); let db = new_db(); let bc = new_chain(&genesis, db.clone()); @@ -2265,7 +2265,7 @@ mod tests { // create a longer fork for i in 0..5 { let canon_block = canon_chain.generate(&mut finalizer).unwrap(); - let hash = BlockView::new(&canon_block).header_view().sha3(); + let hash = BlockView::new(&canon_block).header_view().hash(); bc.insert_block(&mut batch, &canon_block, vec![]); bc.insert_epoch_transition(&mut batch, i, EpochTransition { @@ -2278,7 +2278,7 @@ mod tests { assert_eq!(bc.best_block_number(), 5); - let hash = BlockView::new(&uncle).header_view().sha3(); + let hash = BlockView::new(&uncle).header_view().hash(); bc.insert_block(&mut batch, &uncle, vec![]); bc.insert_epoch_transition(&mut batch, 999, EpochTransition { block_hash: hash, diff --git a/ethcore/src/blockchain/generator/complete.rs b/ethcore/src/blockchain/generator/complete.rs index 0c22d4dfc..4cdda3f47 100644 --- a/ethcore/src/blockchain/generator/complete.rs +++ b/ethcore/src/blockchain/generator/complete.rs @@ -16,7 +16,6 @@ use util::hash::H256; use util::bytes::Bytes; -use util::sha3::Hashable; use views::BlockView; #[derive(Default, Clone)] @@ -46,7 +45,7 @@ impl<'a, I> Iterator for Complete<'a, I> where I: Iterator, ::Ite fn next(&mut self) -> Option { self.iter.next().map(|item| { let rlp = item.complete(self.finalizer.parent_hash.clone()); - self.finalizer.parent_hash = BlockView::new(&rlp).header_view().sha3(); + self.finalizer.parent_hash = BlockView::new(&rlp).header_view().hash(); rlp }) } diff --git a/ethcore/src/blockchain/generator/generator.rs b/ethcore/src/blockchain/generator/generator.rs index a2c2c441a..be9378268 100644 --- a/ethcore/src/blockchain/generator/generator.rs +++ b/ethcore/src/blockchain/generator/generator.rs @@ -111,7 +111,6 @@ impl Iterator for ChainGenerator { mod tests { use util::hash::{H256, H2048}; - use util::sha3::Hashable; use views::BlockView; use blockchain::generator::{ChainIterator, ChainGenerator, BlockFinalizer}; @@ -129,7 +128,7 @@ mod tests { let b1_rlp = canon_chain.generate(&mut finalizer).unwrap(); let b1 = BlockView::new(&b1_rlp); - assert_eq!(b1.header_view().parent_hash(), genesis.header_view().sha3()); + assert_eq!(b1.header_view().parent_hash(), genesis.header_view().hash()); assert_eq!(b1.header_view().number(), 1); let mut fork_chain = canon_chain.fork(1); @@ -137,13 +136,13 @@ mod tests { let b2_rlp_fork = fork_chain.generate(&mut finalizer.fork()).unwrap(); let b2_fork = BlockView::new(&b2_rlp_fork); - assert_eq!(b2_fork.header_view().parent_hash(), b1.header_view().sha3()); + assert_eq!(b2_fork.header_view().parent_hash(), b1.header_view().hash()); assert_eq!(b2_fork.header_view().number(), 2); let b2_rlp = canon_chain.generate(&mut finalizer).unwrap(); let b2 = BlockView::new(&b2_rlp); - assert_eq!(b2.header_view().parent_hash(), b1.header_view().sha3()); + assert_eq!(b2.header_view().parent_hash(), b1.header_view().hash()); assert_eq!(b2.header_view().number(), 2); assert!(b2.header_view().difficulty() > b2_fork.header_view().difficulty()); } @@ -163,7 +162,7 @@ mod tests { assert_eq!(block0.header_view().parent_hash(), H256::default()); assert_eq!(block1.header_view().number(), 1); - assert_eq!(block1.header_view().parent_hash(), block0.header_view().sha3()); + assert_eq!(block1.header_view().parent_hash(), block0.header_view().hash()); } diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index 9e49e610c..b6d7064eb 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -23,7 +23,8 @@ use crypto::ripemd160::Ripemd160 as Ripemd160Digest; use crypto::digest::Digest; use num::{BigUint, Zero, One}; -use util::{U256, H256, Hashable, BytesRef}; +use hash::keccak; +use util::{U256, H256, BytesRef}; use ethkey::{Signature, recover as ec_recover}; use ethjson; @@ -228,7 +229,7 @@ impl Impl for EcRecover { let s = Signature::from_rsv(&r, &s, bit); if s.is_valid() { if let Ok(p) = ec_recover(&s, &hash) { - let r = p.sha3(); + let r = keccak(p); output.write(0, &[0; 12]); output.write(12, &r[12..r.len()]); } @@ -572,14 +573,6 @@ mod tests { #[test] fn ecrecover() { - /*let k = KeyPair::from_secret(b"test".sha3()).unwrap(); - let a: Address = From::from(k.public().sha3()); - println!("Address: {}", a); - let m = b"hello world".sha3(); - println!("Message: {}", m); - let s = k.sign(&m).unwrap(); - println!("Signed: {}", s);*/ - let f = ethereum_builtin("ecrecover"); let i = FromHex::from_hex("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap(); diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index ac8a0aea9..9d5465593 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -23,7 +23,8 @@ use time::precise_time_ns; use itertools::Itertools; // util -use util::{Bytes, PerfTimer, Mutex, RwLock, MutexGuard, Hashable}; +use hash::keccak; +use util::{Bytes, PerfTimer, Mutex, RwLock, MutexGuard}; use util::{journaldb, DBValue, TrieFactory, Trie}; use util::{U256, H256, Address, H2048}; use util::trie::TrieSpec; @@ -652,7 +653,7 @@ impl Client { .map(Into::into) .collect(); - assert_eq!(header.hash(), BlockView::new(block_data).header_view().sha3()); + assert_eq!(header.hash(), BlockView::new(block_data).header_view().hash()); //let traces = From::from(block.traces().clone().unwrap_or_else(Vec::new)); @@ -1503,7 +1504,7 @@ impl BlockChainClient for Client { }; let (_, db) = state.drop(); - let account_db = self.factories.accountdb.readonly(db.as_hashdb(), account.sha3()); + let account_db = self.factories.accountdb.readonly(db.as_hashdb(), keccak(account)); let trie = match self.factories.trie.readonly(account_db.as_hashdb(), &root) { Ok(trie) => trie, _ => { @@ -1591,7 +1592,7 @@ impl BlockChainClient for Client { use verification::queue::kind::BlockLike; use verification::queue::kind::blocks::Unverified; - // create unverified block here so the `sha3` calculation can be cached. + // create unverified block here so the `keccak` calculation can be cached. let unverified = Unverified::new(bytes); { @@ -1792,7 +1793,7 @@ impl BlockChainClient for Client { let dispatch = move |reg_addr, data| { future::done(self.call_contract(BlockId::Latest, reg_addr, data)) }; - r.get_address(dispatch, name.as_bytes().sha3(), "A".to_string()).wait().ok() + r.get_address(dispatch, keccak(name.as_bytes()), "A".to_string()).wait().ok() }) .and_then(|a| if a.is_zero() { None } else { Some(a) }) } @@ -2067,16 +2068,16 @@ mod tests { #[test] fn should_return_correct_log_index() { + use hash::keccak; use super::transaction_receipt; use ethkey::KeyPair; use log_entry::{LogEntry, LocalizedLogEntry}; use receipt::{Receipt, LocalizedReceipt}; use transaction::{Transaction, LocalizedTransaction, Action}; - use util::Hashable; use tests::helpers::TestEngine; // given - let key = KeyPair::from_secret_slice(&"test".sha3()).unwrap(); + let key = KeyPair::from_secret_slice(&keccak("test")).unwrap(); let secret = key.secret(); let engine = TestEngine::new(0); diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 0e7e53e43..256d7953d 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -22,6 +22,7 @@ use std::collections::{HashMap, BTreeMap}; use std::mem; use itertools::Itertools; use rustc_hex::FromHex; +use hash::keccak; use util::*; use rlp::*; use ethkey::{Generator, Random}; @@ -241,7 +242,7 @@ impl TestBlockChainClient { uncle_header.set_parent_hash(self.last_hash.read().clone()); uncle_header.set_number(n as BlockNumber); uncles.append(&uncle_header); - header.set_uncles_hash(uncles.as_raw().sha3()); + header.set_uncles_hash(keccak(uncles.as_raw())); uncles }, _ => RlpStream::new_list(0) diff --git a/ethcore/src/encoded.rs b/ethcore/src/encoded.rs index 125a00fd0..e6eb025e9 100644 --- a/ethcore/src/encoded.rs +++ b/ethcore/src/encoded.rs @@ -28,7 +28,8 @@ use header::{BlockNumber, Header as FullHeader}; use transaction::UnverifiedTransaction; use views; -use util::{Address, Hashable, H256, H2048, U256, HeapSizeOf}; +use util::{Address, H256, H2048, U256, HeapSizeOf}; +use hash::keccak; use rlp::Rlp; /// Owning header view. @@ -64,7 +65,7 @@ impl Header { // forwarders to borrowed view. impl Header { /// Returns the header hash. - pub fn hash(&self) -> H256 { self.sha3() } + pub fn hash(&self) -> H256 { keccak(&self.0) } /// Returns the parent hash. pub fn parent_hash(&self) -> H256 { self.view().parent_hash() } @@ -109,12 +110,6 @@ impl Header { pub fn seal(&self) -> Vec> { self.view().seal() } } -impl Hashable for Header { - fn sha3(&self) -> H256 { - self.0.sha3() - } -} - /// Owning block body view. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "ipc", binary)] @@ -218,7 +213,7 @@ impl Block { // forwarders to borrowed header view. impl Block { /// Returns the header hash. - pub fn hash(&self) -> H256 { self.header_view().sha3() } + pub fn hash(&self) -> H256 { self.header_view().hash() } /// Returns the parent hash. pub fn parent_hash(&self) -> H256 { self.header_view().parent_hash() } diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 508cd3e25..acc1a3834 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -841,6 +841,7 @@ impl Engine for AuthorityRound { mod tests { use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; + use hash::keccak; use util::*; use header::Header; use error::{Error, BlockError}; @@ -895,8 +896,8 @@ mod tests { #[test] fn generates_seal_and_does_not_double_propose() { let tap = Arc::new(AccountProvider::transient_provider()); - let addr1 = tap.insert_account("1".sha3().into(), "1").unwrap(); - let addr2 = tap.insert_account("2".sha3().into(), "2").unwrap(); + let addr1 = tap.insert_account(keccak("1").into(), "1").unwrap(); + let addr2 = tap.insert_account(keccak("2").into(), "2").unwrap(); let spec = Spec::new_test_round(); let engine = &*spec.engine; @@ -927,7 +928,7 @@ mod tests { #[test] fn proposer_switching() { let tap = AccountProvider::transient_provider(); - let addr = tap.insert_account("0".sha3().into(), "0").unwrap(); + let addr = tap.insert_account(keccak("0").into(), "0").unwrap(); let mut parent_header: Header = Header::default(); parent_header.set_seal(vec![encode(&0usize).into_vec()]); parent_header.set_gas_limit("222222".parse::().unwrap()); @@ -952,7 +953,7 @@ mod tests { #[test] fn rejects_future_block() { let tap = AccountProvider::transient_provider(); - let addr = tap.insert_account("0".sha3().into(), "0").unwrap(); + let addr = tap.insert_account(keccak("0").into(), "0").unwrap(); let mut parent_header: Header = Header::default(); parent_header.set_seal(vec![encode(&0usize).into_vec()]); @@ -978,7 +979,7 @@ mod tests { #[test] fn rejects_step_backwards() { let tap = AccountProvider::transient_provider(); - let addr = tap.insert_account("0".sha3().into(), "0").unwrap(); + let addr = tap.insert_account(keccak("0").into(), "0").unwrap(); let mut parent_header: Header = Header::default(); parent_header.set_seal(vec![encode(&4usize).into_vec()]); diff --git a/ethcore/src/engines/basic_authority.rs b/ethcore/src/engines/basic_authority.rs index 5d6a44fc5..c22e66dfd 100644 --- a/ethcore/src/engines/basic_authority.rs +++ b/ethcore/src/engines/basic_authority.rs @@ -252,6 +252,7 @@ impl Engine for BasicAuthority { #[cfg(test)] mod tests { use std::sync::Arc; + use hash::keccak; use util::*; use block::*; use error::{BlockError, Error}; @@ -308,7 +309,7 @@ mod tests { #[test] fn can_generate_seal() { let tap = AccountProvider::transient_provider(); - let addr = tap.insert_account("".sha3().into(), "").unwrap(); + let addr = tap.insert_account(keccak("").into(), "").unwrap(); let spec = new_test_authority(); let engine = &*spec.engine; @@ -326,7 +327,7 @@ mod tests { #[test] fn seals_internally() { let tap = AccountProvider::transient_provider(); - let authority = tap.insert_account("".sha3().into(), "").unwrap(); + let authority = tap.insert_account(keccak("").into(), "").unwrap(); let engine = new_test_authority().engine; assert!(!engine.seals_internally().unwrap()); diff --git a/ethcore/src/engines/tendermint/message.rs b/ethcore/src/engines/tendermint/message.rs index 68bdcb0f7..ae0050bbb 100644 --- a/ethcore/src/engines/tendermint/message.rs +++ b/ethcore/src/engines/tendermint/message.rs @@ -17,6 +17,7 @@ //! Tendermint message handling. use std::cmp; +use hash::keccak; use util::*; use super::{Height, View, BlockHash, Step}; use error::Error; @@ -99,7 +100,7 @@ impl ConsensusMessage { pub fn verify(&self) -> Result { let full_rlp = ::rlp::encode(self); let block_info = Rlp::new(&full_rlp).at(1); - let public_key = recover(&self.signature.into(), &block_info.as_raw().sha3())?; + let public_key = recover(&self.signature.into(), &keccak(block_info.as_raw()))?; Ok(public_to_address(&public_key)) } } @@ -194,12 +195,13 @@ pub fn message_full_rlp(signature: &H520, vote_info: &Bytes) -> Bytes { } pub fn message_hash(vote_step: VoteStep, block_hash: H256) -> H256 { - message_info_rlp(&vote_step, Some(block_hash)).sha3() + keccak(message_info_rlp(&vote_step, Some(block_hash))) } #[cfg(test)] mod tests { use std::sync::Arc; + use hash::keccak; use util::*; use rlp::*; use account_provider::AccountProvider; @@ -228,7 +230,7 @@ mod tests { view: 123, step: Step::Precommit, }, - block_hash: Some("1".sha3()) + block_hash: Some(keccak("1")), }; let raw_rlp = ::rlp::encode(&message).into_vec(); let rlp = Rlp::new(&raw_rlp); @@ -251,12 +253,12 @@ mod tests { #[test] fn generate_and_verify() { let tap = Arc::new(AccountProvider::transient_provider()); - let addr = tap.insert_account("0".sha3().into(), "0").unwrap(); + let addr = tap.insert_account(keccak("0").into(), "0").unwrap(); tap.unlock_account_permanently(addr, "0".into()).unwrap(); let mi = message_info_rlp(&VoteStep::new(123, 2, Step::Precommit), Some(H256::default())); - let raw_rlp = message_full_rlp(&tap.sign(addr, None, mi.sha3()).unwrap().into(), &mi); + let raw_rlp = message_full_rlp(&tap.sign(addr, None, keccak(&mi)).unwrap().into(), &mi); let rlp = UntrustedRlp::new(&raw_rlp); let message: ConsensusMessage = rlp.as_val().unwrap(); diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index cc75e99c3..0f63bac17 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -28,6 +28,7 @@ mod params; use std::sync::{Weak, Arc}; use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; use std::collections::{HashSet, BTreeMap, HashMap}; +use hash::keccak; use std::cmp; use util::*; use client::{Client, EngineClient}; @@ -214,7 +215,7 @@ impl Tendermint { let r = self.view.load(AtomicOrdering::SeqCst); let s = *self.step.read(); let vote_info = message_info_rlp(&VoteStep::new(h, r, s), block_hash); - match (self.signer.read().address(), self.sign(vote_info.sha3()).map(Into::into)) { + match (self.signer.read().address(), self.sign(keccak(&vote_info)).map(Into::into)) { (Some(validator), Ok(signature)) => { let message_rlp = message_full_rlp(&signature, &vote_info); let message = ConsensusMessage::new(signature, h, r, s, block_hash); @@ -499,7 +500,7 @@ impl Engine for Tendermint { let view = self.view.load(AtomicOrdering::SeqCst); let bh = Some(header.bare_hash()); let vote_info = message_info_rlp(&VoteStep::new(height, view, Step::Propose), bh.clone()); - if let Ok(signature) = self.sign(vote_info.sha3()).map(Into::into) { + if let Ok(signature) = self.sign(keccak(&vote_info)).map(Into::into) { // Insert Propose vote. debug!(target: "engine", "Submitting proposal {} at height {} view {}.", header.bare_hash(), height, view); self.votes.vote(ConsensusMessage::new(signature, height, view, Step::Propose, bh), author); @@ -523,7 +524,7 @@ impl Engine for Tendermint { let rlp = UntrustedRlp::new(rlp); let message: ConsensusMessage = rlp.as_val()?; if !self.votes.is_old_or_known(&message) { - let sender = public_to_address(&recover(&message.signature.into(), &rlp.at(1)?.as_raw().sha3())?); + let sender = public_to_address(&recover(&message.signature.into(), &keccak(rlp.at(1)?.as_raw()))?); if !self.is_authority(&sender) { return Err(EngineError::NotAuthorized(sender).into()); } @@ -809,7 +810,7 @@ mod tests { fn vote(engine: &Engine, signer: F, height: usize, view: usize, step: Step, block_hash: Option) -> Bytes where F: FnOnce(H256) -> Result { let mi = message_info_rlp(&VoteStep::new(height, view, step), block_hash); - let m = message_full_rlp(&signer(mi.sha3()).unwrap().into(), &mi); + let m = message_full_rlp(&signer(keccak(&mi)).unwrap().into(), &mi); engine.handle_message(&m).unwrap(); m } @@ -817,7 +818,7 @@ mod tests { fn proposal_seal(tap: &Arc, header: &Header, view: View) -> Vec { let author = header.author(); let vote_info = message_info_rlp(&VoteStep::new(header.number() as Height, view, Step::Propose), Some(header.bare_hash())); - let signature = tap.sign(*author, None, vote_info.sha3()).unwrap(); + let signature = tap.sign(*author, None, keccak(vote_info)).unwrap(); vec![ ::rlp::encode(&view).into_vec(), ::rlp::encode(&H520::from(signature)).into_vec(), @@ -826,7 +827,7 @@ mod tests { } fn insert_and_unlock(tap: &Arc, acc: &str) -> Address { - let addr = tap.insert_account(acc.sha3().into(), acc).unwrap(); + let addr = tap.insert_account(keccak(acc).into(), acc).unwrap(); tap.unlock_account_permanently(addr, acc.into()).unwrap(); addr } @@ -933,7 +934,7 @@ mod tests { let mut seal = proposal_seal(&tap, &header, 0); let vote_info = message_info_rlp(&VoteStep::new(2, 0, Step::Precommit), Some(header.bare_hash())); - let signature1 = tap.sign(proposer, None, vote_info.sha3()).unwrap(); + let signature1 = tap.sign(proposer, None, keccak(&vote_info)).unwrap(); seal[1] = ::rlp::NULL_RLP.to_vec(); seal[2] = ::rlp::encode_list(&vec![H520::from(signature1.clone())]).into_vec(); @@ -946,7 +947,7 @@ mod tests { } let voter = insert_and_unlock(&tap, "0"); - let signature0 = tap.sign(voter, None, vote_info.sha3()).unwrap(); + let signature0 = tap.sign(voter, None, keccak(&vote_info)).unwrap(); seal[2] = ::rlp::encode_list(&vec![H520::from(signature1.clone()), H520::from(signature0.clone())]).into_vec(); header.set_seal(seal.clone()); @@ -954,7 +955,7 @@ mod tests { assert!(engine.verify_block_family(&header, &parent_header, None).is_ok()); let bad_voter = insert_and_unlock(&tap, "101"); - let bad_signature = tap.sign(bad_voter, None, vote_info.sha3()).unwrap(); + let bad_signature = tap.sign(bad_voter, None, keccak(vote_info)).unwrap(); seal[2] = ::rlp::encode_list(&vec![H520::from(signature1), H520::from(bad_signature)]).into_vec(); header.set_seal(seal); @@ -1087,10 +1088,10 @@ mod tests { let mut seal = proposal_seal(&tap, &header, 0); let vote_info = message_info_rlp(&VoteStep::new(2, 0, Step::Precommit), Some(header.bare_hash())); - let signature1 = tap.sign(proposer, None, vote_info.sha3()).unwrap(); + let signature1 = tap.sign(proposer, None, keccak(&vote_info)).unwrap(); let voter = insert_and_unlock(&tap, "0"); - let signature0 = tap.sign(voter, None, vote_info.sha3()).unwrap(); + let signature0 = tap.sign(voter, None, keccak(&vote_info)).unwrap(); seal[1] = ::rlp::NULL_RLP.to_vec(); seal[2] = ::rlp::encode_list(&vec![H520::from(signature1.clone())]).into_vec(); @@ -1127,7 +1128,7 @@ mod tests { assert!(epoch_verifier.verify_light(&header).is_ok()); let bad_voter = insert_and_unlock(&tap, "101"); - let bad_signature = tap.sign(bad_voter, None, vote_info.sha3()).unwrap(); + let bad_signature = tap.sign(bad_voter, None, keccak(&vote_info)).unwrap(); seal[2] = ::rlp::encode_list(&vec![H520::from(signature1), H520::from(bad_signature)]).into_vec(); header.set_seal(seal); diff --git a/ethcore/src/engines/validator_set/contract.rs b/ethcore/src/engines/validator_set/contract.rs index 7c1890379..ff16eb1d2 100644 --- a/ethcore/src/engines/validator_set/contract.rs +++ b/ethcore/src/engines/validator_set/contract.rs @@ -128,6 +128,7 @@ impl ValidatorSet for ValidatorContract { mod tests { use std::sync::Arc; use rustc_hex::FromHex; + use hash::keccak; use util::*; use rlp::encode; use spec::Spec; @@ -153,7 +154,7 @@ mod tests { #[test] fn reports_validators() { let tap = Arc::new(AccountProvider::transient_provider()); - let v1 = tap.insert_account("1".sha3().into(), "").unwrap(); + let v1 = tap.insert_account(keccak("1").into(), "").unwrap(); let client = generate_dummy_client_with_spec_and_accounts(Spec::new_validator_contract, Some(tap.clone())); client.engine().register_client(Arc::downgrade(&client)); let validator_contract = "0000000000000000000000000000000000000005".parse::
().unwrap(); diff --git a/ethcore/src/engines/validator_set/multi.rs b/ethcore/src/engines/validator_set/multi.rs index 9acf6050b..477a453b2 100644 --- a/ethcore/src/engines/validator_set/multi.rs +++ b/ethcore/src/engines/validator_set/multi.rs @@ -144,6 +144,7 @@ impl ValidatorSet for Multi { mod tests { use std::sync::Arc; use std::collections::BTreeMap; + use hash::keccak; use account_provider::AccountProvider; use client::{BlockChainClient, EngineClient}; use engines::EpochChange; @@ -163,9 +164,9 @@ mod tests { let _ = ::env_logger::init(); let tap = Arc::new(AccountProvider::transient_provider()); - let s0: Secret = "0".sha3().into(); + let s0: Secret = keccak("0").into(); let v0 = tap.insert_account(s0.clone(), "").unwrap(); - let v1 = tap.insert_account("1".sha3().into(), "").unwrap(); + let v1 = tap.insert_account(keccak("1").into(), "").unwrap(); let client = generate_dummy_client_with_spec_and_accounts(Spec::new_validator_multi, Some(tap)); client.engine().register_client(Arc::downgrade(&client)); diff --git a/ethcore/src/engines/validator_set/safe_contract.rs b/ethcore/src/engines/validator_set/safe_contract.rs index 1abcf2c8f..6480a298e 100644 --- a/ethcore/src/engines/validator_set/safe_contract.rs +++ b/ethcore/src/engines/validator_set/safe_contract.rs @@ -19,6 +19,7 @@ use std::sync::{Weak, Arc}; use futures::Future; use native_contracts::ValidatorSet as Provider; +use hash::keccak; use util::*; use util::cache::MemoryLruCache; @@ -41,7 +42,7 @@ const MEMOIZE_CAPACITY: usize = 500; const EVENT_NAME: &'static [u8] = &*b"InitiateChange(bytes32,address[])"; lazy_static! { - static ref EVENT_NAME_HASH: H256 = EVENT_NAME.sha3(); + static ref EVENT_NAME_HASH: H256 = keccak(EVENT_NAME); } /// The validator contract should have the following interface: @@ -424,6 +425,7 @@ impl ValidatorSet for ValidatorSafeContract { mod tests { use std::sync::Arc; use rustc_hex::FromHex; + use hash::keccak; use util::*; use types::ids::BlockId; use spec::Spec; @@ -449,9 +451,9 @@ mod tests { #[test] fn knows_validators() { let tap = Arc::new(AccountProvider::transient_provider()); - let s0: Secret = "1".sha3().into(); + let s0: Secret = keccak("1").into(); let v0 = tap.insert_account(s0.clone(), "").unwrap(); - let v1 = tap.insert_account("0".sha3().into(), "").unwrap(); + let v1 = tap.insert_account(keccak("0").into(), "").unwrap(); let chain_id = Spec::new_validator_safe_contract().chain_id(); let client = generate_dummy_client_with_spec_and_accounts(Spec::new_validator_safe_contract, Some(tap)); client.engine().register_client(Arc::downgrade(&client)); diff --git a/ethcore/src/engines/vote_collector.rs b/ethcore/src/engines/vote_collector.rs index b934fdb2e..387715a03 100644 --- a/ethcore/src/engines/vote_collector.rs +++ b/ethcore/src/engines/vote_collector.rs @@ -206,6 +206,7 @@ impl VoteCollector { #[cfg(test)] mod tests { + use hash::keccak; use util::*; use rlp::*; use super::*; @@ -251,7 +252,7 @@ mod tests { #[test] fn seal_retrieval() { let collector = VoteCollector::default(); - let bh = Some("1".sha3()); + let bh = Some(keccak("1")); let mut signatures = Vec::new(); for _ in 0..5 { signatures.push(H520::random()); @@ -263,9 +264,9 @@ mod tests { // Good proposal random_vote(&collector, signatures[0].clone(), propose_round.clone(), bh.clone()); // Wrong block proposal. - random_vote(&collector, signatures[0].clone(), propose_round.clone(), Some("0".sha3())); + random_vote(&collector, signatures[0].clone(), propose_round.clone(), Some(keccak("0"))); // Wrong block commit. - random_vote(&collector, signatures[3].clone(), commit_round.clone(), Some("0".sha3())); + random_vote(&collector, signatures[3].clone(), commit_round.clone(), Some(keccak("0"))); // Wrong round. random_vote(&collector, signatures[0].clone(), 6, bh.clone()); // Wrong round. @@ -291,22 +292,22 @@ mod tests { let round1 = 1; let round3 = 3; // good 1 - random_vote(&collector, H520::random(), round1, Some("0".sha3())); - random_vote(&collector, H520::random(), 0, Some("0".sha3())); + random_vote(&collector, H520::random(), round1, Some(keccak("0"))); + random_vote(&collector, H520::random(), 0, Some(keccak("0"))); // good 3 - random_vote(&collector, H520::random(), round3, Some("0".sha3())); - random_vote(&collector, H520::random(), 2, Some("0".sha3())); + random_vote(&collector, H520::random(), round3, Some(keccak("0"))); + random_vote(&collector, H520::random(), 2, Some(keccak("0"))); // good prevote - random_vote(&collector, H520::random(), round1, Some("1".sha3())); + random_vote(&collector, H520::random(), round1, Some(keccak("1"))); // good prevote let same_sig = H520::random(); - random_vote(&collector, same_sig.clone(), round1, Some("1".sha3())); - random_vote(&collector, same_sig, round1, Some("1".sha3())); + random_vote(&collector, same_sig.clone(), round1, Some(keccak("1"))); + random_vote(&collector, same_sig, round1, Some(keccak("1"))); // good precommit - random_vote(&collector, H520::random(), round3, Some("1".sha3())); + random_vote(&collector, H520::random(), round3, Some(keccak("1"))); // good prevote - random_vote(&collector, H520::random(), round1, Some("0".sha3())); - random_vote(&collector, H520::random(), 4, Some("2".sha3())); + random_vote(&collector, H520::random(), round1, Some(keccak("0"))); + random_vote(&collector, H520::random(), 4, Some(keccak("2"))); assert_eq!(collector.count_round_votes(&round1), 4); assert_eq!(collector.count_round_votes(&round3), 2); @@ -314,7 +315,7 @@ mod tests { let message = TestMessage { signature: H520::default(), step: round1, - block_hash: Some("1".sha3()) + block_hash: Some(keccak("1")) }; assert_eq!(collector.count_aligned_votes(&message), 2); } @@ -325,11 +326,11 @@ mod tests { let vote = |round, hash| { random_vote(&collector, H520::random(), round, hash); }; - vote(6, Some("0".sha3())); - vote(3, Some("0".sha3())); - vote(7, Some("0".sha3())); - vote(8, Some("1".sha3())); - vote(1, Some("1".sha3())); + vote(6, Some(keccak("0"))); + vote(3, Some(keccak("0"))); + vote(7, Some(keccak("0"))); + vote(8, Some(keccak("1"))); + vote(1, Some(keccak("1"))); collector.throw_out_old(&7); assert_eq!(collector.len(), 2); @@ -340,9 +341,9 @@ mod tests { let collector = VoteCollector::default(); let round = 3; // Vote is inserted fine. - assert!(full_vote(&collector, H520::random(), round, Some("0".sha3()), &Address::default())); + assert!(full_vote(&collector, H520::random(), round, Some(keccak("0")), &Address::default())); // Returns the double voting address. - assert!(!full_vote(&collector, H520::random(), round, Some("1".sha3()), &Address::default())); + assert!(!full_vote(&collector, H520::random(), round, Some(keccak("1")), &Address::default())); assert_eq!(collector.count_round_votes(&round), 1); } } diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index fc7b27403..cab05b9ca 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -18,6 +18,7 @@ use std::path::Path; use std::cmp; use std::collections::{BTreeMap, HashMap}; use std::sync::Arc; +use hash::{KECCAK_EMPTY_LIST_RLP}; use ethash::{quick_get_difficulty, slow_get_seedhash, EthashManager}; use util::*; use block::*; @@ -458,7 +459,7 @@ impl Ethash { panic!("Can't calculate genesis block difficulty"); } - let parent_has_uncles = parent.uncles_hash() != &sha3::SHA3_EMPTY_LIST_RLP; + let parent_has_uncles = parent.uncles_hash() != &KECCAK_EMPTY_LIST_RLP; let min_difficulty = self.ethash_params.minimum_difficulty; diff --git a/ethcore/src/ethereum/mod.rs b/ethcore/src/ethereum/mod.rs index dee86883b..0b2b1ec3e 100644 --- a/ethcore/src/ethereum/mod.rs +++ b/ethcore/src/ethereum/mod.rs @@ -119,7 +119,7 @@ mod tests { assert_eq!(morden.state_root(), "f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9".into()); let genesis = morden.genesis_block(); - assert_eq!(BlockView::new(&genesis).header_view().sha3(), "0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303".into()); + assert_eq!(BlockView::new(&genesis).header_view().hash(), "0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303".into()); let _ = morden.engine; } @@ -130,7 +130,7 @@ mod tests { assert_eq!(frontier.state_root(), "d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".into()); let genesis = frontier.genesis_block(); - assert_eq!(BlockView::new(&genesis).header_view().sha3(), "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3".into()); + assert_eq!(BlockView::new(&genesis).header_view().hash(), "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3".into()); let _ = frontier.engine; } diff --git a/ethcore/src/executive.rs b/ethcore/src/executive.rs index 66a770243..7ac35945b 100644 --- a/ethcore/src/executive.rs +++ b/ethcore/src/executive.rs @@ -17,6 +17,7 @@ //! Transaction Execution environment. use std::cmp; use std::sync::Arc; +use hash::keccak; use util::*; use state::{Backend as StateBackend, State, Substate, CleanupMode}; use engines::Engine; @@ -47,20 +48,20 @@ pub fn contract_address(address_scheme: CreateContractAddress, sender: &Address, let mut stream = RlpStream::new_list(2); stream.append(sender); stream.append(nonce); - (From::from(stream.as_raw().sha3()), None) + (From::from(keccak(stream.as_raw())), None) }, CreateContractAddress::FromCodeHash => { - let code_hash = code.sha3(); + let code_hash = keccak(code); let mut buffer = [0xffu8; 20 + 32]; &mut buffer[20..].copy_from_slice(&code_hash[..]); - (From::from((&buffer[..]).sha3()), Some(code_hash)) + (From::from(keccak(&buffer[..])), Some(code_hash)) }, CreateContractAddress::FromSenderAndCodeHash => { - let code_hash = code.sha3(); + let code_hash = keccak(code); let mut buffer = [0u8; 20 + 32]; &mut buffer[..20].copy_from_slice(&sender[..]); &mut buffer[20..].copy_from_slice(&code_hash[..]); - (From::from((&buffer[..]).sha3()), Some(code_hash)) + (From::from(keccak(&buffer[..])), Some(code_hash)) }, } } @@ -1307,8 +1308,8 @@ mod tests { } } - evm_test!{test_sha3: test_sha3_jit, test_sha3_int} - fn test_sha3(factory: Factory) { + evm_test!{test_keccak: test_keccak_jit, test_keccak_int} + fn test_keccak(factory: Factory) { let code = "6064640fffffffff20600055".from_hex().unwrap(); let sender = Address::from_str("0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6").unwrap(); diff --git a/ethcore/src/header.rs b/ethcore/src/header.rs index a9a4f948d..14bbe5d85 100644 --- a/ethcore/src/header.rs +++ b/ethcore/src/header.rs @@ -18,6 +18,7 @@ use std::cmp; use std::cell::RefCell; +use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP, keccak}; use util::*; use basic_types::{LogBloom, ZERO_LOGBLOOM}; use time::get_time; @@ -100,12 +101,12 @@ impl Default for Header { number: 0, author: Address::default(), - transactions_root: SHA3_NULL_RLP, - uncles_hash: SHA3_EMPTY_LIST_RLP, + transactions_root: KECCAK_NULL_RLP, + uncles_hash: KECCAK_EMPTY_LIST_RLP, extra_data: vec![], - state_root: SHA3_NULL_RLP, - receipts_root: SHA3_NULL_RLP, + state_root: KECCAK_NULL_RLP, + receipts_root: KECCAK_NULL_RLP, log_bloom: ZERO_LOGBLOOM.clone(), gas_used: U256::default(), gas_limit: U256::default(), @@ -194,13 +195,13 @@ impl Header { /// Set the seal field of the header. pub fn set_seal(&mut self, a: Vec) { self.seal = a; self.note_dirty(); } - /// Get the hash of this header (sha3 of the RLP). + /// Get the hash of this header (keccak of the RLP). pub fn hash(&self) -> H256 { let mut hash = self.hash.borrow_mut(); match &mut *hash { &mut Some(ref h) => h.clone(), hash @ &mut None => { - let h = self.rlp_sha3(Seal::With); + let h = self.rlp_keccak(Seal::With); *hash = Some(h.clone()); h } @@ -213,7 +214,7 @@ impl Header { match &mut *hash { &mut Some(ref h) => h.clone(), hash @ &mut None => { - let h = self.rlp_sha3(Seal::Without); + let h = self.rlp_keccak(Seal::Without); *hash = Some(h.clone()); h } @@ -257,8 +258,8 @@ impl Header { s.out() } - /// Get the SHA3 (Keccak) of this header, optionally `with_seal`. - pub fn rlp_sha3(&self, with_seal: Seal) -> H256 { self.rlp(with_seal).sha3() } + /// Get the KECCAK (Keccak) of this header, optionally `with_seal`. + pub fn rlp_keccak(&self, with_seal: Seal) -> H256 { keccak(self.rlp(with_seal)) } } impl Decodable for Header { @@ -278,7 +279,7 @@ impl Decodable for Header { timestamp: cmp::min(r.val_at::(11)?, u64::max_value().into()).as_u64(), extra_data: r.val_at(12)?, seal: vec![], - hash: RefCell::new(Some(r.as_raw().sha3())), + hash: RefCell::new(Some(keccak(r.as_raw()))), bare_hash: RefCell::new(None), }; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 15d44626b..5b5c0e5f6 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -101,6 +101,7 @@ extern crate num; extern crate price_info; extern crate rand; extern crate rlp; +extern crate hash; #[macro_use] extern crate rlp_derive; diff --git a/ethcore/src/migrations/state/v7.rs b/ethcore/src/migrations/state/v7.rs index 9a9a65d7b..5a5a92764 100644 --- a/ethcore/src/migrations/state/v7.rs +++ b/ethcore/src/migrations/state/v7.rs @@ -23,14 +23,14 @@ use util::Bytes; use util::{Address, H256}; use util::kvdb::Database; use util::migration::{Batch, Config, Error, Migration, SimpleMigration, Progress}; -use util::sha3::Hashable; +use hash::keccak; use std::sync::Arc; use rlp::{decode, Rlp, RlpStream}; // 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(); + let val_hash = keccak(val); if key_h != val_hash { // this is a key which has been xor'd with an address. @@ -43,7 +43,7 @@ fn attempt_migrate(mut key_h: H256, val: &[u8]) -> Option { return None; } - let address_hash = Address::from(address).sha3(); + let address_hash = keccak(Address::from(address)); // create the xor'd key in place. key_h.copy_from_slice(&*val_hash); diff --git a/ethcore/src/miner/banning_queue.rs b/ethcore/src/miner/banning_queue.rs index 46d7e7cf8..82de8a747 100644 --- a/ethcore/src/miner/banning_queue.rs +++ b/ethcore/src/miner/banning_queue.rs @@ -24,7 +24,8 @@ use transient_hashmap::TransientHashMap; use miner::{TransactionQueue, TransactionQueueDetailsProvider, TransactionImportResult, TransactionOrigin}; use miner::transaction_queue::QueuingInstant; use error::{Error, TransactionError}; -use util::{U256, H256, Address, Hashable}; +use util::{U256, H256, Address}; +use hash::keccak; type Count = u16; @@ -103,7 +104,7 @@ impl BanningTransactionQueue { // Check code if let Action::Create = transaction.action { - let code_hash = transaction.data.sha3(); + let code_hash = keccak(&transaction.data); let count = self.codes_bans.direct().get(&code_hash).cloned().unwrap_or(0); if count > threshold { debug!(target: "txqueue", "Ignoring transaction {:?} because code is banned.", transaction.hash()); @@ -131,7 +132,7 @@ impl BanningTransactionQueue { self.ban_recipient(recipient) }, Action::Create => { - self.ban_codehash(transaction.data.sha3()) + self.ban_codehash(keccak(&transaction.data)) }, }; sender_banned || recipient_or_code_banned @@ -210,13 +211,14 @@ impl DerefMut for BanningTransactionQueue { mod tests { use std::time::Duration; use rustc_hex::FromHex; + use hash::keccak; use super::{BanningTransactionQueue, Threshold}; use ethkey::{Random, Generator}; use transaction::{Transaction, SignedTransaction, Action}; use error::{Error, TransactionError}; use client::TransactionImportResult; use miner::{TransactionQueue, TransactionOrigin}; - use util::{U256, Address, Hashable}; + use util::{U256, Address}; use miner::transaction_queue::test::DummyTransactionDetailsProvider; fn queue() -> BanningTransactionQueue { @@ -310,7 +312,7 @@ mod tests { fn should_not_accept_transactions_with_banned_code() { // given let tx = transaction(Action::Create); - let codehash = tx.data.sha3(); + let codehash = keccak(&tx.data); let mut txq = queue(); // Banlist once (threshold not reached) let banlist1 = txq.ban_codehash(codehash); diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 0639c645f..2fa4a76e2 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -1235,6 +1235,7 @@ mod tests { use std::sync::Arc; use std::time::Duration; use rustc_hex::FromHex; + use hash::keccak; use super::super::{MinerService, PrioritizationStrategy}; use super::*; use block::IsBlock; @@ -1418,7 +1419,7 @@ mod tests { fn should_fail_setting_engine_signer_on_pow() { let spec = Spec::new_pow_test_spec; let tap = Arc::new(AccountProvider::transient_provider()); - let addr = tap.insert_account("1".sha3().into(), "").unwrap(); + let addr = tap.insert_account(keccak("1").into(), "").unwrap(); let client = generate_dummy_client_with_spec_and_accounts(spec, Some(tap.clone())); assert!(match client.miner().set_engine_signer(addr, "".into()) { Err(AccountError::InappropriateChain) => true, _ => false }) } @@ -1427,7 +1428,7 @@ mod tests { fn should_fail_setting_engine_signer_without_account_provider() { let spec = Spec::new_instant; let tap = Arc::new(AccountProvider::transient_provider()); - let addr = tap.insert_account("1".sha3().into(), "").unwrap(); + let addr = tap.insert_account(keccak("1").into(), "").unwrap(); let client = generate_dummy_client_with_spec_and_accounts(spec, None); assert!(match client.miner().set_engine_signer(addr, "".into()) { Err(AccountError::NotFound) => true, _ => false }); } diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index 4204b591d..b8606008c 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -17,6 +17,7 @@ use std::fmt; use std::collections::BTreeMap; use itertools::Itertools; +use hash::{keccak}; use util::*; use state::Account; use ethjson; @@ -61,7 +62,7 @@ impl PodAccount { stream.append(&self.nonce); stream.append(&self.balance); stream.append(&sec_trie_root(self.storage.iter().map(|(k, v)| (k.to_vec(), rlp::encode(&U256::from(&**v)).to_vec())).collect())); - stream.append(&self.code.as_ref().unwrap_or(&vec![]).sha3()); + stream.append(&keccak(&self.code.as_ref().unwrap_or(&vec![]))); stream.out() } @@ -117,7 +118,7 @@ impl fmt::Display for PodAccount { self.balance, self.nonce, self.code.as_ref().map_or(0, |c| c.len()), - self.code.as_ref().map_or_else(H256::new, |c| c.sha3()), + self.code.as_ref().map_or_else(H256::new, |c| keccak(c)), self.storage.len(), ) } diff --git a/ethcore/src/snapshot/account.rs b/ethcore/src/snapshot/account.rs index a9f2ba593..2df29989b 100644 --- a/ethcore/src/snapshot/account.rs +++ b/ethcore/src/snapshot/account.rs @@ -19,8 +19,9 @@ use account_db::{AccountDB, AccountDBMut}; use basic_account::BasicAccount; use snapshot::Error; +use hash::{KECCAK_EMPTY, KECCAK_NULL_RLP}; -use util::{U256, H256, Bytes, HashDB, SHA3_EMPTY, SHA3_NULL_RLP}; +use util::{U256, H256, Bytes, HashDB}; use util::trie::{TrieDB, Trie}; use rlp::{RlpStream, UntrustedRlp}; @@ -30,8 +31,8 @@ use std::collections::HashSet; const ACC_EMPTY: BasicAccount = BasicAccount { nonce: U256([0, 0, 0, 0]), balance: U256([0, 0, 0, 0]), - storage_root: SHA3_NULL_RLP, - code_hash: SHA3_EMPTY, + storage_root: KECCAK_NULL_RLP, + code_hash: KECCAK_EMPTY, }; // whether an encoded account has code and how it is referred to. @@ -78,7 +79,7 @@ pub fn to_fat_rlps(account_hash: &H256, acc: &BasicAccount, acct_db: &AccountDB, .append(&acc.balance); // [has_code, code_hash]. - if acc.code_hash == SHA3_EMPTY { + if acc.code_hash == KECCAK_EMPTY { account_stream.append(&CodeState::Empty.raw()).append_empty_data(); } else if used_code.contains(&acc.code_hash) { account_stream.append(&CodeState::Hash.raw()).append(&acc.code_hash); @@ -164,7 +165,7 @@ pub fn from_fat_rlp( // load the code if it exists. let (code_hash, new_code) = match code_state { - CodeState::Empty => (SHA3_EMPTY, None), + CodeState::Empty => (KECCAK_EMPTY, None), CodeState::Inline => { let code: Bytes = rlp.val_at(3)?; let code_hash = acct_db.insert(&code); @@ -210,8 +211,8 @@ mod tests { use tests::helpers::get_temp_state_db; use snapshot::tests::helpers::fill_storage; - use util::sha3::{SHA3_EMPTY, SHA3_NULL_RLP}; - use util::{Address, H256, HashDB, DBValue, Hashable}; + use hash::{KECCAK_EMPTY, KECCAK_NULL_RLP, keccak}; + use util::{Address, H256, HashDB, DBValue}; use rlp::UntrustedRlp; use std::collections::HashSet; @@ -226,14 +227,14 @@ mod tests { let account = BasicAccount { nonce: 50.into(), balance: 123456789.into(), - storage_root: SHA3_NULL_RLP, - code_hash: SHA3_EMPTY, + storage_root: KECCAK_NULL_RLP, + code_hash: KECCAK_EMPTY, }; let thin_rlp = ::rlp::encode(&account); assert_eq!(::rlp::decode::(&thin_rlp), account); - let fat_rlps = to_fat_rlps(&addr.sha3(), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap(); + let fat_rlps = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap(); let fat_rlp = UntrustedRlp::new(&fat_rlps[0]).at(1).unwrap(); assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account); } @@ -245,20 +246,20 @@ mod tests { let account = { let acct_db = AccountDBMut::new(db.as_hashdb_mut(), &addr); - let mut root = SHA3_NULL_RLP; + let mut root = KECCAK_NULL_RLP; fill_storage(acct_db, &mut root, &mut H256::zero()); BasicAccount { nonce: 25.into(), balance: 987654321.into(), storage_root: root, - code_hash: SHA3_EMPTY, + code_hash: KECCAK_EMPTY, } }; let thin_rlp = ::rlp::encode(&account); assert_eq!(::rlp::decode::(&thin_rlp), account); - let fat_rlp = to_fat_rlps(&addr.sha3(), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap(); + let fat_rlp = to_fat_rlps(&keccak(&addr), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), usize::max_value(), usize::max_value()).unwrap(); let fat_rlp = UntrustedRlp::new(&fat_rlp[0]).at(1).unwrap(); assert_eq!(from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp, H256::zero()).unwrap().0, account); } @@ -270,21 +271,21 @@ mod tests { let account = { let acct_db = AccountDBMut::new(db.as_hashdb_mut(), &addr); - let mut root = SHA3_NULL_RLP; + let mut root = KECCAK_NULL_RLP; fill_storage(acct_db, &mut root, &mut H256::zero()); BasicAccount { nonce: 25.into(), balance: 987654321.into(), storage_root: root, - code_hash: SHA3_EMPTY, + code_hash: KECCAK_EMPTY, } }; let thin_rlp = ::rlp::encode(&account); assert_eq!(::rlp::decode::(&thin_rlp), account); - let fat_rlps = to_fat_rlps(&addr.sha3(), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), 500, 1000).unwrap(); - let mut root = SHA3_NULL_RLP; + let fat_rlps = to_fat_rlps(&keccak(addr), &account, &AccountDB::new(db.as_hashdb(), &addr), &mut Default::default(), 500, 1000).unwrap(); + let mut root = KECCAK_NULL_RLP; let mut restored_account = None; for rlp in fat_rlps { let fat_rlp = UntrustedRlp::new(&rlp).at(1).unwrap(); @@ -314,21 +315,21 @@ mod tests { let account1 = BasicAccount { nonce: 50.into(), balance: 123456789.into(), - storage_root: SHA3_NULL_RLP, + storage_root: KECCAK_NULL_RLP, code_hash: code_hash, }; let account2 = BasicAccount { nonce: 400.into(), balance: 98765432123456789usize.into(), - storage_root: SHA3_NULL_RLP, + storage_root: KECCAK_NULL_RLP, code_hash: code_hash, }; let mut used_code = HashSet::new(); - let fat_rlp1 = to_fat_rlps(&addr1.sha3(), &account1, &AccountDB::new(db.as_hashdb(), &addr1), &mut used_code, usize::max_value(), usize::max_value()).unwrap(); - let fat_rlp2 = to_fat_rlps(&addr2.sha3(), &account2, &AccountDB::new(db.as_hashdb(), &addr2), &mut used_code, usize::max_value(), usize::max_value()).unwrap(); + let fat_rlp1 = to_fat_rlps(&keccak(&addr1), &account1, &AccountDB::new(db.as_hashdb(), &addr1), &mut used_code, usize::max_value(), usize::max_value()).unwrap(); + let fat_rlp2 = to_fat_rlps(&keccak(&addr2), &account2, &AccountDB::new(db.as_hashdb(), &addr2), &mut used_code, usize::max_value(), usize::max_value()).unwrap(); assert_eq!(used_code.len(), 1); let fat_rlp1 = UntrustedRlp::new(&fat_rlp1[0]).at(1).unwrap(); diff --git a/ethcore/src/snapshot/block.rs b/ethcore/src/snapshot/block.rs index e7b100f69..fa4b3c19f 100644 --- a/ethcore/src/snapshot/block.rs +++ b/ethcore/src/snapshot/block.rs @@ -18,10 +18,11 @@ use block::Block; use header::Header; +use hash::keccak; use views::BlockView; use rlp::{DecoderError, RlpStream, UntrustedRlp}; -use util::{Bytes, Hashable, H256}; +use util::{Bytes, H256}; use util::triehash::ordered_trie_root; const HEADER_FIELDS: usize = 8; @@ -111,7 +112,7 @@ impl AbridgedBlock { let mut uncles_rlp = RlpStream::new(); uncles_rlp.append_list(&uncles); - header.set_uncles_hash(uncles_rlp.as_raw().sha3()); + header.set_uncles_hash(keccak(uncles_rlp.as_raw())); let mut seal_fields = Vec::new(); for i in (HEADER_FIELDS + BLOCK_FIELDS)..rlp.item_count()? { diff --git a/ethcore/src/snapshot/io.rs b/ethcore/src/snapshot/io.rs index f28adcf7f..fc3962e73 100644 --- a/ethcore/src/snapshot/io.rs +++ b/ethcore/src/snapshot/io.rs @@ -342,7 +342,7 @@ impl SnapshotReader for LooseReader { #[cfg(test)] mod tests { use devtools::RandomTempPath; - use util::sha3::Hashable; + use hash::keccak; use snapshot::ManifestData; use super::{SnapshotWriter, SnapshotReader, PackedWriter, PackedReader, LooseWriter, LooseReader, SNAPSHOT_VERSION}; @@ -359,24 +359,24 @@ mod tests { let mut block_hashes = Vec::new(); for chunk in STATE_CHUNKS { - let hash = chunk.sha3(); + let hash = keccak(&chunk); state_hashes.push(hash.clone()); writer.write_state_chunk(hash, chunk).unwrap(); } for chunk in BLOCK_CHUNKS { - let hash = chunk.sha3(); + let hash = keccak(&chunk); block_hashes.push(hash.clone()); - writer.write_block_chunk(chunk.sha3(), chunk).unwrap(); + writer.write_block_chunk(keccak(&chunk), chunk).unwrap(); } let manifest = ManifestData { version: SNAPSHOT_VERSION, state_hashes: state_hashes, block_hashes: block_hashes, - state_root: b"notarealroot".sha3(), + state_root: keccak(b"notarealroot"), block_number: 12345678987654321, - block_hash: b"notarealblock".sha3(), + block_hash: keccak(b"notarealblock"), }; writer.finish(manifest.clone()).unwrap(); @@ -398,24 +398,24 @@ mod tests { let mut block_hashes = Vec::new(); for chunk in STATE_CHUNKS { - let hash = chunk.sha3(); + let hash = keccak(&chunk); state_hashes.push(hash.clone()); writer.write_state_chunk(hash, chunk).unwrap(); } for chunk in BLOCK_CHUNKS { - let hash = chunk.sha3(); + let hash = keccak(&chunk); block_hashes.push(hash.clone()); - writer.write_block_chunk(chunk.sha3(), chunk).unwrap(); + writer.write_block_chunk(keccak(&chunk), chunk).unwrap(); } let manifest = ManifestData { version: SNAPSHOT_VERSION, state_hashes: state_hashes, block_hashes: block_hashes, - state_root: b"notarealroot".sha3(), + state_root: keccak(b"notarealroot"), block_number: 12345678987654321, - block_hash: b"notarealblock".sha3(), + block_hash: keccak(b"notarealblock)"), }; writer.finish(manifest.clone()).unwrap(); diff --git a/ethcore/src/snapshot/mod.rs b/ethcore/src/snapshot/mod.rs index a15c15566..974b074cf 100644 --- a/ethcore/src/snapshot/mod.rs +++ b/ethcore/src/snapshot/mod.rs @@ -22,6 +22,7 @@ use std::collections::{HashMap, HashSet}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY}; use account_db::{AccountDB, AccountDBMut}; use blockchain::{BlockChain, BlockProvider}; @@ -29,13 +30,12 @@ use engines::Engine; use header::Header; use ids::BlockId; -use util::{Bytes, Hashable, HashDB, DBValue, snappy, U256}; +use util::{Bytes, HashDB, DBValue, snappy, U256}; use util::Mutex; use util::hash::{H256}; use util::journaldb::{self, Algorithm, JournalDB}; use util::kvdb::KeyValueDB; use util::trie::{TrieDB, TrieDBMut, Trie, TrieMut}; -use util::sha3::SHA3_NULL_RLP; use rlp::{RlpStream, UntrustedRlp}; use bloom_journal::Bloom; @@ -183,7 +183,7 @@ pub fn chunk_secondary<'a>(mut chunker: Box, chain: &'a Bloc let mut chunk_sink = |raw_data: &[u8]| { let compressed_size = snappy::compress_into(raw_data, &mut snappy_buffer); let compressed = &snappy_buffer[..compressed_size]; - let hash = compressed.sha3(); + let hash = keccak(&compressed); let size = compressed.len(); writer.lock().write_block_chunk(hash, compressed)?; @@ -240,7 +240,7 @@ impl<'a> StateChunker<'a> { let compressed_size = snappy::compress_into(&raw_data, &mut self.snappy_buffer); let compressed = &self.snappy_buffer[..compressed_size]; - let hash = compressed.sha3(); + let hash = keccak(&compressed); self.writer.lock().write_state_chunk(hash, compressed)?; trace!(target: "snapshot", "wrote state chunk. size: {}, uncompressed size: {}", compressed_size, raw_data.len()); @@ -318,7 +318,7 @@ impl StateRebuilder { pub fn new(db: Arc, pruning: Algorithm) -> Self { StateRebuilder { db: journaldb::new(db.clone(), pruning, ::db::COL_STATE), - state_root: SHA3_NULL_RLP, + state_root: KECCAK_NULL_RLP, known_code: HashMap::new(), missing_code: HashMap::new(), bloom: StateDB::load_bloom(&*db), @@ -362,7 +362,7 @@ impl StateRebuilder { // batch trie writes { - let mut account_trie = if self.state_root != SHA3_NULL_RLP { + let mut account_trie = if self.state_root != KECCAK_NULL_RLP { TrieDBMut::from_existing(self.db.as_hashdb_mut(), &mut self.state_root)? } else { TrieDBMut::new(self.db.as_hashdb_mut(), &mut self.state_root) @@ -443,7 +443,7 @@ fn rebuild_accounts( // new inline code Some(code) => status.new_code.push((code_hash, code, hash)), None => { - if code_hash != ::util::SHA3_EMPTY { + if code_hash != KECCAK_EMPTY { // see if this code has already been included inline match known_code.get(&code_hash) { Some(&first_with) => { diff --git a/ethcore/src/snapshot/tests/helpers.rs b/ethcore/src/snapshot/tests/helpers.rs index 6429667f5..bfc9db92a 100644 --- a/ethcore/src/snapshot/tests/helpers.rs +++ b/ethcore/src/snapshot/tests/helpers.rs @@ -18,6 +18,7 @@ //! which can be queried before and after a full snapshot/restore cycle. use std::sync::Arc; +use hash::{KECCAK_NULL_RLP}; use account_db::AccountDBMut; use basic_account::BasicAccount; @@ -36,7 +37,6 @@ use util::hashdb::HashDB; use util::journaldb; use util::trie::{Alphabet, StandardMap, SecTrieDBMut, TrieMut, ValueMode}; use util::trie::{TrieDB, TrieDBMut, Trie}; -use util::sha3::SHA3_NULL_RLP; // the proportion of accounts we will alter each tick. const ACCOUNT_CHURN: f32 = 0.01; @@ -51,7 +51,7 @@ impl StateProducer { /// Create a new `StateProducer`. pub fn new() -> Self { StateProducer { - state_root: SHA3_NULL_RLP, + state_root: KECCAK_NULL_RLP, storage_seed: H256::zero(), } } @@ -115,7 +115,7 @@ pub fn fill_storage(mut db: AccountDBMut, root: &mut H256, seed: &mut H256) { count: 100, }; { - let mut trie = if *root == SHA3_NULL_RLP { + let mut trie = if *root == KECCAK_NULL_RLP { SecTrieDBMut::new(&mut db, root) } else { SecTrieDBMut::from_existing(&mut db, root).unwrap() diff --git a/ethcore/src/snapshot/tests/proof_of_authority.rs b/ethcore/src/snapshot/tests/proof_of_authority.rs index 7c44ec20a..509ccb8fd 100644 --- a/ethcore/src/snapshot/tests/proof_of_authority.rs +++ b/ethcore/src/snapshot/tests/proof_of_authority.rs @@ -30,7 +30,7 @@ use spec::Spec; use tests::helpers; use transaction::{Transaction, Action, SignedTransaction}; -use util::{Address, Hashable}; +use util::Address; use util::kvdb; const PASS: &'static str = ""; @@ -38,14 +38,14 @@ const TRANSITION_BLOCK_1: usize = 2; // block at which the contract becomes acti const TRANSITION_BLOCK_2: usize = 10; // block at which the second contract activates. macro_rules! secret { - ($e: expr) => { Secret::from_slice(&$e.sha3()) } + ($e: expr) => { Secret::from_slice(&$crate::hash::keccak($e)) } } lazy_static! { // contract addresses. static ref CONTRACT_ADDR_1: Address = Address::from_str("0000000000000000000000000000000000000005").unwrap(); static ref CONTRACT_ADDR_2: Address = Address::from_str("0000000000000000000000000000000000000006").unwrap(); - // secret: `sha3(1)`, and initial validator. + // secret: `keccak(1)`, and initial validator. static ref RICH_ADDR: Address = Address::from_str("7d577a597b2742b498cb5cf0c26cdcd726d39e6e").unwrap(); // rich address' secret. static ref RICH_SECRET: Secret = secret!("1"); @@ -53,7 +53,7 @@ lazy_static! { /// Contract code used here: https://gist.github.com/anonymous/2a43783647e0f0dfcc359bd6fd81d6d9 -/// Account with secrets "1".sha3() is initially the validator. +/// Account with secrets keccak("1") is initially the validator. /// Transitions to the contract at block 2, initially same validator set. /// Create a new Spec with AuthorityRound which uses a contract at address 5 to determine the current validators using `getValidators`. /// `native_contracts::test_contracts::ValidatorSet` provides a native wrapper for the ABi. diff --git a/ethcore/src/snapshot/tests/proof_of_work.rs b/ethcore/src/snapshot/tests/proof_of_work.rs index a6c0166f5..907fcead8 100644 --- a/ethcore/src/snapshot/tests/proof_of_work.rs +++ b/ethcore/src/snapshot/tests/proof_of_work.rs @@ -71,7 +71,7 @@ fn chunk_and_restore(amount: u64) { version: 2, state_hashes: Vec::new(), block_hashes: block_hashes, - state_root: ::util::sha3::SHA3_NULL_RLP, + state_root: ::hash::KECCAK_NULL_RLP, block_number: amount, block_hash: best_hash, }; @@ -134,7 +134,7 @@ fn checks_flag() { version: 2, state_hashes: Vec::new(), block_hashes: Vec::new(), - state_root: ::util::sha3::SHA3_NULL_RLP, + state_root: ::hash::KECCAK_NULL_RLP, block_number: 102, block_hash: H256::default(), }; diff --git a/ethcore/src/snapshot/tests/state.rs b/ethcore/src/snapshot/tests/state.rs index 744b86577..9a98af5ff 100644 --- a/ethcore/src/snapshot/tests/state.rs +++ b/ethcore/src/snapshot/tests/state.rs @@ -32,10 +32,9 @@ use util::memorydb::MemoryDB; use util::Mutex; use devtools::RandomTempPath; -use util::sha3::SHA3_NULL_RLP; - use std::sync::Arc; use std::sync::atomic::AtomicBool; +use hash::{KECCAK_NULL_RLP, keccak}; #[test] fn snap_and_restore() { @@ -98,7 +97,7 @@ fn snap_and_restore() { fn get_code_from_prev_chunk() { use std::collections::HashSet; use rlp::RlpStream; - use util::{HashDB, H256, U256, Hashable}; + use util::{HashDB, H256, U256}; use account_db::{AccountDBMut, AccountDB}; @@ -107,8 +106,8 @@ fn get_code_from_prev_chunk() { let mut acc_stream = RlpStream::new_list(4); acc_stream.append(&U256::default()) .append(&U256::default()) - .append(&SHA3_NULL_RLP) - .append(&code.sha3()); + .append(&KECCAK_NULL_RLP) + .append(&keccak(code)); let (h1, h2) = (H256::random(), H256::random()); diff --git a/ethcore/src/spec/genesis.rs b/ethcore/src/spec/genesis.rs index b7db9aa90..159320291 100644 --- a/ethcore/src/spec/genesis.rs +++ b/ethcore/src/spec/genesis.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use util::{Address, H256, U256}; -use util::sha3::SHA3_NULL_RLP; +use hash::KECCAK_NULL_RLP; use ethjson; use super::seal::Seal; @@ -54,8 +54,8 @@ impl From for Genesis { timestamp: g.timestamp.map_or(0, Into::into), parent_hash: g.parent_hash.map_or_else(H256::zero, Into::into), gas_limit: g.gas_limit.into(), - transactions_root: g.transactions_root.map_or_else(|| SHA3_NULL_RLP.clone(), Into::into), - receipts_root: g.receipts_root.map_or_else(|| SHA3_NULL_RLP.clone(), Into::into), + transactions_root: g.transactions_root.map_or_else(|| KECCAK_NULL_RLP.clone(), Into::into), + receipts_root: g.receipts_root.map_or_else(|| KECCAK_NULL_RLP.clone(), Into::into), state_root: g.state_root.map(Into::into), gas_used: g.gas_used.map_or_else(U256::zero, Into::into), extra_data: g.extra_data.map_or_else(Vec::new, Into::into), diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index 7d82a470c..02f16b0fa 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -21,6 +21,7 @@ use std::collections::BTreeMap; use std::path::Path; use std::sync::Arc; use rustc_hex::FromHex; +use hash::{KECCAK_NULL_RLP, keccak}; use super::genesis::Genesis; use super::seal::Generic as GenericSeal; @@ -203,9 +204,9 @@ pub struct Spec { pub gas_used: U256, /// The genesis block's timestamp field. pub timestamp: u64, - /// Transactions root of the genesis block. Should be SHA3_NULL_RLP. + /// Transactions root of the genesis block. Should be KECCAK_NULL_RLP. pub transactions_root: H256, - /// Receipts root of the genesis block. Should be SHA3_NULL_RLP. + /// Receipts root of the genesis block. Should be KECCAK_NULL_RLP. pub receipts_root: H256, /// The genesis block's extra data field. pub extra_data: Bytes, @@ -287,7 +288,7 @@ impl Spec { // given a pre-constructor state, run all the given constructors and produce a new state and state root. fn run_constructors(&self, factories: &Factories, mut db: T) -> Result { - let mut root = SHA3_NULL_RLP; + let mut root = KECCAK_NULL_RLP; // basic accounts in spec. { @@ -301,7 +302,7 @@ impl Spec { for (address, account) in self.genesis_state.get().iter() { db.note_non_null_account(address); account.insert_additional( - &mut *factories.accountdb.create(db.as_hashdb_mut(), address.sha3()), + &mut *factories.accountdb.create(db.as_hashdb_mut(), keccak(address)), &factories.trie ); } @@ -333,7 +334,7 @@ impl Spec { trace!(target: "spec", " .. root before = {}", state.root()); let params = ActionParams { code_address: address.clone(), - code_hash: Some(constructor.sha3()), + code_hash: Some(keccak(constructor)), address: address.clone(), sender: from.clone(), origin: from.clone(), @@ -399,7 +400,7 @@ impl Spec { header.set_number(0); header.set_author(self.author.clone()); header.set_transactions_root(self.transactions_root.clone()); - header.set_uncles_hash(RlpStream::new_list(0).out().sha3()); + header.set_uncles_hash(keccak(RlpStream::new_list(0).out())); header.set_extra_data(self.extra_data.clone()); header.set_state_root(self.state_root()); header.set_receipts_root(self.receipts_root.clone()); @@ -483,7 +484,7 @@ impl Spec { /// Create a new Spec which conforms to the Frontier-era Morden chain except that it's a NullEngine consensus. pub fn new_test() -> Spec { load_bundled!("null_morden") } - /// Create a new Spec which is a NullEngine consensus with a premine of address whose secret is sha3(''). + /// Create a new Spec which is a NullEngine consensus with a premine of address whose secret is keccak(''). pub fn new_null() -> Spec { load_bundled!("null") } /// Create a new Spec which constructs a contract at address 5 with storage at 0 equal to 1. @@ -493,15 +494,15 @@ impl Spec { pub fn new_instant() -> Spec { load_bundled!("instant_seal") } /// Create a new Spec with AuthorityRound consensus which does internal sealing (not requiring work). - /// Accounts with secrets "0".sha3() and "1".sha3() are the validators. + /// Accounts with secrets keccak("0") and keccak("1") are the validators. pub fn new_test_round() -> Self { load_bundled!("authority_round") } /// Create a new Spec with Tendermint consensus which does internal sealing (not requiring work). - /// Account "0".sha3() and "1".sha3() are a authorities. + /// Account keccak("0") and keccak("1") are a authorities. pub fn new_test_tendermint() -> Self { load_bundled!("tendermint") } /// TestList.sol used in both specs: https://github.com/paritytech/contracts/pull/30/files - /// Accounts with secrets "0".sha3() and "1".sha3() are initially the validators. + /// Accounts with secrets keccak("0") and keccak("1") are initially the validators. /// Create a new Spec with BasicAuthority which uses a contract at address 5 to determine the current validators using `getValidators`. /// Second validator can be removed with "0xbfc708a000000000000000000000000082a978b3f5962a5b0957d9ee9eef472ee55b42f1" and added back in using "0x4d238c8e00000000000000000000000082a978b3f5962a5b0957d9ee9eef472ee55b42f1". pub fn new_validator_safe_contract() -> Self { load_bundled!("validator_safe_contract") } @@ -512,7 +513,7 @@ impl Spec { pub fn new_validator_contract() -> Self { load_bundled!("validator_contract") } /// Create a new Spec with BasicAuthority which uses multiple validator sets changing with height. - /// Account with secrets "0".sha3() is the validator for block 1 and with "1".sha3() onwards. + /// Account with secrets keccak("0") is the validator for block 1 and with keccak("1") onwards. pub fn new_validator_multi() -> Self { load_bundled!("validator_multi") } /// Create a new spec for a PoW chain @@ -540,7 +541,7 @@ mod tests { assert_eq!(test_spec.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap()); let genesis = test_spec.genesis_block(); - assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap()); + assert_eq!(BlockView::new(&genesis).header_view().hash(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap()); } #[test] diff --git a/ethcore/src/state/account.rs b/ethcore/src/state/account.rs index 1235fd289..e5d8b3814 100644 --- a/ethcore/src/state/account.rs +++ b/ethcore/src/state/account.rs @@ -19,6 +19,7 @@ use std::fmt; use std::sync::Arc; use std::collections::HashMap; +use hash::{KECCAK_EMPTY, KECCAK_NULL_RLP, keccak}; use util::*; use pod_account::*; use rlp::*; @@ -81,10 +82,10 @@ impl Account { Account { balance: balance, nonce: nonce, - storage_root: SHA3_NULL_RLP, + storage_root: KECCAK_NULL_RLP, storage_cache: Self::empty_storage_cache(), storage_changes: storage, - code_hash: code.sha3(), + code_hash: keccak(&code), code_size: Some(code.len()), code_cache: Arc::new(code), code_filth: Filth::Dirty, @@ -101,10 +102,10 @@ impl Account { Account { balance: pod.balance, nonce: pod.nonce, - storage_root: SHA3_NULL_RLP, + storage_root: KECCAK_NULL_RLP, storage_cache: Self::empty_storage_cache(), storage_changes: pod.storage.into_iter().collect(), - code_hash: pod.code.as_ref().map_or(SHA3_EMPTY, |c| c.sha3()), + code_hash: pod.code.as_ref().map_or(KECCAK_EMPTY, |c| keccak(c)), code_filth: Filth::Dirty, code_size: Some(pod.code.as_ref().map_or(0, |c| c.len())), code_cache: Arc::new(pod.code.map_or_else(|| { warn!("POD account with unknown code is being created! Assuming no code."); vec![] }, |c| c)), @@ -117,10 +118,10 @@ impl Account { Account { balance: balance, nonce: nonce, - storage_root: SHA3_NULL_RLP, + storage_root: KECCAK_NULL_RLP, storage_cache: Self::empty_storage_cache(), storage_changes: HashMap::new(), - code_hash: SHA3_EMPTY, + code_hash: KECCAK_EMPTY, code_cache: Arc::new(vec![]), code_size: Some(0), code_filth: Filth::Clean, @@ -140,10 +141,10 @@ impl Account { Account { balance: balance, nonce: nonce, - storage_root: SHA3_NULL_RLP, + storage_root: KECCAK_NULL_RLP, storage_cache: Self::empty_storage_cache(), storage_changes: HashMap::new(), - code_hash: SHA3_EMPTY, + code_hash: KECCAK_EMPTY, code_cache: Arc::new(vec![]), code_size: None, code_filth: Filth::Clean, @@ -154,7 +155,7 @@ impl Account { /// Set this account's code to the given code. /// NOTE: Account should have been created with `new_contract()` pub fn init_code(&mut self, code: Bytes) { - self.code_hash = code.sha3(); + self.code_hash = keccak(&code); self.code_cache = Arc::new(code); self.code_size = Some(self.code_cache.len()); self.code_filth = Filth::Dirty; @@ -211,7 +212,7 @@ impl Account { pub fn address_hash(&self, address: &Address) -> H256 { let hash = self.address_hash.get(); hash.unwrap_or_else(|| { - let hash = address.sha3(); + let hash = keccak(address); self.address_hash.set(Some(hash.clone())); hash }) @@ -220,7 +221,7 @@ impl Account { /// returns the account's code. If `None` then the code cache isn't available - /// get someone who knows to call `note_code`. pub fn code(&self) -> Option> { - if self.code_hash != SHA3_EMPTY && self.code_cache.is_empty() { + if self.code_hash != KECCAK_EMPTY && self.code_cache.is_empty() { return None; } Some(self.code_cache.clone()) @@ -235,7 +236,7 @@ impl Account { #[cfg(test)] /// Provide a byte array which hashes to the `code_hash`. returns the hash as a result. pub fn note_code(&mut self, code: Bytes) -> Result<(), H256> { - let h = code.sha3(); + let h = keccak(&code); if self.code_hash == h { self.code_cache = Arc::new(code); self.code_size = Some(self.code_cache.len()); @@ -247,7 +248,7 @@ impl Account { /// Is `code_cache` valid; such that code is going to return Some? pub fn is_cached(&self) -> bool { - !self.code_cache.is_empty() || (self.code_cache.is_empty() && self.code_hash == SHA3_EMPTY) + !self.code_cache.is_empty() || (self.code_cache.is_empty() && self.code_hash == KECCAK_EMPTY) } /// Provide a database to get `code_hash`. Should not be called if it is a contract without code. @@ -284,7 +285,7 @@ impl Account { // TODO: fill out self.code_cache; trace!("Account::cache_code_size: ic={}; self.code_hash={:?}, self.code_cache={}", self.is_cached(), self.code_hash, self.code_cache.pretty()); self.code_size.is_some() || - if self.code_hash != SHA3_EMPTY { + if self.code_hash != KECCAK_EMPTY { match db.get(&self.code_hash) { Some(x) => { self.code_size = Some(x.len()); @@ -308,19 +309,19 @@ impl Account { /// NOTE: Will panic if `!self.storage_is_clean()` pub fn is_empty(&self) -> bool { assert!(self.storage_is_clean(), "Account::is_empty() may only legally be called when storage is clean."); - self.is_null() && self.storage_root == SHA3_NULL_RLP + self.is_null() && self.storage_root == KECCAK_NULL_RLP } /// Check if account has zero nonce, balance, no code. pub fn is_null(&self) -> bool { self.balance.is_zero() && self.nonce.is_zero() && - self.code_hash == SHA3_EMPTY + self.code_hash == KECCAK_EMPTY } /// Check if account is basic (Has no code). pub fn is_basic(&self) -> bool { - self.code_hash == SHA3_EMPTY + self.code_hash == KECCAK_EMPTY } /// Return the storage root associated with this account or None if it has been altered via the overlay. @@ -592,8 +593,8 @@ mod tests { assert_eq!(a.rlp().to_hex(), "f8448045a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); assert_eq!(a.balance(), &U256::from(69u8)); assert_eq!(a.nonce(), &U256::from(0u8)); - assert_eq!(a.code_hash(), SHA3_EMPTY); - assert_eq!(a.storage_root().unwrap(), &SHA3_NULL_RLP); + assert_eq!(a.code_hash(), KECCAK_EMPTY); + assert_eq!(a.storage_root().unwrap(), &KECCAK_NULL_RLP); } #[test] diff --git a/ethcore/src/state/mod.rs b/ethcore/src/state/mod.rs index 1b36c91ec..e7b0cb228 100644 --- a/ethcore/src/state/mod.rs +++ b/ethcore/src/state/mod.rs @@ -24,6 +24,7 @@ use std::collections::hash_map::Entry; use std::collections::{HashMap, BTreeMap, HashSet}; use std::fmt; use std::sync::Arc; +use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY}; use receipt::Receipt; use engines::Engine; @@ -552,7 +553,7 @@ impl State { /// Get an account's code hash. pub fn code_hash(&self, a: &Address) -> trie::Result { self.ensure_cached(a, RequireCache::None, true, - |a| a.as_ref().map_or(SHA3_EMPTY, |a| a.code_hash())) + |a| a.as_ref().map_or(KECCAK_EMPTY, |a| a.code_hash())) } /// Get accounts' code size. @@ -938,7 +939,7 @@ impl State { /// Returns a merkle proof of the account's trie node omitted or an encountered trie error. /// If the account doesn't exist in the trie, prove that and return defaults. /// Requires a secure trie to be used for accurate results. - /// `account_key` == sha3(address) + /// `account_key` == keccak(address) pub fn prove_account(&self, account_key: H256) -> trie::Result<(Vec, BasicAccount)> { let mut recorder = Recorder::new(); let trie = TrieDB::new(self.db.as_hashdb(), &self.root)?; @@ -949,8 +950,8 @@ impl State { let account = maybe_account.unwrap_or_else(|| BasicAccount { balance: 0.into(), nonce: self.account_start_nonce, - code_hash: SHA3_EMPTY, - storage_root: ::util::sha3::SHA3_NULL_RLP, + code_hash: KECCAK_EMPTY, + storage_root: KECCAK_NULL_RLP, }); Ok((recorder.drain().into_iter().map(|r| r.data).collect(), account)) @@ -959,11 +960,11 @@ impl State { /// Prove an account's storage key's existence or nonexistence in the state. /// Returns a merkle proof of the account's storage trie. /// Requires a secure trie to be used for correctness. - /// `account_key` == sha3(address) - /// `storage_key` == sha3(key) + /// `account_key` == keccak(address) + /// `storage_key` == keccak(key) pub fn prove_storage(&self, account_key: H256, storage_key: H256) -> trie::Result<(Vec, H256)> { // TODO: probably could look into cache somehow but it's keyed by - // address, not sha3(address). + // address, not keccak(address). let trie = TrieDB::new(self.db.as_hashdb(), &self.root)?; let acc = match trie.get_with(&account_key, Account::from_rlp)? { Some(acc) => acc, @@ -1008,13 +1009,13 @@ impl Clone for State { #[cfg(test)] mod tests { - use std::sync::Arc; use std::str::FromStr; use rustc_hex::FromHex; + use hash::keccak; use super::*; use ethkey::Secret; - use util::{U256, H256, Address, Hashable}; + use util::{U256, H256, Address}; use tests::helpers::*; use vm::EnvInfo; use spec::*; @@ -1024,7 +1025,7 @@ mod tests { use evm::CallType; fn secret() -> Secret { - "".sha3().into() + keccak("").into() } #[test] diff --git a/ethcore/src/state_db.rs b/ethcore/src/state_db.rs index e2f6fdaf0..ce96dc4fc 100644 --- a/ethcore/src/state_db.rs +++ b/ethcore/src/state_db.rs @@ -24,7 +24,8 @@ use util::hash::{H256}; use util::hashdb::HashDB; use state::{self, Account}; use header::BlockNumber; -use util::{Address, DBTransaction, UtilError, Mutex, Hashable}; +use hash::keccak; +use util::{Address, DBTransaction, UtilError, Mutex}; use bloom_journal::{Bloom, BloomJournal}; use db::COL_ACCOUNT_BLOOM; use byteorder::{LittleEndian, ByteOrder}; @@ -443,13 +444,13 @@ impl state::Backend for StateDB { fn note_non_null_account(&self, address: &Address) { trace!(target: "account_bloom", "Note account bloom: {:?}", address); let mut bloom = self.account_bloom.lock(); - bloom.set(&*address.sha3()); + bloom.set(&*keccak(address)); } fn is_known_null(&self, address: &Address) -> bool { trace!(target: "account_bloom", "Check account bloom: {:?}", address); let bloom = self.account_bloom.lock(); - let is_null = !bloom.check(&*address.sha3()); + let is_null = !bloom.check(&*keccak(address)); is_null } } diff --git a/ethcore/src/tests/client.rs b/ethcore/src/tests/client.rs index ea7dd32f5..6176e53bb 100644 --- a/ethcore/src/tests/client.rs +++ b/ethcore/src/tests/client.rs @@ -16,6 +16,7 @@ use std::str::FromStr; use std::sync::Arc; +use hash::keccak; use io::IoChannel; use client::{BlockChainClient, MiningBlockChainClient, Client, ClientConfig, BlockId}; use state::{self, State, CleanupMode}; @@ -254,7 +255,7 @@ fn can_mine() { let b = client.prepare_open_block(Address::default(), (3141562.into(), 31415620.into()), vec![]).close(); - assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().sha3()); + assert_eq!(*b.block().header().parent_hash(), BlockView::new(&dummy_blocks[0]).header_view().hash()); } #[test] @@ -298,7 +299,7 @@ fn change_history_size() { #[test] fn does_not_propagate_delayed_transactions() { - let key = KeyPair::from_secret("test".sha3().into()).unwrap(); + let key = KeyPair::from_secret(keccak("test").into()).unwrap(); let secret = key.secret(); let tx0 = PendingTransaction::new(Transaction { nonce: 0.into(), diff --git a/ethcore/src/tests/evm.rs b/ethcore/src/tests/evm.rs index 7b0e03d24..69351d587 100644 --- a/ethcore/src/tests/evm.rs +++ b/ethcore/src/tests/evm.rs @@ -1,6 +1,7 @@ //! Tests of EVM integration with transaction execution. use std::sync::Arc; +use hash::keccak; use vm::{EnvInfo, ActionParams, ActionValue, CallType}; use evm::{Factory, VMType}; use executive::Executive; @@ -16,11 +17,11 @@ use util::*; evm_test!{test_blockhash_eip210: test_blockhash_eip210_jit, test_blockhash_eip210_int} fn test_blockhash_eip210(factory: Factory) { let get_prev_hash_code = Arc::new("600143034060205260206020f3".from_hex().unwrap()); // this returns previous block hash - let get_prev_hash_code_hash = get_prev_hash_code.sha3(); + let get_prev_hash_code_hash = keccak(get_prev_hash_code.as_ref()); // This is same as DEFAULT_BLOCKHASH_CONTRACT except for metropolis transition block check removed. let test_blockhash_contract = "73fffffffffffffffffffffffffffffffffffffffe33141561007a57600143036020526000356101006020510755600061010060205107141561005057600035610100610100602051050761010001555b6000620100006020510714156100755760003561010062010000602051050761020001555b61014a565b4360003512151561009057600060405260206040f35b610100600035430312156100b357610100600035075460605260206060f3610149565b62010000600035430312156100d157600061010060003507146100d4565b60005b156100f6576101006101006000350507610100015460805260206080f3610148565b630100000060003543031215610116576000620100006000350714610119565b60005b1561013c57610100620100006000350507610200015460a052602060a0f3610147565b600060c052602060c0f35b5b5b5b5b"; let blockhash_contract_code = Arc::new(test_blockhash_contract.from_hex().unwrap()); - let blockhash_contract_code_hash = blockhash_contract_code.sha3(); + let blockhash_contract_code_hash = keccak(blockhash_contract_code.as_ref()); let engine = TestEngine::new_metropolis(); let mut env_info = EnvInfo::default(); diff --git a/ethcore/src/tests/helpers.rs b/ethcore/src/tests/helpers.rs index d8ba8313e..3d4a4b336 100644 --- a/ethcore/src/tests/helpers.rs +++ b/ethcore/src/tests/helpers.rs @@ -16,6 +16,7 @@ use std::collections::BTreeMap; use std::sync::Arc; +use hash::keccak; use ethkey::KeyPair; use io::*; use client::{BlockChainClient, Client, ClientConfig}; @@ -178,7 +179,7 @@ pub fn generate_dummy_client_with_spec_accounts_and_data(get_test_spec: F, ac let mut last_hashes = vec![]; let mut last_header = genesis_header.clone(); - let kp = KeyPair::from_secret_slice(&"".sha3()).unwrap(); + let kp = KeyPair::from_secret_slice(&keccak("")).unwrap(); let author = kp.address(); let mut n = 0; diff --git a/ethcore/src/trace/types/filter.rs b/ethcore/src/trace/types/filter.rs index 1b2e2077a..5c92dcedc 100644 --- a/ethcore/src/trace/types/filter.rs +++ b/ethcore/src/trace/types/filter.rs @@ -18,8 +18,8 @@ use std::ops::Range; use bloomchain::{Filter as BloomFilter, Bloom, Number}; +use hash::keccak; use util::Address; -use util::sha3::Hashable; use bloomable::Bloomable; use basic_types::LogBloom; use trace::flat::FlatTrace; @@ -55,7 +55,7 @@ impl AddressesFilter { match self.list.is_empty() { true => vec![LogBloom::default()], false => self.list.iter() - .map(|address| LogBloom::from_bloomed(&address.sha3())) + .map(|address| LogBloom::from_bloomed(&keccak(address))) .collect(), } } @@ -67,7 +67,7 @@ impl AddressesFilter { false => blooms .into_iter() .flat_map(|bloom| self.list.iter() - .map(|address| bloom.with_bloomed(&address.sha3())) + .map(|address| bloom.with_bloomed(&keccak(address))) .collect::>()) .collect(), } @@ -136,7 +136,7 @@ impl Filter { #[cfg(test)] mod tests { use util::Address; - use util::sha3::Hashable; + use hash::keccak; use bloomable::Bloomable; use trace::trace::{Action, Call, Res, Create, CreateResult, Suicide}; use trace::flat::FlatTrace; @@ -166,9 +166,9 @@ mod tests { let blooms = filter.bloom_possibilities(); assert_eq!(blooms.len(), 1); - assert!(blooms[0].contains_bloomed(&Address::from(1).sha3())); - assert!(blooms[0].contains_bloomed(&Address::from(2).sha3())); - assert!(!blooms[0].contains_bloomed(&Address::from(3).sha3())); + assert!(blooms[0].contains_bloomed(&keccak(Address::from(1)))); + assert!(blooms[0].contains_bloomed(&keccak(Address::from(2)))); + assert!(!blooms[0].contains_bloomed(&keccak(Address::from(3)))); } #[test] @@ -182,8 +182,8 @@ mod tests { let blooms = filter.bloom_possibilities(); assert_eq!(blooms.len(), 1); - assert!(blooms[0].contains_bloomed(&Address::from(1).sha3())); - assert!(!blooms[0].contains_bloomed(&Address::from(2).sha3())); + assert!(blooms[0].contains_bloomed(&keccak(Address::from(1)))); + assert!(!blooms[0].contains_bloomed(&keccak(Address::from(2)))); } #[test] @@ -197,8 +197,8 @@ mod tests { let blooms = filter.bloom_possibilities(); assert_eq!(blooms.len(), 1); - assert!(blooms[0].contains_bloomed(&Address::from(1).sha3())); - assert!(!blooms[0].contains_bloomed(&Address::from(2).sha3())); + assert!(blooms[0].contains_bloomed(&keccak(Address::from(1)))); + assert!(!blooms[0].contains_bloomed(&keccak(Address::from(2)))); } #[test] @@ -212,25 +212,25 @@ mod tests { let blooms = filter.bloom_possibilities(); assert_eq!(blooms.len(), 4); - assert!(blooms[0].contains_bloomed(&Address::from(1).sha3())); - assert!(blooms[0].contains_bloomed(&Address::from(2).sha3())); - assert!(!blooms[0].contains_bloomed(&Address::from(3).sha3())); - assert!(!blooms[0].contains_bloomed(&Address::from(4).sha3())); + assert!(blooms[0].contains_bloomed(&keccak(Address::from(1)))); + assert!(blooms[0].contains_bloomed(&keccak(Address::from(2)))); + assert!(!blooms[0].contains_bloomed(&keccak(Address::from(3)))); + assert!(!blooms[0].contains_bloomed(&keccak(Address::from(4)))); - assert!(blooms[1].contains_bloomed(&Address::from(1).sha3())); - assert!(blooms[1].contains_bloomed(&Address::from(4).sha3())); - assert!(!blooms[1].contains_bloomed(&Address::from(2).sha3())); - assert!(!blooms[1].contains_bloomed(&Address::from(3).sha3())); + assert!(blooms[1].contains_bloomed(&keccak(Address::from(1)))); + assert!(blooms[1].contains_bloomed(&keccak(Address::from(4)))); + assert!(!blooms[1].contains_bloomed(&keccak(Address::from(2)))); + assert!(!blooms[1].contains_bloomed(&keccak(Address::from(3)))); - assert!(blooms[2].contains_bloomed(&Address::from(2).sha3())); - assert!(blooms[2].contains_bloomed(&Address::from(3).sha3())); - assert!(!blooms[2].contains_bloomed(&Address::from(1).sha3())); - assert!(!blooms[2].contains_bloomed(&Address::from(4).sha3())); + assert!(blooms[2].contains_bloomed(&keccak(Address::from(2)))); + assert!(blooms[2].contains_bloomed(&keccak(Address::from(3)))); + assert!(!blooms[2].contains_bloomed(&keccak(Address::from(1)))); + assert!(!blooms[2].contains_bloomed(&keccak(Address::from(4)))); - assert!(blooms[3].contains_bloomed(&Address::from(3).sha3())); - assert!(blooms[3].contains_bloomed(&Address::from(4).sha3())); - assert!(!blooms[3].contains_bloomed(&Address::from(1).sha3())); - assert!(!blooms[3].contains_bloomed(&Address::from(2).sha3())); + assert!(blooms[3].contains_bloomed(&keccak(Address::from(3)))); + assert!(blooms[3].contains_bloomed(&keccak(Address::from(4)))); + assert!(!blooms[3].contains_bloomed(&keccak(Address::from(1)))); + assert!(!blooms[3].contains_bloomed(&keccak(Address::from(2)))); } #[test] diff --git a/ethcore/src/trace/types/trace.rs b/ethcore/src/trace/types/trace.rs index 5fa0260c6..af0fa5f6e 100644 --- a/ethcore/src/trace/types/trace.rs +++ b/ethcore/src/trace/types/trace.rs @@ -17,7 +17,7 @@ //! Tracing datatypes. use util::{U256, Bytes, Address}; -use util::sha3::Hashable; +use hash::keccak; use bloomable::Bloomable; use rlp::*; @@ -51,7 +51,7 @@ pub struct CreateResult { impl CreateResult { /// Returns bloom. pub fn bloom(&self) -> LogBloom { - LogBloom::from_bloomed(&self.address.sha3()) + LogBloom::from_bloomed(&keccak(&self.address)) } } @@ -90,8 +90,8 @@ impl Call { /// Returns call action bloom. /// The bloom contains from and to addresses. pub fn bloom(&self) -> LogBloom { - LogBloom::from_bloomed(&self.from.sha3()) - .with_bloomed(&self.to.sha3()) + LogBloom::from_bloomed(&keccak(&self.from)) + .with_bloomed(&keccak(&self.to)) } } @@ -124,7 +124,7 @@ impl Create { /// Returns bloom create action bloom. /// The bloom contains only from address. pub fn bloom(&self) -> LogBloom { - LogBloom::from_bloomed(&self.from.sha3()) + LogBloom::from_bloomed(&keccak(&self.from)) } } @@ -143,8 +143,8 @@ pub struct Suicide { impl Suicide { /// Return suicide action bloom. pub fn bloom(&self) -> LogBloom { - LogBloom::from_bloomed(&self.address.sha3()) - .with_bloomed(&self.refund_address.sha3()) + LogBloom::from_bloomed(&keccak(self.address)) + .with_bloomed(&keccak(self.refund_address)) } } diff --git a/ethcore/src/transaction.rs b/ethcore/src/transaction.rs index 0bd61dceb..e68021768 100644 --- a/ethcore/src/transaction.rs +++ b/ethcore/src/transaction.rs @@ -18,7 +18,7 @@ use std::ops::Deref; use rlp::*; -use util::sha3::Hashable; +use hash::keccak; use util::{H256, Address, U256, Bytes, HeapSizeOf}; use ethkey::{Signature, Secret, Public, recover, public_to_address, Error as EthkeyError}; use error::*; @@ -166,7 +166,7 @@ impl Transaction { pub fn hash(&self, chain_id: Option) -> H256 { let mut stream = RlpStream::new(); self.rlp_append_unsigned_transaction(&mut stream, chain_id); - stream.as_raw().sha3() + keccak(stream.as_raw()) } /// Signs the transaction as coming from `sender`. @@ -273,7 +273,7 @@ impl Decodable for UnverifiedTransaction { if d.item_count()? != 9 { return Err(DecoderError::RlpIncorrectListLen); } - let hash = d.as_raw().sha3(); + let hash = keccak(d.as_raw()); Ok(UnverifiedTransaction { unsigned: Transaction { nonce: d.val_at(0)?, @@ -298,7 +298,7 @@ impl Encodable for UnverifiedTransaction { impl UnverifiedTransaction { /// Used to compute hash of created transactions fn compute_hash(mut self) -> UnverifiedTransaction { - let hash = (&*self.rlp_bytes()).sha3(); + let hash = keccak(&*self.rlp_bytes()); self.hash = hash; self } @@ -356,7 +356,7 @@ impl UnverifiedTransaction { } } - /// Get the hash of this header (sha3 of the RLP). + /// Get the hash of this header (keccak of the RLP). pub fn hash(&self) -> H256 { self.hash } @@ -544,7 +544,8 @@ impl From for PendingTransaction { #[cfg(test)] mod tests { use super::*; - use util::{Hashable, U256}; + use util::{U256}; + use hash::keccak; #[test] fn sender_test() { @@ -574,7 +575,7 @@ mod tests { value: U256::from(1), data: b"Hello!".to_vec() }.sign(&key.secret(), None); - assert_eq!(Address::from(key.public().sha3()), t.sender()); + assert_eq!(Address::from(keccak(key.public())), t.sender()); assert_eq!(t.chain_id(), None); } @@ -608,7 +609,7 @@ mod tests { value: U256::from(1), data: b"Hello!".to_vec() }.sign(&key.secret(), Some(69)); - assert_eq!(Address::from(key.public().sha3()), t.sender()); + assert_eq!(Address::from(keccak(key.public())), t.sender()); assert_eq!(t.chain_id(), Some(69)); } diff --git a/ethcore/src/verification/verification.rs b/ethcore/src/verification/verification.rs index 00976dca7..11fd01d7b 100644 --- a/ethcore/src/verification/verification.rs +++ b/ethcore/src/verification/verification.rs @@ -22,6 +22,7 @@ //! 3. Final verification against the blockchain done before enactment. use std::collections::HashSet; +use hash::keccak; use util::*; use engines::Engine; use error::{BlockError, Error}; @@ -256,7 +257,7 @@ fn verify_block_integrity(block: &[u8], transactions_root: &H256, uncles_hash: & if expected_root != transactions_root { return Err(From::from(BlockError::InvalidTransactionsRoot(Mismatch { expected: expected_root.clone(), found: transactions_root.clone() }))) } - let expected_uncles = &block.at(2)?.as_raw().sha3(); + let expected_uncles = &keccak(block.at(2)?.as_raw()); if expected_uncles != uncles_hash { return Err(From::from(BlockError::InvalidUnclesHash(Mismatch { expected: expected_uncles.clone(), found: uncles_hash.clone() }))) } @@ -266,6 +267,7 @@ fn verify_block_integrity(block: &[u8], transactions_root: &H256, uncles_hash: & #[cfg(test)] mod tests { use std::collections::{BTreeMap, HashMap}; + use hash::keccak; use util::*; use ethkey::{Random, Generator}; use header::*; @@ -324,7 +326,7 @@ mod tests { pub fn insert(&mut self, bytes: Bytes) { let number = BlockView::new(&bytes).header_view().number(); - let hash = BlockView::new(&bytes).header_view().sha3(); + let hash = BlockView::new(&bytes).header_view().hash(); self.blocks.insert(hash.clone(), bytes); self.numbers.insert(number, hash.clone()); } @@ -482,7 +484,7 @@ mod tests { let good_uncles = vec![ good_uncle1.clone(), good_uncle2.clone() ]; let mut uncles_rlp = RlpStream::new(); uncles_rlp.append_list(&good_uncles); - let good_uncles_hash = uncles_rlp.as_raw().sha3(); + let good_uncles_hash = keccak(uncles_rlp.as_raw()); let good_transactions_root = ordered_trie_root(good_transactions.iter().map(|t| ::rlp::encode::(t).into_vec())); let mut parent = good.clone(); diff --git a/ethcore/src/views/block.rs b/ethcore/src/views/block.rs index f00ca548f..723a9f1b8 100644 --- a/ethcore/src/views/block.rs +++ b/ethcore/src/views/block.rs @@ -16,6 +16,7 @@ //! View onto block rlp. +use hash::keccak; use util::*; use header::*; use transaction::*; @@ -44,7 +45,7 @@ impl<'a> BlockView<'a> { /// Block header hash. pub fn hash(&self) -> H256 { - self.sha3() + self.header_view().hash() } /// Return reference to underlaying rlp. @@ -75,7 +76,7 @@ impl<'a> BlockView<'a> { /// Return List of transactions with additional localization info. pub fn localized_transactions(&self) -> Vec { let header = self.header_view(); - let block_hash = header.sha3(); + let block_hash = header.hash(); let block_number = header.number(); self.transactions() .into_iter() @@ -101,7 +102,7 @@ impl<'a> BlockView<'a> { /// Return transaction hashes. pub fn transaction_hashes(&self) -> Vec { - self.rlp.at(1).iter().map(|rlp| rlp.as_raw().sha3()).collect() + self.rlp.at(1).iter().map(|rlp| keccak(rlp.as_raw())).collect() } /// Returns transaction at given index without deserializing unnecessary data. @@ -112,7 +113,7 @@ impl<'a> BlockView<'a> { /// Returns localized transaction at given index. pub fn localized_transaction_at(&self, index: usize) -> Option { let header = self.header_view(); - let block_hash = header.sha3(); + let block_hash = header.hash(); let block_number = header.number(); self.transaction_at(index).map(|t| LocalizedTransaction { signed: t, @@ -140,7 +141,7 @@ impl<'a> BlockView<'a> { /// Return list of uncle hashes of given block. pub fn uncle_hashes(&self) -> Vec { - self.rlp.at(2).iter().map(|rlp| rlp.as_raw().sha3()).collect() + self.rlp.at(2).iter().map(|rlp| keccak(rlp.as_raw())).collect() } /// Return nth uncle. @@ -154,12 +155,6 @@ impl<'a> BlockView<'a> { } } -impl<'a> Hashable for BlockView<'a> { - fn sha3(&self) -> H256 { - self.header_view().sha3() - } -} - #[cfg(test)] mod tests { use std::str::FromStr; diff --git a/ethcore/src/views/body.rs b/ethcore/src/views/body.rs index 038e152b0..d7509af82 100644 --- a/ethcore/src/views/body.rs +++ b/ethcore/src/views/body.rs @@ -16,6 +16,7 @@ //! View onto block body rlp. +use hash::keccak; use util::*; use header::*; use transaction::*; @@ -78,7 +79,7 @@ impl<'a> BodyView<'a> { /// Return transaction hashes. pub fn transaction_hashes(&self) -> Vec { - self.rlp.at(0).iter().map(|rlp| rlp.as_raw().sha3()).collect() + self.rlp.at(0).iter().map(|rlp| keccak(rlp.as_raw())).collect() } /// Returns transaction at given index without deserializing unnecessary data. @@ -114,7 +115,7 @@ impl<'a> BodyView<'a> { /// Return list of uncle hashes of given block. pub fn uncle_hashes(&self) -> Vec { - self.rlp.at(1).iter().map(|rlp| rlp.as_raw().sha3()).collect() + self.rlp.at(1).iter().map(|rlp| keccak(rlp.as_raw())).collect() } /// Return nth uncle. diff --git a/ethcore/src/views/header.rs b/ethcore/src/views/header.rs index 4506d98ec..977aaa9a2 100644 --- a/ethcore/src/views/header.rs +++ b/ethcore/src/views/header.rs @@ -16,7 +16,8 @@ //! View onto block header rlp -use util::{U256, Bytes, Hashable, H256, Address, H2048}; +use hash::keccak; +use util::{U256, Bytes, H256, Address, H2048}; use rlp::Rlp; use header::BlockNumber; @@ -41,7 +42,9 @@ impl<'a> HeaderView<'a> { } /// Returns header hash. - pub fn hash(&self) -> H256 { self.sha3() } + pub fn hash(&self) -> H256 { + keccak(self.rlp.as_raw()) + } /// Returns raw rlp. pub fn rlp(&self) -> &Rlp<'a> { &self.rlp } @@ -95,12 +98,6 @@ impl<'a> HeaderView<'a> { } } -impl<'a> Hashable for HeaderView<'a> { - fn sha3(&self) -> H256 { - self.rlp.as_raw().sha3() - } -} - #[cfg(test)] mod tests { use std::str::FromStr; diff --git a/ethcore/src/views/transaction.rs b/ethcore/src/views/transaction.rs index 11c7fc2f1..e19dfd952 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::{U256, Bytes, Hashable, H256}; +use util::{U256, Bytes, H256}; +use hash::keccak; use rlp::Rlp; /// View onto transaction rlp. @@ -43,6 +44,11 @@ impl<'a> TransactionView<'a> { &self.rlp } + /// Returns transaction hash. + pub fn hash(&self) -> H256 { + keccak(self.rlp.as_raw()) + } + /// Get the nonce field of the transaction. pub fn nonce(&self) -> U256 { self.rlp.val_at(0) } @@ -68,12 +74,6 @@ impl<'a> TransactionView<'a> { pub fn s(&self) -> U256 { self.rlp.val_at(8) } } -impl<'a> Hashable for TransactionView<'a> { - fn sha3(&self) -> H256 { - self.rlp.as_raw().sha3() - } -} - #[cfg(test)] mod tests { use std::str::FromStr; diff --git a/ethcore/types/Cargo.toml b/ethcore/types/Cargo.toml index 82963f960..bb43d0d49 100644 --- a/ethcore/types/Cargo.toml +++ b/ethcore/types/Cargo.toml @@ -10,6 +10,7 @@ rlp_derive = { path = "../../util/rlp_derive" } ethcore-util = { path = "../../util" } ethjson = { path = "../../json" } bloomable = { path = "../../util/bloomable" } +hash = { path = "../../util/hash" } [dev-dependencies] rustc-hex= "1.0" diff --git a/ethcore/types/src/filter.rs b/ethcore/types/src/filter.rs index 6e344b4ef..92fc31edd 100644 --- a/ethcore/types/src/filter.rs +++ b/ethcore/types/src/filter.rs @@ -16,10 +16,11 @@ //! Blockchain filter -use util::{Address, H256, Hashable, H2048}; +use util::{Address, H256, H2048}; use bloomable::Bloomable; use ids::BlockId; use log_entry::LogEntry; +use hash::keccak; /// Blockchain Filter. #[derive(Debug, PartialEq)] @@ -78,7 +79,7 @@ impl Filter { Some(ref addresses) if !addresses.is_empty() => addresses.iter().map(|ref address| { let mut bloom = H2048::default(); - bloom.shift_bloomed(&address.sha3()); + bloom.shift_bloomed(&keccak(address)); bloom }).collect(), _ => vec![H2048::default()] @@ -89,7 +90,7 @@ impl Filter { Some(ref topics) => bs.into_iter().flat_map(|bloom| { topics.into_iter().map(|topic| { let mut b = bloom.clone(); - b.shift_bloomed(&topic.sha3()); + b.shift_bloomed(&keccak(topic)); b }).collect::>() }).collect() diff --git a/ethcore/types/src/lib.rs b/ethcore/types/src/lib.rs index 10a4ac71e..4050bf737 100644 --- a/ethcore/types/src/lib.rs +++ b/ethcore/types/src/lib.rs @@ -22,6 +22,7 @@ extern crate rlp; #[macro_use] extern crate rlp_derive; extern crate bloomable; +extern crate hash; #[cfg(test)] extern crate rustc_hex; diff --git a/ethcore/types/src/log_entry.rs b/ethcore/types/src/log_entry.rs index 152b48a0e..40b0f3ea9 100644 --- a/ethcore/types/src/log_entry.rs +++ b/ethcore/types/src/log_entry.rs @@ -17,7 +17,8 @@ //! Log entry type definition. use std::ops::Deref; -use util::{H256, Address, Bytes, HeapSizeOf, Hashable}; +use hash::keccak; +use util::{H256, Address, Bytes, HeapSizeOf}; use bloomable::Bloomable; use {BlockNumber}; @@ -45,7 +46,7 @@ impl HeapSizeOf for LogEntry { impl LogEntry { /// Calculates the bloom of this log entry. pub fn bloom(&self) -> LogBloom { - self.topics.iter().fold(LogBloom::from_bloomed(&self.address.sha3()), |b, t| b.with_bloomed(&t.sha3())) + self.topics.iter().fold(LogBloom::from_bloomed(&keccak(&self.address)), |b, t| b.with_bloomed(&keccak(t))) } } diff --git a/ethcore/vm/Cargo.toml b/ethcore/vm/Cargo.toml index 50efe936c..3f308d8f5 100644 --- a/ethcore/vm/Cargo.toml +++ b/ethcore/vm/Cargo.toml @@ -11,4 +11,5 @@ common-types = { path = "../types" } evmjit = { path = "../../evmjit", optional = true } ethjson = { path = "../../json" } lazy_static = "0.2" -rlp = { path = "../../util/rlp" } \ No newline at end of file +rlp = { path = "../../util/rlp" } +hash = { path = "../../util/hash" } diff --git a/ethcore/vm/src/action_params.rs b/ethcore/vm/src/action_params.rs index 401d7ee57..ab32e8d9f 100644 --- a/ethcore/vm/src/action_params.rs +++ b/ethcore/vm/src/action_params.rs @@ -17,7 +17,7 @@ //! Evm input params. use util::{Address, Bytes, U256}; use util::hash::{H256}; -use util::sha3::{Hashable, SHA3_EMPTY}; +use hash::{keccak, KECCAK_EMPTY}; use ethjson; use call_type::CallType; @@ -87,7 +87,7 @@ impl Default for ActionParams { fn default() -> ActionParams { ActionParams { code_address: Address::new(), - code_hash: Some(SHA3_EMPTY), + code_hash: Some(KECCAK_EMPTY), address: Address::new(), sender: Address::new(), origin: Address::new(), @@ -106,7 +106,7 @@ impl From for ActionParams { let address: Address = t.address.into(); ActionParams { code_address: Address::new(), - code_hash: Some((&*t.code).sha3()), + code_hash: Some(keccak(&*t.code)), address: address, sender: t.sender.into(), origin: t.origin.into(), diff --git a/ethcore/vm/src/env_info.rs b/ethcore/vm/src/env_info.rs index 8634d9c75..12f035183 100644 --- a/ethcore/vm/src/env_info.rs +++ b/ethcore/vm/src/env_info.rs @@ -18,7 +18,8 @@ use std::cmp; use std::sync::Arc; -use util::{U256, Address, H256, Hashable}; +use hash::keccak; +use util::{U256, Address, H256}; use types::BlockNumber; use ethjson; @@ -68,7 +69,7 @@ impl From for EnvInfo { difficulty: e.difficulty.into(), gas_limit: e.gas_limit.into(), timestamp: e.timestamp.into(), - last_hashes: Arc::new((1..cmp::min(number + 1, 257)).map(|i| format!("{}", number - i).as_bytes().sha3()).collect()), + last_hashes: Arc::new((1..cmp::min(number + 1, 257)).map(|i| keccak(format!("{}", number - i).as_bytes())).collect()), gas_used: U256::default(), } } diff --git a/ethcore/vm/src/lib.rs b/ethcore/vm/src/lib.rs index 0c9e32dc6..010e88eba 100644 --- a/ethcore/vm/src/lib.rs +++ b/ethcore/vm/src/lib.rs @@ -20,6 +20,7 @@ extern crate ethcore_util as util; extern crate common_types as types; extern crate ethjson; extern crate rlp; +extern crate hash; mod action_params; mod call_type; @@ -45,4 +46,4 @@ pub trait Vm { /// It returns either an error, a known amount of gas left, or parameters to be used /// to compute the final gas left. fn exec(&mut self, params: ActionParams, ext: &mut Ext) -> Result; -} \ No newline at end of file +} diff --git a/stratum/Cargo.toml b/stratum/Cargo.toml index dcd605bb8..9bf75779f 100644 --- a/stratum/Cargo.toml +++ b/stratum/Cargo.toml @@ -24,3 +24,4 @@ ethcore-ipc-nano = { path = "../ipc/nano" } futures = "0.1" tokio-core = "0.1" ethcore-logger = { path = "../logger" } +hash = { path = "../util/hash" } diff --git a/stratum/src/lib.rs b/stratum/src/lib.rs index 0042ab1e9..0cffd67b6 100644 --- a/stratum/src/lib.rs +++ b/stratum/src/lib.rs @@ -25,6 +25,7 @@ extern crate ethcore_ipc as ipc; extern crate semver; extern crate futures; extern crate ethcore_logger; +extern crate hash; #[cfg(test)] extern crate tokio_core; extern crate ethcore_devtools as devtools; @@ -54,7 +55,8 @@ use std::sync::Arc; use std::net::SocketAddr; use std::collections::{HashSet, HashMap}; -use util::{H256, Hashable, RwLock, RwLockReadGuard}; +use hash::keccak; +use util::{H256, RwLock, RwLockReadGuard}; type RpcResult = BoxFuture; @@ -228,7 +230,7 @@ impl Stratum { fn authorize(&self, params: Params, meta: SocketMetadata) -> RpcResult { future::result(params.parse::<(String, String)>().map(|(worker_id, secret)|{ if let Some(valid_secret) = self.secret { - let hash = secret.sha3(); + let hash = keccak(secret); if hash != valid_secret { return to_value(&false); } diff --git a/util/benches/trie.rs b/util/benches/trie.rs index e02077ad4..2ae10cf39 100644 --- a/util/benches/trie.rs +++ b/util/benches/trie.rs @@ -20,6 +20,7 @@ extern crate test; extern crate ethcore_util; #[macro_use] extern crate log; +extern crate hash; use test::{Bencher, black_box}; use ethcore_util::hash::*; @@ -27,12 +28,11 @@ use ethcore_util::bytes::*; use ethcore_util::trie::*; use ethcore_util::memorydb::*; use ethcore_util::triehash::*; -use ethcore_util::sha3::*; - +use hash::keccak; fn random_word(alphabet: &[u8], min_count: usize, diff_count: usize, seed: &mut H256) -> Vec { assert!(min_count + diff_count <= 32); - *seed = seed.sha3(); + *seed = keccak(&seed); let r = min_count + (seed[31] as usize % (diff_count + 1)); let mut ret: Vec = Vec::with_capacity(r); for i in 0..r { @@ -43,13 +43,13 @@ fn random_word(alphabet: &[u8], min_count: usize, diff_count: usize, seed: &mut fn random_bytes(min_count: usize, diff_count: usize, seed: &mut H256) -> Vec { assert!(min_count + diff_count <= 32); - *seed = seed.sha3(); + *seed = keccak(&seed); let r = min_count + (seed[31] as usize % (diff_count + 1)); seed[0..r].to_vec() } fn random_value(seed: &mut H256) -> Bytes { - *seed = seed.sha3(); + *seed = keccak(&seed); match seed[0] % 2 { 1 => vec![seed[31];1], _ => seed.to_vec(), @@ -306,11 +306,11 @@ fn triehash_insertions_six_low(b: &mut Bencher) { } #[bench] -fn sha3x10000(b: &mut Bencher) { +fn keccakx10000(b: &mut Bencher) { b.iter(||{ let mut seed = H256::new(); for _ in 0..10000 { - seed = seed.sha3() + seed = keccak(&seed); } }) } From 7849fff41e92c7596bb5fc5149edca30f0cd9a99 Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 31 Aug 2017 11:35:41 +0200 Subject: [PATCH 05/16] finished --- Cargo.lock | 8 +++++++ Cargo.toml | 1 + dapps/Cargo.toml | 1 + dapps/src/apps/fetcher/installers.rs | 6 ++--- dapps/src/lib.rs | 1 + ethcore/light/Cargo.toml | 1 + ethcore/light/src/client/header_chain.rs | 2 +- ethcore/light/src/lib.rs | 1 + ethcore/light/src/on_demand/request.rs | 25 +++++++++++---------- hash-fetch/Cargo.toml | 1 + hash-fetch/src/client.rs | 5 +++-- hash-fetch/src/lib.rs | 1 + hash-fetch/src/urlhint.rs | 5 +++-- parity/blockchain.rs | 9 ++++---- parity/configuration.rs | 7 +++--- parity/main.rs | 5 +++-- parity/snapshot.rs | 7 +++--- rpc/Cargo.toml | 1 + rpc/src/authcodes.rs | 10 +++++---- rpc/src/lib.rs | 1 + rpc/src/tests/ws.rs | 8 +++---- rpc/src/v1/helpers/dispatch.rs | 4 ++-- rpc/src/v1/impls/eth.rs | 3 +-- rpc/src/v1/impls/light/eth.rs | 10 ++++----- rpc/src/v1/impls/light/parity_set.rs | 4 ++-- rpc/src/v1/impls/parity_set.rs | 4 ++-- rpc/src/v1/impls/web3.rs | 4 ++-- rpc/src/v1/tests/eth.rs | 4 ++-- rpc_client/Cargo.toml | 1 + rpc_client/src/client.rs | 5 +++-- rpc_client/src/lib.rs | 1 + secret_store/Cargo.toml | 1 + secret_store/src/key_server_cluster/math.rs | 5 +++-- secret_store/src/key_server_set.rs | 7 +++--- secret_store/src/lib.rs | 1 + sync/Cargo.toml | 1 + sync/src/block_sync.rs | 2 +- sync/src/blocks.rs | 14 ++++++------ sync/src/chain.rs | 14 ++++++------ sync/src/lib.rs | 1 + sync/src/snapshot.rs | 15 +++++++------ sync/src/tests/consensus.rs | 9 ++++---- sync/src/tests/snapshot.rs | 9 ++++---- 43 files changed, 130 insertions(+), 95 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 439ba8936..38569b271 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -654,6 +654,7 @@ dependencies = [ "ethcore-util 1.8.0", "evm 0.1.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -730,6 +731,7 @@ dependencies = [ "ethkey 0.2.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "hyper 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -902,6 +904,7 @@ dependencies = [ "ethcore-network 1.8.0", "ethcore-util 1.8.0", "ethkey 0.2.0", + "hash 0.1.0", "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipnetwork 0.12.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1862,6 +1865,7 @@ dependencies = [ "fdlimit 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "ipnetwork 0.12.6 (registry+https://github.com/rust-lang/crates.io-index)", "isatty 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", @@ -1909,6 +1913,7 @@ dependencies = [ "ethcore-util 1.8.0", "fetch 0.1.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-http-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", @@ -1954,6 +1959,7 @@ dependencies = [ "ethcore-util 1.8.0", "fetch 0.1.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2021,6 +2027,7 @@ dependencies = [ "fetch 0.1.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-http-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", @@ -2057,6 +2064,7 @@ version = "1.4.0" dependencies = [ "ethcore-util 1.8.0", "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "hash 0.1.0", "jsonrpc-core 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "jsonrpc-ws-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 06abd7b31..9f6dd9ccb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,7 @@ parity-updater = { path = "updater" } parity-whisper = { path = "whisper" } path = { path = "util/path" } panic_hook = { path = "panic_hook" } +hash = { path = "util/hash" } parity-dapps = { path = "dapps", optional = true } clippy = { version = "0.0.103", optional = true} diff --git a/dapps/Cargo.toml b/dapps/Cargo.toml index b1751c616..76b026c9a 100644 --- a/dapps/Cargo.toml +++ b/dapps/Cargo.toml @@ -35,6 +35,7 @@ node-health = { path = "./node-health" } parity-hash-fetch = { path = "../hash-fetch" } parity-reactor = { path = "../util/reactor" } parity-ui = { path = "./ui" } +hash = { path = "../util/hash" } clippy = { version = "0.0.103", optional = true} diff --git a/dapps/src/apps/fetcher/installers.rs b/dapps/src/apps/fetcher/installers.rs index 82c91c859..9c220b057 100644 --- a/dapps/src/apps/fetcher/installers.rs +++ b/dapps/src/apps/fetcher/installers.rs @@ -19,9 +19,9 @@ use std::{fs, fmt}; use std::io::{self, Read, Write}; use std::path::PathBuf; use fetch::{self, Mime}; +use hash::keccak_buffer; use util::H256; -use util::sha3::sha3; use page::{LocalPageEndpoint, PageCache}; use handlers::{ContentValidator, ValidatorResponse}; use apps::manifest::{MANIFEST_FILENAME, deserialize_manifest, serialize_manifest, Manifest}; @@ -57,9 +57,9 @@ fn write_response_and_check_hash( file.flush()?; // Validate hash - // TODO [ToDr] calculate sha3 in-flight while reading the response + // TODO [ToDr] calculate keccak in-flight while reading the response let mut file = io::BufReader::new(fs::File::open(&content_path)?); - let hash = sha3(&mut file)?; + let hash = keccak_buffer(&mut file)?; if id == hash { Ok((file.into_inner(), content_path)) } else { diff --git a/dapps/src/lib.rs b/dapps/src/lib.rs index 073db5121..f5670c959 100644 --- a/dapps/src/lib.rs +++ b/dapps/src/lib.rs @@ -43,6 +43,7 @@ extern crate parity_dapps_glue as parity_dapps; extern crate parity_hash_fetch as hash_fetch; extern crate parity_reactor; extern crate parity_ui; +extern crate hash; #[macro_use] extern crate log; diff --git a/ethcore/light/Cargo.toml b/ethcore/light/Cargo.toml index 9fda78f47..f0c8590e3 100644 --- a/ethcore/light/Cargo.toml +++ b/ethcore/light/Cargo.toml @@ -31,6 +31,7 @@ bincode = "0.8.0" serde = "1.0" serde_derive = "1.0" stats = { path = "../../util/stats" } +hash = { path = "../../util/hash" } [features] default = [] diff --git a/ethcore/light/src/client/header_chain.rs b/ethcore/light/src/client/header_chain.rs index d6ac109c7..e99e89c37 100644 --- a/ethcore/light/src/client/header_chain.rs +++ b/ethcore/light/src/client/header_chain.rs @@ -486,7 +486,7 @@ impl HeaderChain { /// Get the genesis hash. pub fn genesis_hash(&self) -> H256 { - ::util::Hashable::sha3(&self.genesis_header) + self.genesis_header.hash() } /// Get the best block's data. diff --git a/ethcore/light/src/lib.rs b/ethcore/light/src/lib.rs index c2ab483d2..0487d5d23 100644 --- a/ethcore/light/src/lib.rs +++ b/ethcore/light/src/lib.rs @@ -83,6 +83,7 @@ extern crate smallvec; extern crate stats; extern crate time; extern crate vm; +extern crate hash; #[cfg(feature = "ipc")] extern crate ethcore_ipc as ipc; diff --git a/ethcore/light/src/on_demand/request.rs b/ethcore/light/src/on_demand/request.rs index 6a2201349..8ee41437b 100644 --- a/ethcore/light/src/on_demand/request.rs +++ b/ethcore/light/src/on_demand/request.rs @@ -25,13 +25,13 @@ use ethcore::receipt::Receipt; use ethcore::state::{self, ProvedExecution}; use ethcore::transaction::SignedTransaction; use vm::EnvInfo; +use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY, KECCAK_EMPTY_LIST_RLP, keccak}; use request::{self as net_request, IncompleteRequest, CompleteRequest, Output, OutputKind, Field}; use rlp::{RlpStream, UntrustedRlp}; use util::{Address, Bytes, DBValue, HashDB, Mutex, H256, U256}; use util::memorydb::MemoryDB; -use util::sha3::{Hashable, SHA3_NULL_RLP, SHA3_EMPTY, SHA3_EMPTY_LIST_RLP}; use util::trie::{Trie, TrieDB, TrieError}; const SUPPLIED_MATCHES: &'static str = "supplied responses always match produced requests; enforced by `check_response`; qed"; @@ -276,7 +276,7 @@ impl From for CheckedRequest { Request::Account(req) => { let net_req = net_request::IncompleteAccountRequest { block_hash: req.header.field(), - address_hash: ::util::Hashable::sha3(&req.address).into(), + address_hash: ::hash::keccak(&req.address).into(), }; CheckedRequest::Account(req, net_req) } @@ -366,7 +366,7 @@ impl CheckedRequest { } CheckedRequest::Receipts(ref check, ref req) => { // empty transactions -> no receipts - if check.0.as_ref().ok().map_or(false, |hdr| hdr.receipts_root() == SHA3_NULL_RLP) { + if check.0.as_ref().ok().map_or(false, |hdr| hdr.receipts_root() == KECCAK_NULL_RLP) { return Some(Response::Receipts(Vec::new())); } @@ -377,7 +377,7 @@ impl CheckedRequest { CheckedRequest::Body(ref check, ref req) => { // check for empty body. if let Some(hdr) = check.0.as_ref().ok() { - if hdr.transactions_root() == SHA3_NULL_RLP && hdr.uncles_hash() == SHA3_EMPTY_LIST_RLP { + if hdr.transactions_root() == KECCAK_NULL_RLP && hdr.uncles_hash() == KECCAK_EMPTY_LIST_RLP { let mut stream = RlpStream::new_list(3); stream.append_raw(hdr.rlp().as_raw(), 1); stream.begin_list(0); @@ -422,7 +422,7 @@ impl CheckedRequest { }) } CheckedRequest::Code(_, ref req) => { - if req.code_hash.as_ref().map_or(false, |&h| h == SHA3_EMPTY) { + if req.code_hash.as_ref().map_or(false, |&h| h == KECCAK_EMPTY) { Some(Response::Code(Vec::new())) } else { None @@ -571,8 +571,8 @@ impl net_request::ResponseLike for Response { match *self { Response::HeaderProof((ref hash, _)) => f(0, Output::Hash(*hash)), Response::Account(None) => { - f(0, Output::Hash(SHA3_EMPTY)); // code hash - f(1, Output::Hash(SHA3_NULL_RLP)); // storage root. + f(0, Output::Hash(KECCAK_EMPTY)); // code hash + f(1, Output::Hash(KECCAK_NULL_RLP)); // storage root. } Response::Account(Some(ref acc)) => { f(0, Output::Hash(acc.code_hash)); @@ -688,7 +688,7 @@ impl HeaderByHash { }; let header = headers.get(0).ok_or(Error::Empty)?; - let hash = header.sha3(); + let hash = header.hash(); match hash == expected_hash { true => { cache.lock().insert_block_header(hash, header.clone()); @@ -713,7 +713,7 @@ impl Body { return Err(Error::WrongTrieRoot(header.transactions_root(), tx_root)); } - let uncles_hash = body.rlp().at(1).as_raw().sha3(); + let uncles_hash = keccak(body.rlp().at(1).as_raw()); if uncles_hash != header.uncles_hash() { return Err(Error::WrongHash(header.uncles_hash(), uncles_hash)); } @@ -768,7 +768,7 @@ impl Account { let mut db = MemoryDB::new(); for node in proof { db.insert(&node[..]); } - match TrieDB::new(&db, &state_root).and_then(|t| t.get(&self.address.sha3()))? { + match TrieDB::new(&db, &state_root).and_then(|t| t.get(&keccak(&self.address)))? { Some(val) => { let rlp = UntrustedRlp::new(&val); Ok(Some(BasicAccount { @@ -800,7 +800,7 @@ impl Code { code_hash: &H256, code: &[u8] ) -> Result, Error> { - let found_hash = code.sha3(); + let found_hash = keccak(code); if &found_hash == code_hash { Ok(code.to_vec()) } else { @@ -853,6 +853,7 @@ mod tests { use util::{MemoryDB, Address, Mutex, H256}; use util::trie::{Trie, TrieMut, SecTrieDB, SecTrieDBMut}; use util::trie::recorder::Recorder; + use hash::keccak; use ethcore::client::{BlockChainClient, TestBlockChainClient, EachBlockWith}; use ethcore::header::Header; @@ -998,7 +999,7 @@ mod tests { #[test] fn check_code() { let code = vec![1u8; 256]; - let code_hash = ::util::Hashable::sha3(&code); + let code_hash = keccak(&code); let header = Header::new(); let req = Code { header: encoded::Header::new(::rlp::encode(&header).into_vec()).into(), diff --git a/hash-fetch/Cargo.toml b/hash-fetch/Cargo.toml index 502723bbb..e87ffea1a 100644 --- a/hash-fetch/Cargo.toml +++ b/hash-fetch/Cargo.toml @@ -18,3 +18,4 @@ fetch = { path = "../util/fetch" } ethcore-util = { path = "../util" } parity-reactor = { path = "../util/reactor" } native-contracts = { path = "../ethcore/native_contracts" } +hash = { path = "../util/hash" } diff --git a/hash-fetch/src/client.rs b/hash-fetch/src/client.rs index bd773e9b3..d63df794a 100644 --- a/hash-fetch/src/client.rs +++ b/hash-fetch/src/client.rs @@ -21,11 +21,12 @@ use std::io::Write; use std::sync::Arc; use std::path::PathBuf; +use hash::keccak_buffer; use fetch::{Fetch, Response, Error as FetchError, Client as FetchClient}; use futures::Future; use parity_reactor::Remote; use urlhint::{ContractClient, URLHintContract, URLHint, URLHintResult}; -use util::{H256, sha3}; +use util::H256; /// API for fetching by hash. pub trait HashFetch: Send + Sync + 'static { @@ -101,7 +102,7 @@ fn validate_hash(path: PathBuf, hash: H256, result: Result // And validate the hash let mut file_reader = io::BufReader::new(fs::File::open(&path)?); - let content_hash = sha3(&mut file_reader)?; + let content_hash = keccak_buffer(&mut file_reader)?; if content_hash != hash { Err(Error::HashMismatch{ got: content_hash, expected: hash }) } else { diff --git a/hash-fetch/src/lib.rs b/hash-fetch/src/lib.rs index 4952ecea2..987770252 100644 --- a/hash-fetch/src/lib.rs +++ b/hash-fetch/src/lib.rs @@ -31,6 +31,7 @@ extern crate native_contracts; extern crate parity_reactor; extern crate rand; extern crate rustc_hex; +extern crate hash; pub extern crate fetch; diff --git a/hash-fetch/src/urlhint.rs b/hash-fetch/src/urlhint.rs index bc4b63cf2..3fa32eb15 100644 --- a/hash-fetch/src/urlhint.rs +++ b/hash-fetch/src/urlhint.rs @@ -20,10 +20,11 @@ use std::sync::Arc; use rustc_hex::ToHex; use mime::Mime; use mime_guess; +use hash::keccak; use futures::{future, BoxFuture, Future}; use native_contracts::{Registry, Urlhint}; -use util::{Address, Bytes, Hashable}; +use util::{Address, Bytes}; const COMMIT_LEN: usize = 20; @@ -164,7 +165,7 @@ impl URLHint for URLHintContract { let urlhint = self.urlhint.clone(); let client = self.client.clone(); - self.registrar.get_address(do_call, "githubhint".sha3(), "A".into()) + self.registrar.get_address(do_call, keccak("githubhint"), "A".into()) .map(|addr| if addr == Address::default() { None } else { Some(addr) }) .and_then(move |address| { let mut fixed_id = [0; 32]; diff --git a/parity/blockchain.rs b/parity/blockchain.rs index 426c8d447..a94b387d4 100644 --- a/parity/blockchain.rs +++ b/parity/blockchain.rs @@ -21,7 +21,8 @@ use std::time::{Instant, Duration}; use std::thread::sleep; use std::sync::Arc; use rustc_hex::FromHex; -use util::{ToPretty, U256, H256, Address, Hashable}; +use hash::{keccak, KECCAK_NULL_RLP}; +use util::{ToPretty, U256, H256, Address}; use rlp::PayloadInfo; use ethcore::service::ClientService; use ethcore::client::{Mode, DatabaseCompactionProfile, VMType, BlockImportError, BlockChainClient, BlockId}; @@ -639,13 +640,13 @@ fn execute_export_state(cmd: ExportState) -> Result<(), String> { out.write_fmt(format_args!("\n\"0x{}\": {{\"balance\": \"{:x}\", \"nonce\": \"{:x}\"", account.hex(), balance, client.nonce(&account, at).unwrap_or_else(U256::zero))).expect("Write error"); let code = client.code(&account, at).unwrap_or(None).unwrap_or_else(Vec::new); if !code.is_empty() { - out.write_fmt(format_args!(", \"code_hash\": \"0x{}\"", code.sha3().hex())).expect("Write error"); + out.write_fmt(format_args!(", \"code_hash\": \"0x{}\"", keccak(&code).hex())).expect("Write error"); if cmd.code { out.write_fmt(format_args!(", \"code\": \"{}\"", code.to_hex())).expect("Write error"); } } - let storage_root = client.storage_root(&account, at).unwrap_or(::util::SHA3_NULL_RLP); - if storage_root != ::util::SHA3_NULL_RLP { + let storage_root = client.storage_root(&account, at).unwrap_or(KECCAK_NULL_RLP); + if storage_root != KECCAK_NULL_RLP { out.write_fmt(format_args!(", \"storage_root\": \"0x{}\"", storage_root.hex())).expect("Write error"); if cmd.storage { out.write_fmt(format_args!(", \"storage\": {{")).expect("Write error"); diff --git a/parity/configuration.rs b/parity/configuration.rs index 681804346..11360c41b 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -22,7 +22,8 @@ use std::collections::BTreeMap; use std::cmp::max; use std::str::FromStr; use cli::{Args, ArgsError}; -use util::{Hashable, H256, U256, Bytes, version_data, Address}; +use hash::keccak; +use util::{H256, U256, Bytes, version_data, Address}; use util::journaldb::Algorithm; use util::Colour; use ethsync::{NetworkConfiguration, is_valid_node_url}; @@ -506,7 +507,7 @@ impl Configuration { io_path: self.directories().db, listen_addr: self.stratum_interface(), port: self.args.flag_ports_shift + self.args.flag_stratum_port, - secret: self.args.flag_stratum_secret.as_ref().map(|s| s.parse::().unwrap_or_else(|_| s.sha3())), + secret: self.args.flag_stratum_secret.as_ref().map(|s| s.parse::().unwrap_or_else(|_| keccak(s))), })) } else { Ok(None) } } @@ -729,7 +730,7 @@ impl Configuration { ret.listen_address = Some(format!("{}", listen)); ret.public_address = public.map(|p| format!("{}", p)); ret.use_secret = match self.args.flag_node_key.as_ref() - .map(|s| s.parse::().or_else(|_| Secret::from_unsafe_slice(&s.sha3())).map_err(|e| format!("Invalid key: {:?}", e)) + .map(|s| s.parse::().or_else(|_| Secret::from_unsafe_slice(&keccak(s))).map_err(|e| format!("Invalid key: {:?}", e)) ) { None => None, Some(Ok(key)) => Some(key), diff --git a/parity/main.rs b/parity/main.rs index d79fb0064..00c219963 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -70,6 +70,7 @@ extern crate parity_whisper; extern crate path; extern crate rpc_cli; extern crate node_filter; +extern crate hash; #[macro_use] extern crate log as rlog; @@ -130,7 +131,7 @@ use std::collections::HashMap; use std::io::{self as stdio, BufReader, Read, Write}; use std::fs::{remove_file, metadata, File, create_dir_all}; use std::path::PathBuf; -use util::sha3::sha3; +use hash::keccak_buffer; use cli::Args; use configuration::{Cmd, Execute, Configuration}; use deprecated::find_deprecated; @@ -140,7 +141,7 @@ use dir::default_hypervisor_path; fn print_hash_of(maybe_file: Option) -> Result { if let Some(file) = maybe_file { let mut f = BufReader::new(File::open(&file).map_err(|_| "Unable to open file".to_owned())?); - let hash = sha3(&mut f).map_err(|_| "Unable to read from file".to_owned())?; + let hash = keccak_buffer(&mut f).map_err(|_| "Unable to read from file".to_owned())?; Ok(hash.hex()) } else { Err("Streaming from standard input not yet supported. Specify a file.".to_owned()) diff --git a/parity/snapshot.rs b/parity/snapshot.rs index dc786a2ea..34aa3252f 100644 --- a/parity/snapshot.rs +++ b/parity/snapshot.rs @@ -20,6 +20,7 @@ use std::time::Duration; use std::path::{Path, PathBuf}; use std::sync::Arc; +use hash::keccak; use ethcore::snapshot::{Progress, RestorationStatus, SnapshotService as SS}; use ethcore::snapshot::io::{SnapshotReader, PackedReader, PackedWriter}; use ethcore::snapshot::service::Service as SnapshotService; @@ -65,8 +66,6 @@ pub struct SnapshotCommand { // helper for reading chunks from arbitrary reader and feeding them into the // service. fn restore_using(snapshot: Arc, reader: &R, recover: bool) -> Result<(), String> { - use util::sha3::Hashable; - let manifest = reader.manifest(); info!("Restoring to block #{} (0x{:?})", manifest.block_number, manifest.block_hash); @@ -95,7 +94,7 @@ fn restore_using(snapshot: Arc, reader: &R, let chunk = reader.chunk(state_hash) .map_err(|e| format!("Encountered error while reading chunk {:?}: {}", state_hash, e))?; - let hash = chunk.sha3(); + let hash = keccak(&chunk); if hash != state_hash { return Err(format!("Mismatched chunk hash. Expected {:?}, got {:?}", state_hash, hash)); } @@ -112,7 +111,7 @@ fn restore_using(snapshot: Arc, reader: &R, let chunk = reader.chunk(block_hash) .map_err(|e| format!("Encountered error while reading chunk {:?}: {}", block_hash, e))?; - let hash = chunk.sha3(); + let hash = keccak(&chunk); if hash != block_hash { return Err(format!("Mismatched chunk hash. Expected {:?}, got {:?}", block_hash, hash)); } diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index a5a371cdf..382878683 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -54,6 +54,7 @@ parity-updater = { path = "../updater" } rlp = { path = "../util/rlp" } stats = { path = "../util/stats" } vm = { path = "../ethcore/vm" } +hash = { path = "../util/hash" } clippy = { version = "0.0.103", optional = true} pretty_assertions = "0.1" diff --git a/rpc/src/authcodes.rs b/rpc/src/authcodes.rs index 4427eda78..bb44eb506 100644 --- a/rpc/src/authcodes.rs +++ b/rpc/src/authcodes.rs @@ -21,7 +21,8 @@ use std::{fs, time, mem}; use itertools::Itertools; use rand::Rng; use rand::os::OsRng; -use util::{H256, Hashable}; +use hash::keccak; +use util::H256; /// Providing current time in seconds pub trait TimeProvider { @@ -162,7 +163,7 @@ impl AuthCodes { return false; } - let as_token = |code| format!("{}:{}", code, time).sha3(); + let as_token = |code| keccak(format!("{}:{}", code, time)); // Check if it's the initial token. if self.is_empty() { @@ -231,12 +232,13 @@ mod tests { use std::io::{Read, Write}; use std::{time, fs}; use std::cell::Cell; + use hash::keccak; - use util::{H256, Hashable}; + use util::H256; use super::*; fn generate_hash(val: &str, time: u64) -> H256 { - format!("{}:{}", val, time).sha3() + keccak(format!("{}:{}", val, time)) } #[test] diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index c429471f3..7db99ba2c 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -60,6 +60,7 @@ extern crate parity_reactor; extern crate parity_updater as updater; extern crate rlp; extern crate stats; +extern crate hash; #[macro_use] extern crate log; diff --git a/rpc/src/tests/ws.rs b/rpc/src/tests/ws.rs index 77fb1ea2c..6d4874845 100644 --- a/rpc/src/tests/ws.rs +++ b/rpc/src/tests/ws.rs @@ -56,7 +56,7 @@ pub fn request(server: Server, request: &str) -> http_client::Respon #[cfg(test)] mod testing { use std::time; - use util::Hashable; + use hash::keccak; use devtools::http_client; use super::{serve, request}; @@ -125,7 +125,7 @@ mod testing { {{}} ", port, - format!("{}:{}", code, timestamp).sha3(), + keccak(format!("{}:{}", code, timestamp)), timestamp, ) ); @@ -155,7 +155,7 @@ mod testing { {{}} ", port, - format!("{}:{}", code, timestamp).sha3(), + keccak(format!("{}:{}", code, timestamp)), timestamp, ) ); @@ -171,7 +171,7 @@ mod testing { {{}} ", port, - format!("{}:{}", code, timestamp).sha3(), + keccak(format!("{}:{}", code, timestamp)), timestamp, ) ); diff --git a/rpc/src/v1/helpers/dispatch.rs b/rpc/src/v1/helpers/dispatch.rs index ab8dd655a..41fc201cf 100644 --- a/rpc/src/v1/helpers/dispatch.rs +++ b/rpc/src/v1/helpers/dispatch.rs @@ -26,8 +26,8 @@ use light::client::LightChainClient; use light::on_demand::{request, OnDemand}; use light::TransactionQueue as LightTransactionQueue; use rlp; +use hash::keccak; use util::{Address, H520, H256, U256, Bytes, Mutex, RwLock}; -use util::sha3::Hashable; use stats::Corpus; use ethkey::Signature; @@ -226,7 +226,7 @@ pub fn eth_data_hash(mut data: Bytes) -> H256 { format!("\x19Ethereum Signed Message:\n{}", data.len()) .into_bytes(); message_data.append(&mut data); - message_data.sha3() + keccak(message_data) } /// Dispatcher for light clients -- fetches default gas price, next nonce, etc. from network. diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 0cf70bf23..f31d32426 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -24,7 +24,6 @@ use futures::{self, future, BoxFuture, Future}; use rlp::{self, UntrustedRlp}; use time::get_time; use util::{H160, H256, Address, U256, H64}; -use util::sha3::Hashable; use util::Mutex; use ethash::SeedHashCompute; @@ -149,7 +148,7 @@ impl EthClient where let view = block.header_view(); Ok(Some(RichBlock { inner: Block { - hash: Some(view.sha3().into()), + hash: Some(view.hash().into()), size: Some(block.rlp().as_raw().len().into()), parent_hash: view.parent_hash().into(), uncles_hash: view.uncles_hash().into(), diff --git a/rpc/src/v1/impls/light/eth.rs b/rpc/src/v1/impls/light/eth.rs index d33b5a06f..7e40bf71a 100644 --- a/rpc/src/v1/impls/light/eth.rs +++ b/rpc/src/v1/impls/light/eth.rs @@ -38,7 +38,7 @@ use ethcore::filter::Filter as EthcoreFilter; use ethcore::transaction::{Action, SignedTransaction, Transaction as EthTransaction}; use ethsync::LightSync; use rlp::UntrustedRlp; -use util::sha3::{SHA3_NULL_RLP, SHA3_EMPTY_LIST_RLP}; +use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; use util::{RwLock, Mutex, U256}; use futures::{future, Future, BoxFuture, IntoFuture}; @@ -295,7 +295,7 @@ impl Eth for EthClient { let (sync, on_demand) = (self.sync.clone(), self.on_demand.clone()); self.fetcher().header(BlockId::Hash(hash.into())).and_then(move |hdr| { - if hdr.transactions_root() == SHA3_NULL_RLP { + if hdr.transactions_root() == KECCAK_NULL_RLP { future::ok(Some(U256::from(0).into())).boxed() } else { sync.with_context(|ctx| on_demand.request(ctx, request::Body(hdr.into()))) @@ -311,7 +311,7 @@ impl Eth for EthClient { let (sync, on_demand) = (self.sync.clone(), self.on_demand.clone()); self.fetcher().header(num.into()).and_then(move |hdr| { - if hdr.transactions_root() == SHA3_NULL_RLP { + if hdr.transactions_root() == KECCAK_NULL_RLP { future::ok(Some(U256::from(0).into())).boxed() } else { sync.with_context(|ctx| on_demand.request(ctx, request::Body(hdr.into()))) @@ -327,7 +327,7 @@ impl Eth for EthClient { let (sync, on_demand) = (self.sync.clone(), self.on_demand.clone()); self.fetcher().header(BlockId::Hash(hash.into())).and_then(move |hdr| { - if hdr.uncles_hash() == SHA3_EMPTY_LIST_RLP { + if hdr.uncles_hash() == KECCAK_EMPTY_LIST_RLP { future::ok(Some(U256::from(0).into())).boxed() } else { sync.with_context(|ctx| on_demand.request(ctx, request::Body(hdr.into()))) @@ -343,7 +343,7 @@ impl Eth for EthClient { let (sync, on_demand) = (self.sync.clone(), self.on_demand.clone()); self.fetcher().header(num.into()).and_then(move |hdr| { - if hdr.uncles_hash() == SHA3_EMPTY_LIST_RLP { + if hdr.uncles_hash() == KECCAK_EMPTY_LIST_RLP { future::ok(Some(U256::from(0).into())).boxed() } else { sync.with_context(|ctx| on_demand.request(ctx, request::Body(hdr.into()))) diff --git a/rpc/src/v1/impls/light/parity_set.rs b/rpc/src/v1/impls/light/parity_set.rs index b97327168..bac6556e3 100644 --- a/rpc/src/v1/impls/light/parity_set.rs +++ b/rpc/src/v1/impls/light/parity_set.rs @@ -23,7 +23,7 @@ use std::sync::Arc; use ethsync::ManageNetwork; use fetch::Fetch; use futures::{BoxFuture, Future}; -use util::sha3; +use hash::keccak_buffer; use jsonrpc_core::Error; use v1::helpers::dapps::DappsService; @@ -129,7 +129,7 @@ impl ParitySet for ParitySetClient { result .map_err(errors::fetch) .and_then(|response| { - sha3(&mut io::BufReader::new(response)).map_err(errors::fetch) + keccak_buffer(&mut io::BufReader::new(response)).map_err(errors::fetch) }) .map(Into::into) })) diff --git a/rpc/src/v1/impls/parity_set.rs b/rpc/src/v1/impls/parity_set.rs index 540964e63..38890d6d1 100644 --- a/rpc/src/v1/impls/parity_set.rs +++ b/rpc/src/v1/impls/parity_set.rs @@ -24,7 +24,7 @@ use ethcore::mode::Mode; use ethsync::ManageNetwork; use fetch::{self, Fetch}; use futures::{BoxFuture, Future}; -use util::sha3; +use hash::keccak_buffer; use updater::{Service as UpdateService}; use jsonrpc_core::Error; @@ -170,7 +170,7 @@ impl ParitySet for ParitySetClient where result .map_err(errors::fetch) .and_then(|response| { - sha3(&mut io::BufReader::new(response)).map_err(errors::fetch) + keccak_buffer(&mut io::BufReader::new(response)).map_err(errors::fetch) }) .map(Into::into) })) diff --git a/rpc/src/v1/impls/web3.rs b/rpc/src/v1/impls/web3.rs index 859c44567..d0be2db81 100644 --- a/rpc/src/v1/impls/web3.rs +++ b/rpc/src/v1/impls/web3.rs @@ -15,11 +15,11 @@ // along with Parity. If not, see . //! Web3 rpc implementation. +use hash::keccak; use jsonrpc_core::*; use util::version; use v1::traits::Web3; use v1::types::{H256, Bytes}; -use util::sha3::Hashable; /// Web3 rpc implementation. pub struct Web3Client; @@ -35,6 +35,6 @@ impl Web3 for Web3Client { } fn sha3(&self, data: Bytes) -> Result { - Ok(data.0.sha3().into()) + Ok(keccak(&data.0).into()) } } diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index cbbfa8dc0..c13ee79c8 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -29,7 +29,7 @@ use ethcore::miner::{MinerOptions, Banning, GasPricer, MinerService, ExternalMin use ethcore::account_provider::AccountProvider; use ethjson::blockchain::BlockChain; use io::IoChannel; -use util::{U256, H256, Address, Hashable}; +use util::{U256, H256, Address}; use jsonrpc_core::IoHandler; use v1::impls::{EthClient, SigningUnsafeClient}; @@ -431,7 +431,7 @@ fn verify_transaction_counts(name: String, chain: BlockChain) { for b in chain.blocks_rlp().iter().filter(|b| Block::is_good(b)).map(|b| BlockView::new(b)) { let count = b.transactions_count(); - let hash = b.sha3(); + let hash = b.hash(); let number = b.header_view().number(); let (req, res) = by_hash(hash, count, &mut id); diff --git a/rpc_client/Cargo.toml b/rpc_client/Cargo.toml index 5cc688fb5..1e39d8b1e 100644 --- a/rpc_client/Cargo.toml +++ b/rpc_client/Cargo.toml @@ -19,3 +19,4 @@ jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "pa jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" } parity-rpc = { path = "../rpc" } ethcore-util = { path = "../util" } +hash = { path = "../util/hash" } diff --git a/rpc_client/src/client.rs b/rpc_client/src/client.rs index 3f55fbe73..9c6c4ebe8 100644 --- a/rpc_client/src/client.rs +++ b/rpc_client/src/client.rs @@ -9,7 +9,8 @@ use std::thread; use std::time; use std::path::PathBuf; -use util::{Hashable, Mutex}; +use hash::keccak; +use util::Mutex; use url::Url; use std::fs::File; @@ -72,7 +73,7 @@ impl Handler for RpcHandler { WsError::new(WsErrorKind::Internal, format!("{}", err)) })?; let secs = timestamp.as_secs(); - let hashed = format!("{}:{}", self.auth_code, secs).sha3(); + let hashed = keccak(format!("{}:{}", self.auth_code, secs)); let proto = format!("{:?}_{}", hashed, secs); r.add_protocol(&proto); Ok(r) diff --git a/rpc_client/src/lib.rs b/rpc_client/src/lib.rs index d1967ccbd..805754b22 100644 --- a/rpc_client/src/lib.rs +++ b/rpc_client/src/lib.rs @@ -11,6 +11,7 @@ extern crate serde; extern crate serde_json; extern crate tempdir; extern crate url; +extern crate hash; #[macro_use] extern crate log; diff --git a/secret_store/Cargo.toml b/secret_store/Cargo.toml index 19f342aa9..8a8010540 100644 --- a/secret_store/Cargo.toml +++ b/secret_store/Cargo.toml @@ -29,6 +29,7 @@ ethabi = "2.0" ethcore = { path = "../ethcore" } ethcore-devtools = { path = "../devtools" } ethcore-util = { path = "../util" } +hash = { path = "../util/hash" } ethcore-ipc = { path = "../ipc/rpc" } ethcore-ipc-nano = { path = "../ipc/nano" } ethcore-logger = { path = "../logger" } diff --git a/secret_store/src/key_server_cluster/math.rs b/secret_store/src/key_server_cluster/math.rs index 047a4556c..1c81d063e 100644 --- a/secret_store/src/key_server_cluster/math.rs +++ b/secret_store/src/key_server_cluster/math.rs @@ -15,7 +15,8 @@ // along with Parity. If not, see . use ethkey::{Public, Secret, Random, Generator, math}; -use util::{U256, H256, Hashable}; +use util::{U256, H256}; +use hash::keccak; use key_server_cluster::Error; #[derive(Debug)] @@ -306,7 +307,7 @@ pub fn combine_message_hash_with_public(message_hash: &H256, public: &Public) -> buffer[32..64].copy_from_slice(&public[0..32]); // calculate hash of buffer - let hash = (&buffer[..]).sha3(); + let hash = keccak(&buffer[..]); // map hash to EC finite field value let hash: U256 = hash.into(); diff --git a/secret_store/src/key_server_set.rs b/secret_store/src/key_server_set.rs index e17dceed5..02e509c89 100644 --- a/secret_store/src/key_server_set.rs +++ b/secret_store/src/key_server_set.rs @@ -22,7 +22,8 @@ use parking_lot::Mutex; use ethcore::filter::Filter; use ethcore::client::{Client, BlockChainClient, BlockId, ChainNotify}; use native_contracts::KeyServerSet as KeyServerSetContract; -use util::{H256, Address, Bytes, Hashable}; +use hash::keccak; +use util::{H256, Address, Bytes}; use types::all::{Error, Public, NodeAddress}; const KEY_SERVER_SET_CONTRACT_REGISTRY_NAME: &'static str = "secretstore_server_set"; @@ -33,8 +34,8 @@ const ADDED_EVENT_NAME: &'static [u8] = &*b"KeyServerAdded(address)"; const REMOVED_EVENT_NAME: &'static [u8] = &*b"KeyServerRemoved(address)"; lazy_static! { - static ref ADDED_EVENT_NAME_HASH: H256 = ADDED_EVENT_NAME.sha3(); - static ref REMOVED_EVENT_NAME_HASH: H256 = REMOVED_EVENT_NAME.sha3(); + static ref ADDED_EVENT_NAME_HASH: H256 = keccak(ADDED_EVENT_NAME); + static ref REMOVED_EVENT_NAME_HASH: H256 = keccak(REMOVED_EVENT_NAME); } /// Key Server set diff --git a/secret_store/src/lib.rs b/secret_store/src/lib.rs index d7f35a55a..8ba8648f8 100644 --- a/secret_store/src/lib.rs +++ b/secret_store/src/lib.rs @@ -44,6 +44,7 @@ extern crate ethcore_logger as logger; extern crate ethcrypto; extern crate ethkey; extern crate native_contracts; +extern crate hash; mod key_server_cluster; mod types; diff --git a/sync/Cargo.toml b/sync/Cargo.toml index b9a411879..5fb3628df 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -18,6 +18,7 @@ ethcore-io = { path = "../util/io" } ethcore-light = { path = "../ethcore/light"} ethcore = { path = "../ethcore" } rlp = { path = "../util/rlp" } +hash = { path = "../util/hash" } clippy = { version = "0.0.103", optional = true} log = "0.3" env_logger = "0.4" diff --git a/sync/src/block_sync.rs b/sync/src/block_sync.rs index 92c8df429..2cf8f974c 100644 --- a/sync/src/block_sync.rs +++ b/sync/src/block_sync.rs @@ -478,7 +478,7 @@ impl BlockDownloader { let receipts = block_and_receipts.receipts; let (h, number, parent) = { let header = BlockView::new(&block).header_view(); - (header.sha3(), header.number(), header.parent_hash()) + (header.hash(), header.number(), header.parent_hash()) }; // Perform basic block verification diff --git a/sync/src/blocks.rs b/sync/src/blocks.rs index dbd797007..b953db09a 100644 --- a/sync/src/blocks.rs +++ b/sync/src/blocks.rs @@ -17,6 +17,7 @@ use std::collections::{HashSet, HashMap}; use std::collections::hash_map::Entry; use smallvec::SmallVec; +use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; use util::*; use rlp::*; use network::NetworkError; @@ -342,7 +343,7 @@ impl BlockCollection { let body = UntrustedRlp::new(&b); let tx = body.at(0)?; let tx_root = ordered_trie_root(tx.iter().map(|r| r.as_raw().to_vec())); //TODO: get rid of vectors here - let uncles = body.at(1)?.as_raw().sha3(); + let uncles = keccak(body.at(1)?.as_raw()); HeaderId { transactions_root: tx_root, uncles: uncles @@ -425,7 +426,7 @@ impl BlockCollection { transactions_root: info.transactions_root().clone(), uncles: info.uncles_hash().clone(), }; - if header_id.transactions_root == sha3::SHA3_NULL_RLP && header_id.uncles == sha3::SHA3_EMPTY_LIST_RLP { + if header_id.transactions_root == KECCAK_NULL_RLP && header_id.uncles == KECCAK_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); @@ -438,7 +439,7 @@ impl BlockCollection { } if self.need_receipts { let receipt_root = info.receipts_root().clone(); - if receipt_root == sha3::SHA3_NULL_RLP { + if receipt_root == KECCAK_NULL_RLP { let receipts_stream = RlpStream::new_list(0); block.receipts = Some(receipts_stream.out()); } else { @@ -489,7 +490,6 @@ mod test { use ethcore::client::{TestBlockChainClient, EachBlockWith, BlockId, BlockChainClient}; use ethcore::views::HeaderView; use ethcore::header::BlockNumber; - use util::*; use rlp::*; fn is_empty(bc: &BlockCollection) -> bool { @@ -526,7 +526,7 @@ mod test { .map(|i| (&client as &BlockChainClient).block(BlockId::Number(i as BlockNumber)).unwrap().into_inner()) .collect(); let headers: Vec<_> = blocks.iter().map(|b| Rlp::new(b).at(0).as_raw().to_vec()).collect(); - let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).sha3()).collect(); + let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).hash()).collect(); let heads: Vec<_> = hashes.iter().enumerate().filter_map(|(i, h)| if i % 20 == 0 { Some(h.clone()) } else { None }).collect(); bc.reset_to(heads); assert!(!bc.is_empty()); @@ -581,7 +581,7 @@ mod test { .map(|i| (&client as &BlockChainClient).block(BlockId::Number(i as BlockNumber)).unwrap().into_inner()) .collect(); let headers: Vec<_> = blocks.iter().map(|b| Rlp::new(b).at(0).as_raw().to_vec()).collect(); - let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).sha3()).collect(); + let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).hash()).collect(); let heads: Vec<_> = hashes.iter().enumerate().filter_map(|(i, h)| if i % 20 == 0 { Some(h.clone()) } else { None }).collect(); bc.reset_to(heads); @@ -605,7 +605,7 @@ mod test { .map(|i| (&client as &BlockChainClient).block(BlockId::Number(i as BlockNumber)).unwrap().into_inner()) .collect(); let headers: Vec<_> = blocks.iter().map(|b| Rlp::new(b).at(0).as_raw().to_vec()).collect(); - let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).sha3()).collect(); + let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).hash()).collect(); let heads: Vec<_> = hashes.iter().enumerate().filter_map(|(i, h)| if i % 20 == 0 { Some(h.clone()) } else { None }).collect(); bc.reset_to(heads); diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 3a472c88f..9c62a2a61 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -91,6 +91,7 @@ use std::collections::{HashSet, HashMap}; use std::cmp; +use hash::keccak; use util::*; use rlp::*; use network::*; @@ -682,7 +683,7 @@ impl ChainSync { peer.confirmation = ForkConfirmation::TooShort; } else { let header = r.at(0)?.as_raw(); - if header.sha3() == fork_hash { + if keccak(&header) == fork_hash { trace!(target: "sync", "{}: Confirmed peer", peer_id); peer.confirmation = ForkConfirmation::Confirmed; if !io.chain_overlay().read().contains_key(&fork_number) { @@ -894,7 +895,7 @@ impl ChainSync { } let block_rlp = r.at(0)?; let header_rlp = block_rlp.at(0)?; - let h = header_rlp.as_raw().sha3(); + let h = keccak(&header_rlp.as_raw()); trace!(target: "sync", "{} -> NewBlock ({})", peer_id, h); let header: BlockHeader = header_rlp.as_val()?; if header.number() > self.highest_block.unwrap_or(0) { @@ -1055,7 +1056,7 @@ impl ChainSync { self.continue_sync(io); return Ok(()); } - self.snapshot.reset_to(&manifest, &manifest_rlp.as_raw().sha3()); + self.snapshot.reset_to(&manifest, &keccak(manifest_rlp.as_raw())); io.snapshot_service().begin_restore(manifest); self.state = SyncState::SnapshotData; @@ -1509,7 +1510,7 @@ impl ChainSync { false => io.snapshot_service().manifest(), }; let block_number = manifest.as_ref().map_or(0, |m| m.block_number); - let manifest_hash = manifest.map_or(H256::new(), |m| m.into_rlp().sha3()); + let manifest_hash = manifest.map_or(H256::new(), |m| keccak(m.into_rlp())); packet.append(&manifest_hash); packet.append(&block_number); } @@ -1531,7 +1532,7 @@ impl ChainSync { match io.chain().block_header(BlockId::Hash(hash)) { Some(hdr) => { let number = hdr.number().into(); - debug_assert_eq!(hdr.sha3(), hash); + debug_assert_eq!(hdr.hash(), hash); if max_headers == 1 || io.chain().block_hash(BlockId::Number(number)) != Some(hash) { // Non canonical header or single header requested @@ -2229,7 +2230,6 @@ mod tests { use tests::helpers::*; use tests::snapshot::TestSnapshotService; use util::{U256, Address, RwLock}; - use util::sha3::Hashable; use util::hash::H256; use util::bytes::Bytes; use rlp::{Rlp, RlpStream, UntrustedRlp}; @@ -2394,7 +2394,7 @@ mod tests { let blocks: Vec<_> = (0 .. 100) .map(|i| (&client as &BlockChainClient).block(BlockId::Number(i as BlockNumber)).map(|b| b.into_inner()).unwrap()).collect(); let headers: Vec<_> = blocks.iter().map(|b| Rlp::new(b).at(0).as_raw().to_vec()).collect(); - let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).sha3()).collect(); + let hashes: Vec<_> = headers.iter().map(|h| HeaderView::new(h).hash()).collect(); let queue = RwLock::new(VecDeque::new()); let ss = TestSnapshotService::new(); diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 51947317d..237d6c437 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -38,6 +38,7 @@ extern crate parking_lot; extern crate smallvec; extern crate rlp; extern crate ipnetwork; +extern crate hash; extern crate ethcore_light as light; diff --git a/sync/src/snapshot.rs b/sync/src/snapshot.rs index cecfbe425..d3cdf274f 100644 --- a/sync/src/snapshot.rs +++ b/sync/src/snapshot.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::{H256, Hashable}; +use hash::keccak; +use util::H256; use std::collections::HashSet; use ethcore::snapshot::ManifestData; @@ -71,7 +71,7 @@ impl Snapshot { /// Validate chunk and mark it as downloaded pub fn validate_chunk(&mut self, chunk: &[u8]) -> Result { - let hash = chunk.sha3(); + let hash = keccak(chunk); if self.completed_chunks.contains(&hash) { trace!(target: "sync", "Ignored proccessed chunk: {}", hash.hex()); return Err(()); @@ -136,6 +136,7 @@ impl Snapshot { #[cfg(test)] mod test { + use hash::keccak; use util::*; use super::*; use ethcore::snapshot::ManifestData; @@ -153,13 +154,13 @@ mod test { let block_chunks: Vec = (0..20).map(|_| H256::random().to_vec()).collect(); let manifest = ManifestData { version: 2, - state_hashes: state_chunks.iter().map(|data| data.sha3()).collect(), - block_hashes: block_chunks.iter().map(|data| data.sha3()).collect(), + state_hashes: state_chunks.iter().map(|data| keccak(data)).collect(), + block_hashes: block_chunks.iter().map(|data| keccak(data)).collect(), state_root: H256::new(), block_number: 42, block_hash: H256::new(), }; - let mhash = manifest.clone().into_rlp().sha3(); + let mhash = keccak(manifest.clone().into_rlp()); (manifest, mhash, state_chunks, block_chunks) } @@ -211,7 +212,7 @@ mod test { assert!(snapshot.is_complete()); assert_eq!(snapshot.done_chunks(), 40); assert_eq!(snapshot.done_chunks(), snapshot.total_chunks()); - assert_eq!(snapshot.snapshot_hash(), Some(manifest.into_rlp().sha3())); + assert_eq!(snapshot.snapshot_hash(), Some(keccak(manifest.into_rlp()))); } #[test] diff --git a/sync/src/tests/consensus.rs b/sync/src/tests/consensus.rs index 9b01156b7..a47c4a486 100644 --- a/sync/src/tests/consensus.rs +++ b/sync/src/tests/consensus.rs @@ -15,6 +15,7 @@ // along with Parity. If not, see . use std::sync::Arc; +use hash::keccak; use util::*; use io::{IoHandler, IoContext, IoChannel}; use ethcore::client::{BlockChainClient, Client}; @@ -56,8 +57,8 @@ fn new_tx(secret: &Secret, nonce: U256, chain_id: u64) -> PendingTransaction { #[test] fn authority_round() { - let s0 = KeyPair::from_secret_slice(&"1".sha3()).unwrap(); - let s1 = KeyPair::from_secret_slice(&"0".sha3()).unwrap(); + let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap(); + let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap(); let ap = Arc::new(AccountProvider::transient_provider()); ap.insert_account(s0.secret().clone(), "").unwrap(); ap.insert_account(s1.secret().clone(), "").unwrap(); @@ -143,8 +144,8 @@ fn authority_round() { #[test] fn tendermint() { - let s0 = KeyPair::from_secret_slice(&"1".sha3()).unwrap(); - let s1 = KeyPair::from_secret_slice(&"0".sha3()).unwrap(); + let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap(); + let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap(); let ap = Arc::new(AccountProvider::transient_provider()); ap.insert_account(s0.secret().clone(), "").unwrap(); ap.insert_account(s1.secret().clone(), "").unwrap(); diff --git a/sync/src/tests/snapshot.rs b/sync/src/tests/snapshot.rs index 9303aa9f7..d49bcf440 100644 --- a/sync/src/tests/snapshot.rs +++ b/sync/src/tests/snapshot.rs @@ -16,6 +16,7 @@ use std::collections::HashMap; use std::sync::Arc; +use hash::keccak; use util::*; use ethcore::snapshot::{SnapshotService, ManifestData, RestorationStatus}; use ethcore::header::BlockNumber; @@ -50,14 +51,14 @@ impl TestSnapshotService { let block_chunks: Vec = (0..num_block_chunks).map(|_| H256::random().to_vec()).collect(); let manifest = ManifestData { version: 2, - state_hashes: state_chunks.iter().map(|data| data.sha3()).collect(), - block_hashes: block_chunks.iter().map(|data| data.sha3()).collect(), + state_hashes: state_chunks.iter().map(|data| keccak(data)).collect(), + block_hashes: block_chunks.iter().map(|data| keccak(data)).collect(), state_root: H256::new(), block_number: block_number, block_hash: block_hash, }; - let mut chunks: HashMap = state_chunks.into_iter().map(|data| (data.sha3(), data)).collect(); - chunks.extend(block_chunks.into_iter().map(|data| (data.sha3(), data))); + let mut chunks: HashMap = state_chunks.into_iter().map(|data| (keccak(&data), data)).collect(); + chunks.extend(block_chunks.into_iter().map(|data| (keccak(&data), data))); TestSnapshotService { manifest: Some(manifest), chunks: chunks, From ba3b2712a1d8f51e83b1c1c19beac4d95bc22b45 Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 31 Aug 2017 12:38:53 +0200 Subject: [PATCH 06/16] fixed grumbles --- util/hash/src/lib.rs | 8 ++++++-- util/network/src/connection.rs | 4 ++-- util/network/src/handshake.rs | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/util/hash/src/lib.rs b/util/hash/src/lib.rs index bd0121248..91d3b4bf9 100644 --- a/util/hash/src/lib.rs +++ b/util/hash/src/lib.rs @@ -31,19 +31,23 @@ pub const KECCAK_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x5 pub const KECCAK_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] ); extern { + /// Hashes input. Returns -1 if either out or input does not exist. Otherwise returns 0. pub fn keccak_256(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) -> i32; + /// Hashes input. Returns -1 if either out or input does not exist. Otherwise returns 0. pub fn keccak_512(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) -> i32; } pub fn keccak>(s: T) -> H256 { let mut result = [0u8; 32]; - keccak_into(s, &mut result); + write_keccak(s, &mut result); H256(result) } -pub fn keccak_into>(s: T, dest: &mut [u8]) { +pub fn write_keccak>(s: T, dest: &mut [u8]) { let input = s.as_ref(); unsafe { + // we can safely ignore keccak_256 output, cause we know that both input + // and dest are properly allocated keccak_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len()); } } diff --git a/util/network/src/connection.rs b/util/network/src/connection.rs index b51813100..45bc5dd7b 100644 --- a/util/network/src/connection.rs +++ b/util/network/src/connection.rs @@ -18,7 +18,7 @@ use std::sync::Arc; use std::collections::VecDeque; use std::net::SocketAddr; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; -use hash::{keccak, keccak_into}; +use hash::{keccak, write_keccak}; use mio::{Token, Ready, PollOpt}; use mio::deprecated::{Handler, EventLoop, TryRead, TryWrite}; use mio::tcp::*; @@ -312,7 +312,7 @@ impl EncryptedConnection { } let mut key_material = H512::new(); shared.copy_to(&mut key_material[0..32]); - keccak_into(&nonce_material, &mut key_material[32..64]); + write_keccak(&nonce_material, &mut key_material[32..64]); keccak(&key_material).copy_to(&mut key_material[32..64]); keccak(&key_material).copy_to(&mut key_material[32..64]); diff --git a/util/network/src/handshake.rs b/util/network/src/handshake.rs index ca38d4360..281bfd8d9 100644 --- a/util/network/src/handshake.rs +++ b/util/network/src/handshake.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use rand::random; -use hash::keccak_into; +use hash::write_keccak; use mio::tcp::*; use util::hash::*; use util::bytes::Bytes; @@ -273,7 +273,7 @@ impl Handshake { // E(remote-pubk, S(ecdhe-random, ecdh-shared-secret^nonce) || H(ecdhe-random-pubk) || pubk || nonce || 0x0) let shared = *ecdh::agree(secret, &self.id)?; sig.copy_from_slice(&*sign(self.ecdhe.secret(), &(&shared ^ &self.nonce))?); - keccak_into(self.ecdhe.public(), hepubk); + write_keccak(self.ecdhe.public(), hepubk); pubk.copy_from_slice(public); nonce.copy_from_slice(&self.nonce); } From d31b7d8968aa0986b7dbca8d97d20ffcd8b034c2 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Thu, 31 Aug 2017 20:50:06 +0800 Subject: [PATCH 07/16] add more hash backward compatibility test for bloom (#6425) --- util/bloom/src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/util/bloom/src/lib.rs b/util/bloom/src/lib.rs index 4fdb61d40..22a2cbc2a 100644 --- a/util/bloom/src/lib.rs +++ b/util/bloom/src/lib.rs @@ -245,14 +245,33 @@ mod tests { } #[test] - fn hash_backward_compatibility() { + fn hash_backward_compatibility_for_new() { let ss = vec!["you", "should", "not", "break", "hash", "backward", "compatibility"]; let mut bloom = Bloom::new(16, 8); for s in ss.iter() { bloom.set(&s); } + let drained_elems: HashSet = bloom.drain_journal().entries.into_iter().map(|t| t.1).collect(); let expected: HashSet = [2094615114573771027u64, 244675582389208413u64].iter().cloned().collect(); assert_eq!(drained_elems, expected); + assert_eq!(bloom.k_num, 12); + } + + #[test] + fn hash_backward_compatibility_for_from_parts() { + let stored_state = vec![2094615114573771027u64, 244675582389208413u64]; + let k_num = 12; + let bloom = Bloom::from_parts(&stored_state, k_num); + + let ss = vec!["you", "should", "not", "break", "hash", "backward", "compatibility"]; + let tt = vec!["this", "doesnot", "exist"]; + for s in ss.iter() { + assert!(bloom.check(&s)); + } + for s in tt.iter() { + assert!(!bloom.check(&s)); + } + } } From e9cc4c848b9880405de45f9a7031481875a25210 Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 31 Aug 2017 15:25:30 +0200 Subject: [PATCH 08/16] replace sha3 with keccak in few comments --- ethash/src/compute.rs | 4 ++-- util/rlp/src/common.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ethash/src/compute.rs b/ethash/src/compute.rs index ae680b686..9eb042147 100644 --- a/ethash/src/compute.rs +++ b/ethash/src/compute.rs @@ -219,7 +219,7 @@ fn keccak_512(input: &[u8], output: &mut [u8]) { } fn keccak_512_inplace(input: &mut [u8]) { - // This is safe since `sha3_*` uses an internal buffer and copies the result to the output. This + // This is safe since `keccak_*` uses an internal buffer and copies the result to the output. This // means that we can reuse the input buffer for both input and output. unsafe { hash::keccak_512(input.as_mut_ptr(), input.len(), input.as_ptr(), input.len()) }; } @@ -335,7 +335,7 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) mem::size_of::(), ); - // compute sha3-512 hash and replicate across mix + // compute keccak-512 hash and replicate across mix hash::keccak_512( out.as_mut_ptr(), NODE_BYTES, diff --git a/util/rlp/src/common.rs b/util/rlp/src/common.rs index 07c75cf98..1f4e2c517 100644 --- a/util/rlp/src/common.rs +++ b/util/rlp/src/common.rs @@ -21,16 +21,16 @@ lazy_static! { } static EMPTY_RLPS: &'static [&'static [u8]] = &[ - // RLP of SHA3_NULL_RLP + // RLP of KECCAK_NULL_RLP &[160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33], - // RLP of SHA3_EMPTY + // RLP of KECCAK_EMPTY &[160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112] ]; static COMMON_RLPS: &'static [&'static [u8]] = &[ - // RLP of SHA3_NULL_RLP + // RLP of KECCAK_NULL_RLP &[160, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33], - // RLP of SHA3_EMPTY + // RLP of KECCAK_EMPTY &[160, 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112], // Other RLPs found in blocks DB using the test below. &[160, 29, 204, 77, 232, 222, 199, 93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, 66, 253, 64, 212, 147, 71], From 56f46edab81f46435ea7ca842d04ef8c73586d87 Mon Sep 17 00:00:00 2001 From: Craig O'Connor Date: Fri, 1 Sep 2017 06:23:41 -0400 Subject: [PATCH 09/16] Pub sub blocks (#6139) * fun start * playin * linting * cleanup kk 1 * cleanup kk 2 * package * oops * grumbles * lint * lint 2 * all-possible-cases * conflict resolution * conflict resolution * ready * lint * remove package-lock * Restart subscription on transport close. * Bring back the formatter. * Fix formatter. --- hw/src/lib.rs | 1 - js/package-lock.json | 4 +- js/src/api/pubsub/parity/parity.js | 19 +-- js/src/views/Signer/store.js | 36 +++-- js/src/views/Status/NodeStatus/nodeStatus.js | 39 +----- js/src/views/Status/NodeStatus/store.js | 137 ++++++++----------- 6 files changed, 92 insertions(+), 144 deletions(-) diff --git a/hw/src/lib.rs b/hw/src/lib.rs index cda6c2241..e4b5c9f54 100644 --- a/hw/src/lib.rs +++ b/hw/src/lib.rs @@ -155,7 +155,6 @@ impl HardwareWalletManager { self.ledger.lock().set_key_path(key_path); } - /// List connected wallets. This only returns wallets that are ready to be used. pub fn list_wallets(&self) -> Vec { self.ledger.lock().list_devices() diff --git a/js/package-lock.json b/js/package-lock.json index 707e5a67d..7b272b35f 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -7722,7 +7722,7 @@ "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha1-UWbihkV/AzBgZL5Ul+jbsMPTIIM=", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "requires": { "brace-expansion": "1.1.8" } @@ -10081,7 +10081,7 @@ "react-qr-reader": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/react-qr-reader/-/react-qr-reader-1.1.3.tgz", - "integrity": "sha1-dDmnZvyZPLj17u/HLCnblh1AswI=", + "integrity": "sha512-ruBF8KaSwUW9nbzjO4rA7/HOCGYZuNUz9od7uBRy8SRBi24nwxWWmwa2z8R6vPGDRglA0y2Qk1aVBuC1olTnHw==", "requires": { "jsqr": "git+https://github.com/JodusNodus/jsQR.git#5ba1acefa1cbb9b2bc92b49f503f2674e2ec212b", "prop-types": "15.5.10", diff --git a/js/src/api/pubsub/parity/parity.js b/js/src/api/pubsub/parity/parity.js index bf18effa1..6df4a9204 100644 --- a/js/src/api/pubsub/parity/parity.js +++ b/js/src/api/pubsub/parity/parity.js @@ -183,16 +183,17 @@ export default class Parity extends PubsubBase { localTransactions (callback) { return this.addListener(this._api, 'parity_localTransactions', (error, transactions) => { - error - ? callback(error) - : callback(null, transactions => { - Object.values(transactions) - .filter(tx => tx.transaction) - .map(tx => { - tx.transaction = outTransaction(tx.transaction); - }); - return transactions; + if (error) { + return callback(error); + } + + Object.values(transactions) + .filter(tx => tx.transaction) + .map(tx => { + tx.transaction = outTransaction(tx.transaction); }); + + callback(null, transactions); }); } diff --git a/js/src/views/Signer/store.js b/js/src/views/Signer/store.js index a18f4e209..7c4fa891d 100644 --- a/js/src/views/Signer/store.js +++ b/js/src/views/Signer/store.js @@ -25,11 +25,13 @@ export default class SignerStore { constructor (api, withLocalTransactions = false, externalLink = '') { this._api = api; - this._timeoutId = 0; this.externalLink = externalLink; if (withLocalTransactions) { - this.fetchLocalTransactions(); + this._api.transport.on('close', () => { + this.subscribeLocalTransactions(); + }); + this.subscribeLocalTransactions(); } } @@ -49,9 +51,7 @@ export default class SignerStore { } @action unsubscribe () { - if (this._timeoutId) { - clearTimeout(this._timeoutId); - } + this.subscription.then(id => this._api.pubsub.unsubscribe([id])); } fetchBalance (address) { @@ -87,21 +87,17 @@ export default class SignerStore { }); } - fetchLocalTransactions = () => { - const nextTimeout = () => { - this._timeoutId = setTimeout(this.fetchLocalTransactions, 1500); - }; + subscribeLocalTransactions = () => { + this.subscription = this._api.pubsub.parity.localTransactions((error, transactions) => { + if (error) { + console.warn('subscribeLocalTransactions', error); + return; + } + const keys = Object + .keys(transactions) + .filter((key) => transactions[key].status !== 'canceled'); - this._api.parity - .localTransactions() - .then((localTransactions) => { - const keys = Object - .keys(localTransactions) - .filter((key) => localTransactions[key].status !== 'canceled'); - - this.setLocalHashes(keys); - }) - .then(() => nextTimeout()) - .catch(() => nextTimeout()); + this.setLocalHashes(keys); + }); } } diff --git a/js/src/views/Status/NodeStatus/nodeStatus.js b/js/src/views/Status/NodeStatus/nodeStatus.js index 7e2e0679e..3022541cf 100644 --- a/js/src/views/Status/NodeStatus/nodeStatus.js +++ b/js/src/views/Status/NodeStatus/nodeStatus.js @@ -18,7 +18,7 @@ import bytes from 'bytes'; import moment from 'moment'; import React, { Component, PropTypes } from 'react'; import { FormattedMessage } from 'react-intl'; -import { connect } from 'react-redux'; +import { observer } from 'mobx-react'; import { Container, ContainerTitle, Input } from '~/ui'; @@ -27,18 +27,12 @@ import StatusStore from './store'; import styles from './nodeStatus.css'; +@observer class NodeStatus extends Component { static contextTypes = { api: PropTypes.object.isRequired }; - static propTypes = { - blockNumber: PropTypes.object, - blockTimestamp: PropTypes.object, - netChain: PropTypes.string, - netPeers: PropTypes.object - }; - statusStore = new StatusStore(this.context.api); componentWillMount () { @@ -50,8 +44,7 @@ class NodeStatus extends Component { } render () { - const { blockNumber, blockTimestamp, netPeers } = this.props; - const { hashrate } = this.statusStore; + const { blockNumber, blockTimestamp, netPeers, hashrate } = this.statusStore; if (!netPeers || !blockNumber) { return null; @@ -157,8 +150,7 @@ class NodeStatus extends Component { } renderSettings () { - const { netChain } = this.props; - const { enode, rpcSettings, netPort = '' } = this.statusStore; + const { chain, enode, rpcSettings, netPort = '' } = this.statusStore; if (!rpcSettings) { return null; @@ -185,7 +177,7 @@ class NodeStatus extends Component { defaultMessage='chain' /> } - value={ netChain } + value={ chain } />
@@ -279,23 +271,4 @@ class NodeStatus extends Component { } } -function mapStateToProps (state) { - const { - blockNumber, - blockTimestamp, - netChain, - netPeers - } = state.nodeStatus; - - return { - blockNumber, - blockTimestamp, - netChain, - netPeers - }; -} - -export default connect( - mapStateToProps, - null -)(NodeStatus); +export default NodeStatus; diff --git a/js/src/views/Status/NodeStatus/store.js b/js/src/views/Status/NodeStatus/store.js index 22fe20070..c9ca6f0de 100644 --- a/js/src/views/Status/NodeStatus/store.js +++ b/js/src/views/Status/NodeStatus/store.js @@ -15,11 +15,15 @@ // along with Parity. If not, see . import BigNumber from 'bignumber.js'; -import { action, observable, transaction } from 'mobx'; +import { action, observable } from 'mobx'; export default class StatusStore { @observable defaultExtraData = ''; @observable enode = ''; + @observable blockNumber = new BigNumber(0); + @observable blockTimestamp = new Date(); + @observable chain = ''; + @observable netPeers = new BigNumber(0); @observable hashrate = new BigNumber(0); @observable netPort = new BigNumber(0); @observable nodeName = ''; @@ -35,39 +39,68 @@ export default class StatusStore { constructor (api) { this.api = api; - } - - @action setLongStatus ({ defaultExtraData, enode, netPort, rpcSettings }) { - transaction(() => { - this.defaultExtraData = defaultExtraData; - this.enode = enode; - this.netPort = netPort; - this.rpcSettings = rpcSettings; + this.api.transport.on('close', () => { + if (this.isPolling) { + this.startPolling(); + } }); } - @action setStatus ({ hashrate }) { - transaction(() => { - this.hashrate = hashrate; - }); + @action setStatuses ({ chain, defaultExtraData, enode, netPeers, netPort, rpcSettings, hashrate }) { + this.chain = chain; + this.defaultExtraData = defaultExtraData; + this.enode = enode; + this.netPeers = netPeers; + this.netPort = netPort; + this.rpcSettings = rpcSettings; + this.hashrate = hashrate; } @action setMinerSettings ({ coinbase, extraData, gasFloorTarget, minGasPrice }) { - transaction(() => { - this.coinbase = coinbase; - this.extraData = extraData; - this.gasFloorTarget = gasFloorTarget; - this.minGasPrice = minGasPrice; - }); - } - - startPolling () { - this._pollStatus(); - this._pollLongStatus(); + this.coinbase = coinbase; + this.extraData = extraData; + this.gasFloorTarget = gasFloorTarget; + this.minGasPrice = minGasPrice; } stopPolling () { - Object.keys(this._timeoutIds).forEach((key) => clearTimeout(this._timeoutIds[key])); + this.isPolling = false; + this.subscription.then(id => this.api.pubsub.unsubscribe([id])); + } + + startPolling () { + this.isPolling = true; + this.subscription = this.api.pubsub.parity.getBlockHeaderByNumber((error, block) => { + if (error) { + console.warn('_startPolling', error); + return; + } + this.subscribed = true; + this.blockNumber = block.number; + this.blockTimestamp = block.timestamp; + this._pollMinerSettings(); + Promise + .all([ + this.api.parity.chain(), + this.api.parity.defaultExtraData(), + this.api.parity.enode().then((enode) => enode).catch(() => '-'), + this.api.parity.netPeers(), + this.api.parity.netPort(), + this.api.parity.rpcSettings(), + this.api.eth.hashrate() + ]) + .then(([ + chain, defaultExtraData, enode, netPeers, netPort, rpcSettings, hashrate + ]) => { + this.setStatuses({ + chain, defaultExtraData, enode, netPeers, netPort, rpcSettings, hashrate + }); + }) + .catch((error) => { + console.error('_pollStatuses', error); + return; + }); + }); } /** @@ -100,60 +133,6 @@ export default class StatusStore { }); } - _pollStatus () { - const nextTimeout = (timeout = 1000) => { - clearTimeout(this._timeoutIds.short); - this._timeoutIds.short = setTimeout(() => this._pollStatus(), timeout); - }; - - return Promise - .all([ - this.api.eth.hashrate() - ]) - .then(([ - hashrate - ]) => { - this.setStatus({ - hashrate - }); - }) - .catch((error) => { - console.error('_pollStatus', error); - }) - .then(() => { - nextTimeout(); - }); - } - - _pollLongStatus () { - const nextTimeout = (timeout = 30000) => { - clearTimeout(this._timeoutIds.long); - this._timeoutIds.long = setTimeout(() => this._pollLongStatus(), timeout); - }; - - this._pollMinerSettings(); - return Promise - .all([ - this.api.parity.defaultExtraData(), - this.api.parity.enode().then((enode) => enode).catch(() => '-'), - this.api.parity.netPort(), - this.api.parity.rpcSettings() - ]) - .then(([ - defaultExtraData, enode, netPort, rpcSettings - ]) => { - this.setLongStatus({ - defaultExtraData, enode, netPort, rpcSettings - }); - }) - .catch((error) => { - console.error('_pollLongStatus', error); - }) - .then(() => { - nextTimeout(); - }); - } - handleUpdateSetting = () => { return this._pollMinerSettings(); }; From cf366bdb298bed6371b14af958efe8f4c0bea2e0 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Fri, 1 Sep 2017 22:57:57 +0800 Subject: [PATCH 10/16] util should not reexport ansi_term --- Cargo.lock | 2 ++ ethcore/Cargo.toml | 1 + ethcore/src/blockchain/blockchain.rs | 1 + ethcore/src/lib.rs | 1 + ethcore/src/miner/miner.rs | 1 + ethcore/src/service.rs | 1 + parity/configuration.rs | 2 +- parity/informant.rs | 4 ++-- parity/run.rs | 3 ++- rpc/Cargo.toml | 1 + rpc/src/lib.rs | 1 + rpc/src/v1/types/confirmations.rs | 2 +- rpc/src/v1/types/transaction_request.rs | 2 +- util/src/lib.rs | 1 - 14 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 15d841103..d5a63f9df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,6 +492,7 @@ dependencies = [ name = "ethcore" version = "1.8.0" dependencies = [ + "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "bloomable 0.1.0", "bloomchain 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2013,6 +2014,7 @@ dependencies = [ name = "parity-rpc" version = "1.8.0" dependencies = [ + "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "cid 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.103 (registry+https://github.com/rust-lang/crates.io-index)", "ethash 1.8.0", diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 6ee332c99..4741c37ee 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -11,6 +11,7 @@ build = "build.rs" "ethcore-ipc-codegen" = { path = "../ipc/codegen" } [dependencies] +ansi_term = "0.9" bit-set = "0.4" bloomchain = "0.1" bn = { git = "https://github.com/paritytech/bn" } diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index 022c93b8f..ac51a789e 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -41,6 +41,7 @@ use db::{self, Writable, Readable, CacheUpdatePolicy}; use cache_manager::CacheManager; use encoded; use engines::epoch::{Transition as EpochTransition, PendingTransition as PendingEpochTransition}; +use ansi_term::Colour; const LOG_BLOOMS_LEVELS: usize = 3; const LOG_BLOOMS_ELEMENTS_PER_INDEX: usize = 16; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index daab42acf..6732e63c6 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -103,6 +103,7 @@ extern crate rand; extern crate rlp; extern crate hash; extern crate heapsize; +extern crate ansi_term; #[macro_use] extern crate rlp_derive; diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 2fa4a76e2..1fa4508bf 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -40,6 +40,7 @@ use miner::service_transaction_checker::ServiceTransactionChecker; use price_info::{Client as PriceInfoClient, PriceInfo}; use price_info::fetch::Client as FetchClient; use header::{Header, BlockNumber}; +use ansi_term::Colour; /// Different possible definitions for pending transaction set. #[derive(Debug, PartialEq)] diff --git a/ethcore/src/service.rs b/ethcore/src/service.rs index 5e65a4de8..1be5c9a19 100644 --- a/ethcore/src/service.rs +++ b/ethcore/src/service.rs @@ -28,6 +28,7 @@ use miner::Miner; use snapshot::ManifestData; use snapshot::service::{Service as SnapshotService, ServiceParams as SnapServiceParams}; use std::sync::atomic::AtomicBool; +use ansi_term::Colour; #[cfg(feature="ipc")] use nanoipc; diff --git a/parity/configuration.rs b/parity/configuration.rs index 11360c41b..c27872905 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -25,7 +25,7 @@ use cli::{Args, ArgsError}; use hash::keccak; use util::{H256, U256, Bytes, version_data, Address}; use util::journaldb::Algorithm; -use util::Colour; +use ansi_term::Colour; use ethsync::{NetworkConfiguration, is_valid_node_url}; use ethcore::ethstore::ethkey::{Secret, Public}; use ethcore::client::{VMType}; diff --git a/parity/informant.rs b/parity/informant.rs index 1935ec9b6..4e2987892 100644 --- a/parity/informant.rs +++ b/parity/informant.rs @@ -16,7 +16,7 @@ extern crate ansi_term; use self::ansi_term::Colour::{White, Yellow, Green, Cyan, Blue}; -use self::ansi_term::Style; +use self::ansi_term::{Colour, Style}; use std::sync::{Arc}; use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering as AtomicOrdering}; @@ -35,7 +35,7 @@ use light::client::LightChainClient; use number_prefix::{binary_prefix, Standalone, Prefixed}; use parity_rpc::{is_major_importing}; use parity_rpc::informant::RpcStats; -use util::{RwLock, Mutex, H256, Colour, Bytes}; +use util::{RwLock, Mutex, H256, Bytes}; /// Format byte counts to standard denominations. pub fn format_bytes(b: usize) -> String { diff --git a/parity/run.rs b/parity/run.rs index a81d61ba0..093a019f3 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -37,7 +37,8 @@ use node_health; use parity_reactor::EventLoop; use parity_rpc::{NetworkSettings, informant, is_major_importing}; use updater::{UpdatePolicy, Updater}; -use util::{Colour, version, Mutex, Condvar}; +use util::{version, Mutex, Condvar}; +use ansi_term::Colour; use node_filter::NodeFilter; use params::{ diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index 382878683..a0bbe95ca 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -8,6 +8,7 @@ authors = ["Parity Technologies "] [lib] [dependencies] +ansi_term = "0.9" cid = "0.2" futures = "0.1" futures-cpupool = "0.1" diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 7db99ba2c..22f623bf6 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -20,6 +20,7 @@ #![cfg_attr(feature="dev", feature(plugin))] #![cfg_attr(feature="dev", plugin(clippy))] +extern crate ansi_term; extern crate cid; extern crate crypto as rust_crypto; extern crate futures; diff --git a/rpc/src/v1/types/confirmations.rs b/rpc/src/v1/types/confirmations.rs index bbc8b350e..39bf8d0a1 100644 --- a/rpc/src/v1/types/confirmations.rs +++ b/rpc/src/v1/types/confirmations.rs @@ -18,7 +18,7 @@ use std::fmt; use serde::{Serialize, Serializer}; -use util::Colour; +use ansi_term::Colour; use util::bytes::ToPretty; use v1::types::{U256, TransactionRequest, RichRawTransaction, H160, H256, H520, Bytes, TransactionCondition, Origin}; diff --git a/rpc/src/v1/types/transaction_request.rs b/rpc/src/v1/types/transaction_request.rs index cb74e071b..7f1c16287 100644 --- a/rpc/src/v1/types/transaction_request.rs +++ b/rpc/src/v1/types/transaction_request.rs @@ -18,7 +18,7 @@ use v1::types::{Bytes, H160, U256, TransactionCondition}; use v1::helpers; -use util::Colour; +use ansi_term::Colour; use std::fmt; diff --git a/util/src/lib.rs b/util/src/lib.rs index 0f16609c7..790a1c3f9 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -149,7 +149,6 @@ pub use vector::*; pub use bigint::prelude::*; pub use bigint::hash; -pub use ansi_term::{Colour, Style}; pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; /// 160-bit integer representing account address From 6b8c9cac23215312932ee40ef5560543a0c053b5 Mon Sep 17 00:00:00 2001 From: GitLab Build Bot Date: Fri, 1 Sep 2017 16:33:31 +0000 Subject: [PATCH 11/16] [ci skip] js-precompiled 20170901-161931 --- Cargo.lock | 2 +- js/package-lock.json | 2 +- js/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d5a63f9df..bf0a72ac1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2119,7 +2119,7 @@ dependencies = [ [[package]] name = "parity-ui-precompiled" version = "1.4.0" -source = "git+https://github.com/paritytech/js-precompiled.git#d809723e58bcb36c0f8d2eca5ca94abbb3690544" +source = "git+https://github.com/paritytech/js-precompiled.git#f34bdb63272fd59edf2062dda44113831964d800" dependencies = [ "parity-dapps-glue 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/js/package-lock.json b/js/package-lock.json index 7b272b35f..0bc45f0c5 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -1,6 +1,6 @@ { "name": "parity.js", - "version": "1.8.17", + "version": "1.8.18", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/js/package.json b/js/package.json index 1bb76b21c..52415f95a 100644 --- a/js/package.json +++ b/js/package.json @@ -1,6 +1,6 @@ { "name": "parity.js", - "version": "1.8.17", + "version": "1.8.18", "main": "release/index.js", "jsnext:main": "src/index.js", "author": "Parity Team ", From 0b5285c282ed48f0a2b387259a0870b1fa5940f1 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Sat, 2 Sep 2017 16:54:39 +0800 Subject: [PATCH 12/16] move timer.rs to ethcore (#6437) --- ethcore/src/client/client.rs | 3 ++- ethcore/src/lib.rs | 1 + ethcore/src/miner/miner.rs | 1 + {util => ethcore}/src/timer.rs | 0 util/src/lib.rs | 2 -- 5 files changed, 4 insertions(+), 3 deletions(-) rename {util => ethcore}/src/timer.rs (100%) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 9d5465593..69674901f 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -24,7 +24,8 @@ use itertools::Itertools; // util use hash::keccak; -use util::{Bytes, PerfTimer, Mutex, RwLock, MutexGuard}; +use timer::PerfTimer; +use util::{Bytes, Mutex, RwLock, MutexGuard}; use util::{journaldb, DBValue, TrieFactory, Trie}; use util::{U256, H256, Address, H2048}; use util::trie::TrieSpec; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 6732e63c6..57abf12dd 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -151,6 +151,7 @@ pub mod service; pub mod snapshot; pub mod spec; pub mod state; +pub mod timer; pub mod trace; pub mod transaction; pub mod verification; diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 1fa4508bf..fb1753760 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -19,6 +19,7 @@ use std::collections::{BTreeMap, HashSet}; use std::sync::Arc; use util::*; +use timer::PerfTimer; use using_queue::{UsingQueue, GetAction}; use account_provider::{AccountProvider, SignError as AccountError}; use state::State; diff --git a/util/src/timer.rs b/ethcore/src/timer.rs similarity index 100% rename from util/src/timer.rs rename to ethcore/src/timer.rs diff --git a/util/src/lib.rs b/util/src/lib.rs index 790a1c3f9..405fec797 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -131,7 +131,6 @@ pub mod nibblevec; pub mod semantic_version; pub mod snappy; pub mod cache; -mod timer; pub use misc::*; pub use hashdb::*; @@ -142,7 +141,6 @@ pub use triehash::*; pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut}; pub use semantic_version::*; pub use kvdb::*; -pub use timer::*; pub use error::*; pub use bytes::*; pub use vector::*; From 2faa28ce9b9e65d4b09db9ab3e38747cdfc3d24a Mon Sep 17 00:00:00 2001 From: Hawstein Date: Sat, 2 Sep 2017 19:14:21 +0800 Subject: [PATCH 13/16] separate semantic_version from util (#6438) --- Cargo.lock | 5 +++++ ethcore/Cargo.toml | 1 + ethcore/src/engines/authority_round/mod.rs | 1 + ethcore/src/engines/basic_authority.rs | 1 + ethcore/src/engines/mod.rs | 1 + ethcore/src/engines/tendermint/mod.rs | 1 + ethcore/src/ethereum/ethash.rs | 1 + ethcore/src/lib.rs | 1 + util/semantic_version/Cargo.toml | 6 ++++++ .../semantic_version.rs => semantic_version/src/lib.rs} | 4 ++-- util/src/lib.rs | 2 -- 11 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 util/semantic_version/Cargo.toml rename util/{src/semantic_version.rs => semantic_version/src/lib.rs} (95%) diff --git a/Cargo.lock b/Cargo.lock index bf0a72ac1..207145a29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -536,6 +536,7 @@ dependencies = [ "rlp_derive 0.1.0", "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "semantic_version 0.1.0", "semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "stats 0.1.0", "table 0.1.0", @@ -2634,6 +2635,10 @@ dependencies = [ "libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "semantic_version" +version = "0.1.0" + [[package]] name = "semver" version = "0.1.20" diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 4741c37ee..59e0b14d1 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -62,6 +62,7 @@ bloomable = { path = "../util/bloomable" } vm = { path = "vm" } wasm = { path = "wasm" } hash = { path = "../util/hash" } +semantic_version = { path = "../util/semantic_version" } [dev-dependencies] native-contracts = { path = "native_contracts", features = ["test_contracts"] } diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index 776aca320..c9c483773 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -42,6 +42,7 @@ use ethkey::{verify_address, Signature}; use io::{IoContext, IoHandler, TimerToken, IoService}; use itertools::{self, Itertools}; use rlp::{UntrustedRlp, encode}; +use semantic_version::SemanticVersion; use util::*; mod finality; diff --git a/ethcore/src/engines/basic_authority.rs b/ethcore/src/engines/basic_authority.rs index c22e66dfd..ccd69b386 100644 --- a/ethcore/src/engines/basic_authority.rs +++ b/ethcore/src/engines/basic_authority.rs @@ -31,6 +31,7 @@ use evm::Schedule; use ethjson; use header::{Header, BlockNumber}; use client::Client; +use semantic_version::SemanticVersion; use super::signer::EngineSigner; use super::validator_set::{ValidatorSet, SimpleList, new_validator_set}; diff --git a/ethcore/src/engines/mod.rs b/ethcore/src/engines/mod.rs index cb13c984a..d3cbdccaa 100644 --- a/ethcore/src/engines/mod.rs +++ b/ethcore/src/engines/mod.rs @@ -54,6 +54,7 @@ use spec::CommonParams; use transaction::{UnverifiedTransaction, SignedTransaction}; use ethkey::Signature; +use semantic_version::SemanticVersion; use util::*; /// Default EIP-210 contrat code. diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index f6981c210..f4b1422d8 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -48,6 +48,7 @@ use super::transition::TransitionHandler; use super::vote_collector::VoteCollector; use self::message::*; use self::params::TendermintParams; +use semantic_version::SemanticVersion; #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] pub enum Step { diff --git a/ethcore/src/ethereum/ethash.rs b/ethcore/src/ethereum/ethash.rs index abb77aefc..c6383039d 100644 --- a/ethcore/src/ethereum/ethash.rs +++ b/ethcore/src/ethereum/ethash.rs @@ -35,6 +35,7 @@ use evm::Schedule; use ethjson; use rlp::{self, UntrustedRlp}; use vm::LastHashes; +use semantic_version::SemanticVersion; /// Parity tries to round block.gas_limit to multiple of this constant pub const PARITY_GAS_LIMIT_DETERMINANT: U256 = U256([37, 0, 0, 0]); diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 57abf12dd..df7c25d3f 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -104,6 +104,7 @@ extern crate rlp; extern crate hash; extern crate heapsize; extern crate ansi_term; +extern crate semantic_version; #[macro_use] extern crate rlp_derive; diff --git a/util/semantic_version/Cargo.toml b/util/semantic_version/Cargo.toml new file mode 100644 index 000000000..5cd888d0a --- /dev/null +++ b/util/semantic_version/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "semantic_version" +version = "0.1.0" +authors = ["Parity Technologies "] + +[dependencies] diff --git a/util/src/semantic_version.rs b/util/semantic_version/src/lib.rs similarity index 95% rename from util/src/semantic_version.rs rename to util/semantic_version/src/lib.rs index fe4b8727f..0a8eeb499 100644 --- a/util/src/semantic_version.rs +++ b/util/semantic_version/src/lib.rs @@ -20,8 +20,8 @@ /// /// # Example /// ``` -/// extern crate ethcore_util as util; -/// use util::semantic_version::*; +/// extern crate semantic_version; +/// use semantic_version::*; /// /// fn main() { /// assert_eq!(SemanticVersion::new(1, 2, 3).as_u32(), 0x010203); diff --git a/util/src/lib.rs b/util/src/lib.rs index 405fec797..305b17220 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -128,7 +128,6 @@ pub mod triehash; pub mod trie; pub mod nibbleslice; pub mod nibblevec; -pub mod semantic_version; pub mod snappy; pub mod cache; @@ -139,7 +138,6 @@ pub use overlaydb::*; pub use journaldb::JournalDB; pub use triehash::*; pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut}; -pub use semantic_version::*; pub use kvdb::*; pub use error::*; pub use bytes::*; From 1d95fe481f6cb77f8491d5a12af6bb17ecc9faf0 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Sun, 3 Sep 2017 02:08:34 +0800 Subject: [PATCH 14/16] fix modexp bug: return 0 if base=0 (#6424) --- ethcore/src/builtin.rs | 92 +++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 29 deletions(-) diff --git a/ethcore/src/builtin.rs b/ethcore/src/builtin.rs index b6d7064eb..a752064cd 100644 --- a/ethcore/src/builtin.rs +++ b/ethcore/src/builtin.rs @@ -267,6 +267,34 @@ impl Impl for Ripemd160 { } } +// calculate modexp: exponentiation by squaring. the `num` crate has pow, but not modular. +fn modexp(mut base: BigUint, mut exp: BigUint, modulus: BigUint) -> BigUint { + use num::Integer; + + match (base.is_zero(), exp.is_zero()) { + (_, true) => return BigUint::one(), // n^0 % m + (true, false) => return BigUint::zero(), // 0^n % m, n>0 + (false, false) if modulus <= BigUint::one() => return BigUint::zero(), // a^b % 1 = 0. + _ => {} + } + + let mut result = BigUint::one(); + base = base % &modulus; + + // fast path for base divisible by modulus. + if base.is_zero() { return BigUint::zero() } + while !exp.is_zero() { + if exp.is_odd() { + result = (result * &base) % &modulus; + } + + exp = exp >> 1; + base = (base.clone() * base) % &modulus; + } + + result +} + impl Impl for ModexpImpl { fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> { let mut reader = input.chain(io::repeat(0)); @@ -295,34 +323,6 @@ impl Impl for ModexpImpl { let exp = read_num(exp_len); let modulus = read_num(mod_len); - // calculate modexp: exponentiation by squaring. the `num` crate has pow, but not modular. - fn modexp(mut base: BigUint, mut exp: BigUint, modulus: BigUint) -> BigUint { - use num::Integer; - - match (base.is_zero(), exp.is_zero()) { - (_, true) => return BigUint::one(), // n^0 % m - (true, false) => return BigUint::zero(), // 0^n % m, n>0 - (false, false) if modulus <= BigUint::one() => return BigUint::zero(), // a^b % 1 = 0. - _ => {} - } - - let mut result = BigUint::one(); - base = base % &modulus; - - // fast path for base divisible by modulus. - if base.is_zero() { return result } - while !exp.is_zero() { - if exp.is_odd() { - result = (result * &base) % &modulus; - } - - exp = exp >> 1; - base = (base.clone() * base) % &modulus; - } - - result - } - // write output to given memory, left padded and same length as the modulus. let bytes = modexp(base, exp, modulus).to_bytes_be(); @@ -504,10 +504,44 @@ impl Impl for Bn128PairingImpl { #[cfg(test)] mod tests { - use super::{Builtin, Linear, ethereum_builtin, Pricer, Modexp}; + use super::{Builtin, Linear, ethereum_builtin, Pricer, Modexp, modexp as me}; use ethjson; use util::{U256, BytesRef}; use rustc_hex::FromHex; + use num::{BigUint, Zero, One}; + + #[test] + fn modexp_func() { + // n^0 % m == 1 + let mut base = BigUint::parse_bytes(b"12345", 10).unwrap(); + let mut exp = BigUint::zero(); + let mut modulus = BigUint::parse_bytes(b"789", 10).unwrap(); + assert_eq!(me(base, exp, modulus), BigUint::one()); + + // 0^n % m == 0 + base = BigUint::zero(); + exp = BigUint::parse_bytes(b"12345", 10).unwrap(); + modulus = BigUint::parse_bytes(b"789", 10).unwrap(); + assert_eq!(me(base, exp, modulus), BigUint::zero()); + + // n^m % 1 == 0 + base = BigUint::parse_bytes(b"12345", 10).unwrap(); + exp = BigUint::parse_bytes(b"789", 10).unwrap(); + modulus = BigUint::one(); + assert_eq!(me(base, exp, modulus), BigUint::zero()); + + // if n % d == 0, then n^m % d == 0 + base = BigUint::parse_bytes(b"12345", 10).unwrap(); + exp = BigUint::parse_bytes(b"789", 10).unwrap(); + modulus = BigUint::parse_bytes(b"15", 10).unwrap(); + assert_eq!(me(base, exp, modulus), BigUint::zero()); + + // others + base = BigUint::parse_bytes(b"12345", 10).unwrap(); + exp = BigUint::parse_bytes(b"789", 10).unwrap(); + modulus = BigUint::parse_bytes(b"97", 10).unwrap(); + assert_eq!(me(base, exp, modulus), BigUint::parse_bytes(b"55", 10).unwrap()); + } #[test] fn identity() { From 7b8af30590ebe7ae78e06c7bd6d70fb79d85cc43 Mon Sep 17 00:00:00 2001 From: Hawstein Date: Sun, 3 Sep 2017 02:09:13 +0800 Subject: [PATCH 15/16] remove re-export of parking_lot in util (#6435) --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + dapps/Cargo.toml | 1 + dapps/src/apps/fetcher/mod.rs | 2 +- dapps/src/handlers/fetch.rs | 2 +- dapps/src/lib.rs | 3 ++- dapps/src/tests/helpers/fetch.rs | 2 +- dapps/src/tests/helpers/registrar.rs | 3 ++- ethcore/Cargo.toml | 1 + ethcore/evm/Cargo.toml | 1 + ethcore/evm/src/interpreter/shared_cache.rs | 3 ++- ethcore/evm/src/lib.rs | 1 + ethcore/light/Cargo.toml | 1 + ethcore/light/src/client/header_chain.rs | 6 +++--- ethcore/light/src/client/mod.rs | 3 ++- ethcore/light/src/client/service.rs | 6 +++--- ethcore/light/src/lib.rs | 1 + ethcore/light/src/net/load_timer.rs | 2 +- ethcore/light/src/net/mod.rs | 5 +++-- ethcore/light/src/net/tests/mod.rs | 2 +- ethcore/light/src/on_demand/mod.rs | 2 +- ethcore/light/src/on_demand/request.rs | 6 ++++-- ethcore/light/src/on_demand/tests.rs | 3 ++- ethcore/light/src/provider.rs | 3 ++- ethcore/node_filter/Cargo.toml | 1 + ethcore/node_filter/src/lib.rs | 4 +++- ethcore/src/account_provider/mod.rs | 2 +- ethcore/src/blockchain/blockchain.rs | 1 + ethcore/src/client/ancient_import.rs | 2 +- ethcore/src/client/client.rs | 3 ++- ethcore/src/client/test_client.rs | 1 + ethcore/src/db.rs | 3 ++- ethcore/src/engines/authority_round/mod.rs | 1 + ethcore/src/engines/basic_authority.rs | 1 + ethcore/src/engines/tendermint/mod.rs | 1 + ethcore/src/engines/validator_set/contract.rs | 1 + ethcore/src/engines/validator_set/multi.rs | 3 ++- ethcore/src/engines/validator_set/safe_contract.rs | 1 + ethcore/src/engines/vote_collector.rs | 1 + ethcore/src/lib.rs | 1 + ethcore/src/miner/external.rs | 3 ++- ethcore/src/miner/miner.rs | 1 + ethcore/src/miner/service_transaction_checker.rs | 3 ++- ethcore/src/miner/stratum.rs | 2 +- ethcore/src/miner/work_notify.rs | 1 + ethcore/src/snapshot/mod.rs | 2 +- ethcore/src/snapshot/service.rs | 3 ++- ethcore/src/snapshot/tests/proof_of_work.rs | 3 ++- ethcore/src/snapshot/tests/state.rs | 2 +- ethcore/src/snapshot/watcher.rs | 2 +- ethcore/src/spec/spec.rs | 1 + ethcore/src/state/backend.rs | 3 ++- ethcore/src/state_db.rs | 3 ++- ethcore/src/trace/db.rs | 3 ++- ethcore/src/verification/queue/mod.rs | 1 + hash-fetch/Cargo.toml | 1 + hash-fetch/src/client.rs | 2 +- hash-fetch/src/lib.rs | 1 + hash-fetch/src/urlhint.rs | 3 ++- parity/blockchain.rs | 3 ++- parity/informant.rs | 3 ++- parity/light_helpers/queue_cull.rs | 2 +- parity/main.rs | 1 + parity/rpc_apis.rs | 2 +- parity/run.rs | 7 ++++--- rpc/Cargo.toml | 1 + rpc/src/lib.rs | 1 + rpc/src/v1/helpers/dispatch.rs | 3 ++- rpc/src/v1/helpers/light_fetch.rs | 3 ++- rpc/src/v1/helpers/signer.rs | 2 +- rpc/src/v1/helpers/signing_queue.rs | 6 ++++-- rpc/src/v1/helpers/subscription_manager.rs | 2 +- rpc/src/v1/impls/eth.rs | 2 +- rpc/src/v1/impls/eth_filter.rs | 3 ++- rpc/src/v1/impls/eth_pubsub.rs | 3 ++- rpc/src/v1/impls/light/eth.rs | 3 ++- rpc/src/v1/impls/pubsub.rs | 2 +- rpc/src/v1/impls/signer.rs | 2 +- rpc/src/v1/impls/signing.rs | 3 ++- rpc/src/v1/informant.rs | 2 +- rpc/src/v1/tests/helpers/miner_service.rs | 3 ++- rpc/src/v1/tests/helpers/snapshot_service.rs | 3 ++- rpc/src/v1/tests/helpers/sync_provider.rs | 3 ++- rpc/src/v1/tests/mocked/eth.rs | 3 ++- rpc_client/Cargo.toml | 1 + rpc_client/src/client.rs | 2 +- rpc_client/src/lib.rs | 1 + stratum/Cargo.toml | 1 + stratum/src/lib.rs | 4 +++- sync/src/chain.rs | 4 +++- sync/src/light_sync/mod.rs | 3 ++- sync/src/light_sync/tests/test_net.rs | 2 +- sync/src/tests/helpers.rs | 1 + sync/src/tests/snapshot.rs | 1 + updater/Cargo.toml | 1 + updater/src/lib.rs | 1 + updater/src/updater.rs | 3 ++- util/network/src/tests.rs | 3 ++- util/src/lib.rs | 2 -- 99 files changed, 161 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 207145a29..b126d3d35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -530,6 +530,7 @@ dependencies = [ "native-contracts 0.1.0", "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "price-info 1.7.0", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.0", @@ -662,6 +663,7 @@ dependencies = [ "heapsize 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.0", "rlp_derive 0.1.0", @@ -771,6 +773,7 @@ dependencies = [ "jsonrpc-tcp-server 7.0.0 (git+https://github.com/paritytech/jsonrpc.git?branch=parity-1.7)", "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -937,6 +940,7 @@ dependencies = [ "lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "parity-wasm 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.0", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "vm 0.1.0", @@ -1650,6 +1654,7 @@ dependencies = [ "futures 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "native-contracts 0.1.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1890,6 +1895,7 @@ dependencies = [ "parity-rpc-client 1.4.0", "parity-updater 1.8.0", "parity-whisper 0.1.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "path 0.1.0", "pretty_assertions 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1932,6 +1938,7 @@ dependencies = [ "parity-hash-fetch 1.8.0", "parity-reactor 0.1.0", "parity-ui 1.8.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1971,6 +1978,7 @@ dependencies = [ "mime_guess 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "native-contracts 0.1.0", "parity-reactor 0.1.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2049,6 +2057,7 @@ dependencies = [ "order-stat 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "parity-reactor 0.1.0", "parity-updater 1.8.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_assertions 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.0", @@ -2077,6 +2086,7 @@ dependencies = [ "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "parity-rpc 1.8.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2140,6 +2150,7 @@ dependencies = [ "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "parity-hash-fetch 1.8.0", "parity-reactor 0.1.0", + "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "path 0.1.0", "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 9f6dd9ccb..8eb4acd28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ number_prefix = "0.2" rpassword = "0.2.1" semver = "0.6" ansi_term = "0.9" +parking_lot = "0.4" regex = "0.2" isatty = "0.1" toml = "0.4" diff --git a/dapps/Cargo.toml b/dapps/Cargo.toml index 76b026c9a..35ec2d983 100644 --- a/dapps/Cargo.toml +++ b/dapps/Cargo.toml @@ -13,6 +13,7 @@ futures = "0.1" linked-hash-map = "0.3" log = "0.3" parity-dapps-glue = "1.7" +parking_lot = "0.4" mime = "0.2" mime_guess = "1.6.1" rand = "0.3" diff --git a/dapps/src/apps/fetcher/mod.rs b/dapps/src/apps/fetcher/mod.rs index 5b91da1a3..fe529d772 100644 --- a/dapps/src/apps/fetcher/mod.rs +++ b/dapps/src/apps/fetcher/mod.rs @@ -32,7 +32,7 @@ use hyper; use hyper::status::StatusCode; use {Embeddable, SyncStatus, random_filename}; -use util::Mutex; +use parking_lot::Mutex; use page::LocalPageEndpoint; use handlers::{ContentHandler, ContentFetcherHandler}; use endpoint::{Endpoint, EndpointPath, Handler}; diff --git a/dapps/src/handlers/fetch.rs b/dapps/src/handlers/fetch.rs index 7740247d9..afe1f5083 100644 --- a/dapps/src/handlers/fetch.rs +++ b/dapps/src/handlers/fetch.rs @@ -23,7 +23,7 @@ use std::time::{Instant, Duration}; use fetch::{self, Fetch}; use futures::Future; use parity_reactor::Remote; -use util::Mutex; +use parking_lot::Mutex; use hyper::{server, Decoder, Encoder, Next, Method, Control}; use hyper::net::HttpStream; diff --git a/dapps/src/lib.rs b/dapps/src/lib.rs index f5670c959..688f1c8ac 100644 --- a/dapps/src/lib.rs +++ b/dapps/src/lib.rs @@ -24,6 +24,7 @@ extern crate futures; extern crate itertools; extern crate linked_hash_map; extern crate mime_guess; +extern crate parking_lot; extern crate rand; extern crate rustc_hex; extern crate serde; @@ -73,7 +74,7 @@ use std::collections::HashMap; use std::mem; use std::path::PathBuf; use std::sync::Arc; -use util::RwLock; +use parking_lot::RwLock; use jsonrpc_http_server::{self as http, hyper, Origin}; diff --git a/dapps/src/tests/helpers/fetch.rs b/dapps/src/tests/helpers/fetch.rs index e6e875c51..853d6857e 100644 --- a/dapps/src/tests/helpers/fetch.rs +++ b/dapps/src/tests/helpers/fetch.rs @@ -16,7 +16,7 @@ use std::{io, thread, time}; use std::sync::{atomic, mpsc, Arc}; -use util::Mutex; +use parking_lot::Mutex; use futures::{self, Future}; use fetch::{self, Fetch}; diff --git a/dapps/src/tests/helpers/registrar.rs b/dapps/src/tests/helpers/registrar.rs index d7890675b..03fe3d5e4 100644 --- a/dapps/src/tests/helpers/registrar.rs +++ b/dapps/src/tests/helpers/registrar.rs @@ -20,7 +20,8 @@ use std::collections::HashMap; use rustc_hex::FromHex; use hash_fetch::urlhint::ContractClient; -use util::{Bytes, Address, Mutex, H256, ToPretty}; +use parking_lot::Mutex; +use util::{Bytes, Address, H256, ToPretty}; const REGISTRAR: &'static str = "8e4e9b13d4b45cb0befc93c3061b1408f67316b2"; const URLHINT: &'static str = "deadbeefcafe0000000000000000000000000000"; diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 59e0b14d1..5b37d96ca 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -46,6 +46,7 @@ lru-cache = "0.1.0" native-contracts = { path = "native_contracts" } num = "0.1" num_cpus = "1.2" +parking_lot = "0.4" price-info = { path = "../price-info" } rand = "0.3" rlp = { path = "../util/rlp" } diff --git a/ethcore/evm/Cargo.toml b/ethcore/evm/Cargo.toml index 7d323d622..e6b72e68a 100644 --- a/ethcore/evm/Cargo.toml +++ b/ethcore/evm/Cargo.toml @@ -16,6 +16,7 @@ log = "0.3" rlp = { path = "../../util/rlp" } vm = { path = "../vm" } parity-wasm = "0.12" +parking_lot = "0.4" ethcore-logger = { path = "../../logger" } wasm-utils = { git = "https://github.com/paritytech/wasm-utils" } hash = { path = "../../util/hash" } diff --git a/ethcore/evm/src/interpreter/shared_cache.rs b/ethcore/evm/src/interpreter/shared_cache.rs index 4f70044dd..ed39d30c8 100644 --- a/ethcore/evm/src/interpreter/shared_cache.rs +++ b/ethcore/evm/src/interpreter/shared_cache.rs @@ -17,7 +17,8 @@ use std::sync::Arc; use hash::KECCAK_EMPTY; use heapsize::HeapSizeOf; -use util::{H256, Mutex}; +use parking_lot::Mutex; +use util::H256; use util::cache::MemoryLruCache; use bit_set::BitSet; use super::super::instructions; diff --git a/ethcore/evm/src/lib.rs b/ethcore/evm/src/lib.rs index d5b08c8d8..4b418fd74 100644 --- a/ethcore/evm/src/lib.rs +++ b/ethcore/evm/src/lib.rs @@ -23,6 +23,7 @@ extern crate ethcore_util as util; extern crate ethjson; extern crate rlp; extern crate parity_wasm; +extern crate parking_lot; extern crate wasm_utils; extern crate ethcore_logger; extern crate heapsize; diff --git a/ethcore/light/Cargo.toml b/ethcore/light/Cargo.toml index 4e7b3523d..46ae3afe0 100644 --- a/ethcore/light/Cargo.toml +++ b/ethcore/light/Cargo.toml @@ -31,6 +31,7 @@ itertools = "0.5" bincode = "0.8.0" serde = "1.0" serde_derive = "1.0" +parking_lot = "0.4" stats = { path = "../../util/stats" } hash = { path = "../../util/hash" } diff --git a/ethcore/light/src/client/header_chain.rs b/ethcore/light/src/client/header_chain.rs index a16cb0c85..85f8123c4 100644 --- a/ethcore/light/src/client/header_chain.rs +++ b/ethcore/light/src/client/header_chain.rs @@ -37,11 +37,11 @@ use ethcore::ids::BlockId; use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp, UntrustedRlp}; use heapsize::HeapSizeOf; -use util::{H256, U256, RwLock}; +use util::{H256, U256}; use util::kvdb::{DBTransaction, KeyValueDB}; use cache::Cache; -use util::Mutex; +use parking_lot::{Mutex, RwLock}; use smallvec::SmallVec; @@ -555,7 +555,7 @@ mod tests { use cache::Cache; use time::Duration; - use util::Mutex; + use parking_lot::Mutex; fn make_db() -> Arc<::util::KeyValueDB> { Arc::new(::util::kvdb::in_memory(0)) diff --git a/ethcore/light/src/client/mod.rs b/ethcore/light/src/client/mod.rs index ee270c977..387abc8ad 100644 --- a/ethcore/light/src/client/mod.rs +++ b/ethcore/light/src/client/mod.rs @@ -30,8 +30,9 @@ use ethcore::spec::Spec; use ethcore::service::ClientIoMessage; use ethcore::encoded; use io::IoChannel; +use parking_lot::{Mutex, RwLock}; -use util::{H256, U256, Mutex, RwLock}; +use util::{H256, U256}; use util::kvdb::{KeyValueDB, CompactionProfile}; use self::header_chain::{AncestryIter, HeaderChain}; diff --git a/ethcore/light/src/client/service.rs b/ethcore/light/src/client/service.rs index 83949a2f1..99dccc999 100644 --- a/ethcore/light/src/client/service.rs +++ b/ethcore/light/src/client/service.rs @@ -28,7 +28,7 @@ use io::{IoContext, IoError, IoHandler, IoService}; use util::kvdb::{Database, DatabaseConfig}; use cache::Cache; -use util::Mutex; +use parking_lot::Mutex; use super::{Client, Config as ClientConfig}; @@ -117,11 +117,11 @@ mod tests { use super::Service; use devtools::RandomTempPath; use ethcore::spec::Spec; - + use std::sync::Arc; use cache::Cache; use time::Duration; - use util::Mutex; + use parking_lot::Mutex; #[test] fn it_works() { diff --git a/ethcore/light/src/lib.rs b/ethcore/light/src/lib.rs index 37de03205..5d9a5ae32 100644 --- a/ethcore/light/src/lib.rs +++ b/ethcore/light/src/lib.rs @@ -77,6 +77,7 @@ extern crate futures; extern crate itertools; extern crate rand; extern crate rlp; +extern crate parking_lot; #[macro_use] extern crate rlp_derive; extern crate serde; diff --git a/ethcore/light/src/net/load_timer.rs b/ethcore/light/src/net/load_timer.rs index a169b86d9..7b78fc693 100644 --- a/ethcore/light/src/net/load_timer.rs +++ b/ethcore/light/src/net/load_timer.rs @@ -32,7 +32,7 @@ use request::{CompleteRequest, Kind}; use bincode; use time; -use util::{RwLock, Mutex}; +use parking_lot::{RwLock, Mutex}; /// Number of time periods samples should be kept for. pub const MOVING_SAMPLE_SIZE: usize = 256; diff --git a/ethcore/light/src/net/mod.rs b/ethcore/light/src/net/mod.rs index 046dc68bd..9f47c56ff 100644 --- a/ethcore/light/src/net/mod.rs +++ b/ethcore/light/src/net/mod.rs @@ -24,7 +24,8 @@ use io::TimerToken; use network::{HostInfo, NetworkProtocolHandler, NetworkContext, PeerId}; use rlp::{RlpStream, UntrustedRlp}; use util::hash::H256; -use util::{DBValue, Mutex, RwLock, U256}; +use util::{DBValue, U256}; +use parking_lot::{Mutex, RwLock}; use time::{Duration, SteadyTime}; use std::collections::{HashMap, HashSet}; @@ -287,7 +288,7 @@ pub type PeerMap = HashMap>; mod id_guard { use network::PeerId; - use util::RwLockReadGuard; + use parking_lot::RwLockReadGuard; use super::{PeerMap, ReqId}; diff --git a/ethcore/light/src/net/tests/mod.rs b/ethcore/light/src/net/tests/mod.rs index ef86c08f5..15a5eb21a 100644 --- a/ethcore/light/src/net/tests/mod.rs +++ b/ethcore/light/src/net/tests/mod.rs @@ -606,7 +606,7 @@ fn id_guard() { pending_requests.insert(req_id_1, req.clone(), 0.into(), ::time::SteadyTime::now()); pending_requests.insert(req_id_2, req, 1.into(), ::time::SteadyTime::now()); - proto.peers.write().insert(peer_id, ::util::Mutex::new(Peer { + proto.peers.write().insert(peer_id, ::parking_lot::Mutex::new(Peer { local_credits: flow_params.create_credits(), status: status(provider.client.chain_info()), capabilities: capabilities.clone(), diff --git a/ethcore/light/src/on_demand/mod.rs b/ethcore/light/src/on_demand/mod.rs index 42469725b..d67b7dc4e 100644 --- a/ethcore/light/src/on_demand/mod.rs +++ b/ethcore/light/src/on_demand/mod.rs @@ -27,7 +27,7 @@ use ethcore::executed::{Executed, ExecutionError}; use futures::{Async, Poll, Future}; use futures::sync::oneshot::{self, Sender, Receiver, Canceled}; use network::PeerId; -use util::{RwLock, Mutex}; +use parking_lot::{RwLock, Mutex}; use net::{ self, Handler, PeerStatus, Status, Capabilities, diff --git a/ethcore/light/src/on_demand/request.rs b/ethcore/light/src/on_demand/request.rs index 8ee41437b..3a38ec974 100644 --- a/ethcore/light/src/on_demand/request.rs +++ b/ethcore/light/src/on_demand/request.rs @@ -30,7 +30,8 @@ use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY, KECCAK_EMPTY_LIST_RLP, keccak}; use request::{self as net_request, IncompleteRequest, CompleteRequest, Output, OutputKind, Field}; use rlp::{RlpStream, UntrustedRlp}; -use util::{Address, Bytes, DBValue, HashDB, Mutex, H256, U256}; +use parking_lot::Mutex; +use util::{Address, Bytes, DBValue, HashDB, H256, U256}; use util::memorydb::MemoryDB; use util::trie::{Trie, TrieDB, TrieError}; @@ -850,7 +851,8 @@ impl TransactionProof { #[cfg(test)] mod tests { use super::*; - use util::{MemoryDB, Address, Mutex, H256}; + use parking_lot::Mutex; + use util::{MemoryDB, Address, H256}; use util::trie::{Trie, TrieMut, SecTrieDB, SecTrieDBMut}; use util::trie::recorder::Recorder; use hash::keccak; diff --git a/ethcore/light/src/on_demand/tests.rs b/ethcore/light/src/on_demand/tests.rs index 10c4ceae5..52d2bc5ea 100644 --- a/ethcore/light/src/on_demand/tests.rs +++ b/ethcore/light/src/on_demand/tests.rs @@ -22,7 +22,8 @@ use ethcore::header::{Header, Seal}; use futures::Future; use network::{PeerId, NodeId}; use net::*; -use util::{H256, Mutex}; +use parking_lot::Mutex; +use util::H256; use time::Duration; use ::request::{self as basic_request, Response}; diff --git a/ethcore/light/src/provider.rs b/ethcore/light/src/provider.rs index 3632783ca..8fc4c93e8 100644 --- a/ethcore/light/src/provider.rs +++ b/ethcore/light/src/provider.rs @@ -24,7 +24,8 @@ use ethcore::client::{BlockChainClient, ProvingBlockChainClient}; use ethcore::transaction::PendingTransaction; use ethcore::ids::BlockId; use ethcore::encoded; -use util::{RwLock, H256}; +use parking_lot::RwLock; +use util::H256; use cht::{self, BlockInfo}; use client::{LightChainClient, AsLightClient}; diff --git a/ethcore/node_filter/Cargo.toml b/ethcore/node_filter/Cargo.toml index e885ef1d1..3abf86205 100644 --- a/ethcore/node_filter/Cargo.toml +++ b/ethcore/node_filter/Cargo.toml @@ -14,3 +14,4 @@ ethcore-network = { path = "../../util/network" } native-contracts = { path = "../native_contracts" } futures = "0.1" log = "0.3" +parking_lot = "0.4" diff --git a/ethcore/node_filter/src/lib.rs b/ethcore/node_filter/src/lib.rs index d3dcbaa3b..b5ed94bb9 100644 --- a/ethcore/node_filter/src/lib.rs +++ b/ethcore/node_filter/src/lib.rs @@ -21,6 +21,7 @@ extern crate ethcore_util as util; extern crate ethcore_network as network; extern crate native_contracts; extern crate futures; +extern crate parking_lot; #[cfg(test)] extern crate ethcore_io as io; #[macro_use] extern crate log; @@ -29,7 +30,8 @@ use std::collections::HashMap; use native_contracts::PeerSet as Contract; use network::{NodeId, ConnectionFilter, ConnectionDirection}; use ethcore::client::{BlockChainClient, BlockId, ChainNotify}; -use util::{Mutex, Address, H256, Bytes}; +use util::{Address, H256, Bytes}; +use parking_lot::Mutex; use futures::Future; const MAX_CACHE_SIZE: usize = 4096; diff --git a/ethcore/src/account_provider/mod.rs b/ethcore/src/account_provider/mod.rs index 752cec964..6f8eab7b7 100755 --- a/ethcore/src/account_provider/mod.rs +++ b/ethcore/src/account_provider/mod.rs @@ -23,7 +23,7 @@ use self::stores::{AddressBook, DappsSettingsStore, NewDappsPolicy}; use std::fmt; use std::collections::{HashMap, HashSet}; use std::time::{Instant, Duration}; -use util::{RwLock}; +use parking_lot::RwLock; use ethstore::{ SimpleSecretStore, SecretStore, Error as SSError, EthStore, EthMultiStore, random_string, SecretVaultRef, StoreAccountRef, OpaqueSecret, diff --git a/ethcore/src/blockchain/blockchain.rs b/ethcore/src/blockchain/blockchain.rs index ac51a789e..c4b39d180 100644 --- a/ethcore/src/blockchain/blockchain.rs +++ b/ethcore/src/blockchain/blockchain.rs @@ -22,6 +22,7 @@ use std::mem; use itertools::Itertools; use bloomchain as bc; use heapsize::HeapSizeOf; +use parking_lot::{Mutex, RwLock}; use util::*; use rlp::*; use header::*; diff --git a/ethcore/src/client/ancient_import.rs b/ethcore/src/client/ancient_import.rs index d4d8696d2..aaf24cdc1 100644 --- a/ethcore/src/client/ancient_import.rs +++ b/ethcore/src/client/ancient_import.rs @@ -23,7 +23,7 @@ use engines::{Engine, EpochVerifier}; use header::Header; use rand::Rng; -use util::RwLock; +use parking_lot::RwLock; // do "heavy" verification on ~1/50 blocks, randomly sampled. const HEAVY_VERIFY_RATE: f32 = 0.02; diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 69674901f..3ad909ae1 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -25,7 +25,7 @@ use itertools::Itertools; // util use hash::keccak; use timer::PerfTimer; -use util::{Bytes, Mutex, RwLock, MutexGuard}; +use util::Bytes; use util::{journaldb, DBValue, TrieFactory, Trie}; use util::{U256, H256, Address, H2048}; use util::trie::TrieSpec; @@ -56,6 +56,7 @@ use io::*; use log_entry::LocalizedLogEntry; use miner::{Miner, MinerService, TransactionImportResult}; use native_contracts::Registry; +use parking_lot::{Mutex, RwLock, MutexGuard}; use rand::OsRng; use receipt::{Receipt, LocalizedReceipt}; use rlp::UntrustedRlp; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 256d7953d..94e40e975 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -23,6 +23,7 @@ use std::mem; use itertools::Itertools; use rustc_hex::FromHex; use hash::keccak; +use parking_lot::RwLock; use util::*; use rlp::*; use ethkey::{Generator, Random}; diff --git a/ethcore/src/db.rs b/ethcore/src/db.rs index bccb8e943..e60b77b2e 100644 --- a/ethcore/src/db.rs +++ b/ethcore/src/db.rs @@ -19,7 +19,8 @@ use std::ops::Deref; use std::hash::Hash; use std::collections::HashMap; -use util::{DBTransaction, KeyValueDB, RwLock}; +use parking_lot::RwLock; +use util::{DBTransaction, KeyValueDB}; use rlp; diff --git a/ethcore/src/engines/authority_round/mod.rs b/ethcore/src/engines/authority_round/mod.rs index c9c483773..8868a0657 100644 --- a/ethcore/src/engines/authority_round/mod.rs +++ b/ethcore/src/engines/authority_round/mod.rs @@ -43,6 +43,7 @@ use io::{IoContext, IoHandler, TimerToken, IoService}; use itertools::{self, Itertools}; use rlp::{UntrustedRlp, encode}; use semantic_version::SemanticVersion; +use parking_lot::{Mutex, RwLock}; use util::*; mod finality; diff --git a/ethcore/src/engines/basic_authority.rs b/ethcore/src/engines/basic_authority.rs index ccd69b386..7847f0432 100644 --- a/ethcore/src/engines/basic_authority.rs +++ b/ethcore/src/engines/basic_authority.rs @@ -19,6 +19,7 @@ use std::sync::{Weak, Arc}; use std::collections::BTreeMap; use std::cmp; +use parking_lot::RwLock; use util::*; use ethkey::{recover, public_to_address, Signature}; use account_provider::AccountProvider; diff --git a/ethcore/src/engines/tendermint/mod.rs b/ethcore/src/engines/tendermint/mod.rs index f4b1422d8..71f5565e9 100644 --- a/ethcore/src/engines/tendermint/mod.rs +++ b/ethcore/src/engines/tendermint/mod.rs @@ -30,6 +30,7 @@ use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; use std::collections::{HashSet, BTreeMap, HashMap}; use hash::keccak; use std::cmp; +use parking_lot::RwLock; use util::*; use client::{Client, EngineClient}; use error::{Error, BlockError}; diff --git a/ethcore/src/engines/validator_set/contract.rs b/ethcore/src/engines/validator_set/contract.rs index ff16eb1d2..28414be0f 100644 --- a/ethcore/src/engines/validator_set/contract.rs +++ b/ethcore/src/engines/validator_set/contract.rs @@ -18,6 +18,7 @@ /// It can also report validators for misbehaviour with two levels: `reportMalicious` and `reportBenign`. use std::sync::Weak; +use parking_lot::RwLock; use util::*; use futures::Future; diff --git a/ethcore/src/engines/validator_set/multi.rs b/ethcore/src/engines/validator_set/multi.rs index 477a453b2..53617094a 100644 --- a/ethcore/src/engines/validator_set/multi.rs +++ b/ethcore/src/engines/validator_set/multi.rs @@ -19,7 +19,8 @@ use std::collections::BTreeMap; use std::sync::Weak; use engines::{Call, Engine}; -use util::{Bytes, H256, Address, RwLock}; +use parking_lot::RwLock; +use util::{Bytes, H256, Address}; use ids::BlockId; use header::{BlockNumber, Header}; use client::{Client, BlockChainClient}; diff --git a/ethcore/src/engines/validator_set/safe_contract.rs b/ethcore/src/engines/validator_set/safe_contract.rs index 6480a298e..dc186818a 100644 --- a/ethcore/src/engines/validator_set/safe_contract.rs +++ b/ethcore/src/engines/validator_set/safe_contract.rs @@ -21,6 +21,7 @@ use futures::Future; use native_contracts::ValidatorSet as Provider; use hash::keccak; +use parking_lot::RwLock; use util::*; use util::cache::MemoryLruCache; use rlp::{UntrustedRlp, RlpStream}; diff --git a/ethcore/src/engines/vote_collector.rs b/ethcore/src/engines/vote_collector.rs index 387715a03..8dd9f7c36 100644 --- a/ethcore/src/engines/vote_collector.rs +++ b/ethcore/src/engines/vote_collector.rs @@ -19,6 +19,7 @@ use std::fmt::Debug; use std::collections::{BTreeMap, HashSet, HashMap}; use std::hash::Hash; +use parking_lot:: RwLock; use util::*; use rlp::{Encodable, RlpStream}; diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index df7c25d3f..7d175ad7a 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -98,6 +98,7 @@ extern crate lru_cache; extern crate native_contracts; extern crate num_cpus; extern crate num; +extern crate parking_lot; extern crate price_info; extern crate rand; extern crate rlp; diff --git a/ethcore/src/miner/external.rs b/ethcore/src/miner/external.rs index 65f8ee8cc..c38bb16d4 100644 --- a/ethcore/src/miner/external.rs +++ b/ethcore/src/miner/external.rs @@ -17,7 +17,8 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::{Instant, Duration}; -use util::{Mutex, U256, H256}; +use util::{U256, H256}; +use parking_lot::Mutex; /// External miner interface. pub trait ExternalMinerService: Send + Sync { diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index fb1753760..6f5e5f23c 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -18,6 +18,7 @@ use std::time::{Instant, Duration}; use std::collections::{BTreeMap, HashSet}; use std::sync::Arc; +use parking_lot::{Mutex, RwLock}; use util::*; use timer::PerfTimer; use using_queue::{UsingQueue, GetAction}; diff --git a/ethcore/src/miner/service_transaction_checker.rs b/ethcore/src/miner/service_transaction_checker.rs index d21643772..a78f2a2c7 100644 --- a/ethcore/src/miner/service_transaction_checker.rs +++ b/ethcore/src/miner/service_transaction_checker.rs @@ -20,7 +20,8 @@ use types::ids::BlockId; use futures::{future, Future}; use native_contracts::ServiceTransactionChecker as Contract; -use util::{U256, Mutex}; +use parking_lot::Mutex; +use util::U256; const SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME: &'static str = "service_transaction_checker"; diff --git a/ethcore/src/miner/stratum.rs b/ethcore/src/miner/stratum.rs index 0031bb715..1586dc69f 100644 --- a/ethcore/src/miner/stratum.rs +++ b/ethcore/src/miner/stratum.rs @@ -28,7 +28,7 @@ use std::fmt; use util::{H256, U256, H64, clean_0x}; use ethereum::ethash::Ethash; use ethash::SeedHashCompute; -use util::Mutex; +use parking_lot::Mutex; use miner::{self, Miner, MinerService}; use client::Client; use block::IsBlock; diff --git a/ethcore/src/miner/work_notify.rs b/ethcore/src/miner/work_notify.rs index ff330d30c..160aa1f46 100644 --- a/ethcore/src/miner/work_notify.rs +++ b/ethcore/src/miner/work_notify.rs @@ -24,6 +24,7 @@ use hyper::{Next}; use hyper::net::HttpStream; use ethash::SeedHashCompute; use hyper::Url; +use parking_lot::Mutex; use util::*; use ethereum::ethash::Ethash; diff --git a/ethcore/src/snapshot/mod.rs b/ethcore/src/snapshot/mod.rs index 974b074cf..fa6ab8f06 100644 --- a/ethcore/src/snapshot/mod.rs +++ b/ethcore/src/snapshot/mod.rs @@ -31,7 +31,7 @@ use header::Header; use ids::BlockId; use util::{Bytes, HashDB, DBValue, snappy, U256}; -use util::Mutex; +use parking_lot::Mutex; use util::hash::{H256}; use util::journaldb::{self, Algorithm, JournalDB}; use util::kvdb::KeyValueDB; diff --git a/ethcore/src/snapshot/service.rs b/ethcore/src/snapshot/service.rs index 47bbb5997..24ce91d39 100644 --- a/ethcore/src/snapshot/service.rs +++ b/ethcore/src/snapshot/service.rs @@ -35,7 +35,8 @@ use service::ClientIoMessage; use io::IoChannel; -use util::{Bytes, H256, Mutex, RwLock, RwLockReadGuard, UtilError}; +use parking_lot::{Mutex, RwLock, RwLockReadGuard}; +use util::{Bytes, H256, UtilError}; use util::journaldb::Algorithm; use util::kvdb::{Database, DatabaseConfig}; use util::snappy; diff --git a/ethcore/src/snapshot/tests/proof_of_work.rs b/ethcore/src/snapshot/tests/proof_of_work.rs index 907fcead8..428023a5c 100644 --- a/ethcore/src/snapshot/tests/proof_of_work.rs +++ b/ethcore/src/snapshot/tests/proof_of_work.rs @@ -24,7 +24,8 @@ use blockchain::BlockChain; use snapshot::{chunk_secondary, Error as SnapshotError, Progress, SnapshotComponents}; use snapshot::io::{PackedReader, PackedWriter, SnapshotReader, SnapshotWriter}; -use util::{Mutex, snappy}; +use parking_lot::Mutex; +use util::snappy; use util::kvdb::{self, KeyValueDB, DBTransaction}; use std::sync::Arc; diff --git a/ethcore/src/snapshot/tests/state.rs b/ethcore/src/snapshot/tests/state.rs index 9a98af5ff..4e2e70a6f 100644 --- a/ethcore/src/snapshot/tests/state.rs +++ b/ethcore/src/snapshot/tests/state.rs @@ -29,7 +29,7 @@ use util::hash::H256; use util::journaldb::{self, Algorithm}; use util::kvdb::{Database, DatabaseConfig}; use util::memorydb::MemoryDB; -use util::Mutex; +use parking_lot::Mutex; use devtools::RandomTempPath; use std::sync::Arc; diff --git a/ethcore/src/snapshot/watcher.rs b/ethcore/src/snapshot/watcher.rs index cff9be18c..89f8b62df 100644 --- a/ethcore/src/snapshot/watcher.rs +++ b/ethcore/src/snapshot/watcher.rs @@ -16,7 +16,7 @@ //! Watcher for snapshot-related chain events. -use util::Mutex; +use parking_lot::Mutex; use client::{BlockChainClient, Client, ChainNotify}; use ids::BlockId; use service::ClientIoMessage; diff --git a/ethcore/src/spec/spec.rs b/ethcore/src/spec/spec.rs index e93703871..98f04da6f 100644 --- a/ethcore/src/spec/spec.rs +++ b/ethcore/src/spec/spec.rs @@ -40,6 +40,7 @@ use state_db::StateDB; use state::{Backend, State, Substate}; use state::backend::Basic as BasicBackend; use trace::{NoopTracer, NoopVMTracer}; +use parking_lot::RwLock; use util::*; /// Parameters common to ethereum-like blockchains. diff --git a/ethcore/src/state/backend.rs b/ethcore/src/state/backend.rs index ea172b5af..ffe63f423 100644 --- a/ethcore/src/state/backend.rs +++ b/ethcore/src/state/backend.rs @@ -25,7 +25,8 @@ use std::collections::{HashSet, HashMap}; use std::sync::Arc; use state::Account; -use util::{Address, MemoryDB, Mutex, H256}; +use parking_lot::Mutex; +use util::{Address, MemoryDB, H256}; use util::hashdb::{AsHashDB, HashDB, DBValue}; /// State backend. See module docs for more details. diff --git a/ethcore/src/state_db.rs b/ethcore/src/state_db.rs index ce96dc4fc..dbba01125 100644 --- a/ethcore/src/state_db.rs +++ b/ethcore/src/state_db.rs @@ -25,7 +25,8 @@ use util::hashdb::HashDB; use state::{self, Account}; use header::BlockNumber; use hash::keccak; -use util::{Address, DBTransaction, UtilError, Mutex}; +use parking_lot::Mutex; +use util::{Address, DBTransaction, UtilError}; use bloom_journal::{Bloom, BloomJournal}; use db::COL_ACCOUNT_BLOOM; use byteorder::{LittleEndian, ByteOrder}; diff --git a/ethcore/src/trace/db.rs b/ethcore/src/trace/db.rs index 0fe057a64..d7055a547 100644 --- a/ethcore/src/trace/db.rs +++ b/ethcore/src/trace/db.rs @@ -21,7 +21,8 @@ use std::sync::Arc; use bloomchain::{Number, Config as BloomConfig}; use bloomchain::group::{BloomGroupDatabase, BloomGroupChain, GroupPosition, BloomGroup}; use heapsize::HeapSizeOf; -use util::{H256, H264, KeyValueDB, DBTransaction, RwLock}; +use parking_lot::RwLock; +use util::{H256, H264, KeyValueDB, DBTransaction}; use header::BlockNumber; use trace::{LocalizedTrace, Config, Filter, Database as TraceDatabase, ImportRequest, DatabaseExtras}; use db::{self, Key, Writable, Readable, CacheUpdatePolicy}; diff --git a/ethcore/src/verification/queue/mod.rs b/ethcore/src/verification/queue/mod.rs index 8fc4a5919..ab019a216 100644 --- a/ethcore/src/verification/queue/mod.rs +++ b/ethcore/src/verification/queue/mod.rs @@ -23,6 +23,7 @@ use std::sync::{Condvar as SCondvar, Mutex as SMutex, Arc}; use std::cmp; use std::collections::{VecDeque, HashSet, HashMap}; use heapsize::HeapSizeOf; +use parking_lot::{Condvar, Mutex, RwLock}; use util::*; use io::*; use error::*; diff --git a/hash-fetch/Cargo.toml b/hash-fetch/Cargo.toml index e87ffea1a..884729a69 100644 --- a/hash-fetch/Cargo.toml +++ b/hash-fetch/Cargo.toml @@ -14,6 +14,7 @@ mime = "0.2" mime_guess = "1.6.1" rand = "0.3" rustc-hex = "1.0" +parking_lot = "0.4" fetch = { path = "../util/fetch" } ethcore-util = { path = "../util" } parity-reactor = { path = "../util/reactor" } diff --git a/hash-fetch/src/client.rs b/hash-fetch/src/client.rs index d63df794a..6ebfdcb5c 100644 --- a/hash-fetch/src/client.rs +++ b/hash-fetch/src/client.rs @@ -191,7 +191,7 @@ fn random_temp_path() -> PathBuf { mod tests { use rustc_hex::FromHex; use std::sync::{Arc, mpsc}; - use util::Mutex; + use parking_lot::Mutex; use futures::future; use fetch::{self, Fetch}; use parity_reactor::Remote; diff --git a/hash-fetch/src/lib.rs b/hash-fetch/src/lib.rs index 987770252..c09af0c89 100644 --- a/hash-fetch/src/lib.rs +++ b/hash-fetch/src/lib.rs @@ -29,6 +29,7 @@ extern crate futures; extern crate mime_guess; extern crate native_contracts; extern crate parity_reactor; +extern crate parking_lot; extern crate rand; extern crate rustc_hex; extern crate hash; diff --git a/hash-fetch/src/urlhint.rs b/hash-fetch/src/urlhint.rs index 3fa32eb15..945d20d72 100644 --- a/hash-fetch/src/urlhint.rs +++ b/hash-fetch/src/urlhint.rs @@ -216,7 +216,8 @@ pub mod tests { use super::*; use super::guess_mime_type; - use util::{Bytes, Address, Mutex, ToPretty}; + use parking_lot::Mutex; + use util::{Bytes, Address, ToPretty}; pub struct FakeRegistrar { pub calls: Arc>>, diff --git a/parity/blockchain.rs b/parity/blockchain.rs index a94b387d4..266be7dfd 100644 --- a/parity/blockchain.rs +++ b/parity/blockchain.rs @@ -155,6 +155,7 @@ pub fn execute(cmd: BlockchainCmd) -> Result<(), String> { fn execute_import_light(cmd: ImportBlockchain) -> Result<(), String> { use light::client::{Service as LightClientService, Config as LightClientConfig}; use light::cache::Cache as LightDataCache; + use parking_lot::Mutex; let timer = Instant::now(); @@ -188,7 +189,7 @@ fn execute_import_light(cmd: ImportBlockchain) -> Result<(), String> { // create dirs used by parity cmd.dirs.create_dirs(false, false, false)?; - let cache = Arc::new(::util::Mutex::new( + let cache = Arc::new(Mutex::new( LightDataCache::new(Default::default(), ::time::Duration::seconds(0)) )); diff --git a/parity/informant.rs b/parity/informant.rs index 4e2987892..166e36a62 100644 --- a/parity/informant.rs +++ b/parity/informant.rs @@ -35,7 +35,8 @@ use light::client::LightChainClient; use number_prefix::{binary_prefix, Standalone, Prefixed}; use parity_rpc::{is_major_importing}; use parity_rpc::informant::RpcStats; -use util::{RwLock, Mutex, H256, Bytes}; +use parking_lot::{RwLock, Mutex}; +use util::{H256, Bytes}; /// Format byte counts to standard denominations. pub fn format_bytes(b: usize) -> String { diff --git a/parity/light_helpers/queue_cull.rs b/parity/light_helpers/queue_cull.rs index 982e69e48..eaf0ca9c5 100644 --- a/parity/light_helpers/queue_cull.rs +++ b/parity/light_helpers/queue_cull.rs @@ -31,7 +31,7 @@ use futures::{future, Future}; use parity_reactor::Remote; -use util::RwLock; +use parking_lot::RwLock; // Attepmt to cull once every 10 minutes. const TOKEN: TimerToken = 1; diff --git a/parity/main.rs b/parity/main.rs index 00c219963..485101b66 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -34,6 +34,7 @@ extern crate isatty; extern crate jsonrpc_core; extern crate num_cpus; extern crate number_prefix; +extern crate parking_lot; extern crate regex; extern crate rlp; extern crate rpassword; diff --git a/parity/rpc_apis.rs b/parity/rpc_apis.rs index 7948d9b20..d3171f381 100644 --- a/parity/rpc_apis.rs +++ b/parity/rpc_apis.rs @@ -38,7 +38,7 @@ use parity_rpc::dispatch::{FullDispatcher, LightDispatcher}; use parity_rpc::informant::{ActivityNotifier, ClientNotifier}; use parity_rpc::{Metadata, NetworkSettings}; use updater::Updater; -use util::{Mutex, RwLock}; +use parking_lot::{Mutex, RwLock}; #[derive(Debug, PartialEq, Clone, Eq, Hash)] pub enum Api { diff --git a/parity/run.rs b/parity/run.rs index 093a019f3..c9c1283ca 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -37,8 +37,9 @@ use node_health; use parity_reactor::EventLoop; use parity_rpc::{NetworkSettings, informant, is_major_importing}; use updater::{UpdatePolicy, Updater}; -use util::{version, Mutex, Condvar}; use ansi_term::Colour; +use util::version; +use parking_lot::{Condvar, Mutex}; use node_filter::NodeFilter; use params::{ @@ -171,7 +172,7 @@ impl ::local_store::NodeInfo for FullNodeInfo { fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc) -> Result<(bool, Option), String> { use light::client as light_client; use ethsync::{LightSyncParams, LightSync, ManageNetwork}; - use util::RwLock; + use parking_lot::{Mutex, RwLock}; // load spec let spec = cmd.spec.spec(&cmd.dirs.cache)?; @@ -206,7 +207,7 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc) -> // TODO: configurable cache size. let cache = LightDataCache::new(Default::default(), ::time::Duration::minutes(GAS_CORPUS_EXPIRATION_MINUTES)); - let cache = Arc::new(::util::Mutex::new(cache)); + let cache = Arc::new(Mutex::new(cache)); // start client and create transaction queue. let mut config = light_client::Config { diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index a0bbe95ca..daa6cf3ce 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -15,6 +15,7 @@ futures-cpupool = "0.1" log = "0.3" multihash ="0.6" order-stat = "0.1" +parking_lot = "0.4" rand = "0.3" rust-crypto = "0.2" rustc-hex = "1.0" diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 22f623bf6..b6e9c28fe 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -28,6 +28,7 @@ extern crate futures_cpupool; extern crate itertools; extern crate multihash; extern crate order_stat; +extern crate parking_lot; extern crate rand; extern crate rustc_hex; extern crate semver; diff --git a/rpc/src/v1/helpers/dispatch.rs b/rpc/src/v1/helpers/dispatch.rs index 41fc201cf..96e2c8b4e 100644 --- a/rpc/src/v1/helpers/dispatch.rs +++ b/rpc/src/v1/helpers/dispatch.rs @@ -27,7 +27,8 @@ use light::on_demand::{request, OnDemand}; use light::TransactionQueue as LightTransactionQueue; use rlp; use hash::keccak; -use util::{Address, H520, H256, U256, Bytes, Mutex, RwLock}; +use util::{Address, H520, H256, U256, Bytes}; +use parking_lot::{Mutex, RwLock}; use stats::Corpus; use ethkey::Signature; diff --git a/rpc/src/v1/helpers/light_fetch.rs b/rpc/src/v1/helpers/light_fetch.rs index 972315272..b46de402f 100644 --- a/rpc/src/v1/helpers/light_fetch.rs +++ b/rpc/src/v1/helpers/light_fetch.rs @@ -37,7 +37,8 @@ use light::on_demand::{request, OnDemand, HeaderRef, Request as OnDemandRequest, use light::request::Field; use ethsync::LightSync; -use util::{Address, Mutex, U256}; +use util::{Address, U256}; +use parking_lot::Mutex; use v1::helpers::{CallRequest as CallRequestHelper, errors, dispatch}; use v1::types::{BlockNumber, CallRequest, Log}; diff --git a/rpc/src/v1/helpers/signer.rs b/rpc/src/v1/helpers/signer.rs index 81d21eb82..6d9606f87 100644 --- a/rpc/src/v1/helpers/signer.rs +++ b/rpc/src/v1/helpers/signer.rs @@ -17,7 +17,7 @@ use std::sync::Arc; use std::ops::Deref; use http::Origin; -use util::Mutex; +use parking_lot::Mutex; use transient_hashmap::TransientHashMap; use ethstore::random_string; diff --git a/rpc/src/v1/helpers/signing_queue.rs b/rpc/src/v1/helpers/signing_queue.rs index e9e6313f4..6dba2f7a2 100644 --- a/rpc/src/v1/helpers/signing_queue.rs +++ b/rpc/src/v1/helpers/signing_queue.rs @@ -19,7 +19,8 @@ use std::cell::RefCell; use std::sync::Arc; use std::collections::BTreeMap; use jsonrpc_core; -use util::{Mutex, RwLock, U256, Address}; +use parking_lot::{Mutex, RwLock}; +use util::{U256, Address}; use ethcore::account_provider::DappId; use v1::helpers::{ConfirmationRequest, ConfirmationPayload}; use v1::types::{ConfirmationResponse, H160 as RpcH160, Origin, DappId as RpcDappId}; @@ -297,7 +298,8 @@ mod test { use std::time::Duration; use std::thread; use std::sync::{mpsc, Arc}; - use util::{Address, U256, Mutex}; + use parking_lot::Mutex; + use util::{Address, U256}; use v1::helpers::{SigningQueue, ConfirmationsQueue, QueueEvent, FilledTransactionRequest, ConfirmationPayload}; use v1::types::ConfirmationResponse; diff --git a/rpc/src/v1/helpers/subscription_manager.rs b/rpc/src/v1/helpers/subscription_manager.rs index c5e4216f4..f529d4810 100644 --- a/rpc/src/v1/helpers/subscription_manager.rs +++ b/rpc/src/v1/helpers/subscription_manager.rs @@ -18,7 +18,7 @@ use std::sync::Arc; use std::sync::atomic::{self, AtomicBool}; -use util::Mutex; +use parking_lot::Mutex; use jsonrpc_core::futures::future::{self, Either}; use jsonrpc_core::futures::sync::mpsc; diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index f31d32426..30afe4771 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -24,7 +24,7 @@ use futures::{self, future, BoxFuture, Future}; use rlp::{self, UntrustedRlp}; use time::get_time; use util::{H160, H256, Address, U256, H64}; -use util::Mutex; +use parking_lot::Mutex; use ethash::SeedHashCompute; use ethcore::account_provider::{AccountProvider, DappId}; diff --git a/rpc/src/v1/impls/eth_filter.rs b/rpc/src/v1/impls/eth_filter.rs index 8f448feb5..ee8e27778 100644 --- a/rpc/src/v1/impls/eth_filter.rs +++ b/rpc/src/v1/impls/eth_filter.rs @@ -23,7 +23,8 @@ use jsonrpc_core::*; use ethcore::miner::MinerService; use ethcore::filter::Filter as EthcoreFilter; use ethcore::client::{BlockChainClient, BlockId}; -use util::{H256, Mutex}; +use util::H256; +use parking_lot::Mutex; use futures::{future, Future, BoxFuture}; diff --git a/rpc/src/v1/impls/eth_pubsub.rs b/rpc/src/v1/impls/eth_pubsub.rs index f918a44ea..4eff9cdaa 100644 --- a/rpc/src/v1/impls/eth_pubsub.rs +++ b/rpc/src/v1/impls/eth_pubsub.rs @@ -39,7 +39,8 @@ use light::cache::Cache; use light::on_demand::OnDemand; use light::client::{LightChainClient, LightChainNotify}; use parity_reactor::Remote; -use util::{RwLock, Mutex, H256, Bytes}; +use util::{H256, Bytes}; +use parking_lot::{RwLock, Mutex}; type Client = Sink; diff --git a/rpc/src/v1/impls/light/eth.rs b/rpc/src/v1/impls/light/eth.rs index 7e40bf71a..c9a0b9999 100644 --- a/rpc/src/v1/impls/light/eth.rs +++ b/rpc/src/v1/impls/light/eth.rs @@ -39,7 +39,8 @@ use ethcore::transaction::{Action, SignedTransaction, Transaction as EthTransact use ethsync::LightSync; use rlp::UntrustedRlp; use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; -use util::{RwLock, Mutex, U256}; +use util::U256; +use parking_lot::{RwLock, Mutex}; use futures::{future, Future, BoxFuture, IntoFuture}; use futures::sync::oneshot; diff --git a/rpc/src/v1/impls/pubsub.rs b/rpc/src/v1/impls/pubsub.rs index 616905f4a..44b9fcbeb 100644 --- a/rpc/src/v1/impls/pubsub.rs +++ b/rpc/src/v1/impls/pubsub.rs @@ -18,7 +18,7 @@ use std::sync::Arc; use std::time::Duration; -use util::RwLock; +use parking_lot::RwLock; use futures::{self, BoxFuture, Future, Stream, Sink}; use jsonrpc_core::{self as core, Error, MetaIoHandler}; diff --git a/rpc/src/v1/impls/signer.rs b/rpc/src/v1/impls/signer.rs index 79f4f0e38..a01b66364 100644 --- a/rpc/src/v1/impls/signer.rs +++ b/rpc/src/v1/impls/signer.rs @@ -24,7 +24,7 @@ use ethkey; use futures::{future, BoxFuture, Future, IntoFuture}; use parity_reactor::Remote; use rlp::UntrustedRlp; -use util::Mutex; +use parking_lot::Mutex; use jsonrpc_core::{futures, Error}; use jsonrpc_pubsub::SubscriptionId; diff --git a/rpc/src/v1/impls/signing.rs b/rpc/src/v1/impls/signing.rs index 23ce3c78a..a7ef5cc10 100644 --- a/rpc/src/v1/impls/signing.rs +++ b/rpc/src/v1/impls/signing.rs @@ -18,7 +18,8 @@ use std::sync::Arc; use transient_hashmap::TransientHashMap; -use util::{U256, Mutex}; +use parking_lot::Mutex; +use util::U256; use ethcore::account_provider::AccountProvider; diff --git a/rpc/src/v1/informant.rs b/rpc/src/v1/informant.rs index 160f0ea9f..0cbe7d449 100644 --- a/rpc/src/v1/informant.rs +++ b/rpc/src/v1/informant.rs @@ -24,7 +24,7 @@ use futures::Future; use futures_cpupool as pool; use jsonrpc_core as rpc; use order_stat; -use util::RwLock; +use parking_lot::RwLock; pub use self::pool::CpuPool; diff --git a/rpc/src/v1/tests/helpers/miner_service.rs b/rpc/src/v1/tests/helpers/miner_service.rs index 7139d636b..5670824dc 100644 --- a/rpc/src/v1/tests/helpers/miner_service.rs +++ b/rpc/src/v1/tests/helpers/miner_service.rs @@ -18,7 +18,8 @@ use std::collections::{BTreeMap, HashMap}; use std::collections::hash_map::Entry; -use util::{Address, H256, Bytes, U256, RwLock, Mutex}; +use util::{Address, H256, Bytes, U256}; +use parking_lot::{RwLock, Mutex}; use ethcore::error::Error; use ethcore::client::MiningBlockChainClient; use ethcore::block::ClosedBlock; diff --git a/rpc/src/v1/tests/helpers/snapshot_service.rs b/rpc/src/v1/tests/helpers/snapshot_service.rs index cfb80619a..6d2b71aea 100644 --- a/rpc/src/v1/tests/helpers/snapshot_service.rs +++ b/rpc/src/v1/tests/helpers/snapshot_service.rs @@ -16,7 +16,8 @@ use ethcore::snapshot::{ManifestData, RestorationStatus, SnapshotService}; -use util::{Bytes, Mutex}; +use parking_lot::Mutex; +use util::Bytes; use util::hash::H256; /// Mocked snapshot service (used for sync info extensions). diff --git a/rpc/src/v1/tests/helpers/sync_provider.rs b/rpc/src/v1/tests/helpers/sync_provider.rs index 83c7db015..4498fa85e 100644 --- a/rpc/src/v1/tests/helpers/sync_provider.rs +++ b/rpc/src/v1/tests/helpers/sync_provider.rs @@ -17,7 +17,8 @@ //! Test implementation of SyncProvider. use std::collections::BTreeMap; -use util::{H256, RwLock}; +use parking_lot::RwLock; +use util::H256; use ethsync::{SyncProvider, EthProtocolInfo, SyncStatus, SyncState, PeerInfo, TransactionStats}; /// TestSyncProvider config. diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index d1835948f..3338cd1bd 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -22,7 +22,8 @@ use rustc_hex::{FromHex, ToHex}; use time::get_time; use rlp; -use util::{U256, Address, H256, Mutex}; +use util::{U256, Address, H256}; +use parking_lot::Mutex; use ethkey::Secret; use ethcore::account_provider::AccountProvider; use ethcore::client::{TestBlockChainClient, EachBlockWith, Executed, TransactionId}; diff --git a/rpc_client/Cargo.toml b/rpc_client/Cargo.toml index 1e39d8b1e..1f1840848 100644 --- a/rpc_client/Cargo.toml +++ b/rpc_client/Cargo.toml @@ -15,6 +15,7 @@ serde_json = "1.0" tempdir = "0.3.5" url = "1.2.0" matches = "0.1" +parking_lot = "0.4" jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" } jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" } parity-rpc = { path = "../rpc" } diff --git a/rpc_client/src/client.rs b/rpc_client/src/client.rs index 9c6c4ebe8..27b72ab78 100644 --- a/rpc_client/src/client.rs +++ b/rpc_client/src/client.rs @@ -10,7 +10,7 @@ use std::time; use std::path::PathBuf; use hash::keccak; -use util::Mutex; +use parking_lot::Mutex; use url::Url; use std::fs::File; diff --git a/rpc_client/src/lib.rs b/rpc_client/src/lib.rs index 805754b22..7459ca9a4 100644 --- a/rpc_client/src/lib.rs +++ b/rpc_client/src/lib.rs @@ -6,6 +6,7 @@ extern crate futures; extern crate jsonrpc_core; extern crate jsonrpc_ws_server as ws; extern crate parity_rpc as rpc; +extern crate parking_lot; extern crate rand; extern crate serde; extern crate serde_json; diff --git a/stratum/Cargo.toml b/stratum/Cargo.toml index 9bf75779f..ffd933a59 100644 --- a/stratum/Cargo.toml +++ b/stratum/Cargo.toml @@ -23,5 +23,6 @@ semver = "0.6" ethcore-ipc-nano = { path = "../ipc/nano" } futures = "0.1" tokio-core = "0.1" +parking_lot = "0.4" ethcore-logger = { path = "../logger" } hash = { path = "../util/hash" } diff --git a/stratum/src/lib.rs b/stratum/src/lib.rs index 0cffd67b6..2649436dd 100644 --- a/stratum/src/lib.rs +++ b/stratum/src/lib.rs @@ -26,6 +26,7 @@ extern crate semver; extern crate futures; extern crate ethcore_logger; extern crate hash; +extern crate parking_lot; #[cfg(test)] extern crate tokio_core; extern crate ethcore_devtools as devtools; @@ -56,7 +57,8 @@ use std::sync::Arc; use std::net::SocketAddr; use std::collections::{HashSet, HashMap}; use hash::keccak; -use util::{H256, RwLock, RwLockReadGuard}; +use parking_lot::{RwLock, RwLockReadGuard}; +use util::H256; type RpcResult = BoxFuture; diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 432a0d0f5..822ae143d 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -93,6 +93,7 @@ use std::collections::{HashSet, HashMap}; use std::cmp; use hash::keccak; use heapsize::HeapSizeOf; +use parking_lot::RwLock; use util::*; use rlp::*; use network::*; @@ -2230,7 +2231,8 @@ mod tests { use network::PeerId; use tests::helpers::*; use tests::snapshot::TestSnapshotService; - use util::{U256, Address, RwLock}; + use parking_lot::RwLock; + use util::{U256, Address}; use util::hash::H256; use util::bytes::Bytes; use rlp::{Rlp, RlpStream, UntrustedRlp}; diff --git a/sync/src/light_sync/mod.rs b/sync/src/light_sync/mod.rs index f05e4d7d0..12b6ca2a8 100644 --- a/sync/src/light_sync/mod.rs +++ b/sync/src/light_sync/mod.rs @@ -45,7 +45,8 @@ use light::net::{ }; use light::request::{self, CompleteHeadersRequest as HeadersRequest}; use network::PeerId; -use util::{U256, H256, Mutex, RwLock}; +use util::{U256, H256}; +use parking_lot::{Mutex, RwLock}; use rand::{Rng, OsRng}; use self::sync_round::{AbortReason, SyncRound, ResponseContext}; diff --git a/sync/src/light_sync/tests/test_net.rs b/sync/src/light_sync/tests/test_net.rs index f6d5eddf0..65ddf92da 100644 --- a/sync/src/light_sync/tests/test_net.rs +++ b/sync/src/light_sync/tests/test_net.rs @@ -29,7 +29,7 @@ use light::client::Client as LightClient; use light::net::{LightProtocol, IoContext, Capabilities, Params as LightParams}; use light::provider::LightProvider; use network::{NodeId, PeerId}; -use util::RwLock; +use parking_lot::RwLock; use time::Duration; use light::cache::Cache; diff --git a/sync/src/tests/helpers.rs b/sync/src/tests/helpers.rs index 3ac68b0fb..f41d630e6 100644 --- a/sync/src/tests/helpers.rs +++ b/sync/src/tests/helpers.rs @@ -16,6 +16,7 @@ use std::collections::{VecDeque, HashSet, HashMap}; use std::sync::Arc; +use parking_lot::RwLock; use util::*; use network::*; use tests::snapshot::*; diff --git a/sync/src/tests/snapshot.rs b/sync/src/tests/snapshot.rs index d49bcf440..ba68f6143 100644 --- a/sync/src/tests/snapshot.rs +++ b/sync/src/tests/snapshot.rs @@ -17,6 +17,7 @@ use std::collections::HashMap; use std::sync::Arc; use hash::keccak; +use parking_lot::Mutex; use util::*; use ethcore::snapshot::{SnapshotService, ManifestData, RestorationStatus}; use ethcore::header::BlockNumber; diff --git a/updater/Cargo.toml b/updater/Cargo.toml index bb6557f6d..90dcbfeab 100644 --- a/updater/Cargo.toml +++ b/updater/Cargo.toml @@ -17,6 +17,7 @@ ethcore = { path = "../ethcore" } ethsync = { path = "../sync" } ethcore-util = { path = "../util" } futures = "0.1" +parking_lot = "0.4" parity-hash-fetch = { path = "../hash-fetch" } ipc-common-types = { path = "../ipc-common-types" } ethcore-ipc = { path = "../ipc/rpc" } diff --git a/updater/src/lib.rs b/updater/src/lib.rs index b27f2039e..64b67b2dc 100644 --- a/updater/src/lib.rs +++ b/updater/src/lib.rs @@ -19,6 +19,7 @@ #[macro_use] extern crate log; extern crate ethcore_util as util; extern crate ipc_common_types; +extern crate parking_lot; extern crate parity_hash_fetch as hash_fetch; extern crate ethcore; extern crate ethabi; diff --git a/updater/src/updater.rs b/updater/src/updater.rs index 817688688..5d2bb1177 100644 --- a/updater/src/updater.rs +++ b/updater/src/updater.rs @@ -31,7 +31,8 @@ use path::restrict_permissions_owner; use service::{Service}; use target_info::Target; use types::all::{ReleaseInfo, OperationsInfo, CapState}; -use util::{Address, H160, H256, Mutex, Bytes}; +use parking_lot::Mutex; +use util::{Address, H160, H256, Bytes}; use util::misc; /// Filter for releases. diff --git a/util/network/src/tests.rs b/util/network/src/tests.rs index d743318ab..f6c8ed1e8 100644 --- a/util/network/src/tests.rs +++ b/util/network/src/tests.rs @@ -19,7 +19,8 @@ use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::Arc; use std::thread; use std::time::*; -use util::{Bytes, Mutex}; +use parking_lot::Mutex; +use util::Bytes; use io::TimerToken; use ethkey::{Random, Generator}; diff --git a/util/src/lib.rs b/util/src/lib.rs index 305b17220..61fc616da 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -145,7 +145,5 @@ pub use vector::*; pub use bigint::prelude::*; pub use bigint::hash; -pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; - /// 160-bit integer representing account address pub type Address = H160; From b731ccea1843560ff436f563c2b6906d2d12cdb2 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 3 Sep 2017 09:11:14 +0200 Subject: [PATCH 16/16] triehash is separated from util (#6428) --- Cargo.lock | 13 ++ ethcore/Cargo.toml | 1 + ethcore/light/Cargo.toml | 1 + ethcore/light/src/cht.rs | 2 +- ethcore/light/src/lib.rs | 1 + ethcore/light/src/on_demand/request.rs | 6 +- ethcore/src/block.rs | 3 +- .../engines/validator_set/safe_contract.rs | 2 +- ethcore/src/lib.rs | 1 + ethcore/src/pod_account.rs | 1 + ethcore/src/pod_state.rs | 1 + ethcore/src/snapshot/block.rs | 4 +- ethcore/src/snapshot/consensus/work.rs | 2 +- ethcore/src/verification/verification.rs | 2 + sync/Cargo.toml | 1 + sync/src/blocks.rs | 1 + sync/src/lib.rs | 1 + util/Cargo.toml | 1 + util/benches/trie.rs | 3 +- util/src/lib.rs | 4 - util/src/trie/triedbmut.rs | 3 +- util/src/vector.rs | 68 --------- util/triehash/Cargo.toml | 9 ++ util/{src/triehash.rs => triehash/src/lib.rs} | 132 ++++++++++-------- 24 files changed, 124 insertions(+), 139 deletions(-) delete mode 100644 util/src/vector.rs create mode 100644 util/triehash/Cargo.toml rename util/{src/triehash.rs => triehash/src/lib.rs} (79%) diff --git a/Cargo.lock b/Cargo.lock index b126d3d35..d53c197cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -543,6 +543,7 @@ dependencies = [ "table 0.1.0", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "transient-hashmap 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "triehash 0.1.0", "using_queue 0.1.0", "vm 0.1.0", "wasm 0.1.0", @@ -672,6 +673,7 @@ dependencies = [ "smallvec 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "stats 0.1.0", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "triehash 0.1.0", "vm 0.1.0", ] @@ -807,6 +809,7 @@ dependencies = [ "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "tiny-keccak 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "triehash 0.1.0", "vergen 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -922,6 +925,7 @@ dependencies = [ "semver 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "triehash 0.1.0", ] [[package]] @@ -3106,6 +3110,15 @@ dependencies = [ "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "triehash" +version = "0.1.0" +dependencies = [ + "ethcore-bigint 0.1.3", + "hash 0.1.0", + "rlp 0.2.0", +] + [[package]] name = "typeable" version = "0.1.2" diff --git a/ethcore/Cargo.toml b/ethcore/Cargo.toml index 5b37d96ca..71326e163 100644 --- a/ethcore/Cargo.toml +++ b/ethcore/Cargo.toml @@ -63,6 +63,7 @@ bloomable = { path = "../util/bloomable" } vm = { path = "vm" } wasm = { path = "wasm" } hash = { path = "../util/hash" } +triehash = { path = "../util/triehash" } semantic_version = { path = "../util/semantic_version" } [dev-dependencies] diff --git a/ethcore/light/Cargo.toml b/ethcore/light/Cargo.toml index 46ae3afe0..4232413fb 100644 --- a/ethcore/light/Cargo.toml +++ b/ethcore/light/Cargo.toml @@ -34,6 +34,7 @@ serde_derive = "1.0" parking_lot = "0.4" stats = { path = "../../util/stats" } hash = { path = "../../util/hash" } +triehash = { path = "../../util/triehash" } [features] default = [] diff --git a/ethcore/light/src/cht.rs b/ethcore/light/src/cht.rs index 7f2ccc3a8..78b45cafc 100644 --- a/ethcore/light/src/cht.rs +++ b/ethcore/light/src/cht.rs @@ -130,7 +130,7 @@ pub fn compute_root(cht_num: u64, iterable: I) -> Option } if v.len() == SIZE as usize { - Some(::util::triehash::trie_root(v)) + Some(::triehash::trie_root(v)) } else { None } diff --git a/ethcore/light/src/lib.rs b/ethcore/light/src/lib.rs index 5d9a5ae32..b7842e055 100644 --- a/ethcore/light/src/lib.rs +++ b/ethcore/light/src/lib.rs @@ -86,6 +86,7 @@ extern crate stats; extern crate time; extern crate vm; extern crate hash; +extern crate triehash; #[cfg(feature = "ipc")] extern crate ethcore_ipc as ipc; diff --git a/ethcore/light/src/on_demand/request.rs b/ethcore/light/src/on_demand/request.rs index 3a38ec974..2dae37555 100644 --- a/ethcore/light/src/on_demand/request.rs +++ b/ethcore/light/src/on_demand/request.rs @@ -709,7 +709,7 @@ impl Body { pub fn check_response(&self, cache: &Mutex<::cache::Cache>, body: &encoded::Body) -> Result { // check the integrity of the the body against the header let header = self.0.as_ref()?; - let tx_root = ::util::triehash::ordered_trie_root(body.rlp().at(0).iter().map(|r| r.as_raw().to_vec())); + let tx_root = ::triehash::ordered_trie_root(body.rlp().at(0).iter().map(|r| r.as_raw().to_vec())); if tx_root != header.transactions_root() { return Err(Error::WrongTrieRoot(header.transactions_root(), tx_root)); } @@ -739,7 +739,7 @@ impl BlockReceipts { /// Check a response with receipts against the stored header. pub fn check_response(&self, cache: &Mutex<::cache::Cache>, receipts: &[Receipt]) -> Result, Error> { let receipts_root = self.0.as_ref()?.receipts_root(); - let found_root = ::util::triehash::ordered_trie_root(receipts.iter().map(|r| ::rlp::encode(r).into_vec())); + let found_root = ::triehash::ordered_trie_root(receipts.iter().map(|r| ::rlp::encode(r).into_vec())); match receipts_root == found_root { true => { @@ -936,7 +936,7 @@ mod tests { }).collect::>(); let mut header = Header::new(); - let receipts_root = ::util::triehash::ordered_trie_root( + let receipts_root = ::triehash::ordered_trie_root( receipts.iter().map(|x| ::rlp::encode(x).into_vec()) ); diff --git a/ethcore/src/block.rs b/ethcore/src/block.rs index 5181dff1f..a48dfbcb7 100644 --- a/ethcore/src/block.rs +++ b/ethcore/src/block.rs @@ -20,9 +20,10 @@ use std::cmp; use std::sync::Arc; use std::collections::HashSet; use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; +use triehash::ordered_trie_root; use rlp::{UntrustedRlp, RlpStream, Encodable, Decodable, DecoderError}; -use util::{Bytes, Address, U256, H256, ordered_trie_root}; +use util::{Bytes, Address, U256, H256}; use util::error::{Mismatch, OutOfBounds}; use basic_types::{LogBloom, Seal}; diff --git a/ethcore/src/engines/validator_set/safe_contract.rs b/ethcore/src/engines/validator_set/safe_contract.rs index dc186818a..82fb448d9 100644 --- a/ethcore/src/engines/validator_set/safe_contract.rs +++ b/ethcore/src/engines/validator_set/safe_contract.rs @@ -353,7 +353,7 @@ impl ValidatorSet for ValidatorSafeContract { // ensure receipts match header. // TODO: optimize? these were just decoded. - let found_root = ::util::triehash::ordered_trie_root( + let found_root = ::triehash::ordered_trie_root( receipts.iter().map(::rlp::encode).map(|x| x.to_vec()) ); if found_root != *old_header.receipts_root() { diff --git a/ethcore/src/lib.rs b/ethcore/src/lib.rs index 7d175ad7a..02211ed77 100644 --- a/ethcore/src/lib.rs +++ b/ethcore/src/lib.rs @@ -104,6 +104,7 @@ extern crate rand; extern crate rlp; extern crate hash; extern crate heapsize; +extern crate triehash; extern crate ansi_term; extern crate semantic_version; diff --git a/ethcore/src/pod_account.rs b/ethcore/src/pod_account.rs index b8606008c..f8c658deb 100644 --- a/ethcore/src/pod_account.rs +++ b/ethcore/src/pod_account.rs @@ -18,6 +18,7 @@ use std::fmt; use std::collections::BTreeMap; use itertools::Itertools; use hash::{keccak}; +use triehash::sec_trie_root; use util::*; use state::Account; use ethjson; diff --git a/ethcore/src/pod_state.rs b/ethcore/src/pod_state.rs index 5a0265dc1..e35e602b2 100644 --- a/ethcore/src/pod_state.rs +++ b/ethcore/src/pod_state.rs @@ -19,6 +19,7 @@ use std::fmt; use std::collections::BTreeMap; use itertools::Itertools; +use triehash::sec_trie_root; use util::*; use pod_account::{self, PodAccount}; use types::state_diff::StateDiff; diff --git a/ethcore/src/snapshot/block.rs b/ethcore/src/snapshot/block.rs index fa4b3c19f..2ef54b53b 100644 --- a/ethcore/src/snapshot/block.rs +++ b/ethcore/src/snapshot/block.rs @@ -23,7 +23,7 @@ use hash::keccak; use views::BlockView; use rlp::{DecoderError, RlpStream, UntrustedRlp}; use util::{Bytes, H256}; -use util::triehash::ordered_trie_root; +use triehash::ordered_trie_root; const HEADER_FIELDS: usize = 8; const BLOCK_FIELDS: usize = 2; @@ -190,7 +190,7 @@ mod tests { b.transactions.push(t2.into()); let receipts_root = b.header.receipts_root().clone(); - b.header.set_transactions_root(::util::triehash::ordered_trie_root( + b.header.set_transactions_root(::triehash::ordered_trie_root( b.transactions.iter().map(::rlp::encode).map(|out| out.into_vec()) )); diff --git a/ethcore/src/snapshot/consensus/work.rs b/ethcore/src/snapshot/consensus/work.rs index 2bf154fc4..7e0b896c4 100644 --- a/ethcore/src/snapshot/consensus/work.rs +++ b/ethcore/src/snapshot/consensus/work.rs @@ -222,7 +222,7 @@ impl Rebuilder for PowRebuilder { use views::BlockView; use snapshot::verify_old_block; use util::U256; - use util::triehash::ordered_trie_root; + use triehash::ordered_trie_root; let rlp = UntrustedRlp::new(chunk); let item_count = rlp.item_count()?; diff --git a/ethcore/src/verification/verification.rs b/ethcore/src/verification/verification.rs index f9290309c..eb055fd4a 100644 --- a/ethcore/src/verification/verification.rs +++ b/ethcore/src/verification/verification.rs @@ -23,6 +23,7 @@ use std::collections::HashSet; use hash::keccak; +use triehash::ordered_trie_root; use heapsize::HeapSizeOf; use util::*; use engines::Engine; @@ -269,6 +270,7 @@ fn verify_block_integrity(block: &[u8], transactions_root: &H256, uncles_hash: & mod tests { use std::collections::{BTreeMap, HashMap}; use hash::keccak; + use triehash::ordered_trie_root; use util::*; use ethkey::{Random, Generator}; use header::*; diff --git a/sync/Cargo.toml b/sync/Cargo.toml index 5fb3628df..0e1bd3d5b 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -19,6 +19,7 @@ ethcore-light = { path = "../ethcore/light"} ethcore = { path = "../ethcore" } rlp = { path = "../util/rlp" } hash = { path = "../util/hash" } +triehash = { path = "../util/triehash" } clippy = { version = "0.0.103", optional = true} log = "0.3" env_logger = "0.4" diff --git a/sync/src/blocks.rs b/sync/src/blocks.rs index 77cb206c8..a07d9dfd0 100644 --- a/sync/src/blocks.rs +++ b/sync/src/blocks.rs @@ -19,6 +19,7 @@ use std::collections::hash_map::Entry; use smallvec::SmallVec; use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP}; use heapsize::HeapSizeOf; +use triehash::ordered_trie_root; use util::*; use rlp::*; use network::NetworkError; diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 237d6c437..b5f97c6bc 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -39,6 +39,7 @@ extern crate smallvec; extern crate rlp; extern crate ipnetwork; extern crate hash; +extern crate triehash; extern crate ethcore_light as light; diff --git a/util/Cargo.toml b/util/Cargo.toml index d04450053..e4e95bad7 100644 --- a/util/Cargo.toml +++ b/util/Cargo.toml @@ -33,6 +33,7 @@ ethcore-bloom-journal = { path = "bloom" } regex = "0.2" lru-cache = "0.1.0" ethcore-logger = { path = "../logger" } +triehash = { path = "triehash" } [features] default = [] diff --git a/util/benches/trie.rs b/util/benches/trie.rs index 2ae10cf39..20b0858aa 100644 --- a/util/benches/trie.rs +++ b/util/benches/trie.rs @@ -17,6 +17,7 @@ #![feature(test)] extern crate test; +extern crate triehash; extern crate ethcore_util; #[macro_use] extern crate log; @@ -27,7 +28,7 @@ use ethcore_util::hash::*; use ethcore_util::bytes::*; use ethcore_util::trie::*; use ethcore_util::memorydb::*; -use ethcore_util::triehash::*; +use triehash::*; use hash::keccak; fn random_word(alphabet: &[u8], min_count: usize, diff_count: usize, seed: &mut H256) -> Vec { diff --git a/util/src/lib.rs b/util/src/lib.rs index 61fc616da..710a96127 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -117,14 +117,12 @@ pub mod common; pub mod error; pub mod bytes; pub mod misc; -pub mod vector; pub mod hashdb; pub mod memorydb; pub mod migration; pub mod overlaydb; pub mod journaldb; pub mod kvdb; -pub mod triehash; pub mod trie; pub mod nibbleslice; pub mod nibblevec; @@ -136,12 +134,10 @@ pub use hashdb::*; pub use memorydb::MemoryDB; pub use overlaydb::*; pub use journaldb::JournalDB; -pub use triehash::*; pub use trie::{Trie, TrieMut, TrieDB, TrieDBMut, TrieFactory, TrieError, SecTrieDB, SecTrieDBMut}; pub use kvdb::*; pub use error::*; pub use bytes::*; -pub use vector::*; pub use bigint::prelude::*; pub use bigint::hash; diff --git a/util/src/trie/triedbmut.rs b/util/src/trie/triedbmut.rs index fa23ca756..14d7f4f81 100644 --- a/util/src/trie/triedbmut.rs +++ b/util/src/trie/triedbmut.rs @@ -937,7 +937,8 @@ impl<'a> Drop for TrieDBMut<'a> { #[cfg(test)] mod tests { - use triehash::trie_root; + extern crate triehash; + use self::triehash::trie_root; use hashdb::*; use memorydb::*; use super::*; diff --git a/util/src/vector.rs b/util/src/vector.rs deleted file mode 100644 index 951d396c7..000000000 --- a/util/src/vector.rs +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2015-2017 Parity Technologies (UK) Ltd. -// This file is part of Parity. - -// Parity is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Parity is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Parity. If not, see . - -//! Vector extensions. - -/// Returns len of prefix shared with elem -/// -/// ```rust -/// extern crate ethcore_util as util; -/// use util::vector::SharedPrefix; -/// -/// fn main () { -/// let a = vec![1,2,3,3,5]; -/// let b = vec![1,2,3]; -/// assert_eq!(a.shared_prefix_len(&b), 3); -/// } -/// ``` -pub trait SharedPrefix { - /// Get common prefix length - fn shared_prefix_len(&self, elem: &[T]) -> usize; -} - -impl SharedPrefix for [T] where T: Eq { - fn shared_prefix_len(&self, elem: &[T]) -> usize { - use std::cmp; - let len = cmp::min(self.len(), elem.len()); - (0..len).take_while(|&i| self[i] == elem[i]).count() - } -} - -#[cfg(test)] -mod test { - use vector::SharedPrefix; - - #[test] - fn test_shared_prefix() { - let a = vec![1,2,3,4,5,6]; - let b = vec![4,2,3,4,5,6]; - assert_eq!(a.shared_prefix_len(&b), 0); - } - - #[test] - fn test_shared_prefix2() { - let a = vec![1,2,3,3,5]; - let b = vec![1,2,3]; - assert_eq!(a.shared_prefix_len(&b), 3); - } - - #[test] - fn test_shared_prefix3() { - let a = vec![1,2,3,4,5,6]; - let b = vec![1,2,3,4,5,6]; - assert_eq!(a.shared_prefix_len(&b), 6); - } -} diff --git a/util/triehash/Cargo.toml b/util/triehash/Cargo.toml new file mode 100644 index 000000000..cc18a6ea8 --- /dev/null +++ b/util/triehash/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "triehash" +version = "0.1.0" +authors = ["Parity Technologies "] + +[dependencies] +rlp = { path = "../rlp" } +ethcore-bigint = { path = "../bigint" } +hash = { path = "../hash" } diff --git a/util/src/triehash.rs b/util/triehash/src/lib.rs similarity index 79% rename from util/src/triehash.rs rename to util/triehash/src/lib.rs index fa790f3f4..d166aa5d0 100644 --- a/util/src/triehash.rs +++ b/util/triehash/src/lib.rs @@ -18,26 +18,31 @@ //! //! This module should be used to generate trie root hash. +extern crate ethcore_bigint; +extern crate hash; +extern crate rlp; + use std::collections::BTreeMap; use std::cmp; -use hash::*; -use keccak::keccak; -use rlp; +use ethcore_bigint::hash::H256; +use hash::keccak; use rlp::RlpStream; -use vector::SharedPrefix; + +fn shared_prefix_len(first: &[T], second: &[T]) -> usize { + let len = cmp::min(first.len(), second.len()); + (0..len).take_while(|&i| first[i] == second[i]).count() +} /// Generates a trie root hash for a vector of values /// /// ```rust -/// extern crate ethcore_util as util; -/// use std::str::FromStr; -/// use util::triehash::*; -/// use util::hash::*; +/// extern crate triehash; +/// use triehash::ordered_trie_root; /// /// fn main() { /// let v = vec![From::from("doe"), From::from("reindeer")]; /// let root = "e766d5d51b89dc39d981b41bda63248d7abce4f0225eefd023792a540bcffee3"; -/// assert_eq!(ordered_trie_root(v), H256::from_str(root).unwrap()); +/// assert_eq!(ordered_trie_root(v), root.parse().unwrap()); /// } /// ``` pub fn ordered_trie_root(input: I) -> H256 @@ -61,10 +66,8 @@ pub fn ordered_trie_root(input: I) -> H256 /// Generates a trie root hash for a vector of key-values /// /// ```rust -/// extern crate ethcore_util as util; -/// use std::str::FromStr; -/// use util::triehash::*; -/// use util::hash::*; +/// extern crate triehash; +/// use triehash::trie_root; /// /// fn main() { /// let v = vec![ @@ -74,7 +77,7 @@ pub fn ordered_trie_root(input: I) -> H256 /// ]; /// /// let root = "8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3"; -/// assert_eq!(trie_root(v), H256::from_str(root).unwrap()); +/// assert_eq!(trie_root(v), root.parse().unwrap()); /// } /// ``` pub fn trie_root(input: I) -> H256 @@ -95,10 +98,8 @@ pub fn trie_root(input: I) -> H256 /// Generates a key-hashed (secure) trie root hash for a vector of key-values. /// /// ```rust -/// extern crate ethcore_util as util; -/// use std::str::FromStr; -/// use util::triehash::*; -/// use util::hash::*; +/// extern crate triehash; +/// use triehash::sec_trie_root; /// /// fn main() { /// let v = vec![ @@ -108,7 +109,7 @@ pub fn trie_root(input: I) -> H256 /// ]; /// /// let root = "d4cd937e4a4368d7931a9cf51686b7e10abb3dce38a39000fd7902a092b64585"; -/// assert_eq!(sec_trie_root(v), H256::from_str(root).unwrap()); +/// assert_eq!(sec_trie_root(v), root.parse().unwrap()); /// } /// ``` pub fn sec_trie_root(input: Vec<(Vec, Vec)>) -> H256 { @@ -216,7 +217,7 @@ fn hash256rlp(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStre .skip(1) // get minimum number of shared nibbles between first and each successive .fold(key.len(), | acc, &(ref k, _) | { - cmp::min(key.shared_prefix_len(k), acc) + cmp::min(shared_prefix_len(key, k), acc) }); // if shared prefix is higher than current prefix append its @@ -288,50 +289,49 @@ fn test_nibbles() { assert_eq!(as_nibbles(&v), e); } -#[test] -fn test_hex_prefix_encode() { - let v = vec![0, 0, 1, 2, 3, 4, 5]; - let e = vec![0x10, 0x01, 0x23, 0x45]; - let h = hex_prefix_encode(&v, false); - assert_eq!(h, e); - - let v = vec![0, 1, 2, 3, 4, 5]; - let e = vec![0x00, 0x01, 0x23, 0x45]; - let h = hex_prefix_encode(&v, false); - assert_eq!(h, e); - - let v = vec![0, 1, 2, 3, 4, 5]; - let e = vec![0x20, 0x01, 0x23, 0x45]; - let h = hex_prefix_encode(&v, true); - assert_eq!(h, e); - - let v = vec![1, 2, 3, 4, 5]; - let e = vec![0x31, 0x23, 0x45]; - let h = hex_prefix_encode(&v, true); - assert_eq!(h, e); - - let v = vec![1, 2, 3, 4]; - let e = vec![0x00, 0x12, 0x34]; - let h = hex_prefix_encode(&v, false); - assert_eq!(h, e); - - let v = vec![4, 1]; - let e = vec![0x20, 0x41]; - let h = hex_prefix_encode(&v, true); - assert_eq!(h, e); -} #[cfg(test)] mod tests { - use std::str::FromStr; - use hash::H256; - use super::trie_root; + use super::{trie_root, shared_prefix_len, hex_prefix_encode}; + + #[test] + fn test_hex_prefix_encode() { + let v = vec![0, 0, 1, 2, 3, 4, 5]; + let e = vec![0x10, 0x01, 0x23, 0x45]; + let h = hex_prefix_encode(&v, false); + assert_eq!(h, e); + + let v = vec![0, 1, 2, 3, 4, 5]; + let e = vec![0x00, 0x01, 0x23, 0x45]; + let h = hex_prefix_encode(&v, false); + assert_eq!(h, e); + + let v = vec![0, 1, 2, 3, 4, 5]; + let e = vec![0x20, 0x01, 0x23, 0x45]; + let h = hex_prefix_encode(&v, true); + assert_eq!(h, e); + + let v = vec![1, 2, 3, 4, 5]; + let e = vec![0x31, 0x23, 0x45]; + let h = hex_prefix_encode(&v, true); + assert_eq!(h, e); + + let v = vec![1, 2, 3, 4]; + let e = vec![0x00, 0x12, 0x34]; + let h = hex_prefix_encode(&v, false); + assert_eq!(h, e); + + let v = vec![4, 1]; + let e = vec![0x20, 0x41]; + let h = hex_prefix_encode(&v, true); + assert_eq!(h, e); + } #[test] fn simple_test() { assert_eq!(trie_root(vec![ (b"A".to_vec(), b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_vec()) - ]), H256::from_str("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab").unwrap()); + ]), "d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab".parse().unwrap()); } #[test] @@ -348,4 +348,24 @@ mod tests { ])); } + #[test] + fn test_shared_prefix() { + let a = vec![1,2,3,4,5,6]; + let b = vec![4,2,3,4,5,6]; + assert_eq!(shared_prefix_len(&a, &b), 0); + } + + #[test] + fn test_shared_prefix2() { + let a = vec![1,2,3,3,5]; + let b = vec![1,2,3]; + assert_eq!(shared_prefix_len(&a, &b), 3); + } + + #[test] + fn test_shared_prefix3() { + let a = vec![1,2,3,4,5,6]; + let b = vec![1,2,3,4,5,6]; + assert_eq!(shared_prefix_len(&a, &b), 6); + } }