referrerpolicy=no-referrer-when-downgrade

sc_consensus_grandpa_rpc/
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
19use jsonrpsee::types::error::{ErrorObject, ErrorObjectOwned};
20
21#[derive(Debug, thiserror::Error)]
22/// Top-level error type for the RPC handler
23pub enum Error {
24	/// The GRANDPA RPC endpoint is not ready.
25	#[error("GRANDPA RPC endpoint not ready")]
26	EndpointNotReady,
27	/// GRANDPA reports the authority set id to be larger than 32-bits.
28	#[error("GRANDPA reports authority set id unreasonably large")]
29	AuthoritySetIdReportedAsUnreasonablyLarge,
30	/// GRANDPA reports voter state with round id or weights larger than 32-bits.
31	#[error("GRANDPA reports voter state as unreasonably large")]
32	VoterStateReportsUnreasonablyLargeNumbers,
33	/// GRANDPA prove finality failed.
34	#[error("GRANDPA prove finality rpc failed: {0}")]
35	ProveFinalityFailed(#[from] sc_consensus_grandpa::FinalityProofError),
36}
37
38/// The error codes returned by jsonrpc.
39pub enum ErrorCode {
40	/// Returned when Grandpa RPC endpoint is not ready.
41	NotReady = 1,
42	/// Authority set ID is larger than 32-bits.
43	AuthoritySetTooLarge,
44	/// Voter state with round id or weights larger than 32-bits.
45	VoterStateTooLarge,
46	/// Failed to prove finality.
47	ProveFinality,
48}
49
50impl From<Error> for ErrorCode {
51	fn from(error: Error) -> Self {
52		match error {
53			Error::EndpointNotReady => ErrorCode::NotReady,
54			Error::AuthoritySetIdReportedAsUnreasonablyLarge => ErrorCode::AuthoritySetTooLarge,
55			Error::VoterStateReportsUnreasonablyLargeNumbers => ErrorCode::VoterStateTooLarge,
56			Error::ProveFinalityFailed(_) => ErrorCode::ProveFinality,
57		}
58	}
59}
60
61impl From<Error> for ErrorObjectOwned {
62	fn from(error: Error) -> Self {
63		let message = error.to_string();
64		let code = ErrorCode::from(error);
65		ErrorObject::owned(code as i32, message, None::<()>)
66	}
67}
68
69impl From<std::num::TryFromIntError> for Error {
70	fn from(_error: std::num::TryFromIntError) -> Self {
71		Error::VoterStateReportsUnreasonablyLargeNumbers
72	}
73}