Skip to content

Commit bec4591

Browse files
authored
Revert capacity planning power management (#551) (#554)
1 parent 22bd184 commit bec4591

File tree

28 files changed

+1354
-4740
lines changed

28 files changed

+1354
-4740
lines changed

substrate-node/pallets/pallet-dao/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,10 @@ impl<T: Config> Pallet<T> {
507507

508508
impl<T: Config> ChangeNode<LocationOf<T>, InterfaceOf<T>, SerialNumberOf<T>> for Pallet<T> {
509509
fn node_changed(old_node: Option<&TfgridNode<T>>, new_node: &TfgridNode<T>) {
510-
let new_node_weight = new_node.resources.total_resources.get_node_weight();
510+
let new_node_weight = new_node.resources.get_node_weight();
511511
match old_node {
512512
Some(node) => {
513-
let old_node_weight = node.resources.total_resources.get_node_weight();
513+
let old_node_weight = node.resources.get_node_weight();
514514

515515
if node.farm_id != new_node.farm_id {
516516
let mut old_farm_weight = FarmWeight::<T>::get(node.farm_id);
@@ -538,7 +538,7 @@ impl<T: Config> ChangeNode<LocationOf<T>, InterfaceOf<T>, SerialNumberOf<T>> for
538538
}
539539

540540
fn node_deleted(node: &TfgridNode<T>) {
541-
let node_weight = node.resources.total_resources.get_node_weight();
541+
let node_weight = node.resources.get_node_weight();
542542
let mut farm_weight = FarmWeight::<T>::get(node.farm_id);
543543
farm_weight = farm_weight.checked_sub(node_weight).unwrap_or(0);
544544
FarmWeight::<T>::insert(node.farm_id, farm_weight);

substrate-node/pallets/pallet-dao/src/tests.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn close_works() {
171171
);
172172

173173
assert_eq!(
174-
e[9],
174+
e[8],
175175
record(MockEvent::DaoModule(DaoEvent::Voted {
176176
account: 10,
177177
proposal_hash: hash,
@@ -182,7 +182,7 @@ fn close_works() {
182182
);
183183

184184
assert_eq!(
185-
e[14],
185+
e[12],
186186
record(MockEvent::DaoModule(DaoEvent::Voted {
187187
account: 11,
188188
proposal_hash: hash,
@@ -197,7 +197,7 @@ fn close_works() {
197197
let total_weight = farm_1_weight + farm_2_weight;
198198

199199
assert_eq!(
200-
e[15],
200+
e[13],
201201
record(MockEvent::DaoModule(DaoEvent::Closed {
202202
proposal_hash: hash,
203203
yes: 2,
@@ -208,7 +208,7 @@ fn close_works() {
208208
);
209209

210210
assert_eq!(
211-
e[16],
211+
e[14],
212212
record(MockEvent::DaoModule(DaoEvent::Approved {
213213
proposal_hash: hash,
214214
}))
@@ -313,7 +313,7 @@ fn motion_approval_works() {
313313
);
314314

315315
assert_eq!(
316-
e[9],
316+
e[8],
317317
record(MockEvent::DaoModule(DaoEvent::Voted {
318318
account: 10,
319319
proposal_hash: hash,
@@ -324,7 +324,7 @@ fn motion_approval_works() {
324324
);
325325

326326
assert_eq!(
327-
e[14],
327+
e[12],
328328
record(MockEvent::DaoModule(DaoEvent::Voted {
329329
account: 11,
330330
proposal_hash: hash,
@@ -339,7 +339,7 @@ fn motion_approval_works() {
339339
let total_weight = farm_1_weight + farm_2_weight;
340340

341341
assert_eq!(
342-
e[15],
342+
e[13],
343343
record(MockEvent::DaoModule(DaoEvent::Closed {
344344
proposal_hash: hash,
345345
yes: 2,
@@ -350,14 +350,14 @@ fn motion_approval_works() {
350350
);
351351

352352
assert_eq!(
353-
e[16],
353+
e[14],
354354
record(MockEvent::DaoModule(DaoEvent::Approved {
355355
proposal_hash: hash,
356356
}))
357357
);
358358

359359
assert_eq!(
360-
e[18],
360+
e[16],
361361
record(MockEvent::DaoModule(DaoEvent::Executed {
362362
proposal_hash: hash,
363363
result: Ok(())
@@ -524,7 +524,7 @@ fn weighted_voting_works() {
524524
);
525525

526526
assert_eq!(
527-
e[9],
527+
e[8],
528528
record(MockEvent::DaoModule(DaoEvent::Voted {
529529
account: 10,
530530
proposal_hash: hash,
@@ -535,7 +535,7 @@ fn weighted_voting_works() {
535535
);
536536

537537
assert_eq!(
538-
e[14],
538+
e[12],
539539
record(MockEvent::DaoModule(DaoEvent::Voted {
540540
account: 11,
541541
proposal_hash: hash,
@@ -549,7 +549,7 @@ fn weighted_voting_works() {
549549
let farm_2_weight = DaoModule::get_vote_weight(2).unwrap();
550550

551551
assert_eq!(
552-
e[15],
552+
e[13],
553553
record(MockEvent::DaoModule(DaoEvent::Closed {
554554
proposal_hash: hash,
555555
yes: 1,
@@ -562,7 +562,7 @@ fn weighted_voting_works() {
562562
// Outcome should be negative since the 2nd farmer which has more weight because he
563563
// has more stake in the network voted no
564564
assert_eq!(
565-
e[16],
565+
e[14],
566566
record(MockEvent::DaoModule(DaoEvent::Disapproved {
567567
proposal_hash: hash,
568568
}))
@@ -622,7 +622,7 @@ fn voting_tfgridmodule_call_works() {
622622
);
623623

624624
assert_eq!(
625-
e[9],
625+
e[8],
626626
record(MockEvent::DaoModule(DaoEvent::Voted {
627627
account: 10,
628628
proposal_hash: hash,
@@ -633,7 +633,7 @@ fn voting_tfgridmodule_call_works() {
633633
);
634634

635635
assert_eq!(
636-
e[14],
636+
e[12],
637637
record(MockEvent::DaoModule(DaoEvent::Voted {
638638
account: 11,
639639
proposal_hash: hash,
@@ -648,7 +648,7 @@ fn voting_tfgridmodule_call_works() {
648648
let total_weight = farm_1_weight + farm_2_weight;
649649

650650
assert_eq!(
651-
e[15],
651+
e[13],
652652
record(MockEvent::DaoModule(DaoEvent::Closed {
653653
proposal_hash: hash,
654654
yes: 2,
@@ -659,14 +659,14 @@ fn voting_tfgridmodule_call_works() {
659659
);
660660

661661
assert_eq!(
662-
e[16],
662+
e[14],
663663
record(MockEvent::DaoModule(DaoEvent::Approved {
664664
proposal_hash: hash,
665665
}))
666666
);
667667

668668
assert_eq!(
669-
e[18],
669+
e[16],
670670
record(MockEvent::DaoModule(DaoEvent::Executed {
671671
proposal_hash: hash,
672672
result: Ok(())

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

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,52 @@ impl<T: Config> Contract<T> {
5353
seconds_elapsed: u64,
5454
) -> Result<u64, DispatchErrorWithPostInfo> {
5555
let total_cost = match &self.contract_type {
56-
types::ContractData::CapacityReservationContract(capacity_reservation_contract) => {
56+
// Calculate total cost for a node contract
57+
types::ContractData::NodeContract(node_contract) => {
5758
// Get the contract billing info to view the amount unbilled for NRU (network resource units)
5859
let contract_billing_info = self.get_billing_info();
5960
// Get the node
60-
let node = pallet_tfgrid::Nodes::<T>::get(capacity_reservation_contract.node_id)
61-
.ok_or(Error::<T>::NodeNotExists)?;
61+
if !pallet_tfgrid::Nodes::<T>::contains_key(node_contract.node_id) {
62+
return Err(DispatchErrorWithPostInfo::from(Error::<T>::NodeNotExists));
63+
}
64+
65+
// We know the contract is using resources, now calculate the cost for each used resource
66+
67+
let node_contract_resources =
68+
pallet::Pallet::<T>::node_contract_resources(self.contract_id);
69+
70+
let mut bill_resources = true;
71+
// If this node contract is deployed on a node which has a rent contract
72+
// We can ignore billing for the resources used by this node contract
73+
if pallet::ActiveRentContractForNode::<T>::contains_key(node_contract.node_id) {
74+
bill_resources = false
75+
}
6276

6377
let contract_cost = calculate_resources_cost::<T>(
64-
&capacity_reservation_contract.resources.total_resources,
65-
capacity_reservation_contract.public_ips,
78+
node_contract_resources.used,
79+
node_contract.public_ips,
6680
seconds_elapsed,
6781
&pricing_policy,
82+
bill_resources,
6883
);
69-
if node.resources.total_resources
70-
== capacity_reservation_contract.resources.total_resources
71-
{
72-
Percent::from_percent(pricing_policy.discount_for_dedication_nodes)
73-
* contract_cost
74-
+ contract_billing_info.amount_unbilled
75-
} else {
76-
contract_cost + contract_billing_info.amount_unbilled
84+
contract_cost + contract_billing_info.amount_unbilled
85+
}
86+
types::ContractData::RentContract(rent_contract) => {
87+
if !pallet_tfgrid::Nodes::<T>::contains_key(rent_contract.node_id) {
88+
return Err(DispatchErrorWithPostInfo::from(Error::<T>::NodeNotExists));
7789
}
90+
let node = pallet_tfgrid::Nodes::<T>::get(rent_contract.node_id).unwrap();
91+
92+
let contract_cost = calculate_resources_cost::<T>(
93+
node.resources,
94+
0,
95+
seconds_elapsed,
96+
&pricing_policy,
97+
true,
98+
);
99+
Percent::from_percent(pricing_policy.discount_for_dedication_nodes) * contract_cost
78100
}
101+
// Calculate total cost for a name contract
79102
types::ContractData::NameContract(_) => {
80103
// bill user for name usage for number of seconds elapsed
81104
let total_cost_u64f64 = (U64F64::from_num(pricing_policy.unique_name.value) / 3600)
@@ -90,30 +113,36 @@ impl<T: Config> Contract<T> {
90113

91114
// Calculates the total cost of a node contract.
92115
pub fn calculate_resources_cost<T: Config>(
93-
resources: &Resources,
116+
resources: Resources,
94117
ipu: u32,
95118
seconds_elapsed: u64,
96119
pricing_policy: &pallet_tfgrid_types::PricingPolicy<T::AccountId>,
120+
bill_resources: bool,
97121
) -> u64 {
98-
let hru = U64F64::from_num(resources.hru) / pricing_policy.su.factor_base_1024();
99-
let sru = U64F64::from_num(resources.sru) / pricing_policy.su.factor_base_1024();
100-
let mru = U64F64::from_num(resources.mru) / pricing_policy.cu.factor_base_1024();
101-
let cru = U64F64::from_num(resources.cru);
102-
103-
let su_used = hru / 1200 + sru / 200;
104-
// the pricing policy su cost value is expressed in 1 hours or 3600 seconds.
105-
// we bill every 3600 seconds but here we need to calculate the cost per second and multiply it by the seconds elapsed.
106-
let su_cost = (U64F64::from_num(pricing_policy.su.value) / 3600)
107-
* U64F64::from_num(seconds_elapsed)
108-
* su_used;
109-
log::debug!("su cost: {:?}", su_cost);
110-
111-
let cu = calculate_cu(cru, mru);
112-
113-
let cu_cost =
114-
(U64F64::from_num(pricing_policy.cu.value) / 3600) * U64F64::from_num(seconds_elapsed) * cu;
115-
log::debug!("cu cost: {:?}", cu_cost);
116-
let mut total_cost = su_cost + cu_cost;
122+
let mut total_cost = U64F64::from_num(0);
123+
124+
if bill_resources {
125+
let hru = U64F64::from_num(resources.hru) / pricing_policy.su.factor_base_1024();
126+
let sru = U64F64::from_num(resources.sru) / pricing_policy.su.factor_base_1024();
127+
let mru = U64F64::from_num(resources.mru) / pricing_policy.cu.factor_base_1024();
128+
let cru = U64F64::from_num(resources.cru);
129+
130+
let su_used = hru / 1200 + sru / 200;
131+
// the pricing policy su cost value is expressed in 1 hours or 3600 seconds.
132+
// we bill every 3600 seconds but here we need to calculate the cost per second and multiply it by the seconds elapsed.
133+
let su_cost = (U64F64::from_num(pricing_policy.su.value) / 3600)
134+
* U64F64::from_num(seconds_elapsed)
135+
* su_used;
136+
log::debug!("su cost: {:?}", su_cost);
137+
138+
let cu = calculate_cu(cru, mru);
139+
140+
let cu_cost = (U64F64::from_num(pricing_policy.cu.value) / 3600)
141+
* U64F64::from_num(seconds_elapsed)
142+
* cu;
143+
log::debug!("cu cost: {:?}", cu_cost);
144+
total_cost = su_cost + cu_cost;
145+
}
117146

118147
if ipu > 0 {
119148
let total_ip_cost = U64F64::from_num(ipu)

0 commit comments

Comments
 (0)