Skip to content

Commit 95223ce

Browse files
Return error when call was reverted (#121)
1 parent c8cea6b commit 95223ce

File tree

14 files changed

+3947
-4065
lines changed

14 files changed

+3947
-4065
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
1818
license = "Apache-2.0"
1919
readme = "README.md"
2020
repository = "https://github.com/Cardinal-Cryptography/drink"
21-
version = "0.16.0"
21+
version = "0.17.0"
2222

2323
[workspace.dependencies]
2424
anyhow = { version = "1.0.71" }
@@ -51,5 +51,5 @@ sp-runtime-interface = { version = "26.0.0" }
5151

5252
# Local dependencies
5353

54-
drink = { version = "=0.16.0", path = "drink" }
55-
drink-test-macro = { version = "=0.16.0", path = "drink/test-macro" }
54+
drink = { version = "=0.17.0", path = "drink" }
55+
drink-test-macro = { version = "=0.17.0", path = "drink/test-macro" }

drink/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use thiserror::Error;
44

55
/// Main error type for the drink crate.
6-
#[derive(Error, Debug)]
6+
#[derive(Clone, Error, Debug)]
77
pub enum Error {
88
/// Externalities could not be initialized.
99
#[error("Failed to build storage: {0}")]

drink/src/session.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,22 @@ where
463463
self.call_internal::<_, V>(None, message, args, endowment)
464464
}
465465

466+
/// Calls the last deployed contract. Expect it to be reverted and the message result to be of
467+
/// type `Result<_, E>`.
468+
pub fn call_and_expect_error<S: AsRef<str> + Debug, E: Debug + Decode>(
469+
&mut self,
470+
message: &str,
471+
args: &[S],
472+
endowment: Option<BalanceOf<T::Runtime>>,
473+
) -> Result<E, SessionError> {
474+
Ok(self
475+
.call_internal::<_, Result<(), E>>(None, message, args, endowment)
476+
.expect_err("Call should fail")
477+
.decode_revert::<Result<(), E>>()?
478+
.expect("Call should return an error")
479+
.expect_err("Call should return an error"))
480+
}
481+
466482
/// Calls a contract with a given address. In case of a successful call, returns the encoded
467483
/// result.
468484
pub fn call_with_address<S: AsRef<str> + Debug, V: Decode>(
@@ -542,7 +558,9 @@ where
542558
});
543559

544560
let ret = match &result.result {
545-
Ok(exec_result) if exec_result.did_revert() => Err(SessionError::CallReverted),
561+
Ok(exec_result) if exec_result.did_revert() => {
562+
Err(SessionError::CallReverted(exec_result.data.clone()))
563+
}
546564
Ok(exec_result) => {
547565
self.record.push_call_return(exec_result.data.clone());
548566
self.record.last_call_return_decoded::<V>()

drink/src/session/error.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//! Module exposing errors and result types for the session API.
22
33
use frame_support::sp_runtime::DispatchError;
4+
use parity_scale_codec::Decode;
45
use thiserror::Error;
56

7+
use crate::errors::MessageResult;
8+
69
/// Session specific errors.
7-
#[derive(Error, Debug)]
10+
#[derive(Clone, Error, Debug)]
811
pub enum SessionError {
912
/// Encoding data failed.
1013
#[error("Encoding call data failed: {0}")]
@@ -25,8 +28,8 @@ pub enum SessionError {
2528
#[error("Code upload failed: {0:?}")]
2629
UploadFailed(DispatchError),
2730
/// Call has been reverted by the contract.
28-
#[error("Contract call has been reverted")]
29-
CallReverted,
31+
#[error("Contract call has been reverted. Encoded error: {0:?}")]
32+
CallReverted(Vec<u8>),
3033
/// Contract call failed (aborted by the pallet).
3134
#[error("Contract call failed before execution: {0:?}")]
3235
CallFailed(DispatchError),
@@ -37,3 +40,15 @@ pub enum SessionError {
3740
#[error("Missing transcoder")]
3841
NoTranscoder,
3942
}
43+
44+
impl SessionError {
45+
/// Check if the error is a revert error and if so, decode the error message.
46+
pub fn decode_revert<T: Decode>(&self) -> Result<MessageResult<T>, Self> {
47+
match self {
48+
SessionError::CallReverted(error) => {
49+
Ok(MessageResult::decode(&mut &error[..]).expect("Failed to decode error"))
50+
}
51+
_ => Err(self.clone()),
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)