1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#![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)]
    /// The total number of trust actions currently active
    pub type CurrentIssued<T: Config> =
        StorageValue<Value = u32, QueryKind = ValueQuery, OnEmpty = DefaultCurrent<T>>;
    #[pallet::storage]
    #[pallet::getter(fn get_trust_issuance)]
    /// A Map of lists of all addresses that each address has issued trust for
    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)]
    /// The current number of _non_trust actions currently active
    pub type CurrentRevoked<T: Config> =
        StorageValue<Value = u32, QueryKind = ValueQuery, OnEmpty = DefaultCurrent<T>>;
    #[pallet::storage]
    #[pallet::getter(fn get_non_trust_issuance)]
    /// A Map of lists of all addresses that each address has revoked trust for
    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)]
    /// A map listing all requests for trust from one account to another.
    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)]
    /// An account and a parameter string to an integer value.
    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> {
        /// Fully give `origin`'s trust to account `address`
        #[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(())
        }

        /// Remove issued trust from an account `address`, making their trust status 'Unknown'
        #[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(())
        }

        /// Place a request for `address` to issue explicit trust to the sender.
        #[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(())
        }

        /// Rescind or cancel a trust request placed to `address`.
        #[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(())
        }

        /// Explcitly mark an account as untrusted
        #[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(())
        }

        /// Return an untrusted `address` to an Unknown trust state
        #[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(())
        }

        /// Defines coefficients that participants should use to weight rating functions.
        #[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(())
        }
    }
}