Skip to content

Commit

Permalink
Return error when call was reverted (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Apr 3, 2024
1 parent c8cea6b commit 95223ce
Show file tree
Hide file tree
Showing 14 changed files with 3,947 additions and 4,065 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/Cardinal-Cryptography/drink"
version = "0.16.0"
version = "0.17.0"

[workspace.dependencies]
anyhow = { version = "1.0.71" }
Expand Down Expand Up @@ -51,5 +51,5 @@ sp-runtime-interface = { version = "26.0.0" }

# Local dependencies

drink = { version = "=0.16.0", path = "drink" }
drink-test-macro = { version = "=0.16.0", path = "drink/test-macro" }
drink = { version = "=0.17.0", path = "drink" }
drink-test-macro = { version = "=0.17.0", path = "drink/test-macro" }
2 changes: 1 addition & 1 deletion drink/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use thiserror::Error;

/// Main error type for the drink crate.
#[derive(Error, Debug)]
#[derive(Clone, Error, Debug)]
pub enum Error {
/// Externalities could not be initialized.
#[error("Failed to build storage: {0}")]
Expand Down
20 changes: 19 additions & 1 deletion drink/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,22 @@ where
self.call_internal::<_, V>(None, message, args, endowment)
}

/// Calls the last deployed contract. Expect it to be reverted and the message result to be of
/// type `Result<_, E>`.
pub fn call_and_expect_error<S: AsRef<str> + Debug, E: Debug + Decode>(
&mut self,
message: &str,
args: &[S],
endowment: Option<BalanceOf<T::Runtime>>,
) -> Result<E, SessionError> {
Ok(self
.call_internal::<_, Result<(), E>>(None, message, args, endowment)
.expect_err("Call should fail")
.decode_revert::<Result<(), E>>()?
.expect("Call should return an error")
.expect_err("Call should return an error"))
}

/// Calls a contract with a given address. In case of a successful call, returns the encoded
/// result.
pub fn call_with_address<S: AsRef<str> + Debug, V: Decode>(
Expand Down Expand Up @@ -542,7 +558,9 @@ where
});

let ret = match &result.result {
Ok(exec_result) if exec_result.did_revert() => Err(SessionError::CallReverted),
Ok(exec_result) if exec_result.did_revert() => {
Err(SessionError::CallReverted(exec_result.data.clone()))
}
Ok(exec_result) => {
self.record.push_call_return(exec_result.data.clone());
self.record.last_call_return_decoded::<V>()
Expand Down
21 changes: 18 additions & 3 deletions drink/src/session/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! Module exposing errors and result types for the session API.

use frame_support::sp_runtime::DispatchError;
use parity_scale_codec::Decode;
use thiserror::Error;

use crate::errors::MessageResult;

/// Session specific errors.
#[derive(Error, Debug)]
#[derive(Clone, Error, Debug)]
pub enum SessionError {
/// Encoding data failed.
#[error("Encoding call data failed: {0}")]
Expand All @@ -25,8 +28,8 @@ pub enum SessionError {
#[error("Code upload failed: {0:?}")]
UploadFailed(DispatchError),
/// Call has been reverted by the contract.
#[error("Contract call has been reverted")]
CallReverted,
#[error("Contract call has been reverted. Encoded error: {0:?}")]
CallReverted(Vec<u8>),
/// Contract call failed (aborted by the pallet).
#[error("Contract call failed before execution: {0:?}")]
CallFailed(DispatchError),
Expand All @@ -37,3 +40,15 @@ pub enum SessionError {
#[error("Missing transcoder")]
NoTranscoder,
}

impl SessionError {
/// Check if the error is a revert error and if so, decode the error message.
pub fn decode_revert<T: Decode>(&self) -> Result<MessageResult<T>, Self> {
match self {
SessionError::CallReverted(error) => {
Ok(MessageResult::decode(&mut &error[..]).expect("Failed to decode error"))
}
_ => Err(self.clone()),
}
}
}
Loading

0 comments on commit 95223ce

Please sign in to comment.