Skip to content

Commit 2ff6adf

Browse files
yeastplumeMichaelCordner
authored andcommitted
Revert "Fix for many wallet config errors (#731)"
This reverts commit 7ceade4.
1 parent 7ceade4 commit 2ff6adf

File tree

7 files changed

+43
-109
lines changed

7 files changed

+43
-109
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: 24 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
//! Configuration file management
1616
17+
use dirs;
1718
use rand::distributions::{Alphanumeric, Distribution};
1819
use rand::thread_rng;
1920
use std::env;
@@ -34,17 +35,15 @@ use crate::util::logger::LoggingConfig;
3435
/// Wallet configuration file name
3536
pub const WALLET_CONFIG_FILE_NAME: &str = "grin-wallet.toml";
3637
const WALLET_LOG_FILE_NAME: &str = "grin-wallet.log";
37-
/// .grin folder, usually in home/.grin
38-
pub const GRIN_HOME: &str = ".grin";
38+
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-
/// Function to get wallet dir and create dirs if not existing
47-
pub fn get_wallet_path(
46+
fn get_grin_path(
4847
chain_type: &global::ChainTypes,
4948
create_path: bool,
5049
) -> Result<PathBuf, ConfigError> {
@@ -69,49 +68,6 @@ pub fn get_wallet_path(
6968
}
7069
}
7170

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
11571
fn check_config_current_dir(path: &str) -> Option<PathBuf> {
11672
let p = env::current_dir();
11773
let mut c = match p {
@@ -166,7 +122,7 @@ fn check_api_secret_file(
166122
) -> Result<(), ConfigError> {
167123
let grin_path = match data_path {
168124
Some(p) => p,
169-
None => get_node_path(data_path, chain_type)?,
125+
None => get_grin_path(chain_type, false)?,
170126
};
171127
let mut api_secret_path = grin_path;
172128
api_secret_path.push(file_name);
@@ -178,7 +134,6 @@ fn check_api_secret_file(
178134
}
179135

180136
/// 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
182137
pub fn initial_setup_wallet(
183138
chain_type: &global::ChainTypes,
184139
data_path: Option<PathBuf>,
@@ -189,45 +144,33 @@ pub fn initial_setup_wallet(
189144
fs::create_dir_all(p)?;
190145
}
191146
}
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
147+
// Use config file if current directory if it exists, .grin home otherwise
211148
let (path, config) = if let Some(p) = check_config_current_dir(WALLET_CONFIG_FILE_NAME) {
212149
let mut path = p.clone();
213150
path.pop();
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)
151+
(path, GlobalWalletConfig::new(p.to_str().unwrap())?)
219152
} else {
220-
// Return defaults config updated with node and wallet dir and chain dir
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
221164
match config_path.clone().exists() {
222165
false => {
223166
let mut default_config = GlobalWalletConfig::for_chain(chain_type);
224167
default_config.config_file_path = Some(config_path);
225-
// Update paths relative to current dir
226-
default_config.update_paths(&wallet_path, &node_path);
227-
(wallet_path, default_config)
168+
// update paths relative to current dir
169+
default_config.update_paths(&grin_path);
170+
(grin_path, default_config)
228171
}
229172
true => {
230-
let mut path = wallet_path.clone();
173+
let mut path = config_path.clone();
231174
path.pop();
232175
(
233176
path,
@@ -236,7 +179,7 @@ pub fn initial_setup_wallet(
236179
}
237180
}
238181
};
239-
// Check API secrets, if ok, return config
182+
240183
check_api_secret_file(chain_type, Some(path.clone()), OWNER_API_SECRET_FILE_NAME)?;
241184
check_api_secret_file(chain_type, Some(path), API_SECRET_FILE_NAME)?;
242185
Ok(config)
@@ -325,7 +268,7 @@ impl GlobalWalletConfig {
325268
}
326269

327270
/// Update paths
328-
pub fn update_paths(&mut self, wallet_home: &PathBuf, node_home: &PathBuf) {
271+
pub fn update_paths(&mut self, wallet_home: &PathBuf) {
329272
let mut wallet_path = wallet_home.clone();
330273
wallet_path.push(GRIN_WALLET_DIR);
331274
self.members.as_mut().unwrap().wallet.data_file_dir =
@@ -334,7 +277,7 @@ impl GlobalWalletConfig {
334277
secret_path.push(OWNER_API_SECRET_FILE_NAME);
335278
self.members.as_mut().unwrap().wallet.api_secret_path =
336279
Some(secret_path.to_str().unwrap().to_owned());
337-
let mut node_secret_path = node_home.clone();
280+
let mut node_secret_path = wallet_home.clone();
338281
node_secret_path.push(API_SECRET_FILE_NAME);
339282
self.members.as_mut().unwrap().wallet.node_api_secret_path =
340283
Some(node_secret_path.to_str().unwrap().to_owned());

controller/src/command.rs

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

73-
/// Write config (default if None), initiate the wallet
7473
pub fn init<L, C, K>(
7574
owner_api: &mut Owner<L, C, K>,
7675
_g_args: &GlobalArgs,
@@ -82,16 +81,12 @@ where
8281
C: NodeClient + 'static,
8382
K: keychain::Keychain + 'static,
8483
{
84+
// Assume global chain type has already been initialized.
8585
let chain_type = global::get_chain_type();
86+
8687
let mut w_lock = owner_api.wallet_inst.lock();
8788
let p = w_lock.lc_provider()?;
88-
p.create_config(
89-
&chain_type,
90-
WALLET_CONFIG_FILE_NAME,
91-
Some(args.config),
92-
None,
93-
None,
94-
)?;
89+
p.create_config(&chain_type, WALLET_CONFIG_FILE_NAME, None, None, None)?;
9590
p.create_wallet(
9691
None,
9792
args.recovery_phrase,

impls/src/lifecycle/default.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ where
8787
None => None,
8888
},
8989
};
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),
90+
let wallet = match wallet_config {
91+
Some(w) => w,
9392
None => match default_config.members.as_ref() {
94-
Some(m) => (m.clone().wallet, true),
95-
None => (WalletConfig::default(), true),
93+
Some(m) => m.clone().wallet,
94+
None => WalletConfig::default(),
9695
},
9796
};
9897
let tor = match tor_config {
@@ -137,16 +136,11 @@ where
137136
if config_file_name.exists() {
138137
return Ok(());
139138
}
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-
};
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);
150144
let res =
151145
default_config.write_to_file(config_file_name.to_str().unwrap(), false, None, None);
152146
if let Err(e) = res {

impls/src/node_clients/http.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ impl NodeClient for HTTPNodeClient {
128128
verified: Some(false),
129129
});
130130
} else {
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");
131+
error!("Unable to contact Node to get version info: {}", e);
133132
return None;
134133
}
135134
}

src/bin/grin-wallet.rs

Lines changed: 4 additions & 1 deletion
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.replace("/", "\\")));
90+
current_dir = Some(PathBuf::from(d));
9191
}
9292
None => {
9393
warn!("Argument --top_level_dir needs a value. Defaulting to current directory")
@@ -138,6 +138,7 @@ fn real_main() -> i32 {
138138
"Using wallet configuration file at {}",
139139
config.config_file_path.as_ref().unwrap().to_str().unwrap()
140140
);
141+
141142
log_build_info();
142143

143144
global::init_global_chain_type(
@@ -151,9 +152,11 @@ fn real_main() -> i32 {
151152
.unwrap()
152153
.clone(),
153154
);
155+
154156
global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base());
155157

156158
let wallet_config = config.clone().members.unwrap().wallet;
157159
let node_client = HTTPNodeClient::new(&wallet_config.check_node_api_http_addr, None).unwrap();
160+
158161
cmd::wallet_command(&args, config, node_client)
159162
}

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, &current_dir);
168+
default_config.update_paths(&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)