mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-03 17:54:28 +00:00
Move state/block.rs into own mod, add SszBlock
This commit is contained in:
parent
693d635f8c
commit
6c8c4d9b1e
11
lighthouse/state/block/mod.rs
Normal file
11
lighthouse/state/block/mod.rs
Normal file
@ -0,0 +1,11 @@
|
||||
extern crate blake2_rfc;
|
||||
|
||||
use super::ssz;
|
||||
use super::utils;
|
||||
use super::attestation_record;
|
||||
|
||||
mod block;
|
||||
mod ssz_block;
|
||||
|
||||
pub use self::block::Block;
|
||||
pub use self::ssz_block::SszBlock;
|
90
lighthouse/state/block/ssz_block.rs
Normal file
90
lighthouse/state/block/ssz_block.rs
Normal file
@ -0,0 +1,90 @@
|
||||
use super::ssz::decode::{
|
||||
decode_length,
|
||||
Decodable,
|
||||
};
|
||||
use super::utils::hash::canonical_hash;
|
||||
|
||||
pub enum BlockValidatorError {
|
||||
SszInvalid,
|
||||
BadPowHash,
|
||||
SlotTooLow,
|
||||
SlotTooHigh,
|
||||
}
|
||||
|
||||
const LENGTH_BYTES: usize = 4;
|
||||
const MIN_SSZ_BLOCK_LENGTH: usize = {
|
||||
32 + // parent_hash
|
||||
8 + // slot_number
|
||||
32 + // randao_reveal
|
||||
LENGTH_BYTES + // attestations (assuming zero)
|
||||
32 + // pow_chain_ref
|
||||
32 + // active_state_root
|
||||
32 // crystallized_state_root
|
||||
};
|
||||
const MAX_SSZ_BLOCK_LENGTH: usize = MIN_SSZ_BLOCK_LENGTH + 2^24;
|
||||
|
||||
|
||||
pub struct SszBlock<'a> {
|
||||
ssz: &'a [u8],
|
||||
attestation_len: usize,
|
||||
}
|
||||
|
||||
impl<'a> SszBlock<'a> {
|
||||
pub fn from_vec(vec: &'a Vec<u8>)
|
||||
-> Result<Self, BlockValidatorError>
|
||||
{
|
||||
if vec.len() < MIN_SSZ_BLOCK_LENGTH {
|
||||
return Err(BlockValidatorError::SszInvalid);
|
||||
}
|
||||
if vec.len() > MAX_SSZ_BLOCK_LENGTH {
|
||||
return Err(BlockValidatorError::SszInvalid);
|
||||
}
|
||||
let attestation_len = decode_length(&vec[72..76], LENGTH_BYTES)
|
||||
.map_err(|_| BlockValidatorError::SszInvalid)?;
|
||||
// Is the length adequate now we know now many attestation
|
||||
// records exist?
|
||||
if vec.len() < (76 + attestation_len + 96) {
|
||||
return Err(BlockValidatorError::SszInvalid);
|
||||
}
|
||||
Ok(Self{
|
||||
ssz: &vec[..],
|
||||
attestation_len
|
||||
})
|
||||
}
|
||||
|
||||
pub fn block_hash(&self) -> Vec<u8> {
|
||||
canonical_hash(self.ssz)
|
||||
}
|
||||
|
||||
pub fn parent_hash(&self) -> &[u8] {
|
||||
&self.ssz[0..32]
|
||||
}
|
||||
|
||||
pub fn slot_number(&self) -> u64 {
|
||||
u64::ssz_decode(&self.ssz[32..40]).unwrap()
|
||||
}
|
||||
|
||||
pub fn randao_reveal(&self) -> &[u8] {
|
||||
&self.ssz[40..72]
|
||||
}
|
||||
|
||||
pub fn attestations(&self) -> &[u8] {
|
||||
let start = 72 + LENGTH_BYTES;
|
||||
&self.ssz[start..(start + self.attestation_len)]
|
||||
}
|
||||
|
||||
pub fn pow_chain_ref(&self) -> &[u8] {
|
||||
let len = self.ssz.len();
|
||||
&self.ssz[(len - 96)..(len - 64)]
|
||||
}
|
||||
|
||||
pub fn act_state_root(&self) -> &[u8] {
|
||||
let len = self.ssz.len();
|
||||
&self.ssz[(len - 64)..(len - 32)]
|
||||
}
|
||||
|
||||
pub fn cry_state_root(&self) -> &[u8] {
|
||||
let len = self.ssz.len();
|
||||
&self.ssz[(len - 32)..(len)]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user