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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use crate::Pallet as Identity;
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_support::{
ensure,
traits::{EnsureOrigin, Get},
};
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
const SEED: u32 = 0;
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}
fn add_registrars<T: Config>(r: u32) -> Result<(), &'static str> {
for i in 0..r {
let registrar: T::AccountId = account("registrar", i, SEED);
let registrar_lookup = T::Lookup::unlookup(registrar.clone());
let _ = T::Currency::make_free_balance_be(®istrar, BalanceOf::<T>::max_value());
let registrar_origin = T::RegistrarOrigin::successful_origin();
Identity::<T>::add_registrar(registrar_origin, registrar_lookup)?;
Identity::<T>::set_fee(RawOrigin::Signed(registrar.clone()).into(), i, 10u32.into())?;
let fields =
IdentityFields(
IdentityField::Display |
IdentityField::Legal | IdentityField::Web |
IdentityField::Riot | IdentityField::Email |
IdentityField::PgpFingerprint |
IdentityField::Image | IdentityField::Twitter,
);
Identity::<T>::set_fields(RawOrigin::Signed(registrar.clone()).into(), i, fields)?;
}
assert_eq!(Registrars::<T>::get().len(), r as usize);
Ok(())
}
fn create_sub_accounts<T: Config>(
who: &T::AccountId,
s: u32,
) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
let mut subs = Vec::new();
let who_origin = RawOrigin::Signed(who.clone());
let data = Data::Raw(vec![0; 32].try_into().unwrap());
for i in 0..s {
let sub_account = account("sub", i, SEED);
subs.push((sub_account, data.clone()));
}
let _ = T::Currency::make_free_balance_be(who, BalanceOf::<T>::max_value() / 2u32.into());
let info = create_identity_info::<T>(1);
Identity::<T>::set_identity(who_origin.into(), Box::new(info))?;
Ok(subs)
}
fn add_sub_accounts<T: Config>(
who: &T::AccountId,
s: u32,
) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
let who_origin = RawOrigin::Signed(who.clone());
let subs = create_sub_accounts::<T>(who, s)?;
Identity::<T>::set_subs(who_origin.into(), subs.clone())?;
Ok(subs)
}
fn create_identity_info<T: Config>(num_fields: u32) -> IdentityInfo<T::MaxAdditionalFields> {
let data = Data::Raw(vec![0; 32].try_into().unwrap());
IdentityInfo {
additional: vec![(data.clone(), data.clone()); num_fields as usize].try_into().unwrap(),
display: data.clone(),
legal: data.clone(),
web: data.clone(),
riot: data.clone(),
email: data.clone(),
pgp_fingerprint: Some([0; 20]),
image: data.clone(),
twitter: data,
}
}
benchmarks! {
add_registrar {
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
ensure!(Registrars::<T>::get().len() as u32 == r, "Registrars not set up correctly.");
let origin = T::RegistrarOrigin::successful_origin();
let account = T::Lookup::unlookup(account("registrar", r + 1, SEED));
}: _<T::Origin>(origin, account)
verify {
ensure!(Registrars::<T>::get().len() as u32 == r + 1, "Registrars not added.");
}
set_identity {
let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;
let x in 1 .. T::MaxAdditionalFields::get();
let caller = {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let caller_origin: <T as frame_system::Config>::Origin = RawOrigin::Signed(caller.clone()).into();
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let initial_info = create_identity_info::<T>(1);
Identity::<T>::set_identity(caller_origin.clone(), Box::new(initial_info))?;
for i in 0..r {
Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(account("registrar", i, SEED)).into(),
i,
caller_lookup.clone(),
Judgement::Reasonable
)?;
}
caller
};
}: _(RawOrigin::Signed(caller.clone()), Box::new(create_identity_info::<T>(x)))
verify {
assert_last_event::<T>(Event::<T>::IdentitySet { who: caller }.into());
}
set_subs_new {
let caller: T::AccountId = whitelisted_caller();
let s in 1 .. T::MaxSubAccounts::get() => ();
let subs = create_sub_accounts::<T>(&caller, s)?;
ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Caller already has subs");
}: set_subs(RawOrigin::Signed(caller.clone()), subs)
verify {
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not added");
}
set_subs_old {
let caller: T::AccountId = whitelisted_caller();
let p in 1 .. T::MaxSubAccounts::get() => {
let _ = add_sub_accounts::<T>(&caller, p)?;
};
let subs = create_sub_accounts::<T>(&caller, 0)?;
ensure!(
SubsOf::<T>::get(&caller).1.len() as u32 == p,
"Caller does have subs",
);
}: set_subs(RawOrigin::Signed(caller.clone()), subs)
verify {
ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Subs not removed");
}
clear_identity {
let caller: T::AccountId = whitelisted_caller();
let caller_origin = <T as frame_system::Config>::Origin::from(RawOrigin::Signed(caller.clone()));
let caller_lookup = <T::Lookup as StaticLookup>::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;
let s in 1 .. T::MaxSubAccounts::get() => {
let caller: T::AccountId = whitelisted_caller();
let _ = add_sub_accounts::<T>(&caller, s)?;
};
let x in 1 .. T::MaxAdditionalFields::get() => {
let info = create_identity_info::<T>(x);
let caller: T::AccountId = whitelisted_caller();
let caller_origin = <T as frame_system::Config>::Origin::from(RawOrigin::Signed(caller));
Identity::<T>::set_identity(caller_origin, Box::new(info))?;
};
for i in 0..r {
Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(account("registrar", i, SEED)).into(),
i,
caller_lookup.clone(),
Judgement::Reasonable
)?;
}
ensure!(IdentityOf::<T>::contains_key(&caller), "Identity does not exist.");
}: _(RawOrigin::Signed(caller.clone()))
verify {
ensure!(!IdentityOf::<T>::contains_key(&caller), "Identity not cleared.");
}
request_judgement {
let caller: T::AccountId = whitelisted_caller();
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;
let x in 1 .. T::MaxAdditionalFields::get() => {
let info = create_identity_info::<T>(x);
let caller: T::AccountId = whitelisted_caller();
let caller_origin = <T as frame_system::Config>::Origin::from(RawOrigin::Signed(caller));
Identity::<T>::set_identity(caller_origin, Box::new(info))?;
};
}: _(RawOrigin::Signed(caller.clone()), r - 1, 10u32.into())
verify {
assert_last_event::<T>(Event::<T>::JudgementRequested { who: caller, registrar_index: r-1 }.into());
}
cancel_request {
let caller: T::AccountId = whitelisted_caller();
let caller_origin = <T as frame_system::Config>::Origin::from(RawOrigin::Signed(caller.clone()));
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;
let x in 1 .. T::MaxAdditionalFields::get() => {
let info = create_identity_info::<T>(x);
let caller: T::AccountId = whitelisted_caller();
let caller_origin = <T as frame_system::Config>::Origin::from(RawOrigin::Signed(caller));
Identity::<T>::set_identity(caller_origin, Box::new(info))?;
};
Identity::<T>::request_judgement(caller_origin, r - 1, 10u32.into())?;
}: _(RawOrigin::Signed(caller.clone()), r - 1)
verify {
assert_last_event::<T>(Event::<T>::JudgementUnrequested { who: caller, registrar_index: r-1 }.into());
}
set_fee {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
let registrar_origin = T::RegistrarOrigin::successful_origin();
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().fee == 0u32.into(), "Fee already set.");
}: _(RawOrigin::Signed(caller), r, 100u32.into())
verify {
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().fee == 100u32.into(), "Fee not changed.");
}
set_account_id {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
let registrar_origin = T::RegistrarOrigin::successful_origin();
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().account == caller, "id not set.");
let new_account = T::Lookup::unlookup(account("new", 0, SEED));
}: _(RawOrigin::Signed(caller), r, new_account)
verify {
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().account == account("new", 0, SEED), "id not changed.");
}
set_fields {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
let registrar_origin = T::RegistrarOrigin::successful_origin();
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
let fields = IdentityFields(
IdentityField::Display | IdentityField::Legal | IdentityField::Web | IdentityField::Riot
| IdentityField::Email | IdentityField::PgpFingerprint | IdentityField::Image | IdentityField::Twitter
);
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().fields == Default::default(), "fields already set.");
}: _(RawOrigin::Signed(caller), r, fields)
verify {
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().fields != Default::default(), "fields not set.");
}
provide_judgement {
let user: T::AccountId = account("user", r, SEED);
let user_origin = <T as frame_system::Config>::Origin::from(RawOrigin::Signed(user.clone()));
let user_lookup = <T::Lookup as StaticLookup>::unlookup(user.clone());
let _ = T::Currency::make_free_balance_be(&user, BalanceOf::<T>::max_value());
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
let r in 1 .. T::MaxRegistrars::get() - 1 => add_registrars::<T>(r)?;
let x in 1 .. T::MaxAdditionalFields::get() => {
let info = create_identity_info::<T>(x);
Identity::<T>::set_identity(user_origin.clone(), Box::new(info))?;
};
let registrar_origin = T::RegistrarOrigin::successful_origin();
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
Identity::<T>::request_judgement(user_origin, r, 10u32.into())?;
}: _(RawOrigin::Signed(caller), r, user_lookup, Judgement::Reasonable)
verify {
assert_last_event::<T>(Event::<T>::JudgementGiven { target: user, registrar_index: r }.into())
}
kill_identity {
let r in 1 .. T::MaxRegistrars::get() => add_registrars::<T>(r)?;
let s in 1 .. T::MaxSubAccounts::get();
let x in 1 .. T::MaxAdditionalFields::get();
let target: T::AccountId = account("target", 0, SEED);
let target_origin: <T as frame_system::Config>::Origin = RawOrigin::Signed(target.clone()).into();
let target_lookup = T::Lookup::unlookup(target.clone());
let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());
let info = create_identity_info::<T>(x);
Identity::<T>::set_identity(target_origin.clone(), Box::new(info))?;
let _ = add_sub_accounts::<T>(&target, s)?;
for i in 0..r {
Identity::<T>::request_judgement(target_origin.clone(), i, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(account("registrar", i, SEED)).into(),
i,
target_lookup.clone(),
Judgement::Reasonable
)?;
}
ensure!(IdentityOf::<T>::contains_key(&target), "Identity not set");
let origin = T::ForceOrigin::successful_origin();
}: _<T::Origin>(origin, target_lookup)
verify {
ensure!(!IdentityOf::<T>::contains_key(&target), "Identity not removed");
}
add_sub {
let s in 1 .. T::MaxSubAccounts::get() - 1;
let caller: T::AccountId = whitelisted_caller();
let _ = add_sub_accounts::<T>(&caller, s)?;
let sub = account("new_sub", 0, SEED);
let data = Data::Raw(vec![0; 32].try_into().unwrap());
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not set.");
}: _(RawOrigin::Signed(caller.clone()), T::Lookup::unlookup(sub), data)
verify {
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s + 1, "Subs not added.");
}
rename_sub {
let s in 1 .. T::MaxSubAccounts::get();
let caller: T::AccountId = whitelisted_caller();
let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);
let data = Data::Raw(vec![1; 32].try_into().unwrap());
ensure!(SuperOf::<T>::get(&sub).unwrap().1 != data, "data already set");
}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone())
verify {
ensure!(SuperOf::<T>::get(&sub).unwrap().1 == data, "data not set");
}
remove_sub {
let s in 1 .. T::MaxSubAccounts::get();
let caller: T::AccountId = whitelisted_caller();
let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);
ensure!(SuperOf::<T>::contains_key(&sub), "Sub doesn't exists");
}: _(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()))
verify {
ensure!(!SuperOf::<T>::contains_key(&sub), "Sub not removed");
}
quit_sub {
let s in 1 .. T::MaxSubAccounts::get() - 1;
let caller: T::AccountId = whitelisted_caller();
let sup = account("super", 0, SEED);
let _ = add_sub_accounts::<T>(&sup, s)?;
let sup_origin = RawOrigin::Signed(sup).into();
Identity::<T>::add_sub(sup_origin, T::Lookup::unlookup(caller.clone()), Data::Raw(vec![0; 32].try_into().unwrap()))?;
ensure!(SuperOf::<T>::contains_key(&caller), "Sub doesn't exists");
}: _(RawOrigin::Signed(caller.clone()))
verify {
ensure!(!SuperOf::<T>::contains_key(&caller), "Sub not removed");
}
impl_benchmark_test_suite!(Identity, crate::tests::new_test_ext(), crate::tests::Test);
}