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

RRR4R: rational -> decimal #1819

Merged
merged 54 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
abf9a65
changelog
rigelrozanski Jul 25, 2018
84c7fab
...
rigelrozanski Jul 25, 2018
c9ed668
decimal func working
rigelrozanski Jul 27, 2018
e4e78b4
decimal complete, untested
rigelrozanski Jul 27, 2018
206ed8b
fixing tests
rigelrozanski Jul 27, 2018
6cbe1ee
Merge remote-tracking branch 'origin/develop' into rigel/decimal
rigelrozanski Jul 27, 2018
ee627ed
decimal compile errors resolved
rigelrozanski Jul 27, 2018
2f130bb
test compile errors
rigelrozanski Jul 27, 2018
a2ced74
precision multiplier test
rigelrozanski Jul 27, 2018
1aa00b7
1% laptop battery
rigelrozanski Jul 27, 2018
9ffbf40
fixed TestNewDecFromStr
rigelrozanski Jul 27, 2018
f8be905
equalities working
rigelrozanski Jul 27, 2018
a598efb
fix bankers round chop
rigelrozanski Jul 27, 2018
697bb2e
...
rigelrozanski Jul 27, 2018
20486a9
working, some decimal issues resolved
rigelrozanski Jul 27, 2018
173acc9
fix rounding error
rigelrozanski Jul 28, 2018
44f24ac
rounding works
rigelrozanski Jul 28, 2018
34a9978
decimal works
rigelrozanski Jul 28, 2018
6b2dab7
...
rigelrozanski Jul 28, 2018
ee0e179
deleted rational
rigelrozanski Jul 28, 2018
546a4ca
rational conversion working
rigelrozanski Jul 28, 2018
5015fd4
revert changelog
rigelrozanski Jul 28, 2018
d2bf108
code compiles (not tests)
rigelrozanski Jul 28, 2018
3f4bb2e
went through all NewDec, made sure they were converted from NewRat pr…
rigelrozanski Jul 28, 2018
12e4022
test debugging
rigelrozanski Jul 28, 2018
d605058
all testing bugs besides the json marshalling fixed
rigelrozanski Aug 1, 2018
0a247fd
json unmarshal
rigelrozanski Aug 1, 2018
40d49bf
lint
rigelrozanski Aug 1, 2018
73c5d9c
Merge remote-tracking branch 'origin/develop' into rigel/decimal
rigelrozanski Aug 1, 2018
89dd6e4
document update
rigelrozanski Aug 1, 2018
5d939f8
Merge remote-tracking branch 'origin/develop' into rigel/decimal
rigelrozanski Aug 3, 2018
e5f2faf
fix lcd test
rigelrozanski Aug 3, 2018
09e7e25
cli test fix
rigelrozanski Aug 3, 2018
196fcce
mostly undo Dece -> Rate
rigelrozanski Aug 3, 2018
f43bf90
val comments
rigelrozanski Aug 3, 2018
16bc41e
Efficiency improvements
ValarDragon Aug 4, 2018
9c954ce
Cache another precisionInt() call.
ValarDragon Aug 4, 2018
9859476
Improve banker rounding efficiency
ValarDragon Aug 5, 2018
348cb1d
remove defer, make negation in-place.
ValarDragon Aug 5, 2018
e26a87d
chris val comments
rigelrozanski Aug 11, 2018
1e57807
bez comments
rigelrozanski Aug 11, 2018
609e07d
Aditya comments
rigelrozanski Aug 11, 2018
81e96e8
...
rigelrozanski Aug 11, 2018
fd52603
val comments
rigelrozanski Aug 13, 2018
b7aaac5
rebasing start
rigelrozanski Aug 13, 2018
e9900a3
Merge remote-tracking branch 'origin/develop' into rigel/decimal
rigelrozanski Aug 13, 2018
18b4826
...
rigelrozanski Aug 13, 2018
b77fc51
compiling
rigelrozanski Aug 13, 2018
24ce274
tests pass
rigelrozanski Aug 13, 2018
b600f71
cli fix
rigelrozanski Aug 13, 2018
d81775b
anton, cwgoes, val comments
rigelrozanski Aug 14, 2018
af48ed2
val and jae comments
rigelrozanski Aug 14, 2018
db72022
type
rigelrozanski Aug 14, 2018
fa57c38
undo reuse quo
rigelrozanski Aug 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ BREAKING CHANGES
* `gaiacli gov vote --voter`
* [x/gov] Added tags sub-package, changed tags to use dash-case
* [x/gov] Governance parameters are now stored in globalparams store
* [core] \#1807 Switch from use of rational to decimal
* [lcd] \#1866 Updated lcd /slashing/signing_info endpoint to take cosmosvalpub instead of cosmosvaladdr
* [types] sdk.NewCoin now takes sdk.Int, sdk.NewInt64Coin takes int64
* [cli] #1551: Officially removed `--name` from CLI commands
Expand Down
2 changes: 1 addition & 1 deletion client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress
accAuth.Coins = sdk.Coins{sdk.NewInt64Coin("steak", 100)}
acc := gapp.NewGenesisAccount(&accAuth)
genesisState.Accounts = append(genesisState.Accounts, acc)
genesisState.StakeData.Pool.LooseTokens = genesisState.StakeData.Pool.LooseTokens.Add(sdk.NewRat(100))
genesisState.StakeData.Pool.LooseTokens = genesisState.StakeData.Pool.LooseTokens.Add(sdk.NewDec(100))
}

appState, err := wire.MarshalJSONIndent(cdc, genesisState)
Expand Down
6 changes: 3 additions & 3 deletions cmd/gaia/app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ func GaiaAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (genesisState
}
acc := NewGenesisAccount(&accAuth)
genaccs[i] = acc
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewRat(freeFermionsAcc)) // increase the supply
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDec(freeFermionsAcc)) // increase the supply

// add the validator
if len(genTx.Name) > 0 {
desc := stake.NewDescription(genTx.Name, "", "", "")
validator := stake.NewValidator(genTx.Address,
sdk.MustGetAccPubKeyBech32(genTx.PubKey), desc)

stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewRat(freeFermionVal)) // increase the supply
stakeData.Pool.LooseTokens = stakeData.Pool.LooseTokens.Add(sdk.NewDec(freeFermionVal)) // increase the supply

// add some new shares to the validator
var issuedDelShares sdk.Rat
var issuedDelShares sdk.Dec
validator, stakeData.Pool, issuedDelShares = validator.AddTokensFromDel(stakeData.Pool, freeFermionVal)
stakeData.Validators = append(stakeData.Validators, validator)

Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func appStateFn(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage {

// Default genesis state
stakeGenesis := stake.DefaultGenesisState()
stakeGenesis.Pool.LooseTokens = sdk.NewRat(1000)
stakeGenesis.Pool.LooseTokens = sdk.NewDec(1000)
genesis := GenesisState{
Accounts: genesisAccounts,
StakeData: stakeGenesis,
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestGaiaCLICreateValidator(t *testing.T) {

validator := executeGetValidator(t, fmt.Sprintf("gaiacli stake validator %s --output=json %v", barAddr, flags))
require.Equal(t, validator.Owner, barAddr)
require.True(sdk.RatEq(t, sdk.NewRat(2), validator.Tokens))
require.True(sdk.DecEq(t, sdk.NewDec(2), validator.Tokens))

// unbond a single share
unbondStr := fmt.Sprintf("gaiacli stake unbond begin %v", flags)
Expand All @@ -149,7 +149,7 @@ func TestGaiaCLICreateValidator(t *testing.T) {
require.Equal(t, int64(9), barAcc.GetCoins().AmountOf("steak").Int64(), "%v", barAcc)
*/
validator = executeGetValidator(t, fmt.Sprintf("gaiacli stake validator %s --output=json %v", barAddr, flags))
require.Equal(t, "1/1", validator.Tokens.String())
require.Equal(t, "1.0000000000", validator.Tokens.String())
}

func TestGaiaCLISubmitProposal(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions docs/spec/governance/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type VotingProcedure struct {

```go
type TallyingProcedure struct {
Threshold rational.Rational // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5
Veto rational.Rational // Minimum proportion of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3
GovernancePenalty sdk.Rat // Penalty if validator does not vote
Threshold sdk.Dec // Minimum propotion of Yes votes for proposal to pass. Initial value: 0.5
Veto sdk.Dec // Minimum proportion of Veto votes to Total votes ratio for proposal to be vetoed. Initial value: 1/3
GovernancePenalty sdk.Dec // Penalty if validator does not vote
GracePeriod int64 // If validator entered validator set in this period of blocks before vote ended, governance penalty does not apply
}
```
Expand Down Expand Up @@ -81,7 +81,7 @@ This type is used in a temp map when tallying

```go
type ValidatorGovInfo struct {
Minus sdk.Rat
Minus sdk.Dec
Vote Vote
}
```
Expand All @@ -103,17 +103,17 @@ type Proposal struct {
VotingStartBlock int64 // Height of the block where MinDeposit was reached. -1 if MinDeposit is not reached
CurrentStatus ProposalStatus // Current status of the proposal

YesVotes sdk.Rat
NoVotes sdk.Rat
NoWithVetoVotes sdk.Rat
AbstainVotes sdk.Rat
YesVotes sdk.Dec
NoVotes sdk.Dec
NoWithVetoVotes sdk.Dec
AbstainVotes sdk.Dec
}
```

We also mention a method to update the tally for a given proposal:

```go
func (proposal Proposal) updateTally(vote byte, amount sdk.Rat)
func (proposal Proposal) updateTally(vote byte, amount sdk.Dec)
```

### Stores
Expand Down
2 changes: 1 addition & 1 deletion docs/spec/inflation/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
The current annual inflation rate.

```golang
type Inflation sdk.Rat
type Inflation sdk.Dec
```

### InflationLastTime
Expand Down
1 change: 0 additions & 1 deletion docs/spec/staking/end_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ EndBlock() ValidatorSetChanges
ClearTendermintUpdates()
return vsc
```

30 changes: 15 additions & 15 deletions docs/spec/staking/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Pool struct {
LooseTokens int64 // tokens not associated with any bonded validator
BondedTokens int64 // reserve of bonded tokens
InflationLastTime int64 // block which the last inflation was processed // TODO make time
Inflation sdk.Rat // current annual inflation rate
Inflation sdk.Dec // current annual inflation rate

DateLastCommissionReset int64 // unix timestamp for last commission accounting reset (daily)
}
Expand All @@ -28,10 +28,10 @@ overall functioning of the stake module.

```golang
type Params struct {
InflationRateChange sdk.Rat // maximum annual change in inflation rate
InflationMax sdk.Rat // maximum inflation rate
InflationMin sdk.Rat // minimum inflation rate
GoalBonded sdk.Rat // Goal of percent bonded atoms
InflationRateChange sdk.Dec // maximum annual change in inflation rate
InflationMax sdk.Dec // maximum inflation rate
InflationMin sdk.Dec // minimum inflation rate
GoalBonded sdk.Dec // Goal of percent bonded atoms

MaxValidators uint16 // maximum number of validators
BondDenom string // bondable coin denomination
Expand Down Expand Up @@ -74,9 +74,9 @@ type Validator struct {
Revoked bool // has the validator been revoked?

Status sdk.BondStatus // validator status (bonded/unbonding/unbonded)
Tokens sdk.Rat // delegated tokens (incl. self-delegation)
DelegatorShares sdk.Rat // total shares issued to a validator's delegators
SlashRatio sdk.Rat // increases each time the validator is slashed
Tokens sdk.Dec // delegated tokens (incl. self-delegation)
DelegatorShares sdk.Dec // total shares issued to a validator's delegators
SlashRatio sdk.Dec // increases each time the validator is slashed

Description Description // description terms for the validator

Expand All @@ -88,10 +88,10 @@ type Validator struct {
}

type CommissionInfo struct {
Rate sdk.Rat // the commission rate of fees charged to any delegators
Max sdk.Rat // maximum commission rate which this validator can ever charge
ChangeRate sdk.Rat // maximum daily increase of the validator commission
ChangeToday sdk.Rat // commission rate change today, reset each day (UTC time)
Rate sdk.Dec // the commission rate of fees charged to any delegators
Max sdk.Dec // maximum commission rate which this validator can ever charge
ChangeRate sdk.Dec // maximum daily increase of the validator commission
ChangeToday sdk.Dec // commission rate change today, reset each day (UTC time)
LastChange int64 // unix timestamp of last commission change
}

Expand All @@ -117,7 +117,7 @@ the transaction is the owner of the bond.

```golang
type Delegation struct {
Shares sdk.Rat // delegation shares recieved
Shares sdk.Dec // delegation shares recieved
Height int64 // last height bond updated
}
```
Expand Down Expand Up @@ -178,8 +178,8 @@ the original redelegation has been completed.

```golang
type Redelegation struct {
SourceShares sdk.Rat // amount of source shares redelegating
DestinationShares sdk.Rat // amount of destination shares created at redelegation
SourceShares sdk.Dec // amount of source shares redelegating
DestinationShares sdk.Dec // amount of destination shares created at redelegation
CompleteTime int64 // unix time to complete redelegation
}
```
12 changes: 6 additions & 6 deletions docs/spec/staking/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Other notes:
- `sender` denotes the address of the sender of the transaction
- `getXxx`, `setXxx`, and `removeXxx` functions are used to retrieve and
modify objects from the store
- `sdk.Rat` refers to a rational numeric type specified by the SDK.
- `sdk.Dec` refers to a decimal type specified by the SDK.

### TxCreateValidator

Expand All @@ -34,9 +34,9 @@ type TxCreateValidator struct {
SelfDelegation coin.Coin

Description Description
Commission sdk.Rat
CommissionMax sdk.Rat
CommissionMaxChange sdk.Rat
Commission sdk.Dec
CommissionMax sdk.Dec
CommissionMaxChange sdk.Dec
}


Expand Down Expand Up @@ -65,7 +65,7 @@ If either the `Description` (excluding `DateBonded` which is constant),
```golang
type TxEditCandidacy struct {
GovernancePubKey crypto.PubKey
Commission sdk.Rat
Commission sdk.Dec
Description Description
}

Expand Down Expand Up @@ -199,7 +199,7 @@ type TxRedelegate struct {
DelegatorAddr Address
ValidatorFrom Validator
ValidatorTo Validator
Shares sdk.Rat
Shares sdk.Dec
CompletedTime int64
}

Expand Down
18 changes: 9 additions & 9 deletions examples/democoin/mock/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// Validator implements sdk.Validator
type Validator struct {
Address sdk.AccAddress
Power sdk.Rat
Power sdk.Dec
}

// Implements sdk.Validator
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/x/assoc/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/oracle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/democoin/x/oracle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions examples/democoin/x/oracle/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -46,7 +46,7 @@ const (

// Info for each payload
type Info struct {
Power sdk.Rat
Power sdk.Dec
Hash []byte
LastSigned int64
Status InfoStatus
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions examples/democoin/x/oracle/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand Down
Loading