Skip to content

Commit

Permalink
fix: exclude node contract resources billing if rent contract (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanVerstraete authored May 4, 2022
1 parent bb155f5 commit f43002c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
56 changes: 34 additions & 22 deletions substrate-node/pallets/pallet-smart-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,19 @@ impl<T: Config> Module<T> {
// 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
}
Expand All @@ -821,6 +829,7 @@ impl<T: Config> Module<T> {
0,
seconds_elapsed,
pricing_policy.clone(),
true
);
Percent::from_percent(pricing_policy.discount_for_dedication_nodes) * contract_cost
}
Expand All @@ -842,29 +851,32 @@ impl<T: Config> Module<T> {
ipu: u32,
seconds_elapsed: u64,
pricing_policy: pallet_tfgrid_types::PricingPolicy<T::AccountId>,
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)
Expand Down
7 changes: 4 additions & 3 deletions substrate-node/pallets/pallet-smart-contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,20 +1130,21 @@ 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,
"some_data".as_bytes().to_vec(),
"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()
Expand All @@ -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);
});
}

Expand Down

0 comments on commit f43002c

Please sign in to comment.