From 4c79048b49601c863408ffd90772844a66f8e2fc Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Mon, 23 Feb 2026 18:56:05 +0300 Subject: [PATCH] migration to new block commitment data type; tests included Signed-off-by: Uncle Stinky --- pallets/slow-clap/Cargo.toml | 2 +- pallets/slow-clap/src/lib.rs | 4 +- pallets/slow-clap/src/migrations/mod.rs | 9 ++++ pallets/slow-clap/src/migrations/v3.rs | 71 +++++++++++++++++++++++++ pallets/slow-clap/src/tests.rs | 60 ++++++++++++++++++++- 5 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 pallets/slow-clap/src/migrations/mod.rs create mode 100644 pallets/slow-clap/src/migrations/v3.rs diff --git a/pallets/slow-clap/Cargo.toml b/pallets/slow-clap/Cargo.toml index a9c9e99..815b94a 100644 --- a/pallets/slow-clap/Cargo.toml +++ b/pallets/slow-clap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-slow-clap" -version = "0.4.21" +version = "0.4.22" description = "Applause protocol for the EVM bridge" license.workspace = true authors.workspace = true diff --git a/pallets/slow-clap/src/lib.rs b/pallets/slow-clap/src/lib.rs index ddfb838..a849fca 100644 --- a/pallets/slow-clap/src/lib.rs +++ b/pallets/slow-clap/src/lib.rs @@ -43,6 +43,7 @@ use ghost_networks::{ }; use ghost_traits::exposure::ExposureListener; +pub mod migrations; pub mod weights; pub use crate::weights::WeightInfo; mod benchmarking; @@ -270,7 +271,7 @@ type OffchainResult = Result>>; pub mod pallet { use super::*; - const STORAGE_VERSION: StorageVersion = StorageVersion::new(2); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(3); #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] @@ -1326,7 +1327,6 @@ impl Pallet { Authorities::::set(&session_index, bounded_authorities); let mut disabled_bitmap = BitMap::new(authorities_len as AuthIndex); - // TODO: make me better for disabled_index in T::DisabledValidators::disabled_validators() { disabled_bitmap.set(disabled_index); } diff --git a/pallets/slow-clap/src/migrations/mod.rs b/pallets/slow-clap/src/migrations/mod.rs new file mode 100644 index 0000000..bb030e2 --- /dev/null +++ b/pallets/slow-clap/src/migrations/mod.rs @@ -0,0 +1,9 @@ +pub mod v3; + +pub type MigrateV2ToV3 = frame_support::migrations::VersionedMigration< + 2, + 3, + v3::CommitmentDetailsTypesChanged, + crate::Pallet, + ::DbWeight, +>; diff --git a/pallets/slow-clap/src/migrations/v3.rs b/pallets/slow-clap/src/migrations/v3.rs new file mode 100644 index 0000000..af7bdaa --- /dev/null +++ b/pallets/slow-clap/src/migrations/v3.rs @@ -0,0 +1,71 @@ +use codec::{Decode, Encode, MaxEncodedLen}; +use frame_support::{ + storage, + traits::{Get, UncheckedOnRuntimeUpgrade}, + weights::Weight, +}; +use ghost_networks::NetworkDataInspectHandler; +use scale_info::TypeInfo; +use sp_runtime::traits::{BlockNumberProvider, UniqueSaturatedInto}; +use sp_runtime::RuntimeDebug; +use sp_std::marker::PhantomData; + +use crate::{AuthIndex, BTreeMap, BlockCommitments, CommitmentDetails, Config, Pallet, LOG_TARGET}; + +#[derive( + RuntimeDebug, + Default, + Copy, + Clone, + Eq, + PartialEq, + Ord, + PartialOrd, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, +)] +pub struct OldCommitmentDetails { + pub last_stored_block: u64, + pub last_updated: u64, + pub commits: u64, +} + +pub struct CommitmentDetailsTypesChanged(PhantomData); +impl UncheckedOnRuntimeUpgrade for CommitmentDetailsTypesChanged { + fn on_runtime_upgrade() -> Weight { + let mut weight = T::DbWeight::get().reads(3); + let current_block_number = as BlockNumberProvider>::current_block_number(); + + for network_id in T::NetworkDataHandler::iter_indexes() { + let mut new_btree_map = BTreeMap::new(); + + let key_hash = BlockCommitments::::hashed_key_for(&network_id); + let old_commitments = storage::unhashed::take_or_default::< + BTreeMap, + >(&key_hash); + + for (authority_index, details) in old_commitments.iter() { + new_btree_map.insert( + authority_index, + CommitmentDetails { + last_stored_block: details.last_stored_block, + last_updated: current_block_number, + commits: details.commits.unique_saturated_into(), + }, + ); + } + BlockCommitments::::insert(&network_id, new_btree_map); + weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); + + log::info!( + target: LOG_TARGET, + "👻 Block commitments migrated from unix timestamp to internal block for network {:?}", + network_id, + ); + } + + weight + } +} diff --git a/pallets/slow-clap/src/tests.rs b/pallets/slow-clap/src/tests.rs index 0e8757d..3246d2d 100644 --- a/pallets/slow-clap/src/tests.rs +++ b/pallets/slow-clap/src/tests.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; use super::*; -use crate::evm_types::Log; use crate::mock::*; +use crate::{evm_types::Log, migrations::v3::OldCommitmentDetails}; use frame_support::{assert_err, assert_ok, dispatch}; use sp_core::offchain::{ @@ -1629,6 +1629,64 @@ fn should_get_balanced_responses_correctly() { }); } +#[test] +fn migration_from_v2_to_v3_works_fine() { + new_test_ext().execute_with(|| { + let network_ids = vec![1, 57232, 232, 775]; + + StorageVersion::new(2).put::(); + System::set_block_number(69); + + for network_id in network_ids.iter() { + prepare_evm_network(Some(*network_id), None); + let mut fake_old_commits = BTreeMap::new(); + + fake_old_commits.insert( + 0, + OldCommitmentDetails { + last_stored_block: (*network_id).into(), + last_updated: 133742069, + commits: 420, + }, + ); + fake_old_commits.insert( + 2, + OldCommitmentDetails { + last_stored_block: (*network_id).into(), + last_updated: 133742069, + commits: 5, + }, + ); + + let key_hash = BlockCommitments::::hashed_key_for(&network_id); + frame_support::storage::unhashed::put(&key_hash, &fake_old_commits); + } + + type Migrate = crate::migrations::MigrateV2ToV3; + ::on_runtime_upgrade(); + + let current_block_number = System::current_block_number(); + + for network_id in network_ids.iter() { + BlockCommitments::::get(&network_id) + .iter() + .for_each(|(authority_id, details)| match authority_id { + 0 => { + assert_eq!(details.last_stored_block, *network_id as u64); + assert_eq!(details.last_updated, current_block_number); + assert_eq!(details.commits, u8::MAX); + } + 2 => { + assert_eq!(details.last_stored_block, *network_id as u64); + assert_eq!(details.last_updated, current_block_number); + assert_eq!(details.commits, 5); + } + _ => panic!("non registered block commitment exists after migration"), + }); + } + }); +} + fn assert_clapped_amount( session_index: &SessionIndex, unique_hash: &H256,