Skip to content

Commit

Permalink
decode DispatchInfo on ExtrinsicFailed events
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Nov 5, 2020
1 parent 7655caa commit eba9fbf
Showing 1 changed file with 102 additions and 2 deletions.
104 changes: 102 additions & 2 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ impl<T: System> EventsDecoder<T> {
input: &mut I,
output: &mut W,
) -> Result<(), Error> {
for arg in args {
let mut iter = args.iter().peekable();
for arg in iter.next() {
match arg {
EventArg::Vec(arg) => {
let len = <Compact<u32>>::decode(input)?;
Expand All @@ -181,7 +182,20 @@ impl<T: System> EventsDecoder<T> {
EventArg::Primitive(name) => {
let result = match name.as_str() {
"DispatchResult" => DispatchResult::decode(input)?,
"DispatchError" => Err(DispatchError::decode(input)?),
"DispatchError" => {
let error = Err(DispatchError::decode(input)?);
if let Some(EventArg::Primitive(name)) = iter.peek() {
match name.as_str() {
"DispatchInfo" => {
// since we return `DispatchError` as an `Err` we may skip the
// `DispatchInfo` within `ExtrinsicFailedEvent` so decode this
DispatchInfo::decode(input)?;
}
_ => (),
};
}
error
}
_ => {
if let Some(size) = self.type_sizes.get(name) {
let mut buf = vec![0; *size];
Expand Down Expand Up @@ -255,6 +269,7 @@ impl<T: System> EventsDecoder<T> {
}
}

#[derive(Debug)]
pub enum Raw {
Event(RawEvent),
Error(RuntimeError),
Expand All @@ -263,6 +278,18 @@ pub enum Raw {
#[cfg(test)]
mod tests {
use super::*;
use frame_metadata::{
DecodeDifferent,
ErrorMetadata,
EventMetadata,
ExtrinsicMetadata,
ModuleMetadata,
RuntimeMetadata,
RuntimeMetadataPrefixed,
RuntimeMetadataV12,
META_RESERVED,
};
use std::convert::TryFrom;

type TestRuntime = crate::NodeTemplateRuntime;

Expand All @@ -286,4 +313,77 @@ mod tests {

assert_eq!(output, vec![1, 0]);
}

#[test]
fn test_decode_system_events_and_error() {
let decoder = EventsDecoder::<TestRuntime>::new(
Metadata::try_from(RuntimeMetadataPrefixed(
META_RESERVED,
RuntimeMetadata::V12(RuntimeMetadataV12 {
modules: DecodeDifferent::Decoded(vec![ModuleMetadata {
name: DecodeDifferent::Decoded("System".to_string()),
storage: None,
calls: None,
event: Some(DecodeDifferent::Decoded(vec![
EventMetadata {
name: DecodeDifferent::Decoded(
"ExtrinsicSuccess".to_string(),
),
arguments: DecodeDifferent::Decoded(vec![
"DispatchInfo".to_string()
]),
documentation: DecodeDifferent::Decoded(vec![]),
},
EventMetadata {
name: DecodeDifferent::Decoded(
"ExtrinsicFailed".to_string(),
),
arguments: DecodeDifferent::Decoded(vec![
"DispatchError".to_string(),
"DispatchInfo".to_string(),
]),
documentation: DecodeDifferent::Decoded(vec![]),
},
])),
constants: DecodeDifferent::Decoded(vec![]),
errors: DecodeDifferent::Decoded(vec![
ErrorMetadata {
name: DecodeDifferent::Decoded(
"InvalidSpecName".to_string(),
),
documentation: DecodeDifferent::Decoded(vec![]),
},
ErrorMetadata {
name: DecodeDifferent::Decoded(
"SpecVersionNeedsToIncrease".to_string(),
),
documentation: DecodeDifferent::Decoded(vec![]),
},
ErrorMetadata {
name: DecodeDifferent::Decoded(
"FailedToExtractRuntimeVersion".to_string(),
),
documentation: DecodeDifferent::Decoded(vec![]),
},
ErrorMetadata {
name: DecodeDifferent::Decoded(
"NonDefaultComposite".to_string(),
),
documentation: DecodeDifferent::Decoded(vec![]),
},
]),
index: 0,
}]),
extrinsic: ExtrinsicMetadata {
version: 0,
signed_extensions: vec![],
},
}),
))
.unwrap(),
);

let input = hex::decode("0c00000000000000482d7c0900000000020000000100000000010300035884723300000000000000000200000000010300035884723300000000000000").unwrap();
decoder.decode_events(&mut &input[..]).unwrap();
}
}

0 comments on commit eba9fbf

Please sign in to comment.