diff --git a/binaries/cuprated/src/blockchain.rs b/binaries/cuprated/src/blockchain.rs index 366f9620..e1f5765b 100644 --- a/binaries/cuprated/src/blockchain.rs +++ b/binaries/cuprated/src/blockchain.rs @@ -1,7 +1,6 @@ //! Blockchain //! //! Will contain the chain manager and syncer. - use tokio::sync::mpsc; use tower::{Service, ServiceExt}; @@ -25,11 +24,13 @@ use types::{ ConsensusBlockchainReadHandle, }; +/// Checks if the genesis block is in the blockchain and adds it if not. pub async fn check_add_genesis( blockchain_read_handle: &mut BlockchainReadHandle, blockchain_write_handle: &mut BlockchainWriteHandle, network: &Network, ) { + // Try to get the chain height, will fail if the genesis block is not in the DB. if blockchain_read_handle .ready() .await @@ -67,6 +68,7 @@ pub async fn check_add_genesis( .unwrap(); } +/// Initializes the consensus services. pub async fn init_consensus( blockchain_read_handle: BlockchainReadHandle, context_config: ContextConfig, @@ -92,13 +94,14 @@ pub async fn init_consensus( Ok((block_verifier_svc, tx_verifier_svc, ctx_service)) } +/// Initializes the blockchain manager task and syncer. pub fn init_blockchain_manager( clearnet_interface: NetworkInterface, - block_downloader_config: BlockDownloaderConfig, blockchain_write_handle: BlockchainWriteHandle, blockchain_read_handle: BlockchainReadHandle, blockchain_context_service: BlockChainContextService, block_verifier_service: ConcreteBlockVerifierService, + block_downloader_config: BlockDownloaderConfig, ) { let (batch_tx, batch_rx) = mpsc::channel(1); diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index 1fd02f74..c71c40c8 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -1,8 +1,12 @@ //! cuprated config +use std::time::Duration; use cuprate_blockchain::config::{ Config as BlockchainConfig, ConfigBuilder as BlockchainConfigBuilder, }; +use cuprate_consensus::ContextConfig; +use cuprate_p2p::{block_downloader::BlockDownloaderConfig, AddressBookConfig, P2PConfig}; +use cuprate_p2p_core::{ClearNet, Network}; pub fn config() -> CupratedConfig { // TODO: read config options from the conf files & cli args. @@ -18,4 +22,41 @@ impl CupratedConfig { pub fn blockchain_config(&self) -> BlockchainConfig { BlockchainConfigBuilder::new().fast().build() } + + pub fn clearnet_config(&self) -> P2PConfig { + P2PConfig { + network: Network::Mainnet, + outbound_connections: 64, + extra_outbound_connections: 0, + max_inbound_connections: 0, + gray_peers_percent: 0.7, + server_config: None, + p2p_port: 0, + rpc_port: 0, + address_book_config: AddressBookConfig { + max_white_list_length: 1000, + max_gray_list_length: 5000, + peer_store_file: "p2p_state.bin".into(), + peer_save_period: Duration::from_secs(60), + }, + } + } + + pub fn block_downloader_config(&self) -> BlockDownloaderConfig { + BlockDownloaderConfig { + buffer_size: 50_000_000, + in_progress_queue_size: 50_000_000, + check_client_pool_interval: Duration::from_secs(45), + target_batch_size: 10_000_000, + initial_batch_size: 1, + } + } + + pub fn network(&self) -> Network { + Network::Mainnet + } + + pub fn context_config(&self) -> ContextConfig { + ContextConfig::main_net() + } } diff --git a/binaries/cuprated/src/main.rs b/binaries/cuprated/src/main.rs index 023c3a81..87fc7aa5 100644 --- a/binaries/cuprated/src/main.rs +++ b/binaries/cuprated/src/main.rs @@ -1,10 +1,3 @@ -use crate::blockchain::check_add_genesis; -use crate::config::CupratedConfig; -use clap::Parser; -use cuprate_p2p::block_downloader::BlockDownloaderConfig; -use cuprate_p2p::P2PConfig; -use cuprate_p2p_core::Network; -use std::time::Duration; use tokio::runtime::Runtime; use tracing::Level; @@ -14,6 +7,9 @@ mod p2p; mod rpc; mod txpool; +use blockchain::check_add_genesis; +use config::CupratedConfig; + fn main() { let config = config::config(); @@ -25,38 +21,32 @@ fn main() { let async_rt = init_tokio_rt(&config); async_rt.block_on(async move { - check_add_genesis(&mut bc_read_handle, &mut bc_write_handle, &Network::Mainnet).await; + check_add_genesis(&mut bc_read_handle, &mut bc_write_handle, &config.network()).await; - let (block_verifier, _tx_verifier, context_svc) = blockchain::init_consensus( - bc_read_handle.clone(), - cuprate_consensus::ContextConfig::main_net(), - ) - .await - .unwrap(); + let (block_verifier, _tx_verifier, context_svc) = + blockchain::init_consensus(bc_read_handle.clone(), config.context_config()) + .await + .unwrap(); let net = cuprate_p2p::initialize_network( p2p::request_handler::P2pProtocolRequestHandler, p2p::core_sync_svc::CoreSyncService(context_svc.clone()), - p2p::dummy_config(), + config.clearnet_config(), ) .await .unwrap(); blockchain::init_blockchain_manager( net, - BlockDownloaderConfig { - buffer_size: 50_000_000, - in_progress_queue_size: 50_000_000, - check_client_pool_interval: Duration::from_secs(45), - target_batch_size: 10_000_000, - initial_batch_size: 1, - }, bc_write_handle, bc_read_handle, context_svc, block_verifier, + config.block_downloader_config(), ); + // TODO: this can be removed as long as the main thread does not exit, so when command handling + // is added futures::future::pending::<()>().await; }); diff --git a/binaries/cuprated/src/p2p.rs b/binaries/cuprated/src/p2p.rs index 0560320b..7715be7c 100644 --- a/binaries/cuprated/src/p2p.rs +++ b/binaries/cuprated/src/p2p.rs @@ -2,28 +2,5 @@ //! //! Will handle initiating the P2P and contains a protocol request handler. -use cuprate_p2p::AddressBookConfig; -use cuprate_p2p_core::Network; -use std::time::Duration; - pub mod core_sync_svc; pub mod request_handler; - -pub fn dummy_config() -> cuprate_p2p::P2PConfig { - cuprate_p2p::P2PConfig { - network: Network::Mainnet, - outbound_connections: 64, - extra_outbound_connections: 0, - max_inbound_connections: 0, - gray_peers_percent: 0.7, - server_config: None, - p2p_port: 0, - rpc_port: 0, - address_book_config: AddressBookConfig { - max_white_list_length: 1000, - max_gray_list_length: 5000, - peer_store_file: "p2p_state.bin".into(), - peer_save_period: Duration::from_secs(60), - }, - } -}