-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add DIP to Peregrine #594
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,5 @@ std = [ | |
"frame-benchmarking?/std", | ||
"sp-runtime?/std", | ||
] | ||
|
||
try-runtime = [] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,4 @@ runtime-benchmarks = [ | |
"frame-benchmarking", | ||
"sp-runtime/runtime-benchmarks", | ||
] | ||
try-runtime = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// KILT Blockchain – https://botlabs.org | ||
// Copyright (C) 2019-2023 BOTLabs GmbH | ||
|
||
// The KILT Blockchain is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// The KILT Blockchain is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// If you feel like getting in touch with us, you can do so at info@botlabs.org | ||
|
||
use crate::{DidIdentifier, Runtime}; | ||
use frame_support::traits::Get; | ||
use pallet_deposit_storage::{ | ||
traits::DepositStorageHooks, DepositEntryOf, DepositKeyOf, FixedDepositCollectorViaDepositsPallet, | ||
}; | ||
use pallet_dip_provider::IdentityCommitmentVersion; | ||
use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; | ||
use runtime_common::constants::dip_provider::COMMITMENT_DEPOSIT; | ||
use scale_info::TypeInfo; | ||
use sp_core::{ConstU128, RuntimeDebug}; | ||
|
||
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)] | ||
pub enum DepositNamespace { | ||
DipProvider, | ||
} | ||
|
||
/// The namespace to use in the [`pallet_deposit_storage::Pallet`] to store | ||
/// all deposits related to DIP commitments. | ||
pub struct DipProviderDepositNamespace; | ||
|
||
impl Get<DepositNamespace> for DipProviderDepositNamespace { | ||
fn get() -> DepositNamespace { | ||
DepositNamespace::DipProvider | ||
} | ||
} | ||
|
||
/// The various different keys that can be stored in the storage-tracking | ||
/// pallet. | ||
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)] | ||
pub enum DepositKey { | ||
DipProvider { | ||
identifier: DidIdentifier, | ||
version: IdentityCommitmentVersion, | ||
}, | ||
} | ||
|
||
impl From<(DidIdentifier, IdentityCommitmentVersion)> for DepositKey { | ||
fn from((identifier, version): (DidIdentifier, IdentityCommitmentVersion)) -> Self { | ||
Self::DipProvider { identifier, version } | ||
} | ||
} | ||
|
||
/// The additional logic to execute whenever a deposit is removed by its | ||
/// owner directly via the [`pallet_deposit_storage::Pallet`] pallet. | ||
pub type DepositCollectorHooks = | ||
FixedDepositCollectorViaDepositsPallet<DipProviderDepositNamespace, ConstU128<COMMITMENT_DEPOSIT>, DepositKey>; | ||
|
||
pub enum CommitmentDepositRemovalHookError { | ||
DecodeKey, | ||
Internal, | ||
} | ||
|
||
impl From<CommitmentDepositRemovalHookError> for u16 { | ||
fn from(value: CommitmentDepositRemovalHookError) -> Self { | ||
match value { | ||
CommitmentDepositRemovalHookError::DecodeKey => 0, | ||
CommitmentDepositRemovalHookError::Internal => u16::MAX, | ||
} | ||
} | ||
} | ||
|
||
/// The logic to execute whenever an identity commitment is generated and | ||
/// stored in the [`pallet_dip_provider::Pallet`] pallet. | ||
/// | ||
/// Upon storing and removing identity commitments, this hook will reserve | ||
/// or release deposits from the [`pallet_deposit_storage::Pallet`] pallet. | ||
pub struct DepositHooks; | ||
|
||
impl DepositStorageHooks<Runtime> for DepositHooks { | ||
type Error = CommitmentDepositRemovalHookError; | ||
|
||
fn on_deposit_reclaimed( | ||
_namespace: &<Runtime as pallet_deposit_storage::Config>::Namespace, | ||
key: &DepositKeyOf<Runtime>, | ||
deposit: DepositEntryOf<Runtime>, | ||
) -> Result<(), Self::Error> { | ||
let DepositKey::DipProvider { identifier, version } = | ||
DepositKey::decode(&mut &key[..]).map_err(|_| CommitmentDepositRemovalHookError::DecodeKey)?; | ||
pallet_dip_provider::Pallet::<Runtime>::delete_identity_commitment_storage_entry( | ||
&identifier, | ||
// Deposit owner is the only one authorized to remove the deposit. | ||
&deposit.deposit.owner, | ||
version, | ||
) | ||
.map_err(|_| { | ||
log::error!( | ||
"Should not fail to remove commitment for identifier {:#?} and version {version}", | ||
identifier | ||
); | ||
CommitmentDepositRemovalHookError::Internal | ||
})?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "runtime-benchmarks")] | ||
pub struct PalletDepositStorageBenchmarkHooks; | ||
|
||
#[cfg(feature = "runtime-benchmarks")] | ||
impl pallet_deposit_storage::traits::BenchmarkHooks<Runtime> for PalletDepositStorageBenchmarkHooks { | ||
fn pre_reclaim_deposit() -> ( | ||
<Runtime as frame_system::Config>::AccountId, | ||
<Runtime as pallet_deposit_storage::Config>::Namespace, | ||
sp_runtime::BoundedVec<u8, <Runtime as pallet_deposit_storage::Config>::MaxKeyLength>, | ||
) { | ||
let submitter = runtime_common::AccountId::from([100u8; 32]); | ||
let namespace = DepositNamespace::DipProvider; | ||
let did_identifier = DidIdentifier::from([200u8; 32]); | ||
let commitment_version = 0u16; | ||
let key: DepositKeyOf<Runtime> = (did_identifier.clone(), 0) | ||
.encode() | ||
.try_into() | ||
.expect("Should not fail to create a key for a DIP commitment."); | ||
|
||
pallet_dip_provider::IdentityCommitments::<Runtime>::insert( | ||
&did_identifier, | ||
commitment_version, | ||
<Runtime as frame_system::Config>::Hash::default(), | ||
); | ||
|
||
assert!(pallet_dip_provider::IdentityCommitments::<Runtime>::get(did_identifier, commitment_version).is_some()); | ||
|
||
(submitter, namespace, key) | ||
} | ||
|
||
fn post_reclaim_deposit() { | ||
let did_identifier = DidIdentifier::from([200u8; 32]); | ||
let commitment_version = 0u16; | ||
assert!(pallet_dip_provider::IdentityCommitments::<Runtime>::get(did_identifier, commitment_version).is_none()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frame-system
and other pallets are missing. Can be done in another PR I guess. Just afraid that the try-runtime tests are not working right now. Did you verify that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pallet does not have any support for
try-runtime
yet. It was required for clippy not to complain. I guess once we add support fortry-runtime
(e.g., once we have a migration), then we will import the required dependencies?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean for the runtime to compile, but in essence having the feature enabled has no effect on those pallets (yet).