Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: save graced contracts total amount due #631

Merged
merged 5 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions substrate-node/pallets/pallet-smart-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,17 +1132,11 @@ impl<T: Config> Pallet<T> {
pallet_tfgrid::Twins::<T>::get(contract.twin_id).ok_or(Error::<T>::TwinNotExists)?;
let usable_balance = Self::get_usable_balance(&twin.account_id);

let mut seconds_elapsed = BillingFrequency::<T>::get() * 6;
// Calculate amount of seconds elapsed based on the contract lock struct

let now = <timestamp::Pallet<T>>::get().saturated_into::<u64>() / 1000;
// this will set the seconds elapsed to the default billing cycle duration in seconds
// if there is no contract lock object yet. A contract lock object will be created later in this function
// https://github.com/threefoldtech/tfchain/issues/261

// Calculate amount of seconds elapsed based on the contract lock struct
let contract_lock = ContractLock::<T>::get(contract.contract_id);
if contract_lock.lock_updated != 0 {
seconds_elapsed = now.checked_sub(contract_lock.lock_updated).unwrap_or(0);
}
let seconds_elapsed = now.checked_sub(contract_lock.lock_updated).unwrap_or(0);

let (amount_due, discount_received) =
contract.calculate_contract_cost_tft(usable_balance, seconds_elapsed)?;
Expand All @@ -1156,17 +1150,21 @@ impl<T: Config> Pallet<T> {
};

// Handle grace
let contract = Self::handle_grace(&mut contract, usable_balance, amount_due)?;
let contract = Self::handle_grace(
&mut contract,
usable_balance,
contract_lock.amount_locked + amount_due,
DylanVerstraete marked this conversation as resolved.
Show resolved Hide resolved
)?;

// Handle contract lock operations
Self::handle_lock(contract, amount_due)?;
DylanVerstraete marked this conversation as resolved.
Show resolved Hide resolved

// If still in grace period, no need to continue doing locking and other stuff
if matches!(contract.state, types::ContractState::GracePeriod(_)) {
log::debug!("contract {} is still in grace", contract.contract_id);
return Ok(().into());
}

// Handle contract lock operations
Self::handle_lock(contract, amount_due)?;

// Always emit a contract billed event
let contract_bill = types::ContractBill {
contract_id: contract.contract_id,
Expand Down
15 changes: 10 additions & 5 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,11 @@ fn test_rent_contract_canceled_due_to_out_of_funds_should_cancel_node_contracts_
// check_report_cost(1, 3, amount_due_as_u128, 12, discount_received);

let our_events = System::events();
assert_eq!(our_events.len(), 10);
assert_eq!(our_events.len(), 21);

for e in our_events.clone() {
log::info!("event: {:?}", e);
}

assert_eq!(
our_events[5],
Expand All @@ -1855,7 +1859,7 @@ fn test_rent_contract_canceled_due_to_out_of_funds_should_cancel_node_contracts_
);

assert_eq!(
our_events[8],
our_events[19],
record(MockEvent::SmartContractModule(SmartContractEvent::<
TestRuntime,
>::NodeContractCanceled {
Expand All @@ -1865,7 +1869,7 @@ fn test_rent_contract_canceled_due_to_out_of_funds_should_cancel_node_contracts_
}))
);
assert_eq!(
our_events[9],
our_events[20],
record(MockEvent::SmartContractModule(SmartContractEvent::<
TestRuntime,
>::RentContractCanceled {
Expand Down Expand Up @@ -3216,9 +3220,10 @@ fn validate_distribution_rewards(
} else {
// 50% is sent to the sales account
let sales_account_balance = Balances::free_balance(&pricing_policy.certified_sales_account);
assert_eq!(
assert_eq_error_rate!(
sales_account_balance,
Perbill::from_percent(50) * total_amount_billed
Perbill::from_percent(50) * total_amount_billed,
1
);
}

Expand Down