| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2024 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use std::{collections::HashMap, sync::Arc};
- use log::{debug, error, info};
- use smol::{fs::read_to_string, Executor};
- use structopt_toml::StructOptToml;
- use darkfi::{
- net::{session::SESSION_DEFAULT, P2p, P2pPtr, Settings},
- rpc::jsonrpc::JsonSubscriber,
- util::path::get_config_path,
- validator::ValidatorPtr,
- Error, Result,
- };
- use crate::{
- proto::{ProtocolProposal, ProtocolSync, ProtocolTx},
- BlockchainNetwork, CONFIG_FILE,
- };
- /// Auxiliary function to generate the P2P network and register all its protocols.
- pub async fn spawn_p2p(
- settings: &Settings,
- validator: &ValidatorPtr,
- subscribers: &HashMap<&'static str, JsonSubscriber>,
- executor: Arc<Executor<'static>>,
- ) -> Result<P2pPtr> {
- info!(target: "darkfid", "Registering sync network P2P protocols...");
- let p2p = P2p::new(settings.clone(), executor.clone()).await?;
- let registry = p2p.protocol_registry();
- let _validator = validator.clone();
- registry
- .register(SESSION_DEFAULT, move |channel, _p2p| {
- let validator = _validator.clone();
- async move { ProtocolSync::init(channel, validator).await.unwrap() }
- })
- .await;
- let _validator = validator.clone();
- let _subscriber = subscribers.get("proposals").unwrap().clone();
- registry
- .register(SESSION_DEFAULT, move |channel, p2p| {
- let validator = _validator.clone();
- let subscriber = _subscriber.clone();
- async move {
- ProtocolProposal::init(channel, validator, p2p, subscriber)
- .await
- .unwrap()
- }
- })
- .await;
- let _validator = validator.clone();
- let _subscriber = subscribers.get("txs").unwrap().clone();
- registry
- .register(SESSION_DEFAULT, move |channel, p2p| {
- let validator = _validator.clone();
- let subscriber = _subscriber.clone();
- async move { ProtocolTx::init(channel, validator, p2p, subscriber).await.unwrap() }
- })
- .await;
- Ok(p2p)
- }
- /// Auxiliary function to parse darkfid configuration file and extract requested
- /// blockchain network config.
- pub async fn parse_blockchain_config(
- config: Option<String>,
- network: &str,
- ) -> Result<BlockchainNetwork> {
- // Grab config path
- let config_path = get_config_path(config, CONFIG_FILE)?;
- debug!(target: "darkfid", "Parsing configuration file: {:?}", config_path);
- // Parse TOML file contents
- let contents = read_to_string(&config_path).await?;
- let contents: toml::Value = match toml::from_str(&contents) {
- Ok(v) => v,
- Err(e) => {
- error!(target: "darkfid", "Failed parsing TOML config: {}", e);
- return Err(Error::ParseFailed("Failed parsing TOML config"))
- }
- };
- // Grab requested network config
- let Some(table) = contents.as_table() else { return Err(Error::ParseFailed("TOML not a map")) };
- let Some(network_configs) = table.get("network_config") else {
- return Err(Error::ParseFailed("TOML does not contain network configurations"))
- };
- let Some(network_configs) = network_configs.as_table() else {
- return Err(Error::ParseFailed("`network_config` not a map"))
- };
- let Some(network_config) = network_configs.get(network) else {
- return Err(Error::ParseFailed("TOML does not contain requested network configuration"))
- };
- let network_config = toml::to_string(&network_config).unwrap();
- let network_config =
- match BlockchainNetwork::from_iter_with_toml::<Vec<String>>(&network_config, vec![]) {
- Ok(v) => v,
- Err(e) => {
- error!(target: "darkfid", "Failed parsing requested network configuration: {}", e);
- return Err(Error::ParseFailed("Failed parsing requested network configuration"))
- }
- };
- debug!(target: "darkfid", "Parsed network configuration: {:?}", network_config);
- Ok(network_config)
- }
|