feat: add trusted nodes configuration (#569)

* Add preferred nodes to config

* Add preferred nodes on boot

* Add flag in config for trusted only mode

* Add preferred nodes configuration to config

* Fix comment

* Add preferred nodes to config file

* Rename preferred_nodes to trusted_nodes

* Change preferred to trusted

I renamed preferred_nodes to trusted_nodes in various places.
Seems I missed quite a bit of them.

* Pull trusted_only from config

* Rename DiscoveryConfig to PeersConfig

* Fix last commit

Now actually renamed DiscoveryConfig

* Rename trusted_only to connect_trusted_nodes_only

* Add helper function

* Use HashSet for boot_nodes and trusted_nodes

* Change trusted nodes functions in ConfigBuilder

* Move trusted peers from discv4 to network config

* Add trusted nodes to peers on Manager creation

* Use NodeRecord in trusted_nodes config

* Fix comment

* Move trusted_nodes config to PeersConfig

* Add trusted nodes directly to peers

* Move network_config to Config impl

* Move start_network to NetworkConfig impl
This commit is contained in:
Tomás
2022-12-28 17:48:11 -03:00
committed by GitHub
parent d9d0ba14c4
commit 76e76bb651
5 changed files with 94 additions and 45 deletions

View File

@ -1,4 +1,13 @@
//! Configuration files.
use std::{collections::HashSet, sync::Arc};
use reth_db::database::Database;
use reth_network::{
config::{mainnet_nodes, rng_secret_key},
NetworkConfig,
};
use reth_primitives::{NodeRecord, H256};
use reth_provider::ProviderImpl;
use serde::{Deserialize, Serialize};
/// Configuration for the reth node.
@ -7,6 +16,28 @@ pub struct Config {
/// Configuration for each stage in the pipeline.
// TODO(onbjerg): Can we make this easier to maintain when we add/remove stages?
pub stages: StageConfig,
/// Configuration for the discovery service.
pub peers: PeersConfig,
}
impl Config {
/// Initializes network config from read data
pub fn network_config<DB: Database>(
&self,
db: Arc<DB>,
chain_id: u64,
genesis_hash: H256,
) -> NetworkConfig<ProviderImpl<DB>> {
let peer_config = reth_network::PeersConfig::default()
.with_trusted_nodes(self.peers.trusted_nodes.clone())
.with_connect_trusted_nodes_only(self.peers.connect_trusted_nodes_only);
NetworkConfig::builder(Arc::new(ProviderImpl::new(db)), rng_secret_key())
.boot_nodes(mainnet_nodes())
.peer_config(peer_config)
.genesis_hash(genesis_hash)
.chain_id(chain_id)
.build()
}
}
/// Configuration for each stage in the pipeline.
@ -78,3 +109,12 @@ impl Default for SenderRecoveryConfig {
Self { commit_threshold: 5_000, batch_size: 1000 }
}
}
/// Configuration for peer managing.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct PeersConfig {
/// Trusted nodes to connect to.
pub trusted_nodes: HashSet<NodeRecord>,
/// Connect to trusted nodes only?
pub connect_trusted_nodes_only: bool,
}

View File

@ -19,13 +19,7 @@ use reth_db::{
use reth_downloaders::{bodies, headers};
use reth_executor::Config as ExecutorConfig;
use reth_interfaces::consensus::ForkchoiceState;
use reth_network::{
config::{mainnet_nodes, rng_secret_key},
error::NetworkError,
NetworkConfig, NetworkHandle, NetworkManager,
};
use reth_primitives::{Account, Header, H256};
use reth_provider::{db_provider::ProviderImpl, BlockProvider, HeaderProvider};
use reth_stages::{
metrics::HeaderMetrics,
stages::{
@ -105,7 +99,8 @@ impl Command {
let genesis_hash = init_genesis(db.clone(), self.chain.genesis.clone())?;
info!("Connecting to p2p");
let network = start_network(network_config(db.clone(), chain_id, genesis_hash)).await?;
let network =
config.network_config(db.clone(), chain_id, genesis_hash).start_network().await?;
// TODO: Are most of these Arcs unnecessary? For example, fetch client is completely
// cloneable on its own
@ -207,31 +202,3 @@ fn init_genesis<DB: Database>(db: Arc<DB>, genesis: Genesis) -> Result<H256, ret
tx.commit()?;
Ok(hash)
}
// TODO: This should be based on some external config
fn network_config<DB: Database>(
db: Arc<DB>,
chain_id: u64,
genesis_hash: H256,
) -> NetworkConfig<ProviderImpl<DB>> {
NetworkConfig::builder(Arc::new(ProviderImpl::new(db)), rng_secret_key())
.boot_nodes(mainnet_nodes())
.genesis_hash(genesis_hash)
.chain_id(chain_id)
.build()
}
/// Starts the networking stack given a [NetworkConfig] and returns a handle to the network.
async fn start_network<C>(config: NetworkConfig<C>) -> Result<NetworkHandle, NetworkError>
where
C: BlockProvider + HeaderProvider + 'static,
{
let client = config.client.clone();
let (handle, network, _txpool, eth) =
NetworkManager::builder(config).await?.request_handler(client).split_with_handle();
tokio::task::spawn(network);
// TODO: tokio::task::spawn(txpool);
tokio::task::spawn(eth);
Ok(handle)
}