From 9832a76f8908c72547ada4a6a27375c9b462e446 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Tue, 30 May 2023 16:54:29 +1000 Subject: [PATCH] hotfix: fixing marshaling of MsgWrappedCreateValidator --- x/checkpointing/types/msgs.go | 10 +++- x/checkpointing/types/msgs_test.go | 81 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 x/checkpointing/types/msgs_test.go diff --git a/x/checkpointing/types/msgs.go b/x/checkpointing/types/msgs.go index aee9efd03..4e82fc200 100644 --- a/x/checkpointing/types/msgs.go +++ b/x/checkpointing/types/msgs.go @@ -3,12 +3,12 @@ package types import ( "errors" + "github.com/babylonchain/babylon/crypto/bls12381" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" ed255192 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - - "github.com/babylonchain/babylon/crypto/bls12381" ) var ( @@ -98,3 +98,9 @@ func (m *MsgWrappedCreateValidator) ValidateBasic() error { func (m *MsgWrappedCreateValidator) GetSigners() []sdk.AccAddress { return m.MsgCreateValidator.GetSigners() } + +// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces +// Needed since msg.MsgCreateValidator.Pubkey is in type Any +func (msg MsgWrappedCreateValidator) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + return msg.MsgCreateValidator.UnpackInterfaces(unpacker) +} diff --git a/x/checkpointing/types/msgs_test.go b/x/checkpointing/types/msgs_test.go new file mode 100644 index 000000000..d92a753e1 --- /dev/null +++ b/x/checkpointing/types/msgs_test.go @@ -0,0 +1,81 @@ +package types_test + +import ( + "testing" + + "cosmossdk.io/math" + appparams "github.com/babylonchain/babylon/app/params" + "github.com/babylonchain/babylon/crypto/bls12381" + "github.com/babylonchain/babylon/privval" + "github.com/babylonchain/babylon/x/checkpointing/types" + "github.com/cometbft/cometbft/crypto/ed25519" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/require" +) + +var ( + pk1 = ed25519.GenPrivKey().PubKey() + valAddr1 = sdk.ValAddress(pk1.Address()) +) + +func TestMsgDecode(t *testing.T) { + registry := codectypes.NewInterfaceRegistry() + cryptocodec.RegisterInterfaces(registry) + types.RegisterInterfaces(registry) + stakingtypes.RegisterInterfaces(registry) + cdc := codec.NewProtoCodec(registry) + + // build MsgWrappedCreateValidator + msg, err := buildMsgWrappedCreateValidatorWithAmount( + sdk.AccAddress(valAddr1), + sdk.TokensFromConsensusPower(10, sdk.DefaultPowerReduction), + ) + require.NoError(t, err) + + // marshal + msgBytes, err := cdc.MarshalInterface(msg) + require.NoError(t, err) + + // unmarshal to sdk.Msg interface + var msg2 sdk.Msg + err = cdc.UnmarshalInterface(msgBytes, &msg2) + require.NoError(t, err) + + // type assertion + msgWithType, ok := msg2.(*types.MsgWrappedCreateValidator) + require.True(t, ok) + + // ensure msgWithType.MsgCreateValidator.Pubkey with type Any is unmarshaled successfully + require.NotNil(t, msgWithType.MsgCreateValidator.Pubkey.GetCachedValue()) +} + +func buildMsgWrappedCreateValidatorWithAmount(addr sdk.AccAddress, bondTokens math.Int) (*types.MsgWrappedCreateValidator, error) { + tmValPrivkey := ed25519.GenPrivKey() + bondCoin := sdk.NewCoin(appparams.DefaultBondDenom, bondTokens) + description := stakingtypes.NewDescription("foo_moniker", "", "", "", "") + commission := stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) + + pk, err := cryptocodec.FromTmPubKeyInterface(tmValPrivkey.PubKey()) + if err != nil { + return nil, err + } + + createValidatorMsg, err := stakingtypes.NewMsgCreateValidator( + sdk.ValAddress(addr), pk, bondCoin, description, commission, sdk.OneInt(), + ) + if err != nil { + return nil, err + } + blsPrivKey := bls12381.GenPrivKey() + pop, err := privval.BuildPoP(tmValPrivkey, blsPrivKey) + if err != nil { + return nil, err + } + blsPubKey := blsPrivKey.PubKey() + + return types.NewMsgWrappedCreateValidator(createValidatorMsg, &blsPubKey, pop) +}