Skip to content

Commit

Permalink
update smart-contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renauter committed Aug 30, 2024
1 parent a782d27 commit 6ea53f8
Showing 1 changed file with 53 additions and 33 deletions.
86 changes: 53 additions & 33 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,8 @@ fn test_node_contract_billing_details() {

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

let twin = TfgridModule::twins(2).unwrap();
let twin_id = 2;
let twin = TfgridModule::twins(twin_id).unwrap();
let initial_twin_balance = Balances::total_balance(&twin.account_id);

assert_ok!(SmartContractModule::create_node_contract(
Expand Down Expand Up @@ -1040,7 +1041,8 @@ fn test_node_contract_billing_works_for_non_existing_accounts() {

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

let twin = TfgridModule::twins(2).unwrap();
let twin_id = 2;
let twin = TfgridModule::twins(twin_id).unwrap();
let initial_twin_balance = Balances::total_balance(&twin.account_id);

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

// contract payment should be overdrawn
let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
let contract_payment_state =
SmartContractModule::contract_payment_state(contract_id).unwrap();
assert_ne!(contract_payment_state.get_overdraft(), 0);

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

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

let twin = TfgridModule::twins(2).unwrap();
let twin_id = 2;
let twin = TfgridModule::twins(twin_id).unwrap();
let initial_twin_balance = Balances::free_balance(&twin.account_id);
let initial_farmer_balance = Balances::free_balance(alice());

Expand Down Expand Up @@ -1623,7 +1627,8 @@ fn test_node_contract_billing_cycles_cancel_contract_during_cycle_without_balanc

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

let twin = TfgridModule::twins(2).unwrap();
let twin_id = 2;
let twin = TfgridModule::twins(twin_id).unwrap();
let initial_twin_balance = Balances::free_balance(&twin.account_id);
info!("initial twin balance: {:?}", initial_twin_balance);
let initial_farmer_balance = Balances::free_balance(alice());
Expand All @@ -1637,7 +1642,6 @@ fn test_node_contract_billing_cycles_cancel_contract_during_cycle_without_balanc
None
));
let contract_id = 1;
let twin_id = 2;
push_contract_resources_used(contract_id);

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

let leave = 1000;
Balances::transfer(
Balances::transfer_allow_death(
RuntimeOrigin::signed(bob()),
charlie(),
initial_twin_balance - total_amount_billed - leave,
Expand Down Expand Up @@ -1816,7 +1820,7 @@ fn test_restore_node_contract_in_grace_works() {
run_to_block(31, Some(&mut pool_state));
run_to_block(41, Some(&mut pool_state));
// Transfer some balance to the owner of the contract to trigger the grace period to stop
Balances::transfer(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), charlie(), 100000000).unwrap();
run_to_block(52, Some(&mut pool_state));
run_to_block(62, Some(&mut pool_state));

Expand Down Expand Up @@ -2009,6 +2013,7 @@ fn test_rent_contract_billing() {
node_id,
None
));
let twin_id = 2;
let contract_id = 1;

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

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

Expand Down Expand Up @@ -2075,7 +2080,7 @@ fn test_rent_contract_billing() {
run_to_block(51, Some(&mut pool_state));

// should bill partial cycle 5 [45-51], 6 blocks
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 6);
let (amount_due_as_u128, discount_received) = calculate_tft_cost(contract_id, twin_id, 6);
assert_ne!(amount_due_as_u128, 0);
check_report_cost(contract_id, amount_due_as_u128, 51, discount_received);
});
Expand Down Expand Up @@ -2159,6 +2164,7 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
node_id,
None
));
let twin_id = 2;
let contract_id = 1;

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

let (amount_due_1_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 10);
let (amount_due_1_as_u128, discount_received) =
calculate_tft_cost(contract_id, twin_id, 10);
assert_ne!(amount_due_1_as_u128, 0);
check_report_cost(
contract_id,
Expand All @@ -2182,7 +2189,7 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
discount_received.clone(),
);

let twin = TfgridModule::twins(2).unwrap();
let twin = TfgridModule::twins(twin_id).unwrap();
let total_balance = Balances::total_balance(&twin.account_id);
let free_balance = Balances::free_balance(&twin.account_id);
assert_ne!(total_balance, free_balance);
Expand All @@ -2191,16 +2198,16 @@ fn test_rent_contract_billing_cancel_should_bill_reserved_balance() {
// cancel contract
// it will bill before removing the contract and it should bill all
// reserved balance
let (amount_due_2_as_u128, discount_received) = calculate_tft_cost(contract_id, 2, 2);
let (amount_due_2_as_u128, discount_received) = calculate_tft_cost(contract_id, twin_id, 2);
assert_ok!(SmartContractModule::cancel_contract(
RuntimeOrigin::signed(bob()),
contract_id
));

let twin = TfgridModule::twins(2).unwrap();
let usable_balance = Balances::usable_balance(&twin.account_id);
assert_ne!(usable_balance, 0);
Balances::transfer(RuntimeOrigin::signed(bob()), alice(), usable_balance).unwrap();
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), alice(), usable_balance)
.unwrap();

// Last amount due is not the same as the first one
assert_ne!(amount_due_1_as_u128, amount_due_2_as_u128);
Expand Down Expand Up @@ -2229,9 +2236,10 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
node_id,
None
));
let twin_id = 3;
let contract_id = 1;

let twin = TfgridModule::twins(3).unwrap();
let twin = TfgridModule::twins(twin_id).unwrap();
let initial_reservable_balance =
Balances::usable_balance(&twin.account_id) - EXISTENTIAL_DEPOSIT;

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

let (amount_due_per_cycle, _) = calculate_tft_cost(contract_id, 2, 10);
let (amount_due_per_cycle, _) = calculate_tft_cost(contract_id, twin_id, 10);
assert_ne!(amount_due_per_cycle, 0);

// expect contractPaymentOverdarwn event with partially billed amount
Expand All @@ -2264,7 +2272,8 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
true
);

let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
let contract_payment_state =
SmartContractModule::contract_payment_state(contract_id).unwrap();
assert_eq!(
contract_payment_state.get_reserve(),
initial_reservable_balance
Expand Down Expand Up @@ -2295,7 +2304,8 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
))),
true
);
let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
let contract_payment_state =
SmartContractModule::contract_payment_state(contract_id).unwrap();
assert_eq!(
contract_payment_state.get_reserve(),
initial_reservable_balance
Expand All @@ -2307,7 +2317,7 @@ fn test_rent_contract_overdrawn_and_partial_bill() {
let should_have_next_cycle = amount_due_per_cycle * 3 - initial_reservable_balance;

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

let contract_payment_state = SmartContractModule::contract_payment_state(contract_id).unwrap();
let contract_payment_state =
SmartContractModule::contract_payment_state(contract_id).unwrap();
assert_eq!(
contract_payment_state.get_reserve(),
amount_due_per_cycle * 3
Expand Down Expand Up @@ -2349,6 +2360,7 @@ fn test_rent_contract_canceled_mid_cycle_should_bill_for_remainder() {
node_id,
None
));
let twin_id = 2;
let contract_id = 1;

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

let twin = TfgridModule::twins(2).unwrap();
let twin = TfgridModule::twins(twin_id).unwrap();

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

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

// Twin should have no more reserved balance
let twin = TfgridModule::twins(2).unwrap();
let twin = TfgridModule::twins(twin_id).unwrap();
let reserved_balance = Balances::reserved_balance(&twin.account_id);
assert_eq!(reserved_balance, 0);
});
Expand Down Expand Up @@ -2413,6 +2425,7 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi
0,
None
));
let twin_id = 2;
let node_contract_id = 2;
push_contract_resources_used(node_contract_id);

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

let (amount_due_as_u128, discount_received) = calculate_tft_cost(rent_contract_id, 2, 10);
let (amount_due_as_u128, discount_received) =
calculate_tft_cost(rent_contract_id, twin_id, 10);
assert_ne!(amount_due_as_u128, 0);
check_report_cost(rent_contract_id, amount_due_as_u128, 11, discount_received);

Expand Down Expand Up @@ -2567,6 +2581,7 @@ fn test_create_rent_contract_and_node_contract_with_ip_billing_works() {
node_id,
None
));
let twin_id = 2;
let rent_contract_id = 1;

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

// check contract 1 costs (Rent Contract)
let (amount_due_as_u128, discount_received) = calculate_tft_cost(rent_contract_id, 2, 10);
let (amount_due_as_u128, discount_received) =
calculate_tft_cost(rent_contract_id, twin_id, 10);
assert_ne!(amount_due_as_u128, 0);
check_report_cost(rent_contract_id, amount_due_as_u128, 11, discount_received);

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

// check contract 2 costs (Node Contract)
let (amount_due_as_u128, discount_received) = calculate_tft_cost(node_contract_id, 2, 10);
let (amount_due_as_u128, discount_received) =
calculate_tft_cost(node_contract_id, twin_id, 10);
assert_ne!(amount_due_as_u128, 0);
check_report_cost(node_contract_id, amount_due_as_u128, 12, discount_received);

Expand Down Expand Up @@ -2700,7 +2717,7 @@ fn test_restore_rent_contract_in_grace_works() {
run_to_block(31, Some(&mut pool_state));

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

pool_state
.write()
Expand Down Expand Up @@ -2805,7 +2822,7 @@ fn test_restore_rent_contract_and_node_contracts_in_grace_works() {
run_to_block(32, Some(&mut pool_state));

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

pool_state
.write()
Expand Down Expand Up @@ -3576,15 +3593,16 @@ fn test_service_contract_bill_works() {
get_timestamp_in_seconds_for_block(1)
);

let consumer_twin = TfgridModule::twins(2).unwrap();
let consumer_twin_id = 2;
let consumer_twin = TfgridModule::twins(consumer_twin_id).unwrap();
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
assert_eq!(consumer_balance, 2500000000);

// Bill 20 min after contract approval
run_to_block(201, Some(&mut pool_state));
assert_ok!(SmartContractModule::service_contract_bill(
RuntimeOrigin::signed(alice()),
1,
service_contract_id,
VARIABLE_AMOUNT,
b"bill_metadata_1".to_vec(),
));
Expand Down Expand Up @@ -3766,9 +3784,11 @@ fn test_service_contract_bill_out_of_funds_fails() {
approve_service_consumer_contract(service_contract_id);

// Drain consumer account
let consumer_twin = TfgridModule::twins(2).unwrap();
let twin_id = 2;
let consumer_twin = TfgridModule::twins(twin_id).unwrap();
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
Balances::transfer(RuntimeOrigin::signed(bob()), alice(), consumer_balance).unwrap();
Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), alice(), consumer_balance)
.unwrap();
let consumer_balance = Balances::free_balance(&consumer_twin.account_id);
assert_eq!(consumer_balance, 0);

Expand Down

0 comments on commit 6ea53f8

Please sign in to comment.