Add basic BeaconChain struct

This commit is contained in:
Paul Hauner 2018-12-30 13:03:20 +11:00
parent 31c78b7718
commit 1081529cc7
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
10 changed files with 82 additions and 10 deletions

View File

@ -34,7 +34,6 @@ name = "lighthouse"
[workspace]
members = [
"beacon_chain/attestation_validation",
"beacon_chain/chain",
"beacon_chain/genesis",
"beacon_chain/naive_fork_choice",
"beacon_chain/spec",
@ -43,11 +42,12 @@ members = [
"beacon_chain/utils/boolean-bitfield",
"beacon_chain/utils/hashing",
"beacon_chain/utils/honey-badger-split",
"beacon_chain/utils/slot-clock",
"beacon_chain/utils/slot_clock",
"beacon_chain/utils/ssz",
"beacon_chain/utils/vec_shuffle",
"beacon_chain/validator_change",
"beacon_chain/validator_induction",
"beacon_chain/validator_shuffling",
"lighthouse/beacon_chain",
"lighthouse/db",
]

View File

@ -1,14 +1,14 @@
use bls::{Signature, BLS_AGG_SIG_BYTE_SIZE};
use spec::ChainSpec;
use ssz::{encode::encode_length, Decodable, LENGTH_BYTES};
use types::{BeaconBlock, BeaconBlockBody};
use types::{BeaconBlock, BeaconBlockBody, Hash256};
/// Generate a genesis BeaconBlock.
pub fn genesis_beacon_block(spec: &ChainSpec) -> BeaconBlock {
pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBlock {
BeaconBlock {
slot: spec.initial_slot_number,
parent_root: spec.zero_hash,
state_root: spec.zero_hash,
state_root: state_root,
randao_reveal: spec.zero_hash,
candidate_pow_receipt_root: spec.zero_hash,
signature: genesis_signature(),
@ -42,8 +42,9 @@ mod tests {
#[test]
fn test_genesis() {
let spec = ChainSpec::foundation();
let state_root = Hash256::from("cats".as_bytes());
// This only checks that the function runs without panic.
genesis_beacon_block(&spec);
genesis_beacon_block(state_root, &spec);
}
}

View File

@ -28,7 +28,7 @@ where
*/
for (index, block_hash) in head_block_hashes.iter().enumerate() {
let ssz = block_store
.get_serialized_block(&block_hash.to_vec()[..])?
.get(&block_hash.to_vec()[..])?
.ok_or(ForkChoiceError::MissingBlock)?;
let (block, _) = BeaconBlock::ssz_decode(&ssz, 0)?;
head_blocks.push((index, block));

View File

@ -8,5 +8,6 @@ edition = "2018"
bls = { path = "../utils/bls" }
boolean-bitfield = { path = "../utils/boolean-bitfield" }
ethereum-types = "0.4.0"
hashing = { path = "../utils/hashing" }
rand = "0.5.5"
ssz = { path = "../utils/ssz" }

View File

@ -1,7 +1,8 @@
use super::ssz::{Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use super::{BeaconBlockBody, Hash256};
use crate::test_utils::TestRandom;
use bls::Signature;
use hashing::canonical_hash;
use rand::RngCore;
#[derive(Debug, PartialEq, Clone)]
@ -15,6 +16,14 @@ pub struct BeaconBlock {
pub body: BeaconBlockBody,
}
impl BeaconBlock {
pub fn canonical_root(&self) -> Hash256 {
// TODO: implement tree hashing.
// https://github.com/sigp/lighthouse/issues/70
Hash256::from(&canonical_hash(&ssz_encode(self))[..])
}
}
impl Encodable for BeaconBlock {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slot);

View File

@ -7,8 +7,9 @@ use super::shard_reassignment_record::ShardReassignmentRecord;
use super::validator_record::ValidatorRecord;
use super::Hash256;
use crate::test_utils::TestRandom;
use hashing::canonical_hash;
use rand::RngCore;
use ssz::{Decodable, DecodeError, Encodable, SszStream};
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
#[derive(Debug, PartialEq, Clone)]
pub struct BeaconState {
@ -51,7 +52,7 @@ impl BeaconState {
pub fn canonical_root(&self) -> Hash256 {
// TODO: implement tree hashing.
// https://github.com/sigp/lighthouse/issues/70
Hash256::zero()
Hash256::from(&canonical_hash(&ssz_encode(self))[..])
}
}

View File

@ -31,6 +31,9 @@ pub mod slashable_vote_data;
pub mod validator_record;
pub mod validator_registration;
pub mod readers;
use self::ethereum_types::{H160, H256, U256};
use std::collections::HashMap;

View File

@ -0,0 +1,31 @@
use crate::{BeaconBlock, Hash256};
pub trait BeaconBlockReader {
fn slot(&self) -> u64;
fn parent_root(&self) -> Hash256;
fn state_root(&self) -> Hash256;
fn canonical_root(&self) -> Hash256;
fn to_beacon_block(self) -> BeaconBlock;
}
impl BeaconBlockReader for BeaconBlock {
fn slot(&self) -> u64 {
self.slot
}
fn parent_root(&self) -> Hash256 {
self.parent_root
}
fn state_root(&self) -> Hash256 {
self.state_root
}
fn canonical_root(&self) -> Hash256 {
self.canonical_root()
}
fn to_beacon_block(self) -> BeaconBlock {
self
}
}

View File

@ -0,0 +1,5 @@
mod block_reader;
mod state_reader;
pub use self::block_reader::BeaconBlockReader;
pub use self::state_reader::BeaconStateReader;

View File

@ -0,0 +1,21 @@
use crate::{BeaconState, Hash256};
pub trait BeaconStateReader {
fn slot(&self) -> u64;
fn canonical_root(&self) -> Hash256;
fn to_beacon_state(self) -> BeaconState;
}
impl BeaconStateReader for BeaconState {
fn slot(&self) -> u64 {
self.slot
}
fn canonical_root(&self) -> Hash256 {
self.canonical_root()
}
fn to_beacon_state(self) -> BeaconState {
self
}
}