diff --git a/eth2/types/Cargo.toml b/eth2/types/Cargo.toml index af53fa597..b3c3f7462 100644 --- a/eth2/types/Cargo.toml +++ b/eth2/types/Cargo.toml @@ -10,4 +10,7 @@ boolean-bitfield = { path = "../utils/boolean-bitfield" } ethereum-types = "0.4.0" hashing = { path = "../utils/hashing" } rand = "0.5.5" +serde = "1.0" +serde_derive = "1.0" +serde_json = "1.0" ssz = { path = "../utils/ssz" } diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 9720472b8..d6738bd6e 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -2,9 +2,10 @@ use super::{AttestationData, Bitfield}; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize)] pub struct Attestation { pub data: AttestationData, pub aggregation_bitfield: Bitfield, diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index 5639099f0..aab5f7563 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -1,6 +1,7 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; pub const SSZ_ATTESTION_DATA_LENGTH: usize = { @@ -14,7 +15,7 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 32 // justified_block_root }; -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq, Default, Serialize)] pub struct AttestationData { pub slot: u64, pub shard: u64, diff --git a/eth2/types/src/attestation_data_and_custody_bit.rs b/eth2/types/src/attestation_data_and_custody_bit.rs index 1b3b2ea3b..8200abf30 100644 --- a/eth2/types/src/attestation_data_and_custody_bit.rs +++ b/eth2/types/src/attestation_data_and_custody_bit.rs @@ -1,9 +1,10 @@ use super::AttestationData; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq, Default, Serialize)] pub struct AttestationDataAndCustodyBit { pub data: AttestationData, pub custody_bit: bool, diff --git a/eth2/types/src/beacon_block/mod.rs b/eth2/types/src/beacon_block/mod.rs index b65f1b053..3e07572d1 100644 --- a/eth2/types/src/beacon_block/mod.rs +++ b/eth2/types/src/beacon_block/mod.rs @@ -2,11 +2,12 @@ use super::{BeaconBlockBody, Eth1Data, Hash256}; use crate::test_utils::TestRandom; use bls::Signature; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; mod signing; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct BeaconBlock { pub slot: u64, pub parent_root: Hash256, diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index 8084c38d9..ad9ec7ea6 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -1,6 +1,7 @@ use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing}; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // The following types are just dummy classes as they will not be defined until @@ -9,7 +10,7 @@ type CustodyReseed = usize; type CustodyChallenge = usize; type CustodyResponse = usize; -#[derive(Debug, PartialEq, Clone, Default)] +#[derive(Debug, PartialEq, Clone, Default, Serialize)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub casper_slashings: Vec, diff --git a/eth2/types/src/beacon_state/mod.rs b/eth2/types/src/beacon_state/mod.rs index bedd12233..71c4d1e87 100644 --- a/eth2/types/src/beacon_state/mod.rs +++ b/eth2/types/src/beacon_state/mod.rs @@ -7,6 +7,7 @@ use super::validator::Validator; use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; mod slot_advance; @@ -16,7 +17,7 @@ pub use self::slot_advance::Error as SlotProcessingError; // Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used. type CustodyChallenge = usize; -#[derive(Debug, PartialEq, Clone, Default)] +#[derive(Debug, PartialEq, Clone, Default, Serialize)] pub struct BeaconState { // Misc pub slot: u64, diff --git a/eth2/types/src/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index 6b10988d6..0eab069b4 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -1,9 +1,10 @@ use super::SlashableVoteData; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct CasperSlashing { pub slashable_vote_data_1: SlashableVoteData, pub slashable_vote_data_2: SlashableVoteData, diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index 5fbaa3342..f727d7b43 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -1,9 +1,10 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize)] pub struct Crosslink { pub slot: u64, pub shard_block_root: Hash256, diff --git a/eth2/types/src/deposit.rs b/eth2/types/src/deposit.rs index cc0c3c524..85b002101 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -1,9 +1,10 @@ use super::{DepositData, Hash256}; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct Deposit { pub merkle_branch: Vec, pub merkle_tree_index: u64, diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index bdc068db3..5c8c302f4 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -1,9 +1,10 @@ use super::DepositInput; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct DepositData { pub amount: u64, pub timestamp: u64, diff --git a/eth2/types/src/deposit_input.rs b/eth2/types/src/deposit_input.rs index bb7bdf5eb..fc53baae9 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -2,9 +2,10 @@ use super::Hash256; use crate::test_utils::TestRandom; use bls::{PublicKey, Signature}; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, diff --git a/eth2/types/src/eth1_data.rs b/eth2/types/src/eth1_data.rs index e8d30109e..6e9bb7d26 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -1,10 +1,11 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default)] +#[derive(Debug, PartialEq, Clone, Default, Serialize)] pub struct Eth1Data { pub deposit_root: Hash256, pub block_hash: Hash256, diff --git a/eth2/types/src/eth1_data_vote.rs b/eth2/types/src/eth1_data_vote.rs index b6917d516..2bfee4d02 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -1,10 +1,11 @@ use super::Eth1Data; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default)] +#[derive(Debug, PartialEq, Clone, Default, Serialize)] pub struct Eth1DataVote { pub eth1_data: Eth1Data, pub vote_count: u64, diff --git a/eth2/types/src/exit.rs b/eth2/types/src/exit.rs index 284a180e7..97f1fd286 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -1,9 +1,10 @@ use crate::test_utils::TestRandom; use bls::Signature; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct Exit { pub slot: u64, pub validator_index: u32, diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index 4c6a7c6a4..c5a06caea 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -1,8 +1,9 @@ use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq, Default, Serialize)] pub struct Fork { pub pre_fork_version: u64, pub post_fork_version: u64, diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index 13990a014..d2af52826 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -1,9 +1,10 @@ use super::{AttestationData, Bitfield}; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize)] pub struct PendingAttestation { pub data: AttestationData, pub aggregation_bitfield: Bitfield, diff --git a/eth2/types/src/proposal_signed_data.rs b/eth2/types/src/proposal_signed_data.rs index 1a433fd5c..829a16987 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -1,9 +1,10 @@ use super::Hash256; use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone, Default)] +#[derive(Debug, PartialEq, Clone, Default, Serialize)] pub struct ProposalSignedData { pub slot: u64, pub shard: u64, diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index 08a165665..a82a37074 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -2,9 +2,10 @@ use super::ProposalSignedData; use crate::test_utils::TestRandom; use bls::Signature; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct ProposerSlashing { pub proposer_index: u32, pub proposal_data_1: ProposalSignedData, diff --git a/eth2/types/src/shard_committee.rs b/eth2/types/src/shard_committee.rs index 943149230..3632cb0c1 100644 --- a/eth2/types/src/shard_committee.rs +++ b/eth2/types/src/shard_committee.rs @@ -1,8 +1,9 @@ use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Serialize)] pub struct ShardCommittee { pub shard: u64, pub committee: Vec, diff --git a/eth2/types/src/shard_reassignment_record.rs b/eth2/types/src/shard_reassignment_record.rs index 5528a92f1..a239233df 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -1,8 +1,9 @@ use crate::test_utils::TestRandom; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct ShardReassignmentRecord { pub validator_index: u64, pub shard: u64, diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index d153da2b9..acffca26d 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -2,9 +2,10 @@ use super::AttestationData; use crate::test_utils::TestRandom; use bls::AggregateSignature; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Serialize)] pub struct SlashableVoteData { pub custody_bit_0_indices: Vec, pub custody_bit_1_indices: Vec, diff --git a/eth2/types/src/special_record.rs b/eth2/types/src/special_record.rs index 21e9c88f6..2ab6f2b5b 100644 --- a/eth2/types/src/special_record.rs +++ b/eth2/types/src/special_record.rs @@ -1,9 +1,10 @@ +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; /// The value of the "type" field of SpecialRecord. /// /// Note: this value must serialize to a u8 and therefore must not be greater than 255. -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone, Copy, Serialize)] pub enum SpecialRecordKind { Logout = 0, CasperSlashing = 1, diff --git a/eth2/types/src/validator.rs b/eth2/types/src/validator.rs index eaf3308b9..708300d9d 100644 --- a/eth2/types/src/validator.rs +++ b/eth2/types/src/validator.rs @@ -2,12 +2,13 @@ use super::Hash256; use crate::test_utils::TestRandom; use bls::PublicKey; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; const STATUS_FLAG_INITIATED_EXIT: u8 = 1; const STATUS_FLAG_WITHDRAWABLE: u8 = 2; -#[derive(Debug, PartialEq, Clone, Copy)] +#[derive(Debug, PartialEq, Clone, Copy, Serialize)] pub enum StatusFlags { InitiatedExit, Withdrawable, @@ -43,7 +44,7 @@ fn status_flag_from_byte(flag: u8) -> Result, StatusFlagsDec } } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize)] pub struct Validator { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs index f673f2a51..196da754d 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -2,10 +2,11 @@ use super::Hash256; use crate::test_utils::TestRandom; use bls::PublicKey; use rand::RngCore; +use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; // The information gathered from the PoW chain validator registration function. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize)] pub struct ValidatorRegistryDeltaBlock { pub latest_registry_delta_root: Hash256, pub validator_index: u32, diff --git a/eth2/utils/bls/Cargo.toml b/eth2/utils/bls/Cargo.toml index 0dc64e483..3b26ee26e 100644 --- a/eth2/utils/bls/Cargo.toml +++ b/eth2/utils/bls/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" bls-aggregates = { git = "https://github.com/sigp/signature-schemes" } hashing = { path = "../hashing" } hex = "0.3" +serde = "1.0" ssz = { path = "../ssz" } diff --git a/eth2/utils/bls/src/aggregate_signature.rs b/eth2/utils/bls/src/aggregate_signature.rs index 505620f4a..6fed183f0 100644 --- a/eth2/utils/bls/src/aggregate_signature.rs +++ b/eth2/utils/bls/src/aggregate_signature.rs @@ -1,6 +1,9 @@ use super::{AggregatePublicKey, Signature}; use bls_aggregates::AggregateSignature as RawAggregateSignature; -use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; +use serde::ser::{Serialize, Serializer}; +use ssz::{ + decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, +}; /// A BLS aggregate signature. /// @@ -44,6 +47,15 @@ impl Decodable for AggregateSignature { } } +impl Serialize for AggregateSignature { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(&ssz_encode(self)) + } +} + impl TreeHash for AggregateSignature { fn hash_tree_root(&self) -> Vec { hash(&self.0.as_bytes()) diff --git a/eth2/utils/bls/src/public_key.rs b/eth2/utils/bls/src/public_key.rs index 5a6bb3ed1..0c2ad81bb 100644 --- a/eth2/utils/bls/src/public_key.rs +++ b/eth2/utils/bls/src/public_key.rs @@ -1,6 +1,7 @@ use super::SecretKey; use bls_aggregates::PublicKey as RawPublicKey; use hex::encode as hex_encode; +use serde::ser::{Serialize, Serializer}; use ssz::{ decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, }; @@ -55,6 +56,15 @@ impl Decodable for PublicKey { } } +impl Serialize for PublicKey { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(&ssz_encode(self)) + } +} + impl TreeHash for PublicKey { fn hash_tree_root(&self) -> Vec { hash(&self.0.as_bytes()) diff --git a/eth2/utils/bls/src/signature.rs b/eth2/utils/bls/src/signature.rs index 59f455523..396e4eab7 100644 --- a/eth2/utils/bls/src/signature.rs +++ b/eth2/utils/bls/src/signature.rs @@ -1,6 +1,9 @@ use super::{PublicKey, SecretKey}; use bls_aggregates::Signature as RawSignature; -use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; +use serde::ser::{Serialize, Serializer}; +use ssz::{ + decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, +}; /// A single BLS signature. /// @@ -63,6 +66,15 @@ impl TreeHash for Signature { } } +impl Serialize for Signature { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(&ssz_encode(self)) + } +} + #[cfg(test)] mod tests { use super::super::Keypair; diff --git a/eth2/utils/boolean-bitfield/Cargo.toml b/eth2/utils/boolean-bitfield/Cargo.toml index b3d05f979..d94b9f7b1 100644 --- a/eth2/utils/boolean-bitfield/Cargo.toml +++ b/eth2/utils/boolean-bitfield/Cargo.toml @@ -7,3 +7,5 @@ edition = "2018" [dependencies] ssz = { path = "../ssz" } bit-vec = "0.5.0" +serde = "1.0" +serde_derive = "1.0" diff --git a/eth2/utils/boolean-bitfield/src/lib.rs b/eth2/utils/boolean-bitfield/src/lib.rs index 33c461361..a00a081be 100644 --- a/eth2/utils/boolean-bitfield/src/lib.rs +++ b/eth2/utils/boolean-bitfield/src/lib.rs @@ -3,6 +3,7 @@ extern crate ssz; use bit_vec::BitVec; +use serde::ser::{Serialize, Serializer}; use std::cmp; use std::default; @@ -149,6 +150,15 @@ impl ssz::Decodable for BooleanBitfield { } } +impl Serialize for BooleanBitfield { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_bytes(&ssz::ssz_encode(self)) + } +} + impl ssz::TreeHash for BooleanBitfield { fn hash_tree_root(&self) -> Vec { self.to_bytes().hash_tree_root()