Skip to content

Commit

Permalink
feat: add support for MultiAddr in RPC config (#3557)
Browse files Browse the repository at this point in the history
Motivation and Context
---

Robust networking support in docker configurations requires us to be able to specify the gRPC address of
things like the node and wallet in terms of domains instead of raw IP addresses.

This adds support for MultiAddr specification of these addresses while still being 100%
backwards compatible.

Also included in #3534 

How Has This Been Tested?
---
No new tests added.
  • Loading branch information
CjS77 authored Nov 10, 2021
1 parent 5be3b9d commit 9f8e289
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 57 deletions.
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.

10 changes: 8 additions & 2 deletions applications/tari_base_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ use tari_app_utilities::{
utilities::setup_runtime,
};
use tari_common::{configuration::bootstrap::ApplicationType, exit_codes::ExitCodes, ConfigBootstrap, GlobalConfig};
use tari_comms::{peer_manager::PeerFeatures, tor::HiddenServiceControllerError};
use tari_comms::{
peer_manager::PeerFeatures,
tor::HiddenServiceControllerError,
utils::multiaddr::multiaddr_to_socketaddr,
};
use tari_core::chain_storage::ChainStorageError;
use tari_shutdown::{Shutdown, ShutdownSignal};
use tokio::{
Expand Down Expand Up @@ -228,7 +232,9 @@ async fn run_node(node_config: Arc<GlobalConfig>, bootstrap: ConfigBootstrap) ->
if node_config.grpc_enabled {
// Go, GRPC, go go
let grpc = crate::grpc::base_node_grpc_server::BaseNodeGrpcServer::from_base_node_context(&ctx);
task::spawn(run_grpc(grpc, node_config.grpc_base_node_address, shutdown.to_signal()));
let socket_addr = multiaddr_to_socketaddr(&node_config.grpc_base_node_address)
.map_err(|e| ExitCodes::ConfigError(e.to_string()))?;
task::spawn(run_grpc(grpc, socket_addr, shutdown.to_signal()));
}

// Run, node, run!
Expand Down
12 changes: 6 additions & 6 deletions applications/tari_console_wallet/src/wallet_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use crate::{
};
use log::*;
use rand::{rngs::OsRng, seq::SliceRandom};
use std::{fs, io::Stdout, net::SocketAddr, path::PathBuf};
use std::{fs, io::Stdout, path::PathBuf};
use tari_common::{exit_codes::ExitCodes, ConfigBootstrap, GlobalConfig};
use tari_comms::peer_manager::Peer;
use tari_comms::{multiaddr::Multiaddr, peer_manager::Peer, utils::multiaddr::multiaddr_to_socketaddr};
use tari_wallet::WalletSqlite;
use tokio::runtime::Handle;
use tonic::transport::Server;
Expand Down Expand Up @@ -213,7 +213,7 @@ pub fn tui_mode(config: WalletModeConfig, mut wallet: WalletSqlite) -> Result<()
..
} = config;
let grpc = WalletGrpcServer::new(wallet.clone());
handle.spawn(run_grpc(grpc, global_config.grpc_console_wallet_address));
handle.spawn(run_grpc(grpc, global_config.grpc_console_wallet_address.clone()));

let notifier = Notifier::new(notify_script, handle.clone(), wallet.clone());

Expand Down Expand Up @@ -294,12 +294,12 @@ pub fn grpc_mode(config: WalletModeConfig, wallet: WalletSqlite) -> Result<(), E
Ok(())
}

async fn run_grpc(grpc: WalletGrpcServer, grpc_console_wallet_address: SocketAddr) -> Result<(), String> {
async fn run_grpc(grpc: WalletGrpcServer, grpc_console_wallet_address: Multiaddr) -> Result<(), String> {
info!(target: LOG_TARGET, "Starting GRPC on {}", grpc_console_wallet_address);

let socket = multiaddr_to_socketaddr(&grpc_console_wallet_address).map_err(|e| e.to_string())?;
Server::builder()
.add_service(tari_app_grpc::tari_rpc::wallet_server::WalletServer::new(grpc))
.serve(grpc_console_wallet_address)
.serve(socket)
.await
.map_err(|e| format!("GRPC server returned error:{}", e))?;
info!(target: LOG_TARGET, "Stopping GRPC");
Expand Down
1 change: 1 addition & 0 deletions applications/tari_merge_mining_proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ envlog = ["env_logger"]
[dependencies]
tari_app_grpc = { path = "../tari_app_grpc" }
tari_common = { path = "../../common" }
tari_comms = { path = "../../comms" }
tari_core = { path = "../../base_layer/core", default-features = false, features = ["transactions"] }
tari_app_utilities = { path = "../tari_app_utilities" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", branch = "main" }
Expand Down
4 changes: 2 additions & 2 deletions applications/tari_merge_mining_proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{block_template_data::BlockTemplateRepository, error::MmProxyError};
use futures::future;
use hyper::{service::make_service_fn, Server};
use proxy::{MergeMiningProxyConfig, MergeMiningProxyService};
use std::convert::Infallible;
use std::convert::{Infallible, TryFrom};
use tari_app_grpc::tari_rpc as grpc;
use tari_app_utilities::initialization::init_configuration;
use tari_common::configuration::bootstrap::ApplicationType;
Expand All @@ -50,7 +50,7 @@ use tokio::time::Duration;
async fn main() -> Result<(), anyhow::Error> {
let (_, config, _) = init_configuration(ApplicationType::MergeMiningProxy)?;

let config = MergeMiningProxyConfig::from(config);
let config = MergeMiningProxyConfig::try_from(config)?;
let addr = config.proxy_host_address;
let client = reqwest::Client::builder()
.connect_timeout(Duration::from_secs(5))
Expand Down
18 changes: 12 additions & 6 deletions applications/tari_merge_mining_proxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use reqwest::{ResponseBuilderExt, Url};
use serde_json as json;
use std::{
cmp,
convert::TryFrom,
future::Future,
net::SocketAddr,
pin::Pin,
Expand All @@ -46,6 +47,7 @@ use std::{
};
use tari_app_grpc::tari_rpc as grpc;
use tari_common::{configuration::Network, GlobalConfig};
use tari_comms::utils::multiaddr::multiaddr_to_socketaddr;
use tari_core::proof_of_work::{monero_rx, monero_rx::FixedByteArray};
use tari_utilities::hex::Hex;
use tracing::{debug, error, info, instrument, trace, warn};
Expand All @@ -70,20 +72,24 @@ pub struct MergeMiningProxyConfig {
pub wait_for_initial_sync_at_startup: bool,
}

impl From<GlobalConfig> for MergeMiningProxyConfig {
fn from(config: GlobalConfig) -> Self {
Self {
impl TryFrom<GlobalConfig> for MergeMiningProxyConfig {
type Error = std::io::Error;

fn try_from(config: GlobalConfig) -> Result<Self, Self::Error> {
let grpc_base_node_address = multiaddr_to_socketaddr(&config.grpc_base_node_address)?;
let grpc_console_wallet_address = multiaddr_to_socketaddr(&config.grpc_console_wallet_address)?;
Ok(Self {
network: config.network,
monerod_url: config.monerod_url,
monerod_username: config.monerod_username,
monerod_password: config.monerod_password,
monerod_use_auth: config.monerod_use_auth,
grpc_base_node_address: config.grpc_base_node_address,
grpc_console_wallet_address: config.grpc_console_wallet_address,
grpc_base_node_address,
grpc_console_wallet_address,
proxy_host_address: config.proxy_host_address,
proxy_submit_to_origin: config.proxy_submit_to_origin,
wait_for_initial_sync_at_startup: config.wait_for_initial_sync_at_startup,
}
})
}
}

Expand Down
1 change: 1 addition & 0 deletions applications/tari_mining_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2018"
[dependencies]
tari_core = { path = "../../base_layer/core", default-features = false }
tari_common = { path = "../../common" }
tari_comms = { path = "../../comms" }
tari_app_utilities = { path = "../tari_app_utilities"}
tari_app_grpc = { path = "../tari_app_grpc" }
tari_crypto = { git = "https://github.com/tari-project/tari-crypto.git", branch = "main" }
Expand Down
19 changes: 7 additions & 12 deletions applications/tari_mining_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ use serde::{Deserialize, Serialize};
use std::time::Duration;
use tari_app_grpc::tari_rpc::{pow_algo::PowAlgos, NewBlockTemplateRequest, PowAlgo};
use tari_common::{GlobalConfig, NetworkConfigPath};
use tari_comms::utils::multiaddr::multiaddr_to_socketaddr;

#[derive(Serialize, Deserialize, Debug)]
pub struct MinerConfig {
pub base_node_grpc_address: Option<String>,
pub wallet_grpc_address: Option<String>,
pub num_mining_threads: usize,
pub mine_on_tip_only: bool,
pub proof_of_work_algo: ProofOfWork,
Expand All @@ -68,8 +67,6 @@ impl NetworkConfigPath for MinerConfig {
impl Default for MinerConfig {
fn default() -> Self {
Self {
base_node_grpc_address: None,
wallet_grpc_address: None,
num_mining_threads: num_cpus::get(),
mine_on_tip_only: true,
proof_of_work_algo: ProofOfWork::Sha3,
Expand All @@ -82,16 +79,14 @@ impl Default for MinerConfig {
}

impl MinerConfig {
pub fn base_node_addr(&self, global: &GlobalConfig) -> String {
self.base_node_grpc_address
.clone()
.unwrap_or_else(|| format!("http://{}", global.grpc_base_node_address))
pub fn base_node_addr(&self, global: &GlobalConfig) -> Result<String, std::io::Error> {
let socket = multiaddr_to_socketaddr(&global.grpc_base_node_address)?;
Ok(format!("http://{}", socket))
}

pub fn wallet_addr(&self, global: &GlobalConfig) -> String {
self.wallet_grpc_address
.clone()
.unwrap_or_else(|| format!("http://{}", global.grpc_console_wallet_address))
pub fn wallet_addr(&self, global: &GlobalConfig) -> Result<String, std::io::Error> {
let socket = multiaddr_to_socketaddr(&global.grpc_console_wallet_address)?;
Ok(format!("http://{}", socket))
}

pub fn pow_algo_request(&self) -> NewBlockTemplateRequest {
Expand Down
2 changes: 2 additions & 0 deletions applications/tari_mining_node/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use thiserror::Error;

#[derive(Debug, Error)]
pub enum MinerError {
#[error("I/O error")]
IOError(#[from] std::io::Error),
#[error("GRPC error: {0}")]
GrpcStatus(#[from] tonic::Status),
#[error("Connection error: {0}")]
Expand Down
8 changes: 4 additions & 4 deletions applications/tari_mining_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ async fn connect(
config: &MinerConfig,
global: &GlobalConfig,
) -> Result<(BaseNodeClient<Channel>, WalletClient<Channel>), MinerError> {
let base_node_addr = config.base_node_addr(global);
let base_node_addr = config.base_node_addr(global)?;
info!(target: LOG_TARGET, "Connecting to base node at {}", base_node_addr);
let node_conn = BaseNodeClient::connect(base_node_addr.clone()).await?;
let wallet_addr = config.wallet_addr(global);
let node_conn = BaseNodeClient::connect(base_node_addr).await?;
let wallet_addr = config.wallet_addr(global)?;
info!(target: LOG_TARGET, "Connecting to wallet at {}", wallet_addr);
let wallet_conn = WalletClient::connect(wallet_addr.clone()).await?;
let wallet_conn = WalletClient::connect(wallet_addr).await?;

Ok((node_conn, wallet_conn))
}
Expand Down
1 change: 1 addition & 0 deletions applications/tari_stratum_transcoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default = []
envlog = ["env_logger"]

[dependencies]
tari_comms = { path = "../../comms" }
tari_app_grpc = { path = "../tari_app_grpc" }
tari_common = { path = "../../common" }
tari_core = { path = "../../base_layer/core", default-features = false, features = ["transactions"] }
Expand Down
4 changes: 2 additions & 2 deletions applications/tari_stratum_transcoder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::error::StratumTranscoderProxyError;
use futures::future;
use hyper::{service::make_service_fn, Server};
use proxy::{StratumTranscoderProxyConfig, StratumTranscoderProxyService};
use std::convert::Infallible;
use std::convert::{Infallible, TryFrom};
use structopt::StructOpt;
use tari_app_grpc::tari_rpc as grpc;
use tari_common::{configuration::bootstrap::ApplicationType, ConfigBootstrap, GlobalConfig};
Expand All @@ -45,7 +45,7 @@ use tokio::time::Duration;
async fn main() -> Result<(), StratumTranscoderProxyError> {
let config = initialize()?;

let config = StratumTranscoderProxyConfig::from(config);
let config = StratumTranscoderProxyConfig::try_from(config)?;
let addr = config.transcoder_host_address;
let client = reqwest::Client::builder()
.connect_timeout(Duration::from_secs(5))
Expand Down
17 changes: 11 additions & 6 deletions applications/tari_stratum_transcoder/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use std::{
};
use tari_app_grpc::{tari_rpc as grpc, tari_rpc::GetCoinbaseRequest};
use tari_common::{configuration::Network, GlobalConfig};
use tari_comms::utils::multiaddr::multiaddr_to_socketaddr;
use tari_core::blocks::{Block, NewBlockTemplate};
use tari_utilities::{hex::Hex, message_format::MessageFormat};
use tracing::{debug, error};
Expand All @@ -58,14 +59,18 @@ pub struct StratumTranscoderProxyConfig {
pub transcoder_host_address: SocketAddr,
}

impl From<GlobalConfig> for StratumTranscoderProxyConfig {
fn from(config: GlobalConfig) -> Self {
Self {
impl TryFrom<GlobalConfig> for StratumTranscoderProxyConfig {
type Error = std::io::Error;

fn try_from(config: GlobalConfig) -> Result<Self, Self::Error> {
let grpc_base_node_address = multiaddr_to_socketaddr(&config.grpc_base_node_address)?;
let grpc_console_wallet_address = multiaddr_to_socketaddr(&config.grpc_console_wallet_address)?;
Ok(Self {
network: config.network,
grpc_base_node_address: config.grpc_base_node_address,
grpc_console_wallet_address: config.grpc_console_wallet_address,
grpc_base_node_address,
grpc_console_wallet_address,
transcoder_host_address: config.transcoder_host_address,
}
})
}
}

Expand Down
Loading

0 comments on commit 9f8e289

Please sign in to comment.