mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-17 04:48:21 +00:00
Create the fork-choice crate.
- Adds the naive fork choice (longest chain) rule. - Adds basic documentation for the crate.
This commit is contained in:
0
eth2/fork_choice/src/basic_lmd_ghost.rs
Normal file
0
eth2/fork_choice/src/basic_lmd_ghost.rs
Normal file
24
eth2/fork_choice/src/lib.rs
Normal file
24
eth2/fork_choice/src/lib.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
//! This crate stores the various implementations of fork-choice rules that can be used for the
|
||||
//! beacon blockchain.
|
||||
//!
|
||||
//! There are four implementations. One is the naive longest chain rule (primarily for testing
|
||||
//! purposes). The other three are proposed implementations of the LMD-GHOST fork-choice rule with various forms of optimisation.
|
||||
//!
|
||||
//! The current implementations are:
|
||||
//! - [`longest-chain`]: Simplistic longest-chain fork choice - primarily for testing, **not for
|
||||
//! production**.
|
||||
//! - [`basic_lmd_ghost`]: This is a simple and very inefficient implementation given in the ethereum 2.0
|
||||
//! specifications (https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_block_root).
|
||||
//! - [`optimised_lmd_ghost`]: This is an optimised version of the naive implementation as proposed
|
||||
//! by Vitalik. The reference implementation can be found at: https://github.com/ethereum/research/blob/master/ghost/ghost.py
|
||||
//! - [`protolambda_lmd_ghost`]: Another optimised version of LMD-GHOST designed by @protolambda.
|
||||
//! The go implementation can be found here: https://github.com/protolambda/lmd-ghost.
|
||||
//!
|
||||
//! [`basic_lmd_ghost`]: struct.BasicLmdGhost.html
|
||||
//! [`optimised_lmd_ghost`]: struct.OptimisedLmdGhost.html
|
||||
//! [`protolambda_lmd_ghost`]: struct.ProtolambdaLmdGhost.html
|
||||
|
||||
pub mod basic_lmd_ghost;
|
||||
pub mod longest_chain;
|
||||
pub mod optimised_lmd_ghost;
|
||||
pub mod protolambda_lmd_ghost;
|
||||
97
eth2/fork_choice/src/longest_chain.rs
Normal file
97
eth2/fork_choice/src/longest_chain.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
extern crate db;
|
||||
extern crate ssz;
|
||||
extern crate types;
|
||||
|
||||
use db::stores::BeaconBlockStore;
|
||||
use db::{ClientDB, DBError};
|
||||
use ssz::{Decodable, DecodeError};
|
||||
use std::sync::Arc;
|
||||
use types::{BeaconBlock, Hash256};
|
||||
|
||||
pub enum ForkChoiceError {
|
||||
BadSszInDatabase,
|
||||
MissingBlock,
|
||||
DBError(String),
|
||||
}
|
||||
|
||||
pub fn longest_chain<T>(
|
||||
head_block_hashes: &[Hash256],
|
||||
block_store: &Arc<BeaconBlockStore<T>>,
|
||||
) -> Result<Option<usize>, ForkChoiceError>
|
||||
where
|
||||
T: ClientDB + Sized,
|
||||
{
|
||||
let mut head_blocks: Vec<(usize, BeaconBlock)> = vec![];
|
||||
|
||||
/*
|
||||
* Load all the head_block hashes from the DB as SszBeaconBlocks.
|
||||
*/
|
||||
for (index, block_hash) in head_block_hashes.iter().enumerate() {
|
||||
let ssz = block_store
|
||||
.get(&block_hash)?
|
||||
.ok_or(ForkChoiceError::MissingBlock)?;
|
||||
let (block, _) = BeaconBlock::ssz_decode(&ssz, 0)?;
|
||||
head_blocks.push((index, block));
|
||||
}
|
||||
|
||||
/*
|
||||
* Loop through all the head blocks and find the highest slot.
|
||||
*/
|
||||
let highest_slot: Option<u64> = None;
|
||||
for (_, block) in &head_blocks {
|
||||
let slot = block.slot;
|
||||
|
||||
match highest_slot {
|
||||
None => Some(slot),
|
||||
Some(winning_slot) => {
|
||||
if slot > winning_slot {
|
||||
Some(slot)
|
||||
} else {
|
||||
Some(winning_slot)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* Loop through all the highest blocks and sort them by highest hash.
|
||||
*
|
||||
* Ultimately, the index of the head_block hash with the highest slot and highest block
|
||||
* hash will be the winner.
|
||||
*/
|
||||
match highest_slot {
|
||||
None => Ok(None),
|
||||
Some(highest_slot) => {
|
||||
let mut highest_blocks = vec![];
|
||||
for (index, block) in head_blocks {
|
||||
if block.slot == highest_slot {
|
||||
highest_blocks.push((index, block))
|
||||
}
|
||||
}
|
||||
|
||||
highest_blocks.sort_by(|a, b| head_block_hashes[a.0].cmp(&head_block_hashes[b.0]));
|
||||
let (index, _) = highest_blocks[0];
|
||||
Ok(Some(index))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DecodeError> for ForkChoiceError {
|
||||
fn from(_: DecodeError) -> Self {
|
||||
ForkChoiceError::BadSszInDatabase
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DBError> for ForkChoiceError {
|
||||
fn from(e: DBError) -> Self {
|
||||
ForkChoiceError::DBError(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_naive_fork_choice() {
|
||||
assert_eq!(2 + 2, 4);
|
||||
}
|
||||
}
|
||||
0
eth2/fork_choice/src/optimised_lmd_ghost.rs
Normal file
0
eth2/fork_choice/src/optimised_lmd_ghost.rs
Normal file
0
eth2/fork_choice/src/protolambda_lmd_ghost.rs
Normal file
0
eth2/fork_choice/src/protolambda_lmd_ghost.rs
Normal file
Reference in New Issue
Block a user