Skip to content

Commit

Permalink
feat: add deposits for DIP provider pallet (#574)
Browse files Browse the repository at this point in the history
Fixes KILTprotocol/ticket#2983.

Add a generic pallet to store deposits, as well as a hooking mechanism
to both the existing provider pallet (to allow runtimes that take
deposits to take deposits, or to do any other operation), and to the new
deposit pallet, which allows other pallets to clean up storage if a
storage is reclaimed back by its owner (e.g., an identity commitment is
removed).

Tests will follow in a separate PR, when time allows for them 😁 The
whole DIP branch won't be merged on `develop` before tests are written,
so it's not a risk. The logic has been tested by spinning up a local
network and trying to commit an identity (which takes a deposit), remove
the commitment (which frees up the deposit), re-commit the identity,
reclaim the deposit (which removes the commitment).

- [x] Implement logic for deposit removal hooks
- [x] Test
- [x] Review
  • Loading branch information
ntn-x2 authored Nov 9, 2023
1 parent 7ffaac4 commit 6d2481e
Show file tree
Hide file tree
Showing 14 changed files with 672 additions and 75 deletions.
19 changes: 18 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ctype = {path = "pallets/ctype", default-features = false}
delegation = {path = "pallets/delegation", default-features = false}
did = {path = "pallets/did", default-features = false}
pallet-configuration = {path = "pallets/pallet-configuration", default-features = false}
pallet-deposit-storage = {path = "pallets/pallet-deposit-storage", default-features = false}
pallet-dip-consumer = {path = "pallets/pallet-dip-consumer", default-features = false}
pallet-dip-provider = {path = "pallets/pallet-dip-provider", default-features = false}
pallet-did-lookup = {path = "pallets/pallet-did-lookup", default-features = false}
Expand Down
5 changes: 3 additions & 2 deletions crates/kilt-dip-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ scale-info = {workspace = true, features = ["derive"]}
# Substrate dependencies
frame-system.workspace = true
frame-support.workspace = true
sp-runtime.workspace = true
sp-core.workspace = true
sp-io.workspace = true
sp-runtime.workspace = true
sp-state-machine.workspace = true
sp-std.workspace = true
sp-trie.workspace = true
Expand All @@ -51,6 +51,7 @@ sp-io = { workspace = true, features = ["std"] }
default = ["std"]
std = [
"hash-db/std",
"log/std",
"did/std",
"pallet-dip-consumer/std",
"pallet-dip-provider/std",
Expand All @@ -59,9 +60,9 @@ std = [
"scale-info/std",
"frame-system/std",
"frame-support/std",
"sp-runtime/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-state-machine/std",
"sp-std/std",
"sp-trie/std",
Expand Down
6 changes: 6 additions & 0 deletions dip-template/runtimes/dip-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ version.workspace = true
substrate-wasm-builder.workspace = true

[dependencies]
log.workspace = true
parity-scale-codec = {workspace = true, features = ["derive"]}
scale-info = {workspace = true, features = ["derive"]}

# DIP
did.workspace = true
kilt-support.workspace = true
kilt-dip-support.workspace = true
kilt-runtime-api-dip-provider.workspace = true
pallet-deposit-storage.workspace = true
pallet-did-lookup.workspace = true
pallet-dip-provider.workspace = true
pallet-web3-names.workspace = true
Expand Down Expand Up @@ -66,11 +69,14 @@ default = [
"std",
]
std = [
"log/std",
"parity-scale-codec/std",
"scale-info/std",
"did/std",
"kilt-support/std",
"kilt-dip-support/std",
"kilt-runtime-api-dip-provider/std",
"pallet-deposit-storage/std",
"pallet-did-lookup/std",
"pallet-dip-provider/std",
"pallet-web3-names/std",
Expand Down
118 changes: 105 additions & 13 deletions dip-template/runtimes/dip-provider/src/dip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// If you feel like getting in touch with us, you can do so at info@botlabs.org

use did::{DidRawOrigin, EnsureDidOrigin, KeyIdOf};
use frame_system::EnsureSigned;
use pallet_did_lookup::linkable_account::LinkableAccountId;
use pallet_dip_provider::{traits::IdentityProvider, IdentityCommitmentVersion};
use parity_scale_codec::{Decode, Encode};
Expand All @@ -25,24 +26,114 @@ use runtime_common::dip::{
merkle::{DidMerkleProofError, DidMerkleRootGenerator},
};
use scale_info::TypeInfo;
use sp_core::ConstU32;
use sp_std::vec::Vec;

use crate::{AccountId, DidIdentifier, Hash, Runtime, RuntimeEvent};
use crate::{
deposit::{DepositHooks, DepositNamespaces},
AccountId, Balances, DidIdentifier, Hash, Runtime, RuntimeEvent, RuntimeHoldReason,
};

pub mod runtime_api {
use super::*;

#[derive(Encode, Decode, TypeInfo)]
pub struct DipProofRequest {
pub(crate) identifier: DidIdentifier,
pub(crate) version: IdentityCommitmentVersion,
pub(crate) keys: Vec<KeyIdOf<Runtime>>,
pub(crate) accounts: Vec<LinkableAccountId>,
pub(crate) should_include_web3_name: bool,
}

#[derive(Encode, Decode, TypeInfo)]
pub enum DipProofError {
IdentityNotFound,
IdentityProviderError(<LinkedDidInfoProviderOf<Runtime> as IdentityProvider<DidIdentifier>>::Error),
MerkleProofError(DidMerkleProofError),
}
}

pub mod deposit {
use super::*;

use crate::{Balance, UNIT};

use frame_support::traits::Get;
use pallet_deposit_storage::{
traits::DepositStorageHooks, DepositEntryOf, DepositKeyOf, FixedDepositCollectorViaDepositsPallet,
};
use parity_scale_codec::MaxEncodedLen;
use sp_core::{ConstU128, RuntimeDebug};

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Eq, RuntimeDebug)]
pub enum DepositNamespaces {
DipProvider,
}

#[derive(Encode, Decode, TypeInfo)]
pub struct RuntimeApiDipProofRequest {
pub(crate) identifier: DidIdentifier,
pub(crate) version: IdentityCommitmentVersion,
pub(crate) keys: Vec<KeyIdOf<Runtime>>,
pub(crate) accounts: Vec<LinkableAccountId>,
pub(crate) should_include_web3_name: bool,
pub struct DipProviderDepositNamespace;

impl Get<DepositNamespaces> for DipProviderDepositNamespace {
fn get() -> DepositNamespaces {
DepositNamespaces::DipProvider
}
}

pub const DEPOSIT_AMOUNT: Balance = 2 * UNIT;

pub type DepositCollectorHooks =
FixedDepositCollectorViaDepositsPallet<DipProviderDepositNamespace, ConstU128<DEPOSIT_AMOUNT>>;

pub enum CommitmentDepositRemovalHookError {
DecodeKey,
Internal,
}

impl From<CommitmentDepositRemovalHookError> for u16 {
fn from(value: CommitmentDepositRemovalHookError) -> Self {
match value {
CommitmentDepositRemovalHookError::DecodeKey => 0,
CommitmentDepositRemovalHookError::Internal => u16::MAX,
}
}
}

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 (identifier, commitment_version) = <(DidIdentifier, IdentityCommitmentVersion)>::decode(&mut &key[..])
.map_err(|_| CommitmentDepositRemovalHookError::DecodeKey)?;
pallet_dip_provider::Pallet::<Runtime>::delete_identity_commitment_storage_entry(
&identifier,
commitment_version,
)
.map_err(|_| {
log::error!(
"Should not fail to remove commitment for identifier {:#?} and version {commitment_version}",
identifier
);
CommitmentDepositRemovalHookError::Internal
})?;
Ok(())
}
}
}

#[derive(Encode, Decode, TypeInfo)]
pub enum RuntimeApiDipProofError {
IdentityNotFound,
IdentityProviderError(<LinkedDidInfoProviderOf<Runtime> as IdentityProvider<DidIdentifier>>::Error),
MerkleProofError(DidMerkleProofError),
impl pallet_deposit_storage::Config for Runtime {
type CheckOrigin = EnsureSigned<AccountId>;
type Currency = Balances;
type DepositHooks = DepositHooks;
type MaxKeyLength = ConstU32<256>;
type Namespace = DepositNamespaces;
type RuntimeEvent = RuntimeEvent;
type RuntimeHoldReason = RuntimeHoldReason;
}

impl pallet_dip_provider::Config for Runtime {
Expand All @@ -54,5 +145,6 @@ impl pallet_dip_provider::Config for Runtime {
type IdentityCommitmentGeneratorError = DidMerkleProofError;
type IdentityProvider = LinkedDidInfoProviderOf<Runtime>;
type IdentityProviderError = <LinkedDidInfoProviderOf<Runtime> as IdentityProvider<DidIdentifier>>::Error;
type ProviderHooks = deposit::DepositCollectorHooks;
type RuntimeEvent = RuntimeEvent;
}
19 changes: 9 additions & 10 deletions dip-template/runtimes/dip-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use frame_system::{
};
use pallet_balances::AccountData;
use pallet_collator_selection::IdentityCollator;
use pallet_dip_provider::traits::IdentityProvider;
use pallet_dip_provider::{traits::IdentityProvider, IdentityProviderOf};
use pallet_session::{FindAccountFromAuthorIndex, PeriodicSessions};
use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInfo};
use runtime_common::dip::merkle::{CompleteMerkleProof, DidMerkleProofOf, DidMerkleRootGenerator};
Expand Down Expand Up @@ -137,7 +137,8 @@ construct_runtime!(
Web3Names: pallet_web3_names = 32,

// DIP
DipProvider: pallet_dip_provider = 40,
DepositStorage: pallet_deposit_storage = 40,
DipProvider: pallet_dip_provider = 41,
}
);

Expand Down Expand Up @@ -568,14 +569,12 @@ impl_runtime_apis! {
}
}

impl kilt_runtime_api_dip_provider::DipProvider<Block, RuntimeApiDipProofRequest, CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, RuntimeApiDipProofError> for Runtime {
fn generate_proof(request: RuntimeApiDipProofRequest) -> Result<CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, RuntimeApiDipProofError> {
let linked_did_info = match <Runtime as pallet_dip_provider::Config>::IdentityProvider::retrieve(&request.identifier) {
Ok(Some(linked_did_info)) => Ok(linked_did_info),
Ok(None) => Err(RuntimeApiDipProofError::IdentityNotFound),
Err(e) => Err(RuntimeApiDipProofError::IdentityProviderError(e))
}?;
DidMerkleRootGenerator::<Runtime>::generate_proof(&linked_did_info, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(RuntimeApiDipProofError::MerkleProofError)
impl kilt_runtime_api_dip_provider::DipProvider<Block, runtime_api::DipProofRequest, CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, runtime_api::DipProofError> for Runtime {
fn generate_proof(request: runtime_api::DipProofRequest) -> Result<CompleteMerkleProof<Hash, DidMerkleProofOf<Runtime>>, runtime_api::DipProofError> {
let maybe_identity_details = IdentityProviderOf::<Runtime>::retrieve(&request.identifier).map_err(runtime_api::DipProofError::IdentityProviderError)?;
let identity_details = maybe_identity_details.ok_or(runtime_api::DipProofError::IdentityNotFound)?;

DidMerkleRootGenerator::<Runtime>::generate_proof(&identity_details, request.version, request.keys.iter(), request.should_include_web3_name, request.accounts.iter()).map_err(runtime_api::DipProofError::MerkleProofError)
}
}
}
1 change: 0 additions & 1 deletion out.txt

This file was deleted.

41 changes: 41 additions & 0 deletions pallets/pallet-deposit-storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
authors.workspace = true
documentation.workspace = true
edition.workspace = true
homepage.workspace = true
license-file.workspace = true
readme.workspace = true
repository.workspace = true
version.workspace = true
name = "pallet-deposit-storage"
description = "Stores all deposits under a single pallet, with suport for namespacing different deposit contexts."

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
# Substrate dependencies
frame-support.workspace = true
frame-system.workspace = true
kilt-support.workspace = true
pallet-dip-provider.workspace = true
parity-scale-codec = {workspace = true, features = ["derive"]}
scale-info = {workspace = true, features = ["derive"]}
sp-runtime.workspace = true
sp-std.workspace = true

log.workspace = true

[features]
default = ["std"]
std = [
"frame-support/std",
"frame-system/std",
"kilt-support/std",
"pallet-dip-provider/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-runtime/std",
"sp-std/std",
"log/std",
]
Loading

0 comments on commit 6d2481e

Please sign in to comment.