Skip to content

Commit

Permalink
handle grace for node contracts under rent-contract
Browse files Browse the repository at this point in the history
  • Loading branch information
sameh-farouk committed Jul 30, 2024
1 parent 36b7984 commit 150f006
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
5 changes: 3 additions & 2 deletions substrate-node/pallets/pallet-smart-contract/src/billing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ impl<T: Config> Pallet<T> {

let total_amount_due = standard_amount_due.defensive_saturating_add(additional_amount_due);

// if the amount due is zero and the contract is not in deleted or grace period state, don't bill the contract (mostly node contarct on a rented node)
if total_amount_due.is_zero() && matches!(contract.state, types::ContractState::Created) {
// if the amount due is zero and the contract is not in deleted, don't bill the contract (mostly node contarct on a rented node)
if total_amount_due.is_zero() && !matches!(contract.state, types::ContractState::Deleted(_)) {
log::info!(
"Amount to be billed is 0, contract state: {:?}, nothing to do with contract_id: {:?}",
contract.state,
Expand Down Expand Up @@ -278,6 +278,7 @@ impl<T: Config> Pallet<T> {
twin_id: contract.twin_id,
block_number: current_block.saturated_into(),
});
Self::handle_grace_rent_contract(&mut contract, types::ContractState::GracePeriod(current_block))?;
}
}
_ => (),
Expand Down
30 changes: 15 additions & 15 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,12 +1213,10 @@ fn test_node_contract_billing_cycles() {
check_report_cost(1, amount_due_1, 11, discount_received);

let twin = TfgridModule::twins(twin_id).unwrap();
let usable_balance = Balances::usable_balance(&twin.account_id);
let free_balance = Balances::free_balance(&twin.account_id);
let reserved_balance = Balances::reserved_balance(&twin.account_id);

let locked_balance = free_balance - usable_balance;
assert_eq!(
locked_balance.saturated_into::<u128>(),
reserved_balance.saturated_into::<u128>(),
amount_due_1 as u128
);

Expand All @@ -1237,12 +1235,11 @@ fn test_node_contract_billing_cycles() {
check_report_cost(1, amount_due_3, 31, discount_received);

let twin = TfgridModule::twins(twin_id).unwrap();
let usable_balance = Balances::usable_balance(&twin.account_id);
let free_balance = Balances::free_balance(&twin.account_id);

let locked_balance = free_balance - usable_balance;
let reserved_balance = Balances::reserved_balance(&twin.account_id);

assert_eq!(
locked_balance.saturated_into::<u128>(),
reserved_balance.saturated_into::<u128>(),
amount_due_1 as u128 + amount_due_2 as u128 + amount_due_3 as u128
);
});
Expand Down Expand Up @@ -1316,12 +1313,12 @@ fn test_node_multiple_contract_billing_cycles() {
);

let twin = TfgridModule::twins(twin_id).unwrap();
let usable_balance = Balances::usable_balance(&twin.account_id);
let free_balance = Balances::free_balance(&twin.account_id);

let locked_balance = free_balance - usable_balance;
// let usable_balance = Balances::usable_balance(&twin.account_id);
// let free_balance = Balances::free_balance(&twin.account_id);
let reserved_balance = Balances::reserved_balance(&twin.account_id);
// let locked_balance = free_balance - usable_balance;
assert_eq!(
locked_balance.saturated_into::<u128>(),
reserved_balance.saturated_into::<u128>(),
amount_due_contract_1 as u128 + amount_due_contract_2 as u128
);
});
Expand Down Expand Up @@ -2458,6 +2455,8 @@ fn test_restore_rent_contract_and_node_contracts_in_grace_works() {
assert_eq!(c1.state, types::ContractState::GracePeriod(11));

let our_events = System::events();
log::debug!("Events: {:?}", our_events);

assert_eq!(
our_events[5],
record(MockEvent::SmartContractModule(SmartContractEvent::<
Expand Down Expand Up @@ -2524,9 +2523,10 @@ fn test_restore_rent_contract_and_node_contracts_in_grace_works() {
assert_eq!(c1.state, types::ContractState::Created);

let our_events = System::events();
log::debug!("Events: {:?}", our_events);

assert_eq!(
our_events[8],
our_events[12],
record(MockEvent::SmartContractModule(SmartContractEvent::<
TestRuntime,
>::ContractGracePeriodEnded {
Expand All @@ -2536,7 +2536,7 @@ fn test_restore_rent_contract_and_node_contracts_in_grace_works() {
}))
);
assert_eq!(
our_events[9],
our_events[13],
record(MockEvent::SmartContractModule(SmartContractEvent::<
TestRuntime,
>::ContractGracePeriodEnded {
Expand Down

0 comments on commit 150f006

Please sign in to comment.