Skip to content

Commit

Permalink
Fix clippy errors
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jun 25, 2023
1 parent a580991 commit fc12b56
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
33 changes: 18 additions & 15 deletions libvcx_core/src/api_vcx/api_global/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,41 @@ pub fn is_main_pool_open() -> bool {
// global_profile.inject_anoncreds_ledger_read()
}

pub fn reset_global_pool() {
setup_global_pool(None);
pub fn reset_global_pool()
-> LibvcxResult<()> {
setup_global_pool(None)?;
legacy_setup_global_pool(None);
Ok(())
}

pub fn setup_global_pool(handle: Option<PoolHandle>) {
pub fn setup_global_pool(handle: Option<PoolHandle>) -> LibvcxResult<()> {
match handle {
None => {
let mut anoncreds_read = ledger_anoncreds_read.write().unwrap();
let mut anoncreds_read = ledger_anoncreds_read.write()?;
*anoncreds_read = None;
let mut anoncreds_write = ledger_anoncreds_write.write().unwrap();
let mut anoncreds_write = ledger_anoncreds_write.write()?;
*anoncreds_write = None;
let mut indy_read = ledger_indy_read.write().unwrap();
let mut indy_read = ledger_indy_read.write()?;
*indy_read = None;
let mut indy_write = ledger_indy_write.write().unwrap();
let mut indy_write = ledger_indy_write.write()?;
*indy_write = None;
}
Some(pool_handle) => {
// todo: remove raw handle from global state, hide behind a trait, leave encapsulated in BaseWallet
let wallet_handle = get_main_wallet_handle();
let ledger_read = Arc::new(IndySdkLedgerRead::new(wallet_handle, pool_handle));
let ledger_write = Arc::new(IndySdkLedgerWrite::new(wallet_handle, pool_handle));
let mut anoncreds_read = ledger_anoncreds_read.write().unwrap();
let mut anoncreds_read = ledger_anoncreds_read.write()?;
*anoncreds_read = Some(ledger_read.clone() as Arc<dyn AnoncredsLedgerRead>);
let mut anoncreds_write = ledger_anoncreds_write.write().unwrap();
let mut anoncreds_write = ledger_anoncreds_write.write()?;
*anoncreds_write = Some(ledger_write.clone() as Arc<dyn AnoncredsLedgerWrite>);
let mut indy_read = ledger_indy_read.write().unwrap();
let mut indy_read = ledger_indy_read.write()?;
*indy_read = Some(ledger_read.clone() as Arc<dyn IndyLedgerRead>);
let mut indy_write = ledger_indy_write.write().unwrap();
let mut indy_write = ledger_indy_write.write()?;
*indy_write = Some(ledger_write.clone() as Arc<dyn IndyLedgerWrite>);
}
}
Ok(())
}

pub async fn open_main_pool(config: &PoolConfig) -> LibvcxResult<()> {
Expand Down Expand Up @@ -114,7 +117,7 @@ pub async fn open_main_pool(config: &PoolConfig) -> LibvcxResult<()> {
.map_err(|err| err.extend("Can not open Pool Ledger"))?;

legacy_setup_global_pool(Some(pool_handle));
setup_global_pool(Some(pool_handle));
setup_global_pool(Some(pool_handle))?;

info!("open_pool >> Pool Opened Successfully");

Expand All @@ -126,7 +129,7 @@ pub async fn close_main_pool() -> LibvcxResult<()> {
indy_close_pool(get_main_pool_handle()?).await?;
// todo: better way to go about this?
legacy_setup_global_pool(None);
setup_global_pool(None);
setup_global_pool(None)?;
Ok(())
}

Expand Down Expand Up @@ -158,7 +161,7 @@ pub mod tests {
};
open_main_pool(&config).await.unwrap();
// delete_test_pool(get_main_pool_handle().unwrap()).await;
reset_global_pool();
reset_global_pool().unwrap();
}

#[cfg(feature = "pool_tests")]
Expand Down Expand Up @@ -187,7 +190,7 @@ pub mod tests {
);

delete_named_test_pool(&pool_name).await;
reset_global_pool();
reset_global_pool().unwrap();
}

#[cfg(feature = "pool_tests")]
Expand Down
12 changes: 6 additions & 6 deletions libvcx_core/src/api_vcx/api_global/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Debug for VcxGlobalsProfile {

impl ProfileV2 for VcxGlobalsProfile {
fn inject_indy_ledger_read(&self) -> LibvcxResult<Arc<dyn IndyLedgerRead>> {
let ledger = ledger_indy_read.read().unwrap();
let ledger = ledger_indy_read.read()?;
match ledger.as_ref() {
None => Err(LibvcxError::from_msg(
LibvcxErrorKind::NotReady,
Expand All @@ -59,7 +59,7 @@ impl ProfileV2 for VcxGlobalsProfile {
}

fn inject_indy_ledger_write(&self) -> LibvcxResult<Arc<dyn IndyLedgerWrite>> {
let ledger = ledger_indy_write.read().unwrap();
let ledger = ledger_indy_write.read()?;
match ledger.as_ref() {
None => Err(LibvcxError::from_msg(
LibvcxErrorKind::NotReady,
Expand All @@ -70,7 +70,7 @@ impl ProfileV2 for VcxGlobalsProfile {
}

fn inject_anoncreds(&self) -> LibvcxResult<Arc<dyn BaseAnonCreds>> {
let anoncreds = base_anoncreds.read().unwrap();
let anoncreds = base_anoncreds.read()?;
match anoncreds.as_ref() {
None => Err(LibvcxError::from_msg(
LibvcxErrorKind::NotReady,
Expand All @@ -81,7 +81,7 @@ impl ProfileV2 for VcxGlobalsProfile {
}

fn inject_anoncreds_ledger_read(&self) -> LibvcxResult<Arc<dyn AnoncredsLedgerRead>> {
let ledger = ledger_anoncreds_read.read().unwrap();
let ledger = ledger_anoncreds_read.read()?;
match ledger.as_ref() {
None => Err(LibvcxError::from_msg(
LibvcxErrorKind::NotReady,
Expand All @@ -92,7 +92,7 @@ impl ProfileV2 for VcxGlobalsProfile {
}

fn inject_anoncreds_ledger_write(&self) -> LibvcxResult<Arc<dyn AnoncredsLedgerWrite>> {
let ledger = ledger_anoncreds_write.read().unwrap();
let ledger = ledger_anoncreds_write.read()?;
match ledger.as_ref() {
None => Err(LibvcxError::from_msg(
LibvcxErrorKind::NotReady,
Expand All @@ -103,7 +103,7 @@ impl ProfileV2 for VcxGlobalsProfile {
}

fn inject_wallet(&self) -> LibvcxResult<Arc<dyn BaseWallet>> {
let global_base_wallet = base_wallet.read().unwrap();
let global_base_wallet = base_wallet.read()?;
match global_base_wallet.as_ref() {
None => Err(LibvcxError::from_msg(
LibvcxErrorKind::NotReady,
Expand Down
8 changes: 7 additions & 1 deletion libvcx_core/src/api_vcx/api_global/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use aries_vcx::global::settings::{
reset_config_values, CONFIG_POOL_NAME, CONFIG_WALLET_KEY, CONFIG_WALLET_KEY_DERIVATION, CONFIG_WALLET_NAME,
CONFIG_WALLET_TYPE, DEFAULT_POOL_NAME, DEFAULT_WALLET_NAME, UNINITIALIZED_WALLET_KEY, WALLET_KDF_DEFAULT,
};
use crate::errors::error::LibvcxResult;

pub fn state_vcx_shutdown(delete: bool) {
info!("vcx_shutdown >>>");
Expand Down Expand Up @@ -51,7 +52,12 @@ pub fn state_vcx_shutdown(delete: bool) {

let _ = reset_config_values();
reset_main_agency_client();
reset_global_pool();
match reset_global_pool() {
Ok(_) => {}
Err(err) => {
error!("Failed to reset global pool: {}", err);
}
}
}

#[cfg(test)]
Expand Down
17 changes: 9 additions & 8 deletions libvcx_core/src/api_vcx/api_global/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,32 @@ pub async fn export_main_wallet(path: &str, backup_key: &str) -> LibvcxResult<()
map_ariesvcx_core_result(indy::wallet::export_wallet(get_main_wallet_handle(), path, backup_key).await)
}

fn setup_global_wallet(handle: WalletHandle) {
fn setup_global_wallet(handle: WalletHandle) -> LibvcxResult<()> {
// new way
let base_wallet_impl: Arc<dyn BaseWallet> = Arc::new(IndySdkWallet::new(handle));
let mut b_wallet = base_wallet.write().unwrap();
let mut b_wallet = base_wallet.write()?;
*b_wallet = Some(base_wallet_impl);
// anoncreds
let base_anoncreds_impl: Arc<dyn BaseAnonCreds> = Arc::new(IndySdkAnonCreds::new(handle));
let mut b_anoncreds = base_anoncreds.write().unwrap();
let mut b_anoncreds = base_anoncreds.write()?;
*b_anoncreds = Some(base_anoncreds_impl);
Ok(())
}

pub fn setup_wallet(handle: WalletHandle) {
pub fn setup_wallet(handle: WalletHandle) -> LibvcxResult<()> {
legacy_setup_global_wallet(handle);
setup_global_wallet(handle);
setup_global_wallet(handle)
}

pub async fn open_as_main_wallet(wallet_config: &WalletConfig) -> LibvcxResult<WalletHandle> {
let handle = indy::wallet::open_wallet(wallet_config).await?;
setup_wallet(handle);
setup_wallet(handle)?;
Ok(handle)
}

pub async fn create_and_open_as_main_wallet(wallet_config: &WalletConfig) -> LibvcxResult<WalletHandle> {
let handle = indy::wallet::create_and_open_wallet(wallet_config).await?;
setup_wallet(handle);
setup_wallet(handle)?;
Ok(handle)
}

Expand All @@ -83,7 +84,7 @@ pub async fn close_main_wallet() -> LibvcxResult<()> {
//legacy
reset_main_wallet_handle();
// new way
let mut b_wallet = base_wallet.write().unwrap();
let mut b_wallet = base_wallet.write()?;
*b_wallet = None;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion libvcx_core/src/api_vcx/utils/devsetup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SetupGlobalsWalletPoolAgency {

setup_wallet(init.wallet_handle);
set_main_agency_client(init.agency_client.clone());
setup_global_pool(Some(init.pool_handle));
setup_global_pool(Some(init.pool_handle)).unwrap();

f(init).await;

Expand Down

0 comments on commit fc12b56

Please sign in to comment.