From 9bf3815d9fbaeb24b9bb468b1fdb9d6997ec63cd Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Wed, 29 Jan 2020 10:59:53 +0100 Subject: [PATCH 1/2] Staking additions for IBC --- x/staking/types/historical_info.go | 6 +++--- x/staking/types/validator.go | 14 ++++++++++++++ x/staking/types/validator_test.go | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/x/staking/types/historical_info.go b/x/staking/types/historical_info.go index 78db1e7dd4a9..1c08caebcde6 100644 --- a/x/staking/types/historical_info.go +++ b/x/staking/types/historical_info.go @@ -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, diff --git a/x/staking/types/validator.go b/x/staking/types/validator.go index d64e0ce3e751..855ed988d290 100644 --- a/x/staking/types/validator.go +++ b/x/staking/types/validator.go @@ -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) @@ -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) { diff --git a/x/staking/types/validator_test.go b/x/staking/types/validator_test.go index e6a7ccbe961a..b8a572444e0c 100644 --- a/x/staking/types/validator_test.go +++ b/x/staking/types/validator_test.go @@ -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()) +} From cce6edcbdda8e0e4b03a962621de3211c914cbf7 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Wed, 29 Jan 2020 11:06:22 +0100 Subject: [PATCH 2/2] CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dac8e9486a10..a0bbca18522d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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