@@ -157,6 +157,7 @@ pub mod pallet {
157157 tokens:: { Fortitude , Precision , Preservation } ,
158158 StorageVersion ,
159159 } ,
160+ weights:: Weight ,
160161 } ;
161162 use frame_system:: pallet_prelude:: * ;
162163 use kilt_support:: {
@@ -165,7 +166,6 @@ pub mod pallet {
165166 } ;
166167 use service_endpoints:: DidEndpoint ;
167168 use sp_runtime:: traits:: { BadOrigin , IdentifyAccount } ;
168- use sp_weights:: Weight ;
169169
170170 use crate :: {
171171 did_details:: {
@@ -395,6 +395,8 @@ pub mod pallet {
395395 /// The new deposit owner.
396396 to : AccountIdOf < T > ,
397397 } ,
398+ CleanupIncomplete ,
399+ CleanupComplete ,
398400 }
399401
400402 #[ pallet:: error]
@@ -465,6 +467,7 @@ pub mod pallet {
465467 /// The DID is linked to other runtime elements that would be left
466468 /// dangling if the DID were to be deleted.
467469 NonZeroReferences ,
470+ TooExpensive ,
468471 /// An error that is not supposed to take place, yet it happened.
469472 Internal ,
470473 }
@@ -541,6 +544,7 @@ pub mod pallet {
541544 impl < T : Config > Pallet < T >
542545 where
543546 T :: AccountId : AsRef < [ u8 ; 32 ] > + From < [ u8 ; 32 ] > ,
547+ <<T :: DeletionHelper as DeletionHelper < T > >:: DeletionIter as Iterator >:: Item : SteppedDeletion ,
544548 {
545549 /// Store a new DID on chain, after verifying that the creation
546550 /// operation has been signed by the KILT account associated with the
@@ -1267,7 +1271,7 @@ pub mod pallet {
12671271
12681272 #[ pallet:: call_index( 17 ) ]
12691273 // TODO: Benchmark base call dispatch + the weight specified in the call
1270- #[ pallet:: weight( 1000 ) ]
1274+ #[ pallet:: weight( * max_weight ) ]
12711275 pub fn cleanup_linked_resources (
12721276 origin : OriginFor < T > ,
12731277 did : DidIdentifierOf < T > ,
@@ -1286,13 +1290,40 @@ pub mod pallet {
12861290 return Err ( DispatchError :: BadOrigin ) ;
12871291 } ;
12881292
1293+ // The cleanup logic does not need to get benchmarked as it consumes the
1294+ // provided weight.
1295+ #[ cfg( not( feature = "runtime-benchmarks" ) ) ]
1296+ {
1297+ let deletion_iter = T :: DeletionHelper :: deletion_iter ( & did) ;
1298+ let mut remaining_weight = max_weight;
1299+ for next_deletion_step in deletion_iter {
1300+ let Some ( ticket) = next_deletion_step. pre_check ( remaining_weight) else {
1301+ // If we run out of gas while there's still stuff to cleanup, apply the cleanups
1302+ // so far and generate an event.
1303+ Self :: deposit_event ( Event :: < T > :: CleanupIncomplete ) ;
1304+ return Ok ( ( ) ) ;
1305+ } ;
1306+ let consumed_weight = next_deletion_step. execute ( ticket) ;
1307+ // The `pre_check` should tell us if this step can underflow. In case this still
1308+ // happens, handle it accordingly.
1309+ let Some ( new_weight) = max_weight. checked_sub ( & consumed_weight) else {
1310+ Self :: deposit_event ( Event :: < T > :: CleanupIncomplete ) ;
1311+ return Ok ( ( ) ) ;
1312+ } ;
1313+ remaining_weight = new_weight;
1314+ }
1315+ // If the whole loop completes, the cleanup is successful.
1316+ Self :: deposit_event ( Event :: < T > :: CleanupComplete ) ;
1317+ }
1318+
12891319 Ok ( ( ) )
12901320 }
12911321 }
12921322
12931323 impl < T : Config > Pallet < T >
12941324 where
12951325 T :: AccountId : AsRef < [ u8 ; 32 ] > + From < [ u8 ; 32 ] > ,
1326+ <<T :: DeletionHelper as DeletionHelper < T > >:: DeletionIter as Iterator >:: Item : SteppedDeletion ,
12961327 {
12971328 /// Try creating a DID.
12981329 ///
@@ -1530,10 +1561,8 @@ pub mod pallet {
15301561 current_endpoints_count <= endpoints_to_remove,
15311562 Error :: <T >:: MaxStoredEndpointsCountExceeded
15321563 ) ;
1533- ensure ! (
1534- T :: DeletionHelper :: linked_resources_count( & did_subject) . is_zero( ) ,
1535- Error :: <T >:: NonZeroReferences
1536- ) ;
1564+ let is_deletion_possible = T :: DeletionHelper :: deletion_iter ( & did_subject) . next ( ) . is_none ( ) ;
1565+ ensure ! ( is_deletion_possible, Error :: <T >:: NonZeroReferences ) ;
15371566
15381567 // This one can fail, albeit this should **never** be the case as we check for
15391568 // the preconditions above.
0 commit comments