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

go-kosu: update scaling methods to be consistent #243

Merged
merged 7 commits into from Aug 29, 2019
@@ -79,15 +79,15 @@ func posterIterator(app *App, totalBalance *big.Int) func(string, *types.Poster)
// calculates a poster's period limit based on their balance and the total poster balance
func posterLimit(periodLimit uint64, posterBalance, totalBalance *big.Int) uint64 {
// copy periodLimit (pl), posterBalance (pb), totalBalance (tb)
var pl, pb, tb big.Int
pl, pb, tb := &big.Int{}, &big.Int{}, &big.Int{}
This conversation was marked as resolved by gchaincl

This comment has been minimized.

Copy link
@gchaincl

gchaincl Aug 29, 2019

Contributor

what's the difference between this and using non-pointers ?

This comment has been minimized.

Copy link
@hrharder

hrharder Aug 29, 2019

Author Member

I wanted to be consistent in how I allocate the bigInts, and in the other method I use n := &big.Int{} rather than var n *big.Int.

I also want to be consistent and use big.Int only as pointers, as per the documentation, so it feels weird to do var n big.Int and then &n everywhere it gets used.

pl.SetUint64(periodLimit)
pb.Set(posterBalance)
tb.Set(totalBalance)

// limit = (posterBalance / totalBalance) * periodLimit
limit := big.NewInt(0)
limit.Mul(&pl, &pb)
limit.Div(limit, &tb)
limit.Mul(pl, pb)
limit.Div(limit, tb)

if !limit.IsUint64() {
return math.MaxUint64
@@ -102,15 +102,18 @@ func scaleBalance(balance *big.Int) int64 {
return int64(0)
}

scaled := &big.Rat{}
divisor := &big.Int{}
scaled := &big.Int{}
ether := &big.Int{}
scaled.Set(balance)

// scale balance by 10**18 (base units for KOSU)
// linter disabled for outdated gosec rule
// nolint:gosec
divisor = divisor.Exp(big.NewInt(10), big.NewInt(18), nil)
scaled.SetFrac(balance, divisor)
ether.Exp(big.NewInt(10), big.NewInt(18), big.NewInt(0))
scaled.Div(balance, ether)

res, _ := scaled.Float64()
power := math.Floor(res)
return int64(power)
if !scaled.IsInt64() {
return math.MaxInt64
This conversation was marked as resolved by gchaincl

This comment has been minimized.

Copy link
@gchaincl

gchaincl Aug 29, 2019

Contributor

what are the implications of this?

This comment has been minimized.

Copy link
@hrharder

hrharder Aug 29, 2019

Author Member

If someone has aquired a balance of Kosu (in full ether units) >= MaxInf64, they will just be given MaxInt64 power on the chain, rather than what they "should" get.

This is a highly unlikely edge case IMO, given the parameterization of our bonding curve. It will be very hard (essentially impossible) to ever get that many tokens.

}
return scaled.Int64()
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.