Skip to content

Commit 844672b

Browse files
committed
Integrate trait into did pallet
1 parent 25ca564 commit 844672b

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

pallets/did/src/lib.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

pallets/did/src/signature.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ use sp_weights::Weight;
2424
use crate::{
2525
did_details::{DidSignature, DidVerificationKeyRelationship},
2626
errors::DidError,
27+
traits::{DeletionHelper, SteppedDeletion},
2728
Config, Did, Pallet, WeightInfo,
2829
};
2930

3031
pub struct DidSignatureVerify<T>(PhantomData<T>);
3132
impl<T: Config> VerifySignature for DidSignatureVerify<T>
3233
where
3334
T::AccountId: AsRef<[u8; 32]> + From<[u8; 32]>,
35+
<<T::DeletionHelper as DeletionHelper<T>>::DeletionIter as Iterator>::Item: SteppedDeletion,
3436
{
3537
type SignerId = <T as Config>::DidIdentifier;
3638
type Payload = Vec<u8>;

pallets/did/src/traits.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ use crate::{Config, DidIdentifierOf};
2525
pub trait DeletionHelper<T>
2626
where
2727
T: Config,
28-
<Self::DeletionIter as Iterator>::Item: SteppedDeletion,
2928
{
3029
type DeletionIter: Iterator;
3130

32-
fn deletion_iter(did: &DidIdentifierOf<T>) -> Self::DeletionIter;
31+
fn deletion_iter(did: &DidIdentifierOf<T>) -> Self::DeletionIter
32+
where
33+
<Self::DeletionIter as Iterator>::Item: SteppedDeletion;
3334
}
3435

3536
impl<T> DeletionHelper<T> for ()
@@ -56,17 +57,19 @@ impl Iterator for EmptyIterator {
5657
pub trait SteppedDeletion {
5758
type VerifiedInfo;
5859

59-
fn pre_check(remaining_weight: Weight) -> Option<Self::VerifiedInfo>;
60+
fn pre_check(&self, remaining_weight: Weight) -> Option<Self::VerifiedInfo>;
6061

61-
fn execute(info: Self::VerifiedInfo);
62+
fn execute(self, info: Self::VerifiedInfo) -> Weight;
6263
}
6364

6465
impl SteppedDeletion for () {
6566
type VerifiedInfo = ();
6667

67-
fn pre_check(_remaining_weight: Weight) -> Self::VerifiedInfo {
68-
()
68+
fn pre_check(&self, _remaining_weight: Weight) -> Option<Self::VerifiedInfo> {
69+
None
6970
}
7071

71-
fn execute(info: Self::VerifiedInfo) {}
72+
fn execute(self, _info: Self::VerifiedInfo) -> Weight {
73+
Weight::zero()
74+
}
7275
}

0 commit comments

Comments
 (0)