referrerpolicy=no-referrer-when-downgrade

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("outdated justification")]
106	OutdatedJustification,
107
108	#[error("This method is not currently available when running in light client mode")]
109	NotAvailableOnLightClient,
110
111	#[error("Remote node has responded with invalid header proof")]
112	InvalidCHTProof,
113
114	#[error("Remote data fetch has been cancelled")]
115	RemoteFetchCancelled,
116
117	#[error("Remote data fetch has been failed")]
118	RemoteFetchFailed,
119
120	#[error("Error decoding call result of {0}")]
121	CallResultDecode(&'static str, #[source] CodecError),
122
123	#[error("Error at calling runtime api: {0}")]
124	RuntimeApiError(#[from] ApiError),
125
126	#[error("Runtime :code missing in storage")]
127	RuntimeCodeMissing,
128
129	#[error("Changes tries are not supported by the runtime")]
130	ChangesTriesNotSupported,
131
132	#[error("Error reading changes tries configuration")]
133	ErrorReadingChangesTriesConfig,
134
135	#[error("Failed to check changes proof: {0}")]
136	ChangesTrieAccessFailed(String),
137
138	#[error("Did not finalize blocks in sequential order.")]
139	NonSequentialFinalization(String),
140
141	#[error("Potential long-range attack: block not in finalized chain.")]
142	NotInFinalizedChain,
143
144	#[error("Failed to get hash of block for building CHT")]
145	MissingHashRequiredForCHT,
146
147	#[error("Calculated state root does not match.")]
148	InvalidStateRoot,
149
150	#[error("Incomplete block import pipeline.")]
151	IncompletePipeline,
152
153	#[error("Transaction pool not ready for block production.")]
154	TransactionPoolNotReady,
155
156	#[error("Database error: {0}")]
157	DatabaseError(#[from] sp_database::error::DatabaseError),
158
159	#[error("Failed to get header for hash {0}")]
160	MissingHeader(String),
161
162	#[error("State Database error: {0}")]
163	StateDatabase(String),
164
165	#[error("Statement store error: {0}")]
166	StatementStore(String),
167
168	#[error("Failed to set the chain head to a block that's too old.")]
169	SetHeadTooOld,
170
171	#[error(transparent)]
172	Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
173
174	// Should be removed/improved once
175	// the storage `fn`s returns typed errors.
176	#[error("Runtime code error: {0}")]
177	RuntimeCode(&'static str),
178
179	// Should be removed/improved once
180	// the storage `fn`s returns typed errors.
181	#[error("Storage error: {0}")]
182	Storage(String),
183}
184
185impl From<Box<dyn sp_state_machine::Error + Send + Sync + 'static>> for Error {
186	fn from(e: Box<dyn sp_state_machine::Error + Send + Sync + 'static>) -> Self {
187		Self::from_state(e)
188	}
189}
190
191impl From<Box<dyn sp_state_machine::Error>> for Error {
192	fn from(e: Box<dyn sp_state_machine::Error>) -> Self {
193		Self::from_state(e)
194	}
195}
196
197impl From<Error> for ApiError {
198	fn from(err: Error) -> ApiError {
199		match err {
200			Error::UnknownBlock(msg) => ApiError::UnknownBlock(msg),
201			Error::RuntimeApiError(err) => err,
202			e => ApiError::Application(Box::new(e)),
203		}
204	}
205}
206
207impl Error {
208	/// Chain a blockchain error.
209	pub fn from_blockchain(e: Box<Error>) -> Self {
210		Error::Blockchain(e)
211	}
212
213	/// Chain a state error.
214	pub fn from_state(e: Box<dyn sp_state_machine::Error>) -> Self {
215		Error::Execution(e)
216	}
217
218	/// Construct from a state db error.
219	// Can not be done directly, since that would make cargo run out of stack if
220	// `sc-state-db` is lib is added as dependency.
221	pub fn from_state_db<E>(e: E) -> Self
222	where
223		E: std::fmt::Debug,
224	{
225		Error::StateDatabase(format!("{:?}", e))
226	}
227}