Skip to content

Commit

Permalink
Include p2p into fuel core (#492)
Browse files Browse the repository at this point in the history
* add network service

* reuse config method

* add local_keypair to p2p_config

* add network service to fuel-core

* add p2p args

* move channels out of service initialization

* add p2p channels to other modules/services

* fmt is hard in the online editor

* ooops

* Sorry for ruining commit history

* oops

* pass bootstrap nodes in a vector instead

* remove box from db trait object

* update peering port

* cleanup filter map

* unwrap to default

* add default values

* start p2p network service only if name is provided

* update cli values

* add correct default values for topics

* set tcp port to be chosen automatically

* merge master

* add some comments

* clean up

* clean extra space

* set default values within clap

* use fuel-crypto for key generation

* add p2p optional feature

* Update fuel-core/Cargo.toml

Co-authored-by: ControlCplusControlV <44706811+ControlCplusControlV@users.noreply.github.com>
Co-authored-by: ControlC ControlV <controlc@ControlCs-MacBook-Pro.local>
Co-authored-by: Brandon Kite <brandonkite92@gmail.com>
  • Loading branch information
4 people authored Aug 24, 2022
1 parent aacc5fa commit e50553a
Show file tree
Hide file tree
Showing 19 changed files with 542 additions and 163 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion fuel-core-bft/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use fuel_core_interfaces::{
bft::BftMpsc,
block_importer::{ImportBlockBroadcast, ImportBlockMpsc},
block_producer::BlockProducerMpsc,
p2p::P2pRequestEvent,
relayer,
};
use parking_lot::Mutex;
Expand All @@ -28,7 +29,7 @@ impl Service {
pub async fn start(
&self,
_relayer: relayer::Sender,
_p2p_consensus: (),
_p2p_consensus: mpsc::Sender<P2pRequestEvent>,
_block_producer: mpsc::Sender<BlockProducerMpsc>,
_block_importer_sender: mpsc::Sender<ImportBlockMpsc>,
_block_importer_broadcast: broadcast::Receiver<ImportBlockBroadcast>,
Expand Down
7 changes: 7 additions & 0 deletions fuel-core-interfaces/src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::model::{BlockHeight, FuelBlock, SealedFuelBlock};
use crate::model::ConsensusVote;
use async_trait::async_trait;
use fuel_tx::Transaction;
use std::sync::Arc;
use tokio::sync::oneshot;
Expand Down Expand Up @@ -31,4 +32,10 @@ pub enum P2pRequestEvent {
BroadcastConsensusVote {
vote: Arc<ConsensusVote>,
},
Stop,
}

#[async_trait]
pub trait P2pDb: Send + Sync {
async fn get_sealed_block(&self, height: BlockHeight) -> Option<Arc<SealedFuelBlock>>;
}
3 changes: 3 additions & 0 deletions fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ fuel-core-bft = { path = "../fuel-core-bft", version = "0.10.1" }
fuel-core-interfaces = { path = "../fuel-core-interfaces", version = "0.10.1", features = [
"serde",
] }
fuel-crypto = { version = "0.6", default-features = false, features = [ "random" ] }
fuel-p2p = { path = "../fuel-p2p", version = "0.10.1", optional = true }
fuel-relayer = { path = "../fuel-relayer", version = "0.10.1" }
fuel-sync = { path = "../fuel-sync", version = "0.10.1" }
fuel-txpool = { path = "../fuel-txpool", version = "0.10.1" }
Expand Down Expand Up @@ -87,3 +89,4 @@ prometheus = ["dep:prometheus"]
default = ["rocksdb", "prometheus", "debug"]
debug = ["fuel-core-interfaces/debug"]
test-helpers = []
p2p = ["dep:fuel-p2p"]
1 change: 1 addition & 0 deletions fuel-core/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Opt {
command: Fuel,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Parser)]
pub enum Fuel {
Run(run::Command),
Expand Down
20 changes: 19 additions & 1 deletion fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use fuel_core::service::{Config, DbType, VMConfig};
use std::{env, io, net, path::PathBuf};
use strum::VariantNames;
use tracing::{info, trace};

#[cfg(feature = "p2p")]
mod p2p;
mod relayer;

#[derive(Debug, Clone, Parser)]
Expand Down Expand Up @@ -55,6 +56,10 @@ pub struct Command {

#[clap(flatten)]
pub relayer_args: relayer::RelayerArgs,

#[cfg(feature = "p2p")]
#[clap(flatten)]
pub p2p_args: p2p::P2pArgs,
}

impl Command {
Expand All @@ -71,9 +76,20 @@ impl Command {
min_gas_price,
predicates,
relayer_args,
#[cfg(feature = "p2p")]
p2p_args,
} = self;

let addr = net::SocketAddr::new(ip, port);

#[cfg(feature = "p2p")]
let p2p = {
match p2p_args.into() {
Ok(value) => value,
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
}
};

Ok(Config {
addr,
database_path,
Expand All @@ -95,6 +111,8 @@ impl Command {
relayer: relayer_args.into(),
bft: Default::default(),
sync: Default::default(),
#[cfg(feature = "p2p")]
p2p,
})
}
}
Expand Down
141 changes: 141 additions & 0 deletions fuel-core/src/cli/run/p2p.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use std::{
net::{IpAddr, Ipv4Addr},
path::PathBuf,
time::Duration,
};

use clap::Args;

use fuel_p2p::{config::P2PConfig, Multiaddr};

#[derive(Debug, Clone, Args)]
pub struct P2pArgs {
/// Path to the location of DER-encoded Secp256k1 Keypair
#[clap(long = "keypair")]
pub keypair: Option<PathBuf>,

/// The name of the p2p Network
/// If this value is not provided the p2p network won't start
#[clap(long = "network", default_value = "")]
pub network: String,

/// p2p network's IP Address
#[clap(long = "address")]
pub address: Option<IpAddr>,

/// p2p network's TCP Port
#[clap(long = "peering-port", default_value = "4001")]
pub peering_port: u16,

/// Max Block size
#[clap(long = "max_block_size", default_value = "100000")]
pub max_block_size: usize,

/// Addresses of the bootstrap nodes
/// They should contain PeerId at the end of the specified Multiaddr
#[clap(long = "bootstrap_nodes")]
pub bootstrap_nodes: Vec<Multiaddr>,

/// Allow nodes to be discoverable on the local network
#[clap(long = "enable_mdns")]
pub enable_mdns: bool,

/// Maximum amount of allowed connected peers
#[clap(long = "max_peers_connected", default_value = "50")]
pub max_peers_connected: usize,

/// Enable random walk for p2p node discovery
#[clap(long = "enable_random_walk")]
pub enable_random_walk: bool,

/// Choose to include private IPv4/IPv6 addresses as discoverable
/// except for the ones stored in `bootstrap_nodes`
#[clap(long = "allow_private_addresses")]
pub allow_private_addresses: bool,

/// Choose how long will connection keep alive if idle
#[clap(long = "connection_idle_timeout", default_value = "120")]
pub connection_idle_timeout: u64,

/// Choose how often to recieve PeerInfo from other nodes
#[clap(long = "info_interval", default_value = "3")]
pub info_interval: u64,

/// Choose the interval at which identification requests are sent to
/// the remote on established connections after the first request
#[clap(long = "identify_interval", default_value = "5")]
pub identify_interval: u64,

/// Choose which topics to subscribe to via gossipsub protocol
#[clap(long = "topics", default_values = &["new_tx", "new_block", "consensus_vote"])]
pub topics: Vec<String>,

/// Choose max mesh size for gossipsub protocol
#[clap(long = "max_mesh_size", default_value = "12")]
pub max_mesh_size: usize,

/// Choose min mesh size for gossipsub protocol
#[clap(long = "min_mesh_size", default_value = "4")]
pub min_mesh_size: usize,

/// Choose ideal mesh size for gossipsub protocol
#[clap(long = "ideal_mesh_size", default_value = "6")]
pub ideal_mesh_size: usize,

/// Choose timeout for sent requests in RequestResponse protocol
#[clap(long = "request_timeout", default_value = "20")]
pub request_timeout: u64,

/// Choose how long RequestResponse protocol connections will live if idle
#[clap(long = "connection_keep_alive", default_value = "20")]
pub connection_keep_alive: u64,
}

impl From<P2pArgs> for anyhow::Result<P2PConfig> {
fn from(args: P2pArgs) -> Self {
let local_keypair = {
match args.keypair {
Some(path) => {
let phrase = std::fs::read_to_string(path)?;

let secret_key = fuel_crypto::SecretKey::new_from_mnemonic_phrase_with_path(
&phrase,
"m/44'/60'/0'/0/0",
)?;

fuel_p2p::config::convert_to_libp2p_keypair(&mut secret_key.to_vec())?
}
_ => {
let mut rand = fuel_crypto::rand::thread_rng();
let secret_key = fuel_crypto::SecretKey::random(&mut rand);

fuel_p2p::config::convert_to_libp2p_keypair(&mut secret_key.to_vec())?
}
}
};

Ok(P2PConfig {
local_keypair,
network_name: args.network,
address: args
.address
.unwrap_or_else(|| IpAddr::V4(Ipv4Addr::from([0, 0, 0, 0]))),
tcp_port: args.peering_port,
max_block_size: args.max_block_size,
bootstrap_nodes: args.bootstrap_nodes,
enable_mdns: args.enable_mdns,
max_peers_connected: args.max_peers_connected,
allow_private_addresses: args.allow_private_addresses,
enable_random_walk: args.enable_random_walk,
connection_idle_timeout: Some(Duration::from_secs(args.connection_idle_timeout)),
topics: args.topics,
max_mesh_size: args.max_mesh_size,
min_mesh_size: args.min_mesh_size,
ideal_mesh_size: args.ideal_mesh_size,
set_request_timeout: Duration::from_secs(args.request_timeout),
set_connection_keep_alive: Duration::from_secs(args.connection_keep_alive),
info_interval: Some(Duration::from_secs(args.info_interval)),
identify_interval: Some(Duration::from_secs(args.identify_interval)),
})
}
}
7 changes: 7 additions & 0 deletions fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use fuel_core_interfaces::{
model::{
BlockHeight, ConsensusId, DaBlockHeight, SealedFuelBlock, ValidatorId, ValidatorStake,
},
p2p::P2pDb,
relayer::{RelayerDb, StakingDiff},
txpool::TxPoolDb,
};
Expand Down Expand Up @@ -131,6 +132,12 @@ unsafe impl Send for Database {}
unsafe impl Sync for Database {}

impl TxPoolDb for Database {}
#[async_trait]
impl P2pDb for Database {
async fn get_sealed_block(&self, height: BlockHeight) -> Option<Arc<SealedFuelBlock>> {
<Self as RelayerDb>::get_sealed_block(self, height).await
}
}

impl Database {
#[cfg(feature = "rocksdb")]
Expand Down
7 changes: 7 additions & 0 deletions fuel-core/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use std::{
};
use strum_macros::{Display, EnumString, EnumVariantNames};

#[cfg(feature = "p2p")]
use fuel_p2p;

#[derive(Clone, Debug)]
pub struct Config {
pub addr: SocketAddr,
Expand All @@ -24,6 +27,8 @@ pub struct Config {
pub bft: fuel_core_bft::Config,
pub sync: fuel_sync::Config,
pub relayer: fuel_relayer::Config,
#[cfg(feature = "p2p")]
pub p2p: fuel_p2p::config::P2PConfig,
}

impl Config {
Expand All @@ -44,6 +49,8 @@ impl Config {
bft: Default::default(),
sync: Default::default(),
relayer: Default::default(),
#[cfg(feature = "p2p")]
p2p: fuel_p2p::config::P2PConfig::default_with_network("test_network"),
}
}
}
Expand Down
Loading

0 comments on commit e50553a

Please sign in to comment.