During the grace period, the NU consumption may be incorrectly accumulated because:
The NU consumption is read and added to the contract's amount due, but the unbilled consumption is not reset.
If the contract enters the grace period, the early return prevents the ContractBillingInformation from being reset.
As a result, the same NU consumption can be added to the contract's due amount repeatedly in each billing cycle during the grace period, leading to overbilling.
During the grace period, the NU consumption may be incorrectly accumulated because:
Here are the Details
1. NU Consumption Accumulation
Here, we call
calculate_contract_cost_tft()
https://github.com/threefoldtech/tfchain/blob/622e7d255b9c76203fba8a316b84b280b59fbba7/substrate-node/pallets/pallet-smart-contract/src/billing.rs#L132-L133This function calculates the contract cost, then read the NU consumption and adding it to the contract's amount due for the current billing cycle. https://github.com/threefoldtech/tfchain/blob/622e7d255b9c76203fba8a316b84b280b59fbba7/substrate-node/pallets/pallet-smart-contract/src/cost.rs#L88
2. Early Return During Grace Period
If the contract enters a grace period, the function returns early after adding the amount due to the contract lock. However, the ContractBillingInformation (which holds the unbilled NU consumption) is not reset at this point. https://github.com/threefoldtech/tfchain/blob/622e7d255b9c76203fba8a316b84b280b59fbba7/substrate-node/pallets/pallet-smart-contract/src/billing.rs#L177-L182
3. Resetting Unbilled NU Consumption
The unbilled NU consumption is reset in the
bill_contract()
function, but this reset occurs after the grace period condition has already triggered an early return in the flow. Therefore, the unbilled NU consumption is not reset if the contract is in the grace period, causing the same NU consumption to be added again in the next cycle. https://github.com/threefoldtech/tfchain/blob/622e7d255b9c76203fba8a316b84b280b59fbba7/substrate-node/pallets/pallet-smart-contract/src/billing.rs#L201-L210