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

Staking additions for IBC #5584

Merged
merged 2 commits into from
Jan 29, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* (types) [\#5581](https://github.com/cosmos/cosmos-sdk/pull/5581) Add convenience functions {,Must}Bech32ifyAddressBytes.
* (staking) [\#5584](https://github.com/cosmos/cosmos-sdk/pull/5584) Add util function `ToTmValidator` that converts a `staking.Validator` type to `*tmtypes.Validator`.

## [v0.38.0] - 2020-01-23

Expand Down
6 changes: 3 additions & 3 deletions x/staking/types/historical_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
// HistoricalInfo contains the historical information that gets stored at each height
type HistoricalInfo struct {
Header abci.Header `json:"header" yaml:"header"`
ValSet []Validator `json:"valset" yaml:"valset"`
ValSet Validators `json:"valset" yaml:"valset"`
}

// NewHistoricalInfo will create a historical information struct from header and valset
// it will first sort valset before inclusion into historical info
func NewHistoricalInfo(header abci.Header, valSet []Validator) HistoricalInfo {
sort.Sort(Validators(valSet))
func NewHistoricalInfo(header abci.Header, valSet Validators) HistoricalInfo {
sort.Sort(valSet)
return HistoricalInfo{
Header: header,
ValSet: valSet,
Expand Down
14 changes: 14 additions & 0 deletions x/staking/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ func (v Validators) ToSDKValidators() (validators []exported.ValidatorI) {
return validators
}

// ToTmValidators casts all validators to the corresponding tendermint type.
func (v Validators) ToTmValidators() []*tmtypes.Validator {
validators := make([]*tmtypes.Validator, len(v))
for i, val := range v {
validators[i] = val.ToTmValidator()
}
return validators
}

// Sort Validators sorts validator array in ascending operator address order
func (v Validators) Sort() {
sort.Sort(v)
Expand Down Expand Up @@ -370,6 +379,11 @@ func (v Validator) ABCIValidatorUpdateZero() abci.ValidatorUpdate {
}
}

// ToTmValidator casts an SDK validator to a tendermint type Validator.
func (v Validator) ToTmValidator() *tmtypes.Validator {
return tmtypes.NewValidator(v.ConsPubKey, v.ConsensusPower())
}

// SetInitialCommission attempts to set a validator's initial commission. An
// error is returned if the commission is invalid.
func (v Validator) SetInitialCommission(commission Commission) (Validator, error) {
Expand Down
16 changes: 16 additions & 0 deletions x/staking/types/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,19 @@ func TestValidatorsSortDeterminism(t *testing.T) {
require.True(t, reflect.DeepEqual(sortedVals, vals), "Validator sort returned different slices")
}
}

func TestValidatorToTm(t *testing.T) {
vals := make(Validators, 10)
expected := make([]*tmtypes.Validator, 10)

for i := range vals {
pk := ed25519.GenPrivKey().PubKey()
val := NewValidator(sdk.ValAddress(pk.Address()), pk, Description{})
val.Status = sdk.Bonded
val.Tokens = sdk.NewInt(rand.Int63())
vals[i] = val
expected[i] = tmtypes.NewValidator(pk, val.ConsensusPower())
}

require.Equal(t, expected, vals.ToTmValidators())
}