1
- // This file is part of Substrate.
2
-
3
- // Copyright (C) 2022 Threefold Tech
4
- // Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
5
- // SPDX-License-Identifier: Apache-2.0
6
-
7
- // Licensed under the Apache License, Version 2.0 (the "License");
8
- // you may not use this file except in compliance with the License.
9
- // You may obtain a copy of the License at
10
- //
11
- // http://www.apache.org/licenses/LICENSE-2.0
12
- //
13
- // Unless required by applicable law or agreed to in writing, software
14
- // distributed under the License is distributed on an "AS IS" BASIS,
15
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- // See the License for the specific language governing permissions and
17
- // limitations under the License.
18
-
19
- //! Vesting pallet benchmarking.
20
-
21
1
#![ cfg( feature = "runtime-benchmarks" ) ]
22
2
23
3
use super :: * ;
24
4
25
- use crate :: Module as SmartContractModule ;
26
- use frame_benchmarking:: { account, benchmarks, whitelisted_caller} ;
27
- use frame_support:: traits :: { Box , Vec } ;
5
+ use crate :: Pallet as SmartContractModule ;
6
+ use frame_benchmarking:: { account, benchmarks, impl_benchmark_test_suite , whitelisted_caller} ;
7
+ use frame_support:: assert_ok ;
28
8
use frame_system:: RawOrigin ;
29
- use sp_std:: vec;
9
+ use sp_std:: {
10
+ convert:: { TryFrom , TryInto } ,
11
+ fmt:: Debug ,
12
+ vec,
13
+ } ;
30
14
31
- use tfchain_support:: types:: { Location , Resources , PublicIP } ;
32
- use pallet_tfgrid;
15
+ use pallet_tfgrid:: {
16
+ types:: { self as pallet_tfgrid_types, LocationInput } ,
17
+ CityNameInput , CountryNameInput , DocumentHashInput , DocumentLinkInput , LatitudeInput ,
18
+ LongitudeInput , ResourcesInput , TwinIpInput ,
19
+ } ;
20
+ use tfchain_support:: { resources:: Resources , types:: IP4 } ;
21
+ const GIGABYTE : u64 = 1024 * 1024 * 1024 ;
33
22
34
23
benchmarks ! {
24
+ where_clause {
25
+ where
26
+ <T as pallet_timestamp:: Config >:: Moment : TryFrom <u64 >,
27
+ <<T as pallet_timestamp:: Config >:: Moment as TryFrom <u64 >>:: Error : Debug ,
28
+ }
29
+
35
30
create_node_contract {
36
31
let a1: T :: AccountId = account( "Alice" , 0 , 0 ) ;
37
32
prepare_farm_and_node:: <T >( a1) ;
38
33
39
34
let caller: T :: AccountId = whitelisted_caller( ) ;
40
35
create_twin:: <T >( caller. clone( ) ) ;
41
- } : _ ( RawOrigin :: Signed ( caller. clone( ) ) , 1 , "some_data" . as_bytes( ) . to_vec( ) , "hash" . as_bytes( ) . to_vec( ) , 1 )
36
+ } : _ ( RawOrigin :: Signed (
37
+ caller. clone( ) ) ,
38
+ 1 ,
39
+ "858f8fb2184b15ecb8c0be8b95398c81" . as_bytes( ) . to_vec( ) . try_into( ) . unwrap( ) ,
40
+ "some_data" . as_bytes( ) . to_vec( ) . try_into( ) . unwrap( ) ,
41
+ 1 ,
42
+ None
43
+ )
42
44
verify {
43
- let contract = SmartContractModule :: <T >:: contracts( 1 ) ;
45
+ let contract = SmartContractModule :: <T >:: contracts( 1 ) . unwrap ( ) ;
44
46
assert_eq!(
45
47
contract. contract_id, 1
46
48
) ;
47
49
}
48
50
49
- add_reports {
51
+ add_nru_reports {
52
+ let stamp: u64 = 1628082000 * 1000 ;
53
+ pallet_timestamp:: Pallet :: <T >:: set_timestamp( stamp. try_into( ) . unwrap( ) ) ;
54
+
55
+ let a1: T :: AccountId = account( "Alice" , 0 , 0 ) ;
56
+ prepare_farm_and_node:: <T >( a1. clone( ) ) ;
57
+
58
+ let caller: T :: AccountId = whitelisted_caller( ) ;
59
+ create_twin:: <T >( caller. clone( ) ) ;
60
+ create_contract:: <T >( caller. clone( ) ) ;
61
+
62
+ let mut reports = Vec :: new( ) ;
63
+
64
+ reports. push( types:: NruConsumption {
65
+ contract_id: 1 ,
66
+ timestamp: 1628082000 * 1000 ,
67
+ window: 1000 ,
68
+ nru: 10 * GIGABYTE ,
69
+ } ) ;
70
+
71
+ } : _ ( RawOrigin :: Signed ( a1. clone( ) ) , reports)
72
+ verify {
73
+ let contract = SmartContractModule :: <T >:: contracts( 1 ) . unwrap( ) ;
74
+ assert_eq!(
75
+ contract. contract_id, 1
76
+ ) ;
77
+ }
78
+
79
+ bill_contract_for_block {
80
+ let stamp: u64 = 1628082000 * 1000 ;
81
+ pallet_timestamp:: Pallet :: <T >:: set_timestamp( stamp. try_into( ) . unwrap( ) ) ;
50
82
let a1: T :: AccountId = account( "Alice" , 0 , 0 ) ;
51
83
prepare_farm_and_node:: <T >( a1. clone( ) ) ;
52
84
@@ -56,26 +88,32 @@ benchmarks! {
56
88
57
89
let mut reports = Vec :: new( ) ;
58
90
59
- let gigabyte = 1000 * 1000 * 1000 ;
60
91
reports. push( types:: Consumption {
61
92
contract_id: 1 ,
62
93
cru: 2 ,
63
94
hru: 0 ,
64
- mru: 8 * gigabyte ,
65
- sru: 25 * gigabyte ,
95
+ mru: 8 * GIGABYTE ,
96
+ sru: 25 * GIGABYTE ,
66
97
nru: 0 ,
67
98
timestamp: 0 ,
68
99
} ) ;
69
100
70
- } : _ ( RawOrigin :: Signed ( a1. clone( ) ) , reports)
101
+ push_contract_resources:: <T >( a1. clone( ) ) ;
102
+
103
+ let stamp: u64 = 1628082000 * 1000 * 10 * 6000 ;
104
+ pallet_timestamp:: Pallet :: <T >:: set_timestamp( stamp. try_into( ) . unwrap( ) ) ;
105
+ // run_to_block::<T>(10);
106
+ } : _ ( RawOrigin :: Signed ( a1. clone( ) ) , 1 )
71
107
verify {
72
- let contract = SmartContractModule :: <T >:: contracts( 1 ) ;
108
+ let contract = SmartContractModule :: <T >:: contracts( 1 ) . unwrap ( ) ;
73
109
assert_eq!(
74
110
contract. contract_id, 1
75
111
) ;
76
112
}
77
113
}
78
114
115
+ impl_benchmark_test_suite ! { Pallet , crate :: tests:: new_test_ext( ) , crate :: tests:: Test }
116
+
79
117
#[ cfg( test) ]
80
118
mod benchmarktests {
81
119
use super :: * ;
@@ -86,70 +124,66 @@ mod benchmarktests {
86
124
fn test_benchmarks ( ) {
87
125
new_test_ext ( ) . execute_with ( || {
88
126
assert_ok ! ( test_benchmark_create_node_contract:: <TestRuntime >( ) ) ;
89
- assert_ok ! ( test_benchmark_add_reports :: <TestRuntime >( ) ) ;
127
+ assert_ok ! ( test_benchmark_add_nru_reports :: <TestRuntime >( ) ) ;
90
128
} ) ;
91
129
}
92
130
}
93
131
94
132
pub fn create_twin < T : Config > ( source : T :: AccountId ) {
95
- let document = "some_link" . as_bytes ( ) . to_vec ( ) ;
96
- let hash = "some_hash" . as_bytes ( ) . to_vec ( ) ;
97
-
98
- pallet_tfgrid:: Module :: < T > :: user_accept_tc (
133
+ assert_ok ! ( pallet_tfgrid:: Pallet :: <T >:: user_accept_tc(
99
134
RawOrigin :: Signed ( source. clone( ) ) . into( ) ,
100
- document . clone ( ) ,
101
- hash . clone ( ) ,
102
- )
103
- . unwrap ( ) ;
104
- let ip = "10.2.3.3" ;
105
- pallet_tfgrid:: Module :: < T > :: create_twin (
135
+ get_document_link_input ( b"some_link" ) ,
136
+ get_document_hash_input ( b"some_hash" ) ,
137
+ ) ) ;
138
+
139
+ let ip = get_twin_ip_input ( b"::1" ) ;
140
+ assert_ok ! ( pallet_tfgrid:: Pallet :: <T >:: create_twin(
106
141
RawOrigin :: Signed ( source) . into( ) ,
107
- ip. as_bytes ( ) . to_vec ( ) ,
108
- )
109
- . unwrap ( ) ;
142
+ ip
143
+ ) ) ;
110
144
}
111
145
112
146
pub fn prepare_farm_and_node < T : Config > ( source : T :: AccountId ) {
113
147
create_twin :: < T > ( source. clone ( ) ) ;
114
148
prepare_farm :: < T > ( source. clone ( ) ) ;
115
149
116
- // random location
117
- let location = Location {
118
- longitude : "12.233213231" . as_bytes ( ) . to_vec ( ) ,
119
- latitude : "32.323112123" . as_bytes ( ) . to_vec ( ) ,
150
+ let resources = ResourcesInput {
151
+ hru : 1024 * GIGABYTE ,
152
+ sru : 512 * GIGABYTE ,
153
+ cru : 8 ,
154
+ mru : 16 * GIGABYTE ,
120
155
} ;
121
156
122
- let resources = Resources {
123
- hru : 1 ,
124
- sru : 1 ,
125
- cru : 1 ,
126
- mru : 1 ,
157
+ // random location
158
+ let location = LocationInput {
159
+ city : get_city_name_input ( b"Ghent" ) ,
160
+ country : get_country_name_input ( b"Belgium" ) ,
161
+ latitude : get_latitude_input ( b"12.233213231" ) ,
162
+ longitude : get_longitude_input ( b"32.323112123" ) ,
127
163
} ;
128
164
129
- let country = "Belgium" . as_bytes ( ) . to_vec ( ) ;
130
- let city = "Ghent" . as_bytes ( ) . to_vec ( ) ;
131
- pallet_tfgrid:: Module :: < T > :: create_node (
132
- RawOrigin :: Signed ( source) . into ( ) ,
165
+ assert_ok ! ( pallet_tfgrid:: Pallet :: <T >:: create_node(
166
+ RawOrigin :: Signed ( source. clone( ) ) . into( ) ,
133
167
1 ,
134
168
resources,
135
169
location,
136
- country,
137
- city,
138
- Vec :: new ( ) ,
170
+ Vec :: new( ) . try_into( ) . unwrap( ) ,
139
171
false ,
140
172
false ,
141
- "some_serial" . as_bytes ( ) . to_vec ( ) ,
142
- )
143
- . unwrap ( ) ;
173
+ None ,
174
+ ) ) ;
144
175
}
145
176
146
177
pub fn prepare_farm < T : Config > ( source : T :: AccountId ) {
147
- let farm_name = "test_farm " ;
178
+ let farm_name = "testfarm " ;
148
179
let mut pub_ips = Vec :: new ( ) ;
149
- pub_ips. push ( PublicIP {
150
- ip : "1.1.1.0" . as_bytes ( ) . to_vec ( ) ,
151
- gateway : "1.1.1.1" . as_bytes ( ) . to_vec ( ) ,
152
- contract_id : 0 ,
180
+ pub_ips. push ( IP4 {
181
+ ip : "185.206.122.33/24" . as_bytes ( ) . to_vec ( ) . try_into ( ) . unwrap ( ) ,
182
+ gw : "185.206.122.1" . as_bytes ( ) . to_vec ( ) . try_into ( ) . unwrap ( ) ,
183
+ } ) ;
184
+ pub_ips. push ( IP4 {
185
+ ip : "185.206.122.34/24" . as_bytes ( ) . to_vec ( ) . try_into ( ) . unwrap ( ) ,
186
+ gw : "185.206.122.1" . as_bytes ( ) . to_vec ( ) . try_into ( ) . unwrap ( ) ,
153
187
} ) ;
154
188
155
189
let su_policy = pallet_tfgrid_types:: Policy {
@@ -180,7 +214,7 @@ pub fn prepare_farm<T: Config>(source: T::AccountId) {
180
214
let x1 = account ( "ferdie" , 0 , 2 ) ;
181
215
let x2 = account ( "eve" , 0 , 3 ) ;
182
216
183
- pallet_tfgrid:: Module :: < T > :: create_pricing_policy (
217
+ assert_ok ! ( pallet_tfgrid:: Pallet :: <T >:: create_pricing_policy(
184
218
RawOrigin :: Root . into( ) ,
185
219
"policy_1" . as_bytes( ) . to_vec( ) ,
186
220
su_policy,
@@ -191,24 +225,72 @@ pub fn prepare_farm<T: Config>(source: T::AccountId) {
191
225
domain_name_policy,
192
226
x1,
193
227
x2,
194
- 80
195
- )
196
- . unwrap ( ) ;
228
+ 80 ,
229
+ ) ) ;
197
230
198
- pallet_tfgrid:: Module :: < T > :: create_farm (
231
+ assert_ok ! ( pallet_tfgrid:: Pallet :: <T >:: create_farm(
199
232
RawOrigin :: Signed ( source) . into( ) ,
200
- farm_name. as_bytes ( ) . to_vec ( ) ,
201
- pub_ips. clone ( ) ,
202
- )
203
- . unwrap ( ) ;
233
+ farm_name. as_bytes( ) . to_vec( ) . try_into( ) . unwrap( ) ,
234
+ pub_ips. clone( ) . try_into( ) . unwrap( ) ,
235
+ ) ) ;
204
236
}
205
237
206
238
pub fn create_contract < T : Config > ( source : T :: AccountId ) {
207
- SmartContractModule :: < T > :: create_node_contract (
239
+ assert_ok ! ( SmartContractModule :: <T >:: create_node_contract(
208
240
RawOrigin :: Signed ( source) . into( ) ,
209
241
1 ,
210
- "some_data123" . as_bytes ( ) . to_vec ( ) ,
211
- "hash123" . as_bytes ( ) . to_vec ( ) ,
212
- 0
213
- ) . unwrap ( )
214
- }
242
+ "858f8fb2184b15ecb8c0be8b95398c81"
243
+ . as_bytes( )
244
+ . to_vec( )
245
+ . try_into( )
246
+ . unwrap( ) ,
247
+ "some_data123" . as_bytes( ) . to_vec( ) . try_into( ) . unwrap( ) ,
248
+ 0 ,
249
+ None ,
250
+ ) ) ;
251
+ }
252
+
253
+ pub fn push_contract_resources < T : Config > ( source : T :: AccountId ) {
254
+ let contract_resources = vec ! [ types:: ContractResources {
255
+ contract_id: 1 ,
256
+ used: Resources {
257
+ sru: 150 * GIGABYTE ,
258
+ cru: 16 ,
259
+ mru: 8 * GIGABYTE ,
260
+ hru: 0 ,
261
+ } ,
262
+ } ] ;
263
+
264
+ assert_ok ! ( SmartContractModule :: <T >:: report_contract_resources(
265
+ RawOrigin :: Signed ( source) . into( ) ,
266
+ contract_resources,
267
+ ) ) ;
268
+ }
269
+
270
+ pub ( crate ) fn get_city_name_input ( city_input : & [ u8 ] ) -> CityNameInput {
271
+ BoundedVec :: try_from ( city_input. to_vec ( ) ) . expect ( "Invalid city name input." )
272
+ }
273
+
274
+ pub ( crate ) fn get_country_name_input ( country_input : & [ u8 ] ) -> CountryNameInput {
275
+ BoundedVec :: try_from ( country_input. to_vec ( ) ) . expect ( "Invalid country name input." )
276
+ }
277
+
278
+ pub ( crate ) fn get_latitude_input ( latitude_input : & [ u8 ] ) -> LatitudeInput {
279
+ BoundedVec :: try_from ( latitude_input. to_vec ( ) ) . expect ( "Invalid latitude input." )
280
+ }
281
+
282
+ pub ( crate ) fn get_longitude_input ( longitude_input : & [ u8 ] ) -> LongitudeInput {
283
+ BoundedVec :: try_from ( longitude_input. to_vec ( ) ) . expect ( "Invalid longitude input." )
284
+ }
285
+
286
+ pub ( crate ) fn get_document_link_input ( document_link_input : & [ u8 ] ) -> DocumentLinkInput {
287
+ BoundedVec :: try_from ( document_link_input. to_vec ( ) ) . expect ( "Invalid document link input." )
288
+ }
289
+
290
+ pub ( crate ) fn get_document_hash_input ( document_hash_input : & [ u8 ] ) -> DocumentHashInput {
291
+ BoundedVec :: try_from ( document_hash_input. to_vec ( ) ) . expect ( "Invalid document hash input." )
292
+ }
293
+
294
+ pub ( crate ) fn get_twin_ip_input ( twin_ip_input : & [ u8 ] ) -> TwinIpInput {
295
+ BoundedVec :: try_from ( twin_ip_input. to_vec ( ) ) . expect ( "Invalid twin ip input." )
296
+ }
0 commit comments