Merge PR #4511: Fix YAML encoding of staking.Validator

* Fix YAML encoding of staking.Validator

Closes: #4508

* Address fede's comment
This commit is contained in:
Alessio Treglia 2019-06-08 19:00:36 +01:00 committed by frog power 4000
parent db6cb238ab
commit b4eb4a9e64
2 changed files with 70 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
"gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -48,6 +49,39 @@ type Validator struct {
MinSelfDelegation sdk.Int `json:"min_self_delegation"` // validator's self declared minimum self delegation
}
func (v Validator) MarshalYAML() (interface{}, error) {
bs, err := yaml.Marshal(struct {
OperatorAddress sdk.ValAddress
ConsPubKey string
Jailed bool
Status sdk.BondStatus
Tokens sdk.Int
DelegatorShares sdk.Dec
Description Description
UnbondingHeight int64
UnbondingCompletionTime time.Time
Commission Commission
MinSelfDelegation sdk.Int
}{
OperatorAddress: v.OperatorAddress,
ConsPubKey: sdk.MustBech32ifyConsPub(v.ConsPubKey),
Jailed: v.Jailed,
Status: v.Status,
Tokens: v.Tokens,
DelegatorShares: v.DelegatorShares,
Description: v.Description,
UnbondingHeight: v.UnbondingHeight,
UnbondingCompletionTime: v.UnbondingCompletionTime,
Commission: v.Commission,
MinSelfDelegation: v.MinSelfDelegation,
})
if err != nil {
return nil, err
}
return string(bs), nil
}
// Validators is a collection of Validator
type Validators []Validator

View File

@ -1,14 +1,16 @@
package types
import (
"fmt"
"testing"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/types"
"gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestValidatorTestEquivalent(t *testing.T) {
@ -317,3 +319,34 @@ func TestValidatorSetInitialCommission(t *testing.T) {
}
}
}
func TestValidatorMarshalYAML(t *testing.T) {
validator := NewValidator(valAddr1, pk1, Description{})
bechifiedPub, err := sdk.Bech32ifyConsPub(validator.ConsPubKey)
require.NoError(t, err)
bs, err := yaml.Marshal(validator)
require.NoError(t, err)
want := fmt.Sprintf(`|
operatoraddress: %s
conspubkey: %s
jailed: false
status: 0
tokens: {}
delegatorshares: "0"
description:
moniker: ""
identity: ""
website: ""
details: ""
unbondingheight: 0
unbondingcompletiontime: 1970-01-01T00:00:00Z
commission:
commissionrates:
rate: "0"
maxrate: "0"
maxchangerate: "0"
updatetime: 1970-01-01T00:00:00Z
minselfdelegation: {}
`, validator.OperatorAddress.String(), bechifiedPub)
require.Equal(t, want, string(bs))
}