Merge PR #5161: FIX: implements yaml marshaller in msg.go

This commit is contained in:
Rahul Dwivedi 2019-10-10 00:16:08 +05:30 committed by Alexander Bezobchuk
parent 1a33f1c8cb
commit 160033414c
2 changed files with 67 additions and 0 deletions

View File

@ -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)

View File

@ -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))
}