From 160033414c5492e2990ad5328b571d31d254ac33 Mon Sep 17 00:00:00 2001 From: Rahul Dwivedi Date: Thu, 10 Oct 2019 00:16:08 +0530 Subject: [PATCH] Merge PR #5161: FIX: implements yaml marshaller in msg.go --- x/staking/types/msg.go | 26 +++++++++++++++++++++++ x/staking/types/msg_test.go | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index 80796a94e..84a3a809c 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -5,6 +5,7 @@ import ( "encoding/json" "github.com/tendermint/tendermint/crypto" + yaml "gopkg.in/yaml.v2" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -112,6 +113,31 @@ func (msg *MsgCreateValidator) UnmarshalJSON(bz []byte) error { return nil } +// custom marshal yaml function due to consensus pubkey +func (msg MsgCreateValidator) MarshalYAML() (interface{}, error) { + bs, err := yaml.Marshal(struct { + Commission CommissionRates + MinSelfDelegation sdk.Int + DelegatorAddress sdk.AccAddress + ValidatorAddress sdk.ValAddress + PubKey string + Value sdk.Coin + }{ + Commission: msg.Commission, + MinSelfDelegation: msg.MinSelfDelegation, + DelegatorAddress: msg.DelegatorAddress, + ValidatorAddress: msg.ValidatorAddress, + PubKey: sdk.MustBech32ifyConsPub(msg.PubKey), + Value: msg.Value, + }) + + if err != nil { + return nil, err + } + + return string(bs), nil +} + // GetSignBytes returns the message bytes to sign over. func (msg MsgCreateValidator) GetSignBytes() []byte { bz := ModuleCdc.MustMarshalJSON(msg) diff --git a/x/staking/types/msg_test.go b/x/staking/types/msg_test.go index 22914ec8a..d4d618300 100644 --- a/x/staking/types/msg_test.go +++ b/x/staking/types/msg_test.go @@ -1,8 +1,11 @@ package types import ( + "fmt" "testing" + yaml "gopkg.in/yaml.v2" + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto" @@ -154,3 +157,41 @@ func TestMsgUndelegate(t *testing.T) { } } } + +//test to validate if NewMsgCreateValidator implements yaml marshaller +func TestMsgMarshalYAML(t *testing.T) { + commission1 := NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) + tc := struct { + name, moniker, identity, website, securityContact, details string + CommissionRates CommissionRates + minSelfDelegation sdk.Int + validatorAddr sdk.ValAddress + pubkey crypto.PubKey + bond sdk.Coin + expectPass bool + }{"basic good", "a", "b", "c", "d", "e", commission1, sdk.OneInt(), valAddr1, pk1, coinPos, true} + + description := NewDescription(tc.moniker, tc.identity, tc.website, tc.securityContact, tc.details) + msg := NewMsgCreateValidator(tc.validatorAddr, tc.pubkey, tc.bond, description, tc.CommissionRates, tc.minSelfDelegation) + bs, err := yaml.Marshal(msg) + require.NoError(t, err) + bechifiedPub, err := sdk.Bech32ifyConsPub(msg.PubKey) + require.NoError(t, err) + + want := fmt.Sprintf(`| + commission: + rate: "0.000000000000000000" + max_rate: "0.000000000000000000" + max_change_rate: "0.000000000000000000" + minselfdelegation: "1" + delegatoraddress: %s + validatoraddress: %s + pubkey: %s + value: + denom: stake + amount: "1000" +`, msg.DelegatorAddress, msg.ValidatorAddress, bechifiedPub) + + require.Equal(t, want, string(bs)) + +}