Skip to content

Commit 7ceade4

Browse files
authored
Fix for many wallet config errors (#731)
* Added an extra warning message when failing to reach the node, work in progress on changing default dir for node api ssecret 'foreign_api_secret' * In the middle of debugging hell, default overwrite any updates to the config made earlier, its never written to a file when running init * More debugging, next trace the overwrite of the config * Added get get_node_path function, seperating wallet dir and node_dir -compiling but some warnings * Tested wallet config creation fix for Issues: #728 #3394 Pull 3420, partial fix for 3002 * Removed some debugging code * Fixed passing config to create_config function, some more test needed before pulling to main * All test succeed: a) normal, b) top-dir, c) here, d) with default config template in working dir * Cleaning up * Fixed a single test for config that needed adjusting
1 parent 3b0cc24 commit 7ceade4

File tree

7 files changed

+109
-43
lines changed

7 files changed

+109
-43
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/src/config.rs

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
//! Configuration file management
1616
17-
use dirs;
1817
use rand::distributions::{Alphanumeric, Distribution};
1918
use rand::thread_rng;
2019
use std::env;
@@ -35,15 +34,17 @@ use crate::util::logger::LoggingConfig;
3534
/// Wallet configuration file name
3635
pub const WALLET_CONFIG_FILE_NAME: &str = "grin-wallet.toml";
3736
const WALLET_LOG_FILE_NAME: &str = "grin-wallet.log";
38-
const GRIN_HOME: &str = ".grin";
37+
/// .grin folder, usually in home/.grin
38+
pub const GRIN_HOME: &str = ".grin";
3939
/// Wallet data directory
4040
pub const GRIN_WALLET_DIR: &str = "wallet_data";
4141
/// Node API secret
4242
pub const API_SECRET_FILE_NAME: &str = ".foreign_api_secret";
4343
/// Owner API secret
4444
pub const OWNER_API_SECRET_FILE_NAME: &str = ".owner_api_secret";
4545

46-
fn get_grin_path(
46+
/// Function to get wallet dir and create dirs if not existing
47+
pub fn get_wallet_path(
4748
chain_type: &global::ChainTypes,
4849
create_path: bool,
4950
) -> Result<PathBuf, ConfigError> {
@@ -68,6 +69,49 @@ fn get_grin_path(
6869
}
6970
}
7071

72+
/// Smart function to find the most likely .api_secret location for the node
73+
pub fn get_node_path(
74+
data_path: Option<PathBuf>,
75+
chain_type: &global::ChainTypes,
76+
) -> Result<PathBuf, ConfigError> {
77+
let node_path = match data_path {
78+
// 1) A If top dir provided and api_secret exist, return top dir
79+
Some(path) => {
80+
let mut node_path = path;
81+
node_path.push(GRIN_HOME);
82+
node_path.push(chain_type.shortname());
83+
node_path.push(API_SECRET_FILE_NAME);
84+
if node_path.exists() {
85+
node_path.pop();
86+
Ok(node_path)
87+
88+
// 1) B If top dir exists, but no config there, return home dir
89+
} else {
90+
let mut node_path = match dirs::home_dir() {
91+
Some(p) => p,
92+
None => PathBuf::new(),
93+
};
94+
node_path.push(GRIN_HOME);
95+
node_path.push(chain_type.shortname());
96+
Ok(node_path)
97+
}
98+
}
99+
100+
// 2) If there is no top_dir provided, always return home dir
101+
None => {
102+
let mut node_path = match dirs::home_dir() {
103+
Some(p) => p,
104+
None => PathBuf::new(),
105+
};
106+
node_path.push(GRIN_HOME);
107+
node_path.push(chain_type.shortname());
108+
Ok(node_path)
109+
}
110+
};
111+
node_path
112+
}
113+
114+
/// Checks if config in current working dir
71115
fn check_config_current_dir(path: &str) -> Option<PathBuf> {
72116
let p = env::current_dir();
73117
let mut c = match p {
@@ -122,7 +166,7 @@ fn check_api_secret_file(
122166
) -> Result<(), ConfigError> {
123167
let grin_path = match data_path {
124168
Some(p) => p,
125-
None => get_grin_path(chain_type, false)?,
169+
None => get_node_path(data_path, chain_type)?,
126170
};
127171
let mut api_secret_path = grin_path;
128172
api_secret_path.push(file_name);
@@ -134,6 +178,7 @@ fn check_api_secret_file(
134178
}
135179

136180
/// Handles setup and detection of paths for wallet
181+
/// Use config file in a) current dir as template, b) in top path, or c) .grin home
137182
pub fn initial_setup_wallet(
138183
chain_type: &global::ChainTypes,
139184
data_path: Option<PathBuf>,
@@ -144,33 +189,45 @@ pub fn initial_setup_wallet(
144189
fs::create_dir_all(p)?;
145190
}
146191
}
147-
// Use config file if current directory if it exists, .grin home otherwise
192+
193+
// Get wallet data_dir path, create it if it does not exist
194+
let wallet_path = match data_path {
195+
Some(p) => {
196+
let mut abs_wallet_path = std::env::current_dir()?;
197+
abs_wallet_path.push(p);
198+
abs_wallet_path
199+
}
200+
None => get_wallet_path(chain_type, create_path)?,
201+
};
202+
203+
// Get path to the node dir(s), first try top dir, if no node api_secret return home./grin
204+
let node_path = get_node_path(Some(wallet_path.clone()), chain_type)?;
205+
206+
// Get path to the newwly to be created config file
207+
let mut config_path = wallet_path.clone();
208+
config_path.push(WALLET_CONFIG_FILE_NAME);
209+
210+
// Check if config exists in working dir, if so, use it as template for newly created config
148211
let (path, config) = if let Some(p) = check_config_current_dir(WALLET_CONFIG_FILE_NAME) {
149212
let mut path = p.clone();
150213
path.pop();
151-
(path, GlobalWalletConfig::new(p.to_str().unwrap())?)
214+
let mut config = GlobalWalletConfig::new(p.to_str().unwrap())?;
215+
// Use template config, update data_dir, network and api secrets
216+
config.config_file_path = Some(config_path);
217+
config.update_paths(&wallet_path, &node_path);
218+
(path, config)
152219
} else {
153-
// Check if grin dir exists
154-
let grin_path = match data_path {
155-
Some(p) => p,
156-
None => get_grin_path(chain_type, create_path)?,
157-
};
158-
159-
// Get path to default config file
160-
let mut config_path = grin_path.clone();
161-
config_path.push(WALLET_CONFIG_FILE_NAME);
162-
163-
// Return defaults if file doesn't exist
220+
// Return defaults config updated with node and wallet dir and chain dir
164221
match config_path.clone().exists() {
165222
false => {
166223
let mut default_config = GlobalWalletConfig::for_chain(chain_type);
167224
default_config.config_file_path = Some(config_path);
168-
// update paths relative to current dir
169-
default_config.update_paths(&grin_path);
170-
(grin_path, default_config)
225+
// Update paths relative to current dir
226+
default_config.update_paths(&wallet_path, &node_path);
227+
(wallet_path, default_config)
171228
}
172229
true => {
173-
let mut path = config_path.clone();
230+
let mut path = wallet_path.clone();
174231
path.pop();
175232
(
176233
path,
@@ -179,7 +236,7 @@ pub fn initial_setup_wallet(
179236
}
180237
}
181238
};
182-
239+
// Check API secrets, if ok, return config
183240
check_api_secret_file(chain_type, Some(path.clone()), OWNER_API_SECRET_FILE_NAME)?;
184241
check_api_secret_file(chain_type, Some(path), API_SECRET_FILE_NAME)?;
185242
Ok(config)
@@ -268,7 +325,7 @@ impl GlobalWalletConfig {
268325
}
269326

270327
/// Update paths
271-
pub fn update_paths(&mut self, wallet_home: &PathBuf) {
328+
pub fn update_paths(&mut self, wallet_home: &PathBuf, node_home: &PathBuf) {
272329
let mut wallet_path = wallet_home.clone();
273330
wallet_path.push(GRIN_WALLET_DIR);
274331
self.members.as_mut().unwrap().wallet.data_file_dir =
@@ -277,7 +334,7 @@ impl GlobalWalletConfig {
277334
secret_path.push(OWNER_API_SECRET_FILE_NAME);
278335
self.members.as_mut().unwrap().wallet.api_secret_path =
279336
Some(secret_path.to_str().unwrap().to_owned());
280-
let mut node_secret_path = wallet_home.clone();
337+
let mut node_secret_path = node_home.clone();
281338
node_secret_path.push(API_SECRET_FILE_NAME);
282339
self.members.as_mut().unwrap().wallet.node_api_secret_path =
283340
Some(node_secret_path.to_str().unwrap().to_owned());

controller/src/command.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub struct InitArgs {
7070
pub restore: bool,
7171
}
7272

73+
/// Write config (default if None), initiate the wallet
7374
pub fn init<L, C, K>(
7475
owner_api: &mut Owner<L, C, K>,
7576
_g_args: &GlobalArgs,
@@ -81,12 +82,16 @@ where
8182
C: NodeClient + 'static,
8283
K: keychain::Keychain + 'static,
8384
{
84-
// Assume global chain type has already been initialized.
8585
let chain_type = global::get_chain_type();
86-
8786
let mut w_lock = owner_api.wallet_inst.lock();
8887
let p = w_lock.lc_provider()?;
89-
p.create_config(&chain_type, WALLET_CONFIG_FILE_NAME, None, None, None)?;
88+
p.create_config(
89+
&chain_type,
90+
WALLET_CONFIG_FILE_NAME,
91+
Some(args.config),
92+
None,
93+
None,
94+
)?;
9095
p.create_wallet(
9196
None,
9297
args.recovery_phrase,

impls/src/lifecycle/default.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ where
8787
None => None,
8888
},
8989
};
90-
let wallet = match wallet_config {
91-
Some(w) => w,
90+
// Check if config was provided, if not load default and set update to "true"
91+
let (wallet, update) = match wallet_config {
92+
Some(w) => (w, false),
9293
None => match default_config.members.as_ref() {
93-
Some(m) => m.clone().wallet,
94-
None => WalletConfig::default(),
94+
Some(m) => (m.clone().wallet, true),
95+
None => (WalletConfig::default(), true),
9596
},
9697
};
9798
let tor = match tor_config {
@@ -136,11 +137,16 @@ where
136137
if config_file_name.exists() {
137138
return Ok(());
138139
}
139-
140-
let mut abs_path = std::env::current_dir()?;
141-
abs_path.push(self.data_dir.clone());
142-
143-
default_config.update_paths(&abs_path);
140+
// default settings are updated if no config was provided, no support for top_dir/here
141+
let mut abs_path_node = std::env::current_dir()?;
142+
abs_path_node.push(self.data_dir.clone());
143+
let mut absolute_path_wallet = std::env::current_dir()?;
144+
absolute_path_wallet.push(self.data_dir.clone());
145+
146+
// if no config provided, update defaults
147+
if update == true {
148+
default_config.update_paths(&abs_path_node, &absolute_path_wallet);
149+
};
144150
let res =
145151
default_config.write_to_file(config_file_name.to_str().unwrap(), false, None, None);
146152
if let Err(e) = res {

impls/src/node_clients/http.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ impl NodeClient for HTTPNodeClient {
128128
verified: Some(false),
129129
});
130130
} else {
131-
error!("Unable to contact Node to get version info: {}", e);
131+
error!("Unable to contact Node to get version info: {}, check your node is running", e);
132+
warn!("Warning: a) Node is offline, or b) 'node_api_secret_path' in 'grin-wallet.toml' is set incorrectly");
132133
return None;
133134
}
134135
}

src/bin/grin-wallet.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn real_main() -> i32 {
8787
let res = args.value_of("top_level_dir");
8888
match res {
8989
Some(d) => {
90-
current_dir = Some(PathBuf::from(d));
90+
current_dir = Some(PathBuf::from(d.replace("/", "\\")));
9191
}
9292
None => {
9393
warn!("Argument --top_level_dir needs a value. Defaulting to current directory")
@@ -138,7 +138,6 @@ fn real_main() -> i32 {
138138
"Using wallet configuration file at {}",
139139
config.config_file_path.as_ref().unwrap().to_str().unwrap()
140140
);
141-
142141
log_build_info();
143142

144143
global::init_global_chain_type(
@@ -152,11 +151,9 @@ fn real_main() -> i32 {
152151
.unwrap()
153152
.clone(),
154153
);
155-
156154
global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base());
157155

158156
let wallet_config = config.clone().members.unwrap().wallet;
159157
let node_client = HTTPNodeClient::new(&wallet_config.check_node_api_http_addr, None).unwrap();
160-
161158
cmd::wallet_command(&args, config, node_client)
162159
}

tests/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub fn config_command_wallet(
165165
.to_owned(),
166166
))?;
167167
}
168-
default_config.update_paths(&current_dir);
168+
default_config.update_paths(&current_dir, &current_dir);
169169
default_config
170170
.write_to_file(config_file_name.to_str().unwrap(), false, None, None)
171171
.unwrap_or_else(|e| {

0 commit comments

Comments
 (0)