Merge pull request #1819: rational -> decimal
* changelog * ... * decimal func working * decimal complete, untested * fixing tests * decimal compile errors resolved * test compile errors * precision multiplier test * 1% laptop battery * fixed TestNewDecFromStr * equalities working * fix bankers round chop * ... * working, some decimal issues resolved * fix rounding error * rounding works * decimal works * ... * deleted rational * rational conversion working * revert changelog * code compiles (not tests) * went through all NewDec, made sure they were converted from NewRat properly * test debugging * all testing bugs besides the json marshalling fixed * json unmarshal * lint * document update * fix lcd test * cli test fix * mostly undo Dece -> Rate * val comments * Efficiency improvements This now caches all of the precision multipliers (as they were all used in non-mutative functions), and caches the precisionInt calculation. (Now it just copies the already calculated value) * Cache another precisionInt() call. * Improve banker rounding efficiency * remove defer, make negation in-place. * chris val comments * bez comments * Aditya comments * ... * val comments * rebasing start * ... * compiling * tests pass * cli fix * anton, cwgoes, val comments * val and jae comments * type * undo reuse quo
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
// Validator implements sdk.Validator
|
||||
type Validator struct {
|
||||
Address sdk.AccAddress
|
||||
Power sdk.Rat
|
||||
Power sdk.Dec
|
||||
}
|
||||
|
||||
// Implements sdk.Validator
|
||||
@@ -29,18 +29,18 @@ func (v Validator) GetPubKey() crypto.PubKey {
|
||||
}
|
||||
|
||||
// Implements sdk.Validator
|
||||
func (v Validator) GetTokens() sdk.Rat {
|
||||
return sdk.ZeroRat()
|
||||
func (v Validator) GetTokens() sdk.Dec {
|
||||
return sdk.ZeroDec()
|
||||
}
|
||||
|
||||
// Implements sdk.Validator
|
||||
func (v Validator) GetPower() sdk.Rat {
|
||||
func (v Validator) GetPower() sdk.Dec {
|
||||
return v.Power
|
||||
}
|
||||
|
||||
// Implements sdk.Validator
|
||||
func (v Validator) GetDelegatorShares() sdk.Rat {
|
||||
return sdk.ZeroRat()
|
||||
func (v Validator) GetDelegatorShares() sdk.Dec {
|
||||
return sdk.ZeroDec()
|
||||
}
|
||||
|
||||
// Implements sdk.Validator
|
||||
@@ -93,8 +93,8 @@ func (vs *ValidatorSet) ValidatorByPubKey(ctx sdk.Context, pubkey crypto.PubKey)
|
||||
}
|
||||
|
||||
// TotalPower implements sdk.ValidatorSet
|
||||
func (vs *ValidatorSet) TotalPower(ctx sdk.Context) sdk.Rat {
|
||||
res := sdk.ZeroRat()
|
||||
func (vs *ValidatorSet) TotalPower(ctx sdk.Context) sdk.Dec {
|
||||
res := sdk.ZeroDec()
|
||||
for _, val := range vs.Validators {
|
||||
res = res.Add(val.Power)
|
||||
}
|
||||
@@ -122,7 +122,7 @@ func (vs *ValidatorSet) RemoveValidator(addr sdk.AccAddress) {
|
||||
}
|
||||
|
||||
// Implements sdk.ValidatorSet
|
||||
func (vs *ValidatorSet) Slash(ctx sdk.Context, pubkey crypto.PubKey, height int64, power int64, amt sdk.Rat) {
|
||||
func (vs *ValidatorSet) Slash(ctx sdk.Context, pubkey crypto.PubKey, height int64, power int64, amt sdk.Dec) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,8 @@ func TestValidatorSet(t *testing.T) {
|
||||
addr2 := []byte("addr2")
|
||||
|
||||
base := &mock.ValidatorSet{[]mock.Validator{
|
||||
{addr1, sdk.NewRat(1)},
|
||||
{addr2, sdk.NewRat(2)},
|
||||
{addr1, sdk.NewDec(1)},
|
||||
{addr2, sdk.NewDec(2)},
|
||||
}}
|
||||
|
||||
valset := NewValidatorSet(wire.NewCodec(), ctx.KVStore(key).Prefix([]byte("assoc")), base, 1, 5)
|
||||
|
||||
@@ -38,7 +38,7 @@ func NewHandler(keeper Keeper) sdk.Handler {
|
||||
In the previous example, the keeper has an `oracle.Keeper`. `oracle.Keeper`s are generated by `NewKeeper`.
|
||||
|
||||
```go
|
||||
func NewKeeper(key sdk.StoreKey, cdc *wire.Codec, valset sdk.ValidatorSet, supermaj sdk.Rat, timeout int64) Keeper {
|
||||
func NewKeeper(key sdk.StoreKey, cdc *wire.Codec, valset sdk.ValidatorSet, supermaj sdk.Dec, timeout int64) Keeper {
|
||||
return Keeper {
|
||||
cdc: cdc,
|
||||
key: key,
|
||||
|
||||
@@ -23,7 +23,7 @@ func (keeper Keeper) update(ctx sdk.Context, val sdk.Validator, valset sdk.Valid
|
||||
// and recalculate voted power
|
||||
hash := ctx.BlockHeader().ValidatorsHash
|
||||
if !bytes.Equal(hash, info.Hash) {
|
||||
info.Power = sdk.ZeroRat()
|
||||
info.Power = sdk.ZeroDec()
|
||||
info.Hash = hash
|
||||
prefix := GetSignPrefix(p, keeper.cdc)
|
||||
store := ctx.KVStore(keeper.key)
|
||||
|
||||
@@ -13,12 +13,12 @@ type Keeper struct {
|
||||
|
||||
valset sdk.ValidatorSet
|
||||
|
||||
supermaj sdk.Rat
|
||||
supermaj sdk.Dec
|
||||
timeout int64
|
||||
}
|
||||
|
||||
// NewKeeper constructs a new keeper
|
||||
func NewKeeper(key sdk.StoreKey, cdc *wire.Codec, valset sdk.ValidatorSet, supermaj sdk.Rat, timeout int64) Keeper {
|
||||
func NewKeeper(key sdk.StoreKey, cdc *wire.Codec, valset sdk.ValidatorSet, supermaj sdk.Dec, timeout int64) Keeper {
|
||||
if timeout < 0 {
|
||||
panic("Timeout should not be negative")
|
||||
}
|
||||
@@ -46,7 +46,7 @@ const (
|
||||
|
||||
// Info for each payload
|
||||
type Info struct {
|
||||
Power sdk.Rat
|
||||
Power sdk.Dec
|
||||
Hash []byte
|
||||
LastSigned int64
|
||||
Status InfoStatus
|
||||
@@ -55,7 +55,7 @@ type Info struct {
|
||||
// EmptyInfo construct an empty Info
|
||||
func EmptyInfo(ctx sdk.Context) Info {
|
||||
return Info{
|
||||
Power: sdk.ZeroRat(),
|
||||
Power: sdk.ZeroDec(),
|
||||
Hash: ctx.BlockHeader().ValidatorsHash,
|
||||
LastSigned: ctx.BlockHeight(),
|
||||
Status: Pending,
|
||||
|
||||
@@ -107,9 +107,9 @@ func TestOracle(t *testing.T) {
|
||||
addr3 := []byte("addr3")
|
||||
addr4 := []byte("addr4")
|
||||
valset := &mock.ValidatorSet{[]mock.Validator{
|
||||
{addr1, sdk.NewRat(7)},
|
||||
{addr2, sdk.NewRat(7)},
|
||||
{addr3, sdk.NewRat(1)},
|
||||
{addr1, sdk.NewDec(7)},
|
||||
{addr2, sdk.NewDec(7)},
|
||||
{addr3, sdk.NewDec(1)},
|
||||
}}
|
||||
|
||||
key := sdk.NewKVStoreKey("testkey")
|
||||
@@ -119,7 +119,7 @@ func TestOracle(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
ctx = ctx.WithBlockHeader(abci.Header{ValidatorsHash: bz})
|
||||
|
||||
ork := NewKeeper(key, cdc, valset, sdk.NewRat(2, 3), 100)
|
||||
ork := NewKeeper(key, cdc, valset, sdk.NewDecWithPrec(667, 3), 100) // 66.7%
|
||||
h := seqHandler(ork, key, sdk.CodespaceRoot)
|
||||
|
||||
// Nonmock.Validator signed, transaction failed
|
||||
@@ -171,7 +171,7 @@ func TestOracle(t *testing.T) {
|
||||
require.Equal(t, 1, getSequence(ctx, key))
|
||||
|
||||
// Should handle mock.Validator set change
|
||||
valset.AddValidator(mock.Validator{addr4, sdk.NewRat(12)})
|
||||
valset.AddValidator(mock.Validator{addr4, sdk.NewDec(12)})
|
||||
bz, err = json.Marshal(valset)
|
||||
require.Nil(t, err)
|
||||
ctx = ctx.WithBlockHeader(abci.Header{ValidatorsHash: bz})
|
||||
|
||||
Reference in New Issue
Block a user