-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathmigrations.rs
127 lines (106 loc) · 4.53 KB
/
migrations.rs
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
// This file is part of Astar.
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use sp_arithmetic::fixed_point::FixedU64;
/// `OnRuntimeUpgrade` logic used to set & configure init dApp staking v3 storage items.
pub struct DAppStakingV3InitConfig<T, G, P>(PhantomData<(T, G, P)>);
impl<
T: Config,
G: Get<(
EraNumber,
TierParameters<T::NumberOfTiers>,
TiersConfiguration<T::NumberOfTiers>,
)>,
P: Get<FixedU64>,
> OnRuntimeUpgrade for DAppStakingV3InitConfig<T, G, P>
{
fn on_runtime_upgrade() -> Weight {
if Pallet::<T>::on_chain_storage_version() >= STORAGE_VERSION {
return T::DbWeight::get().reads(1);
}
// 0. Unwrap arguments
let (init_era, tier_params, base_tier_config) = G::get();
// 1. Prepare init active protocol state
let now = frame_system::Pallet::<T>::block_number();
let voting_period_length = Pallet::<T>::blocks_per_voting_period();
let period_number = 1;
let protocol_state = ProtocolState {
era: init_era,
next_era_start: now.saturating_add(voting_period_length),
period_info: PeriodInfo {
number: period_number,
subperiod: Subperiod::Voting,
next_subperiod_start_era: init_era.saturating_add(1),
},
maintenance: true,
};
// 2. Prepare init current era info - need to set correct eras
let init_era_info = EraInfo {
total_locked: 0,
unlocking: 0,
current_stake_amount: StakeAmount {
voting: 0,
build_and_earn: 0,
era: init_era,
period: period_number,
},
next_stake_amount: StakeAmount {
voting: 0,
build_and_earn: 0,
era: init_era.saturating_add(1),
period: period_number,
},
};
let average_price = P::get();
let init_tier_config = base_tier_config.calculate_new(average_price, &tier_params);
// 3. Write necessary items into storage
ActiveProtocolState::<T>::put(protocol_state);
StaticTierParams::<T>::put(tier_params);
TierConfig::<T>::put(init_tier_config);
STORAGE_VERSION.put::<Pallet<T>>();
CurrentEraInfo::<T>::put(init_era_info);
// 4. Emit events to make indexers happy
Pallet::<T>::deposit_event(Event::<T>::NewEra { era: init_era });
Pallet::<T>::deposit_event(Event::<T>::NewSubperiod {
subperiod: Subperiod::Voting,
number: 1,
});
log::info!("dApp Staking v3 storage initialized.");
T::DbWeight::get().reads_writes(2, 5)
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
assert_eq!(Pallet::<T>::on_chain_storage_version(), STORAGE_VERSION);
let protocol_state = ActiveProtocolState::<T>::get();
assert!(protocol_state.maintenance);
let number_of_tiers = T::NumberOfTiers::get();
let tier_params = StaticTierParams::<T>::get();
assert_eq!(tier_params.reward_portion.len(), number_of_tiers as usize);
assert!(tier_params.is_valid());
let tier_config = TierConfig::<T>::get();
assert_eq!(tier_config.reward_portion.len(), number_of_tiers as usize);
assert_eq!(tier_config.slots_per_tier.len(), number_of_tiers as usize);
assert_eq!(tier_config.tier_thresholds.len(), number_of_tiers as usize);
let current_era_info = CurrentEraInfo::<T>::get();
assert_eq!(
current_era_info.current_stake_amount.era,
protocol_state.era
);
assert_eq!(
current_era_info.next_stake_amount.era,
protocol_state.era + 1
);
Ok(())
}
}