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
239
240
241
242
243
244
245
246
247
248
249
250
251
#![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::*,
        traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons},
    };
    use frame_system::pallet_prelude::*;

    use crate::weights::WeightInfo;

    const LOCK_ID: LockIdentifier = *b"fnlsignl";

    type BalanceOf<T> =
        <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

    #[pallet::config]
    pub trait Config: frame_system::Config {
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
        type WeightInfo: WeightInfo;
        type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;
    }

    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    pub struct Pallet<T>(_);

    #[pallet::storage]
    #[pallet::unbounded]
    #[pallet::getter(fn rating_signal_list)]
    /// Maps identity numbers to a signal transaction hash and a rating number.
    pub type RatingSignalList<T: Config> = StorageDoubleMap<
        _,
        Blake2_128Concat,
        T::AccountId,
        Blake2_128Concat,
        Vec<u8>,
        u8,
        ValueQuery,
    >;

    #[pallet::storage]
    #[pallet::unbounded]
    #[pallet::getter(fn whiteflag_rating_signal_list)]
    /// Maps identity numbers to a signal transaction hash and a rating number.
    pub type WhiteflagRatingSignalList<T: Config> = StorageDoubleMap<
        _,
        Blake2_128Concat,
        T::AccountId,
        Blake2_128Concat,
        Vec<u8>,
        u8,
        ValueQuery,
    >;

    #[pallet::storage]
    #[pallet::unbounded]
    #[pallet::getter(fn signal_paramter_list)]
    /// Maps identity numbers to a signal transaction hash and a rating number.
    pub type SignalParameterList<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> {
        SignalParameterSet(Vec<u8>, u8, T::AccountId),
        SignalSent(Vec<u8>, T::AccountId),
        RatingSignalSent(Vec<u8>, u8, T::AccountId),
        WhiteflagRatingSignalSent(Vec<u8>, u8, T::AccountId),
        RatingSignalUpdated(Vec<u8>, u8, T::AccountId),
        WhiteflagRatingSignalUpdated(Vec<u8>, u8, T::AccountId),
        RatingSignalRevoked(Vec<u8>, T::AccountId),
        WhiteflagRatingSignalRevoked(Vec<u8>, T::AccountId),
        ServiceSignalSent(Vec<u8>, Vec<u8>, T::AccountId),
        SignalLock(<T as frame_system::Config>::AccountId, BalanceOf<T>),
        SignalLockExtended(<T as frame_system::Config>::AccountId, BalanceOf<T>),
        SignalUnlock(<T as frame_system::Config>::AccountId),
    }

    #[pallet::error]
    pub enum Error<T> {
        NoneValue,
        StorageOverflow,
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Defines coefficients that participants should use to weight rating functions.
        #[pallet::weight(<T as Config>::WeightInfo::set_signal_parameter())]
        #[pallet::call_index(0)]
        pub fn set_signal_parameter(
            origin: OriginFor<T>,
            name: Vec<u8>,
            value: u8,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;

            <SignalParameterList<T>>::insert(who.clone(), name.clone(), value);
            Self::deposit_event(Event::SignalParameterSet(name, value, who));

            Ok(())
        }

        /// Creates an on-chain event with a transaction hash as a pointer and a u8 as a rating
        /// number.
        #[pallet::weight(<T as Config>::WeightInfo::send_rating_signal())]
        #[pallet::call_index(1)]
        pub fn send_rating_signal(
            origin: OriginFor<T>,
            target: Vec<u8>,
            rating: u8,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;

            <RatingSignalList<T>>::insert(who.clone(), target.clone(), rating);
            T::Currency::set_lock(LOCK_ID, &who, 10u32.into(), WithdrawReasons::all());
            Self::deposit_event(Event::SignalLock(who.clone(), 10u32.into()));
            Self::deposit_event(Event::RatingSignalSent(target, rating, who));

            Ok(())
        }

        #[pallet::weight(<T as Config>::WeightInfo::send_whiteflag_rating_signal())]
        #[pallet::call_index(2)]
        pub fn send_whiteflag_rating_signal(
            origin: OriginFor<T>,
            target: Vec<u8>,
            rating: u8,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;

            <WhiteflagRatingSignalList<T>>::insert(who.clone(), target.clone(), rating);
            T::Currency::set_lock(LOCK_ID, &who, 10u32.into(), WithdrawReasons::all());
            Self::deposit_event(Event::SignalLock(who.clone(), 10u32.into()));
            Self::deposit_event(Event::WhiteflagRatingSignalSent(target, rating, who));

            Ok(())
        }

        #[pallet::weight(<T as Config>::WeightInfo::update_whiteflag_rating_signal())]
        #[pallet::call_index(3)]
        pub fn update_whiteflag_rating_signal(
            origin: OriginFor<T>,
            target: Vec<u8>,
            new_rating: u8,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;

            <WhiteflagRatingSignalList<T>>::insert(who.clone(), target.clone(), new_rating);
            T::Currency::extend_lock(LOCK_ID, &who, 10u32.into(), WithdrawReasons::all());
            Self::deposit_event(Event::SignalLockExtended(who.clone(), 10u32.into()));
            Self::deposit_event(Event::WhiteflagRatingSignalUpdated(target, new_rating, who));

            Ok(())
        }

        /// Updates an existing rating signal.
        #[pallet::weight(<T as Config>::WeightInfo::update_rating_signal())]
        #[pallet::call_index(4)]
        pub fn update_rating_signal(
            origin: OriginFor<T>,
            target: Vec<u8>,
            new_rating: u8,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;

            <RatingSignalList<T>>::insert(who.clone(), target.clone(), new_rating);
            T::Currency::extend_lock(LOCK_ID, &who, 10u32.into(), WithdrawReasons::all());
            Self::deposit_event(Event::SignalLockExtended(who.clone(), 10u32.into()));
            Self::deposit_event(Event::RatingSignalUpdated(target, new_rating, who));

            Ok(())
        }

        /// Puts out a signal cancelling a previous rating.
        #[pallet::weight(<T as Config>::WeightInfo::revoke_rating_signal())]
        #[pallet::call_index(5)]
        pub fn revoke_rating_signal(origin: OriginFor<T>, target: Vec<u8>) -> DispatchResult {
            let who = ensure_signed(origin)?;

            <RatingSignalList<T>>::remove(who.clone(), target.clone());
            T::Currency::remove_lock(LOCK_ID, &who);
            Self::deposit_event(Event::SignalUnlock(who.clone()));
            Self::deposit_event(Event::RatingSignalRevoked(target, who));

            Ok(())
        }

        #[pallet::weight(<T as Config>::WeightInfo::revoke_whiteflag_rating_signal())]
        #[pallet::call_index(6)]
        pub fn revoke_whiteflag_rating_signal(
            origin: OriginFor<T>,
            target: Vec<u8>,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;

            <WhiteflagRatingSignalList<T>>::remove(who.clone(), target.clone());
            T::Currency::remove_lock(LOCK_ID, &who);
            Self::deposit_event(Event::SignalUnlock(who.clone()));
            Self::deposit_event(Event::WhiteflagRatingSignalRevoked(target, who));

            Ok(())
        }

        /// Creates an on-chain event with a signal payload defined as part of the transaction
        /// without relying on storage.
        #[pallet::weight(<T as Config>::WeightInfo::send_signal())]
        #[pallet::call_index(7)]
        pub fn send_signal(origin: OriginFor<T>, signal: Vec<u8>) -> DispatchResult {
            let who = ensure_signed(origin)?;
            Self::deposit_event(Event::SignalSent(signal, who));
            Ok(())
        }

        #[pallet::weight(<T as Config>::WeightInfo::send_service_signal())]
        #[pallet::call_index(8)]
        pub fn send_service_signal(
            origin: OriginFor<T>,
            service_identifier: Vec<u8>,
            url: Vec<u8>,
        ) -> DispatchResult {
            let who = ensure_signed(origin)?;
            Self::deposit_event(Event::ServiceSignalSent(service_identifier, url, who));
            Ok(())
        }
    }
}