16
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
*/
18
18
#![ cfg_attr( not( feature = "std" ) , no_std) ]
19
+
19
20
#[ cfg( feature = "runtime-benchmarks" ) ]
20
21
mod benchmarking;
22
+
21
23
#[ cfg( test) ]
22
24
mod tests;
23
25
26
+ mod migrations;
27
+
28
+ use codec:: { Decode , Encode } ;
24
29
use frame_support:: {
25
- dispatch:: Weight ,
26
30
ensure,
27
- migration :: remove_storage_prefix ,
28
- traits:: { tokens:: ExistenceRequirement , ChangeMembers , Currency , Get , InitializeMembers } ,
29
- transactional, PalletId ,
31
+ pallet_prelude :: MaxEncodedLen ,
32
+ traits:: { tokens:: ExistenceRequirement , Contains , Currency , Get } ,
33
+ transactional, BoundedVec , PalletId ,
30
34
} ;
31
- use frame_system:: ensure_signed;
32
- use sp_std:: prelude:: * ;
33
- use support:: WithAccountId ;
34
35
36
+ use frame_system:: ensure_signed;
37
+ use scale_info:: TypeInfo ;
38
+ use sp_runtime:: traits:: AccountIdConversion ;
35
39
use sp_runtime:: {
36
- traits:: { AccountIdConversion , CheckedAdd , Saturating , Zero } ,
37
- DispatchResult , Perbill ,
40
+ traits:: { CheckedAdd , Saturating , Zero } ,
41
+ DispatchResult , Perbill , RuntimeDebug ,
38
42
} ;
43
+ use sp_std:: prelude:: * ;
44
+ use support:: WithAccountId ;
39
45
40
46
pub mod weights;
41
47
pub use weights:: WeightInfo ;
@@ -44,10 +50,26 @@ pub use pallet::*;
44
50
45
51
type BalanceOf < T > = <<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: Balance ;
46
52
53
+ // A value placed in storage that represents the current version of the Allocations storage.
54
+ // This value is used by the `on_runtime_upgrade` logic to determine whether we run storage
55
+ // migration logic. This should match directly with the semantic versions of the Rust crate.
56
+ #[ derive( Encode , Decode , MaxEncodedLen , Clone , Copy , PartialEq , Eq , RuntimeDebug , TypeInfo ) ]
57
+ enum Releases {
58
+ V0_0_0Legacy , // To handle Legacy version
59
+ V2_0_21 ,
60
+ }
61
+
62
+ impl Default for Releases {
63
+ fn default ( ) -> Self {
64
+ Releases :: V0_0_0Legacy
65
+ }
66
+ }
67
+
47
68
#[ frame_support:: pallet]
48
69
pub mod pallet {
49
70
use super :: * ;
50
71
use frame_support:: pallet_prelude:: * ;
72
+ use frame_support:: traits:: OnRuntimeUpgrade ;
51
73
use frame_system:: pallet_prelude:: * ;
52
74
53
75
#[ pallet:: config]
@@ -71,20 +93,30 @@ pub mod pallet {
71
93
#[ pallet:: constant]
72
94
type MaxAllocs : Get < u32 > ;
73
95
96
+ type OracleMembers : Contains < Self :: AccountId > ;
97
+
74
98
/// Weight information for extrinsics in this pallet.
75
99
type WeightInfo : WeightInfo ;
76
100
}
77
101
78
102
#[ pallet:: pallet]
79
103
#[ pallet:: generate_store( pub ( super ) trait Store ) ]
80
- #[ pallet:: without_storage_info]
81
104
pub struct Pallet < T > ( PhantomData < T > ) ;
82
105
83
106
#[ pallet:: hooks]
84
107
impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
85
- fn on_runtime_upgrade ( ) -> Weight {
86
- remove_storage_prefix ( <Pallet < T > >:: name ( ) . as_bytes ( ) , b"CoinsConsumed" , b"" ) ;
87
- T :: DbWeight :: get ( ) . writes ( 1 )
108
+ #[ cfg( feature = "try-runtime" ) ]
109
+ fn pre_upgrade ( ) -> Result < ( ) , & ' static str > {
110
+ migrations:: v1:: MigrateToBoundedOracles :: < T > :: pre_upgrade ( )
111
+ }
112
+
113
+ fn on_runtime_upgrade ( ) -> frame_support:: weights:: Weight {
114
+ migrations:: v1:: MigrateToBoundedOracles :: < T > :: on_runtime_upgrade ( )
115
+ }
116
+
117
+ #[ cfg( feature = "try-runtime" ) ]
118
+ fn post_upgrade ( ) -> Result < ( ) , & ' static str > {
119
+ migrations:: v1:: MigrateToBoundedOracles :: < T > :: post_upgrade ( )
88
120
}
89
121
}
90
122
@@ -152,8 +184,8 @@ pub mod pallet {
152
184
/// Can only be called by an oracle, trigger a token mint and dispatch to
153
185
/// `amount`, minus protocol fees
154
186
#[ pallet:: weight(
155
- <T as pallet:: Config >:: WeightInfo :: allocate( )
156
- ) ]
187
+ <T as pallet:: Config >:: WeightInfo :: allocate( )
188
+ ) ]
157
189
// we add the `transactional` modifier here in the event that one of the
158
190
// transfers fail. the code itself should already prevent this but we add
159
191
// this as an additional guarantee.
@@ -185,13 +217,21 @@ pub mod pallet {
185
217
}
186
218
187
219
#[ pallet:: storage]
188
- #[ pallet:: getter( fn oracles) ]
189
- pub type Oracles < T : Config > = StorageValue < _ , Vec < T :: AccountId > , ValueQuery > ;
220
+ pub ( crate ) type StorageVersion < T : Config > = StorageValue < _ , Releases , ValueQuery > ;
221
+
222
+ #[ cfg( feature = "runtime-benchmarks" ) ]
223
+ #[ pallet:: storage]
224
+ #[ pallet:: getter( fn benchmark_oracles) ]
225
+ pub type BenchmarkOracles < T : Config > =
226
+ StorageValue < _ , BoundedVec < T :: AccountId , benchmarking:: MaxMembers > , ValueQuery > ;
190
227
}
191
228
192
229
impl < T : Config > Pallet < T > {
193
230
pub fn is_oracle ( who : T :: AccountId ) -> bool {
194
- Self :: oracles ( ) . contains ( & who)
231
+ #[ cfg( feature = "runtime-benchmarks" ) ]
232
+ return T :: OracleMembers :: contains ( & who) || Self :: benchmark_oracles ( ) . contains ( & who) ;
233
+ #[ cfg( not( feature = "runtime-benchmarks" ) ) ]
234
+ return T :: OracleMembers :: contains ( & who) ;
195
235
}
196
236
197
237
fn ensure_oracle ( origin : T :: Origin ) -> DispatchResult {
@@ -200,15 +240,3 @@ impl<T: Config> Pallet<T> {
200
240
Ok ( ( ) )
201
241
}
202
242
}
203
-
204
- impl < T : Config > ChangeMembers < T :: AccountId > for Pallet < T > {
205
- fn change_members_sorted ( _incoming : & [ T :: AccountId ] , _outgoing : & [ T :: AccountId ] , new : & [ T :: AccountId ] ) {
206
- <Oracles < T > >:: put ( new) ;
207
- }
208
- }
209
-
210
- impl < T : Config > InitializeMembers < T :: AccountId > for Pallet < T > {
211
- fn initialize_members ( init : & [ T :: AccountId ] ) {
212
- <Oracles < T > >:: put ( init) ;
213
- }
214
- }
0 commit comments