Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Apr 24, 2024
1 parent 50dc4b1 commit a5f2a2e
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions client/rpc/src/eth/execute.rs
Expand Up @@ -55,10 +55,10 @@ pub const JSON_RPC_ERROR_DEFAULT: i32 = -32000;
/// The changes contained in https://github.com/paritytech/substrate/pull/10776 changed the
/// serde encoding for variant `DispatchError::Module`.
mod old_types {
use scale_codec::Decode;
use scale_codec::{Decode, Encode};

/// Description of what went wrong when trying to complete an operation on a token.
#[derive(Eq, PartialEq, Clone, Copy, Decode, Debug)]
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug)]
pub enum TokenError {
/// Funds are unavailable.
NoFunds,
Expand All @@ -77,7 +77,7 @@ mod old_types {
}

/// Arithmetic errors.
#[derive(Eq, PartialEq, Clone, Copy, Decode, Debug)]
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug)]
pub enum ArithmeticError {
/// Underflow.
Underflow,
Expand All @@ -87,7 +87,7 @@ mod old_types {
DivisionByZero,
}

#[derive(PartialEq, Eq, Clone, Copy, Decode, Debug)]
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, Debug)]
pub enum DispatchErrorV1 {
/// Some error occurred.
Other(
Expand Down Expand Up @@ -120,7 +120,7 @@ mod old_types {
Arithmetic(ArithmeticError),
}

#[derive(PartialEq, Eq, Clone, Copy, Decode, Debug)]
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, Debug)]
pub enum DispatchErrorV2 {
/// Some error occurred.
Other(
Expand Down Expand Up @@ -164,14 +164,17 @@ mod old_types {

impl Decode for DispatchError {
fn decode<I: scale_codec::Input>(input: &mut I) -> Result<Self, scale_codec::Error> {
if let Ok(r) = DispatchErrorV1::decode(input) {
let remaining = input.remaining_len()?;
let mut v = vec![0u8; remaining.unwrap_or(0)];
let _ = input.read(&mut v);

if let Ok(r) = DispatchErrorV1::decode(&mut v.as_slice()) {
return Ok(DispatchError::V1(r));
}
if let Ok(r) = DispatchErrorV2::decode(input) {
return Ok(DispatchError::V2(r));
}

Err("Could not decode DispatchError".into())
Ok(DispatchError::V2(DispatchErrorV2::decode(
&mut v.as_slice(),
)?))
}
}
}
Expand Down Expand Up @@ -1184,3 +1187,31 @@ fn fee_details(
}),
}
}

#[cfg(test)]
mod tests {
use super::old_types;
use scale_codec::{Decode, Encode};

#[test]
fn test_dispatch_error() {
let v1 = old_types::DispatchErrorV1::Module {
index: 1,
error: 1,
message: None,
};
let v2 = old_types::DispatchErrorV2::TooManyConsumers;

let encoded_v1 = v1.encode();
let encoded_v2 = v2.encode();

assert_eq!(
old_types::DispatchError::decode(&mut encoded_v1.as_slice()),
Ok(old_types::DispatchError::V1(v1))
);
assert_eq!(
old_types::DispatchError::decode(&mut encoded_v2.as_slice()),
Ok(old_types::DispatchError::V2(v2))
);
}
}

0 comments on commit a5f2a2e

Please sign in to comment.