mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-10 20:22:02 +00:00
Removes network parameters from chain spec
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user