Refactor the PowerCmp function
Why we have this: This algorithm here directly follow the rule of ratio to win, and easier to understand. Just think about a smaller space, e.g. 100, instead of 2^256. When one miner has 10% power, it should have 10% ratio to win. that is, when the h is in {0..9}, it wins. Simply speaking, it is: (h + 1) / 100 <= 10% , instead of h / 99 < 10% The former is easier to understand, though both are equivalent when h is an integer and power_ratio < 1)
This commit is contained in:
parent
790ac7b510
commit
0aa56ba665
@ -166,24 +166,25 @@ func PowerCmp(eproof ElectionProof, mpow, totpow BigInt) bool {
|
||||
|
||||
/*
|
||||
Need to check that
|
||||
h(vrfout) / max(h) < e * minerPower / totalPower
|
||||
(h(vrfout) + 1) / (max(h) + 1) <= e * minerPower / totalPower
|
||||
max(h) == 2^256-1
|
||||
which in terms of integer math means:
|
||||
h(vrfout) * totalPower < e * minerPower * (2^256-1)
|
||||
(h(vrfout) + 1) * totalPower <= e * minerPower * 2^256
|
||||
*/
|
||||
|
||||
h := sha256.Sum256(eproof)
|
||||
|
||||
lhs := BigFromBytes(h[:]).Int
|
||||
lhs = lhs.Add(lhs, NewInt(1).Int)
|
||||
lhs = lhs.Mul(lhs, totpow.Int)
|
||||
|
||||
// rhs = minerPower * 2^256 - minerPower
|
||||
// rhs = minerPower << 256 - minerPower
|
||||
// rhs = minerPower * 2^256
|
||||
// rhs = minerPower << 256
|
||||
rhs := new(big.Int).Lsh(mpow.Int, 256)
|
||||
rhs = rhs.Mul(rhs, blocksPerEpoch.Int)
|
||||
rhs = rhs.Sub(rhs, mpow.Int)
|
||||
|
||||
return lhs.Cmp(rhs) == -1
|
||||
|
||||
// return true if lhs is less than or equal to rhs
|
||||
return lhs.Cmp(rhs) < 1
|
||||
}
|
||||
|
||||
func (t *Ticket) Equals(ot *Ticket) bool {
|
||||
|
Loading…
Reference in New Issue
Block a user