Skip to content

Commit 5b47c11

Browse files
notleshpeektism
andauthored
Fix staking try runtime (attempt #2) (#1056)
* Add missing imports * Make staking compile for try-runtime tests (#1014) * fix * fix lack of From impl compilation error Co-authored-by: Amar Singh <[email protected]>
1 parent 0fd10fd commit 5b47c11

2 files changed

Lines changed: 105 additions & 10 deletions

File tree

pallets/parachain-staking/src/lib.rs

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,63 @@ pub mod pallet {
208208
pub state: CollatorStatus,
209209
}
210210

211+
// Temporary manual implementation for migration testing purposes
212+
impl<A: PartialEq, B: PartialEq> PartialEq for CollatorCandidate<A, B> {
213+
fn eq(&self, other: &Self) -> bool {
214+
let must_be_true = self.id == other.id
215+
&& self.bond == other.bond
216+
&& self.total_counted == other.total_counted
217+
&& self.total_backing == other.total_backing
218+
&& self.request == other.request
219+
&& self.state == other.state;
220+
if !must_be_true {
221+
return false;
222+
}
223+
for (x, y) in self.delegators.0.iter().zip(other.delegators.0.iter()) {
224+
if x != y {
225+
return false;
226+
}
227+
}
228+
for (
229+
Bond {
230+
owner: o1,
231+
amount: a1,
232+
},
233+
Bond {
234+
owner: o2,
235+
amount: a2,
236+
},
237+
) in self
238+
.top_delegations
239+
.iter()
240+
.zip(other.top_delegations.iter())
241+
{
242+
if o1 != o2 || a1 != a2 {
243+
return false;
244+
}
245+
}
246+
for (
247+
Bond {
248+
owner: o1,
249+
amount: a1,
250+
},
251+
Bond {
252+
owner: o2,
253+
amount: a2,
254+
},
255+
) in self
256+
.bottom_delegations
257+
.iter()
258+
.zip(other.bottom_delegations.iter())
259+
{
260+
if o1 != o2 || a1 != a2 {
261+
return false;
262+
}
263+
}
264+
true
265+
}
266+
}
267+
211268
/// Convey relevant information describing if a delegator was added to the top or bottom
212269
/// Delegations added to the top yield a new total
213270
#[derive(Clone, Copy, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
@@ -634,6 +691,35 @@ pub mod pallet {
634691
pub status: DelegatorStatus,
635692
}
636693

694+
// Temporary manual implementation for migration testing purposes
695+
impl<A: PartialEq, B: PartialEq> PartialEq for Delegator<A, B> {
696+
fn eq(&self, other: &Self) -> bool {
697+
let must_be_true = self.id == other.id
698+
&& self.total == other.total
699+
&& self.requests == other.requests
700+
&& self.status == other.status;
701+
if !must_be_true {
702+
return false;
703+
}
704+
for (
705+
Bond {
706+
owner: o1,
707+
amount: a1,
708+
},
709+
Bond {
710+
owner: o2,
711+
amount: a2,
712+
},
713+
) in self.delegations.0.iter().zip(other.delegations.0.iter())
714+
{
715+
if o1 != o2 || a1 != a2 {
716+
return false;
717+
}
718+
}
719+
true
720+
}
721+
}
722+
637723
impl<
638724
AccountId: Ord + Clone + Default,
639725
Balance: Copy
@@ -1005,7 +1091,7 @@ pub mod pallet {
10051091
pub action: DelegationChange,
10061092
}
10071093

1008-
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
1094+
#[derive(Clone, Encode, PartialEq, Decode, RuntimeDebug, TypeInfo)]
10091095
/// Pending requests to mutate delegations for each delegator
10101096
pub struct PendingDelegationRequests<AccountId, Balance> {
10111097
/// Number of pending revocations (necessary for determining whether revoke is exit)

pallets/parachain-staking/src/migrations.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use crate::{
2020
BalanceOf, CandidateState, CollatorCandidate, CollatorState2, Config, DelegatorState,
2121
ExitQueue2, NominatorState2, Points, Round, Staked,
2222
};
23+
#[cfg(feature = "try-runtime")]
24+
use crate::{Collator2, Delegator, Nominator2};
25+
#[cfg(feature = "try-runtime")]
26+
use frame_support::Twox64Concat;
2327
use frame_support::{
2428
pallet_prelude::PhantomData,
2529
traits::{Get, OnRuntimeUpgrade},
@@ -83,7 +87,10 @@ impl<T: Config> OnRuntimeUpgrade for RemoveExitQueue<T> {
8387

8488
#[cfg(feature = "try-runtime")]
8589
fn pre_upgrade() -> Result<(), &'static str> {
86-
use frame_support::{storage::migration::storage_iter, traits::OnRuntimeUpgradeHelpersExt};
90+
use frame_support::{
91+
storage::migration::{storage_iter, storage_key_iter},
92+
traits::OnRuntimeUpgradeHelpersExt,
93+
};
8794

8895
let pallet_prefix: &[u8] = b"ParachainStaking";
8996
let collator_state_prefix: &[u8] = b"CollatorState2";
@@ -150,11 +157,12 @@ impl<T: Config> OnRuntimeUpgrade for RemoveExitQueue<T> {
150157
// Check that our example candidate is converted correctly
151158
if new_candidate_count > 0 {
152159
let (account, original_collator_state): (
153-
T::AuthorId,
160+
T::AccountId,
154161
Collator2<T::AccountId, BalanceOf<T>>,
155162
) = Self::get_temp_storage("example_collator").expect("qed");
156163
let new_candidate_state = CandidateState::<T>::get(account).expect("qed");
157-
let old_candidate_converted: CollatorCandidate<_, _> = original_collator_state.into();
164+
let old_candidate_converted: CollatorCandidate<T::AccountId, BalanceOf<T>> =
165+
original_collator_state.into();
158166
assert_eq!(new_candidate_state, old_candidate_converted);
159167
}
160168

@@ -164,15 +172,16 @@ impl<T: Config> OnRuntimeUpgrade for RemoveExitQueue<T> {
164172
let new_delegator_count = DelegatorState::<T>::iter().count() as u64;
165173
assert_eq!(old_nominator_count, new_delegator_count);
166174

167-
// Check that our example nominator is converted correctly
175+
// Check that our example delegator is converted correctly
168176
if new_delegator_count > 0 {
169-
let (account, original_nominator_state): (
170-
T::AuthorId,
177+
let (account, original_delegator_state): (
178+
T::AccountId,
171179
Nominator2<T::AccountId, BalanceOf<T>>,
172180
) = Self::get_temp_storage("example_nominator").expect("qed");
173-
let new_candidate_state = DelegatorState::<T>::get(account).expect("qed");
174-
let old_candidate_converted: Delegator<_, _> = original_nominator_state.into();
175-
assert_eq!(old_candidate_converted, new_candidate_state);
181+
let new_delegator_state = DelegatorState::<T>::get(&account).expect("qed");
182+
let old_delegator_converted: Delegator<T::AccountId, BalanceOf<T>> =
183+
migrate_nominator_to_delegator_state::<T>(account, original_delegator_state);
184+
assert_eq!(old_delegator_converted, new_delegator_state);
176185
}
177186
Ok(())
178187
}

0 commit comments

Comments
 (0)