From 11ace080fdf42f3594ee6d2d7e0d2a070684019a Mon Sep 17 00:00:00 2001
From: s0me0ne-unkn0wn <48632512+s0me0ne-unkn0wn@users.noreply.github.com>
Date: Mon, 19 Feb 2024 10:39:34 +0100
Subject: [PATCH] `im-online` removal cleanup: remove off-chain storage (#2290)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is a follow-up for `im-online` pallet removal that is cleaning up
its off-chain storage. Must be merged no earlier than #2265 is enacted.
Related: #1964

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
---
 substrate/frame/im-online/src/migration.rs | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/substrate/frame/im-online/src/migration.rs b/substrate/frame/im-online/src/migration.rs
index 3860a7ca53295..0d2c0a055b6d8 100644
--- a/substrate/frame/im-online/src/migration.rs
+++ b/substrate/frame/im-online/src/migration.rs
@@ -116,6 +116,21 @@ pub mod v1 {
 	}
 }
 
+/// Clears the pallet's offchain storage.
+///
+/// Must be put in `OffchainWorkerApi::offchain_worker` after
+/// the pallet was removed.
+pub fn clear_offchain_storage(validator_set_size: u32) {
+	(0..validator_set_size).for_each(|idx| {
+		let key = {
+			let mut key = DB_PREFIX.to_vec();
+			key.extend(idx.encode());
+			key
+		};
+		sp_runtime::offchain::storage::StorageValueRef::persistent(&key).clear();
+	});
+}
+
 #[cfg(all(feature = "try-runtime", test))]
 mod test {
 	use super::*;