mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 16:55:46 +00:00
Add basic BeaconChain struct
This commit is contained in:
@@ -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" }
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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))[..])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
31
beacon_chain/types/src/readers/block_reader.rs
Normal file
31
beacon_chain/types/src/readers/block_reader.rs
Normal 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
|
||||
}
|
||||
}
|
||||
5
beacon_chain/types/src/readers/mod.rs
Normal file
5
beacon_chain/types/src/readers/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod block_reader;
|
||||
mod state_reader;
|
||||
|
||||
pub use self::block_reader::BeaconBlockReader;
|
||||
pub use self::state_reader::BeaconStateReader;
|
||||
21
beacon_chain/types/src/readers/state_reader.rs
Normal file
21
beacon_chain/types/src/readers/state_reader.rs
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user