Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 606cab7

Browse files
authored
refactor: Use CompoundCycles in helpers in ExecutionTest (#9696)
This PR switches various helper functions in `ExecutionTest` that calculate fees to return `CompoundCycles` instead of `Cycles`. This pushes the decision on what part of the computed fee (real or nominal) to implementors of the test which makes more sense depending on what needs to be tested. It also facilitates computing expected balance or metrics changes from the same compound value which strengthens the tests.
1 parent 33077fe commit 606cab7

6 files changed

Lines changed: 121 additions & 121 deletions

File tree

rs/execution_environment/src/canister_manager/tests.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,7 +3164,7 @@ fn creating_canisters_always_works_if_limit_is_set_to_zero() {
31643164
test.inject_call_to_ic00(
31653165
Method::CreateCanister,
31663166
EmptyBlob.encode(),
3167-
test.canister_creation_fee(),
3167+
test.canister_creation_fee().real(),
31683168
);
31693169
test.execute_all();
31703170
}
@@ -7503,7 +7503,7 @@ fn create_canister_memory_allocation_makes_subnet_oversubscribed() {
75037503
call_args()
75047504
.other_side(args.encode())
75057505
.on_reject(wasm().reject_message().reject()),
7506-
test.canister_creation_fee() + Cycles::new(1_000_000_000),
7506+
test.canister_creation_fee().real() + Cycles::new(1_000_000_000),
75077507
)
75087508
.build();
75097509
let result = test.ingress(uc, "update", create_canister);
@@ -7527,7 +7527,7 @@ fn create_canister_memory_allocation_makes_subnet_oversubscribed() {
75277527
call_args()
75287528
.other_side(args.encode())
75297529
.on_reject(wasm().reject_message().reject()),
7530-
test.canister_creation_fee() + Cycles::new(1_000_000_000),
7530+
test.canister_creation_fee().real() + Cycles::new(1_000_000_000),
75317531
)
75327532
.build();
75337533

@@ -7563,7 +7563,7 @@ fn create_canister_computes_allocation_makes_subnet_oversubscribed() {
75637563
call_args()
75647564
.other_side(args.encode())
75657565
.on_reject(wasm().reject_message().reject()),
7566-
test.canister_creation_fee() + Cycles::new(1_000_000_000),
7566+
test.canister_creation_fee().real() + Cycles::new(1_000_000_000),
75677567
)
75687568
.build();
75697569
let result = test.ingress(uc, "update", create_canister);
@@ -7585,7 +7585,7 @@ fn create_canister_computes_allocation_makes_subnet_oversubscribed() {
75857585
call_args()
75867586
.other_side(args.encode())
75877587
.on_reject(wasm().reject_message().reject()),
7588-
test.canister_creation_fee() + Cycles::new(1_000_000_000),
7588+
test.canister_creation_fee().real() + Cycles::new(1_000_000_000),
75897589
)
75907590
.build();
75917591
let result = test.ingress(uc, "update", create_canister);
@@ -7608,7 +7608,7 @@ fn create_canister_computes_allocation_makes_subnet_oversubscribed() {
76087608
call_args()
76097609
.other_side(args.encode())
76107610
.on_reject(wasm().reject_message().reject()),
7611-
test.canister_creation_fee() + Cycles::new(1_000_000_000),
7611+
test.canister_creation_fee().real() + Cycles::new(1_000_000_000),
76127612
)
76137613
.build();
76147614

@@ -7643,7 +7643,7 @@ fn create_canister_when_compute_capacity_is_oversubscribed() {
76437643
CanisterId::ic_00(),
76447644
Method::CreateCanister,
76457645
call_args().other_side(args.encode()),
7646-
test.canister_creation_fee(),
7646+
test.canister_creation_fee().real(),
76477647
)
76487648
.build();
76497649

@@ -7666,7 +7666,7 @@ fn create_canister_when_compute_capacity_is_oversubscribed() {
76667666
call_args()
76677667
.other_side(args.encode())
76687668
.on_reject(wasm().reject_message().reject()),
7669-
test.canister_creation_fee(),
7669+
test.canister_creation_fee().real(),
76707670
)
76717671
.build();
76727672
let result = test.ingress(uc, "update", create_canister);
@@ -7688,7 +7688,7 @@ fn create_canister_when_compute_capacity_is_oversubscribed() {
76887688
call_args()
76897689
.other_side(args.encode())
76907690
.on_reject(wasm().reject_message().reject()),
7691-
test.canister_creation_fee(),
7691+
test.canister_creation_fee().real(),
76927692
)
76937693
.build();
76947694
test.ingress(uc, "update", create_canister)
@@ -7763,7 +7763,7 @@ fn create_canister_insufficient_cycles_for_memory_allocation() {
77637763
call_args()
77647764
.other_side(args.encode())
77657765
.on_reject(wasm().reject_message().reject()),
7766-
test.canister_creation_fee(),
7766+
test.canister_creation_fee().real(),
77677767
)
77687768
.build();
77697769

rs/execution_environment/src/execution/call_or_task/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ fn dts_update_concurrent_cycles_change_succeeds() {
192192
)
193193
.real();
194194

195-
let call_charge = test.call_fee("update", &b)
195+
let call_charge = test.call_fee("update", &b).real()
196196
+ max_execution_cost
197-
+ test.max_response_fee()
197+
+ test.max_response_fee().real()
198198
+ transferred_cycles;
199199

200200
let cycles_debit = Cycles::new(1000);
@@ -999,8 +999,8 @@ fn dts_abort_of_replicated_execution_works() {
999999
Cycles::new(initial_cycles)
10001000
- transferred_cycles
10011001
- test.canister_execution_cost(a_id)
1002-
- test.call_fee(method, &b)
1003-
- test.reply_fee(&[42])
1002+
- test.call_fee(method, &b).real()
1003+
- test.reply_fee(&[42]).real()
10041004
);
10051005

10061006
assert_eq!(

rs/execution_environment/src/execution/response/tests.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ fn cycles_correct_if_response_fails() {
398398
test.canister_state(a_id).system_state.balance(),
399399
initial_cycles
400400
- test.canister_execution_cost(a_id)
401-
- test.call_fee("update", &b)
402-
- test.reply_fee(&b)
401+
- test.call_fee("update", &b).real()
402+
- test.reply_fee(&b).real()
403403
);
404404
let ingress_status = test.ingress_status(&ingress_id);
405405
let result = check_ingress_status(ingress_status).unwrap_err();
@@ -448,8 +448,8 @@ fn cycles_correct_if_cleanup_fails() {
448448
test.canister_state(a_id).system_state.balance(),
449449
initial_cycles
450450
- test.canister_execution_cost(a_id)
451-
- test.call_fee("update", &b)
452-
- test.reply_fee(&b)
451+
- test.call_fee("update", &b).real()
452+
- test.reply_fee(&b).real()
453453
);
454454
let ingress_status = test.ingress_status(&ingress_id);
455455
let result = check_ingress_status(ingress_status).unwrap_err();
@@ -1349,15 +1349,16 @@ fn dts_response_concurrent_cycles_change_succeeds() {
13491349
)
13501350
.real();
13511351

1352-
let call_charge = test.call_fee("update", &b)
1352+
let call_charge = test.call_fee("update", &b).real()
13531353
+ max_execution_cost
1354-
+ test.max_response_fee()
1354+
+ test.max_response_fee().real()
13551355
+ transferred_cycles;
13561356

13571357
let refund = test.max_response_fee() - test.reply_fee(&b);
13581358

13591359
// Reset the cycles balance of canister A to simplify cycles bookkeeping.
1360-
let initial_cycles = freezing_threshold + additional_freezing_threshold + call_charge - refund;
1360+
let initial_cycles =
1361+
freezing_threshold + additional_freezing_threshold + call_charge - refund.real();
13611362
let initial_execution_cost = test.canister_execution_cost(a_id);
13621363
test.canister_state_mut(a_id)
13631364
.system_state
@@ -1392,7 +1393,7 @@ fn dts_response_concurrent_cycles_change_succeeds() {
13921393

13931394
assert_eq!(
13941395
test.canister_state(a_id).system_state.balance(),
1395-
initial_cycles + refund - call_charge - cycles_debit
1396+
initial_cycles + refund.real() - call_charge - cycles_debit
13961397
+ (max_execution_cost - (test.canister_execution_cost(a_id) - initial_execution_cost))
13971398
);
13981399
}
@@ -1472,15 +1473,16 @@ fn dts_response_concurrent_cycles_change_fails() {
14721473
)
14731474
.real();
14741475

1475-
let call_charge = test.call_fee("update", &b)
1476+
let call_charge = test.call_fee("update", &b).real()
14761477
+ max_execution_cost
1477-
+ test.max_response_fee()
1478+
+ test.max_response_fee().real()
14781479
+ transferred_cycles;
14791480

14801481
let refund = test.max_response_fee() - test.reply_fee(&b);
14811482

14821483
// Reset the cycles balance of canister A to simplify cycles bookkeeping.
1483-
let initial_cycles = freezing_threshold + additional_freezing_threshold + call_charge - refund;
1484+
let initial_cycles =
1485+
freezing_threshold + additional_freezing_threshold + call_charge - refund.real();
14841486
let initial_execution_cost = test.canister_execution_cost(a_id);
14851487
test.canister_state_mut(a_id)
14861488
.system_state
@@ -1520,13 +1522,13 @@ fn dts_response_concurrent_cycles_change_fails() {
15201522
format!(
15211523
"Error from Canister {a_id}: Canister {a_id} is out of cycles: \
15221524
please top up the canister with at least {} additional cycles",
1523-
call_charge - (initial_cycles + refund - cycles_debit)
1525+
call_charge - (initial_cycles + refund.real() - cycles_debit)
15241526
)
15251527
);
15261528

15271529
assert_eq!(
15281530
test.canister_state(a_id).system_state.balance(),
1529-
initial_cycles + refund - cycles_debit
1531+
initial_cycles + refund.real() - cycles_debit
15301532
+ (max_execution_cost - (test.canister_execution_cost(a_id) - initial_execution_cost))
15311533
);
15321534
}
@@ -1618,15 +1620,16 @@ fn dts_response_with_cleanup_concurrent_cycles_change_succeeds() {
16181620
)
16191621
.real();
16201622

1621-
let call_charge = test.call_fee("update", &b)
1623+
let call_charge = test.call_fee("update", &b).real()
16221624
+ max_execution_cost
1623-
+ test.max_response_fee()
1625+
+ test.max_response_fee().real()
16241626
+ transferred_cycles;
16251627

16261628
let refund = test.max_response_fee() - test.reply_fee(&b);
16271629

16281630
// Reset the cycles balance of canister A to simplify cycles bookkeeping.
1629-
let initial_cycles = freezing_threshold + additional_freezing_threshold + call_charge - refund;
1631+
let initial_cycles =
1632+
freezing_threshold + additional_freezing_threshold + call_charge - refund.real();
16301633
let initial_execution_cost = test.canister_execution_cost(a_id);
16311634
test.canister_state_mut(a_id)
16321635
.system_state
@@ -1668,7 +1671,7 @@ fn dts_response_with_cleanup_concurrent_cycles_change_succeeds() {
16681671

16691672
assert_eq!(
16701673
test.canister_state(a_id).system_state.balance(),
1671-
initial_cycles + refund - cycles_debit
1674+
initial_cycles + refund.real() - cycles_debit
16721675
+ (max_execution_cost - (test.canister_execution_cost(a_id) - initial_execution_cost))
16731676
);
16741677

rs/execution_environment/src/execution_environment/tests.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,13 +2121,13 @@ fn management_canister_xnet_to_nns_called_from_non_nns() {
21212121
test.inject_call_to_ic00(
21222122
Method::CreateCanister,
21232123
EmptyBlob.encode(),
2124-
test.canister_creation_fee(),
2124+
test.canister_creation_fee().real(),
21252125
);
21262126
test.inject_call_to_ic00(Method::RawRand, EmptyBlob.encode(), Cycles::from(0_u64));
21272127
test.inject_call_to_ic00(
21282128
Method::HttpRequest,
21292129
EmptyBlob.encode(),
2130-
test.http_request_fee(NumBytes::from(0), None),
2130+
test.http_request_fee(NumBytes::from(0), None).real(),
21312131
);
21322132
test.execute_all();
21332133
for response in test.xnet_messages().clone() {
@@ -2220,13 +2220,13 @@ fn management_canister_xnet_called_from_non_nns() {
22202220
test.inject_call_to_ic00(
22212221
Method::CreateCanister,
22222222
EmptyBlob.encode(),
2223-
test.canister_creation_fee(),
2223+
test.canister_creation_fee().real(),
22242224
);
22252225
test.inject_call_to_ic00(Method::RawRand, EmptyBlob.encode(), Cycles::from(0_u64));
22262226
test.inject_call_to_ic00(
22272227
Method::HttpRequest,
22282228
EmptyBlob.encode(),
2229-
test.http_request_fee(NumBytes::from(0), None),
2229+
test.http_request_fee(NumBytes::from(0), None).real(),
22302230
);
22312231
test.execute_all();
22322232
for response in test.xnet_messages().clone() {
@@ -2254,7 +2254,7 @@ fn create_canister_xnet_called_from_nns() {
22542254
test.inject_call_to_ic00(
22552255
Method::CreateCanister,
22562256
EmptyBlob.encode(),
2257-
test.canister_creation_fee(),
2257+
test.canister_creation_fee().real(),
22582258
);
22592259
test.execute_all();
22602260
let response = test.xnet_messages()[0].clone();
@@ -2285,7 +2285,7 @@ fn setup_initial_dkg_sender_on_nns() {
22852285
test.inject_call_to_ic00(
22862286
Method::SetupInitialDKG,
22872287
args.encode(),
2288-
test.canister_creation_fee(),
2288+
test.canister_creation_fee().real(),
22892289
);
22902290
test.execute_all();
22912291
assert_eq!(0, test.xnet_messages().len());
@@ -2307,7 +2307,7 @@ fn setup_initial_dkg_sender_not_on_nns() {
23072307
test.inject_call_to_ic00(
23082308
Method::SetupInitialDKG,
23092309
args.encode(),
2310-
test.canister_creation_fee(),
2310+
test.canister_creation_fee().real(),
23112311
);
23122312
test.execute_all();
23132313
let response = test.xnet_messages()[0].clone();
@@ -2317,7 +2317,7 @@ fn setup_initial_dkg_sender_not_on_nns() {
23172317
originator: other_canister,
23182318
respondent: CanisterId::from(own_subnet),
23192319
originator_reply_callback: CallbackId::new(0),
2320-
refund: test.canister_creation_fee(),
2320+
refund: test.canister_creation_fee().real(),
23212321
response_payload: Payload::Reject(RejectContext::new(
23222322
RejectCode::CanisterError,
23232323
format!(
@@ -3044,18 +3044,18 @@ fn execute_canister_http_request() {
30443044
http_request_context.variable_parts_size(),
30453045
Some(NumBytes::from(response_size_limit)),
30463046
);
3047-
assert_eq!(http_request_context.request.payment, payment - fee);
3047+
assert_eq!(http_request_context.request.payment, payment - fee.real());
30483048

30493049
assert_eq!(
3050-
NominalCycles::new(fee.get()),
3050+
fee.nominal(),
30513051
test.state()
30523052
.metadata
30533053
.subnet_metrics
30543054
.get_consumed_cycles_http_outcalls()
30553055
);
30563056

30573057
assert_eq!(
3058-
NominalCycles::new(fee.get()),
3058+
fee.nominal(),
30593059
*test
30603060
.state()
30613061
.metadata
@@ -3643,8 +3643,8 @@ fn replicated_query_refunds_all_sent_cycles() {
36433643
test.canister_state(a_id).system_state.balance(),
36443644
initial_cycles
36453645
- test.canister_execution_cost(a_id)
3646-
- test.call_fee("query", &b_callback)
3647-
- test.reply_fee(&b_callback)
3646+
- test.call_fee("query", &b_callback).real()
3647+
- test.reply_fee(&b_callback).real()
36483648
);
36493649

36503650
// Canister B doesn't get the transferred cycles.
@@ -3727,8 +3727,8 @@ fn replicated_query_can_accept_cycles() {
37273727
test.canister_state(a_id).system_state.balance(),
37283728
initial_cycles
37293729
- test.canister_execution_cost(a_id)
3730-
- test.call_fee("query", &b_callback)
3731-
- test.reply_fee(&response_payload)
3730+
- test.call_fee("query", &b_callback).real()
3731+
- test.reply_fee(&response_payload).real()
37323732
- transferred_cycles
37333733
);
37343734

@@ -3813,8 +3813,8 @@ fn replicated_query_does_not_accept_cycles_on_trap() {
38133813
test.canister_state(a_id).system_state.balance(),
38143814
initial_cycles
38153815
- test.canister_execution_cost(a_id)
3816-
- test.call_fee("query", &b_callback)
3817-
- test.reject_fee(reject_message)
3816+
- test.call_fee("query", &b_callback).real()
3817+
- test.reject_fee(reject_message).real()
38183818
);
38193819

38203820
// Canister B does not get any cycles.
@@ -4002,7 +4002,7 @@ fn test_consumed_cycles_by_use_case_with_refund() {
40024002
// Check that canister A's balance is updated correctly.
40034003
assert_eq!(
40044004
test.canister_state(a_id).system_state.balance(),
4005-
initial_cycles - execution_cost - transmission_cost - transferred_cycles
4005+
initial_cycles - execution_cost - transmission_cost.real() - transferred_cycles
40064006
);
40074007

40084008
assert_eq!(
@@ -4032,7 +4032,7 @@ fn test_consumed_cycles_by_use_case_with_refund() {
40324032
// Check that consumed cycles are correct for both use cases.
40334033
assert_eq!(
40344034
transmission_consumption_after_response,
4035-
NominalCycles::new(transmission_cost.get())
4035+
transmission_cost.nominal(),
40364036
);
40374037

40384038
assert_eq!(

0 commit comments

Comments
 (0)