From 55928ad16569de9bff9139efd89314e4510e7cc6 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 18 Jun 2019 18:03:16 +0200 Subject: [PATCH] Fix BaseAccount's YAML serialization (#4577) --- types/decimal.go | 3 +++ types/int.go | 3 +++ x/auth/types/account.go | 34 +++++++++++++++++++++++++++++++ x/auth/types/account_test.go | 1 - x/staking/types/validator_test.go | 12 +++++------ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index 86c9698c7..1a16cebc8 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -596,6 +596,9 @@ func (d *Dec) UnmarshalJSON(bz []byte) error { return nil } +// MarshalYAML returns Ythe AML representation. +func (d Dec) MarshalYAML() (interface{}, error) { return d.String(), nil } + //___________________________________________________________________________________ // helpers diff --git a/types/int.go b/types/int.go index 35b754daf..98770b4bd 100644 --- a/types/int.go +++ b/types/int.go @@ -355,6 +355,9 @@ func (i *Int) UnmarshalJSON(bz []byte) error { return unmarshalJSON(i.i, bz) } +// MarshalYAML returns Ythe AML representation. +func (i Int) MarshalYAML() (interface{}, error) { return i.String(), nil } + // intended to be used with require/assert: require.True(IntEq(...)) func IntEq(t *testing.T, exp, got Int) (*testing.T, bool, string, string, string) { return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String() diff --git a/x/auth/types/account.go b/x/auth/types/account.go index 1924f079c..fef41221b 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -6,6 +6,7 @@ import ( "time" "github.com/tendermint/tendermint/crypto" + yaml "gopkg.in/yaml.v2" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -188,6 +189,39 @@ func (acc *BaseAccount) SpendableCoins(_ time.Time) sdk.Coins { return acc.GetCoins() } +// MarshalYAML returns the YAML representation of an account. +func (acc BaseAccount) MarshalYAML() (interface{}, error) { + var bs []byte + var err error + var pubkey string + + if acc.PubKey != nil { + pubkey, err = sdk.Bech32ifyAccPub(acc.PubKey) + if err != nil { + return nil, err + } + } + + bs, err = yaml.Marshal(struct { + Address sdk.AccAddress + Coins sdk.Coins + PubKey string + AccountNumber uint64 + Sequence uint64 + }{ + Address: acc.Address, + Coins: acc.Coins, + PubKey: pubkey, + AccountNumber: acc.AccountNumber, + Sequence: acc.Sequence, + }) + if err != nil { + return nil, err + } + + return string(bs), err +} + //----------------------------------------------------------------------------- // Base Vesting Account diff --git a/x/auth/types/account_test.go b/x/auth/types/account_test.go index 581774c86..1ea24f470 100644 --- a/x/auth/types/account_test.go +++ b/x/auth/types/account_test.go @@ -5,7 +5,6 @@ import ( "time" "github.com/stretchr/testify/require" - tmtime "github.com/tendermint/tendermint/types/time" "github.com/cosmos/cosmos-sdk/codec" diff --git a/x/staking/types/validator_test.go b/x/staking/types/validator_test.go index d62cd07e3..af0842df0 100644 --- a/x/staking/types/validator_test.go +++ b/x/staking/types/validator_test.go @@ -331,8 +331,8 @@ func TestValidatorMarshalYAML(t *testing.T) { conspubkey: %s jailed: false status: 0 - tokens: {} - delegatorshares: "0" + tokens: "0" + delegatorshares: "0.000000000000000000" description: moniker: "" identity: "" @@ -342,11 +342,11 @@ func TestValidatorMarshalYAML(t *testing.T) { unbondingcompletiontime: 1970-01-01T00:00:00Z commission: commissionrates: - rate: "0" - maxrate: "0" - maxchangerate: "0" + rate: "0.000000000000000000" + maxrate: "0.000000000000000000" + maxchangerate: "0.000000000000000000" updatetime: 1970-01-01T00:00:00Z - minselfdelegation: {} + minselfdelegation: "1" `, validator.OperatorAddress.String(), bechifiedPub) require.Equal(t, want, string(bs)) }