Hashable::sha3 -> fn keccak for ethcore

This commit is contained in:
debris
2017-08-30 19:18:28 +02:00
parent e120c75d17
commit f0e8abb07b
69 changed files with 429 additions and 398 deletions

View File

@@ -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::<BasicAccount>(&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::<BasicAccount>(&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::<BasicAccount>(&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();

View File

@@ -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()? {

View File

@@ -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();

View File

@@ -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<SnapshotComponents>, 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<KeyValueDB>, 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) => {

View File

@@ -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()

View File

@@ -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.

View File

@@ -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(),
};

View File

@@ -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());