Skip to content

Commit

Permalink
hotfix: fixing marshaling of MsgWrappedCreateValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed May 30, 2023
1 parent 9943d8c commit 9832a76
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
10 changes: 8 additions & 2 deletions x/checkpointing/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
81 changes: 81 additions & 0 deletions x/checkpointing/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9832a76

Please sign in to comment.