Skip to content

Commit

Permalink
Fix decoding of LangError (#919)
Browse files Browse the repository at this point in the history
* Decode enum variants using discriminant

* Clippy
  • Loading branch information
ascjones authored Jan 25, 2023
1 parent 2e8e4eb commit 30ba1ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion crates/cargo-contract/src/cmd/extrinsics/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use contract_build::name_value_println;

use anyhow::{
anyhow,
Context,
Result,
};

Expand Down Expand Up @@ -112,7 +113,11 @@ impl CallCommand {
match result.result {
Ok(ref ret_val) => {
let value = transcoder
.decode_return(&self.message, &mut &ret_val.data[..])?;
.decode_return(&self.message, &mut &ret_val.data[..])
.context(format!(
"Failed to decode return value {:?}",
&ret_val
))?;
let dry_run_result = CallDryRunResult {
result: String::from("Success!"),
reverted: ret_val.did_revert(),
Expand Down
3 changes: 2 additions & 1 deletion crates/transcode/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ impl<'a> Decoder<'a> {
let discriminant = input.read_byte()?;
let variant = variant_type
.variants()
.get(discriminant as usize)
.iter()
.find(|v| v.index == discriminant)
.ok_or_else(|| {
anyhow::anyhow!("No variant found with discriminant {}", discriminant)
})?;
Expand Down
24 changes: 24 additions & 0 deletions crates/transcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,30 @@ mod tests {
assert_eq!(expected, decoded);
}

#[test]
fn decode_lang_error() {
use ink::primitives::LangError;

let metadata = generate_metadata();
let transcoder = ContractMessageTranscoder::new(metadata);

let encoded =
Result::<bool, LangError>::Err(LangError::CouldNotReadInput).encode();
let decoded = transcoder
.decode_return("get", &mut &encoded[..])
.unwrap_or_else(|e| panic!("Error decoding return value {}", e));

let expected = Value::Tuple(Tuple::new(
"Err".into(),
[Value::Tuple(Tuple::new(
Some("CouldNotReadInput"),
Vec::new(),
))]
.to_vec(),
));
assert_eq!(expected, decoded);
}

#[test]
fn decode_contract_event() -> Result<()> {
let metadata = generate_metadata();
Expand Down

0 comments on commit 30ba1ec

Please sign in to comment.