migration to new block commitment data type; tests included
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
24b08a87b1
commit
4c79048b49
@ -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
|
||||
|
||||
@ -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<T, A> = Result<A, OffchainErr<NetworkIdOf<T>>>;
|
||||
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<T: Config> Pallet<T> {
|
||||
Authorities::<T>::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);
|
||||
}
|
||||
|
||||
9
pallets/slow-clap/src/migrations/mod.rs
Normal file
9
pallets/slow-clap/src/migrations/mod.rs
Normal file
@ -0,0 +1,9 @@
|
||||
pub mod v3;
|
||||
|
||||
pub type MigrateV2ToV3<T> = frame_support::migrations::VersionedMigration<
|
||||
2,
|
||||
3,
|
||||
v3::CommitmentDetailsTypesChanged<T>,
|
||||
crate::Pallet<T>,
|
||||
<T as frame_system::Config>::DbWeight,
|
||||
>;
|
||||
71
pallets/slow-clap/src/migrations/v3.rs
Normal file
71
pallets/slow-clap/src/migrations/v3.rs
Normal file
@ -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<T>(PhantomData<T>);
|
||||
impl<T: Config> UncheckedOnRuntimeUpgrade for CommitmentDetailsTypesChanged<T> {
|
||||
fn on_runtime_upgrade() -> Weight {
|
||||
let mut weight = T::DbWeight::get().reads(3);
|
||||
let current_block_number = <Pallet<T> as BlockNumberProvider>::current_block_number();
|
||||
|
||||
for network_id in T::NetworkDataHandler::iter_indexes() {
|
||||
let mut new_btree_map = BTreeMap::new();
|
||||
|
||||
let key_hash = BlockCommitments::<T>::hashed_key_for(&network_id);
|
||||
let old_commitments = storage::unhashed::take_or_default::<
|
||||
BTreeMap<AuthIndex, OldCommitmentDetails>,
|
||||
>(&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::<T>::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
|
||||
}
|
||||
}
|
||||
@ -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::<SlowClap>();
|
||||
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::<Runtime>::hashed_key_for(&network_id);
|
||||
frame_support::storage::unhashed::put(&key_hash, &fake_old_commits);
|
||||
}
|
||||
|
||||
type Migrate = crate::migrations::MigrateV2ToV3<Runtime>;
|
||||
<Migrate as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade();
|
||||
|
||||
let current_block_number = System::current_block_number();
|
||||
|
||||
for network_id in network_ids.iter() {
|
||||
BlockCommitments::<Runtime>::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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user