@@ -223,3 +223,90 @@ fn authorization_expires() {
223223 ) ;
224224 } ) ;
225225}
226+
227+ #[ test]
228+ fn expired_authorization_clears ( ) {
229+ new_test_ext ( ) . execute_with ( || {
230+ run_to_block ( 1 , || None ) ;
231+ let who = 1 ;
232+ assert_ok ! ( TransactionStorage :: <Test >:: authorize_account(
233+ RawOrigin :: Root . into( ) ,
234+ who,
235+ 2 ,
236+ 2000
237+ ) ) ;
238+ assert_eq ! (
239+ TransactionStorage :: <Test >:: account_authorization_extent( who) ,
240+ AuthorizationExtent { transactions: 2 , bytes: 2000 } ,
241+ ) ;
242+
243+ // User uses some of the authorization, and the remaining amount gets updated appropriately
244+ run_to_block ( 2 , || None ) ;
245+ let store_call = Call :: < Test > :: store { data : vec ! [ 0 ; 1000 ] } ;
246+ assert_ok ! ( TransactionStorage :: <Test >:: pre_dispatch_signed( & who, & store_call) ) ;
247+ assert_eq ! (
248+ TransactionStorage :: <Test >:: account_authorization_extent( who) ,
249+ AuthorizationExtent { transactions: 1 , bytes: 1000 } ,
250+ ) ;
251+
252+ // Can't remove too early
253+ run_to_block ( 10 , || None ) ;
254+ let remove_call = Call :: < Test > :: remove_expired_account_authorization { who } ;
255+ assert_noop ! (
256+ TransactionStorage :: <Test >:: pre_dispatch( & remove_call) ,
257+ AUTHORIZATION_NOT_EXPIRED ,
258+ ) ;
259+ assert_noop ! (
260+ Into :: <RuntimeCall >:: into( remove_call. clone( ) ) . dispatch( RawOrigin :: None . into( ) ) ,
261+ Error :: <Test >:: AuthorizationNotExpired ,
262+ ) ;
263+
264+ // User has sufficient storage authorization, but it has expired
265+ run_to_block ( 11 , || None ) ;
266+ assert ! ( Authorizations :: <Test >:: contains_key( AuthorizationScope :: Account ( who) ) ) ;
267+ // User cannot use authorization
268+ assert_noop ! (
269+ TransactionStorage :: <Test >:: pre_dispatch_signed( & who, & store_call) ,
270+ InvalidTransaction :: Payment ,
271+ ) ;
272+ // Anyone can remove it
273+ assert_ok ! ( TransactionStorage :: <Test >:: pre_dispatch( & remove_call) ) ;
274+ assert_ok ! ( Into :: <RuntimeCall >:: into( remove_call) . dispatch( RawOrigin :: None . into( ) ) ) ;
275+ System :: assert_has_event ( RuntimeEvent :: TransactionStorage (
276+ crate :: Event :: ExpiredAccountAuthorizationRemoved { who } ,
277+ ) ) ;
278+ // No longer in storage
279+ assert ! ( !Authorizations :: <Test >:: contains_key( AuthorizationScope :: Account ( who) ) ) ;
280+ } ) ;
281+ }
282+
283+ #[ test]
284+ fn consumed_authorization_clears ( ) {
285+ new_test_ext ( ) . execute_with ( || {
286+ run_to_block ( 1 , || None ) ;
287+ let who = 1 ;
288+ assert_ok ! ( TransactionStorage :: <Test >:: authorize_account(
289+ RawOrigin :: Root . into( ) ,
290+ who,
291+ 2 ,
292+ 2000
293+ ) ) ;
294+ assert_eq ! (
295+ TransactionStorage :: <Test >:: account_authorization_extent( who) ,
296+ AuthorizationExtent { transactions: 2 , bytes: 2000 } ,
297+ ) ;
298+
299+ // User uses some of the authorization, and the remaining amount gets updated appropriately
300+ let call = Call :: < Test > :: store { data : vec ! [ 0 ; 1000 ] } ;
301+ assert_ok ! ( TransactionStorage :: <Test >:: pre_dispatch_signed( & who, & call) ) ;
302+ // Debited half the authorization
303+ assert_eq ! (
304+ TransactionStorage :: <Test >:: account_authorization_extent( who) ,
305+ AuthorizationExtent { transactions: 1 , bytes: 1000 } ,
306+ ) ;
307+ // Consume the remaining amount
308+ assert_ok ! ( TransactionStorage :: <Test >:: pre_dispatch_signed( & who, & call) ) ;
309+ // Key should be cleared from Authorizations
310+ assert ! ( !Authorizations :: <Test >:: contains_key( AuthorizationScope :: Account ( who) ) ) ;
311+ } ) ;
312+ }
0 commit comments