Add basic BeaconChain struct

This commit is contained in:
Paul Hauner
2018-12-30 13:03:20 +11:00
parent 31c78b7718
commit 1081529cc7
10 changed files with 82 additions and 10 deletions

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
}
}