Skip to content

Commit bac7afd

Browse files
committed
Adds patch to make DispatchError compatible with old runtimes
1 parent ea9becf commit bac7afd

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

client/rpc/src/eth/execute.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,93 @@ use crate::{
5050
/// Default JSONRPC error code return by geth
5151
pub const JSON_RPC_ERROR_DEFAULT: i32 = -32000;
5252

53+
/// The types contained in this module are required for backwards compatility when decoding
54+
/// results produced by old versions of substrate.
55+
/// The changes contained in https://github.com/paritytech/substrate/pull/10776 changed the
56+
/// serde encoding for variant `DispatchError::Module`.
57+
mod old_types {
58+
use scale_codec::{Decode, Encode};
59+
#[cfg(feature = "std")]
60+
pub use serde::{de::DeserializeOwned, Deserialize, Serialize};
61+
62+
/// Description of what went wrong when trying to complete an operation on a token.
63+
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug)]
64+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
65+
pub enum TokenError {
66+
/// Funds are unavailable.
67+
NoFunds,
68+
/// Account that must exist would die.
69+
WouldDie,
70+
/// Account cannot exist with the funds that would be given.
71+
BelowMinimum,
72+
/// Account cannot be created.
73+
CannotCreate,
74+
/// The asset in question is unknown.
75+
UnknownAsset,
76+
/// Funds exist but are frozen.
77+
Frozen,
78+
/// Operation is not supported by the asset.
79+
Unsupported,
80+
}
81+
82+
/// Arithmetic errors.
83+
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug)]
84+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
85+
pub enum ArithmeticError {
86+
/// Underflow.
87+
Underflow,
88+
/// Overflow.
89+
Overflow,
90+
/// Division by zero.
91+
DivisionByZero,
92+
}
93+
94+
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, Debug)]
95+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
96+
#[cfg_attr(feature = "std", serde(untagged))]
97+
pub enum DispatchErrorLegacy {
98+
/// Some error occurred.
99+
Other(
100+
#[codec(skip)]
101+
#[cfg_attr(feature = "std", serde(skip_deserializing))]
102+
&'static str,
103+
),
104+
/// Failed to lookup some data.
105+
CannotLookup,
106+
/// A bad origin.
107+
BadOrigin,
108+
/// A custom error in a module.
109+
Module {
110+
/// Module index, matching the metadata module index.
111+
index: u8,
112+
/// Module specific error value.
113+
error: u8,
114+
/// Optional error message.
115+
#[codec(skip)]
116+
#[cfg_attr(feature = "std", serde(skip_deserializing))]
117+
message: Option<&'static str>,
118+
},
119+
/// At least one consumer is remaining so the account cannot be destroyed.
120+
ConsumerRemaining,
121+
/// There are no providers so the account cannot be created.
122+
NoProviders,
123+
/// There are too many consumers so the account cannot be created.
124+
TooManyConsumers,
125+
/// An error to do with tokens.
126+
Token(TokenError),
127+
/// An arithmetic error.
128+
Arithmetic(ArithmeticError),
129+
}
130+
131+
/// Reason why a dispatch call failed.
132+
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, Debug)]
133+
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
134+
pub enum DispatchError {
135+
V1(DispatchErrorLegacy),
136+
V2(sp_runtime::DispatchError),
137+
}
138+
}
139+
53140
/// Allow to adapt a request for `estimate_gas`.
54141
/// Can be used to estimate gas of some contracts using a different function
55142
/// in the case the normal gas estimation doesn't work.
@@ -104,7 +191,11 @@ where
104191
(
105192
details.gas_price,
106193
// Old runtimes require max_fee_per_gas to be None for non transactional calls.
107-
if details.max_fee_per_gas == Some(U256::zero()) { None } else { details.max_fee_per_gas },
194+
if details.max_fee_per_gas == Some(U256::zero()) {
195+
None
196+
} else {
197+
details.max_fee_per_gas
198+
},
108199
details.max_priority_fee_per_gas,
109200
)
110201
};
@@ -259,7 +350,7 @@ where
259350
.call_api_at(params)
260351
.and_then(|r| {
261352
Result::map_err(
262-
<Result<ExecutionInfo::<Vec<u8>>, DispatchError> as Decode>::decode(&mut &r[..]),
353+
<Result<ExecutionInfo::<Vec<u8>>, old_types::DispatchError> as Decode>::decode(&mut &r[..]),
263354
|error| sp_api::ApiError::FailedToDecodeReturnValue {
264355
function: "EthereumRuntimeRPCApi_call",
265356
error,

0 commit comments

Comments
 (0)