From 759d0250c5fd9707bc7d97be49be360e0636ae8c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 6 May 2019 19:50:05 +1000 Subject: [PATCH] Clean out some occurances of `SszStream` --- beacon_node/eth2-libp2p/src/behaviour.rs | 2 +- beacon_node/eth2-libp2p/src/rpc/methods.rs | 2 +- beacon_node/eth2-libp2p/src/rpc/protocol.rs | 2 +- eth2/types/src/beacon_state.rs | 3 ++- eth2/types/src/slot_epoch.rs | 16 +++++++------ eth2/types/src/slot_epoch_macros.rs | 24 +++++++++++++++---- eth2/types/src/slot_height.rs | 2 +- eth2/types/src/tree_hash_vector.rs | 2 +- .../utils/bls/src/fake_aggregate_signature.rs | 2 +- eth2/utils/bls/src/fake_signature.rs | 2 +- eth2/utils/ssz_derive/src/lib.rs | 4 ++-- 11 files changed, 39 insertions(+), 22 deletions(-) diff --git a/beacon_node/eth2-libp2p/src/behaviour.rs b/beacon_node/eth2-libp2p/src/behaviour.rs index e1112e6ff..6d98b6a80 100644 --- a/beacon_node/eth2-libp2p/src/behaviour.rs +++ b/beacon_node/eth2-libp2p/src/behaviour.rs @@ -13,7 +13,7 @@ use libp2p::{ NetworkBehaviour, PeerId, }; use slog::{debug, o, trace, warn}; -use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +use ssz::{ssz_encode, Decodable, DecodeError, Encodable}; use types::{Attestation, BeaconBlock}; use types::{Topic, TopicHash}; diff --git a/beacon_node/eth2-libp2p/src/rpc/methods.rs b/beacon_node/eth2-libp2p/src/rpc/methods.rs index dc0be19a9..a61fd8d26 100644 --- a/beacon_node/eth2-libp2p/src/rpc/methods.rs +++ b/beacon_node/eth2-libp2p/src/rpc/methods.rs @@ -1,4 +1,4 @@ -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{Decodable, DecodeError, Encodable}; /// Available RPC methods types and ids. use ssz_derive::{Decode, Encode}; use types::{BeaconBlockBody, BeaconBlockHeader, Epoch, Hash256, Slot}; diff --git a/beacon_node/eth2-libp2p/src/rpc/protocol.rs b/beacon_node/eth2-libp2p/src/rpc/protocol.rs index 5c1c47fbf..b5a695bea 100644 --- a/beacon_node/eth2-libp2p/src/rpc/protocol.rs +++ b/beacon_node/eth2-libp2p/src/rpc/protocol.rs @@ -1,6 +1,6 @@ use super::methods::*; use libp2p::core::{upgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo}; -use ssz::{ssz_encode, Decodable, DecodeError as SSZDecodeError, Encodable, SszStream}; +use ssz::{ssz_encode, Decodable, DecodeError as SSZDecodeError, Encodable}; use std::hash::{Hash, Hasher}; use std::io; use std::iter; diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index e9b052f99..19a25c325 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -2,11 +2,12 @@ use self::epoch_cache::{get_active_validator_indices, EpochCache, Error as Epoch use crate::test_utils::TestRandom; use crate::*; use cached_tree_hash::{Error as TreeHashCacheError, TreeHashCache}; +use hashing::hash; use int_to_bytes::int_to_bytes32; use pubkey_cache::PubkeyCache; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; -use ssz::{hash, ssz_encode}; +use ssz::ssz_encode; use ssz_derive::{Decode, Encode}; use test_random_derive::TestRandom; use tree_hash::TreeHash; diff --git a/eth2/types/src/slot_epoch.rs b/eth2/types/src/slot_epoch.rs index 6c6a92ecb..a885a9e04 100644 --- a/eth2/types/src/slot_epoch.rs +++ b/eth2/types/src/slot_epoch.rs @@ -14,7 +14,7 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use slog; -use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +use ssz::{ssz_encode, Decodable, DecodeError, Encodable}; use std::cmp::{Ord, Ordering}; use std::fmt; use std::hash::{Hash, Hasher}; @@ -144,11 +144,13 @@ mod epoch_tests { #[test] fn max_epoch_ssz() { let max_epoch = Epoch::max_value(); - let mut ssz = SszStream::new(); - ssz.append(&max_epoch); - let encoded = ssz.drain(); - assert_eq!(&encoded, &[255, 255, 255, 255, 255, 255, 255, 255]); - let (decoded, _i): (Epoch, usize) = <_>::ssz_decode(&encoded, 0).unwrap(); - assert_eq!(max_epoch, decoded); + assert_eq!( + &max_epoch.as_ssz_bytes(), + &[255, 255, 255, 255, 255, 255, 255, 255] + ); + assert_eq!( + max_epoch, + Epoch::from_ssz_bytes(&max_epoch.as_ssz_bytes()).unwrap() + ); } } diff --git a/eth2/types/src/slot_epoch_macros.rs b/eth2/types/src/slot_epoch_macros.rs index 4a48bba9f..8cce51b9d 100644 --- a/eth2/types/src/slot_epoch_macros.rs +++ b/eth2/types/src/slot_epoch_macros.rs @@ -193,16 +193,30 @@ macro_rules! impl_display { macro_rules! impl_ssz { ($type: ident) => { impl Encodable for $type { - fn ssz_append(&self, s: &mut SszStream) { - s.append(&self.0); + fn is_ssz_fixed_len() -> bool { + ::is_ssz_fixed_len() + } + + fn ssz_fixed_len() -> usize { + ::ssz_fixed_len() + } + + fn ssz_append(&self, buf: &mut Vec) { + self.0.ssz_append(buf) } } impl Decodable for $type { - fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { - let (value, i) = <_>::ssz_decode(bytes, i)?; + fn is_ssz_fixed_len() -> bool { + ::is_ssz_fixed_len() + } - Ok(($type(value), i)) + fn ssz_fixed_len() -> usize { + ::ssz_fixed_len() + } + + fn from_ssz_bytes(bytes: &[u8]) -> Result { + Ok($type(u64::from_ssz_bytes(bytes)?)) } } diff --git a/eth2/types/src/slot_height.rs b/eth2/types/src/slot_height.rs index f7a34cbba..75b253333 100644 --- a/eth2/types/src/slot_height.rs +++ b/eth2/types/src/slot_height.rs @@ -2,7 +2,7 @@ use crate::slot_epoch::{Epoch, Slot}; use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; -use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +use ssz::{ssz_encode, Decodable, DecodeError, Encodable}; use std::cmp::{Ord, Ordering}; use std::fmt; use std::hash::{Hash, Hasher}; diff --git a/eth2/types/src/tree_hash_vector.rs b/eth2/types/src/tree_hash_vector.rs index 42a730f25..333249dec 100644 --- a/eth2/types/src/tree_hash_vector.rs +++ b/eth2/types/src/tree_hash_vector.rs @@ -1,7 +1,7 @@ use crate::test_utils::{RngCore, TestRandom}; use cached_tree_hash::CachedTreeHash; use serde_derive::{Deserialize, Serialize}; -use ssz::{Decodable, DecodeError, Encodable, SszStream}; +use ssz::{Decodable, DecodeError, Encodable}; use std::ops::{Deref, DerefMut}; use tree_hash::TreeHash; diff --git a/eth2/utils/bls/src/fake_aggregate_signature.rs b/eth2/utils/bls/src/fake_aggregate_signature.rs index 829a280e1..0ea41b1a2 100644 --- a/eth2/utils/bls/src/fake_aggregate_signature.rs +++ b/eth2/utils/bls/src/fake_aggregate_signature.rs @@ -3,7 +3,7 @@ use cached_tree_hash::cached_tree_hash_ssz_encoding_as_vector; use serde::de::{Deserialize, Deserializer}; use serde::ser::{Serialize, Serializer}; use serde_hex::{encode as hex_encode, PrefixedHexVisitor}; -use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +use ssz::{ssz_encode, Decodable, DecodeError, Encodable}; use tree_hash::tree_hash_ssz_encoding_as_vector; /// A BLS aggregate signature. diff --git a/eth2/utils/bls/src/fake_signature.rs b/eth2/utils/bls/src/fake_signature.rs index a10e40323..40347c49d 100644 --- a/eth2/utils/bls/src/fake_signature.rs +++ b/eth2/utils/bls/src/fake_signature.rs @@ -4,7 +4,7 @@ use hex::encode as hex_encode; use serde::de::{Deserialize, Deserializer}; use serde::ser::{Serialize, Serializer}; use serde_hex::HexVisitor; -use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream}; +use ssz::{ssz_encode, Decodable, DecodeError, Encodable}; use tree_hash::tree_hash_ssz_encoding_as_vector; /// A single BLS signature. diff --git a/eth2/utils/ssz_derive/src/lib.rs b/eth2/utils/ssz_derive/src/lib.rs index 9548ea380..57abd5468 100644 --- a/eth2/utils/ssz_derive/src/lib.rs +++ b/eth2/utils/ssz_derive/src/lib.rs @@ -93,7 +93,7 @@ pub fn ssz_encode_derive(input: TokenStream) -> TokenStream { } fn ssz_fixed_len() -> usize { - if ::is_ssz_fixed_len() { + if ::is_ssz_fixed_len() { #( <#field_types_b as ssz::Encodable>::ssz_fixed_len() + )* @@ -161,7 +161,7 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream { if should_skip_deserializing(field) { // Field should not be deserialized; use a `Default` impl to instantiate. decodes.push(quote! { - #ident: <_>::default(), + #ident: <_>::default() }); } else { let ty = &field.ty;