Add types, more ssz testing for existing types
This commit is contained in:
parent
c9f82f8a1d
commit
368a218af4
@ -13,7 +13,6 @@ pub struct BeaconBlock {
|
|||||||
pub body: BeaconBlockBody,
|
pub body: BeaconBlockBody,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
impl Encodable for BeaconBlock {
|
impl Encodable for BeaconBlock {
|
||||||
fn ssz_append(&self, s: &mut SszStream) {
|
fn ssz_append(&self, s: &mut SszStream) {
|
||||||
s.append(&self.slot);
|
s.append(&self.slot);
|
||||||
@ -21,8 +20,32 @@ impl Encodable for BeaconBlock {
|
|||||||
s.append(&self.state_root);
|
s.append(&self.state_root);
|
||||||
s.append(&self.randao_reveal);
|
s.append(&self.randao_reveal);
|
||||||
s.append(&self.candidate_pow_receipt_root);
|
s.append(&self.candidate_pow_receipt_root);
|
||||||
s.append_vec(&self.signature.as_bytes());
|
s.append(&self.signature);
|
||||||
s.append(&self.body);
|
s.append(&self.body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
impl Decodable for BeaconBlock {
|
||||||
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
|
let (slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (parent_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (state_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (randao_reveal, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (candidate_pow_receipt_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (signature, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (body, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Self {
|
||||||
|
slot,
|
||||||
|
parent_root,
|
||||||
|
state_root,
|
||||||
|
randao_reveal,
|
||||||
|
candidate_pow_receipt_root,
|
||||||
|
signature,
|
||||||
|
body,
|
||||||
|
},
|
||||||
|
i,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -16,8 +16,8 @@ impl Encodable for CasperSlashing {
|
|||||||
|
|
||||||
impl Decodable for CasperSlashing {
|
impl Decodable for CasperSlashing {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
let (slashable_vote_data_1, i) = SlashableVoteData::ssz_decode(bytes, i)?;
|
let (slashable_vote_data_1, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (slashable_vote_data_2, i) = SlashableVoteData::ssz_decode(bytes, i)?;
|
let (slashable_vote_data_2, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
CasperSlashing {
|
CasperSlashing {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||||
use super::Hash256;
|
use super::{DepositData, Hash256};
|
||||||
use bls::{AggregateSignature, PublicKey};
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct Deposit {
|
pub struct Deposit {
|
||||||
@ -20,8 +19,8 @@ impl Encodable for Deposit {
|
|||||||
impl Decodable for Deposit {
|
impl Decodable for Deposit {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
let (merkle_branch, i) = decode_ssz_list(bytes, i)?;
|
let (merkle_branch, i) = decode_ssz_list(bytes, i)?;
|
||||||
let (merkle_tree_index, i) = u64::ssz_decode(bytes, i)?;
|
let (merkle_tree_index, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (deposit_data, i) = DepositData::ssz_decode(bytes, i)?;
|
let (deposit_data, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Self {
|
Self {
|
||||||
@ -34,71 +33,38 @@ impl Decodable for Deposit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[cfg(test)]
|
||||||
pub struct DepositData {
|
mod tests {
|
||||||
pub deposit_input: DepositInput,
|
use super::super::ssz::ssz_encode;
|
||||||
pub value: u64,
|
use super::super::{DepositInput, Hash256};
|
||||||
pub timestamp: u64,
|
use super::*;
|
||||||
}
|
use bls::{Keypair, Signature};
|
||||||
|
|
||||||
impl Encodable for DepositData {
|
#[test]
|
||||||
fn ssz_append(&self, s: &mut SszStream) {
|
pub fn test_ssz_round_trip() {
|
||||||
s.append(&self.deposit_input);
|
let keypair = Keypair::random();
|
||||||
s.append(&self.value);
|
|
||||||
s.append(&self.timestamp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decodable for DepositData {
|
let original = Deposit {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
merkle_branch: vec![
|
||||||
let (deposit_input, i) = DepositInput::ssz_decode(bytes, i)?;
|
Hash256::from("one".as_bytes()),
|
||||||
let (value, i) = u64::ssz_decode(bytes, i)?;
|
Hash256::from("two".as_bytes()),
|
||||||
let (timestamp, i) = u64::ssz_decode(bytes, i)?;
|
],
|
||||||
|
merkle_tree_index: 19,
|
||||||
Ok((
|
deposit_data: DepositData {
|
||||||
Self {
|
deposit_input: DepositInput {
|
||||||
deposit_input,
|
pubkey: keypair.pk,
|
||||||
value,
|
withdrawal_credentials: Hash256::from("cats".as_bytes()),
|
||||||
timestamp,
|
randao_commitment: Hash256::from("dogs".as_bytes()),
|
||||||
|
proof_of_possession: Signature::new(&[42, 42], &keypair.sk),
|
||||||
},
|
},
|
||||||
i,
|
value: 12,
|
||||||
))
|
timestamp: 100,
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
|
||||||
pub struct DepositInput {
|
|
||||||
pub pubkey: PublicKey,
|
|
||||||
pub withdrawal_credentials: Hash256,
|
|
||||||
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,
|
};
|
||||||
))
|
|
||||||
|
let bytes = ssz_encode(&original);
|
||||||
|
let (decoded, _) = Deposit::ssz_decode(&bytes, 0).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
63
beacon_chain/types/src/deposit_data.rs
Normal file
63
beacon_chain/types/src/deposit_data.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||||
|
use super::DepositInput;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct DepositData {
|
||||||
|
pub deposit_input: DepositInput,
|
||||||
|
pub value: u64,
|
||||||
|
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) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (value, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (timestamp, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Self {
|
||||||
|
deposit_input,
|
||||||
|
value,
|
||||||
|
timestamp,
|
||||||
|
},
|
||||||
|
i,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::super::ssz::ssz_encode;
|
||||||
|
use super::super::Hash256;
|
||||||
|
use super::*;
|
||||||
|
use bls::{Keypair, Signature};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_ssz_round_trip() {
|
||||||
|
let keypair = Keypair::random();
|
||||||
|
|
||||||
|
let original = DepositData {
|
||||||
|
deposit_input: DepositInput {
|
||||||
|
pubkey: keypair.pk,
|
||||||
|
withdrawal_credentials: Hash256::from("cats".as_bytes()),
|
||||||
|
randao_commitment: Hash256::from("dogs".as_bytes()),
|
||||||
|
proof_of_possession: Signature::new(&[42, 42], &keypair.sk),
|
||||||
|
},
|
||||||
|
value: 12,
|
||||||
|
timestamp: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
let bytes = ssz_encode(&original);
|
||||||
|
let (decoded, _) = DepositData::ssz_decode(&bytes, 0).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(original, decoded);
|
||||||
|
}
|
||||||
|
}
|
64
beacon_chain/types/src/deposit_input.rs
Normal file
64
beacon_chain/types/src/deposit_input.rs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||||
|
use super::Hash256;
|
||||||
|
use bls::{PublicKey, Signature};
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct DepositInput {
|
||||||
|
pub pubkey: PublicKey,
|
||||||
|
pub withdrawal_credentials: Hash256,
|
||||||
|
pub randao_commitment: Hash256,
|
||||||
|
pub proof_of_possession: Signature,
|
||||||
|
}
|
||||||
|
|
||||||
|
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) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (proof_of_possession, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
Self {
|
||||||
|
pubkey,
|
||||||
|
withdrawal_credentials,
|
||||||
|
randao_commitment,
|
||||||
|
proof_of_possession,
|
||||||
|
},
|
||||||
|
i,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::super::ssz::ssz_encode;
|
||||||
|
use super::*;
|
||||||
|
use bls::{Keypair, Signature};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_ssz_round_trip() {
|
||||||
|
let keypair = Keypair::random();
|
||||||
|
|
||||||
|
let original = DepositInput {
|
||||||
|
pubkey: keypair.pk,
|
||||||
|
withdrawal_credentials: Hash256::from("cats".as_bytes()),
|
||||||
|
randao_commitment: Hash256::from("dogs".as_bytes()),
|
||||||
|
proof_of_possession: Signature::new(&[42, 42], &keypair.sk),
|
||||||
|
};
|
||||||
|
|
||||||
|
let bytes = ssz_encode(&original);
|
||||||
|
let (decoded, _) = DepositInput::ssz_decode(&bytes, 0).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(original, decoded);
|
||||||
|
}
|
||||||
|
}
|
@ -18,9 +18,9 @@ impl Encodable for Exit {
|
|||||||
|
|
||||||
impl Decodable for Exit {
|
impl Decodable for Exit {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
let (slot, i) = u64::ssz_decode(bytes, i)?;
|
let (slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (validator_index, i) = u32::ssz_decode(bytes, i)?;
|
let (validator_index, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (signature, i) = AggregateSignature::ssz_decode(bytes, i)?;
|
let (signature, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Self {
|
Self {
|
||||||
@ -32,3 +32,29 @@ impl Decodable for Exit {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::super::ssz::ssz_encode;
|
||||||
|
use super::*;
|
||||||
|
use bls::{AggregateSignature, Keypair, Signature};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_ssz_round_trip() {
|
||||||
|
let keypair = Keypair::random();
|
||||||
|
let single_signature = Signature::new(&[42, 42], &keypair.sk);
|
||||||
|
let mut signature = AggregateSignature::new();
|
||||||
|
signature.add(&single_signature);
|
||||||
|
|
||||||
|
let original = Exit {
|
||||||
|
slot: 42,
|
||||||
|
validator_index: 12,
|
||||||
|
signature,
|
||||||
|
};
|
||||||
|
|
||||||
|
let bytes = ssz_encode(&original);
|
||||||
|
let (decoded, _) = Exit::ssz_decode(&bytes, 0).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(original, decoded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -15,6 +15,8 @@ pub mod chain_config;
|
|||||||
pub mod crosslink_record;
|
pub mod crosslink_record;
|
||||||
pub mod crystallized_state;
|
pub mod crystallized_state;
|
||||||
pub mod deposit;
|
pub mod deposit;
|
||||||
|
pub mod deposit_data;
|
||||||
|
pub mod deposit_input;
|
||||||
pub mod exit;
|
pub mod exit;
|
||||||
pub mod fork_data;
|
pub mod fork_data;
|
||||||
pub mod pending_attestation_record;
|
pub mod pending_attestation_record;
|
||||||
@ -40,7 +42,9 @@ pub use casper_slashing::CasperSlashing;
|
|||||||
pub use chain_config::ChainConfig;
|
pub use chain_config::ChainConfig;
|
||||||
pub use crosslink_record::CrosslinkRecord;
|
pub use crosslink_record::CrosslinkRecord;
|
||||||
pub use crystallized_state::CrystallizedState;
|
pub use crystallized_state::CrystallizedState;
|
||||||
pub use deposit::{Deposit, DepositData, DepositInput};
|
pub use deposit::Deposit;
|
||||||
|
pub use deposit_data::DepositData;
|
||||||
|
pub use deposit_input::DepositInput;
|
||||||
pub use exit::Exit;
|
pub use exit::Exit;
|
||||||
pub use fork_data::ForkData;
|
pub use fork_data::ForkData;
|
||||||
pub use pending_attestation_record::PendingAttestationRecord;
|
pub use pending_attestation_record::PendingAttestationRecord;
|
||||||
|
@ -18,9 +18,9 @@ impl Encodable for ProposalSignedData {
|
|||||||
|
|
||||||
impl Decodable for ProposalSignedData {
|
impl Decodable for ProposalSignedData {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
let (slot, i) = u64::ssz_decode(bytes, i)?;
|
let (slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (shard, i) = u64::ssz_decode(bytes, i)?;
|
let (shard, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (block_root, i) = Hash256::ssz_decode(bytes, i)?;
|
let (block_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
ProposalSignedData {
|
ProposalSignedData {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||||
use super::ProposalSignedData;
|
use super::ProposalSignedData;
|
||||||
use bls::Signature;
|
use bls::Signature;
|
||||||
|
|
||||||
@ -23,11 +23,11 @@ impl Encodable for ProposerSlashing {
|
|||||||
|
|
||||||
impl Decodable for ProposerSlashing {
|
impl Decodable for ProposerSlashing {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
let (proposer_index, i) = u32::ssz_decode(bytes, i)?;
|
let (proposer_index, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (proposal_data_1, i) = ProposalSignedData::ssz_decode(bytes, i)?;
|
let (proposal_data_1, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (proposal_signature_1, i) = Signature::ssz_decode(bytes, i)?;
|
let (proposal_signature_1, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (proposal_data_2, i) = ProposalSignedData::ssz_decode(bytes, i)?;
|
let (proposal_data_2, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (proposal_signature_2, i) = Signature::ssz_decode(bytes, i)?;
|
let (proposal_signature_2, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
ProposerSlashing {
|
ProposerSlashing {
|
||||||
|
@ -23,8 +23,8 @@ impl Decodable for SlashableVoteData {
|
|||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
let (aggregate_signature_poc_0_indices, i) = decode_ssz_list(bytes, i)?;
|
let (aggregate_signature_poc_0_indices, i) = decode_ssz_list(bytes, i)?;
|
||||||
let (aggregate_signature_poc_1_indices, i) = decode_ssz_list(bytes, i)?;
|
let (aggregate_signature_poc_1_indices, i) = decode_ssz_list(bytes, i)?;
|
||||||
let (data, i) = AttestationData::ssz_decode(bytes, i)?;
|
let (data, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (aggregate_signature, i) = AggregateSignature::ssz_decode(bytes, i)?;
|
let (aggregate_signature, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
SlashableVoteData {
|
SlashableVoteData {
|
||||||
|
Loading…
Reference in New Issue
Block a user