sp_blockchain/
error.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Substrate client possible errors.
19
20use codec::Error as CodecError;
21use sp_api::ApiError;
22use sp_consensus;
23use sp_runtime::transaction_validity::TransactionValidityError;
24use sp_state_machine;
25use std::{self, result};
26
27/// Client Result type alias
28pub type Result<T> = result::Result<T, Error>;
29
30/// Error when the runtime failed to apply an extrinsic.
31#[derive(Debug, thiserror::Error)]
32#[allow(missing_docs)]
33pub enum ApplyExtrinsicFailed {
34	/// The transaction cannot be included into the current block.
35	///
36	/// This doesn't necessary mean that the transaction itself is invalid, but it might be just
37	/// unapplicable onto the current block.
38	#[error("Extrinsic is not valid: {0:?}")]
39	Validity(#[from] TransactionValidityError),
40
41	#[error("Application specific error")]
42	Application(#[source] Box<dyn 'static + std::error::Error + Send + Sync>),
43}
44
45/// Substrate Client error
46#[derive(Debug, thiserror::Error)]
47#[allow(missing_docs)]
48#[non_exhaustive]
49pub enum Error {
50	#[error("Cancelled oneshot channel {0}")]
51	OneShotCancelled(#[from] futures::channel::oneshot::Canceled),
52
53	#[error(transparent)]
54	Consensus(#[from] sp_consensus::Error),
55
56	#[error("Backend error: {0}")]
57	Backend(String),
58
59	#[error("UnknownBlock: {0}")]
60	UnknownBlock(String),
61
62	#[error("UnknownBlocks: {0}")]
63	UnknownBlocks(String),
64
65	#[error(transparent)]
66	ApplyExtrinsicFailed(#[from] ApplyExtrinsicFailed),
67
68	#[error("Child type is invalid")]
69	InvalidChildType,
70
71	#[error("RemoteBodyRequest: invalid extrinsics root expected: {expected} but got {received}")]
72	ExtrinsicRootInvalid { received: String, expected: String },
73
74	// `inner` cannot be made member, since it lacks `std::error::Error` trait bounds.
75	#[error("Execution failed: {0}")]
76	Execution(Box<dyn sp_state_machine::Error>),
77
78	#[error("Blockchain")]
79	Blockchain(#[source] Box<Error>),
80
81	/// A error used by various storage subsystems.
82	///
83	/// Eventually this will be replaced.
84	#[error("{0}")]
85	StorageChanges(sp_state_machine::DefaultError),
86
87	#[error("Invalid child storage key")]
88	InvalidChildStorageKey,
89
90	#[error("Current state of blockchain has invalid authorities set")]
91	InvalidAuthoritiesSet,
92
93	#[error("Failed to get runtime version: {0}")]
94	VersionInvalid(String),
95
96	#[error("Provided state is invalid")]
97	InvalidState,
98
99	#[error("error decoding justification for header")]
100	JustificationDecode,
101
102	#[error("bad justification for header: {0}")]
103	BadJustification(String),
104
105	#[error("This method is not currently available when running in light client mode")]
106	NotAvailableOnLightClient,
107
108	#[error("Remote node has responded with invalid header proof")]
109	InvalidCHTProof,
110
111	#[error("Remote data fetch has been cancelled")]
112	RemoteFetchCancelled,
113
114	#[error("Remote data fetch has been failed")]
115	RemoteFetchFailed,
116
117	#[error("Error decoding call result of {0}")]
118	CallResultDecode(&'static str, #[source] CodecError),
119
120	#[error("Error at calling runtime api: {0}")]
121	RuntimeApiError(#[from] ApiError),
122
123	#[error("Runtime :code missing in storage")]
124	RuntimeCodeMissing,
125
126	#[error("Changes tries are not supported by the runtime")]
127	ChangesTriesNotSupported,
128
129	#[error("Error reading changes tries configuration")]
130	ErrorReadingChangesTriesConfig,
131
132	#[error("Failed to check changes proof: {0}")]
133	ChangesTrieAccessFailed(String),
134
135	#[error("Did not finalize blocks in sequential order.")]
136	NonSequentialFinalization(String),
137
138	#[error("Potential long-range attack: block not in finalized chain.")]
139	NotInFinalizedChain,
140
141	#[error("Failed to get hash of block for building CHT")]
142	MissingHashRequiredForCHT,
143
144	#[error("Calculated state root does not match.")]
145	InvalidStateRoot,
146
147	#[error("Incomplete block import pipeline.")]
148	IncompletePipeline,
149
150	#[error("Transaction pool not ready for block production.")]
151	TransactionPoolNotReady,
152
153	#[error("Database error: {0}")]
154	DatabaseError(#[from] sp_database::error::DatabaseError),
155
156	#[error("Failed to get header for hash {0}")]
157	MissingHeader(String),
158
159	#[error("State Database error: {0}")]
160	StateDatabase(String),
161
162	#[error("Statement store error: {0}")]
163	StatementStore(String),
164
165	#[error("Failed to set the chain head to a block that's too old.")]
166	SetHeadTooOld,
167
168	#[error(transparent)]
169	Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
170
171	// Should be removed/improved once
172	// the storage `fn`s returns typed errors.
173	#[error("Runtime code error: {0}")]
174	RuntimeCode(&'static str),
175
176	// Should be removed/improved once
177	// the storage `fn`s returns typed errors.
178	#[error("Storage error: {0}")]
179	Storage(String),
180}
181
182impl From<Box<dyn sp_state_machine::Error + Send + Sync + 'static>> for Error {
183	fn from(e: Box<dyn sp_state_machine::Error + Send + Sync + 'static>) -> Self {
184		Self::from_state(e)
185	}
186}
187
188impl From<Box<dyn sp_state_machine::Error>> for Error {
189	fn from(e: Box<dyn sp_state_machine::Error>) -> Self {
190		Self::from_state(e)
191	}
192}
193
194impl From<Error> for ApiError {
195	fn from(err: Error) -> ApiError {
196		match err {
197			Error::UnknownBlock(msg) => ApiError::UnknownBlock(msg),
198			Error::RuntimeApiError(err) => err,
199			e => ApiError::Application(Box::new(e)),
200		}
201	}
202}
203
204impl Error {
205	/// Chain a blockchain error.
206	pub fn from_blockchain(e: Box<Error>) -> Self {
207		Error::Blockchain(e)
208	}
209
210	/// Chain a state error.
211	pub fn from_state(e: Box<dyn sp_state_machine::Error>) -> Self {
212		Error::Execution(e)
213	}
214
215	/// Construct from a state db error.
216	// Can not be done directly, since that would make cargo run out of stack if
217	// `sc-state-db` is lib is added as dependency.
218	pub fn from_state_db<E>(e: E) -> Self
219	where
220		E: std::fmt::Debug,
221	{
222		Error::StateDatabase(format!("{:?}", e))
223	}
224}