Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclude node contract resources billing if rent contract is depl… #291

Merged
merged 2 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 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,6 +851,7 @@ 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();
Expand All @@ -863,8 +873,10 @@ impl<T: Config> Module<T> {
* 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 {
total_cost = su_cost + cu_cost;
}
DylanVerstraete marked this conversation as resolved.
Show resolved Hide resolved

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