github.com/cosmos/cosmos-sdk@v0.50.10/x/staking/types/msg_test.go (about)

     1  package types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"cosmossdk.io/math"
     9  
    10  	"github.com/cosmos/cosmos-sdk/codec"
    11  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    12  	cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
    13  	"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
    14  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    15  	sdk "github.com/cosmos/cosmos-sdk/types"
    16  	"github.com/cosmos/cosmos-sdk/x/staking/types"
    17  )
    18  
    19  var coinPos = sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000)
    20  
    21  func TestMsgDecode(t *testing.T) {
    22  	registry := codectypes.NewInterfaceRegistry()
    23  	cryptocodec.RegisterInterfaces(registry)
    24  	types.RegisterInterfaces(registry)
    25  	cdc := codec.NewProtoCodec(registry)
    26  
    27  	// firstly we start testing the pubkey serialization
    28  
    29  	pk1bz, err := cdc.MarshalInterface(pk1)
    30  	require.NoError(t, err)
    31  	var pkUnmarshaled cryptotypes.PubKey
    32  	err = cdc.UnmarshalInterface(pk1bz, &pkUnmarshaled)
    33  	require.NoError(t, err)
    34  	require.True(t, pk1.Equals(pkUnmarshaled.(*ed25519.PubKey)))
    35  
    36  	// now let's try to serialize the whole message
    37  
    38  	commission1 := types.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec())
    39  	msg, err := types.NewMsgCreateValidator(valAddr1.String(), pk1, coinPos, types.Description{}, commission1, math.OneInt())
    40  	require.NoError(t, err)
    41  	msgSerialized, err := cdc.MarshalInterface(msg)
    42  	require.NoError(t, err)
    43  
    44  	var msgUnmarshaled sdk.Msg
    45  	err = cdc.UnmarshalInterface(msgSerialized, &msgUnmarshaled)
    46  	require.NoError(t, err)
    47  	msg2, ok := msgUnmarshaled.(*types.MsgCreateValidator)
    48  	require.True(t, ok)
    49  	require.True(t, msg.Value.IsEqual(msg2.Value))
    50  	require.True(t, msg.Pubkey.Equal(msg2.Pubkey))
    51  }