diff --git a/Gopkg.lock b/Gopkg.lock index 57f7aaa79..46cb95893 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -145,10 +145,7 @@ [[projects]] name = "github.com/magiconair/properties" - packages = [ - ".", - "assert" - ] + packages = ["."] revision = "c3beff4c2358b44d0493c7dda585e7db7ff28ae6" version = "v1.7.6" diff --git a/types/rational.go b/types/rational.go index b3a914659..8ebee7140 100644 --- a/types/rational.go +++ b/types/rational.go @@ -1,6 +1,7 @@ package types import ( + "fmt" "math/big" "strconv" "strings" @@ -79,7 +80,7 @@ func NewRat(num int64, denom ...int64) Rat { } } -//NewFromDecimal - create a rational from decimal string or integer string +// create a rational from decimal string or integer string func NewRatFromDecimal(decimalStr string) (f Rat, err Error) { // first extract any negative symbol @@ -148,14 +149,16 @@ func (r Rat) Sub(r2 Rat) Rat { return ToRat(new(big.Rat).Sub(r.GetRat(), r2.G //func (r Rat) Sub(r2 Rat) Rat { return Rat{new(big.Rat).Sub(r.Rat, r2.GetRat())} } // Sub - subtraction //func (r Rat) String() string { return fmt.Sprintf("%v/%v", r.Num(), r.Denom()) } // Sub - subtraction -var zero = big.NewInt(0) -var one = big.NewInt(1) -var two = big.NewInt(2) -var five = big.NewInt(5) -var nFive = big.NewInt(-5) -var ten = big.NewInt(10) +var ( + zero = big.NewInt(0) + one = big.NewInt(1) + two = big.NewInt(2) + five = big.NewInt(5) + nFive = big.NewInt(-5) + ten = big.NewInt(10) +) -// EvaluateBig - evaluate the rational using bankers rounding +// evaluate the rational using bankers rounding func (r Rat) EvaluateBig() *big.Int { num := r.GetRat().Num() @@ -185,17 +188,25 @@ func (r Rat) EvaluateBig() *big.Int { return d } -// Evaluate - evaluate the rational using bankers rounding +// evaluate the rational using bankers rounding func (r Rat) Evaluate() int64 { return r.EvaluateBig().Int64() } -// Round - round Rat with the provided precisionFactor +// round Rat with the provided precisionFactor func (r Rat) Round(precisionFactor int64) Rat { rTen := ToRat(new(big.Rat).Mul(r.GetRat(), big.NewRat(precisionFactor, 1))) return ToRat(big.NewRat(rTen.Evaluate(), precisionFactor)) } +// TODO panic if negative or if totalDigits < len(initStr)??? +// evaluate as an integer and return left padded string +func (r Rat) ToLeftPadded(totalDigits int8) string { + intStr := r.EvaluateBig().String() + fcode := `%0` + strconv.Itoa(int(totalDigits)) + `s` + return fmt.Sprintf(fcode, intStr) +} + //___________________________________________________________________________________ // Hack to just use json.Marshal for everything until diff --git a/types/rational_test.go b/types/rational_test.go index 8c85cda06..c83af3917 100644 --- a/types/rational_test.go +++ b/types/rational_test.go @@ -192,6 +192,20 @@ func TestRound(t *testing.T) { } } +func TestToLeftPaddedString(t *testing.T) { + tests := []struct { + rat Rat + digits int8 + res string + }{ + {NewRat(100, 3), 8, "00000033"}, + } + + for _, tc := range tests { + assert.Equal(t, tc.res, tc.rat.ToLeftPadded(tc.digits)) + } +} + //func TestZeroSerializationJSON(t *testing.T) { //r := NewRat(0, 1) //err := r.UnmarshalJSON([]byte(`"0/1"`)) diff --git a/x/stake/tick.go b/x/stake/tick.go index 47be614e0..6aa2da95d 100644 --- a/x/stake/tick.go +++ b/x/stake/tick.go @@ -37,7 +37,7 @@ func (k Keeper) Tick(ctx sdk.Context) (change []*abci.Validator, err error) { func (k Keeper) processProvisions(ctx sdk.Context) { pool := k.GetPool(ctx) - pool.Inflation = k.nextInflation(ctx).Round(precision) //TODO make this number a const somewhere? + pool.Inflation = k.nextInflation(ctx).Round(precision) // Because the validators hold a relative bonded share (`GlobalStakeShare`), when // more bonded tokens are added proportionally to all validators the only term diff --git a/x/stake/types.go b/x/stake/types.go index ba89c5288..2799e1d76 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -16,6 +16,8 @@ type Params struct { BondDenom string `json:"bond_denom"` // bondable coin denomination } +// XXX do we want to allow for default params even or do we want to enforce that you +// need to be explicit about defining all params in genesis? func defaultParams() Params { return Params{ InflationRateChange: sdk.NewRat(13, 100),