Add basic BeaconChain
struct
This commit is contained in:
parent
31c78b7718
commit
1081529cc7
@ -34,7 +34,6 @@ name = "lighthouse"
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"beacon_chain/attestation_validation",
|
"beacon_chain/attestation_validation",
|
||||||
"beacon_chain/chain",
|
|
||||||
"beacon_chain/genesis",
|
"beacon_chain/genesis",
|
||||||
"beacon_chain/naive_fork_choice",
|
"beacon_chain/naive_fork_choice",
|
||||||
"beacon_chain/spec",
|
"beacon_chain/spec",
|
||||||
@ -43,11 +42,12 @@ members = [
|
|||||||
"beacon_chain/utils/boolean-bitfield",
|
"beacon_chain/utils/boolean-bitfield",
|
||||||
"beacon_chain/utils/hashing",
|
"beacon_chain/utils/hashing",
|
||||||
"beacon_chain/utils/honey-badger-split",
|
"beacon_chain/utils/honey-badger-split",
|
||||||
"beacon_chain/utils/slot-clock",
|
"beacon_chain/utils/slot_clock",
|
||||||
"beacon_chain/utils/ssz",
|
"beacon_chain/utils/ssz",
|
||||||
"beacon_chain/utils/vec_shuffle",
|
"beacon_chain/utils/vec_shuffle",
|
||||||
"beacon_chain/validator_change",
|
"beacon_chain/validator_change",
|
||||||
"beacon_chain/validator_induction",
|
"beacon_chain/validator_induction",
|
||||||
"beacon_chain/validator_shuffling",
|
"beacon_chain/validator_shuffling",
|
||||||
|
"lighthouse/beacon_chain",
|
||||||
"lighthouse/db",
|
"lighthouse/db",
|
||||||
]
|
]
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
use bls::{Signature, BLS_AGG_SIG_BYTE_SIZE};
|
use bls::{Signature, BLS_AGG_SIG_BYTE_SIZE};
|
||||||
use spec::ChainSpec;
|
use spec::ChainSpec;
|
||||||
use ssz::{encode::encode_length, Decodable, LENGTH_BYTES};
|
use ssz::{encode::encode_length, Decodable, LENGTH_BYTES};
|
||||||
use types::{BeaconBlock, BeaconBlockBody};
|
use types::{BeaconBlock, BeaconBlockBody, Hash256};
|
||||||
|
|
||||||
/// Generate a genesis BeaconBlock.
|
/// Generate a genesis BeaconBlock.
|
||||||
pub fn genesis_beacon_block(spec: &ChainSpec) -> BeaconBlock {
|
pub fn genesis_beacon_block(state_root: Hash256, spec: &ChainSpec) -> BeaconBlock {
|
||||||
BeaconBlock {
|
BeaconBlock {
|
||||||
slot: spec.initial_slot_number,
|
slot: spec.initial_slot_number,
|
||||||
parent_root: spec.zero_hash,
|
parent_root: spec.zero_hash,
|
||||||
state_root: spec.zero_hash,
|
state_root: state_root,
|
||||||
randao_reveal: spec.zero_hash,
|
randao_reveal: spec.zero_hash,
|
||||||
candidate_pow_receipt_root: spec.zero_hash,
|
candidate_pow_receipt_root: spec.zero_hash,
|
||||||
signature: genesis_signature(),
|
signature: genesis_signature(),
|
||||||
@ -42,8 +42,9 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_genesis() {
|
fn test_genesis() {
|
||||||
let spec = ChainSpec::foundation();
|
let spec = ChainSpec::foundation();
|
||||||
|
let state_root = Hash256::from("cats".as_bytes());
|
||||||
|
|
||||||
// This only checks that the function runs without panic.
|
// This only checks that the function runs without panic.
|
||||||
genesis_beacon_block(&spec);
|
genesis_beacon_block(state_root, &spec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ where
|
|||||||
*/
|
*/
|
||||||
for (index, block_hash) in head_block_hashes.iter().enumerate() {
|
for (index, block_hash) in head_block_hashes.iter().enumerate() {
|
||||||
let ssz = block_store
|
let ssz = block_store
|
||||||
.get_serialized_block(&block_hash.to_vec()[..])?
|
.get(&block_hash.to_vec()[..])?
|
||||||
.ok_or(ForkChoiceError::MissingBlock)?;
|
.ok_or(ForkChoiceError::MissingBlock)?;
|
||||||
let (block, _) = BeaconBlock::ssz_decode(&ssz, 0)?;
|
let (block, _) = BeaconBlock::ssz_decode(&ssz, 0)?;
|
||||||
head_blocks.push((index, block));
|
head_blocks.push((index, block));
|
||||||
|
@ -8,5 +8,6 @@ edition = "2018"
|
|||||||
bls = { path = "../utils/bls" }
|
bls = { path = "../utils/bls" }
|
||||||
boolean-bitfield = { path = "../utils/boolean-bitfield" }
|
boolean-bitfield = { path = "../utils/boolean-bitfield" }
|
||||||
ethereum-types = "0.4.0"
|
ethereum-types = "0.4.0"
|
||||||
|
hashing = { path = "../utils/hashing" }
|
||||||
rand = "0.5.5"
|
rand = "0.5.5"
|
||||||
ssz = { path = "../utils/ssz" }
|
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 super::{BeaconBlockBody, Hash256};
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
use bls::Signature;
|
use bls::Signature;
|
||||||
|
use hashing::canonical_hash;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
@ -15,6 +16,14 @@ pub struct BeaconBlock {
|
|||||||
pub body: BeaconBlockBody,
|
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 {
|
impl Encodable for BeaconBlock {
|
||||||
fn ssz_append(&self, s: &mut SszStream) {
|
fn ssz_append(&self, s: &mut SszStream) {
|
||||||
s.append(&self.slot);
|
s.append(&self.slot);
|
||||||
|
@ -7,8 +7,9 @@ use super::shard_reassignment_record::ShardReassignmentRecord;
|
|||||||
use super::validator_record::ValidatorRecord;
|
use super::validator_record::ValidatorRecord;
|
||||||
use super::Hash256;
|
use super::Hash256;
|
||||||
use crate::test_utils::TestRandom;
|
use crate::test_utils::TestRandom;
|
||||||
|
use hashing::canonical_hash;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct BeaconState {
|
pub struct BeaconState {
|
||||||
@ -51,7 +52,7 @@ impl BeaconState {
|
|||||||
pub fn canonical_root(&self) -> Hash256 {
|
pub fn canonical_root(&self) -> Hash256 {
|
||||||
// TODO: implement tree hashing.
|
// TODO: implement tree hashing.
|
||||||
// https://github.com/sigp/lighthouse/issues/70
|
// 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_record;
|
||||||
pub mod validator_registration;
|
pub mod validator_registration;
|
||||||
|
|
||||||
|
pub mod readers;
|
||||||
|
|
||||||
|
|
||||||
use self::ethereum_types::{H160, H256, U256};
|
use self::ethereum_types::{H160, H256, U256};
|
||||||
use std::collections::HashMap;
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user