[Temp Commit] Implements more basic skeleton code.

This commit is contained in:
Age Manning 2019-03-04 16:39:37 +11:00
parent 2e020a3efa
commit 3b8f29a914
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
19 changed files with 195 additions and 93 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "beacon_chain"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
authors = ["Paul Hauner <paul@paulhauner.com>", "Age Manning <Age@AgeManning.com>"]
edition = "2018"
[dependencies]

View File

@ -0,0 +1,56 @@
// 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,
));
}

View File

@ -1,7 +1,7 @@
use clap::ArgMatches;
use db::DBType;
use fork_choice::ForkChoiceAlgorithm;
use network::NetworkConfiguration;
use network::NetworkConfig;
use slog::error;
use std::fs;
use std::net::IpAddr;
@ -13,7 +13,7 @@ use types::ChainSpec;
pub struct ClientConfig {
pub data_dir: PathBuf,
pub spec: ChainSpec,
pub net_conf: network::NetworkConfiguration,
pub net_conf: network::NetworkConfig,
pub fork_choice: ForkChoiceAlgorithm,
pub db_type: DBType,
pub db_name: PathBuf,
@ -34,7 +34,7 @@ impl Default for ClientConfig {
data_dir: data_dir.clone(),
// default to foundation for chain specs
spec: ChainSpec::foundation(),
net_conf: NetworkConfiguration::default(),
net_conf: NetworkConfig::default(),
// default to bitwise LMD Ghost
fork_choice: ForkChoiceAlgorithm::BitwiseLMDGhost,
// default to memory db for now
@ -53,12 +53,13 @@ impl ClientConfig {
// Network related args
// Custom listening address ipv4/ipv6
// TODO: Handle list of addresses
if let Some(listen_address_str) = args.value_of("listen_address") {
if let Ok(listen_address) = listen_address_str.parse::<IpAddr>() {
config.net_conf.listen_address = Some(listen_address);
config.net_conf.listen_address = Some(Vec::new(listen_address));
} else {
error!(log, "Invalid Ip Address"; "Address" => listen_address_str);
return Err("Invalid Ip Address");
error!(log, "Invalid IP Address"; "Address" => listen_address_str);
return Err("Invalid IP Address");
}
}
// Custom p2p listen port

View File

@ -13,14 +13,15 @@ pub use client_types::ClientTypes;
use exit_future::{Exit, Signal};
use std::marker::PhantomData;
//use std::sync::Arc;
use network::NetworkService;
use tokio::runtime::TaskExecutor;
//use network::NetworkService;
/// Main beacon node client service. This provides the connection and initialisation of the clients
/// sub-services in multiple threads.
pub struct Client<T: ClientTypes> {
config: ClientConfig,
// beacon_chain: Arc<BeaconChain<T, U, F>>,
// network: Option<Arc<NetworkService>>,
network: Option<Arc<NetworkService>>,
exit: exit_future::Exit,
exit_signal: Option<Signal>,
log: slog::Logger,
@ -28,6 +29,7 @@ pub struct Client<T: ClientTypes> {
}
impl<T: ClientTypes> Client<T> {
/// Generate an instance of the client. Spawn and link all internal subprocesses.
pub fn new(
config: ClientConfig,
log: slog::Logger,
@ -35,16 +37,21 @@ impl<T: ClientTypes> Client<T> {
) -> error::Result<Self> {
let (exit_signal, exit) = exit_future::signal();
// TODO: generate a beacon_chain service.
// start the network service, libp2p and syncing threads
// TODO: Add beacon_chain reference to network parameters
let network_config = config.net_config;
let network_logger = client.log.new(o!("Service" => "Network"));
let (network, network_send) = NetworkService::new(network_config, network_logger);
Ok(Client {
config,
exit,
exit_signal: Some(exit_signal),
log,
network: Some(network),
phantom: PhantomData,
})
}
pub fn logger(&self) -> slog::Logger {
self.log.clone()
}
}

2
beacon_node/client/src/notifier.rs Executable file → Normal file
View File

@ -17,7 +17,7 @@ pub fn run<T: ClientTypes>(client: &Client<T>, executor: TaskExecutor, exit: Exi
// notification heartbeat
let interval = Interval::new(Instant::now(), Duration::from_secs(5));
let log = client.logger();
let log = client.log.new(o!("Service" => "Notifier"));
// build heartbeat logic here
let heartbeat = move |_| {

View File

@ -0,0 +1,7 @@
[package]
name = "libp2p"
version = "0.1.0"
authors = ["Age Manning <Age@AgeManning.com>"]
edition = "2018"
[dependencies]

View File

@ -0,0 +1,11 @@
/// This crate contains the main link for lighthouse to rust-libp2p. It therefore re-exports
/// all required libp2p functionality.
///
/// This crate builds and manages the libp2p services required by the beacon node.
extern crate libp2p;
mod libp2p_service;
pub use libp2p::{GossipsubConfig, PeerId};
pub use libp2p_service::LibP2PService;

View File

View File

@ -5,6 +5,6 @@ authors = ["Age Manning <Age@AgeManning.com>"]
edition = "2018"
[dependencies]
# SigP repository until PR is merged
libp2p = { git = "https://github.com/SigP/rust-libp2p", branch = "gossipsub" }
libp2p = { git = "../libp2p" }
version = { path = "../version" }
types = { path = "../../eth2/types" }

View File

@ -0,0 +1,8 @@
// generates error types
use error_chain::{
error_chain, error_chain_processing, impl_error_chain_kind, impl_error_chain_processed,
impl_extract_backtrace,
};
error_chain! {}

View File

@ -1,4 +1,6 @@
/// This crate provides the network server for Lighthouse.
mod network_configuration;
mod network_config;
mod service;
pub use network_configuration::NetworkConfiguration;
pub use network_config::NetworkConfig;
pub use service::NetworkService;

View File

@ -0,0 +1,18 @@
use crate::node_message::NodeMessage;
/// Handles messages received from the network and client and organises syncing.
pub struct MessageHandler {
sync: Syncer,
//TODO: Implement beacon chain
//chain: BeaconChain
}
/// Types of messages the handler can receive.
pub enum HandlerMessage {
/// Peer has connected.
PeerConnected(PeerId),
/// Peer has disconnected,
PeerDisconnected(PeerId),
/// A Node message has been received.
Message(Peer, NodeMessage),
}

View File

@ -0,0 +1,27 @@
use types::{H256,Slot}
/// Messages between nodes across the network.
pub enum NodeMessage {
Status(Status),
BlockRequest,
}
pub struct Status {
/// Current node version.
version: u8
/// Genesis Hash.
genesis_hash: H256
/// Best known slot number.
best_slot: Slot
/// Best known slot hash.
best_slot_hash: H256
}
/// Types of messages that the network service can receive.
pub enum NetworkMessage {
/// Send a message to libp2p service.
//TODO: Define typing for messages accross the wire
Send(Node, Message),
}

View File

@ -4,10 +4,10 @@ use version;
#[derive(Debug, Clone)]
/// Network configuration for lighthouse.
pub struct NetworkConfiguration {
pub struct NetworkConfig {
//TODO: stubbing networking initial params, change in the future
/// IP address to listen on.
pub listen_address: Option<IpAddr>,
pub listen_addresses: Option<Vec<IpAddr>>,
/// Listen port UDP/TCP.
pub listen_port: Option<u16>,
/// Gossipsub configuration parameters.
@ -16,14 +16,13 @@ pub struct NetworkConfiguration {
pub boot_nodes: Vec<String>,
/// Client version
pub client_version: String,
//TODO: more to be added
}
impl Default for NetworkConfiguration {
impl Default for NetworkConfig {
/// Generate a default network configuration.
fn default() -> Self {
NetworkConfiguration {
listen_address: None,
NetworkConfig {
listen_addresses: None,
listen_port: None,
gs_config: GossipsubConfigBuilder::new().build(),
boot_nodes: Vec::new(),
@ -32,8 +31,8 @@ impl Default for NetworkConfiguration {
}
}
impl NetworkConfiguration {
impl NetworkConfig {
pub fn new() -> Self {
NetworkConfiguration::default()
NetworkConfig::default()
}
}

View File

View File

@ -37,6 +37,7 @@ pub fn run_beacon_node(config: ClientConfig, log: slog::Logger) -> error::Result
runtime.block_on(ctrlc);
// perform global shutdown operations.
info!(log, "Shutting down..");
exit_signal.fire();
drop(client);

View File

@ -5,4 +5,5 @@ authors = ["Age Manning <Age@AgeManning.com>"]
edition = "2018"
[dependencies]
types = { path = "../../eth2/types" }
libp2p = { git = "../libp2p/" }

View File

@ -1,68 +1,10 @@
// /// Syncing for lighthouse.
/// Syncing for lighthouse.
///
/// Stores the various syncing methods for the beacon chain.
mod simple_sync;
/*
// for initial testing and setup, to be replaced.
pub fn sync_server(config: Config) {
// Set up database
let db = match config.db_type {
_ => Arc::new(MemoryDB::open()),
//TODO: Box db
//DBType::Memory => Arc::new(Box::new(MemoryDB::open())),
//DBType::RocksDB => Arc::new(Box::new(DiskDB::open(&config.db_name, None))),
};
pub use crate::SimpleSync;
// build block
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, 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
let _chain_result = BeaconChain::genesis(
state_store.clone(),
block_store.clone(),
slot_clock,
genesis_time,
latest_eth1_data,
initial_validator_deposits,
spec,
fork_choice,
);
pub enum SyncMethod {
SimpleSync,
}
*/

View File

@ -0,0 +1,22 @@
use std::collections::HashMap;
use types::{Slot, H256};
/// Keeps track of syncing information for known connected peers.
pub struct PeerSyncInfo {
best_slot: Slot,
best_slot_hash: H256,
}
/// The current syncing state.
pub enum SyncState {
Idle,
Downloading,
Stopped,
}
/// Simple Syncing protocol.
pub struct SimpleSync {
genesis_hash: H256,
known_peers: HashMap<PeerId, PeerSyncInfo>,
state: SyncState,
}