1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
56// 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.
1718//! Substrate client possible errors.
1920use 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};
2627/// Client Result type alias
28pub type Result<T> = result::Result<T, Error>;
2930/// 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:?}")]
39Validity(#[from] TransactionValidityError),
4041#[error("Application specific error")]
42Application(#[source] Box<dyn 'static + std::error::Error + Send + Sync>),
43}
4445/// Substrate Client error
46#[derive(Debug, thiserror::Error)]
47#[allow(missing_docs)]
48#[non_exhaustive]
49pub enum Error {
50#[error("Cancelled oneshot channel {0}")]
51OneShotCancelled(#[from] futures::channel::oneshot::Canceled),
5253#[error(transparent)]
54Consensus(#[from] sp_consensus::Error),
5556#[error("Backend error: {0}")]
57Backend(String),
5859#[error("UnknownBlock: {0}")]
60UnknownBlock(String),
6162#[error("UnknownBlocks: {0}")]
63UnknownBlocks(String),
6465#[error(transparent)]
66ApplyExtrinsicFailed(#[from] ApplyExtrinsicFailed),
6768#[error("Child type is invalid")]
69InvalidChildType,
7071#[error("RemoteBodyRequest: invalid extrinsics root expected: {expected} but got {received}")]
72ExtrinsicRootInvalid { received: String, expected: String },
7374// `inner` cannot be made member, since it lacks `std::error::Error` trait bounds.
75#[error("Execution failed: {0}")]
76Execution(Box<dyn sp_state_machine::Error>),
7778#[error("Blockchain")]
79Blockchain(#[source] Box<Error>),
8081/// A error used by various storage subsystems.
82 ///
83 /// Eventually this will be replaced.
84#[error("{0}")]
85StorageChanges(sp_state_machine::DefaultError),
8687#[error("Invalid child storage key")]
88InvalidChildStorageKey,
8990#[error("Current state of blockchain has invalid authorities set")]
91InvalidAuthoritiesSet,
9293#[error("Failed to get runtime version: {0}")]
94VersionInvalid(String),
9596#[error("Provided state is invalid")]
97InvalidState,
9899#[error("error decoding justification for header")]
100JustificationDecode,
101102#[error("bad justification for header: {0}")]
103BadJustification(String),
104105#[error("outdated justification")]
106OutdatedJustification,
107108#[error("This method is not currently available when running in light client mode")]
109NotAvailableOnLightClient,
110111#[error("Remote node has responded with invalid header proof")]
112InvalidCHTProof,
113114#[error("Remote data fetch has been cancelled")]
115RemoteFetchCancelled,
116117#[error("Remote data fetch has been failed")]
118RemoteFetchFailed,
119120#[error("Error decoding call result of {0}")]
121CallResultDecode(&'static str, #[source] CodecError),
122123#[error("Error at calling runtime api: {0}")]
124RuntimeApiError(#[from] ApiError),
125126#[error("Runtime :code missing in storage")]
127RuntimeCodeMissing,
128129#[error("Changes tries are not supported by the runtime")]
130ChangesTriesNotSupported,
131132#[error("Error reading changes tries configuration")]
133ErrorReadingChangesTriesConfig,
134135#[error("Failed to check changes proof: {0}")]
136ChangesTrieAccessFailed(String),
137138#[error("Did not finalize blocks in sequential order.")]
139NonSequentialFinalization(String),
140141#[error("Potential long-range attack: block not in finalized chain.")]
142NotInFinalizedChain,
143144#[error("Failed to get hash of block for building CHT")]
145MissingHashRequiredForCHT,
146147#[error("Calculated state root does not match.")]
148InvalidStateRoot,
149150#[error("Incomplete block import pipeline.")]
151IncompletePipeline,
152153#[error("Transaction pool not ready for block production.")]
154TransactionPoolNotReady,
155156#[error("Database error: {0}")]
157DatabaseError(#[from] sp_database::error::DatabaseError),
158159#[error("Failed to get header for hash {0}")]
160MissingHeader(String),
161162#[error("State Database error: {0}")]
163StateDatabase(String),
164165#[error("Statement store error: {0}")]
166StatementStore(String),
167168#[error("Failed to set the chain head to a block that's too old.")]
169SetHeadTooOld,
170171#[error(transparent)]
172Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
173174// Should be removed/improved once
175 // the storage `fn`s returns typed errors.
176#[error("Runtime code error: {0}")]
177RuntimeCode(&'static str),
178179// Should be removed/improved once
180 // the storage `fn`s returns typed errors.
181#[error("Storage error: {0}")]
182Storage(String),
183}
184185impl From<Box<dyn sp_state_machine::Error + Send + Sync + 'static>> for Error {
186fn from(e: Box<dyn sp_state_machine::Error + Send + Sync + 'static>) -> Self {
187Self::from_state(e)
188 }
189}
190191impl From<Box<dyn sp_state_machine::Error>> for Error {
192fn from(e: Box<dyn sp_state_machine::Error>) -> Self {
193Self::from_state(e)
194 }
195}
196197impl From<Error> for ApiError {
198fn from(err: Error) -> ApiError {
199match err {
200 Error::UnknownBlock(msg) => ApiError::UnknownBlock(msg),
201 Error::RuntimeApiError(err) => err,
202 e => ApiError::Application(Box::new(e)),
203 }
204 }
205}
206207impl Error {
208/// Chain a blockchain error.
209pub fn from_blockchain(e: Box<Error>) -> Self {
210 Error::Blockchain(e)
211 }
212213/// Chain a state error.
214pub fn from_state(e: Box<dyn sp_state_machine::Error>) -> Self {
215 Error::Execution(e)
216 }
217218/// 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.
221pub fn from_state_db<E>(e: E) -> Self
222where
223E: std::fmt::Debug,
224 {
225 Error::StateDatabase(format!("{:?}", e))
226 }
227}