1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
56// 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.
1011// 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.
1516// 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/>.
1819//! A manual sealing engine: the engine listens for rpc calls to seal blocks and create forks.
20//! This is suitable for a testing environment.
2122use futures::channel::{mpsc::SendError, oneshot};
23use jsonrpsee::types::error::{ErrorObject, ErrorObjectOwned};
24use sc_consensus::ImportResult;
25use sp_blockchain::Error as BlockchainError;
26use sp_consensus::Error as ConsensusError;
27use sp_inherents::Error as InherentsError;
2829/// Error code for rpc
30mod codes {
31pub const SERVER_SHUTTING_DOWN: i32 = 10_000;
32pub const BLOCK_IMPORT_FAILED: i32 = 11_000;
33pub const EMPTY_TRANSACTION_POOL: i32 = 12_000;
34pub const BLOCK_NOT_FOUND: i32 = 13_000;
35pub const CONSENSUS_ERROR: i32 = 14_000;
36pub const INHERENTS_ERROR: i32 = 15_000;
37pub const BLOCKCHAIN_ERROR: i32 = 16_000;
38pub const UNKNOWN_ERROR: i32 = 20_000;
39}
4041/// errors encountered by background block authorship task
42#[derive(Debug, thiserror::Error)]
43pub enum Error {
44/// An error occurred while importing the block
45#[error("Block import failed: {0:?}")]
46BlockImportError(ImportResult),
47/// Transaction pool is empty, cannot create a block
48#[error(
49"Transaction pool is empty, set create_empty to true, if you want to create empty blocks"
50)]
51EmptyTransactionPool,
52/// encountered during creation of Proposer.
53#[error("Consensus Error: {0}")]
54ConsensusError(#[from] ConsensusError),
55/// Failed to create Inherents data
56#[error("Inherents Error: {0}")]
57InherentError(#[from] InherentsError),
58/// error encountered during finalization
59#[error("Finalization Error: {0}")]
60BlockchainError(#[from] BlockchainError),
61/// Supplied parent_hash doesn't exist in chain
62#[error("Supplied parent_hash: {0} doesn't exist in chain")]
63BlockNotFound(String),
64/// Some string error
65#[error("{0}")]
66StringError(String),
67/// send error
68#[error("Consensus process is terminating")]
69Canceled(#[from] oneshot::Canceled),
70/// send error
71#[error("Consensus process is terminating")]
72SendError(#[from] SendError),
73/// Some other error.
74#[error("Other error: {0}")]
75Other(Box<dyn std::error::Error + Send + Sync>),
76}
7778impl From<ImportResult> for Error {
79fn from(err: ImportResult) -> Self {
80 Error::BlockImportError(err)
81 }
82}
8384impl From<String> for Error {
85fn from(s: String) -> Self {
86 Error::StringError(s)
87 }
88}
8990impl Error {
91fn to_code(&self) -> i32 {
92use Error::*;
93match self {
94 BlockImportError(_) => codes::BLOCK_IMPORT_FAILED,
95 BlockNotFound(_) => codes::BLOCK_NOT_FOUND,
96 EmptyTransactionPool => codes::EMPTY_TRANSACTION_POOL,
97 ConsensusError(_) => codes::CONSENSUS_ERROR,
98 InherentError(_) => codes::INHERENTS_ERROR,
99 BlockchainError(_) => codes::BLOCKCHAIN_ERROR,
100 SendError(_) | Canceled(_) => codes::SERVER_SHUTTING_DOWN,
101_ => codes::UNKNOWN_ERROR,
102 }
103 }
104}
105106impl From<Error> for ErrorObjectOwned {
107fn from(err: Error) -> Self {
108 ErrorObject::owned(err.to_code(), err.to_string(), None::<()>)
109 }
110}