1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use codec::{Decode, Encode};
use core::marker::PhantomData;
use frame_election_provider_support::ScoreProvider;
use frame_support::traits::OnRuntimeUpgrade;
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
pub struct CheckCounterPrefix<T: crate::Config<I>, I: 'static>(sp_std::marker::PhantomData<(T, I)>);
impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for CheckCounterPrefix<T, I> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
frame_support::weights::Weight::zero()
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
#[frame_support::storage_alias]
type CounterForListNodes<T: crate::Config<I>, I: 'static> =
StorageValue<crate::Pallet<T, I>, u32>;
ensure!(
crate::ListNodes::<T, I>::count() == CounterForListNodes::<T, I>::get().unwrap(),
"wrong list node counter"
);
crate::log!(
info,
"checked bags-list prefix to be correct and have {} nodes",
crate::ListNodes::<T, I>::count()
);
Ok(())
}
}
mod old {
use super::*;
use frame_support::pallet_prelude::*;
#[derive(Encode, Decode)]
pub struct PreScoreNode<T: crate::Config<I>, I: 'static = ()> {
pub id: T::AccountId,
pub prev: Option<T::AccountId>,
pub next: Option<T::AccountId>,
pub bag_upper: T::Score,
#[codec(skip)]
pub _phantom: PhantomData<I>,
}
#[frame_support::storage_alias]
pub type ListNodes<T: crate::Config<I>, I: 'static> = StorageMap<
crate::Pallet<T, I>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
PreScoreNode<T, I>,
>;
#[frame_support::storage_alias]
pub type CounterForListNodes<T: crate::Config<I>, I: 'static> =
StorageValue<crate::Pallet<T, I>, u32, ValueQuery>;
#[frame_support::storage_alias]
pub type TempStorage<T: crate::Config<I>, I: 'static> =
StorageValue<crate::Pallet<T, I>, u32, ValueQuery>;
}
pub struct AddScore<T: crate::Config<I>, I: 'static = ()>(sp_std::marker::PhantomData<(T, I)>);
impl<T: crate::Config<I>, I: 'static> OnRuntimeUpgrade for AddScore<T, I> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
ensure!(crate::ListNodes::<T, I>::iter().count() == 0, "list node data is not corrupt");
let iter_node_count: u32 = old::ListNodes::<T, I>::iter().count() as u32;
let tracked_node_count: u32 = old::CounterForListNodes::<T, I>::get();
crate::log!(info, "number of nodes before: {:?} {:?}", iter_node_count, tracked_node_count);
ensure!(iter_node_count == tracked_node_count, "Node count is wrong.");
old::TempStorage::<T, I>::put(iter_node_count);
Ok(())
}
fn on_runtime_upgrade() -> frame_support::weights::Weight {
for (_key, node) in old::ListNodes::<T, I>::iter() {
let score = T::ScoreProvider::score(&node.id);
let new_node = crate::Node {
id: node.id.clone(),
prev: node.prev,
next: node.next,
bag_upper: node.bag_upper,
score,
_phantom: node._phantom,
};
crate::ListNodes::<T, I>::insert(node.id, new_node);
}
return frame_support::weights::Weight::MAX
}
#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
let node_count_before = old::TempStorage::<T, I>::take();
let iter_node_count_after: u32 = crate::ListNodes::<T, I>::iter().count() as u32;
let tracked_node_count_after: u32 = crate::ListNodes::<T, I>::count();
crate::log!(
info,
"number of nodes after: {:?} {:?}",
iter_node_count_after,
tracked_node_count_after,
);
ensure!(iter_node_count_after == node_count_before, "Not all nodes were migrated.");
ensure!(tracked_node_count_after == iter_node_count_after, "Node count is wrong.");
Ok(())
}
}