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	/// Call to an unsafe RPC was denied.
52	#[error(transparent)]
53	UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
54}
55
56/// Base code for all authorship errors.
57const BASE_ERROR: i32 = crate::error::base::AUTHOR;
58/// Extrinsic has an invalid format.
59const BAD_FORMAT: i32 = BASE_ERROR + 1;
60/// Error during transaction verification in runtime.
61const VERIFICATION_ERROR: i32 = BASE_ERROR + 2;
62
63/// Pool rejected the transaction as invalid
64const POOL_INVALID_TX: i32 = BASE_ERROR + 10;
65/// Cannot determine transaction validity.
66const POOL_UNKNOWN_VALIDITY: i32 = POOL_INVALID_TX + 1;
67/// The transaction is temporarily banned.
68const POOL_TEMPORARILY_BANNED: i32 = POOL_INVALID_TX + 2;
69/// The transaction is already in the pool
70const POOL_ALREADY_IMPORTED: i32 = POOL_INVALID_TX + 3;
71/// Transaction has too low priority to replace existing one in the pool.
72const POOL_TOO_LOW_PRIORITY: i32 = POOL_INVALID_TX + 4;
73/// Including this transaction would cause a dependency cycle.
74const POOL_CYCLE_DETECTED: i32 = POOL_INVALID_TX + 5;
75/// The transaction was not included to the pool because of the limits.
76const POOL_IMMEDIATELY_DROPPED: i32 = POOL_INVALID_TX + 6;
77/// The transaction was not included to the pool since it is unactionable,
78/// it is not propagable and the local node does not author blocks.
79const POOL_UNACTIONABLE: i32 = POOL_INVALID_TX + 8;
80/// Transaction does not provide any tags, so the pool can't identify it.
81const POOL_NO_TAGS: i32 = POOL_INVALID_TX + 9;
82/// Invalid block ID.
83const POOL_INVALID_BLOCK_ID: i32 = POOL_INVALID_TX + 10;
84/// The pool is not accepting future transactions.
85const POOL_FUTURE_TX: i32 = POOL_INVALID_TX + 11;
86/// Other error.
87const OTHER_ERR: i32 = BASE_ERROR + 40;
88
89impl From<Error> for ErrorObjectOwned {
90	fn from(e: Error) -> ErrorObjectOwned {
91		use sc_transaction_pool_api::error::Error as PoolError;
92
93		match e {
94			Error::BadFormat(e) => ErrorObject::owned(
95				BAD_FORMAT,
96				format!("Extrinsic has invalid format: {}", e),
97				None::<()>,
98			),
99			Error::Verification(e) => ErrorObject::owned(
100				VERIFICATION_ERROR,
101				format!("Verification Error: {}", e),
102				Some(format!("{:?}", e)),
103			),
104			Error::Pool(PoolError::InvalidTransaction(InvalidTransaction::Custom(e))) => {
105				ErrorObject::owned(
106					POOL_INVALID_TX,
107					"Invalid Transaction",
108					Some(format!("Custom error: {}", e)),
109				)
110			},
111			Error::Pool(PoolError::InvalidTransaction(e)) => {
112				let msg: &str = e.into();
113				ErrorObject::owned(
114					POOL_INVALID_TX,
115					"Invalid Transaction",
116					Some(msg),
117				)
118			},
119			Error::Pool(PoolError::UnknownTransaction(e)) => {
120				ErrorObject::owned(
121					POOL_UNKNOWN_VALIDITY,
122					"Unknown Transaction Validity",
123					Some(format!("{:?}", e)),
124				)
125			},
126			Error::Pool(PoolError::TemporarilyBanned) =>
127				ErrorObject::owned(
128				POOL_TEMPORARILY_BANNED,
129				"Transaction is temporarily banned",
130				None::<()>,
131			),
132			Error::Pool(PoolError::AlreadyImported(hash)) =>
133				ErrorObject::owned(
134				POOL_ALREADY_IMPORTED,
135				"Transaction Already Imported",
136				Some(format!("{:?}", hash)),
137			),
138			Error::Pool(PoolError::TooLowPriority { old, new }) => ErrorObject::owned(
139				POOL_TOO_LOW_PRIORITY,
140				format!("Priority is too low: ({} vs {})", old, new),
141				Some("The transaction has too low priority to replace another transaction already in the pool.")
142			),
143			Error::Pool(PoolError::CycleDetected) =>
144				ErrorObject::owned(
145				POOL_CYCLE_DETECTED,
146				"Cycle Detected",
147				None::<()>
148			),
149			Error::Pool(PoolError::ImmediatelyDropped) => ErrorObject::owned(
150				POOL_IMMEDIATELY_DROPPED,
151				"Immediately Dropped",
152				Some("The transaction couldn't enter the pool because of the limit"),
153			),
154			Error::Pool(PoolError::Unactionable) => ErrorObject::owned(
155				POOL_UNACTIONABLE,
156				"Unactionable",
157				Some("The transaction is unactionable since it is not propagable and \
158				the local node does not author blocks")
159			),
160			Error::Pool(PoolError::NoTagsProvided) => ErrorObject::owned(
161				POOL_NO_TAGS,
162				"No tags provided",
163				Some("Transaction does not provide any tags, so the pool can't identify it")
164			),
165			Error::Pool(PoolError::InvalidBlockId(_)) =>
166				ErrorObject::owned(
167				POOL_INVALID_BLOCK_ID,
168				"The provided block ID is not valid",
169				None::<()>
170			),
171			Error::Pool(PoolError::RejectedFutureTransaction) => {
172				ErrorObject::owned(
173					POOL_FUTURE_TX,
174					"The pool is not accepting future transactions",
175					None::<()>,
176				)
177			},
178			Error::UnsafeRpcCalled(e) => e.into(),
179			other => ErrorObject::owned(
180				OTHER_ERR,
181				other.to_string(),
182				None::<()>,
183			)
184		}
185	}
186}