Implement ssz enc/decode for beaconblock

Not yet fully tested
This commit is contained in:
Paul Hauner 2018-12-19 17:56:09 +11:00
parent 0260564c15
commit c9f82f8a1d
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
4 changed files with 134 additions and 9 deletions

View File

@ -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<Exit>,
}
/*
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,
))
}
}
*/

View File

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

View File

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

View File

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