Removes network parameters from chain spec

This commit is contained in:
Age Manning 2019-04-03 16:00:09 +11:00
parent f54bd79f84
commit dd3a4f0b43
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
9 changed files with 71 additions and 30 deletions

View File

@ -25,13 +25,10 @@ impl Default for ClientConfig {
fs::create_dir_all(&data_dir) fs::create_dir_all(&data_dir)
.unwrap_or_else(|_| panic!("Unable to create {:?}", &data_dir)); .unwrap_or_else(|_| panic!("Unable to create {:?}", &data_dir));
// currently lighthouse spec
let default_spec = ChainSpec::lighthouse_testnet(); let default_spec = ChainSpec::lighthouse_testnet();
let default_pubsub_topics = vec![ // builds a chain-specific network config
default_spec.beacon_chain_topic.clone(), let net_conf = NetworkConfig::from(default_spec.chain_id);
default_spec.shard_topic_prefix.clone(),
]; // simple singular attestation topic for now.
let default_net_conf =
NetworkConfig::new(default_spec.boot_nodes.clone(), default_pubsub_topics);
Self { Self {
data_dir: PathBuf::from(".lighthouse"), data_dir: PathBuf::from(".lighthouse"),

View File

@ -1,5 +1,6 @@
use crate::rpc::{RPCEvent, RPCMessage, Rpc}; use crate::rpc::{RPCEvent, RPCMessage, Rpc};
use crate::NetworkConfig; use crate::NetworkConfig;
use crate::{Topic, TopicHash};
use futures::prelude::*; use futures::prelude::*;
use libp2p::{ use libp2p::{
core::{ core::{
@ -15,7 +16,6 @@ use libp2p::{
use slog::{debug, o, trace, warn}; use slog::{debug, o, trace, warn};
use ssz::{ssz_encode, Decode, DecodeError, Encode}; use ssz::{ssz_encode, Decode, DecodeError, Encode};
use types::{Attestation, BeaconBlock}; use types::{Attestation, BeaconBlock};
use types::{Topic, TopicHash};
/// Builds the network behaviour for the libp2p Swarm. /// Builds the network behaviour for the libp2p Swarm.
/// Implements gossipsub message routing. /// Implements gossipsub message routing.

View File

@ -20,8 +20,12 @@ pub struct Config {
boot_nodes: Vec<String>, boot_nodes: Vec<String>,
/// Client version /// Client version
pub client_version: String, pub client_version: String,
/// List of topics to subscribe to as strings /// List of extra topics to initially subscribe to as strings.
pub topics: Vec<String>, pub topics: Vec<String>,
/// Shard pubsub topic prefix.
pub shard_prefix: String,
/// The main beacon chain topic to subscribe to.
pub beacon_chain_topic: String,
} }
impl Default for Config { impl Default for Config {
@ -37,17 +41,16 @@ impl Default for Config {
boot_nodes: vec![], boot_nodes: vec![],
client_version: version::version(), client_version: version::version(),
topics: Vec::new(), topics: Vec::new(),
beacon_chain_topic: String::from("beacon_chain"),
shard_prefix: String::from("attestations"), // single topic for all attestation for the moment.
} }
} }
} }
/// Generates a default Config.
impl Config { impl Config {
pub fn new(boot_nodes: Vec<Multiaddr>, topics: Vec<String>) -> Self { pub fn new() -> Self {
let mut conf = Config::default(); Config::default()
conf.boot_nodes = boot_nodes;
conf.topics = topics;
conf
} }
pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>, MultiaddrError> { pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>, MultiaddrError> {
@ -90,3 +93,43 @@ impl Default for IdentifyConfig {
} }
} }
} }
/// Creates a standard network config from a chain_id.
///
/// This creates specified network parameters for each chain type.
impl From<ChainType> for Config {
fn from(chain_type: ChainType) -> Self {
match chain_type {
ChainType::Foundation => Config::default(),
ChainType::LighthouseTestnet => {
let boot_nodes = vec!["/ip4/127.0.0.1/tcp/9000"
.parse()
.expect("correct multiaddr")];
Self {
boot_nodes,
..Config::default()
}
}
ChainType::Other => Config::default(),
}
}
}
pub enum ChainType {
Foundation,
LighthouseTestnet,
Other,
}
/// Maps a chain id to a ChainType.
impl From<u8> for ChainType {
fn from(chain_id: u8) -> Self {
match chain_id {
1 => ChainType::Foundation,
2 => ChainType::LighthouseTestnet,
_ => ChainType::Other,
}
}
}

View File

@ -10,6 +10,9 @@ mod service;
pub use behaviour::PubsubMessage; pub use behaviour::PubsubMessage;
pub use config::Config as NetworkConfig; pub use config::Config as NetworkConfig;
pub use libp2p::floodsub::{Topic, TopicBuilder, TopicHash};
pub use libp2p::multiaddr;
pub use libp2p::Multiaddr;
pub use libp2p::{ pub use libp2p::{
gossipsub::{GossipsubConfig, GossipsubConfigBuilder}, gossipsub::{GossipsubConfig, GossipsubConfigBuilder},
PeerId, PeerId,
@ -17,5 +20,3 @@ pub use libp2p::{
pub use rpc::RPCEvent; pub use rpc::RPCEvent;
pub use service::Libp2pEvent; pub use service::Libp2pEvent;
pub use service::Service; pub use service::Service;
pub use types::multiaddr;
pub use types::Multiaddr;

View File

@ -3,6 +3,7 @@ use crate::error;
use crate::multiaddr::Protocol; use crate::multiaddr::Protocol;
use crate::rpc::RPCEvent; use crate::rpc::RPCEvent;
use crate::NetworkConfig; use crate::NetworkConfig;
use crate::{TopicBuilder, TopicHash};
use futures::prelude::*; use futures::prelude::*;
use futures::Stream; use futures::Stream;
use libp2p::core::{ use libp2p::core::{
@ -17,7 +18,6 @@ use libp2p::{core, secio, PeerId, Swarm, Transport};
use slog::{debug, info, trace, warn}; use slog::{debug, info, trace, warn};
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use std::time::Duration; use std::time::Duration;
use types::{TopicBuilder, TopicHash};
type Libp2pStream = Boxed<(PeerId, StreamMuxerBox), Error>; type Libp2pStream = Boxed<(PeerId, StreamMuxerBox), Error>;
type Libp2pBehaviour = Behaviour<Substream<StreamMuxerBox>>; type Libp2pBehaviour = Behaviour<Substream<StreamMuxerBox>>;
@ -85,9 +85,17 @@ impl Service {
} }
// subscribe to default gossipsub topics // subscribe to default gossipsub topics
let mut topics = vec![];
//TODO: Handle multiple shard attestations. For now we simply use a separate topic for
//attestations
topics.push(config.shard_prefix);
topics.push(config.beacon_chain_topic);
topics.append(&mut config.topics.clone());
let mut subscribed_topics = vec![]; let mut subscribed_topics = vec![];
for topic in config.topics { for topic in topics {
let t = TopicBuilder::new(topic.to_string()).build(); let t = TopicBuilder::new(topic.clone()).build();
if swarm.subscribe(t) { if swarm.subscribe(t) {
trace!(log, "Subscribed to topic: {:?}", topic); trace!(log, "Subscribed to topic: {:?}", topic);
subscribed_topics.push(topic); subscribed_topics.push(topic);

View File

@ -4,6 +4,7 @@ use crate::NetworkConfig;
use beacon_chain::{BeaconChain, BeaconChainTypes}; use beacon_chain::{BeaconChain, BeaconChainTypes};
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError}; use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
use eth2_libp2p::Service as LibP2PService; use eth2_libp2p::Service as LibP2PService;
use eth2_libp2p::Topic;
use eth2_libp2p::{Libp2pEvent, PeerId}; use eth2_libp2p::{Libp2pEvent, PeerId};
use eth2_libp2p::{PubsubMessage, RPCEvent}; use eth2_libp2p::{PubsubMessage, RPCEvent};
use futures::prelude::*; use futures::prelude::*;
@ -13,7 +14,6 @@ use slog::{debug, info, o, trace};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::Arc; use std::sync::Arc;
use tokio::runtime::TaskExecutor; use tokio::runtime::TaskExecutor;
use types::Topic;
/// Service that handles communication between internal services and the eth2_libp2p network service. /// Service that handles communication between internal services and the eth2_libp2p network service.
pub struct Service<T: BeaconChainTypes> { pub struct Service<T: BeaconChainTypes> {

View File

@ -1,5 +1,6 @@
use beacon_chain::{BeaconChain, BeaconChainTypes}; use beacon_chain::{BeaconChain, BeaconChainTypes};
use eth2_libp2p::PubsubMessage; use eth2_libp2p::PubsubMessage;
use eth2_libp2p::TopicBuilder;
use futures::Future; use futures::Future;
use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink}; use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink};
use network::NetworkMessage; use network::NetworkMessage;
@ -140,7 +141,7 @@ impl<T: BeaconChainTypes> AttestationService for AttestationServiceInstance<T> {
let topic_string = self.chain.get_spec().shard_topic_prefix.clone(); let topic_string = self.chain.get_spec().shard_topic_prefix.clone();
// valid attestation, propagate to the network // valid attestation, propagate to the network
let topic = types::TopicBuilder::new(topic_string).build(); let topic = TopicBuilder::new(topic_string).build();
let message = PubsubMessage::Attestation(attestation); let message = PubsubMessage::Attestation(attestation);
self.network_chan self.network_chan

View File

@ -107,10 +107,7 @@ pub struct ChainSpec {
/* /*
* Network specific parameters * Network specific parameters
*/ */
pub boot_nodes: Vec<Multiaddr>,
pub chain_id: u8, pub chain_id: u8,
pub beacon_chain_topic: String,
pub shard_topic_prefix: String,
} }
impl ChainSpec { impl ChainSpec {
@ -219,10 +216,7 @@ impl ChainSpec {
/* /*
* Network specific * Network specific
*/ */
boot_nodes: vec![],
chain_id: 1, // foundation chain id chain_id: 1, // foundation chain id
beacon_chain_topic: String::from("beacon_chain"),
shard_topic_prefix: String::from("attestations"), // simple single attestation topic for now
} }
} }

View File

@ -82,6 +82,3 @@ pub type ProposerMap = HashMap<u64, usize>;
pub use bls::{AggregatePublicKey, AggregateSignature, Keypair, PublicKey, SecretKey, Signature}; pub use bls::{AggregatePublicKey, AggregateSignature, Keypair, PublicKey, SecretKey, Signature};
pub use fixed_len_vec::{typenum, typenum::Unsigned, FixedLenVec}; pub use fixed_len_vec::{typenum, typenum::Unsigned, FixedLenVec};
pub use libp2p::floodsub::{Topic, TopicBuilder, TopicHash};
pub use libp2p::multiaddr;
pub use libp2p::Multiaddr;