Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub struct Config {
pub network: thunder::types::Network,
pub network_magic_override: Option<thunder::net::peer_message::MagicBytes>,
pub server_names: HashSet<String>,
pub wallet_dir: PathBuf,
}

#[derive(Clone)]
Expand Down Expand Up @@ -237,12 +238,7 @@ impl App {
.enable_all()
.build()?;

tracing::info!(
"Instantiating wallet with data directory: {}",
config.datadir.display()
);

let wallet = Wallet::new(&config.datadir.join("wallet.mdb"))?;
let wallet = Wallet::new(&config.wallet_dir.join("wallet.mdb"))?;
if let Some(seed_phrase_path) = &config.mnemonic_seed_phrase_path {
let mnemonic = std::fs::read_to_string(seed_phrase_path)?;
let () = wallet.set_seed_from_mnemonic(mnemonic.as_str())?;
Expand Down
107 changes: 84 additions & 23 deletions app/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,80 @@ use thunder::types::{Network, THIS_SIDECHAIN};

use crate::util::saturating_pred_level;

#[derive(Clone, Debug)]
pub struct Config {
pub add_peers: HashSet<thunder::types::net::PeerAddress>,
pub datadir: PathBuf,
pub headless: bool,
/// If None, logging to file should be disabled.
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_url: url::Url,
pub mnemonic_seed_phrase_path: Option<PathBuf>,
pub net_addr: SocketAddr,
pub network: Network,
pub network_magic_override: Option<thunder::net::peer_message::MagicBytes>,
pub private_rpc_addr: SocketAddr,
pub rpc_addr: SocketAddr,
pub server_names: HashSet<String>,
pub wallet_dir: PathBuf,
}

impl Config {
/// Log all fields at info level
#[track_caller]
pub fn log_all_fields(&self, msg: &str) {
let Self {
add_peers,
datadir,
headless,
log_dir,
log_level,
log_level_file,
mainchain_grpc_url,
mnemonic_seed_phrase_path,
net_addr,
network,
network_magic_override,
private_rpc_addr,
rpc_addr,
server_names,
wallet_dir,
} = self;
let add_peers = std::fmt::from_fn(|f| {
f.debug_set()
.entries(add_peers.iter().map(|peer_addr| {
std::fmt::from_fn(|f| std::fmt::Display::fmt(peer_addr, f))
}))
.finish()
});
tracing::info!(
%add_peers,
datadir = %datadir.display(),
%headless,
log_dir = log_dir.as_ref().map(|path|
tracing::field::display(path.display())
),
%log_level,
%log_level_file,
%mainchain_grpc_url,
mnemonic_seed_phrase_path = mnemonic_seed_phrase_path.as_ref()
.map(|path| tracing::field::display(path.display())),
%net_addr,
%network,
network_magic_override = network_magic_override.map(|magic|
tracing::field::display(const_hex::encode(magic))
),
%private_rpc_addr,
%rpc_addr,
?server_names,
wallet_dir = %wallet_dir.display(),
msg,
)
}
}

const fn ipv4_socket_addr(ipv4_octets: [u8; 4], port: u16) -> SocketAddr {
let [a, b, c, d] = ipv4_octets;
let ipv4 = Ipv4Addr::new(a, b, c, d);
Expand Down Expand Up @@ -109,7 +183,8 @@ pub(super) struct Cli {
/// seed peers.
#[arg(long = "add-peer")]
add_peers: Vec<thunder::types::net::PeerAddress>,
/// Data directory for storing blockchain and wallet data
/// Data directory for storing blockchain data.
/// Wallet data is stored here by default.
#[command(flatten)]
datadir: DatadirArg,
/// If specified, the gui will not launch.
Expand Down Expand Up @@ -157,35 +232,19 @@ pub(super) struct Cli {
/// This option can be specified multiple times.
#[arg(long = "server-name")]
server_names: Vec<String>,
}

#[derive(Clone, Debug)]
pub struct Config {
pub add_peers: HashSet<thunder::types::net::PeerAddress>,
pub datadir: PathBuf,
pub headless: bool,
/// If None, logging to file should be disabled.
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_url: url::Url,
pub mnemonic_seed_phrase_path: Option<PathBuf>,
pub net_addr: SocketAddr,
pub network: Network,
pub network_magic_override: Option<thunder::net::peer_message::MagicBytes>,
pub private_rpc_addr: SocketAddr,
pub rpc_addr: SocketAddr,
pub server_names: HashSet<String>,
/// Data directory for storing wallet data
#[arg(long)]
wallet_dir: Option<PathBuf>,
}

impl Cli {
pub fn get_config(self) -> anyhow::Result<Config> {
let datadir = self.datadir.0;
let log_dir = match self.log_dir {
None => {
let version_dir_name =
format!("v{}", env!("CARGO_PKG_VERSION"));
let log_dir =
self.datadir.0.join("logs").join(version_dir_name);
let log_dir = datadir.join("logs").join(version_dir_name);
Some(log_dir)
}
Some(log_dir) => {
Expand All @@ -201,9 +260,10 @@ impl Cli {
} else {
saturating_pred_level(self.log_level)
};
let wallet_dir = self.wallet_dir.unwrap_or_else(|| datadir.clone());
Ok(Config {
add_peers: HashSet::from_iter(self.add_peers),
datadir: self.datadir.0,
datadir,
headless: self.headless,
log_dir,
log_level,
Expand All @@ -216,6 +276,7 @@ impl Cli {
private_rpc_addr: self.private_rpc_addr,
rpc_addr: self.rpc_addr,
server_names: HashSet::from_iter(self.server_names),
wallet_dir,
})
}
}
2 changes: 2 additions & 0 deletions app/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ fn main() -> anyhow::Result<()> {
config.log_level,
config.log_level_file,
)?;
let () = config.log_all_fields("Loaded config");

let (app_tx, app_rx) = oneshot::channel::<anyhow::Error>();

Expand All @@ -227,6 +228,7 @@ fn main() -> anyhow::Result<()> {
network: config.network,
network_magic_override: config.network_magic_override,
server_names: config.server_names,
wallet_dir: config.wallet_dir,
};
let app = app::App::new(app_config).inspect(|app| {
// spawn rpc server
Expand Down
Loading