From ad3732598b0604a38a3771a729b021b74ee18a54 Mon Sep 17 00:00:00 2001
From: Branislav Kontur <bkontur@gmail.com>
Date: Fri, 21 Feb 2025 10:03:37 +0100
Subject: [PATCH] Align error codes with other pallets

---
 polkadot/runtime/parachains/src/paras/mod.rs   | 16 +++++++---------
 polkadot/runtime/parachains/src/paras/tests.rs |  8 ++++----
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/polkadot/runtime/parachains/src/paras/mod.rs b/polkadot/runtime/parachains/src/paras/mod.rs
index 95bab14651d..7b64cc09027 100644
--- a/polkadot/runtime/parachains/src/paras/mod.rs
+++ b/polkadot/runtime/parachains/src/paras/mod.rs
@@ -720,8 +720,10 @@ pub mod pallet {
 		CannotUpgradeCode,
 		/// Invalid validation code size.
 		InvalidCode,
-		/// The given code is not authorized.
-		NotAuthorizedCode,
+		/// No upgrade authorized.
+		NothingAuthorized,
+		/// The submitted code is not authorized.
+		Unauthorized,
 	}
 
 	/// All currently active PVF pre-checking votes.
@@ -1214,13 +1216,9 @@ pub mod pallet {
 			// no need to ensure, anybody can do this
 
 			// Ensure `new_code` is authorized
-			if let Some(authorized_code_hash) = AuthorizedCodeHash::<T>::take(&para) {
-				if new_code.hash() != authorized_code_hash {
-					return Err(Error::<T>::NotAuthorizedCode.into());
-				}
-			} else {
-				return Err(Error::<T>::NotAuthorizedCode.into());
-			}
+			let authorized_code_hash =
+				AuthorizedCodeHash::<T>::take(&para).ok_or(Error::<T>::NothingAuthorized)?;
+			ensure!(authorized_code_hash == new_code.hash(), Error::<T>::Unauthorized);
 
 			// TODO: FAIL-CI - more validations?
 
diff --git a/polkadot/runtime/parachains/src/paras/tests.rs b/polkadot/runtime/parachains/src/paras/tests.rs
index 2eb1bf593b2..fbdefaac346 100644
--- a/polkadot/runtime/parachains/src/paras/tests.rs
+++ b/polkadot/runtime/parachains/src/paras/tests.rs
@@ -2041,7 +2041,7 @@ fn authorize_and_apply_set_current_code_works() {
 				para_a,
 				code_1.clone()
 			),
-			Error::<Test>::NotAuthorizedCode,
+			Error::<Test>::NothingAuthorized,
 		);
 
 		// non-root user cannot authorize
@@ -2066,7 +2066,7 @@ fn authorize_and_apply_set_current_code_works() {
 				para_a,
 				code_2.clone()
 			),
-			Error::<Test>::NotAuthorizedCode,
+			Error::<Test>::Unauthorized,
 		);
 		assert_eq!(AuthorizedCodeHash::<Test>::get(para_a), Some(code_1_hash));
 		assert!(CurrentCodeHash::<Test>::get(para_a).is_none());
@@ -2111,7 +2111,7 @@ fn authorize_and_apply_set_current_code_works() {
 				para_a,
 				code_1.clone()
 			),
-			Error::<Test>::NotAuthorizedCode,
+			Error::<Test>::Unauthorized,
 		);
 		assert_err!(
 			Paras::apply_authorized_force_set_current_code(
@@ -2119,7 +2119,7 @@ fn authorize_and_apply_set_current_code_works() {
 				para_a,
 				code_2.clone()
 			),
-			Error::<Test>::NotAuthorizedCode,
+			Error::<Test>::Unauthorized,
 		);
 
 		// apply just authorized
-- 
GitLab