Starts initialisation of beacon chain in the client
This commit is contained in:
parent
2e0c8e2e47
commit
bbad4bfa19
146
beacon_node/beacon_chain/src/initialise.rs
Normal file
146
beacon_node/beacon_chain/src/initialise.rs
Normal file
@ -0,0 +1,146 @@
|
||||
// Initialisation functions to generate a new BeaconChain.
|
||||
// Note: A new version of ClientTypes may need to be implemented for the lighthouse
|
||||
// testnet. These are examples. Also. there is code duplication which can/should be cleaned up.
|
||||
|
||||
use crate::BeaconChain;
|
||||
use bls;
|
||||
use db::stores::{BeaconBlockStore, BeaconStateStore};
|
||||
use db::{DiskDB, MemoryDB};
|
||||
use fork_choice::BitwiseLMDGhost;
|
||||
use slot_clock::SystemTimeSlotClock;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use types::{ChainSpec, Deposit, DepositData, DepositInput, Eth1Data, Hash256, Keypair};
|
||||
|
||||
//TODO: Correct this for prod
|
||||
//TODO: Account for historical db
|
||||
pub fn initialise_beacon_chain(
|
||||
chain_spec: &ChainSpec,
|
||||
db_name: Option<&PathBuf>,
|
||||
) -> Arc<BeaconChain<DiskDB, SystemTimeSlotClock, BitwiseLMDGhost<DiskDB>>> {
|
||||
// set up the db
|
||||
let db = Arc::new(DiskDB::open(
|
||||
db_name.expect("Database directory must be included"),
|
||||
None,
|
||||
));
|
||||
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
|
||||
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
|
||||
|
||||
// Slot clock
|
||||
let genesis_time = 1_549_935_547; // 12th Feb 2018 (arbitrary value in the past).
|
||||
let slot_clock = SystemTimeSlotClock::new(genesis_time, chain_spec.seconds_per_slot)
|
||||
.expect("Unable to load SystemTimeSlotClock");
|
||||
// Choose the fork choice
|
||||
let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone());
|
||||
|
||||
/*
|
||||
* Generate some random data to start a chain with.
|
||||
*
|
||||
* This is will need to be replace for production usage.
|
||||
*/
|
||||
let latest_eth1_data = Eth1Data {
|
||||
deposit_root: Hash256::zero(),
|
||||
block_hash: Hash256::zero(),
|
||||
};
|
||||
let keypairs: Vec<Keypair> = (0..10)
|
||||
.collect::<Vec<usize>>()
|
||||
.iter()
|
||||
.map(|_| Keypair::random())
|
||||
.collect();
|
||||
let initial_validator_deposits = keypairs
|
||||
.iter()
|
||||
.map(|keypair| Deposit {
|
||||
branch: vec![], // branch verification is not chain_specified.
|
||||
index: 0, // index verification is not chain_specified.
|
||||
deposit_data: DepositData {
|
||||
amount: 32_000_000_000, // 32 ETH (in Gwei)
|
||||
timestamp: genesis_time - 1,
|
||||
deposit_input: DepositInput {
|
||||
pubkey: keypair.pk.clone(),
|
||||
withdrawal_credentials: Hash256::zero(), // Withdrawal not possible.
|
||||
proof_of_possession: bls::create_proof_of_possession(&keypair),
|
||||
},
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Genesis chain
|
||||
// TODO:Remove the expect here. Propagate errors and handle somewhat gracefully.
|
||||
Arc::new(
|
||||
BeaconChain::genesis(
|
||||
state_store.clone(),
|
||||
block_store.clone(),
|
||||
slot_clock,
|
||||
genesis_time,
|
||||
latest_eth1_data,
|
||||
initial_validator_deposits,
|
||||
chain_spec.clone(),
|
||||
fork_choice,
|
||||
)
|
||||
.expect("Cannot initialise a beacon chain. Exiting"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Initialisation of a test beacon chain, uses an in memory db with fixed genesis time.
|
||||
pub fn initialise_test_beacon_chain(
|
||||
chain_spec: &ChainSpec,
|
||||
_db_name: Option<&PathBuf>,
|
||||
) -> Arc<BeaconChain<MemoryDB, SystemTimeSlotClock, BitwiseLMDGhost<MemoryDB>>> {
|
||||
let db = Arc::new(MemoryDB::open());
|
||||
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
|
||||
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
|
||||
|
||||
// Slot clock
|
||||
let genesis_time = 1_549_935_547; // 12th Feb 2018 (arbitrary value in the past).
|
||||
let slot_clock = SystemTimeSlotClock::new(genesis_time, chain_spec.seconds_per_slot)
|
||||
.expect("Unable to load SystemTimeSlotClock");
|
||||
// Choose the fork choice
|
||||
let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone());
|
||||
|
||||
/*
|
||||
* Generate some random data to start a chain with.
|
||||
*
|
||||
* This is will need to be replace for production usage.
|
||||
*/
|
||||
let latest_eth1_data = Eth1Data {
|
||||
deposit_root: Hash256::zero(),
|
||||
block_hash: Hash256::zero(),
|
||||
};
|
||||
let keypairs: Vec<Keypair> = (0..10)
|
||||
.collect::<Vec<usize>>()
|
||||
.iter()
|
||||
.map(|_| Keypair::random())
|
||||
.collect();
|
||||
let initial_validator_deposits = keypairs
|
||||
.iter()
|
||||
.map(|keypair| Deposit {
|
||||
branch: vec![], // branch verification is not chain_specified.
|
||||
index: 0, // index verification is not chain_specified.
|
||||
deposit_data: DepositData {
|
||||
amount: 32_000_000_000, // 32 ETH (in Gwei)
|
||||
timestamp: genesis_time - 1,
|
||||
deposit_input: DepositInput {
|
||||
pubkey: keypair.pk.clone(),
|
||||
withdrawal_credentials: Hash256::zero(), // Withdrawal not possible.
|
||||
proof_of_possession: bls::create_proof_of_possession(&keypair),
|
||||
},
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Genesis chain
|
||||
// TODO: Handle error correctly
|
||||
Arc::new(
|
||||
BeaconChain::genesis(
|
||||
state_store.clone(),
|
||||
block_store.clone(),
|
||||
slot_clock,
|
||||
genesis_time,
|
||||
latest_eth1_data,
|
||||
initial_validator_deposits,
|
||||
chain_spec.clone(),
|
||||
fork_choice,
|
||||
)
|
||||
.expect("Cannot generate beacon chain"),
|
||||
)
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
// Initialisation functions to generate a new BeaconChain.
|
||||
|
||||
pub fn initialise_test_chain(
|
||||
config: &ClientConfig,
|
||||
) -> Arc<BeaconChain<MemoryDB, SystemTimeSlotClock, BitwiseLMDGhost>> {
|
||||
let spec = config.spec;
|
||||
// Slot clock
|
||||
let genesis_time = 1_549_935_547; // 12th Feb 2018 (arbitrary value in the past).
|
||||
let slot_clock = SystemTimeSlotClock::new(genesis_time, spec.slot_duration)
|
||||
.expect("Unable to load SystemTimeSlotClock");
|
||||
// Choose the fork choice
|
||||
let fork_choice = BitwiseLMDGhost::new(block_store.clone(), state_store.clone());
|
||||
|
||||
/*
|
||||
* Generate some random data to start a chain with.
|
||||
*
|
||||
* This is will need to be replace for production usage.
|
||||
*/
|
||||
let latest_eth1_data = Eth1Data {
|
||||
deposit_root: Hash256::zero(),
|
||||
block_hash: Hash256::zero(),
|
||||
};
|
||||
let keypairs: Vec<Keypair> = (0..10)
|
||||
.collect::<Vec<usize>>()
|
||||
.iter()
|
||||
.map(|_| Keypair::random())
|
||||
.collect();
|
||||
let initial_validator_deposits = keypairs
|
||||
.iter()
|
||||
.map(|keypair| Deposit {
|
||||
branch: vec![], // branch verification is not specified.
|
||||
index: 0, // index verification is not specified.
|
||||
deposit_data: DepositData {
|
||||
amount: 32_000_000_000, // 32 ETH (in Gwei)
|
||||
timestamp: genesis_time - 1,
|
||||
deposit_input: DepositInput {
|
||||
pubkey: keypair.pk.clone(),
|
||||
withdrawal_credentials: Hash256::zero(), // Withdrawal not possible.
|
||||
proof_of_possession: create_proof_of_possession(&keypair),
|
||||
},
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Genesis chain
|
||||
Arc::new(BeaconChain::genesis(
|
||||
state_store.clone(),
|
||||
block_store.clone(),
|
||||
slot_clock,
|
||||
genesis_time,
|
||||
latest_eth1_data,
|
||||
initial_validator_deposits,
|
||||
spec,
|
||||
fork_choice,
|
||||
));
|
||||
}
|
@ -2,6 +2,7 @@ mod attestation_aggregator;
|
||||
mod beacon_chain;
|
||||
mod checkpoint;
|
||||
mod errors;
|
||||
pub mod initialise;
|
||||
|
||||
pub use self::beacon_chain::{BeaconChain, BlockProcessingOutcome, InvalidBlock, ValidBlock};
|
||||
pub use self::checkpoint::CheckPoint;
|
||||
|
@ -1,25 +1,39 @@
|
||||
use db::{ClientDB, DiskDB, MemoryDB};
|
||||
use fork_choice::{BitwiseLMDGhost, ForkChoice};
|
||||
use slot_clock::{SlotClock, SystemTimeSlotClock, TestingSlotClock};
|
||||
use beacon_chain::initialise;
|
||||
use std::sync::Arc;
|
||||
use crate::ClientConfig
|
||||
|
||||
pub trait ClientTypes {
|
||||
type ForkChoice: ForkChoice;
|
||||
type DB: ClientDB;
|
||||
type SlotClock: SlotClock;
|
||||
|
||||
pub fn initialise_beacon_chain(cchain_spec: &ClientConfig) -> Arc<BeaconChain<DB,SlotClock,ForkChoice>>);
|
||||
}
|
||||
|
||||
pub struct StandardClientType {}
|
||||
pub struct StandardClientType
|
||||
|
||||
impl ClientTypes for StandardClientType {
|
||||
type DB = DiskDB;
|
||||
type ForkChoice = BitwiseLMDGhost<DiskDB>;
|
||||
type SlotClock = SystemTimeSlotClock;
|
||||
|
||||
pub fn initialise_beacon_chain(config: &ClientConfig) -> Arc<BeaconChain<DB,SlotClock,ForkChoice>>) {
|
||||
initialise::initialise_beacon_chain(config.chain_spec, config.db_name)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct TestingClientType {}
|
||||
pub struct TestingClientType
|
||||
|
||||
impl ClientTypes for TestingClientType {
|
||||
type DB = MemoryDB;
|
||||
type SlotClock = TestingSlotClock;
|
||||
type ForkChoice = BitwiseLMDGhost<MemoryDB>;
|
||||
|
||||
pub fn initialise_beacon_chain(config: &ClientConfig) -> Arc<BeaconChain<DB,SlotClock,ForkChoice>>) {
|
||||
initialise::initialise_test_beacon_chain(config.chain_spec, None)
|
||||
}
|
||||
}
|
||||
|
@ -21,31 +21,36 @@ use tokio::runtime::TaskExecutor;
|
||||
/// sub-services in multiple threads.
|
||||
pub struct Client<T: ClientTypes> {
|
||||
config: ClientConfig,
|
||||
// beacon_chain: Arc<BeaconChain<T, U, F>>,
|
||||
beacon_chain: Arc<BeaconChain<T::DB, T::SlotClock, T::ForkChoice>>,
|
||||
pub network: Arc<NetworkService>,
|
||||
pub exit: exit_future::Exit,
|
||||
pub exit_signal: Signal,
|
||||
log: slog::Logger,
|
||||
phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: ClientTypes> Client<T> {
|
||||
/// Generate an instance of the client. Spawn and link all internal subprocesses.
|
||||
pub fn new(
|
||||
config: ClientConfig,
|
||||
client_type: T,
|
||||
log: slog::Logger,
|
||||
executor: &TaskExecutor,
|
||||
) -> error::Result<Self> {
|
||||
let (exit_signal, exit) = exit_future::signal();
|
||||
|
||||
// TODO: generate a beacon_chain service.
|
||||
// generate a beacon chain
|
||||
let beacon_chain = client_type.initialise_beacon_chain(&config);
|
||||
|
||||
// Start the network service, libp2p and syncing threads
|
||||
// TODO: Add beacon_chain reference to network parameters
|
||||
let network_config = config.net_conf.clone();
|
||||
let network_config = &config.net_conf;
|
||||
let network_logger = log.new(o!("Service" => "Network"));
|
||||
let (network, network_send) =
|
||||
NetworkService::new(network_config, executor, network_logger)?;
|
||||
let (network, network_send) = NetworkService::new(
|
||||
beacon_chain.clone(),
|
||||
network_config,
|
||||
executor,
|
||||
network_logger,
|
||||
)?;
|
||||
|
||||
Ok(Client {
|
||||
config,
|
||||
|
@ -5,6 +5,7 @@ authors = ["Age Manning <Age@AgeManning.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
beacon_chain = { path = "../beacon_chain" }
|
||||
libp2p = { path = "../libp2p" }
|
||||
version = { path = "../version" }
|
||||
types = { path = "../../eth2/types" }
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::error;
|
||||
use crate::messages::NodeMessage;
|
||||
use beacon_chain::BeaconChain;
|
||||
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
|
||||
use futures::future;
|
||||
use futures::prelude::*;
|
||||
@ -7,6 +8,7 @@ use libp2p::rpc;
|
||||
use libp2p::{PeerId, RPCEvent};
|
||||
use slog::debug;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
use sync::SimpleSync;
|
||||
use types::Hash256;
|
||||
@ -15,12 +17,14 @@ use types::Hash256;
|
||||
const HELLO_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
|
||||
/// Handles messages received from the network and client and organises syncing.
|
||||
pub struct MessageHandler {
|
||||
pub struct MessageHandler<T: ClientTypes> {
|
||||
/// Currently loaded and initialised beacon chain.
|
||||
chain: BeaconChain<T::DB, T::SlotClock, T::ForkChoice>,
|
||||
/// The syncing framework.
|
||||
sync: SimpleSync,
|
||||
//TODO: Implement beacon chain
|
||||
//chain: BeaconChain
|
||||
/// A mapping of peers we have sent a HELLO rpc request to
|
||||
/// A mapping of peers we have sent a HELLO rpc request to.
|
||||
hello_requests: HashMap<PeerId, Instant>,
|
||||
/// The `MessageHandler` logger.
|
||||
log: slog::Logger,
|
||||
}
|
||||
|
||||
@ -37,9 +41,10 @@ pub enum HandlerMessage {
|
||||
RPC(RPCEvent),
|
||||
}
|
||||
|
||||
impl MessageHandler {
|
||||
impl<T: ClientTypes> MessageHandler<T> {
|
||||
/// Initializes and runs the MessageHandler.
|
||||
pub fn new(
|
||||
beacon_chain: Arc<BeaconChain<T::DB, T::SlotClock, T::ForkChoice>>,
|
||||
executor: &tokio::runtime::TaskExecutor,
|
||||
log: slog::Logger,
|
||||
) -> error::Result<Sender<HandlerMessage>> {
|
||||
@ -49,12 +54,13 @@ impl MessageHandler {
|
||||
|
||||
// Initialise sync and begin processing in thread
|
||||
//TODO: Load genesis from BeaconChain
|
||||
//TODO: Initialise beacon chain
|
||||
let temp_genesis = Hash256::zero();
|
||||
|
||||
// generate the Message handler
|
||||
let sync = SimpleSync::new(temp_genesis);
|
||||
//TODO: Initialise beacon chain
|
||||
let mut handler = MessageHandler {
|
||||
chain: beacon_chain,
|
||||
sync,
|
||||
hello_requests: HashMap::new(),
|
||||
log: log.clone(),
|
||||
@ -74,6 +80,13 @@ impl MessageHandler {
|
||||
}
|
||||
|
||||
fn handle_message(&mut self, message: HandlerMessage) {
|
||||
debug!(self.log, "Message received {:?}", message);
|
||||
match message {
|
||||
HandlerMessage::PeerDialed(peer_id) => self.send_hello(peer_id),
|
||||
//TODO: Handle all messages
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends a HELLO RPC request to a newly connected peer.
|
||||
fn send_hello(&self, peer_id: PeerId) {}
|
||||
}
|
||||
|
@ -15,25 +15,28 @@ use libp2p::{Libp2pEvent, PeerId};
|
||||
use slog::{debug, info, o, trace, warn, Logger};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::runtime::TaskExecutor;
|
||||
use client::ClientTypes;
|
||||
|
||||
/// Service that handles communication between internal services and the libp2p network service.
|
||||
pub struct Service {
|
||||
pub struct Service<T: ClientTypes> {
|
||||
//libp2p_service: Arc<Mutex<LibP2PService>>,
|
||||
libp2p_exit: oneshot::Sender<()>,
|
||||
network_send: crossbeam_channel::Sender<NetworkMessage>,
|
||||
//message_handler: MessageHandler,
|
||||
//message_handler_send: Sender<HandlerMessage>,
|
||||
PhantomData: T,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
impl<T: ClientTypes> Service<T> {
|
||||
pub fn new(
|
||||
config: NetworkConfig,
|
||||
beacon_chain: Arc<BeaconChain<T::DB, T::SlotClock, T::ForkChoice>,
|
||||
config: &NetworkConfig,
|
||||
executor: &TaskExecutor,
|
||||
log: slog::Logger,
|
||||
) -> error::Result<(Arc<Self>, Sender<NetworkMessage>)> {
|
||||
// launch message handler thread
|
||||
let message_handler_log = log.new(o!("Service" => "MessageHandler"));
|
||||
let message_handler_send = MessageHandler::new(executor, message_handler_log)?;
|
||||
let message_handler_send = MessageHandler::new(beacon_chain, executor, message_handler_log)?;
|
||||
|
||||
// launch libp2p service
|
||||
let libp2p_log = log.new(o!("Service" => "Libp2p"));
|
||||
|
@ -31,7 +31,7 @@ pub fn run_beacon_node(config: ClientConfig, log: slog::Logger) -> error::Result
|
||||
|
||||
let executor = runtime.executor();
|
||||
|
||||
// currently testing - using TestingNode type
|
||||
// currently testing - using TestingClientType
|
||||
let client: Client<TestingClientType> = Client::new(config, log.clone(), &executor)?;
|
||||
notifier::run(&client, executor, exit);
|
||||
|
||||
|
@ -257,10 +257,10 @@ impl ChainSpec {
|
||||
.parse()
|
||||
.expect("correct multiaddr")];
|
||||
|
||||
let mut standard_spec = ChainSpec::foundation();
|
||||
standard_spec.boot_nodes = boot_nodes;
|
||||
|
||||
standard_spec
|
||||
Self {
|
||||
boot_nodes,
|
||||
..ChainSpec::foundation()
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a `ChainSpec` compatible with the specification suitable for 8 validators.
|
||||
|
Loading…
Reference in New Issue
Block a user