Remove SSZ helpers
This commit is contained in:
parent
7f00f167e3
commit
be4c6701ac
@ -44,7 +44,6 @@ members = [
|
||||
"beacon_chain/utils/honey-badger-split",
|
||||
"beacon_chain/utils/slot-clock",
|
||||
"beacon_chain/utils/ssz",
|
||||
"beacon_chain/utils/ssz_helpers",
|
||||
"beacon_chain/utils/vec_shuffle",
|
||||
"beacon_chain/validator_change",
|
||||
"beacon_chain/validator_induction",
|
||||
|
@ -8,5 +8,4 @@ bls = { path = "../utils/bls" }
|
||||
db = { path = "../../lighthouse/db" }
|
||||
hashing = { path = "../utils/hashing" }
|
||||
ssz = { path = "../utils/ssz" }
|
||||
ssz_helpers = { path = "../utils/ssz_helpers" }
|
||||
types = { path = "../types" }
|
||||
|
@ -2,7 +2,6 @@ extern crate bls;
|
||||
extern crate db;
|
||||
extern crate hashing;
|
||||
extern crate ssz;
|
||||
extern crate ssz_helpers;
|
||||
extern crate types;
|
||||
|
||||
#[macro_use]
|
||||
|
@ -8,7 +8,6 @@ bls = { path = "../utils/bls" }
|
||||
db = { path = "../../lighthouse/db" }
|
||||
naive_fork_choice = { path = "../naive_fork_choice" }
|
||||
ssz = { path = "../utils/ssz" }
|
||||
ssz_helpers = { path = "../utils/ssz_helpers" }
|
||||
state-transition = { path = "../state-transition" }
|
||||
types = { path = "../types" }
|
||||
validator_induction = { path = "../validator_induction" }
|
||||
|
@ -1,7 +1,6 @@
|
||||
extern crate db;
|
||||
extern crate naive_fork_choice;
|
||||
extern crate ssz;
|
||||
extern crate ssz_helpers;
|
||||
extern crate state_transition;
|
||||
extern crate types;
|
||||
extern crate validator_induction;
|
||||
|
@ -1,10 +0,0 @@
|
||||
[package]
|
||||
name = "ssz_helpers"
|
||||
version = "0.1.0"
|
||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||
|
||||
[dependencies]
|
||||
bls = { path = "../bls" }
|
||||
hashing = { path = "../hashing" }
|
||||
types = { path = "../../types" }
|
||||
ssz = { path = "../ssz" }
|
@ -1,162 +0,0 @@
|
||||
use super::bls::BLS_AGG_SIG_BYTE_SIZE;
|
||||
use super::ssz::decode::decode_length;
|
||||
use super::ssz::LENGTH_BYTES;
|
||||
use super::types::attestation::MIN_SSZ_ATTESTION_RECORD_LENGTH;
|
||||
use super::types::attestation_data::SSZ_ATTESTION_DATA_LENGTH;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum AttestationSplitError {
|
||||
TooShort,
|
||||
}
|
||||
|
||||
/// Given some ssz slice, find the bounds of each serialized Attestation and return a vec of
|
||||
/// slices point to each.
|
||||
pub fn split_all_attestations<'a>(
|
||||
full_ssz: &'a [u8],
|
||||
index: usize,
|
||||
) -> Result<Vec<&'a [u8]>, AttestationSplitError> {
|
||||
let mut v = vec![];
|
||||
let mut index = index;
|
||||
while index < full_ssz.len() - 1 {
|
||||
let (slice, i) = split_one_attestation(full_ssz, index)?;
|
||||
v.push(slice);
|
||||
index = i;
|
||||
}
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// Given some ssz slice, find the bounds of one serialized Attestation
|
||||
/// and return a slice pointing to that.
|
||||
pub fn split_one_attestation(
|
||||
full_ssz: &[u8],
|
||||
index: usize,
|
||||
) -> Result<(&[u8], usize), AttestationSplitError> {
|
||||
let length = determine_ssz_attestation_len(full_ssz, index)?;
|
||||
let end = index + length;
|
||||
|
||||
// The check to ensure that the slice exists _should_ be redundant as it is already checked in
|
||||
// `determine_ssz_attestation_len`, however it is checked here again for additional safety
|
||||
// against panics.
|
||||
match full_ssz.get(index..end) {
|
||||
None => Err(AttestationSplitError::TooShort),
|
||||
Some(slice) => Ok((slice, end)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Given some SSZ, assume that a serialized `Attestation` begins at the `index` position and
|
||||
/// attempt to find the length (in bytes) of that serialized `Attestation`.
|
||||
///
|
||||
/// This function does not perform validation on the `Attestation`. It is very likely that
|
||||
/// given some sufficiently long non-`Attestation` bytes it will not raise an error.
|
||||
fn determine_ssz_attestation_len(
|
||||
full_ssz: &[u8],
|
||||
index: usize,
|
||||
) -> Result<usize, AttestationSplitError> {
|
||||
if full_ssz.len() < MIN_SSZ_ATTESTION_RECORD_LENGTH {
|
||||
return Err(AttestationSplitError::TooShort);
|
||||
}
|
||||
|
||||
let data_struct_end = index + SSZ_ATTESTION_DATA_LENGTH;
|
||||
|
||||
// Determine the end of the first bitfield.
|
||||
let participation_bitfield_len = decode_length(full_ssz, data_struct_end, LENGTH_BYTES)
|
||||
.map_err(|_| AttestationSplitError::TooShort)?;
|
||||
let participation_bitfield_end = data_struct_end + LENGTH_BYTES + participation_bitfield_len;
|
||||
|
||||
// Determine the end of the second bitfield.
|
||||
let custody_bitfield_len = decode_length(full_ssz, participation_bitfield_end, LENGTH_BYTES)
|
||||
.map_err(|_| AttestationSplitError::TooShort)?;
|
||||
let custody_bitfield_end = participation_bitfield_end + LENGTH_BYTES + custody_bitfield_len;
|
||||
|
||||
// Determine the very end of the Attestation.
|
||||
let agg_sig_end = custody_bitfield_end + LENGTH_BYTES + BLS_AGG_SIG_BYTE_SIZE;
|
||||
|
||||
if agg_sig_end > full_ssz.len() {
|
||||
Err(AttestationSplitError::TooShort)
|
||||
} else {
|
||||
Ok(agg_sig_end - index)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::bls::AggregateSignature;
|
||||
use super::super::ssz::{Decodable, SszStream};
|
||||
use super::super::types::{Attestation, AttestationData, Bitfield, Hash256};
|
||||
use super::*;
|
||||
|
||||
fn get_two_records() -> Vec<Attestation> {
|
||||
let a = Attestation {
|
||||
data: AttestationData {
|
||||
slot: 7,
|
||||
shard: 9,
|
||||
beacon_block_hash: Hash256::from("a_beacon".as_bytes()),
|
||||
epoch_boundary_hash: Hash256::from("a_epoch".as_bytes()),
|
||||
shard_block_hash: Hash256::from("a_shard".as_bytes()),
|
||||
latest_crosslink_hash: Hash256::from("a_xlink".as_bytes()),
|
||||
justified_slot: 19,
|
||||
justified_block_hash: Hash256::from("a_justified".as_bytes()),
|
||||
},
|
||||
participation_bitfield: Bitfield::from_bytes(&vec![17; 42][..]),
|
||||
custody_bitfield: Bitfield::from_bytes(&vec![255; 12][..]),
|
||||
aggregate_sig: AggregateSignature::new(),
|
||||
};
|
||||
let b = Attestation {
|
||||
data: AttestationData {
|
||||
slot: 9,
|
||||
shard: 7,
|
||||
beacon_block_hash: Hash256::from("b_beacon".as_bytes()),
|
||||
epoch_boundary_hash: Hash256::from("b_epoch".as_bytes()),
|
||||
shard_block_hash: Hash256::from("b_shard".as_bytes()),
|
||||
latest_crosslink_hash: Hash256::from("b_xlink".as_bytes()),
|
||||
justified_slot: 15,
|
||||
justified_block_hash: Hash256::from("b_justified".as_bytes()),
|
||||
},
|
||||
participation_bitfield: Bitfield::from_bytes(&vec![1; 42][..]),
|
||||
custody_bitfield: Bitfield::from_bytes(&vec![11; 3][..]),
|
||||
aggregate_sig: AggregateSignature::new(),
|
||||
};
|
||||
vec![a, b]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_attestation_ssz_split() {
|
||||
let ars = get_two_records();
|
||||
let a = ars[0].clone();
|
||||
let b = ars[1].clone();
|
||||
|
||||
/*
|
||||
* Test split one
|
||||
*/
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(&a);
|
||||
let ssz = ssz_stream.drain();
|
||||
let (a_ssz, i) = split_one_attestation(&ssz, 0).unwrap();
|
||||
assert_eq!(i, ssz.len());
|
||||
let (decoded_a, _) = Attestation::ssz_decode(a_ssz, 0).unwrap();
|
||||
assert_eq!(a, decoded_a);
|
||||
|
||||
/*
|
||||
* Test split two
|
||||
*/
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(&a);
|
||||
ssz_stream.append(&b);
|
||||
let ssz = ssz_stream.drain();
|
||||
let ssz_vec = split_all_attestations(&ssz, 0).unwrap();
|
||||
let (decoded_a, _) = Attestation::ssz_decode(ssz_vec[0], 0).unwrap();
|
||||
let (decoded_b, _) = Attestation::ssz_decode(ssz_vec[1], 0).unwrap();
|
||||
assert_eq!(a, decoded_a);
|
||||
assert_eq!(b, decoded_b);
|
||||
|
||||
/*
|
||||
* Test split two with shortened ssz
|
||||
*/
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(&a);
|
||||
ssz_stream.append(&b);
|
||||
let ssz = ssz_stream.drain();
|
||||
let ssz = &ssz[0..ssz.len() - 1];
|
||||
assert!(split_all_attestations(&ssz, 0).is_err());
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
extern crate bls;
|
||||
extern crate hashing;
|
||||
extern crate ssz;
|
||||
extern crate types;
|
||||
|
||||
pub mod attestation_ssz_splitter;
|
||||
pub mod ssz_beacon_block;
|
@ -1,480 +0,0 @@
|
||||
use super::hashing::canonical_hash;
|
||||
use super::ssz::decode::{decode_length, Decodable};
|
||||
use super::types::beacon_block::{MAX_SSZ_BLOCK_LENGTH, MIN_SSZ_BLOCK_LENGTH};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum SszBeaconBlockError {
|
||||
TooShort,
|
||||
TooLong,
|
||||
}
|
||||
|
||||
/*
|
||||
* Constants used for navigating the SSZ bytes.
|
||||
*/
|
||||
const LENGTH_PREFIX_BYTES: usize = 4;
|
||||
const SLOT_BYTES: usize = 8;
|
||||
const HASH_SIZE: usize = 32;
|
||||
const RANDAO_REVEAL_BYTES: usize = HASH_SIZE;
|
||||
const POW_CHAIN_REF_BYTES: usize = HASH_SIZE;
|
||||
const ACTIVE_STATE_BYTES: usize = HASH_SIZE;
|
||||
const CRYSTALLIZED_STATE_BYTES: usize = HASH_SIZE;
|
||||
|
||||
/// Allows for reading of block values directly from serialized ssz bytes.
|
||||
///
|
||||
/// The purpose of this struct is to provide the functionality to read block fields directly from
|
||||
/// some serialized SSZ slice allowing us to read the block without fully
|
||||
/// de-serializing it.
|
||||
///
|
||||
/// This struct should be as "zero-copy" as possible. The `ssz` field is a reference to some slice
|
||||
/// and each function reads from that slice.
|
||||
///
|
||||
/// Use this to perform intial checks before we fully de-serialize a block. It should only really
|
||||
/// be used to verify blocks that come in from the network, for internal operations we should use a
|
||||
/// full `BeaconBlock`.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct SszBeaconBlock<'a> {
|
||||
ssz: &'a [u8],
|
||||
block_ssz_len: usize,
|
||||
// Ancestors
|
||||
ancestors_position: usize,
|
||||
ancestors_len: usize,
|
||||
// Attestations
|
||||
attestations_position: usize,
|
||||
attestations_len: usize,
|
||||
// Specials
|
||||
specials_position: usize,
|
||||
specials_len: usize,
|
||||
}
|
||||
|
||||
impl<'a> SszBeaconBlock<'a> {
|
||||
/// Create a new instance from a slice reference.
|
||||
///
|
||||
/// This function will validate the length of the ssz string, however it will not validate the
|
||||
/// contents.
|
||||
///
|
||||
/// The returned `SszBeaconBlock` instance will contain a `len` field which can be used to determine
|
||||
/// how many bytes were read from the slice. In the case of multiple, sequentually serialized
|
||||
/// blocks `len` can be used to assume the location of the next serialized block.
|
||||
pub fn from_slice(vec: &'a [u8]) -> Result<Self, SszBeaconBlockError> {
|
||||
let untrimmed_ssz = &vec[..];
|
||||
|
||||
/*
|
||||
* Ensure the SSZ is long enough to be a block
|
||||
*/
|
||||
if vec.len() < MIN_SSZ_BLOCK_LENGTH {
|
||||
return Err(SszBeaconBlockError::TooShort);
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure the SSZ slice isn't longer than is possible for a block.
|
||||
*/
|
||||
if vec.len() > MAX_SSZ_BLOCK_LENGTH {
|
||||
return Err(SszBeaconBlockError::TooLong);
|
||||
}
|
||||
|
||||
/*
|
||||
* Determine how many bytes are used to store ancestor hashes.
|
||||
*/
|
||||
let ancestors_position = SLOT_BYTES + RANDAO_REVEAL_BYTES + POW_CHAIN_REF_BYTES;
|
||||
let ancestors_len = decode_length(untrimmed_ssz, ancestors_position, LENGTH_PREFIX_BYTES)
|
||||
.map_err(|_| SszBeaconBlockError::TooShort)?;
|
||||
|
||||
/*
|
||||
* Determine how many bytes are used to store attestation records.
|
||||
*/
|
||||
let attestations_position = ancestors_position + LENGTH_PREFIX_BYTES + ancestors_len + // end of ancestor bytes
|
||||
ACTIVE_STATE_BYTES +
|
||||
CRYSTALLIZED_STATE_BYTES;
|
||||
let attestations_len =
|
||||
decode_length(untrimmed_ssz, attestations_position, LENGTH_PREFIX_BYTES)
|
||||
.map_err(|_| SszBeaconBlockError::TooShort)?;
|
||||
|
||||
/*
|
||||
* Determine how many bytes are used to store specials.
|
||||
*/
|
||||
let specials_position = attestations_position + LENGTH_PREFIX_BYTES + attestations_len;
|
||||
let specials_len = decode_length(untrimmed_ssz, specials_position, LENGTH_PREFIX_BYTES)
|
||||
.map_err(|_| SszBeaconBlockError::TooShort)?;
|
||||
|
||||
/*
|
||||
* Now that all variable field lengths are known (ancestors, attestations, specials) we can
|
||||
* know the exact length of the block and reject it if the slice is too short.
|
||||
*/
|
||||
let block_ssz_len = MIN_SSZ_BLOCK_LENGTH + ancestors_len + attestations_len + specials_len;
|
||||
if vec.len() < block_ssz_len {
|
||||
return Err(SszBeaconBlockError::TooShort);
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
ssz: &untrimmed_ssz[0..block_ssz_len],
|
||||
block_ssz_len,
|
||||
ancestors_position,
|
||||
ancestors_len,
|
||||
attestations_position,
|
||||
attestations_len,
|
||||
specials_position,
|
||||
specials_len,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.ssz.len()
|
||||
}
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.ssz.is_empty()
|
||||
}
|
||||
|
||||
/// Returns this block as ssz.
|
||||
///
|
||||
/// Does not include any excess ssz bytes that were supplied to this struct.
|
||||
pub fn block_ssz(&self) -> &'a [u8] {
|
||||
&self.ssz[0..self.block_ssz_len]
|
||||
}
|
||||
|
||||
/// Return the canonical hash for this block.
|
||||
pub fn block_hash(&self) -> Vec<u8> {
|
||||
canonical_hash(&self.ssz)
|
||||
}
|
||||
|
||||
/// Return the bytes representing `ancestor_hashes[0]`.
|
||||
///
|
||||
/// The first hash in `ancestor_hashes` is the parent of the block.
|
||||
pub fn parent_hash(&self) -> Option<&[u8]> {
|
||||
let ancestor_ssz = self.ancestor_hashes();
|
||||
let start = LENGTH_PREFIX_BYTES;
|
||||
ancestor_ssz.get(start..start + HASH_SIZE)
|
||||
}
|
||||
|
||||
/// Return the `slot` field.
|
||||
pub fn slot(&self) -> u64 {
|
||||
/*
|
||||
* An error should be unreachable from this decode
|
||||
* because we checked the length of the array at
|
||||
* the initalization of this struct.
|
||||
*
|
||||
* If you can make this function panic, please report
|
||||
* it to paul@sigmaprime.io
|
||||
*/
|
||||
if let Ok((n, _)) = u64::ssz_decode(&self.ssz, 0) {
|
||||
n
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the `randao_reveal` field.
|
||||
pub fn randao_reveal(&self) -> &[u8] {
|
||||
let start = SLOT_BYTES;
|
||||
&self.ssz[start..start + RANDAO_REVEAL_BYTES]
|
||||
}
|
||||
|
||||
/// Return the `pow_chain_reference` field.
|
||||
pub fn pow_chain_reference(&self) -> &[u8] {
|
||||
let start = SLOT_BYTES + RANDAO_REVEAL_BYTES;
|
||||
&self.ssz[start..start + POW_CHAIN_REF_BYTES]
|
||||
}
|
||||
|
||||
/// Return the serialized `ancestor_hashes` bytes, including length prefix.
|
||||
pub fn ancestor_hashes(&self) -> &[u8] {
|
||||
let start = self.ancestors_position;
|
||||
&self.ssz[start..(start + self.ancestors_len + LENGTH_PREFIX_BYTES)]
|
||||
}
|
||||
|
||||
/// Return the `active_state_root` field.
|
||||
pub fn act_state_root(&self) -> &[u8] {
|
||||
let start = self.ancestors_position + LENGTH_PREFIX_BYTES + self.ancestors_len;
|
||||
&self.ssz[start..(start + 32)]
|
||||
}
|
||||
|
||||
/// Return the `active_state_root` field.
|
||||
pub fn cry_state_root(&self) -> &[u8] {
|
||||
let start =
|
||||
self.ancestors_position + LENGTH_PREFIX_BYTES + self.ancestors_len + ACTIVE_STATE_BYTES;
|
||||
&self.ssz[start..(start + 32)]
|
||||
}
|
||||
|
||||
/// Return the serialized `attestations` bytes, including length prefix.
|
||||
pub fn attestations(&self) -> &[u8] {
|
||||
let start = self.attestations_position;
|
||||
&self.ssz[start..(start + self.attestations_len + LENGTH_PREFIX_BYTES)]
|
||||
}
|
||||
|
||||
/// Return the serialized `attestations` bytes _without_ the length prefix.
|
||||
pub fn attestations_without_length(&self) -> &[u8] {
|
||||
let start = self.attestations_position + LENGTH_PREFIX_BYTES;
|
||||
&self.ssz[start..start + self.attestations_len]
|
||||
}
|
||||
|
||||
/// Return the serialized `specials` bytes, including length prefix.
|
||||
pub fn specials(&self) -> &[u8] {
|
||||
let start = self.specials_position;
|
||||
&self.ssz[start..(start + self.specials_len + LENGTH_PREFIX_BYTES)]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::ssz::encode::encode_length;
|
||||
use super::super::ssz::SszStream;
|
||||
use super::super::types::Hash256;
|
||||
use super::super::types::{Attestation, BeaconBlock, SpecialRecord};
|
||||
use super::*;
|
||||
|
||||
fn get_block_ssz(b: &BeaconBlock) -> Vec<u8> {
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(b);
|
||||
ssz_stream.drain()
|
||||
}
|
||||
|
||||
fn get_special_record_ssz(sr: &SpecialRecord) -> Vec<u8> {
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(sr);
|
||||
ssz_stream.drain()
|
||||
}
|
||||
|
||||
fn get_attestation_record_ssz(ar: &Attestation) -> Vec<u8> {
|
||||
let mut ssz_stream = SszStream::new();
|
||||
ssz_stream.append(ar);
|
||||
ssz_stream.drain()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_zero_attestation_records() {
|
||||
let mut b = BeaconBlock::zero();
|
||||
b.attestations = vec![];
|
||||
let ssz = get_block_ssz(&b);
|
||||
|
||||
assert!(SszBeaconBlock::from_slice(&ssz[..]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_single_attestation_record_one_byte_short() {
|
||||
let mut b = BeaconBlock::zero();
|
||||
b.attestations = vec![Attestation::zero()];
|
||||
let ssz = get_block_ssz(&b);
|
||||
|
||||
assert_eq!(
|
||||
SszBeaconBlock::from_slice(&ssz[0..(ssz.len() - 1)]),
|
||||
Err(SszBeaconBlockError::TooShort)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_single_attestation_record_one_byte_long() {
|
||||
let mut b = BeaconBlock::zero();
|
||||
b.attestations = vec![Attestation::zero()];
|
||||
let mut ssz = get_block_ssz(&b);
|
||||
let original_len = ssz.len();
|
||||
ssz.push(42);
|
||||
|
||||
let ssz_block = SszBeaconBlock::from_slice(&ssz[..]).unwrap();
|
||||
|
||||
assert_eq!(ssz_block.len(), original_len);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_single_attestation_record() {
|
||||
let mut b = BeaconBlock::zero();
|
||||
b.attestations = vec![Attestation::zero()];
|
||||
let ssz = get_block_ssz(&b);
|
||||
|
||||
assert!(SszBeaconBlock::from_slice(&ssz[..]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_block_hash() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
let hash = ssz_block.block_hash();
|
||||
// Note: this hash was not generated by some external program,
|
||||
// it was simply printed then copied into the code. This test
|
||||
// will tell us if the hash changes, not that it matches some
|
||||
// canonical reference.
|
||||
let expected_hash = [
|
||||
254, 192, 124, 164, 240, 137, 162, 126, 50, 255, 118, 88, 189, 151, 221, 4, 40, 121,
|
||||
198, 33, 248, 221, 104, 255, 46, 234, 146, 161, 202, 140, 109, 175,
|
||||
];
|
||||
assert_eq!(hash, expected_hash);
|
||||
|
||||
/*
|
||||
* Test if you give the SszBeaconBlock too many ssz bytes
|
||||
*/
|
||||
let mut too_long = serialized.clone();
|
||||
too_long.push(42);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&too_long).unwrap();
|
||||
let hash = ssz_block.block_hash();
|
||||
assert_eq!(hash, expected_hash);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_slot() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
block.slot = 42;
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
|
||||
assert_eq!(ssz_block.slot(), 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_randao_reveal() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
let reference_hash = Hash256::from([42_u8; 32]);
|
||||
block.randao_reveal = reference_hash.clone();
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
|
||||
assert_eq!(ssz_block.randao_reveal(), &reference_hash.to_vec()[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_ancestor_hashes() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
let h = Hash256::from(&vec![42_u8; 32][..]);
|
||||
block.ancestor_hashes.push(h);
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
|
||||
let mut expected = encode_length(32, LENGTH_PREFIX_BYTES);
|
||||
expected.append(&mut h.to_vec());
|
||||
|
||||
assert_eq!(ssz_block.ancestor_hashes(), &expected[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_parent_hash() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.ancestor_hashes = vec![
|
||||
Hash256::from("cats".as_bytes()),
|
||||
Hash256::from("dogs".as_bytes()),
|
||||
Hash256::from("birds".as_bytes()),
|
||||
];
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
ssz_block.parent_hash().unwrap(),
|
||||
&Hash256::from("cats".as_bytes()).to_vec()[..]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_specials() {
|
||||
/*
|
||||
* Without data
|
||||
*/
|
||||
let mut block = BeaconBlock::zero();
|
||||
let s = SpecialRecord::logout(&[]);
|
||||
block.specials.push(s.clone());
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
let sr_ssz = get_special_record_ssz(&s);
|
||||
|
||||
let mut expected = encode_length(sr_ssz.len(), LENGTH_PREFIX_BYTES);
|
||||
expected.append(&mut sr_ssz.to_vec());
|
||||
|
||||
assert_eq!(ssz_block.specials(), &expected[..]);
|
||||
|
||||
/*
|
||||
* With data
|
||||
*/
|
||||
let mut block = BeaconBlock::zero();
|
||||
let s = SpecialRecord::randao_change(&[16, 17, 18]);
|
||||
block.specials.push(s.clone());
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
let sr_ssz = get_special_record_ssz(&s);
|
||||
|
||||
let mut expected = encode_length(sr_ssz.len(), LENGTH_PREFIX_BYTES);
|
||||
expected.append(&mut sr_ssz.to_vec());
|
||||
|
||||
assert_eq!(ssz_block.specials(), &expected[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_attestations() {
|
||||
/*
|
||||
* Single Attestation
|
||||
*/
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
let ssz_ar = get_attestation_record_ssz(&Attestation::zero());
|
||||
|
||||
let mut expected = encode_length(ssz_ar.len(), LENGTH_PREFIX_BYTES);
|
||||
expected.append(&mut ssz_ar.to_vec());
|
||||
|
||||
assert_eq!(ssz_block.attestations(), &expected[..]);
|
||||
|
||||
/*
|
||||
* Multiple Attestations
|
||||
*/
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
block.attestations.push(Attestation::zero());
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
let mut ssz_ar = get_attestation_record_ssz(&Attestation::zero());
|
||||
ssz_ar.append(&mut get_attestation_record_ssz(&Attestation::zero()));
|
||||
|
||||
let mut expected = encode_length(ssz_ar.len(), LENGTH_PREFIX_BYTES);
|
||||
expected.append(&mut ssz_ar.to_vec());
|
||||
|
||||
assert_eq!(ssz_block.attestations(), &expected[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_pow_chain_reference() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
let reference_hash = Hash256::from([42_u8; 32]);
|
||||
block.pow_chain_reference = reference_hash.clone();
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
ssz_block.pow_chain_reference(),
|
||||
&reference_hash.to_vec()[..]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_act_state_root() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
let reference_hash = Hash256::from([42_u8; 32]);
|
||||
block.active_state_root = reference_hash.clone();
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
|
||||
assert_eq!(ssz_block.act_state_root(), &reference_hash.to_vec()[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ssz_block_cry_state_root() {
|
||||
let mut block = BeaconBlock::zero();
|
||||
block.attestations.push(Attestation::zero());
|
||||
let reference_hash = Hash256::from([42_u8; 32]);
|
||||
block.crystallized_state_root = reference_hash.clone();
|
||||
|
||||
let serialized = get_block_ssz(&block);
|
||||
let ssz_block = SszBeaconBlock::from_slice(&serialized).unwrap();
|
||||
|
||||
assert_eq!(ssz_block.cry_state_root(), &reference_hash.to_vec()[..]);
|
||||
}
|
||||
}
|
@ -9,5 +9,4 @@ bls = { path = "../../beacon_chain/utils/bls" }
|
||||
bytes = "0.4.10"
|
||||
rocksdb = "0.10.1"
|
||||
ssz = { path = "../../beacon_chain/utils/ssz" }
|
||||
ssz_helpers = { path = "../../beacon_chain/utils/ssz_helpers" }
|
||||
types = { path = "../../beacon_chain/types" }
|
||||
|
Loading…
Reference in New Issue
Block a user