From b4eb4a9e64fdc38c6f149400ff28161f8ef9fbf8 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Sat, 8 Jun 2019 19:00:36 +0100 Subject: [PATCH] Merge PR #4511: Fix YAML encoding of staking.Validator * Fix YAML encoding of staking.Validator Closes: #4508 * Address fede's comment --- x/staking/types/validator.go | 34 +++++++++++++++++++++++++++ x/staking/types/validator_test.go | 39 ++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/x/staking/types/validator.go b/x/staking/types/validator.go index d97a2039d..598426e15 100644 --- a/x/staking/types/validator.go +++ b/x/staking/types/validator.go @@ -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 diff --git a/x/staking/types/validator_test.go b/x/staking/types/validator_test.go index 54d16716d..d62cd07e3 100644 --- a/x/staking/types/validator_test.go +++ b/x/staking/types/validator_test.go @@ -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)) +}