From c9f82f8a1d94745b145cfc1c3f9002810a587e9e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 19 Dec 2018 17:56:09 +1100 Subject: [PATCH] Implement ssz enc/decode for beaconblock Not yet fully tested --- beacon_chain/types/src/beacon_block_body.rs | 35 ++++++--- beacon_chain/types/src/deposit.rs | 80 +++++++++++++++++++++ beacon_chain/types/src/exit.rs | 26 +++++++ beacon_chain/types/src/proposer_slashing.rs | 2 +- 4 files changed, 134 insertions(+), 9 deletions(-) diff --git a/beacon_chain/types/src/beacon_block_body.rs b/beacon_chain/types/src/beacon_block_body.rs index 66db774b7..f0c767155 100644 --- a/beacon_chain/types/src/beacon_block_body.rs +++ b/beacon_chain/types/src/beacon_block_body.rs @@ -1,4 +1,4 @@ -use super::ssz::{Encodable, SszStream}; +use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream}; use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing}; #[derive(Debug, PartialEq, Clone, Default)] @@ -10,14 +10,33 @@ pub struct BeaconBlockBody { pub exits: Vec, } -/* impl Encodable for BeaconBlockBody { fn ssz_append(&self, s: &mut SszStream) { - s.append(&self.proposer_slashings); - s.append(&self.casper_slashings); - s.append(&self.attestations); - s.append(&self.deposits); - s.append(&self.exits); + s.append_vec(&self.proposer_slashings); + s.append_vec(&self.casper_slashings); + s.append_vec(&self.attestations); + s.append_vec(&self.deposits); + s.append_vec(&self.exits); + } +} + +impl Decodable for BeaconBlockBody { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + let (proposer_slashings, i) = decode_ssz_list(bytes, i)?; + let (casper_slashings, i) = decode_ssz_list(bytes, i)?; + let (attestations, i) = decode_ssz_list(bytes, i)?; + let (deposits, i) = decode_ssz_list(bytes, i)?; + let (exits, i) = decode_ssz_list(bytes, i)?; + + Ok(( + Self { + proposer_slashings, + casper_slashings, + attestations, + deposits, + exits, + }, + i, + )) } } -*/ diff --git a/beacon_chain/types/src/deposit.rs b/beacon_chain/types/src/deposit.rs index 7a6e70409..3b2ad87a6 100644 --- a/beacon_chain/types/src/deposit.rs +++ b/beacon_chain/types/src/deposit.rs @@ -1,3 +1,4 @@ +use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream}; use super::Hash256; use bls::{AggregateSignature, PublicKey}; @@ -8,6 +9,31 @@ pub struct Deposit { pub deposit_data: DepositData, } +impl Encodable for Deposit { + fn ssz_append(&self, s: &mut SszStream) { + s.append_vec(&self.merkle_branch); + s.append(&self.merkle_tree_index); + s.append(&self.deposit_data); + } +} + +impl Decodable for Deposit { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + let (merkle_branch, i) = decode_ssz_list(bytes, i)?; + let (merkle_tree_index, i) = u64::ssz_decode(bytes, i)?; + let (deposit_data, i) = DepositData::ssz_decode(bytes, i)?; + + Ok(( + Self { + merkle_branch, + merkle_tree_index, + deposit_data, + }, + i, + )) + } +} + #[derive(Debug, PartialEq, Clone)] pub struct DepositData { pub deposit_input: DepositInput, @@ -15,6 +41,31 @@ pub struct DepositData { pub timestamp: u64, } +impl Encodable for DepositData { + fn ssz_append(&self, s: &mut SszStream) { + s.append(&self.deposit_input); + s.append(&self.value); + s.append(&self.timestamp); + } +} + +impl Decodable for DepositData { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + let (deposit_input, i) = DepositInput::ssz_decode(bytes, i)?; + let (value, i) = u64::ssz_decode(bytes, i)?; + let (timestamp, i) = u64::ssz_decode(bytes, i)?; + + Ok(( + Self { + deposit_input, + value, + timestamp, + }, + i, + )) + } +} + #[derive(Debug, PartialEq, Clone)] pub struct DepositInput { pub pubkey: PublicKey, @@ -22,3 +73,32 @@ pub struct DepositInput { pub randao_commitment: Hash256, pub proof_of_possession: AggregateSignature, } + +impl Encodable for DepositInput { + fn ssz_append(&self, s: &mut SszStream) { + s.append_vec(&self.pubkey.as_bytes()); + s.append(&self.withdrawal_credentials); + s.append(&self.randao_commitment); + s.append(&self.proof_of_possession); + } +} + +impl Decodable for DepositInput { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + let (pubkey_bytes, i) = decode_ssz_list(bytes, i)?; + let pubkey = PublicKey::from_bytes(&pubkey_bytes).map_err(|_| DecodeError::TooShort)?; + let (withdrawal_credentials, i) = Hash256::ssz_decode(bytes, i)?; + let (randao_commitment, i) = Hash256::ssz_decode(bytes, i)?; + let (proof_of_possession, i) = AggregateSignature::ssz_decode(bytes, i)?; + + Ok(( + Self { + pubkey, + withdrawal_credentials, + randao_commitment, + proof_of_possession, + }, + i, + )) + } +} diff --git a/beacon_chain/types/src/exit.rs b/beacon_chain/types/src/exit.rs index 8de66c630..c4753354d 100644 --- a/beacon_chain/types/src/exit.rs +++ b/beacon_chain/types/src/exit.rs @@ -1,3 +1,4 @@ +use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; use bls::AggregateSignature; #[derive(Debug, PartialEq, Clone)] @@ -6,3 +7,28 @@ pub struct Exit { pub validator_index: u32, pub signature: AggregateSignature, } + +impl Encodable for Exit { + fn ssz_append(&self, s: &mut SszStream) { + s.append(&self.slot); + s.append(&self.validator_index); + s.append(&self.signature); + } +} + +impl Decodable for Exit { + fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { + let (slot, i) = u64::ssz_decode(bytes, i)?; + let (validator_index, i) = u32::ssz_decode(bytes, i)?; + let (signature, i) = AggregateSignature::ssz_decode(bytes, i)?; + + Ok(( + Self { + slot, + validator_index, + signature, + }, + i, + )) + } +} diff --git a/beacon_chain/types/src/proposer_slashing.rs b/beacon_chain/types/src/proposer_slashing.rs index 33d8103b9..0daab8ea8 100644 --- a/beacon_chain/types/src/proposer_slashing.rs +++ b/beacon_chain/types/src/proposer_slashing.rs @@ -1,4 +1,4 @@ -use super::ssz::{Decodable, DecodeError, Encodable, SszStream}; +use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream}; use super::ProposalSignedData; use bls::Signature;