Skip to content
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

Implement Identity KeyStore pallet #824

Merged
merged 18 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 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 @@ -42,6 +42,7 @@ members = [
"pallets/pools",
"pallets/loans",
"pallets/permissions",
"pallets/keystore",
"libs/common-traits",
"libs/common-types",
"libs/proofs",
Expand Down
38 changes: 38 additions & 0 deletions pallets/keystore/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = 'pallet-keystore'
authors = ["Centrifuge <[email protected]>"]
description = 'Keystore pallet for runtime'
edition = '2018'
license = "LGPL-3.0"
repository = "https://github.com/centrifuge/centrifuge-chain/pallets/keystore"
version = '1.0.0'

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

[dependencies]
serde = { version = "1.0.102" }
codec = { package = 'parity-scale-codec', version = '3.0.0', features = ['derive'] , default-features = false }
scale-info = { version = "2.0", default-features = false, features = ["derive"] }

frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false , optional = true , branch = "polkadot-v0.9.24" }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.24" }
pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = true , branch = "polkadot-v0.9.24" }

[features]
default = ['std']
runtime-benchmarks = ['frame-benchmarking']
std = [
'codec/std',
'frame-support/std',
'frame-system/std',
'sp-runtime/std',
'sp-std/std',
]
113 changes: 113 additions & 0 deletions pallets/keystore/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
// This file is part of Centrifuge chain project.

// Centrifuge 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 (see http://www.gnu.org/licenses).

// Centrifuge 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.

use super::*;

use codec::EncodeLike;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_support::traits::Currency;
use frame_system::RawOrigin;
use scale_info::prelude::format;
use sp_runtime::traits::Hash;

benchmarks! {
where_clause {
where
T: Config<Balance = u128>,
T::AccountId: EncodeLike<<T as frame_system::Config>::AccountId>,
}

add_keys {
let n in 1..T::MaxKeys::get();
let caller: T::AccountId = account("acc_0", 0, 0);
let test_keys: Vec<AddKey<T::Hash>> = build_test_keys::<T>(n);
T::Currency::deposit_creating(&caller.clone().into(), T::DefaultKeyDeposit::get() * n as u128);
let origin = RawOrigin::Signed(caller.clone());
}: add_keys(origin, test_keys)
verify {
assert_eq!(Keys::<T>::iter().collect::<Vec<_>>().len() as u32, n);
}

revoke_keys {
let n in 1..T::MaxKeys::get();
let caller: T::AccountId = account("acc_1", 1, 1);
let test_keys: Vec<AddKey<T::Hash>> = build_test_keys::<T>(n);

add_keys_to_storage::<T>(caller.clone(), test_keys.clone());

let key_hashes: Vec<T::Hash> = test_keys.iter().map(|add_key| add_key.key).collect();
let origin = RawOrigin::Signed(caller.clone());
}: revoke_keys(origin, key_hashes, KeyPurpose::P2PDiscovery)
verify {
assert_eq!(Keys::<T>::iter().collect::<Vec<_>>().len() as u32, n);
assert!(all_keys_are_revoked::<T>(caller.clone()));
}

set_deposit {
let deposit = 2 * T::DefaultKeyDeposit::get();
}: set_deposit(RawOrigin::Root, deposit)
verify {
assert_eq!(KeyDeposit::<T>::get(), 2 * T::DefaultKeyDeposit::get());
}
}

fn all_keys_are_revoked<T: Config>(account_id: T::AccountId) -> bool {
for (_, key) in Keys::<T>::iter_prefix(account_id) {
if let None = key.revoked_at {
return false;
}
}

true
}

fn add_keys_to_storage<T: Config>(account_id: T::AccountId, keys: Vec<AddKey<T::Hash>>) {
for key in keys.iter() {
let key_id: KeyId<T::Hash> = (key.key.clone(), key.purpose.clone());

Keys::<T>::insert(
account_id.clone(),
key_id,
Key {
purpose: key.purpose.clone(),
key_type: key.key_type.clone(),
revoked_at: None,
deposit: T::DefaultKeyDeposit::get(),
},
);
}
}

fn build_test_keys<T: Config>(n: u32) -> Vec<AddKey<T::Hash>> {
let mut keys: Vec<AddKey<T::Hash>> = Vec::new();

for i in 0..n {
let hash = format!("some_hash_{}", i);

let key_hash = T::Hashing::hash(hash.as_bytes());

keys.push(AddKey::<T::Hash> {
key: key_hash,
purpose: KeyPurpose::P2PDiscovery,
key_type: KeyType::ECDSA,
});
}

return keys;
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext(),
crate::mock::MockRuntime,
);
Loading