-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[frame/im-online] remove network state from heartbeats #14251
Changes from 1 commit
0073c30
c8218b9
a282042
b60c5c4
5c7956e
97b9fd2
cd589f6
e88d7e2
1eb8086
41bca16
619a28e
aaefc66
57246bf
8e4061d
dcc7b38
81d9560
c822c2d
ace0f6e
906fa96
4092dfb
9538acd
8818c18
eedf528
11c4fd7
aedfdb9
f8970f6
fbbbd3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
+ pre_upgrade and post_upgrade working fn
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,17 @@ mod v0 { | |
SessionIndex, | ||
Twox64Concat, | ||
AuthIndex, | ||
WrapperOpaque<BoundedOpaqueNetworkState<u32, u32, T::MaxPeerInHeartbeats>>, | ||
WrapperOpaque< | ||
BoundedOpaqueNetworkState< | ||
<T as Config>::MaxPeerInHeartbeats, /* XXX: use similar type because | ||
* `MaxPeerDataEncodingSize` was removed in | ||
* v1 */ | ||
<T as Config>::MaxPeerInHeartbeats, /* XXX: use similar type because | ||
* `MaxPeerDataEncodingSize` was removed in | ||
* v1 */ | ||
<T as Config>::MaxPeerInHeartbeats, | ||
>, | ||
>, | ||
>; | ||
} | ||
|
||
|
@@ -74,16 +84,13 @@ pub mod v1 { | |
|
||
impl<T: Config> OnRuntimeUpgrade for Migration<T> { | ||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), TryRuntimeError> { | ||
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> { | ||
ensure!(StorageVersion::get::<Pallet<T>>() == 0, "can only upgrade from version 0"); | ||
|
||
log::info!( | ||
target: TARGET, | ||
"Migrating {} received heartbeats", | ||
v0::ReceivedHeartbeats::<T>::iter().count() | ||
); | ||
let count = v0::ReceivedHeartbeats::<T>::iter().count(); | ||
log::info!(target: TARGET, "Migrating {} received heartbeats", count); | ||
|
||
Ok(()) | ||
Ok((count as u32).encode()) | ||
} | ||
|
||
fn on_runtime_upgrade() -> Weight { | ||
|
@@ -97,33 +104,38 @@ pub mod v1 { | |
} | ||
|
||
let count = v0::ReceivedHeartbeats::<T>::iter().count(); | ||
melekes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
weight.saturating_accrue( | ||
T::DbWeight::get().reads(v0::ReceivedHeartbeats::<T>::iter().count() as u64), | ||
); | ||
weight.saturating_accrue( | ||
T::DbWeight::get().writes(v0::ReceivedHeartbeats::<T>::iter().count() as u64), | ||
); | ||
weight.saturating_accrue(T::DbWeight::get().reads(count as u64)); | ||
weight.saturating_accrue(T::DbWeight::get().writes(count as u64)); | ||
|
||
v0::ReceivedHeartbeats::<T>::translate::<_, _>( | ||
|k: T::SessionIndex, T::AccountId, state: _| { | ||
log::trace!(target: TARGET, "Migrated received heartbeat for {:?}...", k); | ||
Some(()) | ||
}, | ||
); | ||
let heartbeats = v0::ReceivedHeartbeats::<T>::drain().collect::<Vec<_>>(); | ||
for (session_index, auth_index, _) in heartbeats { | ||
log::trace!( | ||
target: TARGET, | ||
"Migrated received heartbeat for {:?}...", | ||
(session_index, auth_index) | ||
); | ||
crate::ReceivedHeartbeats::<T>::insert(session_index, auth_index, ()); | ||
} | ||
|
||
StorageVersion::new(1).put::<Pallet<T>>(); | ||
weight.saturating_add(T::DbWeight::get().writes(1)) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade(state: Vec<u8>) -> DispatchResult { | ||
ensure!(StorageVersion::get::<Pallet<T>>() == 1, "must upgrade"); | ||
let old_received_heartbeats: u32 = | ||
Decode::decode(&mut &state[..]).expect("pre_upgrade provides a valid state; qed"); | ||
let new_received_heartbeats = crate::ReceivedHeartbeats::<T>.iter().count(); | ||
|
||
log::info!( | ||
target: TARGET, | ||
"Migrated {} received heartbeats", | ||
crate::ReceivedHeartbeats::<T>::iter().count() | ||
); | ||
if new_received_heartbeats != old_received_heartbeats { | ||
log::error!( | ||
target: TARGET, | ||
"migrated {} received heartbeats, expected {}", | ||
new_received_heartbeats, | ||
old_received_heartbeats | ||
); | ||
} | ||
ensure!(StorageVersion::get::<Pallet<T>>() == 1, "must upgrade"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have been There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about line 64? ensure!(StorageVersion::get::<Pallet<T>>() == 0, "can only upgrade from version 0"); I thought if pre-upgrade fails, post-upgrade won't be executed either, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, I also have overseen this 🙈 So, the general idea is that these migrations may stay in the runtime for quite some time and we also have tests on Polkadot that run these migrations using try runtime. Now, when the migration would be applied (which could be at any time) tests will start to fail. TLDR: The check should be removed from |
||
|
||
Ok(()) | ||
} | ||
|
@@ -134,9 +146,7 @@ pub mod v1 { | |
#[cfg(feature = "try-runtime")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about try-runtime CLI cmd? Or it's not used? I've seen at least 15 migration.rs files containing that flag. |
||
mod test { | ||
use super::*; | ||
use crate::mock::{Test as T, *}; | ||
|
||
use frame_support::bounded_vec; | ||
use crate::mock::*; | ||
|
||
#[test] | ||
fn migration_works() { | ||
|
@@ -150,7 +160,7 @@ mod test { | |
|
||
assert_eq!(v0::ReceivedHeartbeats::<T>::iter().count(), 2); | ||
assert_eq!( | ||
v1::ReceivedHeartbeats::<T>::iter().count(), | ||
crate::ReceivedHeartbeats::<T>::iter().count(), | ||
0, | ||
"V1 storage should be corrupted" | ||
); | ||
|
@@ -160,11 +170,14 @@ mod test { | |
v1::Migration::<T>::post_upgrade(state).unwrap(); | ||
|
||
assert_eq!(v0::ReceivedHeartbeats::<T>::iter().count(), 2); | ||
assert_eq!(v1::ReceivedHeartbeats::<T>::iter().count(), 2); | ||
assert_eq!(crate::ReceivedHeartbeats::<T>::iter().count(), 2); | ||
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1); | ||
|
||
assert!(v1::ReceivedHeartbeats::<T>::contains(¤t_session, AuthIndex::from(0))); | ||
assert_eq!((), v1::ReceivedHeartbeats::<T>::get(¤t_session, AuthIndex::from(1))); | ||
assert!(crate::ReceivedHeartbeats::<T>::contains(¤t_session, AuthIndex::from(0))); | ||
assert_eq!( | ||
(), | ||
crate::ReceivedHeartbeats::<T>::get(¤t_session, AuthIndex::from(1)) | ||
); | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've noticed there's a difference between iterating over keys and values. If so, when should I iterate over values? over keys?