Skip to content

Commit ee66730

Browse files
fix: properly calculate rewards to be distributed in all cases (#490)
1 parent feaaf28 commit ee66730

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

substrate-node/pallets/pallet-smart-contract/src/lib.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ pub mod pallet {
158158
pub type PalletVersion<T> = StorageValue<_, types::StorageVersion, ValueQuery>;
159159

160160
#[pallet::type_value]
161-
pub fn DefaultBillingFrequency<T: Config>() -> u64 { T::BillingFrequency::get() }
161+
pub fn DefaultBillingFrequency<T: Config>() -> u64 {
162+
T::BillingFrequency::get()
163+
}
162164

163165
#[pallet::storage]
164166
#[pallet::getter(fn billing_frequency)]
@@ -305,26 +307,26 @@ pub mod pallet {
305307
}
306308

307309
#[pallet::genesis_config]
308-
pub struct GenesisConfig {
310+
pub struct GenesisConfig {
309311
pub billing_frequency: u64,
310-
}
312+
}
311313

312314
// The default value for the genesis config type.
313-
#[cfg(feature = "std")]
314-
impl Default for GenesisConfig {
315-
fn default() -> Self {
316-
Self {
315+
#[cfg(feature = "std")]
316+
impl Default for GenesisConfig {
317+
fn default() -> Self {
318+
Self {
317319
billing_frequency: 600,
318320
}
319-
}
320-
}
321+
}
322+
}
321323

322-
#[pallet::genesis_build]
323-
impl<T: Config> GenesisBuild<T> for GenesisConfig {
324-
fn build(&self) {
324+
#[pallet::genesis_build]
325+
impl<T: Config> GenesisBuild<T> for GenesisConfig {
326+
fn build(&self) {
325327
BillingFrequency::<T>::put(self.billing_frequency);
326-
}
327-
}
328+
}
329+
}
328330

329331
#[pallet::call]
330332
impl<T: Config> Pallet<T> {
@@ -1144,14 +1146,19 @@ impl<T: Config> Pallet<T> {
11441146
WithdrawReasons::all(),
11451147
);
11461148

1149+
// Fetch twin balance, if the amount locked in the contract lock exceeds the current unlocked
1150+
// balance we can only transfer out the remaining balance
1151+
// https://github.com/threefoldtech/tfchain/issues/479
1152+
let twin_balance = Self::get_usable_balance(&twin.account_id);
1153+
11471154
// Fetch the default pricing policy
11481155
let pricing_policy = pallet_tfgrid::PricingPolicies::<T>::get(1)
11491156
.ok_or(Error::<T>::PricingPolicyNotExists)?;
11501157
// Distribute cultivation rewards
11511158
match Self::_distribute_cultivation_rewards(
11521159
&contract,
11531160
&pricing_policy,
1154-
contract_lock.amount_locked,
1161+
twin_balance.min(contract_lock.amount_locked),
11551162
) {
11561163
Ok(_) => (),
11571164
Err(err) => log::error!("error while distributing cultivation rewards {:?}", err),

substrate-node/pallets/pallet-smart-contract/src/tests.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ fn test_node_contract_grace_period_cancels_contract_when_grace_period_ends_works
13321332
prepare_farm_and_node();
13331333
run_to_block(1);
13341334
TFTPriceModule::set_prices(Origin::signed(bob()), 50, 101).unwrap();
1335+
let initial_total_issuance = Balances::total_issuance();
13351336

13361337
assert_ok!(SmartContractModule::create_node_contract(
13371338
Origin::signed(charlie()),
@@ -1367,6 +1368,9 @@ fn test_node_contract_grace_period_cancels_contract_when_grace_period_ends_works
13671368
true
13681369
);
13691370

1371+
let twin = TfgridModule::twins(3).unwrap();
1372+
let free_balance = Balances::free_balance(&twin.account_id);
1373+
13701374
run_to_block(32);
13711375
run_to_block(42);
13721376
run_to_block(52);
@@ -1379,6 +1383,9 @@ fn test_node_contract_grace_period_cancels_contract_when_grace_period_ends_works
13791383
run_to_block(122);
13801384
run_to_block(132);
13811385

1386+
// The user's total free balance should be distributed
1387+
validate_distribution_rewards(initial_total_issuance, free_balance, false);
1388+
13821389
let c1 = SmartContractModule::contracts(1);
13831390
assert_eq!(c1, None);
13841391
});
@@ -1637,16 +1644,7 @@ fn test_rent_contract_canceled_due_to_out_of_funds_should_cancel_node_contracts_
16371644
// check_report_cost(1, 3, amount_due_as_u128, 12, discount_received);
16381645

16391646
let our_events = System::events();
1640-
// Event 1: Rent contract created
1641-
// Event 2: Node Contract created
1642-
// Event 3: Updated used resources
1643-
// Event 4: Grace period started rent contract
1644-
// Event 5: Grace period started node contract
1645-
// Event 6-17: Rent contract billed
1646-
// Event 18: Node contract canceled
1647-
// Event 19: Rent contract Canceled
1648-
// => no Node Contract billed event
1649-
assert_eq!(our_events.len(), 20);
1647+
assert_eq!(our_events.len(), 31);
16501648

16511649
assert_eq!(
16521650
our_events[5],
@@ -1672,7 +1670,7 @@ fn test_rent_contract_canceled_due_to_out_of_funds_should_cancel_node_contracts_
16721670
);
16731671

16741672
assert_eq!(
1675-
our_events[18],
1673+
our_events[29],
16761674
record(MockEvent::SmartContractModule(SmartContractEvent::<
16771675
TestRuntime,
16781676
>::NodeContractCanceled {
@@ -1682,7 +1680,7 @@ fn test_rent_contract_canceled_due_to_out_of_funds_should_cancel_node_contracts_
16821680
}))
16831681
);
16841682
assert_eq!(
1685-
our_events[19],
1683+
our_events[30],
16861684
record(MockEvent::SmartContractModule(SmartContractEvent::<
16871685
TestRuntime,
16881686
>::RentContractCanceled {

0 commit comments

Comments
 (0)