referrerpolicy=no-referrer-when-downgrade

sc_consensus_manual_seal/
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//! A manual sealing engine: the engine listens for rpc calls to seal blocks and create forks.
20//! This is suitable for a testing environment.
21
22use 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;
28
29/// Error code for rpc
30mod codes {
31	pub const SERVER_SHUTTING_DOWN: i32 = 10_000;
32	pub const BLOCK_IMPORT_FAILED: i32 = 11_000;
33	pub const EMPTY_TRANSACTION_POOL: i32 = 12_000;
34	pub const BLOCK_NOT_FOUND: i32 = 13_000;
35	pub const CONSENSUS_ERROR: i32 = 14_000;
36	pub const INHERENTS_ERROR: i32 = 15_000;
37	pub const BLOCKCHAIN_ERROR: i32 = 16_000;
38	pub const UNKNOWN_ERROR: i32 = 20_000;
39}
40
41/// 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:?}")]
46	BlockImportError(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	)]
51	EmptyTransactionPool,
52	/// encountered during creation of Proposer.
53	#[error("Consensus Error: {0}")]
54	ConsensusError(#[from] ConsensusError),
55	/// Failed to create Inherents data
56	#[error("Inherents Error: {0}")]
57	InherentError(#[from] InherentsError),
58	/// error encountered during finalization
59	#[error("Finalization Error: {0}")]
60	BlockchainError(#[from] BlockchainError),
61	/// Supplied parent_hash doesn't exist in chain
62	#[error("Supplied parent_hash: {0} doesn't exist in chain")]
63	BlockNotFound(String),
64	/// Some string error
65	#[error("{0}")]
66	StringError(String),
67	/// send error
68	#[error("Consensus process is terminating")]
69	Canceled(#[from] oneshot::Canceled),
70	/// send error
71	#[error("Consensus process is terminating")]
72	SendError(#[from] SendError),
73	/// Some other error.
74	#[error("Other error: {0}")]
75	Other(Box<dyn std::error::Error + Send + Sync>),
76}
77
78impl From<ImportResult> for Error {
79	fn from(err: ImportResult) -> Self {
80		Error::BlockImportError(err)
81	}
82}
83
84impl From<String> for Error {
85	fn from(s: String) -> Self {
86		Error::StringError(s)
87	}
88}
89
90impl Error {
91	fn to_code(&self) -> i32 {
92		use Error::*;
93		match 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}
105
106impl From<Error> for ErrorObjectOwned {
107	fn from(err: Error) -> Self {
108		ErrorObject::owned(err.to_code(), err.to_string(), None::<()>)
109	}
110}