Skip to content

Commit 6ea53f8

Browse files
committed
update smart-contract tests
1 parent a782d27 commit 6ea53f8

File tree

1 file changed

+53
-33
lines changed
  • substrate-node/pallets/pallet-smart-contract/src

1 file changed

+53
-33
lines changed

substrate-node/pallets/pallet-smart-contract/src/tests.rs

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,8 @@ fn test_node_contract_billing_details() {
981981

982982
TFTPriceModule::set_prices(RuntimeOrigin::signed(alice()), 50, 101).unwrap();
983983

984-
let twin = TfgridModule::twins(2).unwrap();
984+
let twin_id = 2;
985+
let twin = TfgridModule::twins(twin_id).unwrap();
985986
let initial_twin_balance = Balances::total_balance(&twin.account_id);
986987

987988
assert_ok!(SmartContractModule::create_node_contract(
@@ -1040,7 +1041,8 @@ fn test_node_contract_billing_works_for_non_existing_accounts() {
10401041

10411042
TFTPriceModule::set_prices(RuntimeOrigin::signed(alice()), 50, 101).unwrap();
10421043

1043-
let twin = TfgridModule::twins(2).unwrap();
1044+
let twin_id = 2;
1045+
let twin = TfgridModule::twins(twin_id).unwrap();
10441046
let initial_twin_balance = Balances::total_balance(&twin.account_id);
10451047

10461048
assert_ok!(SmartContractModule::create_node_contract(
@@ -1124,7 +1126,8 @@ fn test_billing_node_contract_in_graceperiod_should_reset_unbilled_network_consu
11241126
assert_eq!(c1.state, types::ContractState::GracePeriod(11));
11251127

11261128
// contract payment should be overdrawn
1127-
let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
1129+
let contract_payment_state =
1130+
SmartContractModule::contract_payment_state(contract_id).unwrap();
11281131
assert_ne!(contract_payment_state.get_overdraft(), 0);
11291132

11301133
// amount unbilled should have been reset after adding the amount to the contract overdraft
@@ -1148,7 +1151,8 @@ fn test_node_contract_billing_details_with_solution_provider() {
11481151

11491152
TFTPriceModule::set_prices(RuntimeOrigin::signed(alice()), 50, 101).unwrap();
11501153

1151-
let twin = TfgridModule::twins(2).unwrap();
1154+
let twin_id = 2;
1155+
let twin = TfgridModule::twins(twin_id).unwrap();
11521156
let initial_twin_balance = Balances::free_balance(&twin.account_id);
11531157
let initial_farmer_balance = Balances::free_balance(alice());
11541158

@@ -1623,7 +1627,8 @@ fn test_node_contract_billing_cycles_cancel_contract_during_cycle_without_balanc
16231627

16241628
TFTPriceModule::set_prices(RuntimeOrigin::signed(alice()), 50, 101).unwrap();
16251629

1626-
let twin = TfgridModule::twins(2).unwrap();
1630+
let twin_id = 2;
1631+
let twin = TfgridModule::twins(twin_id).unwrap();
16271632
let initial_twin_balance = Balances::free_balance(&twin.account_id);
16281633
info!("initial twin balance: {:?}", initial_twin_balance);
16291634
let initial_farmer_balance = Balances::free_balance(alice());
@@ -1637,7 +1642,6 @@ fn test_node_contract_billing_cycles_cancel_contract_during_cycle_without_balanc
16371642
None
16381643
));
16391644
let contract_id = 1;
1640-
let twin_id = 2;
16411645
push_contract_resources_used(contract_id);
16421646

16431647
let (amount_due_1, discount_received) = calculate_tft_cost(contract_id, twin_id, 10);
@@ -1663,7 +1667,7 @@ fn test_node_contract_billing_cycles_cancel_contract_during_cycle_without_balanc
16631667
info!("total amount billed: {:?}", total_amount_billed);
16641668

16651669
let leave = 1000;
1666-
Balances::transfer(
1670+
Balances::transfer_allow_death(
16671671
RuntimeOrigin::signed(bob()),
16681672
charlie(),
16691673
initial_twin_balance - total_amount_billed - leave,
@@ -1816,7 +1820,7 @@ fn test_restore_node_contract_in_grace_works() {
18161820
run_to_block(31, Some(&mut pool_state));
18171821
run_to_block(41, Some(&mut pool_state));
18181822
// Transfer some balance to the owner of the contract to trigger the grace period to stop
1819-
Balances::transfer(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
1823+
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
18201824
run_to_block(52, Some(&mut pool_state));
18211825
run_to_block(62, Some(&mut pool_state));
18221826

@@ -2009,6 +2013,7 @@ fn test_rent_contract_billing() {
20092013
node_id,
20102014
None
20112015
));
2016+
let twin_id = 2;
20122017
let contract_id = 1;
20132018

20142019
let contract = SmartContractModule::contracts(contract_id).unwrap();
@@ -2038,7 +2043,7 @@ fn test_rent_contract_billing() {
20382043
run_to_block(21, Some(&mut pool_state));
20392044

20402045
// should bill partial cycle 2 [15-21], 6 blocks
2041-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 6);
2046+
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, twin_id, 6);
20422047
assert_ne!(amount_due_as_u128, 0);
20432048
check_report_cost(contract_id, amount_due_as_u128, 21, discount_received);
20442049

@@ -2075,7 +2080,7 @@ fn test_rent_contract_billing() {
20752080
run_to_block(51, Some(&mut pool_state));
20762081

20772082
// should bill partial cycle 5 [45-51], 6 blocks
2078-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 6);
2083+
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, twin_id, 6);
20792084
assert_ne!(amount_due_as_u128, 0);
20802085
check_report_cost(contract_id, amount_due_as_u128, 51, discount_received);
20812086
});
@@ -2159,6 +2164,7 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
21592164
node_id,
21602165
None
21612166
));
2167+
let twin_id = 2;
21622168
let contract_id = 1;
21632169

21642170
let contract = SmartContractModule::contracts(contract_id).unwrap();
@@ -2173,7 +2179,8 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
21732179
.should_call_bill_contract(contract_id, Ok(Pays::Yes.into()), 11);
21742180
run_to_block(11, Some(&mut pool_state));
21752181

2176-
let (amount_due_1_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 10);
2182+
let (amount_due_1_as_u128, discount_received) =
2183+
calculate_tft_cost(contract_id, twin_id, 10);
21772184
assert_ne!(amount_due_1_as_u128, 0);
21782185
check_report_cost(
21792186
contract_id,
@@ -2182,7 +2189,7 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
21822189
discount_received.clone(),
21832190
);
21842191

2185-
let twin = TfgridModule::twins(2).unwrap();
2192+
let twin = TfgridModule::twins(twin_id).unwrap();
21862193
let total_balance = Balances::total_balance(&twin.account_id);
21872194
let free_balance = Balances::free_balance(&twin.account_id);
21882195
assert_ne!(total_balance, free_balance);
@@ -2191,16 +2198,16 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
21912198
// cancel contract
21922199
// it will bill before removing the contract and it should bill all
21932200
// reserved balance
2194-
let (amount_due_2_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 2);
2201+
let (amount_due_2_as_u128, discount_received) = calculate_tft_cost(contract_id, twin_id, 2);
21952202
assert_ok!(SmartContractModule::cancel_contract(
21962203
RuntimeOrigin::signed(bob()),
21972204
contract_id
21982205
));
21992206

2200-
let twin = TfgridModule::twins(2).unwrap();
22012207
let usable_balance = Balances::usable_balance(&twin.account_id);
22022208
assert_ne!(usable_balance, 0);
2203-
Balances::transfer(RuntimeOrigin::signed(bob()), alice(), usable_balance).unwrap();
2209+
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), alice(), usable_balance)
2210+
.unwrap();
22042211

22052212
// Last amount due is not the same as the first one
22062213
assert_ne!(amount_due_1_as_u128, amount_due_2_as_u128);
@@ -2229,9 +2236,10 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
22292236
node_id,
22302237
None
22312238
));
2239+
let twin_id = 3;
22322240
let contract_id = 1;
22332241

2234-
let twin = TfgridModule::twins(3).unwrap();
2242+
let twin = TfgridModule::twins(twin_id).unwrap();
22352243
let initial_reservable_balance =
22362244
Balances::usable_balance(&twin.account_id) - EXISTENTIAL_DEPOSIT;
22372245

@@ -2247,7 +2255,7 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
22472255
.should_call_bill_contract(contract_id, Ok(Pays::Yes.into()), 11);
22482256
run_to_block(11, Some(&mut pool_state));
22492257

2250-
let (amount_due_per_cycle, _) = calculate_tft_cost(contract_id, 2, 10);
2258+
let (amount_due_per_cycle, _) = calculate_tft_cost(contract_id, twin_id, 10);
22512259
assert_ne!(amount_due_per_cycle, 0);
22522260

22532261
// expect contractPaymentOverdarwn event with partially billed amount
@@ -2264,7 +2272,8 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
22642272
true
22652273
);
22662274

2267-
let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
2275+
let contract_payment_state =
2276+
SmartContractModule::contract_payment_state(contract_id).unwrap();
22682277
assert_eq!(
22692278
contract_payment_state.get_reserve(),
22702279
initial_reservable_balance
@@ -2295,7 +2304,8 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
22952304
))),
22962305
true
22972306
);
2298-
let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
2307+
let contract_payment_state =
2308+
SmartContractModule::contract_payment_state(contract_id).unwrap();
22992309
assert_eq!(
23002310
contract_payment_state.get_reserve(),
23012311
initial_reservable_balance
@@ -2307,7 +2317,7 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
23072317
let should_have_next_cycle = amount_due_per_cycle * 3 - initial_reservable_balance;
23082318

23092319
// transfer some balance to the owner of the contract to trigger the grace period to stop
2310-
Balances::transfer(
2320+
Balances::transfer_allow_death(
23112321
RuntimeOrigin::signed(bob()),
23122322
charlie(),
23132323
should_have_next_cycle,
@@ -2319,7 +2329,8 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
23192329
.should_call_bill_contract(contract_id, Ok(Pays::Yes.into()), 31);
23202330
run_to_block(31, Some(&mut pool_state));
23212331

2322-
let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
2332+
let contract_payment_state =
2333+
SmartContractModule::contract_payment_state(contract_id).unwrap();
23232334
assert_eq!(
23242335
contract_payment_state.get_reserve(),
23252336
amount_due_per_cycle * 3
@@ -2349,6 +2360,7 @@ fn test_rent_contract_canceled_mid_cycle_should_bill_for_remainder() {
23492360
node_id,
23502361
None
23512362
));
2363+
let twin_id = 2;
23522364
let contract_id = 1;
23532365

23542366
let contract = SmartContractModule::contracts(contract_id).unwrap();
@@ -2358,14 +2370,14 @@ fn test_rent_contract_canceled_mid_cycle_should_bill_for_remainder() {
23582370
types::ContractData::RentContract(rent_contract)
23592371
);
23602372

2361-
let twin = TfgridModule::twins(2).unwrap();
2373+
let twin = TfgridModule::twins(twin_id).unwrap();
23622374

23632375
let reserved_balance = Balances::reserved_balance(&twin.account_id);
23642376
info!("reserved balance: {:?}", reserved_balance);
23652377

23662378
run_to_block(8, Some(&mut pool_state));
23672379
// Calculate the cost for 7 blocks of runtime (created a block 1, canceled at block 8)
2368-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 7);
2380+
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, twin_id, 7);
23692381
// cancel rent contract at block 8
23702382
assert_ok!(SmartContractModule::cancel_contract(
23712383
RuntimeOrigin::signed(bob()),
@@ -2380,7 +2392,7 @@ fn test_rent_contract_canceled_mid_cycle_should_bill_for_remainder() {
23802392
);
23812393

23822394
// Twin should have no more reserved balance
2383-
let twin = TfgridModule::twins(2).unwrap();
2395+
let twin = TfgridModule::twins(twin_id).unwrap();
23842396
let reserved_balance = Balances::reserved_balance(&twin.account_id);
23852397
assert_eq!(reserved_balance, 0);
23862398
});
@@ -2413,6 +2425,7 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi
24132425
0,
24142426
None
24152427
));
2428+
let twin_id = 2;
24162429
let node_contract_id = 2;
24172430
push_contract_resources_used(node_contract_id);
24182431

@@ -2425,7 +2438,8 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi
24252438
.should_call_bill_contract(node_contract_id, Ok(Pays::Yes.into()), 12);
24262439
run_to_block(12, Some(&mut pool_state));
24272440

2428-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(rent_contract_id, 2, 10);
2441+
let (amount_due_as_u128, discount_received) =
2442+
calculate_tft_cost(rent_contract_id, twin_id, 10);
24292443
assert_ne!(amount_due_as_u128, 0);
24302444
check_report_cost(rent_contract_id, amount_due_as_u128, 11, discount_received);
24312445

@@ -2567,6 +2581,7 @@ fn test_create_rent_contract_and_node_contract_with_ip_billing_works() {
25672581
node_id,
25682582
None
25692583
));
2584+
let twin_id = 2;
25702585
let rent_contract_id = 1;
25712586

25722587
run_to_block(2, Some(&mut pool_state));
@@ -2588,7 +2603,8 @@ fn test_create_rent_contract_and_node_contract_with_ip_billing_works() {
25882603
run_to_block(11, Some(&mut pool_state));
25892604

25902605
// check contract 1 costs (Rent Contract)
2591-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(rent_contract_id, 2, 10);
2606+
let (amount_due_as_u128, discount_received) =
2607+
calculate_tft_cost(rent_contract_id, twin_id, 10);
25922608
assert_ne!(amount_due_as_u128, 0);
25932609
check_report_cost(rent_contract_id, amount_due_as_u128, 11, discount_received);
25942610

@@ -2598,7 +2614,8 @@ fn test_create_rent_contract_and_node_contract_with_ip_billing_works() {
25982614
run_to_block(12, Some(&mut pool_state));
25992615

26002616
// check contract 2 costs (Node Contract)
2601-
let (amount_due_as_u128, discount_received) = calculate_tft_cost(node_contract_id, 2, 10);
2617+
let (amount_due_as_u128, discount_received) =
2618+
calculate_tft_cost(node_contract_id, twin_id, 10);
26022619
assert_ne!(amount_due_as_u128, 0);
26032620
check_report_cost(node_contract_id, amount_due_as_u128, 12, discount_received);
26042621

@@ -2700,7 +2717,7 @@ fn test_restore_rent_contract_in_grace_works() {
27002717
run_to_block(31, Some(&mut pool_state));
27012718

27022719
// Transfer some balance to the owner of the contract to trigger the grace period to stop
2703-
Balances::transfer(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
2720+
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
27042721

27052722
pool_state
27062723
.write()
@@ -2805,7 +2822,7 @@ fn test_restore_rent_contract_and_node_contracts_in_grace_works() {
28052822
run_to_block(32, Some(&mut pool_state));
28062823

28072824
// Transfer some balance to the owner of the contract to trigger the grace period to stop
2808-
Balances::transfer(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
2825+
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
28092826

28102827
pool_state
28112828
.write()
@@ -3576,15 +3593,16 @@ fn test_service_contract_bill_works() {
35763593
get_timestamp_in_seconds_for_block(1)
35773594
);
35783595

3579-
let consumer_twin = TfgridModule::twins(2).unwrap();
3596+
let consumer_twin_id = 2;
3597+
let consumer_twin = TfgridModule::twins(consumer_twin_id).unwrap();
35803598
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
35813599
assert_eq!(consumer_balance, 2500000000);
35823600

35833601
// Bill 20 min after contract approval
35843602
run_to_block(201, Some(&mut pool_state));
35853603
assert_ok!(SmartContractModule::service_contract_bill(
35863604
RuntimeOrigin::signed(alice()),
3587-
1,
3605+
service_contract_id,
35883606
VARIABLE_AMOUNT,
35893607
b"bill_metadata_1".to_vec(),
35903608
));
@@ -3766,9 +3784,11 @@ fn test_service_contract_bill_out_of_funds_fails() {
37663784
approve_service_consumer_contract(service_contract_id);
37673785

37683786
// Drain consumer account
3769-
let consumer_twin = TfgridModule::twins(2).unwrap();
3787+
let twin_id = 2;
3788+
let consumer_twin = TfgridModule::twins(twin_id).unwrap();
37703789
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
3771-
Balances::transfer(RuntimeOrigin::signed(bob()), alice(), consumer_balance).unwrap();
3790+
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), alice(), consumer_balance)
3791+
.unwrap();
37723792
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
37733793
assert_eq!(consumer_balance, 0);
37743794

0 commit comments

Comments
 (0)