Skip to content

Commit

Permalink
wip: Compile WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Jun 1, 2022
1 parent 00e7e59 commit c1ecd61
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 42 deletions.
85 changes: 44 additions & 41 deletions pallets/proxy-keystore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,40 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;
pub use weights::*;
use sp_std::vec::Vec;
use frame_support::pallet_prelude::*;
use scale_info::TypeInfo;

pub mod weights;

// make sure representation is 1 byte
// TODO(cdamian): Given the above, should we use #[pallet::compact]?
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
enum KeyPurpose {
pub enum KeyPurpose {
P2PDiscovery,
P2PDocumentSigning
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
enum KeyType {
pub enum KeyType {
ECDSA,
EDDSA
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
struct Key<T: Config> {
pub struct Key<BlockNumber,Balance> {
purpose: KeyPurpose,
key_type: KeyType,
revoked_at: Option<T::BlockNumber>,
deposit: T::Balance,
revoked_at: Option<BlockNumber>,
deposit: Balance,
}

type KeyId<T: Config> = (T::Hash, KeyPurpose);
pub type KeyId<Hash> = (Hash, KeyPurpose);

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
struct AddKey<T: Config> {
key: T::Hash,
pub struct AddKey<Hash> {
key: Hash,
purpose: KeyPurpose,
key_type: KeyType,
}
Expand All @@ -56,12 +60,6 @@ pub mod pallet {
use sp_runtime::traits::AtLeast32BitUnsigned;
use sp_runtime::FixedPointOperand;

#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
// TODO(cdamian): W/ or w/o storage info?
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
Expand Down Expand Up @@ -89,11 +87,16 @@ pub mod pallet {
/// Origin used when setting a deposit.
type AdminOrigin: EnsureOrigin<Self::Origin>;

/// Weight information.
// TODO(cdamian): Run benchmark to get these weights.
// Weight information.
// type WeightInfo: WeightInfo;
type WeightInfo: WeightInfo;
}

#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
#[pallet::without_storage_info]
// TODO(cdamian): W/ or w/o storage info?
pub struct Pallet<T>(_);

/// Keys that are currently stored.
#[pallet::storage]
Expand All @@ -103,8 +106,8 @@ pub mod pallet {
Blake2_128Concat,
T::AccountId,
Blake2_128Concat,
KeyId<T>,
Key<T>,
KeyId<T::Hash>,
Key<T::BlockNumber, T::Balance>,
>;

/// Storage used for retrieving last key by purpose.
Expand All @@ -116,7 +119,7 @@ pub mod pallet {
T::AccountId,
Blake2_128Concat,
KeyPurpose,
KeyId<T>,
KeyId<T::Hash>,
>;

/// Stores the current deposit that will be taken when saving a key.
Expand Down Expand Up @@ -171,25 +174,25 @@ pub mod pallet {
impl<T: Config> Pallet<T> {

/// Create a new keystore for a specific account.
#[pallet::weight(T::WeightInfo::create_keystore().max(T::WeightInfo::create_keystore()))]
pub fn create_keystore(origin: OriginFor<T>, keys: Vec<AddKey<T>>) -> DispatchResult {
#[pallet::weight(T::WeightInfo::create_keystore(T::MaxKeys::get() as u32))]
pub fn create_keystore(origin: OriginFor<T>, keys: Vec<AddKey<T::Hash>>) -> DispatchResult {
let identity = ensure_signed(origin)?;

// Validate number of keys.
ensure!(keys.len() > 0, Error::<T>::NoKeys);
ensure!(keys.len() <= T::MaxKeys::get() as usize, Error::<T>::TooManyKeys);

// Should we check if origin is Proxy?
ensure!(!<KeystoreExists<T>>::contains_key(identity), Error::<T>::KeystoreExists);
ensure!(!<KeystoreExists<T>>::contains_key(identity.clone()), Error::<T>::KeystoreExists);

<KeystoreExists<T>>::insert(identity.clone(), true);

let key_deposit = <KeyDeposit<T>>::get();

// Add keys & take deposit per key.
for add_key in keys {
Self::add_key(identity.clone(), add_key, key_deposit)?;
Self::reserve_key_deposit(identity.clone(), key_deposit)?;
Self::add_key(identity.clone(), add_key.clone(), key_deposit.clone())?;
Self::reserve_key_deposit(identity.clone(), key_deposit.clone())?;

Self::deposit_event(
Event::Added(
Expand All @@ -205,21 +208,21 @@ pub mod pallet {
}

/// Add keys to an existing account keystore.
#[pallet::weight(T::WeightInfo::add_keys().max(T::WeightInfo::add_keys()))]
pub fn add_keys(origin: OriginFor<T>, keys: Vec<AddKey<T>>) -> DispatchResult {
#[pallet::weight(T::WeightInfo::add_keys(T::MaxKeys::get() as u32))]
pub fn add_keys(origin: OriginFor<T>, keys: Vec<AddKey<T::Hash>>) -> DispatchResult {
let identity = ensure_signed(origin)?;

// Validate number of keys.
ensure!(keys.len() > 0, Error::<T>::NoKeys);
ensure!(keys.len() <= T::MaxKeys::get() as usize, Error::<T>::TooManyKeys);

// Ensure identity is created.
ensure!(<KeystoreExists<T>>::contains_key(identity), Error::<T>::KeystoreDoesNotExist);
ensure!(<KeystoreExists<T>>::contains_key(identity.clone()), Error::<T>::KeystoreDoesNotExist);

let key_deposit = <KeyDeposit<T>>::get();

for add_key in keys {
Self::add_key(identity.clone(), add_key, key_deposit)?;
Self::add_key(identity.clone(), add_key.clone(), key_deposit.clone())?;

Self::deposit_event(
Event::Added(
Expand All @@ -234,7 +237,7 @@ pub mod pallet {
Ok(())
}

#[pallet::weight(T::WeightInfo::revoke_keys().max(T::WeightInfo::revoke_keys()))]
#[pallet::weight(T::WeightInfo::revoke_keys(T::MaxKeys::get() as u32))]
pub fn revoke_keys(origin: OriginFor<T>, key_hashes: Vec<T::Hash>) -> DispatchResult {
let identity = ensure_signed(origin)?;

Expand All @@ -243,26 +246,26 @@ pub mod pallet {
ensure!(key_hashes.len() <= T::MaxKeys::get() as usize, Error::<T>::TooManyKeys);

// Ensure identity is created.
ensure!(<KeystoreExists<T>>::contains_key(identity), Error::<T>::KeystoreDoesNotExist);
ensure!(<KeystoreExists<T>>::contains_key(identity.clone()), Error::<T>::KeystoreDoesNotExist);

let block_number = <frame_system::Pallet<T>>::block_number();

for key_hash in key_hashes {
Self::revoke_key(identity.clone(), key_hash, block_number)?;
Self::revoke_key(identity.clone(), key_hash.clone(), block_number.clone())?;

Self::deposit_event(
Event::Revoked(
identity.clone(),
key_hash,
block_number,
key_hash.clone(),
block_number.clone(),
),
)
}

Ok(())
}

#[pallet::weight(T::WeightInfo::revoke_keys().max(T::WeightInfo::set_deposit()))]
#[pallet::weight(T::WeightInfo::revoke_keys(T::MaxKeys::get() as u32))]
pub fn set_deposit(origin: OriginFor<T>, new_deposit: T::Balance) -> DispatchResult {
// Ensure that the origin is council or root.
Self::ensure_admin_origin(origin)?;
Expand Down Expand Up @@ -315,10 +318,10 @@ pub mod pallet {
/// The `key_deposit` is reserved upon success.
fn add_key(
account_id: T::AccountId,
add_key: AddKey<T>,
add_key: AddKey<T::Hash>,
key_deposit: T::Balance,
) -> DispatchResult {
let key_id: KeyId<T> = (add_key.key.clone(), add_key.purpose.clone());
let key_id: KeyId<T::Hash> = (add_key.key.clone(), add_key.purpose.clone());

// Check if we have a key with the same ID.
if <Keys<T>>::contains_key(account_id.clone(), key_id.clone()) {
Expand Down Expand Up @@ -355,7 +358,7 @@ pub mod pallet {
}

// Replace the any value that we might have in there since checks were OK.
let _ = key_id_opt.insert(key_id);
let _ = key_id_opt.insert(key_id.clone());

Ok(())
},
Expand All @@ -365,7 +368,7 @@ pub mod pallet {
<Keys<T>>::insert(
account_id.clone(),
key_id.clone(),
Key::<T>{
Key{
purpose: add_key.purpose.clone(),
key_type: add_key.key_type.clone(),
revoked_at: None,
Expand All @@ -389,7 +392,7 @@ pub mod pallet {
key_hash: T::Hash,
block_number: T::BlockNumber,
) -> DispatchResult {
let mut key_id_opt: Option<KeyId<T>> = None;
let mut key_id_opt: Option<KeyId<T::Hash>> = None;

let iter = <Keys<T>>::iter_prefix(account_id.clone());

Expand All @@ -409,7 +412,7 @@ pub mod pallet {
<Keys<T>>::try_mutate(
account_id.clone(),
key_id,
|key_opt| -> Result<T::Balance, DispatchError> {
|key_opt| -> DispatchResult {
return match key_opt {
Some(key) => {
// Check if key was already revoked.
Expand All @@ -424,7 +427,7 @@ pub mod pallet {
<LastKeyByPurpose<T>>::try_mutate(
account_id.clone(),
key.purpose.clone(),
|mut last_key_id_opt| -> DispatchResult {
|last_key_id_opt| -> DispatchResult {
return match last_key_id_opt {
Some(last_key_id) => {
if last_key_id.0 == key_hash {
Expand Down
38 changes: 38 additions & 0 deletions pallets/proxy-keystore/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{
traits::Get,
weights::{constants::RocksDbWeight, Weight},
};
use sp_std::marker::PhantomData;

/// Weight functions needed for pallet_proxy_keystore.
pub trait WeightInfo {
fn create_keystore(n: u32) -> Weight;
fn add_keys(n: u32) -> Weight;
fn revoke_keys(n: u32) -> Weight;
fn set_deposit() -> Weight;
}

/// Weights for pallet_proxy_keystore using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);

// TODO(cdamian): Update this.
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn create_keystore(_n: u32) -> Weight {
10 as Weight
}

fn add_keys(_n: u32) -> Weight {
10 as Weight
}

fn revoke_keys(_n: u32) -> Weight {
10 as Weight
}

fn set_deposit() -> Weight {
10 as Weight
}
}
4 changes: 3 additions & 1 deletion runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ impl pallet_sudo::Config for Runtime {
}

parameter_types! {
pub const MaxKeys = 10;
pub const MaxKeys: u32 = 10;
pub const DefaultKeyDeposit: Balance = 100 * CFG;
}

Expand All @@ -1274,6 +1274,7 @@ impl pallet_proxy_keystore::pallet::Config for Runtime {
type MaxKeys = MaxKeys;
type DefaultKeyDeposit = DefaultKeyDeposit;
type AdminOrigin = EnsureRootOr<AllOfCouncil>;
type WeightInfo = weights::pallet_proxy_keystore::SubstrateWeight<Runtime>;
}

// Frame Order in this block dictates the index of each one in the metadata
Expand Down Expand Up @@ -1332,6 +1333,7 @@ construct_runtime!(
NftSales: pallet_nft_sales::{Pallet, Call, Storage, Event<T>} = 100,
Nfts: pallet_nft::{Pallet, Call, Event<T>} = 103,
Bridge: pallet_bridge::{Pallet, Call, Storage, Config<T>, Event<T>} = 101,
ProxyKeystore: pallet_proxy_keystore::{Pallet, Call, Storage, Event<T>} = 104,

// XCM
XcmpQueue: cumulus_pallet_xcmp_queue::{Pallet, Call, Storage, Event<T>} = 120,
Expand Down
1 change: 1 addition & 0 deletions runtime/development/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ pub mod pallet_nft_sales;
pub mod pallet_permissions;
pub mod pallet_pools;
pub mod pallet_restricted_tokens;
pub mod pallet_proxy_keystore;
31 changes: 31 additions & 0 deletions runtime/development/src/weights/pallet_proxy_keystore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{
traits::Get,
weights::{constants::RocksDbWeight, Weight},
};
use sp_std::marker::PhantomData;
use pallet_proxy_keystore::weights::WeightInfo;

/// Weights for pallet_proxy_keystore using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);

// TODO(cdamian): Update this.
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn create_keystore(_n: u32) -> Weight {
10 as Weight
}

fn add_keys(_n: u32) -> Weight {
10 as Weight
}

fn revoke_keys(_n: u32) -> Weight {
10 as Weight
}

fn set_deposit() -> Weight {
10 as Weight
}
}

0 comments on commit c1ecd61

Please sign in to comment.