From 3c6e9e45124b70b2c26e1f93da606eec6dd5921f Mon Sep 17 00:00:00 2001 From: Matthias Kandora Date: Mon, 30 Aug 2021 17:32:58 +0200 Subject: [PATCH 1/4] fix: control flow corrected --- client/src/interface.rs | 21 ++++++++++++-- client/src/tests/interface_tests.rs | 45 +++++++++++++++++------------ 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/client/src/interface.rs b/client/src/interface.rs index 58aaabc42..0cc88755e 100644 --- a/client/src/interface.rs +++ b/client/src/interface.rs @@ -157,14 +157,24 @@ impl Stronghold { match result { Ok(_) => { // exists - let _result = self + match self .target .send(WriteToVault { location, payload, hint, }) - .await; + .await + { + Ok(result) => { + return if result.is_ok() { + StatusMessage::OK + } else { + StatusMessage::Error(result.err().unwrap().to_string()) + } + } + Err(e) => return StatusMessage::Error(e.to_string()), + }; } Err(_) => { // does not exist @@ -177,7 +187,7 @@ impl Stronghold { { Ok(_) => { // write to vault - if let Ok(_result) = self + if let Ok(result) = self .target .send(WriteToVault { location, @@ -186,6 +196,11 @@ impl Stronghold { }) .await { + if result.is_ok() { + return StatusMessage::OK; + } else { + return StatusMessage::Error(result.err().unwrap().to_string()); + } } else { return StatusMessage::Error("Error Writing data".into()); } diff --git a/client/src/tests/interface_tests.rs b/client/src/tests/interface_tests.rs index 7e9a47505..8ec6edf91 100644 --- a/client/src/tests/interface_tests.rs +++ b/client/src/tests/interface_tests.rs @@ -31,14 +31,15 @@ async fn test_stronghold() { .unwrap(); // Write at the first record of the vault using Some(0). Also creates the new vault. - stronghold + assert!(stronghold .write_to_vault( loc0.clone(), b"test".to_vec(), RecordHint::new(b"first hint").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); // read head. let (p, _) = stronghold.read_secret(client_path.clone(), loc0.clone()).await; @@ -46,28 +47,30 @@ async fn test_stronghold() { assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("test")); // Write on the next record of the vault using None. This calls InitRecord and creates a new one at index 1. - stronghold + assert!(stronghold .write_to_vault( loc1.clone(), b"another test".to_vec(), RecordHint::new(b"another hint").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); // read head. let (p, _) = stronghold.read_secret(client_path.clone(), loc1.clone()).await; assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("another test")); - stronghold + assert!(stronghold .write_to_vault( loc2.clone(), b"yet another test".to_vec(), RecordHint::new(b"yet another hint").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); // read head. let (p, _) = stronghold.read_secret(client_path.clone(), loc2.clone()).await; @@ -174,14 +177,15 @@ async fn run_stronghold_multi_actors() { stronghold.switch_actor_target(client_path0.clone()).await; - stronghold + assert!(stronghold .write_to_vault( loc0.clone(), b"test".to_vec(), RecordHint::new(b"0").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); // read head. let (p, _) = stronghold.read_secret(client_path0.clone(), loc0.clone()).await; @@ -191,14 +195,15 @@ async fn run_stronghold_multi_actors() { stronghold.switch_actor_target(client_path1.clone()).await; // Write on the next record of the vault using None. This calls InitRecord and creates a new one at index 1. - stronghold + assert!(stronghold .write_to_vault( loc0.clone(), b"another test".to_vec(), RecordHint::new(b"1").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); // read head. let (p, _) = stronghold.read_secret(client_path1.clone(), loc0.clone()).await; @@ -207,14 +212,15 @@ async fn run_stronghold_multi_actors() { stronghold.switch_actor_target(client_path0.clone()).await; - stronghold + assert!(stronghold .write_to_vault( loc0.clone(), b"yet another test".to_vec(), RecordHint::new(b"2").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); let (p, _) = stronghold.read_secret(client_path0.clone(), loc0.clone()).await; @@ -249,27 +255,29 @@ async fn run_stronghold_multi_actors() { assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("another test")); - stronghold + assert!(stronghold .write_to_vault( loc3.clone(), b"a new actor test".to_vec(), RecordHint::new(b"2").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); let (p, _) = stronghold.read_secret(client_path2.clone(), loc3).await; assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("a new actor test")); - stronghold + assert!(stronghold .write_to_vault( loc4.clone(), b"a new actor test again".to_vec(), RecordHint::new(b"3").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); let (p, _) = stronghold.read_secret(client_path2, loc4.clone()).await; @@ -329,14 +337,15 @@ async fn test_stronghold_generics() { .await .unwrap(); - stronghold + assert!(stronghold .write_to_vault( slip10_seed.clone(), b"AAAAAA".to_vec(), RecordHint::new(b"first hint").expect(line_error!()), vec![], ) - .await; + .await + .is_ok()); let (p, _) = stronghold.read_secret(client_path, slip10_seed).await; assert_eq!(std::str::from_utf8(&p.unwrap()), Ok("AAAAAA")); From 87b29b43b55fb0341128f80c21949c0bf466787c Mon Sep 17 00:00:00 2001 From: Matthias Kandora Date: Mon, 30 Aug 2021 17:41:26 +0200 Subject: [PATCH 2/4] add changes doc --- .changes/write_to_vault.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/write_to_vault.md diff --git a/.changes/write_to_vault.md b/.changes/write_to_vault.md new file mode 100644 index 000000000..4252f5d04 --- /dev/null +++ b/.changes/write_to_vault.md @@ -0,0 +1,5 @@ +--- +"iota-stronghold": patch +--- + +- corrects wrong control flow. `write_to_vault` always returned an error even if the operation was successful. \ No newline at end of file From 59d7d619a927d572f2eea26ebe9d774694f1e35a Mon Sep 17 00:00:00 2001 From: Matthias Kandora Date: Mon, 30 Aug 2021 18:21:06 +0200 Subject: [PATCH 3/4] Update client/src/interface.rs Co-authored-by: Elena Frank <57632201+elenaf9@users.noreply.github.com> --- client/src/interface.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/client/src/interface.rs b/client/src/interface.rs index 0cc88755e..1024f075f 100644 --- a/client/src/interface.rs +++ b/client/src/interface.rs @@ -166,13 +166,10 @@ impl Stronghold { }) .await { - Ok(result) => { - return if result.is_ok() { - StatusMessage::OK - } else { - StatusMessage::Error(result.err().unwrap().to_string()) - } - } + Ok(result) => match result { + Ok(_) => StatusMessage::OK, + Err(e) => StatusMessage::Error(e.to_string()), + }, Err(e) => return StatusMessage::Error(e.to_string()), }; } From e1bfc478c860c491580e4e7bb93a70aead9bcf11 Mon Sep 17 00:00:00 2001 From: Matthias Kandora Date: Mon, 30 Aug 2021 18:36:24 +0200 Subject: [PATCH 4/4] fix: regression --- client/src/interface.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/interface.rs b/client/src/interface.rs index 1024f075f..805ada20c 100644 --- a/client/src/interface.rs +++ b/client/src/interface.rs @@ -157,7 +157,7 @@ impl Stronghold { match result { Ok(_) => { // exists - match self + return match self .target .send(WriteToVault { location, @@ -170,7 +170,7 @@ impl Stronghold { Ok(_) => StatusMessage::OK, Err(e) => StatusMessage::Error(e.to_string()), }, - Err(e) => return StatusMessage::Error(e.to_string()), + Err(e) => StatusMessage::Error(e.to_string()), }; } Err(_) => {