From 4fe66115eddd9241d6a54249e26762bce963deed Mon Sep 17 00:00:00 2001 From: Henry Harder Date: Wed, 28 Aug 2019 16:40:38 -0700 Subject: [PATCH 1/5] go-kosu: use pointer types in poster limit --- packages/go-kosu/abci/rebalance.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/go-kosu/abci/rebalance.go b/packages/go-kosu/abci/rebalance.go index 651b84c6..c087c87a 100644 --- a/packages/go-kosu/abci/rebalance.go +++ b/packages/go-kosu/abci/rebalance.go @@ -77,15 +77,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 + var pl, pb, tb *big.Int 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 From cf6a4b2c27b7546bc722d0224a6a4315bb9c9e3b Mon Sep 17 00:00:00 2001 From: Henry Harder Date: Wed, 28 Aug 2019 16:40:54 -0700 Subject: [PATCH 2/5] go-kosu: update scaleBalance to not use Rat --- packages/go-kosu/abci/witness.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/go-kosu/abci/witness.go b/packages/go-kosu/abci/witness.go index 516054c3..fe03b2ae 100644 --- a/packages/go-kosu/abci/witness.go +++ b/packages/go-kosu/abci/witness.go @@ -102,15 +102,16 @@ 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) - // 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 scaled.Int64() + } + return math.MaxInt64 } From dfaf411aa7015acf750038a8b1b876ff901af176 Mon Sep 17 00:00:00 2001 From: Henry Harder Date: Wed, 28 Aug 2019 16:54:52 -0700 Subject: [PATCH 3/5] go-kosu: fix copy of big.Ints --- packages/go-kosu/abci/rebalance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/go-kosu/abci/rebalance.go b/packages/go-kosu/abci/rebalance.go index c087c87a..be78cdb7 100644 --- a/packages/go-kosu/abci/rebalance.go +++ b/packages/go-kosu/abci/rebalance.go @@ -77,7 +77,7 @@ 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{} pl.SetUint64(periodLimit) pb.Set(posterBalance) tb.Set(totalBalance) From 8a916aa76076a1537e7fdf9359dfec0fd45ca3f9 Mon Sep 17 00:00:00 2001 From: Henry Harder Date: Wed, 28 Aug 2019 16:56:29 -0700 Subject: [PATCH 4/5] go-kosu: re-add gosec lint skip --- packages/go-kosu/abci/witness.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/go-kosu/abci/witness.go b/packages/go-kosu/abci/witness.go index fe03b2ae..35a41820 100644 --- a/packages/go-kosu/abci/witness.go +++ b/packages/go-kosu/abci/witness.go @@ -107,6 +107,8 @@ func scaleBalance(balance *big.Int) int64 { scaled.Set(balance) // scale balance by 10**18 (base units for KOSU) + // linter disabled for outdated gosec rule + // nolint:gosec ether.Exp(big.NewInt(10), big.NewInt(18), big.NewInt(0)) scaled.Div(balance, ether) From 9c82c0793c157109b3dbc13e4f4346d3a0fa770a Mon Sep 17 00:00:00 2001 From: Henry Harder Date: Wed, 28 Aug 2019 16:57:14 -0700 Subject: [PATCH 5/5] go-kosu: update overflow check for consistency --- packages/go-kosu/abci/witness.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/go-kosu/abci/witness.go b/packages/go-kosu/abci/witness.go index 35a41820..9cacd837 100644 --- a/packages/go-kosu/abci/witness.go +++ b/packages/go-kosu/abci/witness.go @@ -112,8 +112,8 @@ func scaleBalance(balance *big.Int) int64 { ether.Exp(big.NewInt(10), big.NewInt(18), big.NewInt(0)) scaled.Div(balance, ether) - if scaled.IsInt64() { - return scaled.Int64() + if !scaled.IsInt64() { + return math.MaxInt64 } - return math.MaxInt64 + return scaled.Int64() }