From 9ad06d57fcaa199df4bf5164231d14bfa1f168ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= <tomusdrw@users.noreply.github.com>
Date: Thu, 7 Mar 2019 10:53:53 +0100
Subject: [PATCH] Extract specific transaction pool errors. (#1930)

* Extract specific transaction pool errors.

* Convert error codes to constants.
---
 substrate/core/rpc/src/author/error.rs | 61 ++++++++++++++++++++++++--
 1 file changed, 58 insertions(+), 3 deletions(-)

diff --git a/substrate/core/rpc/src/author/error.rs b/substrate/core/rpc/src/author/error.rs
index e1a6f4a22d4..9c1ec232252 100644
--- a/substrate/core/rpc/src/author/error.rs
+++ b/substrate/core/rpc/src/author/error.rs
@@ -47,22 +47,77 @@ error_chain! {
 	}
 }
 
-const ERROR: i64 = 1000;
+/// Base code for all authorship errors.
+const BASE_ERROR: i64 = 1000;
+/// Extrinsic has an invalid format.
+const BAD_FORMAT: i64 = BASE_ERROR + 1;
+/// Error during transaction verification in runtime.
+const VERIFICATION_ERROR: i64 = BASE_ERROR + 2;
+
+/// Pool rejected the transaction as invalid
+const POOL_INVALID_TX: i64 = BASE_ERROR + 10;
+/// Cannot determine transaction validity.
+const POOL_UNKNOWN_VALIDITY: i64 = POOL_INVALID_TX + 1;
+/// The transaction is temporarily banned.
+const POOL_TEMPORARILY_BANNED: i64 = POOL_INVALID_TX + 2;
+/// The transaction is already in the pool
+const POOL_ALREADY_IMPORTED: i64 = POOL_INVALID_TX + 3;
+/// Transaction has too low priority to replace existing one in the pool.
+const POOL_TOO_LOW_PRIORITY: i64 = POOL_INVALID_TX + 4;
+/// Including this transaction would cause a dependency cycle.
+const POOL_CYCLE_DETECTED: i64 = POOL_INVALID_TX + 5;
+/// The transaction was not included to the pool because of the limits.
+const POOL_IMMEDIATELY_DROPPED: i64 = POOL_INVALID_TX + 6;
 
 impl From<Error> for rpc::Error {
 	fn from(e: Error) -> Self {
 		match e {
 			Error(ErrorKind::Unimplemented, _) => errors::unimplemented(),
 			Error(ErrorKind::BadFormat, _) => rpc::Error {
-				code: rpc::ErrorCode::ServerError(ERROR + 1),
+				code: rpc::ErrorCode::ServerError(BAD_FORMAT),
 				message: "Extrinsic has invalid format.".into(),
 				data: None,
 			},
 			Error(ErrorKind::Verification(e), _) => rpc::Error {
-				code: rpc::ErrorCode::ServerError(ERROR + 2),
+				code: rpc::ErrorCode::ServerError(VERIFICATION_ERROR),
 				message: e.description().into(),
 				data: Some(format!("{:?}", e).into()),
 			},
+			Error(ErrorKind::Pool(txpool::error::ErrorKind::InvalidTransaction(code)), _) => rpc::Error {
+				code: rpc::ErrorCode::ServerError(POOL_INVALID_TX),
+				message: "Invalid Transaction".into(),
+				data: Some(code.into()),
+			},
+			Error(ErrorKind::Pool(txpool::error::ErrorKind::UnknownTransactionValidity(code)), _) => rpc::Error {
+				code: rpc::ErrorCode::ServerError(POOL_UNKNOWN_VALIDITY),
+				message: "Unknown Transaction Validity".into(),
+				data: Some(code.into()),
+			},
+			Error(ErrorKind::Pool(txpool::error::ErrorKind::TemporarilyBanned), _) => rpc::Error {
+				code: rpc::ErrorCode::ServerError(POOL_TEMPORARILY_BANNED),
+				message: "Transaction is temporarily banned".into(),
+				data: None,
+			},
+			Error(ErrorKind::Pool(txpool::error::ErrorKind::AlreadyImported(hash)), _) => rpc::Error {
+				code: rpc::ErrorCode::ServerError(POOL_ALREADY_IMPORTED),
+				message: "Transaction Already Imported".into(),
+				data: Some(format!("{:?}", hash).into()),
+			},
+			Error(ErrorKind::Pool(txpool::error::ErrorKind::TooLowPriority(old, new)), _) => rpc::Error {
+				code: rpc::ErrorCode::ServerError(POOL_TOO_LOW_PRIORITY),
+				message: format!("Priority is too low: ({} vs {})", old, new),
+				data: Some("The transaction has too low priority to replace another transaction already in the pool.".into()),
+			},
+			Error(ErrorKind::Pool(txpool::error::ErrorKind::CycleDetected), _) => rpc::Error {
+				code: rpc::ErrorCode::ServerError(POOL_CYCLE_DETECTED),
+				message: "Cycle Detected".into(),
+				data: None,
+			},
+			Error(ErrorKind::Pool(txpool::error::ErrorKind::ImmediatelyDropped), _) => rpc::Error {
+				code: rpc::ErrorCode::ServerError(POOL_IMMEDIATELY_DROPPED),
+				message: "Immediately Dropped" .into(),
+				data: Some("The transaction couldn't enter the pool because of the limit".into()),
+			},
 			e => errors::internal(e),
 		}
 	}
-- 
GitLab