sc_rpc_api/author/
error.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Authoring RPC module errors.
20
21use jsonrpsee::types::error::{ErrorObject, ErrorObjectOwned};
22use sp_runtime::transaction_validity::InvalidTransaction;
23
24/// Author RPC Result type.
25pub type Result<T> = std::result::Result<T, Error>;
26
27/// Author RPC errors.
28#[derive(Debug, thiserror::Error)]
29pub enum Error {
30	/// Client error.
31	#[error("Client error: {}", .0)]
32	Client(Box<dyn std::error::Error + Send + Sync>),
33	/// Transaction pool error,
34	#[error("Transaction pool error: {}", .0)]
35	Pool(#[from] sc_transaction_pool_api::error::Error),
36	/// Verification error
37	#[error("Extrinsic verification error: {}", .0)]
38	Verification(Box<dyn std::error::Error + Send + Sync>),
39	/// Incorrect extrinsic format.
40	#[error("Invalid extrinsic format: {}", .0)]
41	BadFormat(#[from] codec::Error),
42	/// Key type ID has an unknown format.
43	#[error("Invalid key type ID format (should be of length four)")]
44	BadKeyType,
45	/// Some random issue with the key store. Shouldn't happen.
46	#[error("The key store is unavailable")]
47	KeystoreUnavailable,
48	/// Invalid session keys encoding.
49	#[error("Session keys are not encoded correctly")]
50	InvalidSessionKeys,
51	/// `SessionKeys` runtime api missing.
52	#[error("`SessionKeys` runtime api not present in the runtime")]
53	MissingSessionKeysApi,
54	/// Call to an unsafe RPC was denied.
55	#[error(transparent)]
56	UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
57}
58
59/// Base code for all authorship errors.
60const BASE_ERROR: i32 = crate::error::base::AUTHOR;
61/// Extrinsic has an invalid format.
62const BAD_FORMAT: i32 = BASE_ERROR + 1;
63/// Error during transaction verification in runtime.
64const VERIFICATION_ERROR: i32 = BASE_ERROR + 2;
65
66/// Pool rejected the transaction as invalid
67const POOL_INVALID_TX: i32 = BASE_ERROR + 10;
68/// Cannot determine transaction validity.
69const POOL_UNKNOWN_VALIDITY: i32 = POOL_INVALID_TX + 1;
70/// The transaction is temporarily banned.
71const POOL_TEMPORARILY_BANNED: i32 = POOL_INVALID_TX + 2;
72/// The transaction is already in the pool
73const POOL_ALREADY_IMPORTED: i32 = POOL_INVALID_TX + 3;
74/// Transaction has too low priority to replace existing one in the pool.
75const POOL_TOO_LOW_PRIORITY: i32 = POOL_INVALID_TX + 4;
76/// Including this transaction would cause a dependency cycle.
77const POOL_CYCLE_DETECTED: i32 = POOL_INVALID_TX + 5;
78/// The transaction was not included to the pool because of the limits.
79const POOL_IMMEDIATELY_DROPPED: i32 = POOL_INVALID_TX + 6;
80/// The transaction was not included to the pool since it is unactionable,
81/// it is not propagable and the local node does not author blocks.
82const POOL_UNACTIONABLE: i32 = POOL_INVALID_TX + 8;
83/// Transaction does not provide any tags, so the pool can't identify it.
84const POOL_NO_TAGS: i32 = POOL_INVALID_TX + 9;
85/// Invalid block ID.
86const POOL_INVALID_BLOCK_ID: i32 = POOL_INVALID_TX + 10;
87/// The pool is not accepting future transactions.
88const POOL_FUTURE_TX: i32 = POOL_INVALID_TX + 11;
89/// Other error.
90const OTHER_ERR: i32 = BASE_ERROR + 40;
91
92impl From<Error> for ErrorObjectOwned {
93	fn from(e: Error) -> ErrorObjectOwned {
94		use sc_transaction_pool_api::error::Error as PoolError;
95
96		match e {
97			Error::BadFormat(e) => ErrorObject::owned(
98				BAD_FORMAT,
99				format!("Extrinsic has invalid format: {}", e),
100				None::<()>,
101			),
102			Error::Verification(e) => ErrorObject::owned(
103				VERIFICATION_ERROR,
104				format!("Verification Error: {}", e),
105				Some(format!("{:?}", e)),
106			),
107			Error::Pool(PoolError::InvalidTransaction(InvalidTransaction::Custom(e))) => {
108				ErrorObject::owned(
109					POOL_INVALID_TX,
110					"Invalid Transaction",
111					Some(format!("Custom error: {}", e)),
112				)
113			},
114			Error::Pool(PoolError::InvalidTransaction(e)) => {
115				let msg: &str = e.into();
116				ErrorObject::owned(
117					POOL_INVALID_TX,
118					"Invalid Transaction",
119					Some(msg),
120				)
121			},
122			Error::Pool(PoolError::UnknownTransaction(e)) => {
123				ErrorObject::owned(
124					POOL_UNKNOWN_VALIDITY,
125					"Unknown Transaction Validity",
126					Some(format!("{:?}", e)),
127				)
128			},
129			Error::Pool(PoolError::TemporarilyBanned) =>
130				ErrorObject::owned(
131				POOL_TEMPORARILY_BANNED,
132				"Transaction is temporarily banned",
133				None::<()>,
134			),
135			Error::Pool(PoolError::AlreadyImported(hash)) =>
136				ErrorObject::owned(
137				POOL_ALREADY_IMPORTED,
138				"Transaction Already Imported",
139				Some(format!("{:?}", hash)),
140			),
141			Error::Pool(PoolError::TooLowPriority { old, new }) => ErrorObject::owned(
142				POOL_TOO_LOW_PRIORITY,
143				format!("Priority is too low: ({} vs {})", old, new),
144				Some("The transaction has too low priority to replace another transaction already in the pool.")
145			),
146			Error::Pool(PoolError::CycleDetected) =>
147				ErrorObject::owned(
148				POOL_CYCLE_DETECTED,
149				"Cycle Detected",
150				None::<()>
151			),
152			Error::Pool(PoolError::ImmediatelyDropped) => ErrorObject::owned(
153				POOL_IMMEDIATELY_DROPPED,
154				"Immediately Dropped",
155				Some("The transaction couldn't enter the pool because of the limit"),
156			),
157			Error::Pool(PoolError::Unactionable) => ErrorObject::owned(
158				POOL_UNACTIONABLE,
159				"Unactionable",
160				Some("The transaction is unactionable since it is not propagable and \
161				the local node does not author blocks")
162			),
163			Error::Pool(PoolError::NoTagsProvided) => ErrorObject::owned(
164				POOL_NO_TAGS,
165				"No tags provided",
166				Some("Transaction does not provide any tags, so the pool can't identify it")
167			),
168			Error::Pool(PoolError::InvalidBlockId(_)) =>
169				ErrorObject::owned(
170				POOL_INVALID_BLOCK_ID,
171				"The provided block ID is not valid",
172				None::<()>
173			),
174			Error::Pool(PoolError::RejectedFutureTransaction) => {
175				ErrorObject::owned(
176					POOL_FUTURE_TX,
177					"The pool is not accepting future transactions",
178					None::<()>,
179				)
180			},
181			Error::UnsafeRpcCalled(e) => e.into(),
182			other => ErrorObject::owned(
183				OTHER_ERR,
184				other.to_string(),
185				None::<()>,
186			)
187		}
188	}
189}