diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index 4508f84eefc38..4fb7d9c7737fb 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -450,7 +450,7 @@ pub type DispatchResult = sp_std::result::Result<(), DispatchError>; pub type DispatchResultWithInfo = sp_std::result::Result>; /// Reason why a dispatch call failed. -#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)] +#[derive(Eq, Clone, Copy, Encode, Decode, RuntimeDebug)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] pub enum DispatchError { /// Some error occurred. @@ -589,6 +589,27 @@ impl traits::Printable for DispatchErrorWithPostInfo where } } +impl PartialEq for DispatchError { + fn eq(&self, other: &Self) -> bool { + use DispatchError::*; + + match (self, other) { + (CannotLookup, CannotLookup) | + (BadOrigin, BadOrigin) | + (ConsumerRemaining, ConsumerRemaining) | + (NoProviders, NoProviders) => true, + + (Other(l), Other(r)) => l == r, + ( + Module { index: index_l, error: error_l, .. }, + Module { index: index_r, error: error_r, .. }, + ) => (index_l == index_r) && (error_l == error_r), + + _ => false, + } + } +} + /// This type specifies the outcome of dispatching a call to a module. /// /// In case of failure an error specific to the module is returned. @@ -826,6 +847,38 @@ mod tests { ); } + #[test] + fn dispatch_error_equality() { + use DispatchError::*; + + let variants = vec![ + Other("foo"), + Other("bar"), + CannotLookup, + BadOrigin, + Module { index: 1, error: 1, message: None }, + Module { index: 1, error: 2, message: None }, + Module { index: 2, error: 1, message: None }, + ConsumerRemaining, + NoProviders, + ]; + for (i, variant) in variants.iter().enumerate() { + for (j, other_variant) in variants.iter().enumerate() { + if i == j { + assert_eq!(variant, other_variant); + } else { + assert_ne!(variant, other_variant); + } + } + } + + // Ignores `message` field in `Module` variant. + assert_eq!( + Module { index: 1, error: 1, message: Some("foo") }, + Module { index: 1, error: 1, message: None}, + ); + } + #[test] fn multi_signature_ecdsa_verify_works() { let msg = &b"test-message"[..];