Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from hyperledger/master
Browse files Browse the repository at this point in the history
rebase
  • Loading branch information
mattraffel authored Jan 11, 2019
2 parents 8320b8f + 705cf88 commit 51c6b89
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
28 changes: 26 additions & 2 deletions cli/src/commands/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ pub mod rotate_key_command {
}

fn _get_current_verkey(pool_handle: i32, pool_name: &str, wallet_handle: i32, wallet_name: &str, did: &str) -> Result<Option<String>, ()> {
//TODO: There nym is requested. Due to freshness issues response might be stale or outdated. Something should be done with it
let response_json = Ledger::build_get_nym_request(Some(did), did)
.and_then(|request| Ledger::sign_and_submit_request(pool_handle, wallet_handle, did, &request))
.map_err(|err| handle_transaction_error(err, Some(did), Some(pool_name), Some(wallet_name)))?;
Expand Down Expand Up @@ -664,6 +665,27 @@ pub mod tests {
#[cfg(feature = "nullpay_plugin")]
use commands::ledger::tests::{set_fees, create_address_and_mint_sources, get_source_input, FEES, OUTPUT};

fn ensure_nym_written(ctx: &CommandContext, did: &str, verkey: &str) {
let wallet_handle = ensure_opened_wallet_handle(ctx).unwrap();
let request = Ledger::build_get_nym_request(None, did).unwrap();
let request = Ledger::sign_request(wallet_handle, did, &request).unwrap();
submit_retry(ctx, &request, |response| {
let res = req_for_nym(response);
match res {
Some(ref verkey_received) if verkey_received == verkey => Ok(()),
_ => Err(())
}
}).unwrap()
}

fn req_for_nym(response: &str) -> Option<String> {
let parsed = serde_json::from_str::<serde_json::Value>(&response).ok()?;
let data = parsed["result"]["data"].as_str()?;
let data = serde_json::from_str::<serde_json::Value>(&data).ok()?;
let verkey = data["verkey"].as_str()?;
Some(verkey.to_string())
}

#[test]
pub fn rotate_works() {
TestUtils::cleanup_storage();
Expand All @@ -677,6 +699,7 @@ pub mod tests {
let (did, verkey) = Did::new(wallet_handle, "{}").unwrap();
use_did(&ctx, DID_TRUSTEE);
send_nym(&ctx, &did, &verkey, None);
ensure_nym_written(&ctx, &did, &verkey);
use_did(&ctx, &did);

let did_info = get_did_info(wallet_handle, &did);
Expand Down Expand Up @@ -713,6 +736,7 @@ pub mod tests {
let new_verkey = Did::replace_keys_start(wallet_handle, &did, "{}").unwrap();
let request = Ledger::build_nym_request(&did, &did, Some(&new_verkey), None, None).unwrap();
Ledger::sign_and_submit_request(pool_handle, wallet_handle, &did, &request).unwrap();
ensure_nym_written(&ctx, &did, &new_verkey);

let did_info = get_did_info(wallet_handle, &did);
assert_eq!(did_info["verkey"].as_str().unwrap(), verkey);
Expand Down Expand Up @@ -746,7 +770,7 @@ pub mod tests {
use_did(&ctx, DID_TRUSTEE);
send_nym(&ctx, &did, &verkey, None);
use_did(&ctx, &did);

ensure_nym_written(&ctx, &did, &verkey);

let new_verkey = Did::replace_keys_start(wallet_handle, &did, "{}").unwrap();

Expand Down Expand Up @@ -885,4 +909,4 @@ pub mod tests {
cmd.execute(&ctx, &params).unwrap();
}
}
}
}
26 changes: 4 additions & 22 deletions cli/src/commands/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4434,7 +4434,7 @@ pub mod tests {

fn _ensure_nym_added(ctx: &CommandContext, did: &str) {
let request = Ledger::build_get_nym_request(None, did).unwrap();
_submit_retry(ctx, &request, |response| {
submit_retry(ctx, &request, |response| {
serde_json::from_str::<Response<ReplyResult<String>>>(&response)
.and_then(|response| serde_json::from_str::<serde_json::Value>(&response.result.unwrap().data))
}).unwrap();
Expand All @@ -4443,7 +4443,7 @@ pub mod tests {
fn _ensure_attrib_added(ctx: &CommandContext, did: &str, raw: Option<&str>, hash: Option<&str>, enc: Option<&str>) {
let attr = if raw.is_some() { Some("endpoint") } else { None };
let request = Ledger::build_get_attrib_request(None, did, attr, hash, enc).unwrap();
_submit_retry(ctx, &request, |response| {
submit_retry(ctx, &request, |response| {
serde_json::from_str::<Response<ReplyResult<String>>>(&response)
.map_err(|_| ())
.and_then(|response| {
Expand All @@ -4456,7 +4456,7 @@ pub mod tests {
fn _ensure_schema_added(ctx: &CommandContext, did: &str) {
let id = build_schema_id(did, "gvt", "1.0");
let request = Ledger::build_get_schema_request(None, &id).unwrap();
_submit_retry(ctx, &request, |response| {
submit_retry(ctx, &request, |response| {
let schema: serde_json::Value = serde_json::from_str(&response).unwrap();
schema["result"]["seqNo"].as_i64().ok_or(())
}).unwrap();
Expand All @@ -4465,27 +4465,9 @@ pub mod tests {
fn _ensure_cred_def_added(ctx: &CommandContext, did: &str, schema_id: &str) {
let id = build_cred_def_id(did, schema_id, "CL", "TAG");
let request = Ledger::build_get_cred_def_request(None, &id).unwrap();
_submit_retry(ctx, &request, |response| {
submit_retry(ctx, &request, |response| {
let cred_def: serde_json::Value = serde_json::from_str(&response).unwrap();
cred_def["result"]["seqNo"].as_i64().ok_or(())
}).unwrap();
}

fn _submit_retry<F, T, E>(ctx: &CommandContext, request: &str, parser: F) -> Result<(), ()>
where F: Fn(&str) -> Result<T, E> {
const SUBMIT_RETRY_CNT: usize = 3;
const SUBMIT_TIMEOUT_SEC: u64 = 2;

let pool_handle = ensure_connected_pool_handle(ctx).unwrap();

for _ in 0..SUBMIT_RETRY_CNT {
let response = Ledger::submit_request(pool_handle, request).unwrap();
if parser(&response).is_ok() {
return Ok(());
}
::std::thread::sleep(::std::time::Duration::from_secs(SUBMIT_TIMEOUT_SEC));
}

return Err(());
}
}
20 changes: 20 additions & 0 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use self::regex::Regex;
use command_executor::{CommandContext, CommandParams};

use std;
use libindy::ledger::Ledger;

pub fn get_str_param<'a>(name: &'a str, params: &'a CommandParams) -> Result<&'a str, ()> {
match params.get(name) {
Expand Down Expand Up @@ -237,4 +238,23 @@ pub fn set_connected_pool(ctx: &CommandContext, value: Option<(i32, String)>) {
ctx.set_int_value("CONNECTED_POOL_HANDLE", value.as_ref().map(|value| value.0.to_owned()));
ctx.set_string_value("CONNECTED_POOL_NAME", value.as_ref().map(|value| value.1.to_owned()));
ctx.set_sub_prompt(1, value.map(|value| format!("pool({})", value.1)));
}

#[cfg(test)]
pub fn submit_retry<F, T, E>(ctx: &CommandContext, request: &str, parser: F) -> Result<(), ()>
where F: Fn(&str) -> Result<T, E> {
const SUBMIT_RETRY_CNT: usize = 3;
const SUBMIT_TIMEOUT_SEC: u64 = 2;

let pool_handle = ensure_connected_pool_handle(ctx).unwrap();

for _ in 0..SUBMIT_RETRY_CNT {
let response = Ledger::submit_request(pool_handle, request).unwrap();
if parser(&response).is_ok() {
return Ok(());
}
::std::thread::sleep(::std::time::Duration::from_secs(SUBMIT_TIMEOUT_SEC));
}

return Err(());
}

0 comments on commit 51c6b89

Please sign in to comment.