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

the farm weight calculation #968

Closed
A-Harby opened this issue Apr 30, 2024 · 3 comments
Closed

the farm weight calculation #968

A-Harby opened this issue Apr 30, 2024 · 3 comments
Assignees

Comments

@A-Harby
Copy link

A-Harby commented Apr 30, 2024

Describe the bug

Is the farm weight calculated correctly?

For example, I'll calculate the weight for farm 1 in QAnet:

Farm 1 in QAnet has only 2 nodes, and both are up and have the same resource as shown below.
image

To calculate the CU and SU for the node, I will use the equation mentioned here.

image
Node resources is

  • HRU: 8383.559944153
  • SRU: 476.939781189
  • CRU: 24
  • MRU: 188.875312805

CU = min( max(MRU/4, CRU/2), max(MRU/8, CRU), max(MRU/2, CRU/4) )
CU = min(max(188.875312805/4, 24/2), max(188.875312805/8, 24), max(188.875312805/2, 24/4))
CU = min(max(47.21882820125, 12), max(23.609414100625, 24), max(94.4376564025, 6))
CU = min (47.21882820125, 24, 94.4376564025)
CU = 24

SU = HRU/1200 + SRU/200
SU = 8383.559944153/1200 + 476.939781189/200
SU = 6.986299953461 + 2.384698905945
SU = 9.370998859406

The equation for the weight of a farm is: 2 * (sum of CU of all nodes) + (sum of SU of all nodes).
since both nodes have exactly the same resources,
Weight = 2 * (sum of CU of all nodes) + (sum of SU of all nodes)
Weight = 2 * (24+24) + (9.370998859406+9.370998859406)
Weight = 2 * 48 + 18.741997719
Weight = 114.741997719

So the weight for Farm 1 should be 114, but in the chain it's shown as 88.
image
So is the wrong calculation from me, the equation am I using, or the chain?

@renauter renauter self-assigned this Apr 30, 2024
@renauter
Copy link
Collaborator

So let s do the maths for this guy node_id = 3 and farm_id = 1 on QAnet:

image

Here is the rust code that calculates node weight extracted from TFChain (

pub fn get_cu(&self) -> u64 {
let cu = self.calc_cu();
let calculated_cu = 2 * (cu as u128 / GIGABYTE / ONE_THOUSAND);
calculated_cu as u64
}
fn calc_cu(&self) -> u64 {
let cru_min = self.cru as u128 * 2 * GIGABYTE * ONE_THOUSAND;
let mru_min =
((self.mru as u128).checked_sub(1).unwrap_or(0) * GIGABYTE) * ONE_THOUSAND / 4;
let sru_min = self.sru as u128 * ONE_THOUSAND / 50;
if cru_min < mru_min && cru_min < sru_min {
cru_min as u64
} else if mru_min < cru_min && mru_min < sru_min {
mru_min as u64
} else if sru_min < cru_min && sru_min < mru_min {
sru_min as u64
} else {
0
}
}
pub fn get_su(&self) -> u64 {
let su = self.hru as u128 * ONE_THOUSAND / 1200 + self.sru as u128 * ONE_THOUSAND / 250;
let calculated_su = su / GIGABYTE;
let result = calculated_su as u128 / ONE_THOUSAND;
result as u64
}
pub fn get_node_weight(&self) -> u64 {
let cu = self.get_cu();
let su = self.get_su();
cu * 2 + su
}
) and applied to this node:

const GIGABYTE: u128 = 1024 * 1024 * 1024;
const ONE_THOUSAND: u128 = 1000;
const CRU: u64 = 24;
const MRU: u64 = 202803462144;
const SRU: u64 = 512110190592;
const HRU: u64 = 9001778946048;

fn calc_cu() -> u64 {
    let cru_min = CRU as u128 * 2 * GIGABYTE * ONE_THOUSAND;
    let mru_min =
        ((MRU as u128).checked_sub(1).unwrap_or(0) * GIGABYTE) * ONE_THOUSAND / 4;
    let sru_min = SRU as u128 * ONE_THOUSAND / 50;

    if cru_min < mru_min && cru_min < sru_min {
        cru_min as u64
    } else if mru_min < cru_min && mru_min < sru_min {
        mru_min as u64
    } else if sru_min < cru_min && sru_min < mru_min {
        sru_min as u64
    } else {
        0
    }
}
    
fn get_cu() -> u64 {
    let cu = calc_cu();
    let calculated_cu = 2 * (cu as u128 / GIGABYTE / ONE_THOUSAND);
    calculated_cu as u64
}
    
fn get_su() -> u64 {
    let su = HRU as u128 * ONE_THOUSAND / 1200 + SRU as u128 * ONE_THOUSAND / 250;
    let calculated_su = su / GIGABYTE;
    let result = calculated_su as u128 / ONE_THOUSAND;
    result as u64
}

fn main() {
    println!("CU: {}", get_cu());
    println!("SU: {}", get_su());
}

Copy past previous code in rust playground https://play.rust-lang.org/ and run gives:

CU: 18
SU: 8

Which gives for farm weight:

Weight = 2 * (sum of CU of all nodes) + (sum of SU of all nodes)
Weight = 2 * (18+18) + (8+8)
Weight = 2 * 36 + 16
Weight = 88

@renauter
Copy link
Collaborator

I guess the confusion is because you are using formulas from billing capacity instead of minting/farming capacity which are different.

Note: nevertheless, we already reported some incoherence related to weight calculation here #966 and need to check if it reflects what is done in minting code.

@A-Harby
Copy link
Author

A-Harby commented Apr 30, 2024

Yeah, with Resource Units Calculation it makes sense now.

Thanks for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants