diff --git a/beacon_node/db/src/stores/beacon_block_store.rs b/beacon_node/db/src/stores/beacon_block_store.rs index 8a1fc2b0d..bd5149cfd 100644 --- a/beacon_node/db/src/stores/beacon_block_store.rs +++ b/beacon_node/db/src/stores/beacon_block_store.rs @@ -134,9 +134,9 @@ mod tests { let store = BeaconBlockStore::new(db.clone()); let ssz = "definitly not a valid block".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); - db.put(DB_COLUMN, hash, ssz).unwrap(); + db.put(DB_COLUMN, hash.as_bytes(), ssz).unwrap(); assert_eq!( store.block_at_slot(hash, Slot::from(42_u64)), Err(BeaconBlockAtSlotError::DBError( @@ -151,10 +151,10 @@ mod tests { let store = BeaconBlockStore::new(db.clone()); let ssz = "some bytes".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); - let other_hash = &Hash256::from("another hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); + let other_hash = &Hash256::from([0xBB; 32]); - db.put(DB_COLUMN, hash, ssz).unwrap(); + db.put(DB_COLUMN, hash.as_bytes(), ssz).unwrap(); assert_eq!( store.block_at_slot(other_hash, Slot::from(42_u64)), Err(BeaconBlockAtSlotError::UnknownBeaconBlock(*other_hash)) @@ -169,18 +169,15 @@ mod tests { let thread_count = 10; let write_count = 10; - // We're expecting the product of these numbers to fit in one byte. - assert!(thread_count * write_count <= 255); - let mut handles = vec![]; for t in 0..thread_count { let wc = write_count; let bs = bs.clone(); let handle = thread::spawn(move || { for w in 0..wc { - let key = (t * w) as u8; + let key = t * w; let val = 42; - bs.put(&[key][..].into(), &vec![val]).unwrap(); + bs.put(&Hash256::from_low_u64_le(key), &vec![val]).unwrap(); } }); handles.push(handle); @@ -192,9 +189,9 @@ mod tests { for t in 0..thread_count { for w in 0..write_count { - let key = (t * w) as u8; - assert!(bs.exists(&[key][..].into()).unwrap()); - let val = bs.get(&[key][..].into()).unwrap().unwrap(); + let key = t * w; + assert!(bs.exists(&Hash256::from_low_u64_le(key)).unwrap()); + let val = bs.get(&Hash256::from_low_u64_le(key)).unwrap().unwrap(); assert_eq!(vec![42], val); } } @@ -208,19 +205,20 @@ mod tests { // Specify test block parameters. let hashes = [ - Hash256::from(&[0; 32][..]), - Hash256::from(&[1; 32][..]), - Hash256::from(&[2; 32][..]), - Hash256::from(&[3; 32][..]), - Hash256::from(&[4; 32][..]), + Hash256::from([0; 32]), + Hash256::from([1; 32]), + Hash256::from([2; 32]), + Hash256::from([3; 32]), + Hash256::from([4; 32]), ]; let parent_hashes = [ - Hash256::from(&[255; 32][..]), // Genesis block. - Hash256::from(&[0; 32][..]), - Hash256::from(&[1; 32][..]), - Hash256::from(&[2; 32][..]), - Hash256::from(&[3; 32][..]), + Hash256::from([255; 32]), // Genesis block. + Hash256::from([0; 32]), + Hash256::from([1; 32]), + Hash256::from([2; 32]), + Hash256::from([3; 32]), ]; + let unknown_hash = Hash256::from([101; 32]); // different from all above let slots: Vec = vec![0, 1, 3, 4, 5].iter().map(|x| Slot::new(*x)).collect(); // Generate a vec of random blocks and store them in the DB. @@ -233,7 +231,7 @@ mod tests { block.slot = slots[i]; let ssz = ssz_encode(&block); - db.put(DB_COLUMN, &hashes[i], &ssz).unwrap(); + db.put(DB_COLUMN, hashes[i].as_bytes(), &ssz).unwrap(); blocks.push(block); } @@ -255,11 +253,10 @@ mod tests { let ssz = bs.block_at_slot(&hashes[4], Slot::new(6)).unwrap(); assert_eq!(ssz, None); - let bad_hash = &Hash256::from("unknown".as_bytes()); - let ssz = bs.block_at_slot(bad_hash, Slot::new(2)); + let ssz = bs.block_at_slot(&unknown_hash, Slot::new(2)); assert_eq!( ssz, - Err(BeaconBlockAtSlotError::UnknownBeaconBlock(*bad_hash)) + Err(BeaconBlockAtSlotError::UnknownBeaconBlock(unknown_hash)) ); } } diff --git a/beacon_node/db/src/stores/macros.rs b/beacon_node/db/src/stores/macros.rs index e26e101c9..6c53e40ee 100644 --- a/beacon_node/db/src/stores/macros.rs +++ b/beacon_node/db/src/stores/macros.rs @@ -2,19 +2,19 @@ macro_rules! impl_crud_for_store { ($store: ident, $db_column: expr) => { impl $store { pub fn put(&self, hash: &Hash256, ssz: &[u8]) -> Result<(), DBError> { - self.db.put($db_column, hash, ssz) + self.db.put($db_column, hash.as_bytes(), ssz) } pub fn get(&self, hash: &Hash256) -> Result>, DBError> { - self.db.get($db_column, hash) + self.db.get($db_column, hash.as_bytes()) } pub fn exists(&self, hash: &Hash256) -> Result { - self.db.exists($db_column, hash) + self.db.exists($db_column, hash.as_bytes()) } pub fn delete(&self, hash: &Hash256) -> Result<(), DBError> { - self.db.delete($db_column, hash) + self.db.delete($db_column, hash.as_bytes()) } } }; @@ -29,10 +29,10 @@ macro_rules! test_crud_for_store { let store = $store::new(db.clone()); let ssz = "some bytes".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); store.put(hash, ssz).unwrap(); - assert_eq!(db.get(DB_COLUMN, hash).unwrap().unwrap(), ssz); + assert_eq!(db.get(DB_COLUMN, hash.as_bytes()).unwrap().unwrap(), ssz); } #[test] @@ -41,9 +41,9 @@ macro_rules! test_crud_for_store { let store = $store::new(db.clone()); let ssz = "some bytes".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); - db.put(DB_COLUMN, hash, ssz).unwrap(); + db.put(DB_COLUMN, hash.as_bytes(), ssz).unwrap(); assert_eq!(store.get(hash).unwrap().unwrap(), ssz); } @@ -53,10 +53,10 @@ macro_rules! test_crud_for_store { let store = $store::new(db.clone()); let ssz = "some bytes".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); - let other_hash = &Hash256::from("another hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); + let other_hash = &Hash256::from([0xBB; 32]); - db.put(DB_COLUMN, other_hash, ssz).unwrap(); + db.put(DB_COLUMN, other_hash.as_bytes(), ssz).unwrap(); assert_eq!(store.get(hash).unwrap(), None); } @@ -66,9 +66,9 @@ macro_rules! test_crud_for_store { let store = $store::new(db.clone()); let ssz = "some bytes".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); - db.put(DB_COLUMN, hash, ssz).unwrap(); + db.put(DB_COLUMN, hash.as_bytes(), ssz).unwrap(); assert!(store.exists(hash).unwrap()); } @@ -78,10 +78,10 @@ macro_rules! test_crud_for_store { let store = $store::new(db.clone()); let ssz = "some bytes".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); - let other_hash = &Hash256::from("another hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); + let other_hash = &Hash256::from([0xBB; 32]); - db.put(DB_COLUMN, hash, ssz).unwrap(); + db.put(DB_COLUMN, hash.as_bytes(), ssz).unwrap(); assert!(!store.exists(other_hash).unwrap()); } @@ -91,13 +91,13 @@ macro_rules! test_crud_for_store { let store = $store::new(db.clone()); let ssz = "some bytes".as_bytes(); - let hash = &Hash256::from("some hash".as_bytes()); + let hash = &Hash256::from([0xAA; 32]); - db.put(DB_COLUMN, hash, ssz).unwrap(); - assert!(db.exists(DB_COLUMN, hash).unwrap()); + db.put(DB_COLUMN, hash.as_bytes(), ssz).unwrap(); + assert!(db.exists(DB_COLUMN, hash.as_bytes()).unwrap()); store.delete(hash).unwrap(); - assert!(!db.exists(DB_COLUMN, hash).unwrap()); + assert!(!db.exists(DB_COLUMN, hash.as_bytes()).unwrap()); } }; } diff --git a/beacon_node/db/src/stores/pow_chain_store.rs b/beacon_node/db/src/stores/pow_chain_store.rs index a7c77bab5..5c8b97907 100644 --- a/beacon_node/db/src/stores/pow_chain_store.rs +++ b/beacon_node/db/src/stores/pow_chain_store.rs @@ -37,7 +37,7 @@ mod tests { let db = Arc::new(MemoryDB::open()); let store = PoWChainStore::new(db.clone()); - let hash = &Hash256::from("some hash".as_bytes()).to_vec(); + let hash = &Hash256::from([0xAA; 32]).as_bytes().to_vec(); store.put_block_hash(hash).unwrap(); assert!(db.exists(DB_COLUMN, hash).unwrap()); @@ -48,7 +48,7 @@ mod tests { let db = Arc::new(MemoryDB::open()); let store = PoWChainStore::new(db.clone()); - let hash = &Hash256::from("some hash".as_bytes()).to_vec(); + let hash = &Hash256::from([0xAA; 32]).as_bytes().to_vec(); db.put(DB_COLUMN, hash, &[0]).unwrap(); assert!(store.block_hash_exists(hash).unwrap()); @@ -59,8 +59,8 @@ mod tests { let db = Arc::new(MemoryDB::open()); let store = PoWChainStore::new(db.clone()); - let hash = &Hash256::from("some hash".as_bytes()).to_vec(); - let other_hash = &Hash256::from("another hash".as_bytes()).to_vec(); + let hash = &Hash256::from([0xAA; 32]).as_bytes().to_vec(); + let other_hash = &Hash256::from([0xBB; 32]).as_bytes().to_vec(); db.put(DB_COLUMN, hash, &[0]).unwrap(); assert!(!store.block_hash_exists(other_hash).unwrap()); diff --git a/eth2/fork_choice/src/bitwise_lmd_ghost.rs b/eth2/fork_choice/src/bitwise_lmd_ghost.rs index 60aa38fe7..8a26b3e3c 100644 --- a/eth2/fork_choice/src/bitwise_lmd_ghost.rs +++ b/eth2/fork_choice/src/bitwise_lmd_ghost.rs @@ -210,7 +210,7 @@ where trace!("Child vote length: {}", votes.len()); for (candidate, votes) in votes.iter() { - let candidate_bit: BitVec = BitVec::from_bytes(&candidate); + let candidate_bit: BitVec = BitVec::from_bytes(candidate.as_bytes()); // if the bitmasks don't match, exclude candidate if !bitmask.iter().eq(candidate_bit.iter().take(bit)) { diff --git a/eth2/state_processing/src/block_processable.rs b/eth2/state_processing/src/block_processable.rs index 32327aad3..a54bb96d4 100644 --- a/eth2/state_processing/src/block_processable.rs +++ b/eth2/state_processing/src/block_processable.rs @@ -134,9 +134,10 @@ fn per_block_processing_signature_optional( let new_mix = { let mut mix = state.latest_randao_mixes [state.slot.as_usize() % spec.latest_randao_mixes_length] + .as_bytes() .to_vec(); mix.append(&mut ssz_encode(&block.randao_reveal)); - Hash256::from(&hash(&mix)[..]) + Hash256::from_slice(&hash(&mix)[..]) }; state.latest_randao_mixes[state.slot.as_usize() % spec.latest_randao_mixes_length] = new_mix; diff --git a/eth2/state_processing/src/epoch_processable.rs b/eth2/state_processing/src/epoch_processable.rs index 0ecd1bbd1..ff6f18113 100644 --- a/eth2/state_processing/src/epoch_processable.rs +++ b/eth2/state_processing/src/epoch_processable.rs @@ -626,7 +626,7 @@ impl EpochProcessable for BeaconState { } fn hash_tree_root(input: Vec) -> Hash256 { - Hash256::from(&input.hash_tree_root()[..]) + Hash256::from_slice(&input.hash_tree_root()[..]) } fn winning_root( diff --git a/eth2/types/Cargo.toml b/eth2/types/Cargo.toml index f70e8b490..1b9eed9b8 100644 --- a/eth2/types/Cargo.toml +++ b/eth2/types/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] bls = { path = "../utils/bls" } boolean-bitfield = { path = "../utils/boolean-bitfield" } -ethereum-types = "0.4.0" +ethereum-types = "0.5" hashing = { path = "../utils/hashing" } honey-badger-split = { path = "../utils/honey-badger-split" } log = "0.4" @@ -21,6 +21,7 @@ ssz = { path = "../utils/ssz" } ssz_derive = { path = "../utils/ssz_derive" } swap_or_not_shuffle = { path = "../utils/swap_or_not_shuffle" } test_random_derive = { path = "../utils/test_random_derive" } +int_to_bytes = { path = "../utils/int_to_bytes" } [dev-dependencies] env_logger = "0.6.0" diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 4ac81bb4c..eff9512ab 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -16,7 +16,7 @@ pub struct Attestation { impl Attestation { pub fn canonical_root(&self) -> Hash256 { - Hash256::from(&self.hash_tree_root()[..]) + Hash256::from_slice(&self.hash_tree_root()[..]) } pub fn signable_message(&self, custody_bit: bool) -> Vec { diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index 73b5facfa..a7351125f 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -35,7 +35,7 @@ impl Eq for AttestationData {} impl AttestationData { pub fn canonical_root(&self) -> Hash256 { - Hash256::from(&self.hash_tree_root()[..]) + Hash256::from_slice(&self.hash_tree_root()[..]) } pub fn signable_message(&self, custody_bit: bool) -> Vec { diff --git a/eth2/types/src/attester_slashing/builder.rs b/eth2/types/src/attester_slashing/builder.rs index ed203d6e1..abade77c9 100644 --- a/eth2/types/src/attester_slashing/builder.rs +++ b/eth2/types/src/attester_slashing/builder.rs @@ -27,8 +27,8 @@ impl AttesterSlashingBuilder { let shard = 0; let justified_epoch = Epoch::new(0); let epoch = Epoch::new(0); - let hash_1 = Hash256::from(&[1][..]); - let hash_2 = Hash256::from(&[2][..]); + let hash_1 = Hash256::from_low_u64_le(1); + let hash_2 = Hash256::from_low_u64_le(2); let mut slashable_attestation_1 = SlashableAttestation { validator_indices: validator_indices.to_vec(), diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index a10fd2a6e..f69cdaec9 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -42,7 +42,7 @@ impl BeaconBlock { } pub fn canonical_root(&self) -> Hash256 { - Hash256::from(&self.hash_tree_root()[..]) + Hash256::from_slice(&self.hash_tree_root()[..]) } pub fn proposal_root(&self, spec: &ChainSpec) -> Hash256 { @@ -57,7 +57,7 @@ impl BeaconBlock { shard: spec.beacon_chain_shard_number, block_root: block_without_signature_root, }; - Hash256::from(&proposal.hash_tree_root()[..]) + Hash256::from_slice(&proposal.hash_tree_root()[..]) } } diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index a9e2f2673..5c87d274a 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -3,6 +3,7 @@ use crate::test_utils::TestRandom; use crate::{validator::StatusFlags, validator_registry::get_active_validator_indices, *}; use bls::verify_proof_of_possession; use honey_badger_split::SplitExt; +use int_to_bytes::int_to_bytes32; use log::{debug, error, trace}; use rand::RngCore; use rayon::prelude::*; @@ -326,7 +327,7 @@ impl BeaconState { /// /// Spec v0.2.0 pub fn canonical_root(&self) -> Hash256 { - Hash256::from(&self.hash_tree_root()[..]) + Hash256::from_slice(&self.hash_tree_root()[..]) } /// The epoch corresponding to `self.slot`. @@ -487,19 +488,20 @@ impl BeaconState { let mut input = self .get_randao_mix(epoch, spec) .ok_or_else(|| Error::InsufficientRandaoMixes)? + .as_bytes() .to_vec(); input.append( &mut self .get_active_index_root(epoch, spec) .ok_or_else(|| Error::InsufficientIndexRoots)? + .as_bytes() .to_vec(), ); - // TODO: ensure `Hash256::from(u64)` == `int_to_bytes32`. - input.append(&mut Hash256::from(epoch.as_u64()).to_vec()); + input.append(&mut int_to_bytes32(epoch.as_u64())); - Ok(Hash256::from(&hash(&input[..])[..])) + Ok(Hash256::from_slice(&hash(&input[..])[..])) } /// Returns the crosslink committees for some slot. @@ -1311,7 +1313,7 @@ impl BeaconState { } fn hash_tree_root(input: Vec) -> Hash256 { - Hash256::from(&input.hash_tree_root()[..]) + Hash256::from_slice(&input.hash_tree_root()[..]) } impl From for InclusionError { diff --git a/eth2/types/src/beacon_state/builder.rs b/eth2/types/src/beacon_state/builder.rs index 02886a86e..4889b5b5a 100644 --- a/eth2/types/src/beacon_state/builder.rs +++ b/eth2/types/src/beacon_state/builder.rs @@ -145,8 +145,8 @@ impl BeaconStateBuilder { state.previous_calculation_epoch = epoch - 1; state.current_calculation_epoch = epoch; - state.previous_epoch_seed = Hash256::from(&b"previous_seed"[..]); - state.current_epoch_seed = Hash256::from(&b"current_seed"[..]); + state.previous_epoch_seed = Hash256::from([0x01; 32]); + state.current_epoch_seed = Hash256::from([0x02; 32]); state.previous_justified_epoch = epoch - 2; state.justified_epoch = epoch - 1; diff --git a/eth2/types/src/proposer_slashing/builder.rs b/eth2/types/src/proposer_slashing/builder.rs index 7923ff74d..c9d2ff272 100644 --- a/eth2/types/src/proposer_slashing/builder.rs +++ b/eth2/types/src/proposer_slashing/builder.rs @@ -25,13 +25,13 @@ impl ProposerSlashingBuilder { let proposal_data_1 = ProposalSignedData { slot, shard, - block_root: Hash256::from(&[1][..]), + block_root: Hash256::from_low_u64_le(1), }; let proposal_data_2 = ProposalSignedData { slot, shard, - block_root: Hash256::from(&[2][..]), + block_root: Hash256::from_low_u64_le(2), }; let proposal_signature_1 = { diff --git a/eth2/types/src/test_utils/address.rs b/eth2/types/src/test_utils/address.rs index 2d60b72da..13de2dec9 100644 --- a/eth2/types/src/test_utils/address.rs +++ b/eth2/types/src/test_utils/address.rs @@ -6,6 +6,6 @@ impl TestRandom for Address { fn random_for_test(rng: &mut T) -> Self { let mut key_bytes = vec![0; 20]; rng.fill_bytes(&mut key_bytes); - Address::from(&key_bytes[..]) + Address::from_slice(&key_bytes[..]) } } diff --git a/eth2/types/src/test_utils/hash256.rs b/eth2/types/src/test_utils/hash256.rs index 98f5e7899..a227679da 100644 --- a/eth2/types/src/test_utils/hash256.rs +++ b/eth2/types/src/test_utils/hash256.rs @@ -6,6 +6,6 @@ impl TestRandom for Hash256 { fn random_for_test(rng: &mut T) -> Self { let mut key_bytes = vec![0; 32]; rng.fill_bytes(&mut key_bytes); - Hash256::from(&key_bytes[..]) + Hash256::from_slice(&key_bytes[..]) } } diff --git a/eth2/utils/ssz/Cargo.toml b/eth2/utils/ssz/Cargo.toml index 25326cb5b..f13db5def 100644 --- a/eth2/utils/ssz/Cargo.toml +++ b/eth2/utils/ssz/Cargo.toml @@ -6,5 +6,5 @@ edition = "2018" [dependencies] bytes = "0.4.9" -ethereum-types = "0.4.0" +ethereum-types = "0.5" hashing = { path = "../hashing" } diff --git a/eth2/utils/ssz/src/impl_decode.rs b/eth2/utils/ssz/src/impl_decode.rs index b9ca48f9b..b13cbeb5d 100644 --- a/eth2/utils/ssz/src/impl_decode.rs +++ b/eth2/utils/ssz/src/impl_decode.rs @@ -59,7 +59,7 @@ impl Decodable for H256 { if bytes.len() < 32 || bytes.len() - 32 < index { Err(DecodeError::TooShort) } else { - Ok((H256::from(&bytes[index..(index + 32)]), index + 32)) + Ok((H256::from_slice(&bytes[index..(index + 32)]), index + 32)) } } } @@ -69,7 +69,7 @@ impl Decodable for Address { if bytes.len() < 20 || bytes.len() - 20 < index { Err(DecodeError::TooShort) } else { - Ok((Address::from(&bytes[index..(index + 20)]), index + 20)) + Ok((Address::from_slice(&bytes[index..(index + 20)]), index + 20)) } } } @@ -95,7 +95,7 @@ mod tests { */ let input = vec![42_u8; 32]; let (decoded, i) = H256::ssz_decode(&input, 0).unwrap(); - assert_eq!(decoded.to_vec(), input); + assert_eq!(decoded.as_bytes(), &input[..]); assert_eq!(i, 32); /* @@ -104,7 +104,7 @@ mod tests { let mut input = vec![42_u8; 32]; input.push(12); let (decoded, i) = H256::ssz_decode(&input, 0).unwrap(); - assert_eq!(decoded.to_vec()[..], input[0..32]); + assert_eq!(decoded.as_bytes(), &input[0..32]); assert_eq!(i, 32); /* diff --git a/eth2/utils/ssz/src/impl_encode.rs b/eth2/utils/ssz/src/impl_encode.rs index 5f73b8483..bb1ec42d5 100644 --- a/eth2/utils/ssz/src/impl_encode.rs +++ b/eth2/utils/ssz/src/impl_encode.rs @@ -55,13 +55,13 @@ impl Encodable for bool { impl Encodable for H256 { fn ssz_append(&self, s: &mut SszStream) { - s.append_encoded_raw(&self.to_vec()); + s.append_encoded_raw(self.as_bytes()); } } impl Encodable for Address { fn ssz_append(&self, s: &mut SszStream) { - s.append_encoded_raw(&self.to_vec()); + s.append_encoded_raw(self.as_bytes()); } }