@@ -38,12 +38,14 @@ pub fn execute(
38
38
payment_wallet,
39
39
attestation_report,
40
40
} => try_register_worker ( deps, info, ip_address, payment_wallet, attestation_report) ,
41
- ExecuteMsg :: SetWorkerWallet { ip_address, payment_wallet } => {
42
- try_set_worker_wallet ( deps, info, ip_address, payment_wallet)
43
- }
44
- ExecuteMsg :: SetWorkerAddress { new_ip_address, old_ip_address } => {
45
- try_set_worker_address ( deps, info, new_ip_address, old_ip_address)
46
- }
41
+ ExecuteMsg :: SetWorkerWallet {
42
+ ip_address,
43
+ payment_wallet,
44
+ } => try_set_worker_wallet ( deps, info, ip_address, payment_wallet) ,
45
+ ExecuteMsg :: SetWorkerAddress {
46
+ new_ip_address,
47
+ old_ip_address,
48
+ } => try_set_worker_address ( deps, info, new_ip_address, old_ip_address) ,
47
49
ExecuteMsg :: ReportLiveliness { } => try_report_liveliness ( deps, info) ,
48
50
ExecuteMsg :: ReportWork { } => try_report_work ( deps, info) ,
49
51
}
@@ -56,7 +58,6 @@ pub fn try_register_worker(
56
58
payment_wallet : String ,
57
59
attestation_report : String ,
58
60
) -> StdResult < Response > {
59
-
60
61
let worker = Worker {
61
62
ip_address,
62
63
payment_wallet,
@@ -77,7 +78,9 @@ pub fn try_set_worker_wallet(
77
78
let worker_entry = WORKERS_MAP . get ( _deps. storage , & ip_address) ;
78
79
if let Some ( worker) = worker_entry {
79
80
if _info. sender != worker. payment_wallet {
80
- return Err ( StdError :: generic_err ( "Only the owner has the authority to modify the payment wallet" ) ) ;
81
+ return Err ( StdError :: generic_err (
82
+ "Only the owner has the authority to modify the payment wallet" ,
83
+ ) ) ;
81
84
}
82
85
let worker = Worker {
83
86
payment_wallet,
@@ -101,7 +104,9 @@ pub fn try_set_worker_address(
101
104
let worker_entry = WORKERS_MAP . get ( _deps. storage , & old_ip_address) ;
102
105
if let Some ( worker) = worker_entry {
103
106
if _info. sender != worker. payment_wallet {
104
- return Err ( StdError :: generic_err ( "Only the owner has the authority to modify the IP address" ) ) ;
107
+ return Err ( StdError :: generic_err (
108
+ "Only the owner has the authority to modify the IP address" ,
109
+ ) ) ;
105
110
}
106
111
let worker = Worker {
107
112
payment_wallet : worker. payment_wallet ,
@@ -149,7 +154,6 @@ fn query_workers(
149
154
_signature : String ,
150
155
_sender_public_key : String ,
151
156
) -> StdResult < GetWorkersResponse > {
152
-
153
157
let workers: Vec < _ > = WORKERS_MAP
154
158
. iter ( _deps. storage ) ?
155
159
. map ( |x| {
@@ -172,9 +176,14 @@ fn query_liveliness_challenge(_deps: Deps) -> StdResult<GetLivelinessChallengeRe
172
176
173
177
#[ cfg( test) ]
174
178
mod tests {
179
+ use crate :: msg;
180
+
175
181
use super :: * ;
176
- use cosmwasm_std:: { from_binary, testing:: * } ;
182
+ use cosmwasm_std:: { from_binary, testing:: * , OwnedDeps } ;
177
183
use cosmwasm_std:: { Coin , Uint128 } ;
184
+ const IP_ADDRESS : & str = "127.0.0.1" ;
185
+ const PAYMENT_WALLET : & str = "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" ;
186
+ const ATTESTATION_REPORT : & str = "" ;
178
187
179
188
#[ test]
180
189
fn proper_initialization ( ) {
@@ -194,117 +203,210 @@ mod tests {
194
203
assert_eq ! ( 0 , res. messages. len( ) ) ;
195
204
}
196
205
197
- #[ test]
198
- fn register_worker_success ( ) {
206
+ fn init_contract ( ) -> (
207
+ StdResult < Response > ,
208
+ OwnedDeps < MockStorage , MockApi , MockQuerier > ,
209
+ ) {
199
210
let mut deps = mock_dependencies ( ) ;
200
211
let env = mock_env ( ) ;
201
212
let info = mock_info ( "admin" , & [ ] ) ;
202
213
let msg = InstantiateMsg { } ;
203
- let _res = instantiate ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , msg) . unwrap ( ) ;
214
+ let res = instantiate ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , msg) ;
204
215
216
+ ( res, deps)
217
+ }
218
+
219
+ fn register_worker (
220
+ deps : & mut OwnedDeps < MockStorage , MockApi , MockQuerier > ,
221
+ ip_address : String ,
222
+ payment_wallet : String ,
223
+ attestation_report : String ,
224
+ ) -> StdResult < Response > {
205
225
let execute_msg = ExecuteMsg :: RegisterWorker {
206
- ip_address : String :: from ( "127.0.0.1" ) ,
207
- payment_wallet : "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" . to_string ( ) ,
208
- attestation_report : "" . to_string ( ) ,
226
+ ip_address,
227
+ payment_wallet : payment_wallet . clone ( ) ,
228
+ attestation_report,
209
229
} ;
210
- let res = execute ( deps. as_mut ( ) , env, info, execute_msg) . unwrap ( ) ;
211
- assert_eq ! ( 0 , res. messages. len( ) ) ;
230
+ execute (
231
+ deps. as_mut ( ) ,
232
+ mock_env ( ) ,
233
+ mock_info ( & payment_wallet, & [ ] ) ,
234
+ execute_msg,
235
+ )
236
+ }
237
+
238
+ #[ test]
239
+ fn register_worker_success ( ) {
240
+ let ( res, mut deps) = init_contract ( ) ;
241
+ assert_eq ! ( res. unwrap( ) . messages. len( ) , 0 ) ;
242
+
243
+ let res = register_worker (
244
+ & mut deps,
245
+ IP_ADDRESS . into ( ) ,
246
+ PAYMENT_WALLET . into ( ) ,
247
+ ATTESTATION_REPORT . into ( ) ,
248
+ )
249
+ . unwrap ( ) ;
250
+
212
251
let worker: Worker = from_binary ( & res. data . unwrap ( ) ) . unwrap ( ) ;
213
252
assert_eq ! (
214
253
worker,
215
254
Worker {
216
- ip_address: String :: from ( "127.0.0.1" ) ,
217
- payment_wallet: "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" . to_string ( ) ,
218
- attestation_report: "" . to_string ( ) ,
255
+ ip_address: IP_ADDRESS . into ( ) ,
256
+ payment_wallet: PAYMENT_WALLET . into ( ) ,
257
+ attestation_report: ATTESTATION_REPORT . into ( ) ,
219
258
}
220
259
) ;
221
260
}
222
261
223
262
#[ test]
224
263
fn set_worker_wallet ( ) {
225
- let mut deps = mock_dependencies ( ) ;
226
- let env = mock_env ( ) ;
227
- let info = mock_info ( "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" , & [ ] ) ;
228
- let msg = InstantiateMsg { } ;
229
- let _res = instantiate ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , msg) . unwrap ( ) ;
230
-
231
- let ip_address = String :: from ( "127.0.0.1" ) ;
232
- let payment_wallet = "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" . to_string ( ) ;
233
- let attestation_report = "" . to_string ( ) ;
234
-
235
- let execute_msg = ExecuteMsg :: RegisterWorker {
236
- ip_address : ip_address. clone ( ) ,
237
- payment_wallet,
238
- attestation_report : attestation_report. clone ( ) ,
239
- } ;
240
- let res = execute ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , execute_msg) . unwrap ( ) ;
264
+ let ( res, mut deps) = init_contract ( ) ;
265
+ assert_eq ! ( res. unwrap( ) . messages. len( ) , 0 ) ;
266
+
267
+ let res = register_worker (
268
+ & mut deps,
269
+ IP_ADDRESS . into ( ) ,
270
+ PAYMENT_WALLET . into ( ) ,
271
+ ATTESTATION_REPORT . into ( ) ,
272
+ )
273
+ . unwrap ( ) ;
241
274
assert_eq ! ( 0 , res. messages. len( ) ) ;
242
275
243
276
let new_payment_wallet = "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450007" . to_string ( ) ;
244
277
let execute_msg = ExecuteMsg :: SetWorkerWallet {
245
- ip_address : ip_address . clone ( ) ,
278
+ ip_address : IP_ADDRESS . into ( ) ,
246
279
payment_wallet : new_payment_wallet. clone ( ) ,
247
280
} ;
248
- let res = execute ( deps. as_mut ( ) , env, info, execute_msg) . unwrap ( ) ;
281
+ let res = execute (
282
+ deps. as_mut ( ) ,
283
+ mock_env ( ) ,
284
+ mock_info ( PAYMENT_WALLET , & [ ] ) ,
285
+ execute_msg,
286
+ )
287
+ . unwrap ( ) ;
249
288
assert_eq ! ( 0 , res. messages. len( ) ) ;
250
289
251
290
let worker: Worker = from_binary ( & res. data . unwrap ( ) ) . unwrap ( ) ;
252
291
assert_eq ! (
253
292
worker,
254
293
Worker {
255
- ip_address: ip_address . clone ( ) ,
294
+ ip_address: IP_ADDRESS . into ( ) ,
256
295
payment_wallet: new_payment_wallet,
257
- attestation_report,
296
+ attestation_report: ATTESTATION_REPORT . into ( ) ,
258
297
}
259
298
) ;
260
299
}
261
300
262
301
#[ test]
263
- fn set_worker_address ( ) {
264
- let mut deps = mock_dependencies ( ) ;
265
- let env = mock_env ( ) ;
266
- let info = mock_info ( "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" , & [ ] ) ;
267
- let msg = InstantiateMsg { } ;
268
- let _res = instantiate ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , msg) . unwrap ( ) ;
269
-
270
- let ip_address = String :: from ( "127.0.0.1" ) ;
271
- let payment_wallet = "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" . to_string ( ) ;
272
- let attestation_report = "" . to_string ( ) ;
302
+ fn set_worker_wallet_unauthorized ( ) {
303
+ let ( res, mut deps) = init_contract ( ) ;
304
+ assert_eq ! ( res. unwrap( ) . messages. len( ) , 0 ) ;
305
+
306
+ let res = register_worker (
307
+ & mut deps,
308
+ IP_ADDRESS . into ( ) ,
309
+ PAYMENT_WALLET . into ( ) ,
310
+ ATTESTATION_REPORT . into ( ) ,
311
+ )
312
+ . unwrap ( ) ;
313
+ assert_eq ! ( 0 , res. messages. len( ) ) ;
273
314
274
- let execute_msg = ExecuteMsg :: RegisterWorker {
275
- ip_address : ip_address . clone ( ) ,
276
- payment_wallet : payment_wallet . clone ( ) ,
277
- attestation_report : attestation_report . clone ( ) ,
315
+ let new_payment_wallet = "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450007" . to_string ( ) ;
316
+ let execute_msg = ExecuteMsg :: SetWorkerWallet {
317
+ ip_address : IP_ADDRESS . into ( ) ,
318
+ payment_wallet : new_payment_wallet . clone ( ) ,
278
319
} ;
279
- let res = execute ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , execute_msg) . unwrap ( ) ;
320
+
321
+ // set as sender foreign wallet
322
+ let res = execute (
323
+ deps. as_mut ( ) ,
324
+ mock_env ( ) ,
325
+ mock_info ( & new_payment_wallet, & [ ] ) ,
326
+ execute_msg,
327
+ ) ;
328
+ assert ! ( res. is_err( ) ) ;
329
+ assert_eq ! (
330
+ res. unwrap_err( ) ,
331
+ StdError :: generic_err( "Only the owner has the authority to modify the payment wallet" , )
332
+ ) ;
333
+ }
334
+
335
+ #[ test]
336
+ fn set_worker_address ( ) {
337
+ let ( res, mut deps) = init_contract ( ) ;
338
+ assert_eq ! ( res. unwrap( ) . messages. len( ) , 0 ) ;
339
+
340
+ let res = register_worker (
341
+ & mut deps,
342
+ IP_ADDRESS . into ( ) ,
343
+ PAYMENT_WALLET . into ( ) ,
344
+ ATTESTATION_REPORT . into ( ) ,
345
+ )
346
+ . unwrap ( ) ;
280
347
assert_eq ! ( 0 , res. messages. len( ) ) ;
281
348
282
349
let new_ip_address = String :: from ( "147.4.4.7" ) ;
283
350
let execute_msg = ExecuteMsg :: SetWorkerAddress {
284
351
new_ip_address : new_ip_address. clone ( ) ,
285
- old_ip_address : ip_address . clone ( ) ,
352
+ old_ip_address : IP_ADDRESS . into ( ) ,
286
353
} ;
287
- let res = execute ( deps. as_mut ( ) , env, info, execute_msg) . unwrap ( ) ;
354
+ let res = execute (
355
+ deps. as_mut ( ) ,
356
+ mock_env ( ) ,
357
+ mock_info ( PAYMENT_WALLET , & [ ] ) ,
358
+ execute_msg,
359
+ )
360
+ . unwrap ( ) ;
288
361
assert_eq ! ( 0 , res. messages. len( ) ) ;
289
362
290
363
let worker: Worker = from_binary ( & res. data . unwrap ( ) ) . unwrap ( ) ;
291
364
assert_eq ! (
292
365
worker,
293
366
Worker {
294
367
ip_address: new_ip_address,
295
- payment_wallet,
296
- attestation_report,
368
+ payment_wallet: PAYMENT_WALLET . into ( ) ,
369
+ attestation_report: ATTESTATION_REPORT . into ( ) ,
297
370
}
298
371
) ;
299
372
}
300
373
374
+ #[ test]
375
+ fn set_worker_address_unauthorized ( ) {
376
+ let ( res, mut deps) = init_contract ( ) ;
377
+ assert_eq ! ( res. unwrap( ) . messages. len( ) , 0 ) ;
378
+
379
+ let res = register_worker (
380
+ & mut deps,
381
+ IP_ADDRESS . into ( ) ,
382
+ PAYMENT_WALLET . into ( ) ,
383
+ ATTESTATION_REPORT . into ( ) ,
384
+ )
385
+ . unwrap ( ) ;
386
+ assert_eq ! ( 0 , res. messages. len( ) ) ;
387
+
388
+ let new_ip_address = String :: from ( "147.4.4.7" ) ;
389
+ let execute_msg = ExecuteMsg :: SetWorkerAddress {
390
+ new_ip_address : new_ip_address. clone ( ) ,
391
+ old_ip_address : IP_ADDRESS . into ( ) ,
392
+ } ;
393
+ let res = execute (
394
+ deps. as_mut ( ) ,
395
+ mock_env ( ) ,
396
+ mock_info ( "fake_acc" , & [ ] ) ,
397
+ execute_msg,
398
+ ) ;
399
+ assert ! ( res. is_err( ) ) ;
400
+ assert_eq ! (
401
+ res. unwrap_err( ) ,
402
+ StdError :: generic_err( "Only the owner has the authority to modify the IP address" , )
403
+ ) ;
404
+ }
405
+
301
406
#[ test]
302
407
fn query_workers ( ) {
303
- let mut deps = mock_dependencies ( ) ;
304
- let env = mock_env ( ) ;
305
- let info = mock_info ( "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s03" , & [ ] ) ;
306
- let msg = InstantiateMsg { } ;
307
- let _res = instantiate ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , msg) . unwrap ( ) ;
408
+ let ( res, mut deps) = init_contract ( ) ;
409
+ assert_eq ! ( res. unwrap( ) . messages. len( ) , 0 ) ;
308
410
309
411
let ip_address_1 = String :: from ( "127.0.0.1" ) ;
310
412
let ip_address_2 = String :: from ( "127.0.0.2" ) ;
@@ -314,36 +416,39 @@ mod tests {
314
416
let payment_wallet_2 = "secret1ap26qrlp8mcq2pg6r47w43l0y8zkqm8a450s07" . to_string ( ) ;
315
417
let attestation_report = "" . to_string ( ) ;
316
418
317
- let execute_msg = ExecuteMsg :: RegisterWorker {
318
- ip_address : ip_address_1. clone ( ) ,
319
- payment_wallet : payment_wallet_1. clone ( ) ,
320
- attestation_report : attestation_report. clone ( ) ,
321
- } ;
322
- let res = execute ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , execute_msg) . unwrap ( ) ;
419
+ let res = register_worker (
420
+ & mut deps,
421
+ ip_address_1. clone ( ) ,
422
+ payment_wallet_1. clone ( ) ,
423
+ ATTESTATION_REPORT . into ( ) ,
424
+ )
425
+ . unwrap ( ) ;
323
426
assert_eq ! ( 0 , res. messages. len( ) ) ;
324
427
325
- let execute_msg = ExecuteMsg :: RegisterWorker {
326
- ip_address : ip_address_2. clone ( ) ,
327
- payment_wallet : payment_wallet_1. clone ( ) ,
328
- attestation_report : attestation_report. clone ( ) ,
329
- } ;
330
- let res = execute ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , execute_msg) . unwrap ( ) ;
428
+ let res = register_worker (
429
+ & mut deps,
430
+ ip_address_2. clone ( ) ,
431
+ payment_wallet_1. clone ( ) ,
432
+ ATTESTATION_REPORT . into ( ) ,
433
+ )
434
+ . unwrap ( ) ;
331
435
assert_eq ! ( 0 , res. messages. len( ) ) ;
332
436
333
- let execute_msg = ExecuteMsg :: RegisterWorker {
334
- ip_address : ip_address_3. clone ( ) ,
335
- payment_wallet : payment_wallet_2. clone ( ) ,
336
- attestation_report : attestation_report. clone ( ) ,
337
- } ;
338
- let res = execute ( deps. as_mut ( ) , env. clone ( ) , info. clone ( ) , execute_msg) . unwrap ( ) ;
437
+ let res = register_worker (
438
+ & mut deps,
439
+ ip_address_3. clone ( ) ,
440
+ payment_wallet_2. clone ( ) ,
441
+ ATTESTATION_REPORT . into ( ) ,
442
+ )
443
+ . unwrap ( ) ;
339
444
assert_eq ! ( 0 , res. messages. len( ) ) ;
340
445
341
446
let query_msg = QueryMsg :: GetWorkers {
342
447
address : "" . to_string ( ) ,
343
448
signature : "" . to_string ( ) ,
344
449
subscriber_public_key : "" . to_string ( ) ,
345
450
} ;
346
- let res = query ( deps. as_ref ( ) , env , query_msg) . unwrap ( ) ;
451
+ let res = query ( deps. as_ref ( ) , mock_env ( ) , query_msg) . unwrap ( ) ;
347
452
348
453
let workers: GetWorkersResponse = from_binary ( & res) . unwrap ( ) ;
349
454
assert_eq ! (
0 commit comments