Skip to content

Commit

Permalink
Merge pull request #733 from zancas/single_way_to_create_zingoconfig
Browse files Browse the repository at this point in the history
Single way to create zingoconfig
  • Loading branch information
zancas authored Nov 28, 2023
2 parents cf4804c + f2ccc1f commit 2772fd7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
9 changes: 4 additions & 5 deletions integration-tests/tests/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,9 @@ mod fast {
let mut wallet_location = regtest_manager.zingo_datadir;
wallet_location.pop();
wallet_location.push("zingo_client_1");
let zingo_config = ZingoConfig::create_unconnected(
zingoconfig::ChainType::Regtest(regtest_network),
Some(wallet_location.clone()),
);
let zingo_config = ZingoConfig::build(zingoconfig::ChainType::Regtest(regtest_network))
.set_wallet_dir(wallet_location.clone())
.create();
wallet_location.push("zingo-wallet.dat");
let read_buffer = File::open(wallet_location.clone()).unwrap();

Expand Down Expand Up @@ -495,7 +494,7 @@ mod fast {
// with 3 addresses containig all receivers.
let data = include_bytes!("zingo-wallet-v26.dat");

let config = zingoconfig::ZingoConfig::create_unconnected(ChainType::Testnet, None);
let config = zingoconfig::ZingoConfig::build(ChainType::Testnet).create();
let wallet = LightWallet::read_internal(&data[..], &config)
.await
.map_err(|e| format!("Cannot deserialize LightWallet version 26 file: {}", e))
Expand Down
69 changes: 62 additions & 7 deletions zingoconfig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ pub fn construct_lightwalletd_uri(server: Option<String>) -> http::Uri {
.unwrap()
}

#[derive(Clone, Debug)]
pub struct ZingoConfigBuilder {
pub lightwalletd_uri: Option<http::Uri>,
pub chain: ChainType,
pub reorg_buffer_offset: Option<u32>,
pub monitor_mempool: Option<bool>,
/// The directory where the wallet and logfiles will be created. By default, this will be in ~/.zcash on Linux and %APPDATA%\Zcash on Windows. For mac it is in: ~/Library/Application Support/Zcash
pub wallet_dir: Option<PathBuf>,
/// The filename of the wallet. This will be created in the `wallet_dir`.
pub wallet_name: Option<PathBuf>,
/// The filename of the logfile. This will be created in the `wallet_dir`.
pub logfile_name: Option<PathBuf>,
}
/// Configuration data that is necessary? and sufficient? for the creation of a LightClient.
#[derive(Clone, Debug)]
pub struct ZingoConfig {
Expand All @@ -70,21 +83,63 @@ pub struct ZingoConfig {
/// The filename of the logfile. This will be created in the `wallet_dir`.
pub logfile_name: PathBuf,
}

impl ZingoConfig {
// Create an unconnected (to any server) config to test for local wallet etc...
pub fn create_unconnected(chain: ChainType, dir: Option<PathBuf>) -> ZingoConfig {
impl ZingoConfigBuilder {
pub fn set_wallet_dir(mut self, dir: PathBuf) -> Self {
self.wallet_dir = Some(dir);
self
}
pub fn set_lightwalletd(mut self, lightwalletd_uri: http::Uri) -> Self {
self.lightwalletd_uri = Some(lightwalletd_uri);
self
}
pub fn create(&self) -> ZingoConfig {
let lightwalletd_uri = if let Some(uri) = self.lightwalletd_uri.clone() {
uri
} else {
http::Uri::default()
};
ZingoConfig {
lightwalletd_uri: Arc::new(RwLock::new(http::Uri::default())),
chain,
lightwalletd_uri: Arc::new(RwLock::new(lightwalletd_uri)),
chain: self.chain,
monitor_mempool: false,
reorg_buffer_offset: REORG_BUFFER_OFFSET,
wallet_dir: dir,
wallet_dir: self.wallet_dir.clone(),
wallet_name: DEFAULT_WALLET_NAME.into(),
logfile_name: DEFAULT_LOGFILE_NAME.into(),
}
}
}
impl Default for ZingoConfigBuilder {
fn default() -> Self {
ZingoConfigBuilder {
lightwalletd_uri: None,
monitor_mempool: None,
reorg_buffer_offset: None,
wallet_dir: None,
wallet_name: None,
logfile_name: None,
chain: ChainType::Mainnet,
}
}
}

impl ZingoConfig {
#[deprecated]
// Create an unconnected (to any server) config to test for local wallet etc...
pub fn create_unconnected(chain: ChainType, dir: Option<PathBuf>) -> ZingoConfig {
if let Some(dir) = dir {
ZingoConfig::build(chain).set_wallet_dir(dir).create()
} else {
ZingoConfig::build(chain).create()
}
}

pub fn build(chain: ChainType) -> ZingoConfigBuilder {
ZingoConfigBuilder {
chain,
..ZingoConfigBuilder::default()
}
}
//Convenience wrapper
pub fn sapling_activation_height(&self) -> u64 {
self.chain
Expand Down
4 changes: 3 additions & 1 deletion zingolib/src/lightclient/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ mod tests {
.expect("This path is available.");

let wallet_name = data_dir.join("zingo-wallet.dat");
let config = ZingoConfig::create_unconnected(ChainType::FakeMainnet, Some(data_dir));
let config = ZingoConfig::build(ChainType::FakeMainnet)
.set_wallet_dir(data_dir)
.create();
let lc = LightClient::create_from_wallet_base(
WalletBase::MnemonicPhrase(CHIMNEY_BETTER_SEED.to_string()),
&config,
Expand Down

0 comments on commit 2772fd7

Please sign in to comment.