Skip to content

Commit

Permalink
Reuse client instead of creating a new one (#408)
Browse files Browse the repository at this point in the history
* Reuse client instead of creating a new one

* typo
  • Loading branch information
Thoralf-M authored May 8, 2023
1 parent c72b659 commit 8cb54dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
5 changes: 4 additions & 1 deletion sdk/src/wallet/account/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ impl AccountBuilder {
}
}

let client = self.client_options.read().await.clone().finish().await?;
let client = match accounts.first() {
Some(account) => account.client.clone(),
None => self.client_options.read().await.clone().finish().await?,
};

// If addresses are provided we will use them directly without the additional checks, because then we assume
// that it's for offline signing and the secretManager can't be used
Expand Down
27 changes: 18 additions & 9 deletions sdk/src/wallet/wallet/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,6 @@ impl WalletBuilder {
#[cfg(feature = "storage")]
storage_manager.lock().await.save_wallet_data(&self).await?;

let client = self
.client_options
.clone()
.ok_or(crate::wallet::Error::MissingParameter("client_options"))?
.finish()
.await?;

#[cfg(feature = "events")]
let event_emitter = Arc::new(tokio::sync::Mutex::new(EventEmitter::new()));

Expand All @@ -201,10 +194,23 @@ impl WalletBuilder {
unlock_unused_inputs(&mut accounts)?;
#[cfg(not(feature = "storage"))]
let accounts = Vec::new();
// Client is only required if there are existing accounts
let client = if accounts.is_empty() {
None
} else {
Some(
self.client_options
.clone()
.ok_or(crate::wallet::Error::MissingParameter("client_options"))?
.finish()
.await?,
)
};
let mut accounts: Vec<Account> = try_join_all(accounts.into_iter().map(|a| {
Account::new(
a,
client.clone(),
// Safe to unwrap because we create the client if accounts aren't empty
client.as_ref().expect("client must exist").clone(),
self.secret_manager
.clone()
.expect("secret_manager needs to be provided"),
Expand All @@ -221,7 +227,10 @@ impl WalletBuilder {
// In the other case it was loaded from the database and addresses are up to date.
if new_provided_client_options {
for account in accounts.iter_mut() {
account.update_account_with_new_client(client.clone()).await?;
// Safe to unwrap because we create the client if accounts aren't empty
account
.update_account_with_new_client(client.as_ref().expect("client must exist").clone())
.await?;
}
}

Expand Down

0 comments on commit 8cb54dd

Please sign in to comment.