|
16 | 16 | // limitations under the License. |
17 | 17 |
|
18 | 18 | use frame_support::{ |
19 | | - assert_noop, assert_ok, |
20 | | - dispatch::{DispatchError, DispatchResult}, |
| 19 | + assert_noop, assert_ok, dispatch::DispatchResult, pallet_prelude::ConstU32, |
21 | 20 | storage::with_storage_layer, |
22 | | - StorageMap, StorageValue, |
23 | 21 | }; |
| 22 | +use pallet::*; |
24 | 23 | use sp_io::TestExternalities; |
25 | 24 |
|
26 | | -pub trait Config: frame_support_test::Config {} |
| 25 | +#[frame_support::pallet] |
| 26 | +pub mod pallet { |
| 27 | + use frame_support::{ensure, pallet_prelude::*}; |
| 28 | + use frame_system::pallet_prelude::*; |
27 | 29 |
|
28 | | -frame_support::decl_module! { |
29 | | - pub struct Module<T: Config> for enum Call where origin: T::Origin, system=frame_support_test { |
30 | | - #[weight = 0] |
31 | | - fn value_commits(_origin, v: u32) { |
32 | | - Value::set(v); |
33 | | - } |
| 30 | + #[pallet::pallet] |
| 31 | + pub struct Pallet<T>(_); |
34 | 32 |
|
35 | | - #[weight = 0] |
36 | | - fn value_rollbacks(_origin, v: u32) -> DispatchResult { |
37 | | - Value::set(v); |
38 | | - Err(DispatchError::Other("nah")) |
39 | | - } |
| 33 | + #[pallet::config] |
| 34 | + pub trait Config: frame_system::Config {} |
| 35 | + |
| 36 | + #[pallet::storage] |
| 37 | + pub type Value<T> = StorageValue<_, u32, ValueQuery>; |
| 38 | + |
| 39 | + #[pallet::storage] |
| 40 | + pub type Map<T> = StorageMap<_, Blake2_128Concat, u32, u32, ValueQuery>; |
| 41 | + |
| 42 | + #[pallet::error] |
| 43 | + pub enum Error<T> { |
| 44 | + Revert, |
40 | 45 | } |
41 | | -} |
42 | 46 |
|
43 | | -frame_support::decl_storage! { |
44 | | - trait Store for Module<T: Config> as StorageTransactions { |
45 | | - pub Value: u32; |
46 | | - pub Map: map hasher(twox_64_concat) String => u32; |
| 47 | + #[pallet::call] |
| 48 | + impl<T: Config> Pallet<T> { |
| 49 | + #[pallet::weight(1)] |
| 50 | + pub fn set_value(_origin: OriginFor<T>, value: u32) -> DispatchResult { |
| 51 | + Value::<T>::put(value); |
| 52 | + ensure!(value != 1, Error::<T>::Revert); |
| 53 | + Ok(()) |
| 54 | + } |
47 | 55 | } |
48 | 56 | } |
49 | 57 |
|
50 | | -struct Runtime; |
| 58 | +pub type BlockNumber = u64; |
| 59 | +pub type Index = u64; |
| 60 | +pub type Header = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo256>; |
| 61 | +pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>; |
| 62 | +pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, (), ()>; |
51 | 63 |
|
52 | | -impl frame_support_test::Config for Runtime { |
53 | | - type Origin = u32; |
54 | | - type BlockNumber = u32; |
55 | | - type PalletInfo = frame_support_test::PanicPalletInfo; |
| 64 | +impl frame_system::Config for Runtime { |
| 65 | + type BlockWeights = (); |
| 66 | + type BlockLength = (); |
56 | 67 | type DbWeight = (); |
| 68 | + type BaseCallFilter = frame_support::traits::Everything; |
| 69 | + type Origin = Origin; |
| 70 | + type Index = u64; |
| 71 | + type BlockNumber = u32; |
| 72 | + type Call = Call; |
| 73 | + type Hash = sp_runtime::testing::H256; |
| 74 | + type Hashing = sp_runtime::traits::BlakeTwo256; |
| 75 | + type AccountId = u64; |
| 76 | + type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>; |
| 77 | + type Header = Header; |
| 78 | + type Event = Event; |
| 79 | + type BlockHashCount = ConstU32<250>; |
| 80 | + type Version = (); |
| 81 | + type PalletInfo = PalletInfo; |
| 82 | + type AccountData = (); |
| 83 | + type OnNewAccount = (); |
| 84 | + type OnKilledAccount = (); |
| 85 | + type SystemWeightInfo = (); |
| 86 | + type SS58Prefix = (); |
| 87 | + type OnSetCode = (); |
| 88 | + type MaxConsumers = ConstU32<16>; |
57 | 89 | } |
58 | 90 |
|
59 | | -impl Config for Runtime {} |
| 91 | +impl pallet::Config for Runtime {} |
| 92 | + |
| 93 | +frame_support::construct_runtime!( |
| 94 | + pub enum Runtime where |
| 95 | + Block = Block, |
| 96 | + NodeBlock = Block, |
| 97 | + UncheckedExtrinsic = UncheckedExtrinsic |
| 98 | + { |
| 99 | + System: frame_system, |
| 100 | + MyPallet: pallet, |
| 101 | + } |
| 102 | +); |
60 | 103 |
|
61 | 104 | #[test] |
62 | 105 | fn storage_transaction_basic_commit() { |
63 | 106 | TestExternalities::default().execute_with(|| { |
64 | | - assert_eq!(Value::get(), 0); |
65 | | - assert!(!Map::contains_key("val0")); |
| 107 | + assert_eq!(Value::<Runtime>::get(), 0); |
| 108 | + assert!(!Map::<Runtime>::contains_key(0)); |
66 | 109 |
|
67 | 110 | assert_ok!(with_storage_layer(|| -> DispatchResult { |
68 | | - Value::set(99); |
69 | | - Map::insert("val0", 99); |
70 | | - assert_eq!(Value::get(), 99); |
71 | | - assert_eq!(Map::get("val0"), 99); |
| 111 | + Value::<Runtime>::set(99); |
| 112 | + Map::<Runtime>::insert(0, 99); |
| 113 | + assert_eq!(Value::<Runtime>::get(), 99); |
| 114 | + assert_eq!(Map::<Runtime>::get(0), 99); |
72 | 115 | Ok(()) |
73 | 116 | })); |
74 | 117 |
|
75 | | - assert_eq!(Value::get(), 99); |
76 | | - assert_eq!(Map::get("val0"), 99); |
| 118 | + assert_eq!(Value::<Runtime>::get(), 99); |
| 119 | + assert_eq!(Map::<Runtime>::get(0), 99); |
77 | 120 | }); |
78 | 121 | } |
79 | 122 |
|
80 | 123 | #[test] |
81 | 124 | fn storage_transaction_basic_rollback() { |
82 | 125 | TestExternalities::default().execute_with(|| { |
83 | | - assert_eq!(Value::get(), 0); |
84 | | - assert_eq!(Map::get("val0"), 0); |
| 126 | + assert_eq!(Value::<Runtime>::get(), 0); |
| 127 | + assert_eq!(Map::<Runtime>::get(0), 0); |
85 | 128 |
|
86 | 129 | assert_noop!( |
87 | 130 | with_storage_layer(|| -> DispatchResult { |
88 | | - Value::set(99); |
89 | | - Map::insert("val0", 99); |
90 | | - assert_eq!(Value::get(), 99); |
91 | | - assert_eq!(Map::get("val0"), 99); |
| 131 | + Value::<Runtime>::set(99); |
| 132 | + Map::<Runtime>::insert(0, 99); |
| 133 | + assert_eq!(Value::<Runtime>::get(), 99); |
| 134 | + assert_eq!(Map::<Runtime>::get(0), 99); |
92 | 135 | Err("revert".into()) |
93 | 136 | }), |
94 | 137 | "revert" |
95 | 138 | ); |
96 | 139 |
|
97 | | - assert_eq!(Value::get(), 0); |
98 | | - assert_eq!(Map::get("val0"), 0); |
| 140 | + assert_eq!(Value::<Runtime>::get(), 0); |
| 141 | + assert_eq!(Map::<Runtime>::get(0), 0); |
99 | 142 | }); |
100 | 143 | } |
101 | 144 |
|
102 | 145 | #[test] |
103 | 146 | fn storage_transaction_rollback_then_commit() { |
104 | 147 | TestExternalities::default().execute_with(|| { |
105 | | - Value::set(1); |
106 | | - Map::insert("val1", 1); |
| 148 | + Value::<Runtime>::set(1); |
| 149 | + Map::<Runtime>::insert(1, 1); |
107 | 150 |
|
108 | 151 | assert_ok!(with_storage_layer(|| -> DispatchResult { |
109 | | - Value::set(2); |
110 | | - Map::insert("val1", 2); |
111 | | - Map::insert("val2", 2); |
| 152 | + Value::<Runtime>::set(2); |
| 153 | + Map::<Runtime>::insert(1, 2); |
| 154 | + Map::<Runtime>::insert(2, 2); |
112 | 155 |
|
113 | 156 | assert_noop!( |
114 | 157 | with_storage_layer(|| -> DispatchResult { |
115 | | - Value::set(3); |
116 | | - Map::insert("val1", 3); |
117 | | - Map::insert("val2", 3); |
118 | | - Map::insert("val3", 3); |
| 158 | + Value::<Runtime>::set(3); |
| 159 | + Map::<Runtime>::insert(1, 3); |
| 160 | + Map::<Runtime>::insert(2, 3); |
| 161 | + Map::<Runtime>::insert(3, 3); |
119 | 162 |
|
120 | | - assert_eq!(Value::get(), 3); |
121 | | - assert_eq!(Map::get("val1"), 3); |
122 | | - assert_eq!(Map::get("val2"), 3); |
123 | | - assert_eq!(Map::get("val3"), 3); |
| 163 | + assert_eq!(Value::<Runtime>::get(), 3); |
| 164 | + assert_eq!(Map::<Runtime>::get(1), 3); |
| 165 | + assert_eq!(Map::<Runtime>::get(2), 3); |
| 166 | + assert_eq!(Map::<Runtime>::get(3), 3); |
124 | 167 |
|
125 | 168 | Err("revert".into()) |
126 | 169 | }), |
127 | 170 | "revert" |
128 | 171 | ); |
129 | 172 |
|
130 | | - assert_eq!(Value::get(), 2); |
131 | | - assert_eq!(Map::get("val1"), 2); |
132 | | - assert_eq!(Map::get("val2"), 2); |
133 | | - assert_eq!(Map::get("val3"), 0); |
| 173 | + assert_eq!(Value::<Runtime>::get(), 2); |
| 174 | + assert_eq!(Map::<Runtime>::get(1), 2); |
| 175 | + assert_eq!(Map::<Runtime>::get(2), 2); |
| 176 | + assert_eq!(Map::<Runtime>::get(3), 0); |
134 | 177 |
|
135 | 178 | Ok(()) |
136 | 179 | })); |
137 | 180 |
|
138 | | - assert_eq!(Value::get(), 2); |
139 | | - assert_eq!(Map::get("val1"), 2); |
140 | | - assert_eq!(Map::get("val2"), 2); |
141 | | - assert_eq!(Map::get("val3"), 0); |
| 181 | + assert_eq!(Value::<Runtime>::get(), 2); |
| 182 | + assert_eq!(Map::<Runtime>::get(1), 2); |
| 183 | + assert_eq!(Map::<Runtime>::get(2), 2); |
| 184 | + assert_eq!(Map::<Runtime>::get(3), 0); |
142 | 185 | }); |
143 | 186 | } |
144 | 187 |
|
145 | 188 | #[test] |
146 | 189 | fn storage_transaction_commit_then_rollback() { |
147 | 190 | TestExternalities::default().execute_with(|| { |
148 | | - Value::set(1); |
149 | | - Map::insert("val1", 1); |
| 191 | + Value::<Runtime>::set(1); |
| 192 | + Map::<Runtime>::insert(1, 1); |
150 | 193 |
|
151 | 194 | assert_noop!( |
152 | 195 | with_storage_layer(|| -> DispatchResult { |
153 | | - Value::set(2); |
154 | | - Map::insert("val1", 2); |
155 | | - Map::insert("val2", 2); |
| 196 | + Value::<Runtime>::set(2); |
| 197 | + Map::<Runtime>::insert(1, 2); |
| 198 | + Map::<Runtime>::insert(2, 2); |
156 | 199 |
|
157 | 200 | assert_ok!(with_storage_layer(|| -> DispatchResult { |
158 | | - Value::set(3); |
159 | | - Map::insert("val1", 3); |
160 | | - Map::insert("val2", 3); |
161 | | - Map::insert("val3", 3); |
| 201 | + Value::<Runtime>::set(3); |
| 202 | + Map::<Runtime>::insert(1, 3); |
| 203 | + Map::<Runtime>::insert(2, 3); |
| 204 | + Map::<Runtime>::insert(3, 3); |
162 | 205 |
|
163 | | - assert_eq!(Value::get(), 3); |
164 | | - assert_eq!(Map::get("val1"), 3); |
165 | | - assert_eq!(Map::get("val2"), 3); |
166 | | - assert_eq!(Map::get("val3"), 3); |
| 206 | + assert_eq!(Value::<Runtime>::get(), 3); |
| 207 | + assert_eq!(Map::<Runtime>::get(1), 3); |
| 208 | + assert_eq!(Map::<Runtime>::get(2), 3); |
| 209 | + assert_eq!(Map::<Runtime>::get(3), 3); |
167 | 210 |
|
168 | 211 | Ok(()) |
169 | 212 | })); |
170 | 213 |
|
171 | | - assert_eq!(Value::get(), 3); |
172 | | - assert_eq!(Map::get("val1"), 3); |
173 | | - assert_eq!(Map::get("val2"), 3); |
174 | | - assert_eq!(Map::get("val3"), 3); |
| 214 | + assert_eq!(Value::<Runtime>::get(), 3); |
| 215 | + assert_eq!(Map::<Runtime>::get(1), 3); |
| 216 | + assert_eq!(Map::<Runtime>::get(2), 3); |
| 217 | + assert_eq!(Map::<Runtime>::get(3), 3); |
175 | 218 |
|
176 | 219 | Err("revert".into()) |
177 | 220 | }), |
178 | 221 | "revert" |
179 | 222 | ); |
180 | 223 |
|
181 | | - assert_eq!(Value::get(), 1); |
182 | | - assert_eq!(Map::get("val1"), 1); |
183 | | - assert_eq!(Map::get("val2"), 0); |
184 | | - assert_eq!(Map::get("val3"), 0); |
| 224 | + assert_eq!(Value::<Runtime>::get(), 1); |
| 225 | + assert_eq!(Map::<Runtime>::get(1), 1); |
| 226 | + assert_eq!(Map::<Runtime>::get(2), 0); |
| 227 | + assert_eq!(Map::<Runtime>::get(3), 0); |
185 | 228 | }); |
186 | 229 | } |
187 | 230 |
|
188 | 231 | #[test] |
189 | 232 | fn storage_layer_in_decl_module() { |
190 | 233 | TestExternalities::default().execute_with(|| { |
191 | | - let origin = 0; |
192 | | - assert_ok!(<Module<Runtime>>::value_commits(origin, 2)); |
193 | | - assert_eq!(Value::get(), 2); |
| 234 | + use sp_runtime::traits::Dispatchable; |
| 235 | + let call1 = Call::MyPallet(pallet::Call::set_value { value: 2 }); |
| 236 | + assert_ok!(call1.dispatch(Origin::signed(0))); |
| 237 | + assert_eq!(Value::<Runtime>::get(), 2); |
194 | 238 |
|
195 | | - assert_noop!(<Module<Runtime>>::value_rollbacks(origin, 3), "nah"); |
| 239 | + let call2 = Call::MyPallet(pallet::Call::set_value { value: 1 }); |
| 240 | + assert_noop!(call2.dispatch(Origin::signed(0)), Error::<Runtime>::Revert); |
196 | 241 | }); |
197 | 242 | } |
0 commit comments