Skip to content

Commit

Permalink
Merge PR cosmos#5584: Staking additions for IBC
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze authored Jan 29, 2020
1 parent deea89e commit ec35cf2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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())
}

0 comments on commit ec35cf2

Please sign in to comment.