Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Apr 5, 2023
1 parent 44efd6c commit 0327564
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 21 deletions.
4 changes: 4 additions & 0 deletions applications/tari_app_utilities/src/common_cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub struct CommonCliArgs {
#[clap()]
pub log_level: Option<Level>,

/// Supply a network (overrides existing configuration)
#[clap(long, env = "TARI_NETWORK")]
pub network: Option<Network>,

/// Overrides for properties in the config file, e.g. -p base_node.netwok=esmeralda
#[clap(short = 'p', parse(try_from_str = parse_key_val), multiple_occurrences(true))]
pub config_property_overrides: Vec<(String, String)>,
Expand Down
1 change: 1 addition & 0 deletions applications/tari_base_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub async fn run_base_node(
config: config_path.into_os_string().into_string().unwrap(),
log_config: None,
log_level: None,
network: None,
config_property_overrides: vec![],
},
init: true,
Expand Down
1 change: 1 addition & 0 deletions applications/tari_console_wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub fn run_wallet(shutdown: &mut Shutdown, runtime: Runtime, config: &mut Applic
config: config_path.into_os_string().into_string().unwrap(),
log_config: None,
log_level: None,
network: None,
config_property_overrides: vec![],
},
password: None,
Expand Down
22 changes: 14 additions & 8 deletions applications/tari_miner/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@
//! All miner options configured under `[miner]` section of
//! Tari's `config.toml`.

use std::{str::FromStr, time::Duration};
use std::time::Duration;

use serde::{Deserialize, Serialize};
use tari_app_grpc::tari_rpc::{pow_algo::PowAlgos, NewBlockTemplateRequest, PowAlgo};
use tari_common::SubConfigPath;
use tari_common::{configuration::Network, SubConfigPath};
use tari_common_types::grpc_authentication::GrpcAuthentication;
use tari_comms::multiaddr::Multiaddr;

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct MinerConfig {
/// GRPC address of base node
pub base_node_grpc_address: Multiaddr,
pub base_node_grpc_address: Option<Multiaddr>,
/// GRPC address of console wallet
pub wallet_grpc_address: Multiaddr,
pub wallet_grpc_address: Option<Multiaddr>,
/// GRPC authentication for console wallet
pub wallet_grpc_authentication: GrpcAuthentication,
/// Number of mining threads
Expand All @@ -73,6 +73,8 @@ pub struct MinerConfig {
/// Note that this data is publicly readable, but it is suggested you populate it so that
/// pool dominance can be seen before any one party has more than 51%.
pub coinbase_extra: String,
/// Selected network
pub network: Network,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand All @@ -90,8 +92,8 @@ impl SubConfigPath for MinerConfig {
impl Default for MinerConfig {
fn default() -> Self {
Self {
base_node_grpc_address: Multiaddr::from_str("/ip4/127.0.0.1/tcp/18142").unwrap(),
wallet_grpc_address: Multiaddr::from_str("/ip4/127.0.0.1/tcp/18143").unwrap(),
base_node_grpc_address: None,
wallet_grpc_address: None,
wallet_grpc_authentication: GrpcAuthentication::default(),
num_mining_threads: num_cpus::get(),
mine_on_tip_only: true,
Expand All @@ -101,6 +103,7 @@ impl Default for MinerConfig {
mining_wallet_address: String::new(),
mining_worker_name: String::new(),
coinbase_extra: "tari_miner".to_string(),
network: Default::default(),
}
}
}
Expand All @@ -127,7 +130,10 @@ impl MinerConfig {

#[cfg(test)]
mod test {
use std::str::FromStr;

use tari_common::DefaultConfigLoader;
use tari_comms::multiaddr::Multiaddr;

use crate::config::MinerConfig;

Expand All @@ -147,8 +153,8 @@ mine_on_tip_only = false
assert_eq!(config.num_mining_threads, 2);
assert_eq!(config.wallet_grpc_address, MinerConfig::default().wallet_grpc_address);
assert_eq!(
config.base_node_grpc_address.to_string(),
"/dns4/my_base_node/tcp/1234".to_string()
config.base_node_grpc_address,
Some(Multiaddr::from_str("/dns4/my_base_node/tcp/1234").unwrap())
);
assert!(!config.mine_on_tip_only);
}
Expand Down
48 changes: 45 additions & 3 deletions applications/tari_miner/src/run_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use tari_app_grpc::{
tari_rpc::{base_node_client::BaseNodeClient, wallet_client::WalletClient},
};
use tari_common::{
configuration::bootstrap::{grpc_default_port, ApplicationType},
exit_codes::{ExitCode, ExitError},
load_configuration,
DefaultConfigLoader,
Expand Down Expand Up @@ -59,8 +60,12 @@ type WalletGrpcClient = WalletClient<InterceptedService<Channel, ClientAuthentic

pub async fn start_miner(cli: Cli) -> Result<(), ExitError> {
let config_path = cli.common.config_path();
println!("cli common {:?}", &cli.common);
let cfg = load_configuration(config_path.as_path(), true, &cli.common)?;
let config = MinerConfig::load_from(&cfg).expect("Failed to load config");
println!("cfg {:?}", &cfg);
let mut config = MinerConfig::load_from(&cfg).expect("Failed to load config");
println!("config {:?}", config);
setup_grpc_config(&mut config);
debug!(target: LOG_TARGET_FILE, "{:?}", config);
if !config.mining_wallet_address.is_empty() && !config.mining_pool_address.is_empty() {
let url = config.mining_pool_address.clone();
Expand Down Expand Up @@ -164,7 +169,12 @@ pub async fn start_miner(cli: Cli) -> Result<(), ExitError> {
}

async fn connect(config: &MinerConfig) -> Result<(BaseNodeClient<Channel>, WalletGrpcClient), MinerError> {
let base_node_addr = multiaddr_to_socketaddr(&config.base_node_grpc_address)?;
let base_node_addr = multiaddr_to_socketaddr(
&config
.base_node_grpc_address
.clone()
.expect("no base node grpc address found"),
)?;
info!(target: LOG_TARGET, "🔗 Connecting to base node at {}", base_node_addr);
let node_conn = BaseNodeClient::connect(format!("http://{}", base_node_addr)).await?;

Expand All @@ -184,7 +194,15 @@ async fn connect(config: &MinerConfig) -> Result<(BaseNodeClient<Channel>, Walle
}

async fn connect_wallet(config: &MinerConfig) -> Result<WalletGrpcClient, MinerError> {
let wallet_addr = format!("http://{}", multiaddr_to_socketaddr(&config.wallet_grpc_address)?);
let wallet_addr = format!(
"http://{}",
multiaddr_to_socketaddr(
&config
.wallet_grpc_address
.clone()
.expect("Wallet grpc address not found")
)?
);
info!(target: LOG_TARGET, "👛 Connecting to wallet at {}", wallet_addr);
let channel = Endpoint::from_str(&wallet_addr)?.connect().await?;
let wallet_conn = WalletClient::with_interceptor(
Expand Down Expand Up @@ -341,3 +359,27 @@ async fn validate_tip(
}
Ok(())
}

fn setup_grpc_config(config: &mut MinerConfig) {
if config.base_node_grpc_address.is_none() {
config.base_node_grpc_address = Some(
format!(
"/ip4/127.0.0.1/tcp/{}",
grpc_default_port(ApplicationType::BaseNode, config.network)
)
.parse()
.unwrap(),
);
}

if config.wallet_grpc_address.is_none() {
config.wallet_grpc_address = Some(
format!(
"/ip4/127.0.0.1/tcp/{}",
grpc_default_port(ApplicationType::ConsoleWallet, config.network)
)
.parse()
.unwrap(),
);
}
}
16 changes: 6 additions & 10 deletions common/src/configuration/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,18 @@ pub fn grpc_default_port(app_type: ApplicationType, network: Network) -> u16 {
Network::MainNet => 18102u16,
Network::StageNet => 18172u16,
Network::NextNet => 18182u16,
Network::Weatherwax => 18112,
Network::Dibbler => 18122,
Network::Esmeralda => 18142,
Network::Igor => 18152,
Network::LocalNet => 18162,
Network::Esmeralda => 18142u16,
Network::Igor => 18152u16,
Network::LocalNet => 18162u16,
_ => unreachable!("Network {} not supported", network),
},
ApplicationType::ConsoleWallet => match network {
Network::MainNet => 18103u16,
Network::StageNet => 18173u16,
Network::NextNet => 18183u16,
Network::Weatherwax => 18113,
Network::Dibbler => 18123,
Network::Esmeralda => 18143,
Network::Igor => 18153,
Network::LocalNet => 18163,
Network::Esmeralda => 18143u16,
Network::Igor => 18153u16,
Network::LocalNet => 18163u16,
_ => unreachable!("Network {} not supported", network),
},
_ => unreachable!("Application {} not supported", app_type),
Expand Down
1 change: 1 addition & 0 deletions integration_tests/tests/utils/merge_mining_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl MergeMiningProxyProcess {
config: config_path.into_os_string().into_string().unwrap(),
log_config: None,
log_level: None,
network: None,
config_property_overrides: vec![
("merge_mining_proxy.listener_address".to_string(), proxy_full_address),
(
Expand Down
1 change: 1 addition & 0 deletions integration_tests/tests/utils/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl MinerProcess {
("miner.num_mining_threads".to_string(), "1".to_string()),
("miner.mine_on_tip_only".to_string(), "false".to_string()),
],
network: None,
},
mine_until_height: None,
miner_max_blocks: blocks,
Expand Down
1 change: 1 addition & 0 deletions integration_tests/tests/utils/wallet_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ pub fn get_default_cli() -> Cli {
config: Default::default(),
log_config: None,
log_level: None,
network: None,
config_property_overrides: vec![],
},
password: None,
Expand Down

0 comments on commit 0327564

Please sign in to comment.