diff --git a/substrate-node/pallets/pallet-smart-contract/src/lib.rs b/substrate-node/pallets/pallet-smart-contract/src/lib.rs index fefe01de4..4e9bc4065 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/lib.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/lib.rs @@ -802,11 +802,19 @@ impl Module { // We know the contract is using resources, now calculate the cost for each used resource let node_contract_resources = NodeContractResources::get(contract.contract_id); + let mut bill_resources = true; + // If this node contract is deployed on a node which has a rent contract + // We can ignore billing for the resources used by this node contract + if ActiveRentContractForNode::contains_key(node_contract.node_id) { + bill_resources = false + } + let contract_cost = Self::calculate_resources_cost( node_contract_resources.used, node_contract.public_ips, seconds_elapsed, pricing_policy.clone(), + bill_resources ); contract_cost + contract_billing_info.amount_unbilled } @@ -821,6 +829,7 @@ impl Module { 0, seconds_elapsed, pricing_policy.clone(), + true ); Percent::from_percent(pricing_policy.discount_for_dedication_nodes) * contract_cost } @@ -842,29 +851,32 @@ impl Module { ipu: u32, seconds_elapsed: u64, pricing_policy: pallet_tfgrid_types::PricingPolicy, + bill_resources: bool ) -> u64 { - let hru = U64F64::from_num(resources.hru) / pricing_policy.su.factor(); - let sru = U64F64::from_num(resources.sru) / pricing_policy.su.factor(); - let mru = U64F64::from_num(resources.mru) / pricing_policy.cu.factor(); - let cru = U64F64::from_num(resources.cru); - - let su_used = hru / 1200 + sru / 200; - // the pricing policy su cost value is expressed in 1 hours or 3600 seconds. - // we bill every 3600 seconds but here we need to calculate the cost per second and multiply it by the seconds elapsed. - let su_cost = (U64F64::from_num(pricing_policy.su.value) / 3600) - * U64F64::from_num(seconds_elapsed) - * su_used; - debug::info!("su cost: {:?}", su_cost); - - let cu = Self::calculate_cu(cru, mru); - - let cu_cost = (U64F64::from_num(pricing_policy.cu.value) / 3600) - * U64F64::from_num(seconds_elapsed) - * cu; - debug::info!("cu cost: {:?}", cu_cost); - - // save total - let mut total_cost = su_cost + cu_cost; + let mut total_cost = U64F64::from_num(0); + + if bill_resources { + let hru = U64F64::from_num(resources.hru) / pricing_policy.su.factor(); + let sru = U64F64::from_num(resources.sru) / pricing_policy.su.factor(); + let mru = U64F64::from_num(resources.mru) / pricing_policy.cu.factor(); + let cru = U64F64::from_num(resources.cru); + + let su_used = hru / 1200 + sru / 200; + // the pricing policy su cost value is expressed in 1 hours or 3600 seconds. + // we bill every 3600 seconds but here we need to calculate the cost per second and multiply it by the seconds elapsed. + let su_cost = (U64F64::from_num(pricing_policy.su.value) / 3600) + * U64F64::from_num(seconds_elapsed) + * su_used; + debug::info!("su cost: {:?}", su_cost); + + let cu = Self::calculate_cu(cru, mru); + + let cu_cost = (U64F64::from_num(pricing_policy.cu.value) / 3600) + * U64F64::from_num(seconds_elapsed) + * cu; + debug::info!("cu cost: {:?}", cu_cost); + total_cost = su_cost + cu_cost; + } if ipu > 0 { let total_ip_cost = U64F64::from_num(ipu) diff --git a/substrate-node/pallets/pallet-smart-contract/src/tests.rs b/substrate-node/pallets/pallet-smart-contract/src/tests.rs index 2f1e7c7e1..d50d0b647 100644 --- a/substrate-node/pallets/pallet-smart-contract/src/tests.rs +++ b/substrate-node/pallets/pallet-smart-contract/src/tests.rs @@ -1130,7 +1130,7 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi Origin::signed(bob()), node_id )); - + assert_ok!(SmartContractModule::create_node_contract( Origin::signed(bob()), 1, @@ -1138,12 +1138,13 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi "hash".as_bytes().to_vec(), 0 )); + push_contract_resources_used(2); run_to_block(12); let (amount_due_as_u128, discount_received) = calculate_tft_cost(1, 2, 11); assert_ne!(amount_due_as_u128, 0); - check_report_cost(1, 2, amount_due_as_u128, 12, discount_received); + check_report_cost(1, 3, amount_due_as_u128, 12, discount_received); let our_events = System::events() .into_iter() @@ -1164,7 +1165,7 @@ fn test_create_rent_contract_and_node_contract_excludes_node_contract_from_billi // Event 2: Node Contract created // Event 4: Rent contract billed // => no Node Contract billed event - assert_eq!(our_events.len(), 3); + assert_eq!(our_events.len(), 4); }); }