Removes network parameters from chain spec
This commit is contained in:
parent
a31d6bcb22
commit
64abd0bc5b
@ -25,13 +25,10 @@ impl Default for ClientConfig {
|
||||
fs::create_dir_all(&data_dir)
|
||||
.unwrap_or_else(|_| panic!("Unable to create {:?}", &data_dir));
|
||||
|
||||
// currently lighthouse spec
|
||||
let default_spec = ChainSpec::lighthouse_testnet();
|
||||
let default_pubsub_topics = vec![
|
||||
default_spec.beacon_chain_topic.clone(),
|
||||
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);
|
||||
// builds a chain-specific network config
|
||||
let net_conf = NetworkConfig::from(default_spec.chain_id);
|
||||
|
||||
Self {
|
||||
data_dir: PathBuf::from(".lighthouse"),
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::rpc::{RPCEvent, RPCMessage, Rpc};
|
||||
use crate::NetworkConfig;
|
||||
use crate::{Topic, TopicHash};
|
||||
use futures::prelude::*;
|
||||
use libp2p::{
|
||||
core::{
|
||||
@ -15,7 +16,6 @@ use libp2p::{
|
||||
use slog::{debug, o, trace, warn};
|
||||
use ssz::{ssz_encode, Decode, DecodeError, Encode};
|
||||
use types::{Attestation, BeaconBlock};
|
||||
use types::{Topic, TopicHash};
|
||||
|
||||
/// Builds the network behaviour for the libp2p Swarm.
|
||||
/// Implements gossipsub message routing.
|
||||
|
@ -20,8 +20,12 @@ pub struct Config {
|
||||
boot_nodes: Vec<String>,
|
||||
/// Client version
|
||||
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>,
|
||||
/// 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 {
|
||||
@ -37,17 +41,16 @@ impl Default for Config {
|
||||
boot_nodes: vec![],
|
||||
client_version: version::version(),
|
||||
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 {
|
||||
pub fn new(boot_nodes: Vec<Multiaddr>, topics: Vec<String>) -> Self {
|
||||
let mut conf = Config::default();
|
||||
conf.boot_nodes = boot_nodes;
|
||||
conf.topics = topics;
|
||||
|
||||
conf
|
||||
pub fn new() -> Self {
|
||||
Config::default()
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,9 @@ mod service;
|
||||
|
||||
pub use behaviour::PubsubMessage;
|
||||
pub use config::Config as NetworkConfig;
|
||||
pub use libp2p::floodsub::{Topic, TopicBuilder, TopicHash};
|
||||
pub use libp2p::multiaddr;
|
||||
pub use libp2p::Multiaddr;
|
||||
pub use libp2p::{
|
||||
gossipsub::{GossipsubConfig, GossipsubConfigBuilder},
|
||||
PeerId,
|
||||
@ -17,5 +20,3 @@ pub use libp2p::{
|
||||
pub use rpc::RPCEvent;
|
||||
pub use service::Libp2pEvent;
|
||||
pub use service::Service;
|
||||
pub use types::multiaddr;
|
||||
pub use types::Multiaddr;
|
||||
|
@ -3,6 +3,7 @@ use crate::error;
|
||||
use crate::multiaddr::Protocol;
|
||||
use crate::rpc::RPCEvent;
|
||||
use crate::NetworkConfig;
|
||||
use crate::{TopicBuilder, TopicHash};
|
||||
use futures::prelude::*;
|
||||
use futures::Stream;
|
||||
use libp2p::core::{
|
||||
@ -17,7 +18,6 @@ use libp2p::{core, secio, PeerId, Swarm, Transport};
|
||||
use slog::{debug, info, trace, warn};
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::time::Duration;
|
||||
use types::{TopicBuilder, TopicHash};
|
||||
|
||||
type Libp2pStream = Boxed<(PeerId, StreamMuxerBox), Error>;
|
||||
type Libp2pBehaviour = Behaviour<Substream<StreamMuxerBox>>;
|
||||
@ -85,9 +85,17 @@ impl Service {
|
||||
}
|
||||
|
||||
// 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![];
|
||||
for topic in config.topics {
|
||||
let t = TopicBuilder::new(topic.to_string()).build();
|
||||
for topic in topics {
|
||||
let t = TopicBuilder::new(topic.clone()).build();
|
||||
if swarm.subscribe(t) {
|
||||
trace!(log, "Subscribed to topic: {:?}", topic);
|
||||
subscribed_topics.push(topic);
|
||||
|
@ -4,6 +4,7 @@ use crate::NetworkConfig;
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
|
||||
use eth2_libp2p::Service as LibP2PService;
|
||||
use eth2_libp2p::Topic;
|
||||
use eth2_libp2p::{Libp2pEvent, PeerId};
|
||||
use eth2_libp2p::{PubsubMessage, RPCEvent};
|
||||
use futures::prelude::*;
|
||||
@ -13,7 +14,6 @@ use slog::{debug, info, o, trace};
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use tokio::runtime::TaskExecutor;
|
||||
use types::Topic;
|
||||
|
||||
/// Service that handles communication between internal services and the eth2_libp2p network service.
|
||||
pub struct Service<T: BeaconChainTypes> {
|
||||
|
@ -1,5 +1,6 @@
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use eth2_libp2p::PubsubMessage;
|
||||
use eth2_libp2p::TopicBuilder;
|
||||
use futures::Future;
|
||||
use grpcio::{RpcContext, RpcStatus, RpcStatusCode, UnarySink};
|
||||
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();
|
||||
|
||||
// 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);
|
||||
|
||||
self.network_chan
|
||||
|
@ -107,10 +107,7 @@ pub struct ChainSpec {
|
||||
/*
|
||||
* Network specific parameters
|
||||
*/
|
||||
pub boot_nodes: Vec<Multiaddr>,
|
||||
pub chain_id: u8,
|
||||
pub beacon_chain_topic: String,
|
||||
pub shard_topic_prefix: String,
|
||||
}
|
||||
|
||||
impl ChainSpec {
|
||||
@ -219,10 +216,7 @@ impl ChainSpec {
|
||||
/*
|
||||
* Network specific
|
||||
*/
|
||||
boot_nodes: vec![],
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,6 +82,3 @@ pub type ProposerMap = HashMap<u64, usize>;
|
||||
|
||||
pub use bls::{AggregatePublicKey, AggregateSignature, Keypair, PublicKey, SecretKey, Signature};
|
||||
pub use fixed_len_vec::{typenum, typenum::Unsigned, FixedLenVec};
|
||||
pub use libp2p::floodsub::{Topic, TopicBuilder, TopicHash};
|
||||
pub use libp2p::multiaddr;
|
||||
pub use libp2p::Multiaddr;
|
||||
|
Loading…
Reference in New Issue
Block a user