Global config instance (#769)

* config: global instance

* fix: cli tests

* fix: owner api tests

* config: save tor config

* fix: owner rpc tests

* fix: set config file path on init and owner api call

* tor: store launched services, get global config at command loop, remove unused listen args, check for empty global config to set default, do not save empty tor config at owner

* api: update config in memory after successful file write

* arti: restart proxy in loop

* fix: comment for tor config save

* config: set or init, update without error

* config: scoped tor config

* build: update lock file

* config: ability to store multiple configurations at memory from config path

* tor: restart service on config change

* fix: owner api test

* foreign: pass tor config from args, stop service, error handling

* arti: move runtime wrapper to single file

* config: read existing config from file at create_config

* config: handle error on read

* arti: single dir for cache

* foreign: register config listener before launch

* config: save temp

* config: remove unused error type

* config: make path and members not optional, return an error on config creation if it exists, pass default tor config to send if not set

* arti: non public runtime wrapper

* config: migrate comments

* fix: rpc tests

* config: return from cache if exists

* config: save disabled tor config when null provided at owner and tor section was not set

* config: update tor with args at listener

* config: pass pathbuf directly on creation

* config: unique tmp file name, setting permissions and providing config version

* fix global config handling

* fix config persistence

* fix tor service restart

* fix config synchronization

* require wallet close before changing data directory

* unify global config updates

---------

Co-authored-by: wiesche <wiesche89@googlemail.com>
This commit is contained in:
ardocrat
2026-07-24 22:15:19 +02:00
committed by GitHub
co-authored by wiesche
parent 7fb47646e9
commit d2fe2b90f6
46 changed files with 4880 additions and 3311 deletions
+19 -27
View File
@@ -31,7 +31,7 @@ use std::env;
use std::path::PathBuf;
use std::path::MAIN_SEPARATOR;
// include build information
/// Include build information
pub mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
@@ -55,7 +55,7 @@ pub fn info_strings() -> (String, String) {
)
}
// Helper fuction to format paths according to OS, avoids bugs on Linux
/// Helper function to format paths according to OS, avoids bugs on Linux
pub fn fmt_path(path: String) -> String {
let sep = &MAIN_SEPARATOR.to_string();
let path = path.replace("/", &sep).replace("\\", &sep);
@@ -117,22 +117,24 @@ fn real_main() -> i32 {
// Load relevant config, try and load a wallet config file
// Use defaults for configuration if config file not found anywhere
let mut config = match config::initial_setup_wallet(&chain_type, current_dir, create_path) {
let config = match config::initial_setup_wallet(&chain_type, current_dir, create_path) {
Ok(c) => c,
Err(e) => match e {
ConfigError::PathNotFoundError(m) => {
println!("Wallet configuration not found at {}. (Run `grin-wallet init` to create a new wallet)", m);
return 0;
Err(e) => {
return match e {
ConfigError::PathNotFoundError(m) => {
println!("Wallet configuration not found at {}. (Run `grin-wallet init` to create a new wallet)", m);
0
}
m => {
println!("Unable to load wallet configuration: {} (Run `grin-wallet init` to create a new wallet)", m);
0
}
}
m => {
println!("Unable to load wallet configuration: {} (Run `grin-wallet init` to create a new wallet)", m);
return 0;
}
},
}
};
// Load logging config
let mut l = config.members.as_mut().unwrap().logging.clone().unwrap();
let mut l = config.members.logging.clone().unwrap();
// no logging to stdout if we're running cli
match args.subcommand() {
("cli", _) => l.log_to_stdout = true,
@@ -141,24 +143,14 @@ fn real_main() -> i32 {
init_logger(Some(l), None);
info!(
"Using wallet configuration file at {}",
config.config_file_path.as_ref().unwrap().to_str().unwrap()
config.config_file_path.to_str().unwrap()
);
log_build_info();
global::init_global_chain_type(
config
.members
.as_ref()
.unwrap()
.wallet
.chain_type
.as_ref()
.unwrap()
.clone(),
);
global::init_global_chain_type(config.members.wallet.chain_type.as_ref().unwrap().clone());
global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base());
let wallet_config = config.clone().members.unwrap().wallet;
global::init_global_accept_fee_base(config.members.wallet.accept_fee_base());
let wallet_config = config.clone().members.wallet;
let timeout = wallet_config.api_request_timeout();
let node_client =
HTTPNodeClient::new(&wallet_config.check_node_api_http_addr, None, timeout).unwrap();
+9 -5
View File
@@ -19,7 +19,8 @@ use clap::App;
//use colored::Colorize;
use grin_keychain as keychain;
use grin_wallet_api::Owner;
use grin_wallet_config::{TorConfig, WalletConfig};
use grin_wallet_config::config::get_global_config;
use grin_wallet_config::GlobalWalletConfig;
use grin_wallet_controller::command::GlobalArgs;
use grin_wallet_controller::Error;
use grin_wallet_impls::DefaultWalletImpl;
@@ -107,10 +108,9 @@ pub fn start_updater_thread(rx: Receiver<StatusMessage>) -> Result<(), Error> {
}
pub fn command_loop<L, C, K>(
config: GlobalWalletConfig,
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'static, L, C, K>>>>,
keychain_mask: Option<SecretKey>,
wallet_config: &WalletConfig,
tor_config: &TorConfig,
global_wallet_args: &GlobalArgs,
test_mode: bool,
) -> Result<(), Error>
@@ -151,7 +151,7 @@ where
// catch updater messages
let (tx, rx) = channel();
let mut owner_api = Owner::new(wallet_inst, Some(tx));
let mut owner_api = Owner::new(wallet_inst, Some(tx), config.config_file_path.clone());
start_updater_thread(rx)?;
// start the automatic updater
@@ -218,11 +218,15 @@ where
}
_ => keychain_mask,
};
let config = get_global_config(&config.config_file_path)
.map_err(|e| Error::GenericError(e.to_string()))?;
let tor_config = config.tor_config();
let wallet_config = config.members.wallet;
match wallet_args::parse_and_execute(
&mut owner_api,
keychain_mask.clone(),
&wallet_config,
&tor_config,
tor_config,
&global_wallet_args,
&args,
test_mode,
+2 -14
View File
@@ -30,13 +30,8 @@ pub fn wallet_command<C>(
where
C: NodeClient + 'static,
{
// just get defaults from the global config
let wallet_config = config.members.clone().unwrap().wallet;
let tor_config = config.members.unwrap().tor;
// Check the node version info, and exit with report if we're not compatible
let global_wallet_args = wallet_args::parse_global_args(&wallet_config, &wallet_args)
let global_wallet_args = wallet_args::parse_global_args(&config.members.wallet, &wallet_args)
.expect("Can't read configuration file");
node_client.set_node_api_secret(global_wallet_args.node_api_secret.clone());
@@ -63,14 +58,7 @@ where
}
// ... if node isn't available, allow offline functions
let res = wallet_args::wallet_command(
wallet_args,
wallet_config,
tor_config,
node_client,
false,
|_| {},
);
let res = wallet_args::wallet_command(wallet_args, config, node_client, false, |_| {});
// we need to give log output a chance to catch up before exiting
thread::sleep(Duration::from_millis(100));
+28 -49
View File
@@ -25,7 +25,7 @@ use grin_core as core;
use grin_core::core::amount_to_hr_string;
use grin_keychain as keychain;
use grin_wallet_api::Owner;
use grin_wallet_config::{TorConfig, WalletConfig};
use grin_wallet_config::{GlobalWalletConfig, TorConfig, WalletConfig};
use grin_wallet_controller::{command, Error};
use grin_wallet_impls::{DefaultLCProvider, DefaultWalletImpl};
use grin_wallet_libwallet::{self, Slate, SlatepackAddress, SlatepackArmor};
@@ -395,23 +395,6 @@ where
Ok(command::RecoverArgs { passphrase })
}
pub fn parse_listen_args(
config: &mut WalletConfig,
tor_config: &mut TorConfig,
args: &ArgMatches,
) -> Result<command::ListenArgs, ParseError> {
if let Some(port) = args.value_of("port") {
config.api_listen_port = port.parse().unwrap();
}
if let Some(bridge) = args.value_of("bridge") {
tor_config.bridge.bridge_line = Some(bridge.into());
}
if args.is_present("no_tor") {
tor_config.use_tor_listener = false;
}
Ok(command::ListenArgs {})
}
pub fn parse_owner_api_args(
config: &mut WalletConfig,
args: &ArgMatches,
@@ -996,8 +979,7 @@ pub fn parse_verify_proof_args(args: &ArgMatches) -> Result<command::ProofVerify
pub fn wallet_command<C, F>(
wallet_args: &ArgMatches,
mut wallet_config: WalletConfig,
tor_config: Option<TorConfig>,
config: GlobalWalletConfig,
mut node_client: C,
test_mode: bool,
wallet_inst_cb: F,
@@ -1019,6 +1001,7 @@ where
>,
),
{
let mut wallet_config = config.members.wallet.clone();
if let Some(dir) = wallet_args.value_of("top_level_dir") {
wallet_config.data_file_dir = dir.to_string().clone();
}
@@ -1041,13 +1024,6 @@ where
wallet_config.data_file_dir = top_level_wallet_dir.to_str().unwrap().into();
}
// for backwards compatibility: If tor config doesn't exist in the file, assume
// the top level directory for data
let tor_config = tor_config.unwrap_or_else(|| TorConfig {
send_config_dir: wallet_config.data_file_dir.clone(),
..Default::default()
});
// Instantiate wallet (doesn't open the wallet)
let wallet =
inst_wallet::<DefaultLCProvider<C, keychain::ExtKeychain>, C, keychain::ExtKeychain>(
@@ -1105,20 +1081,20 @@ where
let res = match wallet_args.subcommand() {
("cli", Some(_)) => command_loop(
config,
wallet,
keychain_mask,
&wallet_config,
&tor_config,
&global_wallet_args,
test_mode,
),
_ => {
let mut owner_api = Owner::new(wallet, None);
let tor_config = config.tor_config();
let mut owner_api = Owner::new(wallet, None, config.config_file_path);
parse_and_execute(
&mut owner_api,
keychain_mask,
&wallet_config,
&tor_config,
tor_config,
&global_wallet_args,
&wallet_args,
test_mode,
@@ -1138,7 +1114,7 @@ pub fn parse_and_execute<L, C, K>(
owner_api: &mut Owner<L, C, K>,
keychain_mask: Option<SecretKey>,
wallet_config: &WalletConfig,
tor_config: &TorConfig,
tor_config: TorConfig,
global_wallet_args: &command::GlobalArgs,
wallet_args: &ArgMatches,
test_mode: bool,
@@ -1174,14 +1150,25 @@ where
}
("listen", Some(args)) => {
let mut c = wallet_config.clone();
let mut t = tor_config.clone();
let a = arg_parse!(parse_listen_args(&mut c, &mut t, &args));
if let Some(port) = args.value_of("port") {
c.api_listen_port = port.parse().unwrap();
}
let bridge = if let Some(bridge) = args.value_of("bridge") {
Some(bridge.into())
} else {
None
};
let use_tor = if args.is_present("no_tor") {
Some(false)
} else {
None
};
command::listen(
owner_api,
Arc::new(Mutex::new(keychain_mask)),
&c,
&t,
&a,
c,
bridge,
use_tor,
&global_wallet_args.clone(),
cli_mode,
test_mode,
@@ -1192,13 +1179,12 @@ where
let mut g = global_wallet_args.clone();
g.tls_conf = None;
arg_parse!(parse_owner_api_args(&mut c, &args));
command::owner_api(owner_api, keychain_mask, &c, &tor_config, &g, test_mode)
command::owner_api(owner_api, keychain_mask, &c, &g, test_mode)
}
("web", Some(_)) => command::owner_api(
owner_api,
keychain_mask,
wallet_config,
tor_config,
global_wallet_args,
test_mode,
),
@@ -1220,22 +1206,15 @@ where
command::send(
owner_api,
km,
Some(tor_config.clone()),
a,
tor_config,
wallet_config.dark_background_color_scheme.unwrap_or(true),
test_mode,
)
}
("receive", Some(args)) => {
let a = arg_parse!(parse_receive_args(&args));
command::receive(
owner_api,
km,
&global_wallet_args,
a,
Some(tor_config.clone()),
test_mode,
)
command::receive(owner_api, km, &global_wallet_args, a, tor_config, test_mode)
}
("unpack", Some(args)) => {
let a = arg_parse!(parse_unpack_args(&args));
@@ -1259,7 +1238,7 @@ where
command::process_invoice(
owner_api,
km,
Some(tor_config.clone()),
tor_config,
a,
wallet_config.dark_background_color_scheme.unwrap_or(true),
test_mode,