From 566c9dfa7de7d3aeb5b71f75cd3813c9327336b3 Mon Sep 17 00:00:00 2001 From: Sameh Abouel-saad Date: Mon, 30 Sep 2024 11:37:15 +0300 Subject: [PATCH] test: correct case 2 --- .../pallet-smart-contract/src/tests.rs | 79 ++++++------------- 1 file changed, 26 insertions(+), 53 deletions(-) diff --git a/substrate-node/pallets/pallet-smart-contract/src/tests.rs b/substrate-node/pallets/pallet-smart-contract/src/tests.rs index e7359576d..397b1d93b 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/tests.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/tests.rs @@ -3141,7 +3141,7 @@ fn test_node_contract_on_dedicated_node_shouldnt_be_restored_if_rent_contract_in #[test] fn test_node_contract_with_overdraft_on_dedicated_node_shouldnt_be_restored_when_rent_contract_overdraft_settled() { - // Test ensuring that a node contract on a dedicated node remains in the grace period if the rent contract is in the grace period, even if the node contract overdraft is settled + // Test ensuring that a node contract with overdraft on a dedicated node remains in the grace period when the rent contract overdraft is settled let (mut ext, mut pool_state) = new_test_ext_with_pool_state(0); ext.execute_with(|| { run_to_block(1, Some(&mut pool_state)); @@ -3154,7 +3154,7 @@ fn test_node_contract_with_overdraft_on_dedicated_node_shouldnt_be_restored_when // Create a rent contract assert_ok!(SmartContractModule::create_rent_contract( - RuntimeOrigin::signed(bob()), + RuntimeOrigin::signed(charlie()), node_id, None )); @@ -3164,7 +3164,7 @@ fn test_node_contract_with_overdraft_on_dedicated_node_shouldnt_be_restored_when // Create a node contract with a public IP assert_ok!(SmartContractModule::create_node_contract( - RuntimeOrigin::signed(bob()), + RuntimeOrigin::signed(charlie()), node_id, generate_deployment_hash(), get_deployment_data(), @@ -3174,6 +3174,11 @@ fn test_node_contract_with_overdraft_on_dedicated_node_shouldnt_be_restored_when let node_contract_id = 2; push_contract_resources_used(node_contract_id); + // Get spendable balance + let spendable_balance = SmartContractModule::get_usable_balance(&charlie()); + + // The grace period for the rent contract should start + // Node contract should also transition to grace period pool_state .write() .should_call_bill_contract(rent_contract_id, Ok(Pays::Yes.into()), 11); @@ -3184,64 +3189,32 @@ fn test_node_contract_with_overdraft_on_dedicated_node_shouldnt_be_restored_when .should_call_bill_contract(node_contract_id, Ok(Pays::Yes.into()), 12); run_to_block(12, Some(&mut pool_state)); - // Transfer all balance from the owner of the contract to trigger the grace period to start - let bob_balance = Balances::usable_balance(bob()); - Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), charlie(), bob_balance).unwrap(); - - // The grace period for the rent contract should start - // Node contract should also transition to grace period - pool_state - .write() - .should_call_bill_contract(rent_contract_id, Ok(Pays::Yes.into()), 21); - run_to_block(21, Some(&mut pool_state)); + let node_contract = SmartContractModule::contracts(rent_contract_id).unwrap(); + assert_eq!(node_contract.state, types::ContractState::GracePeriod(11)); - // Check if events contains ContractGracePeriodStarted events for both contracts - let events = System::events(); - log::debug!("Events: {:?}", events); - assert_eq!( - events - .iter() - .filter(|e| matches!( - e.event, - MockEvent::SmartContractModule(SmartContractEvent::ContractGracePeriodStarted { .. }) - )) - .count(), - 2 - ); + let node_contract = SmartContractModule::contracts(node_contract_id).unwrap(); + assert_eq!(node_contract.state, types::ContractState::GracePeriod(11)); - // Transfer some balance to the owner of the contract to settel only the cost of IP rent (node contract) + // Transfer some balance to the owner of the contract to settle only the cost of node resources (rent contract) // Case 2: see https://github.com/threefoldtech/tfchain/issues/1002 - // Calculate the node contract cost - let (amount_due, discount_level) = calculate_tft_cost(node_contract_id, 2, 10); + // Calculate the rent contract cost + let (contract_cost, discount_level) = calculate_tft_cost(rent_contract_id, 3, 20); + let amount_due = contract_cost - spendable_balance; log::debug!("Amount due: {}, discount level: {:?}", amount_due, discount_level); - Balances::transfer_allow_death(RuntimeOrigin::signed(charlie()), bob(), amount_due).unwrap(); + Balances::transfer_allow_death(RuntimeOrigin::signed(bob()), charlie(), amount_due).unwrap(); + // The grace period for the rent contract should end, + // but the node contract should remain in the grace period due to an outstanding overdraft pool_state .write() - .should_call_bill_contract(node_contract_id, Ok(Pays::Yes.into()), 22); - run_to_block(22, Some(&mut pool_state)); - - // Check if last event is Contract billed event - let events = System::events(); - log::debug!("Events: {:?}", events); - let contract_bill_event = types::ContractBill { - contract_id: node_contract_id, - timestamp: 1628082132, - discount_level, - amount_billed: amount_due as u128, - }; - assert_eq!( - events - .last() - .unwrap() - .event, - MockEvent::SmartContractModule(SmartContractEvent::ContractBilled(contract_bill_event)) - ); + .should_call_bill_contract(rent_contract_id, Ok(Pays::Yes.into()), 21); + run_to_block(21, Some(&mut pool_state)); + + let node_contract = SmartContractModule::contracts(rent_contract_id).unwrap(); + assert_eq!(node_contract.state, types::ContractState::Created); - // Node contract cost settled (IP address cost) but should remain in grace period due to suspened rent contract - // Because the cost of the node resources utlized by this node contract is billed to the rent contract let node_contract = SmartContractModule::contracts(node_contract_id).unwrap(); - assert_eq!(node_contract.state, types::ContractState::GracePeriod(21)); + assert_eq!(node_contract.state, types::ContractState::GracePeriod(11)); }); } @@ -3305,7 +3278,7 @@ fn test_rent_contract_should_wait_for_proper_distribution_of_rewards_when_grace_ .should_call_bill_contract(rent_contract_id, Ok(Pays::Yes.into()), 21); run_to_block(21, Some(&mut pool_state)); - // Check if events contains ContractGracePeriodStarted events for both contracts + // Check if events contain ContractGracePeriodStarted events for both contracts let events = System::events(); log::debug!("Events: {:?}", events); assert_eq!(