Skip to content

Commit f43002c

Browse files
fix: exclude node contract resources billing if rent contract (#291)
1 parent bb155f5 commit f43002c

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,19 @@ impl<T: Config> Module<T> {
802802
// We know the contract is using resources, now calculate the cost for each used resource
803803
let node_contract_resources = NodeContractResources::get(contract.contract_id);
804804

805+
let mut bill_resources = true;
806+
// If this node contract is deployed on a node which has a rent contract
807+
// We can ignore billing for the resources used by this node contract
808+
if ActiveRentContractForNode::contains_key(node_contract.node_id) {
809+
bill_resources = false
810+
}
811+
805812
let contract_cost = Self::calculate_resources_cost(
806813
node_contract_resources.used,
807814
node_contract.public_ips,
808815
seconds_elapsed,
809816
pricing_policy.clone(),
817+
bill_resources
810818
);
811819
contract_cost + contract_billing_info.amount_unbilled
812820
}
@@ -821,6 +829,7 @@ impl<T: Config> Module<T> {
821829
0,
822830
seconds_elapsed,
823831
pricing_policy.clone(),
832+
true
824833
);
825834
Percent::from_percent(pricing_policy.discount_for_dedication_nodes) * contract_cost
826835
}
@@ -842,29 +851,32 @@ impl<T: Config> Module<T> {
842851
ipu: u32,
843852
seconds_elapsed: u64,
844853
pricing_policy: pallet_tfgrid_types::PricingPolicy<T::AccountId>,
854+
bill_resources: bool
845855
) -> u64 {
846-
let hru = U64F64::from_num(resources.hru) / pricing_policy.su.factor();
847-
let sru = U64F64::from_num(resources.sru) / pricing_policy.su.factor();
848-
let mru = U64F64::from_num(resources.mru) / pricing_policy.cu.factor();
849-
let cru = U64F64::from_num(resources.cru);
850-
851-
let su_used = hru / 1200 + sru / 200;
852-
// the pricing policy su cost value is expressed in 1 hours or 3600 seconds.
853-
// we bill every 3600 seconds but here we need to calculate the cost per second and multiply it by the seconds elapsed.
854-
let su_cost = (U64F64::from_num(pricing_policy.su.value) / 3600)
855-
* U64F64::from_num(seconds_elapsed)
856-
* su_used;
857-
debug::info!("su cost: {:?}", su_cost);
858-
859-
let cu = Self::calculate_cu(cru, mru);
860-
861-
let cu_cost = (U64F64::from_num(pricing_policy.cu.value) / 3600)
862-
* U64F64::from_num(seconds_elapsed)
863-
* cu;
864-
debug::info!("cu cost: {:?}", cu_cost);
865-
866-
// save total
867-
let mut total_cost = su_cost + cu_cost;
856+
let mut total_cost = U64F64::from_num(0);
857+
858+
if bill_resources {
859+
let hru = U64F64::from_num(resources.hru) / pricing_policy.su.factor();
860+
let sru = U64F64::from_num(resources.sru) / pricing_policy.su.factor();
861+
let mru = U64F64::from_num(resources.mru) / pricing_policy.cu.factor();
862+
let cru = U64F64::from_num(resources.cru);
863+
864+
let su_used = hru / 1200 + sru / 200;
865+
// the pricing policy su cost value is expressed in 1 hours or 3600 seconds.
866+
// we bill every 3600 seconds but here we need to calculate the cost per second and multiply it by the seconds elapsed.
867+
let su_cost = (U64F64::from_num(pricing_policy.su.value) / 3600)
868+
* U64F64::from_num(seconds_elapsed)
869+
* su_used;
870+
debug::info!("su cost: {:?}", su_cost);
871+
872+
let cu = Self::calculate_cu(cru, mru);
873+
874+
let cu_cost = (U64F64::from_num(pricing_policy.cu.value) / 3600)
875+
* U64F64::from_num(seconds_elapsed)
876+
* cu;
877+
debug::info!("cu cost: {:?}", cu_cost);
878+
total_cost = su_cost + cu_cost;
879+
}
868880

869881
if ipu > 0 {
870882
let total_ip_cost = U64F64::from_num(ipu)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,20 +1130,21 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi
11301130
Origin::signed(bob()),
11311131
node_id
11321132
));
1133-
1133+
11341134
assert_ok!(SmartContractModule::create_node_contract(
11351135
Origin::signed(bob()),
11361136
1,
11371137
"some_data".as_bytes().to_vec(),
11381138
"hash".as_bytes().to_vec(),
11391139
0
11401140
));
1141+
push_contract_resources_used(2);
11411142

11421143
run_to_block(12);
11431144

11441145
let (amount_due_as_u128, discount_received) = calculate_tft_cost(1, 2, 11);
11451146
assert_ne!(amount_due_as_u128, 0);
1146-
check_report_cost(1, 2, amount_due_as_u128, 12, discount_received);
1147+
check_report_cost(1, 3, amount_due_as_u128, 12, discount_received);
11471148

11481149
let our_events = System::events()
11491150
.into_iter()
@@ -1164,7 +1165,7 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi
11641165
// Event 2: Node Contract created
11651166
// Event 4: Rent contract billed
11661167
// => no Node Contract billed event
1167-
assert_eq!(our_events.len(), 3);
1168+
assert_eq!(our_events.len(), 4);
11681169
});
11691170
}
11701171

0 commit comments

Comments
 (0)