Skip to content

Commit

Permalink
Merge PR cosmos#4511: Fix YAML encoding of staking.Validator
Browse files Browse the repository at this point in the history
* Fix YAML encoding of staking.Validator

Closes: cosmos#4508

* Address fede's comment
  • Loading branch information
alessio authored and rigelrozanski committed Jun 8, 2019
1 parent db6cb23 commit b4eb4a9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
34 changes: 34 additions & 0 deletions x/staking/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
39 changes: 36 additions & 3 deletions x/staking/types/validator_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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))
}

0 comments on commit b4eb4a9

Please sign in to comment.