Skip to content

Commit

Permalink
Merge pull request hyperledger-indy#2238 from toshirin33/perf_improve…
Browse files Browse the repository at this point in the history
…_listMyDidsWithMeta

IS-1561: performance improvement of listMyDidsWithMeta
  • Loading branch information
Artemkaaas authored Oct 13, 2020
2 parents 99648cd + 07fd4ad commit f9eb2cf
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
35 changes: 31 additions & 4 deletions libindy/src/commands/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,34 @@ impl DidCommandExecutor {
let mut did_search =
self.wallet_service.search_indy_records::<Did>(wallet_handle, "{}", &SearchOptions::id_value())?;

let mut metadata_search =
self.wallet_service.search_indy_records::<DidMetadata>(wallet_handle, "{}", &SearchOptions::id_value())?;

let mut temporarydid_search =
self.wallet_service.search_indy_records::<TemporaryDid>(wallet_handle, "{}", &SearchOptions::id_value())?;

let mut dids: Vec<DidWithMeta> = Vec::new();

let mut metadata_map: HashMap<String, String>= HashMap::new();
let mut temporarydid_map: HashMap<String, String>= HashMap::new();

while let Some(record) = metadata_search.fetch_next_record()? {
let did_id = record.get_id();
let tup: DidMetadata = record.get_value()
.ok_or(err_msg(IndyErrorKind::InvalidState, "No value for DID record"))
.and_then(|tags_json| serde_json::from_str(&tags_json)
.to_indy(IndyErrorKind::InvalidState, format!("Cannot deserialize Did: {:?}", did_id)))?;
metadata_map.insert(String::from(did_id), tup.value);
}

while let Some(record) = temporarydid_search.fetch_next_record()? {
let did_id = record.get_id();
let did: TemporaryDid = record.get_value()
.ok_or(err_msg(IndyErrorKind::InvalidState, "No value for DID record"))
.and_then(|tags_json| serde_json::from_str(&tags_json)
.to_indy(IndyErrorKind::InvalidState, format!("Cannot deserialize Did: {:?}", did_id)))?;
temporarydid_map.insert(did.did.0, did.verkey);
}

while let Some(did_record) = did_search.fetch_next_record()? {
let did_id = did_record.get_id();
Expand All @@ -319,14 +346,14 @@ impl DidCommandExecutor {
.and_then(|tags_json| serde_json::from_str(&tags_json)
.to_indy(IndyErrorKind::InvalidState, format!("Cannot deserialize Did: {:?}", did_id)))?;

let metadata = self.wallet_service.get_indy_opt_object::<DidMetadata>(wallet_handle, &did.did.0, &RecordOptions::id_value())?;
let temp_verkey = self.wallet_service.get_indy_opt_object::<TemporaryDid>(wallet_handle, &did.did.0, &RecordOptions::id_value())?;
let temp_verkey = temporarydid_map.remove(&did.did.0);
let metadata = metadata_map.remove(&did.did.0);

let did_with_meta = DidWithMeta {
did: did.did,
verkey: did.verkey,
temp_verkey: temp_verkey.map(|tv| tv.verkey),
metadata: metadata.map(|m| m.value),
temp_verkey: temp_verkey,
metadata: metadata,
};

dids.push(did_with_meta);
Expand Down
55 changes: 54 additions & 1 deletion libindy/tests/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::api::{INVALID_WALLET_HANDLE, INVALID_POOL_HANDLE};

#[cfg(feature = "local_nodes_pool")]
use std::thread;
use std::collections::HashMap;

pub const ENCRYPTED_MESSAGE: &'static [u8; 45] = &[187, 227, 10, 29, 46, 178, 12, 179, 197, 69, 171, 70, 228, 204, 52, 22, 199, 54, 62, 13, 115, 5, 216, 66, 20, 131, 121, 29, 251, 224, 253, 201, 75, 73, 225, 237, 219, 133, 35, 217, 131, 135, 232, 129, 32];
pub const SIGNATURE: &'static [u8; 64] = &[20, 191, 100, 213, 101, 12, 197, 198, 203, 49, 89, 220, 205, 192, 224, 221, 97, 77, 220, 190, 90, 60, 142, 23, 16, 240, 189, 129, 45, 148, 245, 8, 102, 95, 95, 249, 100, 89, 41, 227, 213, 25, 100, 1, 232, 188, 245, 235, 186, 21, 52, 176, 236, 11, 99, 70, 155, 159, 89, 215, 197, 239, 138, 5];
Expand Down Expand Up @@ -1032,4 +1033,56 @@ mod medium_cases {
assert_code!(ErrorCode::CommonInvalidStructure, res);
}
}
}

mod list_my_dids_with_meta{
use super::*;

#[test]
fn indy_list_dids(){
let setup = Setup::did();
let dids = did::list_my_dids_with_meta(setup.wallet_handle).unwrap();
let info_list: serde_json::Value = serde_json::from_str(&dids).unwrap();
assert_eq!(info_list.as_array().unwrap().len(), 1);
assert!(info_list[0]["metadata"].is_null());
assert_eq!(setup.verkey, info_list[0]["verkey"].as_str().unwrap().to_string());
}

#[test]
fn indy_list_dids_after_creating_dids(){
let setup = Setup::wallet();
let mut did2verkey: HashMap<String, String> = HashMap::new();
for _x in 0..10{
let (my_did, my_verkey) = did::create_my_did(setup.wallet_handle, "{}").unwrap();
did::set_did_metadata(setup.wallet_handle, &my_did, METADATA).unwrap();
did2verkey.insert(String::from(my_did), String::from(my_verkey));
}
let dids = did::list_my_dids_with_meta(setup.wallet_handle).unwrap();
let info_list: serde_json::Value = serde_json::from_str(&dids).unwrap();
assert_eq!(info_list.as_array().unwrap().len(), 10);
for info in info_list.as_array().unwrap() {
assert_eq!(info["metadata"].as_str().unwrap().to_string(),
METADATA.to_string());
assert_eq!(&info["verkey"].as_str().unwrap().to_string(),
did2verkey.get(&(info["did"]).as_str().unwrap().to_string()).unwrap());
}
}

#[test]
fn indy_list_dids_after_replace_keys_start(){
let setup = Setup::wallet();
let mut did2tempverkey: HashMap<String, String> = HashMap::new();
for _x in 0..10{
let (my_did, _my_verkey) = did::create_my_did(setup.wallet_handle, "{}").unwrap();
let temp_verkey = did::replace_keys_start(setup.wallet_handle, &my_did, "{}").unwrap();
did2tempverkey.insert(String::from(&my_did), temp_verkey);
}
let dids = did::list_my_dids_with_meta(setup.wallet_handle).unwrap();
let info_list: serde_json::Value = serde_json::from_str(&dids).unwrap();
for info in info_list.as_array().unwrap() {
let did = info["did"].as_str().unwrap().to_string();
assert_eq!(&info["tempVerkey"].as_str().unwrap().to_string(),
did2tempverkey.get(&did).unwrap());
}
}
}
}
4 changes: 4 additions & 0 deletions libindy/tests/utils/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ pub fn get_my_did_with_metadata(wallet_handle: WalletHandle, did: &str) -> Resul
did::get_my_did_with_metadata(wallet_handle, did).wait()
}

pub fn list_my_dids_with_meta(wallet_handle: WalletHandle) -> Result<String, IndyError> {
did::list_my_dids_with_metadata(wallet_handle).wait()
}

pub fn abbreviate_verkey(did: &str, verkey: &str) -> Result<String, IndyError> {
did::abbreviate_verkey(did, verkey).wait()
}
Expand Down

0 comments on commit f9eb2cf

Please sign in to comment.