mirror of
https://github.com/sigp/lighthouse.git
synced 2026-06-29 10:54:24 +00:00
Merge branch 'master' into validator-record-update
This commit is contained in:
@@ -8,5 +8,5 @@ edition = "2018"
|
||||
bls = { path = "../utils/bls" }
|
||||
boolean-bitfield = { path = "../utils/boolean-bitfield" }
|
||||
ethereum-types = "0.4.0"
|
||||
rand = "0.3"
|
||||
rand = "0.5.5"
|
||||
ssz = { path = "../utils/ssz" }
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
use super::attestation_data::SSZ_ATTESTION_DATA_LENGTH;
|
||||
use super::bls::{AggregateSignature, BLS_AGG_SIG_BYTE_SIZE};
|
||||
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream, LENGTH_BYTES};
|
||||
use super::bls::AggregateSignature;
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::{AttestationData, Bitfield};
|
||||
|
||||
pub const MIN_SSZ_ATTESTION_RECORD_LENGTH: usize = {
|
||||
SSZ_ATTESTION_DATA_LENGTH + // data
|
||||
5 + // participation_bitfield (assuming 1 byte of bitfield)
|
||||
5 + // custody_bitfield (assuming 1 byte of bitfield)
|
||||
LENGTH_BYTES + BLS_AGG_SIG_BYTE_SIZE // aggregate sig
|
||||
};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Attestation {
|
||||
@@ -23,7 +17,7 @@ impl Encodable for Attestation {
|
||||
s.append(&self.data);
|
||||
s.append(&self.participation_bitfield);
|
||||
s.append(&self.custody_bitfield);
|
||||
s.append_vec(&self.aggregate_sig.as_bytes());
|
||||
s.append(&self.aggregate_sig);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,9 +26,7 @@ impl Decodable for Attestation {
|
||||
let (data, i) = AttestationData::ssz_decode(bytes, i)?;
|
||||
let (participation_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
|
||||
let (custody_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
|
||||
let (agg_sig_bytes, i) = decode_ssz_list(bytes, i)?;
|
||||
let aggregate_sig =
|
||||
AggregateSignature::from_bytes(&agg_sig_bytes).map_err(|_| DecodeError::TooShort)?; // also could be TooLong
|
||||
let (aggregate_sig, i) = AggregateSignature::ssz_decode(bytes, i)?;
|
||||
|
||||
let attestation_record = Self {
|
||||
data,
|
||||
@@ -57,30 +49,31 @@ impl Attestation {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Attestation {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
data: <_>::random_for_test(rng),
|
||||
participation_bitfield: <_>::random_for_test(rng),
|
||||
custody_bitfield: <_>::random_for_test(rng),
|
||||
aggregate_sig: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_attestation_record_min_ssz_length() {
|
||||
let ar = Attestation::zero();
|
||||
let ssz = ssz_encode(&ar);
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = Attestation::random_for_test(&mut rng);
|
||||
|
||||
assert_eq!(ssz.len(), MIN_SSZ_ATTESTION_RECORD_LENGTH);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_attestation_record_ssz_round_trip() {
|
||||
let original = Attestation {
|
||||
data: AttestationData::zero(),
|
||||
participation_bitfield: Bitfield::from_bytes(&vec![17; 42][..]),
|
||||
custody_bitfield: Bitfield::from_bytes(&vec![18; 12][..]),
|
||||
aggregate_sig: AggregateSignature::new(),
|
||||
};
|
||||
|
||||
let ssz = ssz_encode(&original);
|
||||
let (decoded, _) = Attestation::ssz_decode(&ssz, 0).unwrap();
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
|
||||
8 + // slot
|
||||
@@ -83,27 +85,35 @@ impl Decodable for AttestationData {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for AttestationData {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
slot: <_>::random_for_test(rng),
|
||||
shard: <_>::random_for_test(rng),
|
||||
beacon_block_hash: <_>::random_for_test(rng),
|
||||
epoch_boundary_hash: <_>::random_for_test(rng),
|
||||
shard_block_hash: <_>::random_for_test(rng),
|
||||
latest_crosslink_hash: <_>::random_for_test(rng),
|
||||
justified_slot: <_>::random_for_test(rng),
|
||||
justified_block_hash: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_attestation_record_ssz_round_trip() {
|
||||
let original = AttestationData {
|
||||
slot: 42,
|
||||
shard: 16,
|
||||
beacon_block_hash: Hash256::from("beacon".as_bytes()),
|
||||
epoch_boundary_hash: Hash256::from("epoch".as_bytes()),
|
||||
shard_block_hash: Hash256::from("shard".as_bytes()),
|
||||
latest_crosslink_hash: Hash256::from("xlink".as_bytes()),
|
||||
justified_slot: 8,
|
||||
justified_block_hash: Hash256::from("justified".as_bytes()),
|
||||
};
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = AttestationData::random_for_test(&mut rng);
|
||||
|
||||
let ssz = ssz_encode(&original);
|
||||
|
||||
let (decoded, _) = AttestationData::ssz_decode(&ssz, 0).unwrap();
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
|
||||
@@ -1,142 +1,86 @@
|
||||
use super::attestation::Attestation;
|
||||
use super::special_record::SpecialRecord;
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::Hash256;
|
||||
use super::{BeaconBlockBody, Hash256};
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::AggregateSignature;
|
||||
use rand::RngCore;
|
||||
|
||||
pub const MIN_SSZ_BLOCK_LENGTH: usize = {
|
||||
8 + // slot
|
||||
32 + // randao_reveal
|
||||
32 + // pow_chain_reference
|
||||
4 + // ancestor hashes (assuming empty)
|
||||
32 + // active_state_root
|
||||
32 + // crystallized_state_root
|
||||
4 + // attestations (assuming empty)
|
||||
4 // specials (assuming empty)
|
||||
};
|
||||
pub const MAX_SSZ_BLOCK_LENGTH: usize = MIN_SSZ_BLOCK_LENGTH + (1 << 24);
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct BeaconBlock {
|
||||
pub slot: u64,
|
||||
pub parent_root: Hash256,
|
||||
pub state_root: Hash256,
|
||||
pub randao_reveal: Hash256,
|
||||
pub pow_chain_reference: Hash256,
|
||||
pub ancestor_hashes: Vec<Hash256>,
|
||||
pub active_state_root: Hash256,
|
||||
pub crystallized_state_root: Hash256,
|
||||
pub attestations: Vec<Attestation>,
|
||||
pub specials: Vec<SpecialRecord>,
|
||||
}
|
||||
|
||||
impl BeaconBlock {
|
||||
pub fn zero() -> Self {
|
||||
Self {
|
||||
slot: 0,
|
||||
randao_reveal: Hash256::zero(),
|
||||
pow_chain_reference: Hash256::zero(),
|
||||
ancestor_hashes: vec![],
|
||||
active_state_root: Hash256::zero(),
|
||||
crystallized_state_root: Hash256::zero(),
|
||||
attestations: vec![],
|
||||
specials: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a reference to `ancestor_hashes[0]`.
|
||||
///
|
||||
/// The first hash in `ancestor_hashes` is the parent of the block.
|
||||
pub fn parent_hash(&self) -> Option<&Hash256> {
|
||||
self.ancestor_hashes.get(0)
|
||||
}
|
||||
pub candidate_pow_receipt_root: Hash256,
|
||||
pub signature: AggregateSignature,
|
||||
pub body: BeaconBlockBody,
|
||||
}
|
||||
|
||||
impl Encodable for BeaconBlock {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.slot);
|
||||
s.append(&self.parent_root);
|
||||
s.append(&self.state_root);
|
||||
s.append(&self.randao_reveal);
|
||||
s.append(&self.pow_chain_reference);
|
||||
s.append_vec(&self.ancestor_hashes);
|
||||
s.append(&self.active_state_root);
|
||||
s.append(&self.crystallized_state_root);
|
||||
s.append_vec(&self.attestations);
|
||||
s.append_vec(&self.specials);
|
||||
s.append(&self.candidate_pow_receipt_root);
|
||||
s.append(&self.signature);
|
||||
s.append(&self.body);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for BeaconBlock {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (slot, i) = u64::ssz_decode(bytes, i)?;
|
||||
let (randao_reveal, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (pow_chain_reference, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (ancestor_hashes, i) = Decodable::ssz_decode(bytes, i)?;
|
||||
let (active_state_root, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (crystallized_state_root, i) = Hash256::ssz_decode(bytes, i)?;
|
||||
let (attestations, i) = Decodable::ssz_decode(bytes, i)?;
|
||||
let (specials, i) = Decodable::ssz_decode(bytes, i)?;
|
||||
let block = BeaconBlock {
|
||||
slot,
|
||||
randao_reveal,
|
||||
pow_chain_reference,
|
||||
ancestor_hashes,
|
||||
active_state_root,
|
||||
crystallized_state_root,
|
||||
attestations,
|
||||
specials,
|
||||
};
|
||||
Ok((block, i))
|
||||
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,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for BeaconBlock {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
slot: <_>::random_for_test(rng),
|
||||
parent_root: <_>::random_for_test(rng),
|
||||
state_root: <_>::random_for_test(rng),
|
||||
randao_reveal: <_>::random_for_test(rng),
|
||||
candidate_pow_receipt_root: <_>::random_for_test(rng),
|
||||
signature: <_>::random_for_test(rng),
|
||||
body: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
fn test_block_zero() {
|
||||
let b = BeaconBlock::zero();
|
||||
assert_eq!(b.slot, 0);
|
||||
assert!(b.randao_reveal.is_zero());
|
||||
assert!(b.pow_chain_reference.is_zero());
|
||||
assert_eq!(b.ancestor_hashes, vec![]);
|
||||
assert!(b.active_state_root.is_zero());
|
||||
assert!(b.crystallized_state_root.is_zero());
|
||||
assert_eq!(b.attestations.len(), 0);
|
||||
assert_eq!(b.specials.len(), 0);
|
||||
}
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = BeaconBlock::random_for_test(&mut rng);
|
||||
|
||||
#[test]
|
||||
pub fn test_block_ssz_encode_decode() {
|
||||
let mut b = BeaconBlock::zero();
|
||||
b.ancestor_hashes = vec![Hash256::zero(); 32];
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(&b);
|
||||
let ssz = ssz_stream.drain();
|
||||
|
||||
let (b_decoded, _) = BeaconBlock::ssz_decode(&ssz, 0).unwrap();
|
||||
|
||||
assert_eq!(b, b_decoded);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_block_min_ssz_length() {
|
||||
let b = BeaconBlock::zero();
|
||||
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(&b);
|
||||
let ssz = ssz_stream.drain();
|
||||
|
||||
assert_eq!(ssz.len(), MIN_SSZ_BLOCK_LENGTH);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_block_parent_hash() {
|
||||
let mut b = BeaconBlock::zero();
|
||||
b.ancestor_hashes = vec![
|
||||
Hash256::from("cats".as_bytes()),
|
||||
Hash256::from("dogs".as_bytes()),
|
||||
Hash256::from("birds".as_bytes()),
|
||||
];
|
||||
|
||||
assert_eq!(b.parent_hash().unwrap(), &Hash256::from("cats".as_bytes()));
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::{Attestation, CasperSlashing, Deposit, Exit, ProposerSlashing};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
pub struct BeaconBlockBody {
|
||||
pub proposer_slashings: Vec<ProposerSlashing>,
|
||||
pub casper_slashings: Vec<CasperSlashing>,
|
||||
pub attestations: Vec<Attestation>,
|
||||
pub deposits: Vec<Deposit>,
|
||||
pub exits: Vec<Exit>,
|
||||
}
|
||||
|
||||
impl Encodable for BeaconBlockBody {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
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) = <_>::ssz_decode(bytes, i)?;
|
||||
let (casper_slashings, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (attestations, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (deposits, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (exits, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
proposer_slashings,
|
||||
casper_slashings,
|
||||
attestations,
|
||||
deposits,
|
||||
exits,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for BeaconBlockBody {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
proposer_slashings: <_>::random_for_test(rng),
|
||||
casper_slashings: <_>::random_for_test(rng),
|
||||
attestations: <_>::random_for_test(rng),
|
||||
deposits: <_>::random_for_test(rng),
|
||||
exits: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = BeaconBlockBody::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
|
||||
60
beacon_chain/types/src/casper_slashing.rs
Normal file
60
beacon_chain/types/src/casper_slashing.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::SlashableVoteData;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct CasperSlashing {
|
||||
pub slashable_vote_data_1: SlashableVoteData,
|
||||
pub slashable_vote_data_2: SlashableVoteData,
|
||||
}
|
||||
|
||||
impl Encodable for CasperSlashing {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.slashable_vote_data_1);
|
||||
s.append(&self.slashable_vote_data_2);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for CasperSlashing {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (slashable_vote_data_1, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (slashable_vote_data_2, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
CasperSlashing {
|
||||
slashable_vote_data_1,
|
||||
slashable_vote_data_2,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for CasperSlashing {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
slashable_vote_data_1: <_>::random_for_test(rng),
|
||||
slashable_vote_data_2: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = CasperSlashing::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
64
beacon_chain/types/src/deposit.rs
Normal file
64
beacon_chain/types/src/deposit.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::{DepositData, Hash256};
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Deposit {
|
||||
pub merkle_branch: Vec<Hash256>,
|
||||
pub merkle_tree_index: u64,
|
||||
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) = <_>::ssz_decode(bytes, i)?;
|
||||
let (merkle_tree_index, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (deposit_data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
merkle_branch,
|
||||
merkle_tree_index,
|
||||
deposit_data,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Deposit {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
merkle_branch: <_>::random_for_test(rng),
|
||||
merkle_tree_index: <_>::random_for_test(rng),
|
||||
deposit_data: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = Deposit::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
65
beacon_chain/types/src/deposit_data.rs
Normal file
65
beacon_chain/types/src/deposit_data.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::DepositInput;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[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,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for DepositData {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
deposit_input: <_>::random_for_test(rng),
|
||||
value: <_>::random_for_test(rng),
|
||||
timestamp: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = DepositData::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
72
beacon_chain/types/src/deposit_input.rs
Normal file
72
beacon_chain/types/src/deposit_input.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::{PublicKey, Signature};
|
||||
use rand::RngCore;
|
||||
|
||||
#[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,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for DepositInput {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
pubkey: <_>::random_for_test(rng),
|
||||
withdrawal_credentials: <_>::random_for_test(rng),
|
||||
randao_commitment: <_>::random_for_test(rng),
|
||||
proof_of_possession: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = DepositInput::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
65
beacon_chain/types/src/exit.rs
Normal file
65
beacon_chain/types/src/exit.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::Signature;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Exit {
|
||||
pub slot: u64,
|
||||
pub validator_index: u32,
|
||||
pub signature: Signature,
|
||||
}
|
||||
|
||||
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) = <_>::ssz_decode(bytes, i)?;
|
||||
let (validator_index, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (signature, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
slot,
|
||||
validator_index,
|
||||
signature,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Exit {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
slot: <_>::random_for_test(rng),
|
||||
validator_index: <_>::random_for_test(rng),
|
||||
signature: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = Exit::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
@@ -3,23 +3,34 @@ extern crate boolean_bitfield;
|
||||
extern crate ethereum_types;
|
||||
extern crate ssz;
|
||||
|
||||
pub mod test_utils;
|
||||
|
||||
pub mod active_state;
|
||||
pub mod attestation_data;
|
||||
pub mod attestation;
|
||||
pub mod beacon_block;
|
||||
pub mod beacon_block_body;
|
||||
pub mod beacon_state;
|
||||
pub mod candidate_pow_receipt_root_record;
|
||||
pub mod casper_slashing;
|
||||
pub mod chain_config;
|
||||
pub mod crosslink_record;
|
||||
pub mod crystallized_state;
|
||||
pub mod deposit;
|
||||
pub mod deposit_data;
|
||||
pub mod deposit_input;
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
pub mod exit;
|
||||
>>>>>>> master
|
||||
pub mod fork_data;
|
||||
pub mod pending_attestation_record;
|
||||
pub mod proposal_signed_data;
|
||||
pub mod proposer_slashing;
|
||||
pub mod shard_and_committee;
|
||||
pub mod shard_reassignment_record;
|
||||
pub mod special_record;
|
||||
pub mod slashable_vote_data;
|
||||
pub mod validator_record;
|
||||
|
||||
use self::ethereum_types::{H160, H256, U256};
|
||||
@@ -29,15 +40,24 @@ pub use crate::active_state::ActiveState;
|
||||
pub use crate::attestation_data::AttestationData;
|
||||
pub use crate::attestation::Attestation;
|
||||
pub use crate::beacon_block::BeaconBlock;
|
||||
pub use crate::beacon_block_body::BeaconBlockBody;
|
||||
pub use crate::beacon_state::BeaconState;
|
||||
pub use crate::casper_slashing::CasperSlashing;
|
||||
pub use crate::chain_config::ChainConfig;
|
||||
pub use crate::crosslink_record::CrosslinkRecord;
|
||||
pub use crate::crystallized_state::CrystallizedState;
|
||||
pub use crate::deposit::Deposit;
|
||||
pub use crate::deposit_data::DepositData;
|
||||
pub use crate::deposit_input::DepositInput;
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
pub use crate::exit::Exit;
|
||||
>>>>>>> master
|
||||
pub use crate::fork_data::ForkData;
|
||||
pub use crate::pending_attestation_record::PendingAttestationRecord;
|
||||
pub use crate::proposal_signed_data::ProposalSignedData;
|
||||
pub use crate::proposer_slashing::ProposerSlashing;
|
||||
pub use crate::slashable_vote_data::SlashableVoteData;
|
||||
pub use crate::shard_and_committee::ShardAndCommittee;
|
||||
pub use crate::special_record::{SpecialRecord, SpecialRecordKind};
|
||||
pub use crate::validator_record::{ValidatorRecord, ValidatorStatus};
|
||||
|
||||
65
beacon_chain/types/src/proposal_signed_data.rs
Normal file
65
beacon_chain/types/src/proposal_signed_data.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::Hash256;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Default)]
|
||||
pub struct ProposalSignedData {
|
||||
pub slot: u64,
|
||||
pub shard: u64,
|
||||
pub block_root: Hash256,
|
||||
}
|
||||
|
||||
impl Encodable for ProposalSignedData {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.slot);
|
||||
s.append(&self.shard);
|
||||
s.append(&self.block_root);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for ProposalSignedData {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (shard, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (block_root, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
ProposalSignedData {
|
||||
slot,
|
||||
shard,
|
||||
block_root,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for ProposalSignedData {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
slot: <_>::random_for_test(rng),
|
||||
shard: <_>::random_for_test(rng),
|
||||
block_root: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = ProposalSignedData::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
76
beacon_chain/types/src/proposer_slashing.rs
Normal file
76
beacon_chain/types/src/proposer_slashing.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::ProposalSignedData;
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::Signature;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct ProposerSlashing {
|
||||
pub proposer_index: u32,
|
||||
pub proposal_data_1: ProposalSignedData,
|
||||
pub proposal_signature_1: Signature,
|
||||
pub proposal_data_2: ProposalSignedData,
|
||||
pub proposal_signature_2: Signature,
|
||||
}
|
||||
|
||||
impl Encodable for ProposerSlashing {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append(&self.proposer_index);
|
||||
s.append(&self.proposal_data_1);
|
||||
s.append(&self.proposal_signature_1);
|
||||
s.append(&self.proposal_data_2);
|
||||
s.append(&self.proposal_signature_2);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for ProposerSlashing {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (proposer_index, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (proposal_data_1, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (proposal_signature_1, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (proposal_data_2, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (proposal_signature_2, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
ProposerSlashing {
|
||||
proposer_index,
|
||||
proposal_data_1,
|
||||
proposal_signature_1,
|
||||
proposal_data_2,
|
||||
proposal_signature_2,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for ProposerSlashing {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
proposer_index: <_>::random_for_test(rng),
|
||||
proposal_data_1: <_>::random_for_test(rng),
|
||||
proposal_signature_1: <_>::random_for_test(rng),
|
||||
proposal_data_2: <_>::random_for_test(rng),
|
||||
proposal_signature_2: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = ProposerSlashing::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
71
beacon_chain/types/src/slashable_vote_data.rs
Normal file
71
beacon_chain/types/src/slashable_vote_data.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||
use super::AttestationData;
|
||||
use crate::test_utils::TestRandom;
|
||||
use bls::AggregateSignature;
|
||||
use rand::RngCore;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct SlashableVoteData {
|
||||
pub aggregate_signature_poc_0_indices: Vec<u32>,
|
||||
pub aggregate_signature_poc_1_indices: Vec<u32>,
|
||||
pub data: AttestationData,
|
||||
pub aggregate_signature: AggregateSignature,
|
||||
}
|
||||
|
||||
impl Encodable for SlashableVoteData {
|
||||
fn ssz_append(&self, s: &mut SszStream) {
|
||||
s.append_vec(&self.aggregate_signature_poc_0_indices);
|
||||
s.append_vec(&self.aggregate_signature_poc_1_indices);
|
||||
s.append(&self.data);
|
||||
s.append(&self.aggregate_signature);
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for SlashableVoteData {
|
||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||
let (aggregate_signature_poc_0_indices, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (aggregate_signature_poc_1_indices, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (data, i) = <_>::ssz_decode(bytes, i)?;
|
||||
let (aggregate_signature, i) = <_>::ssz_decode(bytes, i)?;
|
||||
|
||||
Ok((
|
||||
SlashableVoteData {
|
||||
aggregate_signature_poc_0_indices,
|
||||
aggregate_signature_poc_1_indices,
|
||||
data,
|
||||
aggregate_signature,
|
||||
},
|
||||
i,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for SlashableVoteData {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
Self {
|
||||
aggregate_signature_poc_0_indices: <_>::random_for_test(rng),
|
||||
aggregate_signature_poc_1_indices: <_>::random_for_test(rng),
|
||||
data: <_>::random_for_test(rng),
|
||||
aggregate_signature: <_>::random_for_test(rng),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::TestRandom;
|
||||
use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||
let original = SlashableVoteData::random_for_test(&mut rng);
|
||||
|
||||
let bytes = ssz_encode(&original);
|
||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||
|
||||
assert_eq!(original, decoded);
|
||||
}
|
||||
}
|
||||
12
beacon_chain/types/src/test_utils/aggregate_signature.rs
Normal file
12
beacon_chain/types/src/test_utils/aggregate_signature.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use super::TestRandom;
|
||||
use bls::{AggregateSignature, Signature};
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for AggregateSignature {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
let signature = Signature::random_for_test(rng);
|
||||
let mut aggregate_signature = AggregateSignature::new();
|
||||
aggregate_signature.add(&signature);
|
||||
aggregate_signature
|
||||
}
|
||||
}
|
||||
11
beacon_chain/types/src/test_utils/bitfield.rs
Normal file
11
beacon_chain/types/src/test_utils/bitfield.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use super::super::Bitfield;
|
||||
use super::TestRandom;
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Bitfield {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
let mut raw_bytes = vec![0; 32];
|
||||
rng.fill_bytes(&mut raw_bytes);
|
||||
Bitfield::from_bytes(&raw_bytes)
|
||||
}
|
||||
}
|
||||
11
beacon_chain/types/src/test_utils/hash256.rs
Normal file
11
beacon_chain/types/src/test_utils/hash256.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use super::TestRandom;
|
||||
use crate::Hash256;
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Hash256 {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
let mut key_bytes = vec![0; 32];
|
||||
rng.fill_bytes(&mut key_bytes);
|
||||
Hash256::from(&key_bytes[..])
|
||||
}
|
||||
}
|
||||
40
beacon_chain/types/src/test_utils/mod.rs
Normal file
40
beacon_chain/types/src/test_utils/mod.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use rand::RngCore;
|
||||
|
||||
pub use rand::{prng::XorShiftRng, SeedableRng};
|
||||
|
||||
pub mod aggregate_signature;
|
||||
pub mod bitfield;
|
||||
pub mod hash256;
|
||||
pub mod signature;
|
||||
pub mod secret_key;
|
||||
pub mod public_key;
|
||||
|
||||
pub trait TestRandom<T>
|
||||
where T: RngCore
|
||||
{
|
||||
fn random_for_test(rng: &mut T) -> Self;
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for u64 {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
rng.next_u64()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for u32 {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
rng.next_u32()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RngCore, U> TestRandom<T> for Vec<U>
|
||||
where U: TestRandom<T>
|
||||
{
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
vec![
|
||||
<U>::random_for_test(rng),
|
||||
<U>::random_for_test(rng),
|
||||
<U>::random_for_test(rng),
|
||||
]
|
||||
}
|
||||
}
|
||||
10
beacon_chain/types/src/test_utils/public_key.rs
Normal file
10
beacon_chain/types/src/test_utils/public_key.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use super::TestRandom;
|
||||
use bls::{PublicKey, SecretKey};
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for PublicKey {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
let secret_key = SecretKey::random_for_test(rng);
|
||||
PublicKey::from_secret_key(&secret_key)
|
||||
}
|
||||
}
|
||||
19
beacon_chain/types/src/test_utils/secret_key.rs
Normal file
19
beacon_chain/types/src/test_utils/secret_key.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use super::TestRandom;
|
||||
use bls::SecretKey;
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for SecretKey {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
let mut key_bytes = vec![0; 48];
|
||||
rng.fill_bytes(&mut key_bytes);
|
||||
/*
|
||||
* An `unreachable!` is used here as there's no reason why you cannot constuct a key from a
|
||||
* fixed-length byte slice. Also, this should only be used during testing so a panic is
|
||||
* acceptable.
|
||||
*/
|
||||
match SecretKey::from_bytes(&key_bytes) {
|
||||
Ok(key) => key,
|
||||
Err(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
13
beacon_chain/types/src/test_utils/signature.rs
Normal file
13
beacon_chain/types/src/test_utils/signature.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use super::TestRandom;
|
||||
use bls::{SecretKey, Signature};
|
||||
use rand::RngCore;
|
||||
|
||||
impl<T: RngCore> TestRandom<T> for Signature {
|
||||
fn random_for_test(rng: &mut T) -> Self {
|
||||
let secret_key = SecretKey::random_for_test(rng);
|
||||
let mut message = vec![0; 32];
|
||||
rng.fill_bytes(&mut message);
|
||||
|
||||
Signature::new(&message, &secret_key)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user