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

R4R: Distr-PR-2 Truncate Decimal Functionality #2379

Merged
merged 18 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ BREAKING CHANGES
* [codec] \#2324 All referrences to wire have been renamed to codec. Additionally, wire.NewCodec is now codec.New().
* [types] \#2343 Make sdk.Msg have a names field, to facilitate automatic tagging.
* [baseapp] \#2366 Automatically add action tags to all messages
* [x/staking] \#2244 staking now holds a consensus-address-index instead of a consensus-pubkey-index

* Tendermint

Expand Down Expand Up @@ -128,6 +129,7 @@ IMPROVEMENTS
* [simulation] Logs get written to file if large, and also get printed on panics \#2285
* [gaiad] \#1992 Add optional flag to `gaiad testnet` to make config directory of daemon (default `gaiad`) and cli (default `gaiacli`) configurable
* [x/stake] Add stake `Queriers` for Gaia-lite endpoints. This increases the staking endpoints performance by reusing the staking `keeper` logic for queries. [#2249](https://github.com/cosmos/cosmos-sdk/pull/2149)
* [types/decimal] \#2378 - Added truncate functionality to decimal

* Tendermint

Expand Down
11 changes: 6 additions & 5 deletions docs/spec/staking/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ type Params struct {
Validators are identified according to the `OperatorAddr`, an SDK validator
address for the operator of the validator.

Validators also have a `ConsPubKey`, the public key of the validator.

Validators are indexed in the store using the following maps:
Validators also have a `ConsPubKey`, the public key of the validator used in
Tendermint consensus. The validator can be retrieved from it's `ConsPubKey`
once it can be converted into the corresponding `ConsAddr`. Validators are
indexed in the store using the following maps:

- Validators: `0x02 | OperatorAddr -> amino(validator)`
- ValidatorsByPubKey: `0x03 | ConsPubKey -> OperatorAddr`
- ValidatorsByConsAddr: `0x03 | ConsAddr -> OperatorAddr`
- ValidatorsByPower: `0x05 | power | blockHeight | blockTx -> OperatorAddr`

`Validators` is the primary index - it ensures that each operator can have only one
Expand All @@ -69,7 +70,7 @@ validator.

```golang
type Validator struct {
ConsensusPubKey crypto.PubKey // Tendermint consensus pubkey of validator
ConsPubKey crypto.PubKey // Tendermint consensus pubkey of validator
Jailed bool // has the validator been jailed?

Status sdk.BondStatus // validator status (bonded/unbonding/unbonded)
Expand Down
22 changes: 16 additions & 6 deletions examples/democoin/mock/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func (v Validator) GetOperator() sdk.ValAddress {
}

// Implements sdk.Validator
func (v Validator) GetPubKey() crypto.PubKey {
func (v Validator) GetConsPubKey() crypto.PubKey {
return nil
}

// Implements sdk.Validator
func (v Validator) GetConsAddr() sdk.ConsAddress {
return nil
}

Expand Down Expand Up @@ -88,7 +93,12 @@ func (vs *ValidatorSet) Validator(ctx sdk.Context, addr sdk.ValAddress) sdk.Vali
}

// ValidatorByPubKey implements sdk.ValidatorSet
func (vs *ValidatorSet) ValidatorByPubKey(ctx sdk.Context, pubkey crypto.PubKey) sdk.Validator {
func (vs *ValidatorSet) ValidatorByConsPubKey(_ sdk.Context, _ crypto.PubKey) sdk.Validator {
panic("not implemented")
}

// ValidatorByPubKey implements sdk.ValidatorSet
func (vs *ValidatorSet) ValidatorByConsAddr(_ sdk.Context, _ sdk.ConsAddress) sdk.Validator {
panic("not implemented")
}

Expand Down Expand Up @@ -122,21 +132,21 @@ 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.Dec) {
func (vs *ValidatorSet) Slash(_ sdk.Context, _ sdk.ConsAddress, _ int64, _ int64, _ sdk.Dec) {
panic("not implemented")
}

// Implements sdk.ValidatorSet
func (vs *ValidatorSet) Jail(ctx sdk.Context, pubkey crypto.PubKey) {
func (vs *ValidatorSet) Jail(_ sdk.Context, _ sdk.ConsAddress) {
panic("not implemented")
}

// Implements sdk.ValidatorSet
func (vs *ValidatorSet) Unjail(ctx sdk.Context, pubkey crypto.PubKey) {
func (vs *ValidatorSet) Unjail(_ sdk.Context, _ sdk.ConsAddress) {
panic("not implemented")
}

// Implements sdk.ValidatorSet
func (vs *ValidatorSet) Delegation(ctx sdk.Context, addrDel sdk.AccAddress, addrVal sdk.ValAddress) sdk.Delegation {
func (vs *ValidatorSet) Delegation(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) sdk.Delegation {
panic("not implemented")
}
5 changes: 5 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ func ConsAddressFromBech32(address string) (addr ConsAddress, err error) {
return ConsAddress(bz), nil
}

// get ConsAddress from pubkey
func GetConsAddress(pubkey crypto.PubKey) ConsAddress {
return ConsAddress(pubkey.Address())
}

// Returns boolean for whether two ConsAddress are Equal
func (ca ConsAddress) Equals(ca2 ConsAddress) bool {
if ca.Empty() && ca2.Empty() {
Expand Down
26 changes: 26 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,32 @@ func (d Dec) RoundInt() Int {

//___________________________________________________________________________________

// similar to chopPrecisionAndRound, but always rounds down
func chopPrecisionAndTruncate(d *big.Int) *big.Int {
return d.Quo(d, precisionReuse)
}

func chopPrecisionAndTruncateNonMutative(d *big.Int) *big.Int {
rigelrozanski marked this conversation as resolved.
Show resolved Hide resolved
tmp := new(big.Int).Set(d)
return chopPrecisionAndTruncate(tmp)
}

// TruncateInt64 truncates the decimals from the number and returns an int64
func (d Dec) TruncateInt64() int64 {
chopped := chopPrecisionAndTruncateNonMutative(d.Int)
if !chopped.IsInt64() {
panic("Int64() out of bound")
}
return chopped.Int64()
}

// TruncateInt truncates the decimals from the number and returns an Int
func (d Dec) TruncateInt() Int {
return NewIntFromBigInt(chopPrecisionAndTruncateNonMutative(d.Int))
}

//___________________________________________________________________________________

// reuse nil values
var (
nilAmino string
Expand Down
26 changes: 26 additions & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ func TestBankerRoundChop(t *testing.T) {
}
}

func TestTruncate(t *testing.T) {
tests := []struct {
d1 Dec
exp int64
}{
{mustNewDecFromStr(t, "0"), 0},
{mustNewDecFromStr(t, "0.25"), 0},
{mustNewDecFromStr(t, "0.75"), 0},
{mustNewDecFromStr(t, "1"), 1},
{mustNewDecFromStr(t, "1.5"), 1},
{mustNewDecFromStr(t, "7.5"), 7},
{mustNewDecFromStr(t, "7.6"), 7},
{mustNewDecFromStr(t, "7.4"), 7},
{mustNewDecFromStr(t, "100.1"), 100},
{mustNewDecFromStr(t, "1000.1"), 1000},
}

for tcIndex, tc := range tests {
resNeg := tc.d1.Neg().TruncateInt64()
require.Equal(t, -1*tc.exp, resNeg, "negative tc %d", tcIndex)

resPos := tc.d1.TruncateInt64()
require.Equal(t, tc.exp, resPos, "positive tc %d", tcIndex)
}
}

func TestToLeftPadded(t *testing.T) {
tests := []struct {
dec Dec
Expand Down
35 changes: 18 additions & 17 deletions types/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ func (b BondStatus) Equal(b2 BondStatus) bool {

// validator for a delegated proof of stake system
type Validator interface {
GetJailed() bool // whether the validator is jailed
GetMoniker() string // moniker of the validator
GetStatus() BondStatus // status of the validator
GetOperator() ValAddress // operator address to receive/return validators coins
GetPubKey() crypto.PubKey // validation pubkey
GetPower() Dec // validation power
GetTokens() Dec // validation tokens
GetDelegatorShares() Dec // Total out standing delegator shares
GetBondHeight() int64 // height in which the validator became active
GetJailed() bool // whether the validator is jailed
GetMoniker() string // moniker of the validator
GetStatus() BondStatus // status of the validator
GetOperator() ValAddress // operator address to receive/return validators coins
GetConsPubKey() crypto.PubKey // validation consensus pubkey
GetConsAddr() ConsAddress // validation consensus address
GetPower() Dec // validation power
GetTokens() Dec // validation tokens
GetDelegatorShares() Dec // Total out standing delegator shares
GetBondHeight() int64 // height in which the validator became active
}

// validator which fulfills abci validator interface for use in Tendermint
func ABCIValidator(v Validator) abci.Validator {
return abci.Validator{
PubKey: tmtypes.TM2PB.PubKey(v.GetPubKey()),
Address: v.GetPubKey().Address(),
PubKey: tmtypes.TM2PB.PubKey(v.GetConsPubKey()),
Address: v.GetConsPubKey().Address(),
Power: v.GetPower().RoundInt64(),
}
}
Expand All @@ -67,14 +68,14 @@ type ValidatorSet interface {
IterateValidatorsBonded(Context,
func(index int64, validator Validator) (stop bool))

Validator(Context, ValAddress) Validator // get a particular validator by operator
ValidatorByPubKey(Context, crypto.PubKey) Validator // get a particular validator by signing PubKey
Validator(Context, ValAddress) Validator // get a particular validator by operator address
ValidatorByConsAddr(Context, ConsAddress) Validator // get a particular validator by consensus address
TotalPower(Context) Dec // total power of the validator set

// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
Slash(Context, crypto.PubKey, int64, int64, Dec)
Jail(Context, crypto.PubKey) // jail a validator
Unjail(Context, crypto.PubKey) // unjail a validator
Slash(Context, ConsAddress, int64, int64, Dec)
Jail(Context, ConsAddress) // jail a validator
Unjail(Context, ConsAddress) // unjail a validator

// Delegation allows for getting a particular delegation for a given validator
// and delegator outside the scope of the staking module.
Expand All @@ -87,7 +88,7 @@ type ValidatorSet interface {
type Delegation interface {
GetDelegator() AccAddress // delegator AccAddress for the bond
GetValidator() ValAddress // validator operator address
GetBondShares() Dec // amount of validator's shares
GetShares() Dec // amount of validator's shares held in this delegation
}

// properties for the set of all delegations for a particular
Expand Down
91 changes: 0 additions & 91 deletions x/distribution/keeper.go

This file was deleted.

31 changes: 0 additions & 31 deletions x/distribution/keeper_test.go

This file was deleted.

Loading