Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include p2p into fuel core #492

Merged
merged 36 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
56763da
add network service
leviathanbeak Jul 15, 2022
f049814
reuse config method
leviathanbeak Jul 15, 2022
7e5ecbf
add local_keypair to p2p_config
leviathanbeak Jul 15, 2022
5f47f0c
add network service to fuel-core
leviathanbeak Jul 15, 2022
e731966
add p2p args
leviathanbeak Jul 15, 2022
1d1f0fd
move channels out of service initialization
leviathanbeak Jul 15, 2022
f9f1d17
add p2p channels to other modules/services
leviathanbeak Jul 15, 2022
c4d2963
Merge branch 'master' into leviathanbeak/include_p2p_into_fuel_core
ControlCplusControlV Jul 28, 2022
49677f9
fmt is hard in the online editor
Jul 28, 2022
9cbc347
ooops
Jul 28, 2022
f29f0b5
Sorry for ruining commit history
Jul 28, 2022
d88d81a
oops
Jul 28, 2022
fe7d5fb
pass bootstrap nodes in a vector instead
leviathanbeak Aug 2, 2022
156e2d6
remove box from db trait object
leviathanbeak Aug 2, 2022
b8d08da
update peering port
leviathanbeak Aug 2, 2022
c3fe4b9
Merge branch 'leviathanbeak/include_p2p_into_fuel_core' of github.com…
leviathanbeak Aug 3, 2022
5663183
cleanup filter map
leviathanbeak Aug 3, 2022
46e9528
unwrap to default
leviathanbeak Aug 3, 2022
31d1c86
add default values
leviathanbeak Aug 3, 2022
03fae45
start p2p network service only if name is provided
leviathanbeak Aug 3, 2022
6fc6e38
update cli values
leviathanbeak Aug 3, 2022
d5a2b17
add correct default values for topics
leviathanbeak Aug 3, 2022
d66ac42
set tcp port to be chosen automatically
leviathanbeak Aug 9, 2022
5fc9b6c
merge master
leviathanbeak Aug 11, 2022
df7aeee
add some comments
leviathanbeak Aug 11, 2022
25e9f83
Merge branch 'master' of github.com:FuelLabs/fuel-core into leviathan…
leviathanbeak Aug 11, 2022
b6cc049
Merge branch 'master' of github.com:FuelLabs/fuel-core into leviathan…
leviathanbeak Aug 15, 2022
8d817bd
clean up
leviathanbeak Aug 15, 2022
bb940b6
Merge branch 'master' into leviathanbeak/include_p2p_into_fuel_core
ControlCplusControlV Aug 17, 2022
9d8e16c
clean extra space
leviathanbeak Aug 17, 2022
418ed65
set default values within clap
leviathanbeak Aug 17, 2022
53581e5
use fuel-crypto for key generation
leviathanbeak Aug 18, 2022
06e0daf
add p2p optional feature
leviathanbeak Aug 22, 2022
3fe6d82
Merge branch 'master' into leviathanbeak/include_p2p_into_fuel_core
leviathanbeak Aug 22, 2022
cae141c
Update fuel-core/Cargo.toml
Voxelot Aug 23, 2022
ac83774
Merge branch 'master' into leviathanbeak/include_p2p_into_fuel_core
Voxelot Aug 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)),
ControlCplusControlV marked this conversation as resolved.
Show resolved Hide resolved
}
};

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