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 3 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
39 changes: 17 additions & 22 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
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);
}

// Calculate amount of seconds elapsed based on the contract lock struct
let mut contract_lock = ContractLock::<T>::get(contract.contract_id);
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 @@ -1155,8 +1149,18 @@ impl<T: Config> Pallet<T> {
return Ok(().into());
};

let total_lock_amount = contract_lock.amount_locked + amount_due;
// Handle grace
let contract = Self::handle_grace(&mut contract, usable_balance, amount_due)?;
let contract = Self::handle_grace(&mut contract, usable_balance, total_lock_amount)?;

// Only update contract lock in state (Created, GracePeriod)
if !matches!(contract.state, types::ContractState::Deleted(_)) {
// increment cycles billed and update the internal lock struct
contract_lock.lock_updated = now;
contract_lock.cycles += 1;
contract_lock.amount_locked = total_lock_amount;
ContractLock::<T>::insert(contract.contract_id, &contract_lock);
}

// If still in grace period, no need to continue doing locking and other stuff
if matches!(contract.state, types::ContractState::GracePeriod(_)) {
Expand All @@ -1165,7 +1169,7 @@ impl<T: Config> Pallet<T> {
}

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

// Always emit a contract billed event
let contract_bill = types::ContractBill {
Expand Down Expand Up @@ -1297,20 +1301,11 @@ impl<T: Config> Pallet<T> {

fn handle_lock(
contract: &mut types::Contract<T>,
mut contract_lock: types::ContractLock<BalanceOf<T>>,
DylanVerstraete marked this conversation as resolved.
Show resolved Hide resolved
amount_due: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let now = <timestamp::Pallet<T>>::get().saturated_into::<u64>() / 1000;

let mut contract_lock = ContractLock::<T>::get(contract.contract_id);
// Only update contract lock in state (Created, GracePeriod)
if !matches!(contract.state, types::ContractState::Deleted(_)) {
// increment cycles billed and update the internal lock struct
contract_lock.lock_updated = now;
contract_lock.cycles += 1;
contract_lock.amount_locked = contract_lock.amount_locked + amount_due;
ContractLock::<T>::insert(contract.contract_id, &contract_lock);
}

// Only lock an amount from the user's balance if the contract is in create state
// The lock is specified on the user's account, since a user can have multiple contracts
// Just extend the lock with the amount due for this contract billing period (lock will be created if not exists)
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