Skip to content
Open
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
3 changes: 2 additions & 1 deletion darkside-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ async fn sent_transaction_reorged_into_mempool() {
.set_chain_type(ChainType::Regtest(activation_heights))
.set_wallet_dir(light_client.wallet_dir().unwrap())
.set_wallet_config(WalletConfig::Read)
.build();
.build()
.unwrap();
let mut loaded_client = LightClient::new(config, true).await.unwrap();

loaded_client.sync_and_await().await.unwrap();
Expand Down
9 changes: 6 additions & 3 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,8 @@ mod slow {
birthday: 1,
wallet_settings: default_test_wallet_settings(),
})
.build();
.build()
.unwrap();
let mut watch_client = LightClient::new(zingo_config, false).await.unwrap();
// assert empty wallet before rescan
let balance = watch_client
Expand Down Expand Up @@ -4614,7 +4615,8 @@ mod testnet_test {
wallet_settings: default_test_wallet_settings(),
})
.set_wallet_dir(wallet_dir.path().to_path_buf())
.build();
.build()
.unwrap();

let mut lightclient = LightClient::new(config, true).await.unwrap();
lightclient.save_task().await;
Expand All @@ -4640,7 +4642,8 @@ mod testnet_test {
.set_indexer_uri((DEFAULT_INDEXER_URI_TESTNET).parse::<http::Uri>().unwrap())
.set_wallet_config(WalletConfig::Read)
.set_wallet_dir(wallet_dir.path().to_path_buf())
.build();
.build()
.unwrap();
LightClient::new(config, true).await.unwrap();

test_count += 1;
Expand Down
6 changes: 4 additions & 2 deletions libtonode-tests/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ async fn sync_mainnet_test() {
birthday: 1_500_000,
wallet_settings: default_test_wallet_settings(),
})
.build();
.build()
.unwrap();
let mut lightclient = LightClient::new(config, true).await.unwrap();

lightclient.sync().await.unwrap();
Expand Down Expand Up @@ -168,7 +169,8 @@ async fn add_subtree_roots() {
birthday: 2_000_000,
wallet_settings: default_test_wallet_settings(),
})
.build();
.build()
.unwrap();
let mut lightclient = LightClient::new(config, true).await.unwrap();

let mut grpc_client = GrpcIndexer::new(lightclient.indexer_uri().clone())
Expand Down
5 changes: 3 additions & 2 deletions zingo-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,13 @@ fn build_zingo_config(filled_template: &ConfigTemplate) -> std::io::Result<Clien
}
};

Ok(ClientConfig::builder()
ClientConfig::builder()
.set_indexer_uri(filled_template.server.clone())
.set_chain_type(filled_template.chaintype)
.set_wallet_dir(filled_template.data_dir.clone())
.set_wallet_config(wallet_config)
.build())
.build()
.map_err(|e| std::io::Error::other(e.to_string()))
}

pub(crate) fn startup(filled_template: &ConfigTemplate) -> std::io::Result<CommandChannel> {
Expand Down
1 change: 1 addition & 0 deletions zingolib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- `config::ClientConfigBuilder`: `build` method now returns result for improved error handling.

### Removed

Expand Down
78 changes: 46 additions & 32 deletions zingolib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ impl ClientConfigBuilder {
}

/// Build a [`ClientConfig`] from the builder.
pub fn build(self) -> ClientConfig {
let wallet_dir = wallet_dir_or_default(self.wallet_dir, self.chain_type);
pub fn build(self) -> Result<ClientConfig, ClientConfigError> {
let wallet_dir = wallet_dir_or_default(self.wallet_dir, self.chain_type)?;
let wallet_name = wallet_name_or_default(self.wallet_name);
ClientConfig {

Ok(ClientConfig {
indexer_uri: self
.indexer_uri
.clone()
Expand All @@ -387,7 +388,7 @@ impl ClientConfigBuilder {
wallet_dir,
wallet_name,
wallet_config: self.wallet_config,
}
})
}
}

Expand Down Expand Up @@ -423,48 +424,60 @@ fn wallet_name_or_default(opt_wallet_name: Option<String>) -> String {
}
}

fn wallet_dir_or_default(opt_wallet_dir: Option<PathBuf>, chain: ChainType) -> PathBuf {
fn wallet_dir_or_default(
opt_wallet_dir: Option<PathBuf>,
chain: ChainType,
) -> Result<PathBuf, ClientConfigError> {
let wallet_dir: PathBuf;
#[cfg(any(target_os = "ios", target_os = "android"))]
{
// TODO: handle errors
wallet_dir = opt_wallet_dir.unwrap();
wallet_dir = opt_wallet_dir.ok_or_else(|| ClientConfigError::WalletDirNotSpecified)?;
}

#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
wallet_dir = opt_wallet_dir.clone().unwrap_or_else(|| {
let mut dir = dirs::data_dir().expect("Couldn't determine user's data directory!");
wallet_dir = opt_wallet_dir.clone().map_or_else(
|| {
let mut dir = dirs::data_dir().ok_or(ClientConfigError::UsersDataDirNotFound)?;

#[cfg(any(target_os = "macos", target_os = "windows"))]
{
dir.push("Zcash");
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
{
dir.push("Zcash");
}

#[cfg(not(any(target_os = "macos", target_os = "windows")))]
{
dir.push(".zcash");
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
{
dir.push(".zcash");
}

match chain {
ChainType::Mainnet => {}
ChainType::Testnet => dir.push("testnet3"),
ChainType::Regtest(_) => dir.push("regtest"),
}
match chain {
ChainType::Mainnet => {}
ChainType::Testnet => dir.push("testnet3"),
ChainType::Regtest(_) => dir.push("regtest"),
}

dir
});
Ok(dir)
},
Ok,
)?;

// Create directory if it doesn't exist on non-mobile platforms
match std::fs::create_dir_all(wallet_dir.clone()) {
Ok(()) => {}
Err(e) => {
panic!("Couldn't create zcash directory!\n {e}");
}
}
std::fs::create_dir_all(wallet_dir.clone())
.map_err(|e| ClientConfigError::FileError(e.to_string()))?;
}

wallet_dir
Ok(wallet_dir)
}

/// Invalid client config.
#[derive(thiserror::Error, Debug, Clone)]
pub enum ClientConfigError {
#[error("Wallet directory must be specified for iOS and Android platforms.")]
WalletDirNotSpecified,
#[error("User's default data directory not found.")]
UsersDataDirNotFound,
#[error("Failed to create wallet directory. {0}")]
FileError(String),
}

#[cfg(test)]
Expand All @@ -485,7 +498,8 @@ mod tests {
.set_indexer_uri(valid_uri.clone())
.set_chain_type(ChainType::Mainnet)
.set_wallet_dir(temp_path)
.build();
.build()
.unwrap();

assert_eq!(valid_config.indexer_uri(), valid_uri);
assert_eq!(valid_config.chain_type(), ChainType::Mainnet);
Expand Down
3 changes: 2 additions & 1 deletion zingolib/src/lightclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ mod tests {
birthday: 1,
wallet_settings: default_test_wallet_settings(),
})
.build();
.build()
.unwrap();

let mut lc = LightClient::new(config.clone(), false).await.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion zingolib/src/lightclient/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ mod shielding {
birthday: 419200,
wallet_settings: default_test_wallet_settings(),
})
.build();
.build()
.unwrap();
LightClient::new(config, true).await.unwrap()
}

Expand Down
3 changes: 2 additions & 1 deletion zingolib/src/lightclient/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ mod test {
birthday: 419200,
wallet_settings: default_test_wallet_settings(),
})
.build();
.build()
.unwrap();
LightClient::new(config, true).await.unwrap()
}

Expand Down
7 changes: 5 additions & 2 deletions zingolib/src/wallet/disk/testing/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl NetworkSeedVersion {
.set_wallet_dir(self.example_wallet_path().parent().unwrap().to_path_buf())
.set_wallet_config(WalletConfig::Read)
.build()
.unwrap()
}
NetworkSeedVersion::Testnet(_) => ClientConfig::builder()
.set_indexer_uri(DEFAULT_INDEXER_URI_TESTNET.parse::<Uri>().unwrap())
Expand All @@ -217,7 +218,8 @@ impl NetworkSeedVersion {
)
.set_wallet_dir(self.example_wallet_path().parent().unwrap().to_path_buf())
.set_wallet_config(WalletConfig::Read)
.build(),
.build()
.unwrap(),
NetworkSeedVersion::Mainnet(_) => ClientConfig::builder()
.set_indexer_uri(DEFAULT_INDEXER_URI.parse::<Uri>().unwrap())
.set_chain_type(ChainType::Mainnet)
Expand All @@ -230,7 +232,8 @@ impl NetworkSeedVersion {
)
.set_wallet_dir(self.example_wallet_path().parent().unwrap().to_path_buf())
.set_wallet_config(WalletConfig::Read)
.build(),
.build()
.unwrap(),
};

LightClient::new(config, false).await.unwrap()
Expand Down
3 changes: 2 additions & 1 deletion zingolib/src/wallet/disk/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ async fn reload_wallet_from_file() {
.set_chain_type(mid_client_network)
.set_wallet_dir(mid_client.wallet_dir().unwrap())
.set_wallet_config(WalletConfig::Read)
.build();
.build()
.unwrap();
let loaded_client = LightClient::new(config, true).await.unwrap();
let loaded_wallet = loaded_client.wallet().read().await;

Expand Down
1 change: 1 addition & 0 deletions zingolib_testutils/src/scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl ClientBuilder {
.set_wallet_dir(conf_path)
.set_wallet_config(wallet_config)
.build()
.unwrap()
}

/// TODO: Add Doc Comment Here!
Expand Down
Loading