@@ -21,23 +21,55 @@ use polymesh_common_utilities::{
21
21
benchs:: { AccountIdOf , User , UserBuilder } ,
22
22
TestUtilsFn ,
23
23
} ;
24
- use polymesh_primitives:: { asset:: AssetType , TrustedFor , TrustedIssuer } ;
24
+ use polymesh_primitives:: { asset:: AssetType , ClaimType , Scope , TrustedFor , TrustedIssuer } ;
25
+ use sp_std:: convert:: TryFrom ;
25
26
26
27
const MAX_DEFAULT_TRUSTED_CLAIM_ISSUERS : u32 = 3 ;
27
28
const MAX_TRUSTED_ISSUER_PER_CONDITION : u32 = 3 ;
28
29
const MAX_SENDER_CONDITIONS_PER_COMPLIANCE : u32 = 3 ;
29
30
const MAX_RECEIVER_CONDITIONS_PER_COMPLIANCE : u32 = 3 ;
30
31
const MAX_COMPLIANCE_REQUIREMENTS : u32 = 2 ;
31
32
33
+ const MAX_CONDITIONS : u32 = 10 ;
34
+ const MAX_CONDITION_TYPE_CLAIMS : u32 = 10 ;
35
+ const MAX_CONDITION_ISSUERS : u32 = 10 ;
36
+ const MAX_CONDITION_ISSUER_CLAIM_TYPES : u32 = 10 ;
37
+
38
+ const CLAIM_TYPES : & [ ClaimType ] = & [
39
+ ClaimType :: Accredited ,
40
+ ClaimType :: Affiliate ,
41
+ ClaimType :: BuyLockup ,
42
+ ClaimType :: SellLockup ,
43
+ ClaimType :: CustomerDueDiligence ,
44
+ ClaimType :: KnowYourCustomer ,
45
+ ClaimType :: Jurisdiction ,
46
+ ClaimType :: Exempted ,
47
+ ClaimType :: Blocked ,
48
+ ClaimType :: InvestorUniqueness ,
49
+ ClaimType :: NoType ,
50
+ ClaimType :: InvestorUniquenessV2 ,
51
+ ] ;
52
+
32
53
/// Create a token issuer trusted for `Any`.
33
- pub fn make_issuer < T : IdentityConfig + TestUtilsFn < AccountIdOf < T > > > ( id : u32 ) -> TrustedIssuer {
54
+ pub fn make_issuer < T : IdentityConfig + TestUtilsFn < AccountIdOf < T > > > (
55
+ id : u32 ,
56
+ claim_type_len : Option < usize > ,
57
+ ) -> TrustedIssuer {
34
58
let u = UserBuilder :: < T > :: default ( )
35
59
. generate_did ( )
36
60
. seed ( id)
37
61
. build ( "ISSUER" ) ;
38
62
TrustedIssuer {
39
63
issuer : IdentityId :: from ( u. did . unwrap ( ) ) ,
40
- trusted_for : TrustedFor :: Any ,
64
+ trusted_for : match claim_type_len {
65
+ None => TrustedFor :: Any ,
66
+ Some ( len) => TrustedFor :: Specific (
67
+ ( 0 ..len)
68
+ . into_iter ( )
69
+ . map ( |idx| CLAIM_TYPES [ idx % CLAIM_TYPES . len ( ) ] )
70
+ . collect ( ) ,
71
+ ) ,
72
+ } ,
41
73
}
42
74
}
43
75
@@ -46,16 +78,29 @@ pub fn make_issuer<T: IdentityConfig + TestUtilsFn<AccountIdOf<T>>>(id: u32) ->
46
78
/// - It could have more complexity if `TrustedIssuer::trusted_for` is a vector but not on
47
79
/// benchmarking of add/remove. That could be useful for benchmarking executions/evaluation of
48
80
/// complience requiriments.
49
- pub fn make_issuers < T : IdentityConfig + TestUtilsFn < AccountIdOf < T > > > ( s : u32 ) -> Vec < TrustedIssuer > {
50
- ( 0 ..s) . map ( |i| make_issuer :: < T > ( i) ) . collect :: < Vec < _ > > ( )
81
+ pub fn make_issuers < T : IdentityConfig + TestUtilsFn < AccountIdOf < T > > > (
82
+ s : u32 ,
83
+ claim_type_len : Option < usize > ,
84
+ ) -> Vec < TrustedIssuer > {
85
+ ( 0 ..s)
86
+ . map ( |i| make_issuer :: < T > ( i, claim_type_len) )
87
+ . collect ( )
51
88
}
52
89
53
90
/// Create simple conditions with a variable number of `issuers`.
54
- pub fn make_conditions ( s : u32 , issuers : & Vec < TrustedIssuer > ) -> Vec < Condition > {
91
+ pub fn make_conditions ( s : u32 , claims : Option < usize > , issuers : & [ TrustedIssuer ] ) -> Vec < Condition > {
55
92
( 0 ..s)
56
93
. map ( |_| Condition {
57
- condition_type : ConditionType :: IsPresent ( Claim :: NoData ) ,
58
- issuers : issuers. clone ( ) ,
94
+ condition_type : match claims {
95
+ None => ConditionType :: IsPresent ( Claim :: NoData ) ,
96
+ Some ( len) => ConditionType :: IsAnyOf (
97
+ ( 0 ..len)
98
+ . into_iter ( )
99
+ . map ( |_| Claim :: Blocked ( Scope :: Custom ( vec ! [ 0 ] ) ) )
100
+ . collect ( ) ,
101
+ ) ,
102
+ } ,
103
+ issuers : issuers. to_vec ( ) ,
59
104
} )
60
105
. collect ( )
61
106
}
@@ -103,7 +148,7 @@ struct ComplianceRequirementInfo<T: Config> {
103
148
104
149
impl < T : Config + TestUtilsFn < AccountIdOf < T > > > ComplianceRequirementInfo < T > {
105
150
pub fn add_default_trusted_claim_issuer ( self : & Self , i : u32 ) {
106
- make_issuers :: < T > ( i) . into_iter ( ) . for_each ( |issuer| {
151
+ make_issuers :: < T > ( i, None ) . into_iter ( ) . for_each ( |issuer| {
107
152
Module :: < T > :: add_default_trusted_claim_issuer (
108
153
self . owner . origin . clone ( ) . into ( ) ,
109
154
self . ticker . clone ( ) ,
@@ -130,9 +175,9 @@ impl<T: Config + TestUtilsFn<AccountIdOf<T>>> ComplianceRequirementBuilder<T> {
130
175
let ticker = make_token :: < T > ( & owner, b"1" . to_vec ( ) ) ;
131
176
132
177
// Create issuers (i) and conditions(s & r).
133
- let issuers = make_issuers :: < T > ( trusted_issuer_count) ;
134
- let sender_conditions = make_conditions ( sender_conditions_count, & issuers) ;
135
- let receiver_conditions = make_conditions ( receiver_conditions_count, & issuers) ;
178
+ let issuers = make_issuers :: < T > ( trusted_issuer_count, None ) ;
179
+ let sender_conditions = make_conditions ( sender_conditions_count, None , & issuers) ;
180
+ let receiver_conditions = make_conditions ( receiver_conditions_count, None , & issuers) ;
136
181
137
182
let info = ComplianceRequirementInfo {
138
183
owner,
@@ -168,9 +213,40 @@ impl<T: Config> ComplianceRequirementBuilder<T> {
168
213
}
169
214
}
170
215
216
+ fn setup_conditions_bench < T : Config + TestUtilsFn < AccountIdOf < T > > > (
217
+ conditions : u32 ,
218
+ claims : u32 ,
219
+ issuers : u32 ,
220
+ claim_types : u32 ,
221
+ ) -> Vec < Condition > {
222
+ let issuers = make_issuers :: < T > ( issuers, Some ( claim_types as usize ) ) ;
223
+ let conditions = make_conditions ( conditions, Some ( claims as usize ) , & issuers) ;
224
+ conditions
225
+ }
226
+
227
+ fn conditions_bench ( conditions : Vec < Condition > ) {
228
+ let encoded = conditions. encode ( ) ;
229
+ let decoded = Vec :: < Condition > :: decode ( & mut encoded. as_slice ( ) )
230
+ . expect ( "This shouldn't fail since we just encoded a `Vec<Condition>` value." ) ;
231
+ if !conditions. eq ( & decoded) {
232
+ panic ! ( "This shouldn't fail." ) ;
233
+ }
234
+ }
235
+
171
236
benchmarks ! {
172
237
where_clause { where T : TestUtilsFn <AccountIdOf <T >> }
173
238
239
+ condition_costs {
240
+ let a in 1 ..MAX_CONDITIONS ;
241
+ let b in 1 ..MAX_CONDITION_TYPE_CLAIMS ;
242
+ let c in 1 ..MAX_CONDITION_ISSUERS ;
243
+ let d in 1 ..MAX_CONDITION_ISSUER_CLAIM_TYPES ;
244
+
245
+ let conditions = setup_conditions_bench:: <T >( a, b, c, d) ;
246
+ } : {
247
+ conditions_bench( conditions) ;
248
+ }
249
+
174
250
add_compliance_requirement {
175
251
// INTERNAL: This benchmark only evaluate the adding operation. Its execution should be measured in another module.
176
252
let s in 1 ..MAX_SENDER_CONDITIONS_PER_COMPLIANCE ;
@@ -237,7 +313,7 @@ benchmarks! {
237
313
d. add_default_trusted_claim_issuer( MAX_DEFAULT_TRUSTED_CLAIM_ISSUERS -1 ) ;
238
314
239
315
// Add one more for benchmarking.
240
- let new_issuer = make_issuer:: <T >( MAX_DEFAULT_TRUSTED_CLAIM_ISSUERS ) ;
316
+ let new_issuer = make_issuer:: <T >( MAX_DEFAULT_TRUSTED_CLAIM_ISSUERS , None ) ;
241
317
} : _( d. owner. origin, d. ticker, new_issuer. clone( ) )
242
318
verify {
243
319
let trusted_issuers = Module :: <T >:: trusted_claim_issuer( d. ticker) ;
@@ -300,9 +376,9 @@ benchmarks! {
300
376
MAX_RECEIVER_CONDITIONS_PER_COMPLIANCE )
301
377
. add_compliance_requirement( ) . build( ) ;
302
378
303
- let issuers = make_issuers:: <T >( MAX_TRUSTED_ISSUER_PER_CONDITION ) ;
304
- let sender_conditions = make_conditions( MAX_SENDER_CONDITIONS_PER_COMPLIANCE , & issuers) ;
305
- let receiver_conditions = make_conditions( MAX_RECEIVER_CONDITIONS_PER_COMPLIANCE , & issuers) ;
379
+ let issuers = make_issuers:: <T >( MAX_TRUSTED_ISSUER_PER_CONDITION , None ) ;
380
+ let sender_conditions = make_conditions( MAX_SENDER_CONDITIONS_PER_COMPLIANCE , None , & issuers) ;
381
+ let receiver_conditions = make_conditions( MAX_RECEIVER_CONDITIONS_PER_COMPLIANCE , None , & issuers) ;
306
382
307
383
// Add more requirements to the asset, if `c > 1`.
308
384
( 1 ..c) . for_each( |_i| {
0 commit comments