lighthouse-pulse/lighthouse/state/block.rs

77 lines
2.1 KiB
Rust
Raw Normal View History

2018-08-10 00:48:03 +00:00
use super::utils::types::Hash256;
use super::attestation_record::AttestationRecord;
2018-09-10 05:51:57 +00:00
use super::ssz::{ Encodable, SszStream };
2018-08-10 00:48:03 +00:00
const SSZ_BLOCK_LENGTH: usize = 192;
2018-07-06 07:54:07 +00:00
pub struct Block {
2018-08-10 00:48:03 +00:00
pub parent_hash: Hash256,
pub slot_number: u64,
pub randao_reveal: Hash256,
pub attestations: Vec<AttestationRecord>,
pub pow_chain_ref: Hash256,
pub active_state_root: Hash256,
pub crystallized_state_root: Hash256,
2018-09-09 14:36:00 +00:00
}
2018-08-10 00:48:03 +00:00
2018-07-06 07:54:07 +00:00
impl Block {
2018-08-10 00:48:03 +00:00
pub fn zero() -> Self {
Self {
parent_hash: Hash256::zero(),
slot_number: 0,
randao_reveal: Hash256::zero(),
attestations: vec![],
pow_chain_ref: Hash256::zero(),
active_state_root: Hash256::zero(),
crystallized_state_root: Hash256::zero(),
2018-07-06 07:54:07 +00:00
}
}
2018-09-10 05:51:57 +00:00
/// Return the bytes that should be signed in order to
/// attest for this block.
pub fn encode_for_signing(&self)
2018-08-10 00:48:03 +00:00
-> [u8; SSZ_BLOCK_LENGTH]
{
2018-09-10 05:51:57 +00:00
let mut s = SszStream::new();
2018-07-06 07:54:07 +00:00
s.append(&self.parent_hash);
2018-08-10 00:48:03 +00:00
s.append(&self.slot_number);
2018-07-06 07:54:07 +00:00
s.append(&self.randao_reveal);
2018-08-10 00:48:03 +00:00
s.append(&self.pow_chain_ref);
s.append(&self.active_state_root);
s.append(&self.crystallized_state_root);
let vec = s.drain();
let mut encoded = [0; SSZ_BLOCK_LENGTH];
encoded.copy_from_slice(&vec); encoded
2018-07-06 07:54:07 +00:00
}
}
2018-09-10 05:51:57 +00:00
impl Encodable for Block {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.parent_hash);
s.append(&self.slot_number);
s.append(&self.randao_reveal);
s.append_vec(&self.attestations);
2018-09-10 05:51:57 +00:00
s.append(&self.pow_chain_ref);
s.append(&self.active_state_root);
s.append(&self.crystallized_state_root);
}
}
2018-07-06 07:54:07 +00:00
#[cfg(test)]
mod tests {
use super::*;
2018-07-09 06:51:50 +00:00
#[test]
2018-08-10 01:22:15 +00:00
fn test_block_zero() {
let b = Block::zero();
assert!(b.parent_hash.is_zero());
assert_eq!(b.slot_number, 0);
assert!(b.randao_reveal.is_zero());
assert_eq!(b.attestations.len(), 0);
assert!(b.pow_chain_ref.is_zero());
assert!(b.active_state_root.is_zero());
assert!(b.crystallized_state_root.is_zero());
2018-07-09 06:51:50 +00:00
}
2018-07-06 07:54:07 +00:00
}