Skip to content

Commit

Permalink
multi: turn CLI rpc addr param into URL (#34)
Browse files Browse the repository at this point in the history
* app: rename gRPC address -> host

* multi: turn CLI rpc addr param into URL

This is required for us to be able to connect to remotely running
Thunder instances.

* wallet: more descriptive NoSeed error message
  • Loading branch information
torkelrogstad authored Feb 7, 2025
1 parent 50b4e07 commit 08bcceb
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ impl App {

tracing::info!(
"Connecting to mainchain at {}",
config.mainchain_grpc_address
config.mainchain_grpc_url
);
let rt_guard = runtime.enter();
let transport = tonic::transport::channel::Channel::from_shared(
format!("{}", config.mainchain_grpc_address),
format!("{}", config.mainchain_grpc_url),
)
.unwrap()
.concurrency_limit(256)
Expand All @@ -212,7 +212,7 @@ impl App {
.block_on(Self::check_proto_support(transport.clone()))
.map_err(|err| {
Error::VerifyMainchainServices(
config.mainchain_grpc_address.clone(),
config.mainchain_grpc_url.clone(),
err,
)
})? {
Expand Down
8 changes: 4 additions & 4 deletions app/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ pub(super) struct Cli {
#[arg(default_value_t = tracing::Level::DEBUG, long)]
log_level: tracing::Level,

/// Address (protocol + host + port) to connect to mainchain node gRPC server
/// Connect to mainchain node gRPC server running at this URL
#[arg(default_value = "http://localhost:50051", long)]
mainchain_grpc_address: url::Url,
mainchain_grpc_url: url::Url,

/// Path to a mnemonic seed phrase
#[arg(long)]
Expand All @@ -141,7 +141,7 @@ pub struct Config {
pub log_dir: Option<PathBuf>,
pub log_level: tracing::Level,
pub log_level_file: tracing::Level, // Level for logs that get written to file
pub mainchain_grpc_address: url::Url,
pub mainchain_grpc_url: url::Url,
pub mnemonic_seed_phrase_path: Option<PathBuf>,
pub net_addr: SocketAddr,
pub rpc_addr: SocketAddr,
Expand Down Expand Up @@ -171,7 +171,7 @@ impl Cli {
log_dir,
log_level: self.log_level,
log_level_file: self.log_level_file,
mainchain_grpc_address: self.mainchain_grpc_address,
mainchain_grpc_url: self.mainchain_grpc_url,
mnemonic_seed_phrase_path: self.mnemonic_seed_phrase_path,
net_addr: self.net_addr,
rpc_addr: self.rpc_addr,
Expand Down
15 changes: 6 additions & 9 deletions app/gui/console_logs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{
net::SocketAddr,
sync::{
atomic::{self, AtomicBool},
Arc,
},
use std::sync::{
atomic::{self, AtomicBool},
Arc,
};

use clap::Parser;
Expand Down Expand Up @@ -32,12 +29,12 @@ pub struct ConsoleCommand {
pub struct ConsoleLogs {
line_buffer: LineBuffer,
command_input: String,
rpc_addr: SocketAddr,
rpc_addr: url::Url,
running_command: Arc<AtomicBool>,
}

impl ConsoleLogs {
pub fn new(line_buffer: LineBuffer, rpc_addr: SocketAddr) -> Self {
pub fn new(line_buffer: LineBuffer, rpc_addr: url::Url) -> Self {
Self {
line_buffer,
command_input: String::new(),
Expand Down Expand Up @@ -71,7 +68,7 @@ impl ConsoleLogs {
}
};
let cli = thunder_app_cli_lib::Cli {
rpc_addr: self.rpc_addr,
rpc_url: self.rpc_addr.clone(),
timeout: None,
command,
};
Expand Down
4 changes: 2 additions & 2 deletions app/gui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{net::SocketAddr, task::Poll};
use std::task::Poll;

use eframe::egui::{self, RichText};
use strum::{EnumIter, IntoEnumIterator};
Expand Down Expand Up @@ -189,7 +189,7 @@ impl EguiApp {
app: Option<App>,
cc: &eframe::CreationContext<'_>,
logs_capture: LineBuffer,
rpc_addr: SocketAddr,
rpc_addr: url::Url,
) -> Self {
// Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals.
// Restore app state using cc.storage (requires the "persistence" feature).
Expand Down
4 changes: 3 additions & 1 deletion app/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ fn run_egui_app(
app: Result<crate::app::App, crate::app::Error>,
) -> Result<(), eframe::Error> {
let native_options = eframe::NativeOptions::default();
let rpc_addr = url::Url::parse(&format!("http://{}", config.rpc_addr))
.expect("failed to parse rpc addr");
eframe::run_native(
"Thunder",
native_options,
Expand All @@ -155,7 +157,7 @@ fn run_egui_app(
app.ok(),
cc,
line_buffer,
config.rpc_addr,
rpc_addr,
)))
}),
)
Expand Down
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde_json = { workspace = true }
thunder = { path = "../lib" }
thunder_app_rpc_api = { path = "../rpc-api" }
tokio = { workspace = true }
url = "2.5.4"
utoipa = { workspace = true }

[lints]
Expand All @@ -26,4 +27,4 @@ path = "lib.rs"

[[bin]]
name = "thunder_app_cli"
path = "main.rs"
path = "main.rs"
20 changes: 6 additions & 14 deletions cli/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
time::Duration,
};
use std::{net::SocketAddr, time::Duration};

use clap::{Parser, Subcommand};
use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use thunder::types::{Address, Txid, THIS_SIDECHAIN};
use thunder::types::{Address, Txid};
use thunder_app_rpc_api::RpcClient;

#[derive(Clone, Debug, Subcommand)]
Expand Down Expand Up @@ -87,17 +84,12 @@ pub enum Command {
},
}

const DEFAULT_RPC_ADDR: SocketAddr = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
6000 + THIS_SIDECHAIN as u16,
);

#[derive(Clone, Debug, Parser)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// address for use by the RPC server
#[arg(default_value_t = DEFAULT_RPC_ADDR, long)]
pub rpc_addr: SocketAddr,
/// Base URL used for requests to the RPC server.
#[arg(default_value = "http://localhost:6009", long)]
pub rpc_url: url::Url,

#[arg(long, help = "Timeout for RPC requests in seconds (default: 60)")]
pub timeout: Option<u64>,
Expand All @@ -113,7 +105,7 @@ impl Cli {
.request_timeout(Duration::from_secs(
self.timeout.unwrap_or(DEFAULT_TIMEOUT),
))
.build(format!("http://{}", self.rpc_addr))?;
.build(self.rpc_url.clone())?;
let res = match self.command {
Command::Balance => {
let balance = rpc_client.balance().await?;
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ThunderApp {
"--datadir".to_owned(),
self.data_dir.display().to_string(),
"--headless".to_owned(),
"--mainchain-grpc-address".to_owned(),
"--mainchain-grpc-url".to_owned(),
format!("http://127.0.0.1:{}", self.mainchain_grpc_port),
"--net-addr".to_owned(),
format!("127.0.0.1:{}", self.net_port),
Expand Down
6 changes: 4 additions & 2 deletions lib/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ pub enum Error {
Io(#[from] std::io::Error),
#[error("no index for address {address}")]
NoIndex { address: Address },
#[error("wallet doesn't have a seed")]
#[error(
"wallet does not have a seed (set with RPC `set-seed-from-mnemonic`)"
)]
NoSeed,
#[error("not enough funds")]
NotEnoughFunds,
#[error("utxo doesn't exist")]
#[error("utxo does not exist")]
NoUtxo,
#[error("failed to parse mnemonic seed phrase")]
ParseMnemonic(#[source] bip39::ErrorKind),
Expand Down

0 comments on commit 08bcceb

Please sign in to comment.