#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use weights::*;
#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResult, inherent::Vec, pallet_prelude::*};
use frame_system::pallet_prelude::*;
use crate::weights::WeightInfo;
#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::type_value]
pub fn DefaultCurrent<T: Config>() -> u32 {
0
}
#[pallet::storage]
#[pallet::getter(fn get_current_trust_count)]
pub type CurrentIssued<T: Config> =
StorageValue<Value = u32, QueryKind = ValueQuery, OnEmpty = DefaultCurrent<T>>;
#[pallet::storage]
#[pallet::getter(fn get_trust_issuance)]
pub type TrustIssuance<T: Config> =
StorageDoubleMap<_, Blake2_128Concat, T::AccountId, Blake2_128Concat, T::AccountId, u32>;
#[pallet::storage]
#[pallet::getter(fn get_current_non_trust_count)]
pub type CurrentRevoked<T: Config> =
StorageValue<Value = u32, QueryKind = ValueQuery, OnEmpty = DefaultCurrent<T>>;
#[pallet::storage]
#[pallet::getter(fn get_non_trust_issuance)]
pub type TrustRevocation<T: Config> =
StorageDoubleMap<_, Blake2_128Concat, T::AccountId, Blake2_128Concat, T::AccountId, u32>;
#[pallet::storage]
#[pallet::getter(fn get_current_trust_requests)]
pub type CurrentRequests<T: Config> =
StorageValue<Value = u32, QueryKind = ValueQuery, OnEmpty = DefaultCurrent<T>>;
#[pallet::storage]
#[pallet::getter(fn get_trust_request)]
pub type TrustRequestList<T: Config> =
StorageDoubleMap<_, Blake2_128Concat, T::AccountId, Blake2_128Concat, T::AccountId, u32>;
#[pallet::storage]
#[pallet::unbounded]
#[pallet::getter(fn trust_paramter_list)]
pub type TrustParameterList<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Blake2_128Concat,
Vec<u8>,
u8,
ValueQuery,
>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
TrustParameterSet(Vec<u8>, u8, T::AccountId),
TrustIssued(T::AccountId, T::AccountId),
TrustRevoked(T::AccountId, T::AccountId),
TrustRequest(T::AccountId, T::AccountId),
TrustRequestRemoved(T::AccountId, T::AccountId),
TrustIssuanceRemoved(T::AccountId, T::AccountId),
TrustRevocationRemoved(T::AccountId, T::AccountId),
}
#[pallet::error]
pub enum Error<T> {
NoneValue,
StorageOverflow,
TrustExists,
TrustNotFound,
TrustRequestExists,
TrustRequestNotFound,
TrustRevocationExists,
TrustRevocationNotFound,
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(<T as Config>::WeightInfo::issue_trust())]
#[pallet::call_index(0)]
pub fn issue_trust(origin: OriginFor<T>, address: T::AccountId) -> DispatchResult {
let who = ensure_signed(origin)?;
if !<TrustIssuance<T>>::contains_key(&who, &address) {
let total: u32 = <CurrentIssued<T>>::get();
let new_total: u32 = total.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
<TrustIssuance<T>>::insert(&who, &address, total);
<CurrentIssued<T>>::put(new_total);
Self::deposit_event(Event::TrustIssued(who, address));
} else {
return Err(Error::<T>::TrustExists.into())
}
Ok(())
}
#[pallet::weight(<T as Config>::WeightInfo::remove_trust())]
#[pallet::call_index(1)]
pub fn remove_trust(origin: OriginFor<T>, address: T::AccountId) -> DispatchResult {
let who = ensure_signed(origin)?;
if <TrustIssuance<T>>::contains_key(&who, &address) {
let key = <CurrentIssued<T>>::get();
let new_key: u32 = key.checked_sub(1).ok_or(Error::<T>::StorageOverflow)?;
<TrustIssuance<T>>::remove(&who, &address);
<CurrentIssued<T>>::put(new_key);
Self::deposit_event(Event::TrustIssuanceRemoved(address, who));
} else {
return Err(Error::<T>::TrustNotFound.into())
}
Ok(())
}
#[pallet::weight(<T as Config>::WeightInfo::request_trust())]
#[pallet::call_index(2)]
pub fn request_trust(origin: OriginFor<T>, address: T::AccountId) -> DispatchResult {
let who = ensure_signed(origin)?;
if !<TrustRequestList<T>>::contains_key(&who, &address) {
let total: u32 = <CurrentRequests<T>>::get();
let new_total: u32 = total.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
<CurrentRequests<T>>::put(new_total);
<TrustRequestList<T>>::insert(&who, &address, total);
Self::deposit_event(Event::TrustRequest(who, address));
} else {
return Err(Error::<T>::TrustRequestExists.into())
}
Ok(())
}
#[pallet::weight(<T as Config>::WeightInfo::cancel_trust_request())]
#[pallet::call_index(3)]
pub fn cancel_trust_request(origin: OriginFor<T>, address: T::AccountId) -> DispatchResult {
let who = ensure_signed(origin)?;
if <TrustRequestList<T>>::contains_key(&who, &address) {
let key = <CurrentRequests<T>>::get();
let new_key: u32 = key.checked_sub(1).ok_or(Error::<T>::StorageOverflow)?;
<TrustRequestList<T>>::remove(&who, &address);
<CurrentRequests<T>>::put(new_key);
Self::deposit_event(Event::TrustRequestRemoved(address, who));
} else {
return Err(Error::<T>::TrustRequestNotFound.into())
}
Ok(())
}
#[pallet::weight(<T as Config>::WeightInfo::revoke_trust())]
#[pallet::call_index(4)]
pub fn revoke_trust(origin: OriginFor<T>, address: T::AccountId) -> DispatchResult {
let who = ensure_signed(origin)?;
if !<TrustRevocation<T>>::contains_key(&who, &address) {
let key: u32 = <CurrentRevoked<T>>::get();
let new_key: u32 = key.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
<TrustRevocation<T>>::insert(&who, &address, key);
<CurrentRevoked<T>>::put(new_key);
Self::deposit_event(Event::TrustRevoked(address, who));
} else {
return Err(Error::<T>::TrustRevocationExists.into())
}
Ok(())
}
#[pallet::weight(<T as Config>::WeightInfo::remove_revoked_trust())]
#[pallet::call_index(5)]
pub fn remove_revoked_trust(origin: OriginFor<T>, address: T::AccountId) -> DispatchResult {
let who = ensure_signed(origin)?;
if <TrustRevocation<T>>::contains_key(&who, &address) {
let key: u32 = <CurrentRevoked<T>>::get();
let new_key: u32 = key.checked_sub(1).ok_or(Error::<T>::StorageOverflow)?;
<TrustRevocation<T>>::remove(&who, &address);
<CurrentRevoked<T>>::put(new_key);
Self::deposit_event(Event::TrustRevocationRemoved(address, who));
} else {
return Err(Error::<T>::TrustRevocationNotFound.into())
}
Ok(())
}
#[pallet::weight(<T as Config>::WeightInfo::set_trust_parameter())]
#[pallet::call_index(6)]
pub fn set_trust_parameter(
origin: OriginFor<T>,
name: Vec<u8>,
value: u8,
) -> DispatchResult {
let who = ensure_signed(origin)?;
<TrustParameterList<T>>::insert(who.clone(), name.clone(), value);
Self::deposit_event(Event::TrustParameterSet(name, value, who));
Ok(())
}
}
}