From f3b919b31d72094e9e309be700e85fd602c5faa6 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 4 May 2022 12:04:32 +0200 Subject: [PATCH 1/6] Make Unknown error message non-optional --- packages/vm/src/backend.rs | 22 ++++------------------ packages/vm/src/errors/vm_error.rs | 2 +- packages/vm/src/imports.rs | 10 +++------- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/packages/vm/src/backend.rs b/packages/vm/src/backend.rs index eae0a51462..c0c843af97 100644 --- a/packages/vm/src/backend.rs +++ b/packages/vm/src/backend.rs @@ -169,8 +169,8 @@ pub enum BackendError { IteratorDoesNotExist { id: u32 }, #[error("Ran out of gas during call into backend")] OutOfGas {}, - #[error("Unknown error during call into backend: {msg:?}")] - Unknown { msg: Option }, + #[error("Unknown error during call into backend: {msg}")] + Unknown { msg: String }, // This is the only error case of BackendError that is reported back to the contract. #[error("User error during call into backend: {msg}")] UserErr { msg: String }, @@ -195,15 +195,10 @@ impl BackendError { pub fn unknown(msg: impl ToString) -> Self { BackendError::Unknown { - msg: Some(msg.to_string()), + msg: msg.to_string(), } } - /// Use `::unknown(msg: S)` if possible - pub fn unknown_without_message() -> Self { - BackendError::Unknown { msg: None } - } - pub fn user_err(msg: impl ToString) -> Self { BackendError::UserErr { msg: msg.to_string(), @@ -347,16 +342,7 @@ mod tests { fn ffi_error_unknown() { let error = BackendError::unknown("broken"); match error { - BackendError::Unknown { msg, .. } => assert_eq!(msg.unwrap(), "broken"), - e => panic!("Unexpected error: {:?}", e), - } - } - - #[test] - fn ffi_error_unknown_without_message() { - let error = BackendError::unknown_without_message(); - match error { - BackendError::Unknown { msg, .. } => assert!(msg.is_none()), + BackendError::Unknown { msg, .. } => assert_eq!(msg, "broken"), e => panic!("Unexpected error: {:?}", e), } } diff --git a/packages/vm/src/errors/vm_error.rs b/packages/vm/src/errors/vm_error.rs index ba06982788..7da6b00a85 100644 --- a/packages/vm/src/errors/vm_error.rs +++ b/packages/vm/src/errors/vm_error.rs @@ -385,7 +385,7 @@ mod tests { VmError::BackendErr { source: BackendError::Unknown { msg }, .. - } => assert_eq!(msg.unwrap(), "something went wrong"), + } => assert_eq!(msg, "something went wrong"), e => panic!("Unexpected error: {:?}", e), } } diff --git a/packages/vm/src/imports.rs b/packages/vm/src/imports.rs index 922717e9c2..c183f2511d 100644 --- a/packages/vm/src/imports.rs +++ b/packages/vm/src/imports.rs @@ -896,9 +896,7 @@ mod tests { VmError::BackendErr { source: BackendError::Unknown { msg, .. }, .. - } => { - assert_eq!(msg.unwrap(), "Temporarily unavailable"); - } + } => assert_eq!(msg, "Temporarily unavailable"), err => panic!("Incorrect error returned: {:?}", err), } } @@ -989,9 +987,7 @@ mod tests { VmError::BackendErr { source: BackendError::Unknown { msg, .. }, .. - } => { - assert_eq!(msg.unwrap(), "Temporarily unavailable"); - } + } => assert_eq!(msg, "Temporarily unavailable"), err => panic!("Incorrect error returned: {:?}", err), } } @@ -1093,7 +1089,7 @@ mod tests { VmError::BackendErr { source: BackendError::Unknown { msg, .. }, .. - } => assert_eq!(msg.unwrap(), "Temporarily unavailable"), + } => assert_eq!(msg, "Temporarily unavailable"), err => panic!("Incorrect error returned: {:?}", err), }; } From 0bd0e8ae5ff80e8f88aba067a69cf9726a247d69 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 4 May 2022 12:05:26 +0200 Subject: [PATCH 2/6] Replace ToString with Into --- packages/vm/src/backend.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/vm/src/backend.rs b/packages/vm/src/backend.rs index c0c843af97..269ce7a40c 100644 --- a/packages/vm/src/backend.rs +++ b/packages/vm/src/backend.rs @@ -193,16 +193,12 @@ impl BackendError { BackendError::OutOfGas {} } - pub fn unknown(msg: impl ToString) -> Self { - BackendError::Unknown { - msg: msg.to_string(), - } + pub fn unknown(msg: impl Into) -> Self { + BackendError::Unknown { msg: msg.into() } } - pub fn user_err(msg: impl ToString) -> Self { - BackendError::UserErr { - msg: msg.to_string(), - } + pub fn user_err(msg: impl Into) -> Self { + BackendError::UserErr { msg: msg.into() } } } From ce15166b9c84574979e5ded4cf9cc3b33d647922 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 4 May 2022 12:06:18 +0200 Subject: [PATCH 3/6] Implement PartialEq for BackendError --- packages/vm/src/backend.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vm/src/backend.rs b/packages/vm/src/backend.rs index 269ce7a40c..9863f99fba 100644 --- a/packages/vm/src/backend.rs +++ b/packages/vm/src/backend.rs @@ -157,7 +157,7 @@ pub trait Querier { /// attached. pub type BackendResult = (core::result::Result, GasInfo); -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum BackendError { #[error("Panic in FFI call")] ForeignPanic {}, From a59376d74bd9ad9365441ad2aa03037bad4a97f7 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 4 May 2022 12:07:21 +0200 Subject: [PATCH 4/6] Replace ffi_error_ with backend_err_ in test names --- packages/vm/src/backend.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/vm/src/backend.rs b/packages/vm/src/backend.rs index 9863f99fba..d60a9169d2 100644 --- a/packages/vm/src/backend.rs +++ b/packages/vm/src/backend.rs @@ -299,7 +299,7 @@ mod tests { // constructors #[test] - fn ffi_error_foreign_panic() { + fn backend_err_foreign_panic() { let error = BackendError::foreign_panic(); match error { BackendError::ForeignPanic { .. } => {} @@ -308,7 +308,7 @@ mod tests { } #[test] - fn ffi_error_bad_argument() { + fn backend_err_bad_argument() { let error = BackendError::bad_argument(); match error { BackendError::BadArgument { .. } => {} @@ -326,7 +326,7 @@ mod tests { } #[test] - fn ffi_error_out_of_gas() { + fn backend_err_out_of_gas() { let error = BackendError::out_of_gas(); match error { BackendError::OutOfGas { .. } => {} @@ -335,7 +335,7 @@ mod tests { } #[test] - fn ffi_error_unknown() { + fn backend_err_unknown() { let error = BackendError::unknown("broken"); match error { BackendError::Unknown { msg, .. } => assert_eq!(msg, "broken"), @@ -344,7 +344,7 @@ mod tests { } #[test] - fn ffi_error_user_err() { + fn backend_err_user_err() { let error = BackendError::user_err("invalid input"); match error { BackendError::UserErr { msg, .. } => assert_eq!(msg, "invalid input"), From 8b77152b1aa2f75933f23edbe2d258c270214a94 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 5 May 2022 16:47:28 +0200 Subject: [PATCH 5/6] Make BackendError non_exhaustive --- packages/vm/src/backend.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/vm/src/backend.rs b/packages/vm/src/backend.rs index d60a9169d2..ca29b345c4 100644 --- a/packages/vm/src/backend.rs +++ b/packages/vm/src/backend.rs @@ -158,6 +158,7 @@ pub trait Querier { pub type BackendResult = (core::result::Result, GasInfo); #[derive(Error, Debug, PartialEq)] +#[non_exhaustive] pub enum BackendError { #[error("Panic in FFI call")] ForeignPanic {}, From 6518742b2fbd0419f022f1c9454687fe879c646c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 5 May 2022 19:15:18 +0200 Subject: [PATCH 6/6] Add CHANGELOG entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec28bd71a2..c266e26a62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,11 @@ and this project adheres to - all: Updated Rust edition to 2021 - cosmwasm-std: Rename `SubMsgExecutionResponse` to `SubMsgResponse`. - cosmwasm-crypto: Update dependency `k256` to ^0.10.4. +- cosmwasm-vm: `BackendError` was changed to `non_exhaustive` for future + extension; `BackendError` now implements `PartialEq` for easier test code; the + `msg` in `BackendError::Unknown` became non-optional because it was always + set; the argument in `BackendError::unknown`/`::user_err` was change to + `impl Into` to avoid unnecessary clones. ### Deprecated