From 6c2895b3b7018e0ceffe72b265be4402b609e137 Mon Sep 17 00:00:00 2001 From: Zebedeusz Date: Mon, 14 Oct 2024 12:21:49 +0200 Subject: [PATCH 1/4] Remove getter attribute from pallet offences --- prdoc/pr_6027.prdoc | 9 +++++++++ substrate/frame/offences/src/lib.rs | 16 +++++++++++++++- substrate/frame/offences/src/tests.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 prdoc/pr_6027.prdoc diff --git a/prdoc/pr_6027.prdoc b/prdoc/pr_6027.prdoc new file mode 100644 index 0000000000000..36bdb57b25f58 --- /dev/null +++ b/prdoc/pr_6027.prdoc @@ -0,0 +1,9 @@ +title: Remove pallet::getter from pallet-offences +doc: + - audience: Runtime Dev + description: | + This PR removes pallet::getter from pallet-offences from type Reports. It also adds a test to verify that retrieval of Reports still works with storage::getter. + +crates: + - name: pallet-offences + bump: patch diff --git a/substrate/frame/offences/src/lib.rs b/substrate/frame/offences/src/lib.rs index ffea32a1f4703..9d8195ba4dff1 100644 --- a/substrate/frame/offences/src/lib.rs +++ b/substrate/frame/offences/src/lib.rs @@ -73,7 +73,6 @@ pub mod pallet { /// The primary structure that holds all offence records keyed by report identifiers. #[pallet::storage] - #[pallet::getter(fn reports)] pub type Reports = StorageMap< _, Twox64Concat, @@ -81,6 +80,21 @@ pub mod pallet { OffenceDetails, >; + impl Pallet { + #[deprecated(note = "Use Reports::::get(id) instead.")] + pub fn reports( + k: KArg, + ) -> Option> + where + KArg: frame_support::__private::codec::EncodeLike>, + { + as frame_support::storage::StorageMap< + ReportIdOf, + OffenceDetails, + >>::get(k) + } + } + /// A vector of reports of the same kind that happened at the same time slot. #[pallet::storage] pub type ConcurrentReportsIndex = StorageDoubleMap< diff --git a/substrate/frame/offences/src/tests.rs b/substrate/frame/offences/src/tests.rs index 4897b78f3e4d3..70a2f8744b482 100644 --- a/substrate/frame/offences/src/tests.rs +++ b/substrate/frame/offences/src/tests.rs @@ -21,12 +21,35 @@ use super::*; use crate::mock::{ - new_test_ext, offence_reports, with_on_offence_fractions, Offence, Offences, RuntimeEvent, - System, KIND, + new_test_ext, offence_reports, with_on_offence_fractions, Offence, Offences, Runtime, + RuntimeEvent, System, KIND, }; use frame_system::{EventRecord, Phase}; +use sp_core::H256; use sp_runtime::Perbill; +#[test] +fn should_get_reports_with_storagemap_getter_and_deprecated_getter() { + new_test_ext().execute_with(|| { + // given + let report_id: ReportIdOf = H256::from_low_u64_be(1); + let offence_details = OffenceDetails { offender: 1, reporters: vec![2, 3] }; + + Reports::::insert(report_id, offence_details.clone()); + + // when + #[allow(deprecated)] + let stored_offence_details = Offences::reports(report_id); + // then + assert_eq!(stored_offence_details, Some(offence_details.clone())); + + // when + let stored_offence_details = Reports::::get(report_id); + // then + assert_eq!(stored_offence_details, Some(offence_details.clone())); + }); +} + #[test] fn should_report_an_authority_and_trigger_on_offence() { new_test_ext().execute_with(|| { From db1ec46a4a81c035d0548c29ed4255390b718d7c Mon Sep 17 00:00:00 2001 From: Zebedeusz Date: Tue, 15 Oct 2024 12:04:33 +0200 Subject: [PATCH 2/4] Getter moved to other impl block, deprecation note removed, simplified implem --- substrate/frame/offences/src/lib.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/substrate/frame/offences/src/lib.rs b/substrate/frame/offences/src/lib.rs index 9d8195ba4dff1..18f37c759a6a2 100644 --- a/substrate/frame/offences/src/lib.rs +++ b/substrate/frame/offences/src/lib.rs @@ -80,21 +80,6 @@ pub mod pallet { OffenceDetails, >; - impl Pallet { - #[deprecated(note = "Use Reports::::get(id) instead.")] - pub fn reports( - k: KArg, - ) -> Option> - where - KArg: frame_support::__private::codec::EncodeLike>, - { - as frame_support::storage::StorageMap< - ReportIdOf, - OffenceDetails, - >>::get(k) - } - } - /// A vector of reports of the same kind that happened at the same time slot. #[pallet::storage] pub type ConcurrentReportsIndex = StorageDoubleMap< @@ -166,6 +151,13 @@ where } impl Pallet { + /// Get the offence details from reports of given ID. + pub fn reports( + report_id: ReportIdOf, + ) -> Option> { + Reports::::get(report_id) + } + /// Compute the ID for the given report properties. /// /// The report id depends on the offence kind, time slot and the id of offender. From c6fd4254115d7853db0808bc0850deac009ac9eb Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 15 Oct 2024 10:06:41 -0400 Subject: [PATCH 3/4] Update substrate/frame/offences/src/tests.rs --- substrate/frame/offences/src/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/substrate/frame/offences/src/tests.rs b/substrate/frame/offences/src/tests.rs index 70a2f8744b482..9a56e2807236e 100644 --- a/substrate/frame/offences/src/tests.rs +++ b/substrate/frame/offences/src/tests.rs @@ -38,7 +38,6 @@ fn should_get_reports_with_storagemap_getter_and_deprecated_getter() { Reports::::insert(report_id, offence_details.clone()); // when - #[allow(deprecated)] let stored_offence_details = Offences::reports(report_id); // then assert_eq!(stored_offence_details, Some(offence_details.clone())); From 0a4f3a41e84e24e9c274b19743565964121e09a0 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 15 Oct 2024 10:07:11 -0400 Subject: [PATCH 4/4] Update substrate/frame/offences/src/tests.rs --- substrate/frame/offences/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/offences/src/tests.rs b/substrate/frame/offences/src/tests.rs index 9a56e2807236e..ab72b51054d6b 100644 --- a/substrate/frame/offences/src/tests.rs +++ b/substrate/frame/offences/src/tests.rs @@ -29,7 +29,7 @@ use sp_core::H256; use sp_runtime::Perbill; #[test] -fn should_get_reports_with_storagemap_getter_and_deprecated_getter() { +fn should_get_reports_with_storagemap_getter_and_function_getter() { new_test_ext().execute_with(|| { // given let report_id: ReportIdOf = H256::from_low_u64_be(1);