Skip to content

Commit

Permalink
test: correct case 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sameh-farouk committed Sep 30, 2024
1 parent 9f75e2d commit 566c9df
Showing 1 changed file with 26 additions and 53 deletions.
79 changes: 26 additions & 53 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
));
Expand All @@ -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(),
Expand All @@ -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);
Expand All @@ -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));
});
}

Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit 566c9df

Please sign in to comment.