diff --git a/codec/types/any.go b/codec/types/any.go index b879180481db..edd718cf69ab 100644 --- a/codec/types/any.go +++ b/codec/types/any.go @@ -1,6 +1,7 @@ package types import ( + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/gogo/protobuf/proto" ) @@ -98,6 +99,19 @@ func UnsafePackAny(x interface{}) *Any { return &Any{cachedValue: x} } +// PackAny is a checked and safe version of UnsafePackAny. It assures that +// `x` implements the proto.Message interface and uses it to serialize `x`. +func PackAny(x interface{}) (*Any, error) { + if intoany, ok := x.(IntoAny); ok { + return intoany.AsAny(), nil + } + protoMsg, ok := x.(proto.Message) + if !ok { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting %T to implement proto.Message", x) + } + return NewAnyWithValue(protoMsg) +} + // GetCachedValue returns the cached value from the Any if present func (any *Any) GetCachedValue() interface{} { return any.cachedValue diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index 9b9482883b40..e95e9cc5ba98 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -10,6 +10,7 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -19,11 +20,11 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234)) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) // create validator with 50% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidator(t, sdk.ValAddress(addrs[0]), valConsPk1, 100, commission) + sh.CreateValidator(t, sdk.ValAddress(addrs[0]), valConsPk1, 100, commission) val := app.StakingKeeper.Validator(ctx, valAddrs[0]) // allocate tokens @@ -48,15 +49,15 @@ func TestAllocateTokensToManyValidators(t *testing.T) { addrs := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1234)) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) // create validator with 50% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) + sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) // create second validator with 0% commission commission = stakingtypes.NewCommissionRates(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission) + sh.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission) abciValA := abci.Validator{ Address: valConsPk1.Address(), @@ -118,19 +119,19 @@ func TestAllocateTokensTruncation(t *testing.T) { addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234)) valAddrs := simapp.ConvertAddrsToValAddrs(addrs) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) // create validator with 10% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[0], valConsPk1, 110, commission) + sh.CreateValidator(t, valAddrs[0], valConsPk1, 110, commission) // create second validator with 10% commission commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission) + sh.CreateValidator(t, valAddrs[1], valConsPk2, 100, commission) // create third validator with 10% commission commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[2], valConsPk3, 100, commission) + sh.CreateValidator(t, valAddrs[2], valConsPk3, 100, commission) abciValA := abci.Validator{ Address: valConsPk1.Address(), diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index 4903999058c6..9153ad835315 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -9,20 +9,21 @@ import ( "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) func TestCalculateRewardsBasic(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) + sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -73,12 +74,12 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) // create validator with 50% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) valPower := int64(100) - sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) + sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -134,14 +135,14 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission valPower := int64(100) commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) + sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -209,13 +210,13 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(100000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) + sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -233,7 +234,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens) // second delegation - sk.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) + sh.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0]) // fetch updated validator @@ -275,7 +276,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) // set module account coins distrAcc := app.DistrKeeper.GetDistributionAccount(ctx) @@ -285,7 +286,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { // create validator with 50% commission power := int64(100) commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - valTokens := sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, power, commission) + valTokens := sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, power, commission) // assert correct initial balance expTokens := balanceTokens.Sub(valTokens) @@ -344,12 +345,12 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) // create validator with 50% commission valPower := int64(100) commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) + sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -410,14 +411,14 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) // create validator with 50% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) valPower := int64(100) - sk.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) + sh.CreateValidatorWithValPower(t, valAddrs[0], valConsPk1, valPower, commission) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -440,7 +441,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3) // second delegation - sk.DelegateWithPower(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) + sh.DelegateWithPower(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0]) @@ -484,7 +485,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { app := simapp.Setup(false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000)) valAddrs := simapp.ConvertAddrsToValAddrs(addr) initial := int64(20) @@ -499,7 +500,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { // create validator with 50% commission commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) + sh.CreateValidator(t, valAddrs[0], valConsPk1, 100, commission) // end block to bond validator staking.EndBlocker(ctx, app.StakingKeeper) @@ -518,7 +519,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { require.Equal(t, uint64(2), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx)) // second delegation - sk.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) + sh.Delegate(t, sdk.AccAddress(valAddrs[1]), valAddrs[0], 100) // historical count should be 3 (second delegation init) require.Equal(t, uint64(3), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx)) diff --git a/x/distribution/keeper/grpc_query_test.go b/x/distribution/keeper/grpc_query_test.go index 88a96140451b..24d9a72d1a06 100644 --- a/x/distribution/keeper/grpc_query_test.go +++ b/x/distribution/keeper/grpc_query_test.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -340,9 +341,9 @@ func (suite *KeeperTestSuite) TestGRPCValidatorSlashes() { func (suite *KeeperTestSuite) TestGRPCDelegationRewards() { app, ctx, addrs, valAddrs := suite.app, suite.ctx, suite.addrs, suite.valAddrs - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) comm := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidator(suite.T(), valAddrs[0], valConsPk1, 100, comm) + sh.CreateValidator(suite.T(), valAddrs[0], valConsPk1, 100, comm) staking.EndBlocker(ctx, app.StakingKeeper) ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1) diff --git a/x/distribution/keeper/querier_test.go b/x/distribution/keeper/querier_test.go index d619cf49bda5..6d4e281719f1 100644 --- a/x/distribution/keeper/querier_test.go +++ b/x/distribution/keeper/querier_test.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/distribution/keeper" "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -169,9 +170,9 @@ func TestQueries(t *testing.T) { require.Equal(t, []types.ValidatorSlashEvent{slashOne, slashTwo}, slashes) // test delegation rewards query - sk := NewHandlerT(ctx, app.StakingKeeper) + sh := teststaking.NewService(ctx, app.StakingKeeper) comm := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0)) - sk.CreateValidator(t, valOpAddr1, valConsPk1, 100, comm) + sh.CreateValidator(t, valOpAddr1, valConsPk1, 100, comm) staking.EndBlocker(ctx, app.StakingKeeper) diff --git a/x/slashing/handler_test.go b/x/slashing/handler_test.go index 416d2cc185de..94a4b5f4f98b 100644 --- a/x/slashing/handler_test.go +++ b/x/slashing/handler_test.go @@ -18,6 +18,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing/keeper" "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -59,6 +60,7 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) { ctx := app.BaseApp.NewContext(false, tmproto.Header{}) pks := simapp.CreateTestPubKeys(1) simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(200)) + ss := teststaking.NewHandlerT(ctx, app.StakingKeeper) slh := slashing.NewHandler(app.SlashingKeeper) amtInt := int64(100) diff --git a/x/staking/handler.go b/x/staking/handler.go index 6c647f9343a3..815d7b39e6e3 100644 --- a/x/staking/handler.go +++ b/x/staking/handler.go @@ -7,6 +7,7 @@ import ( gogotypes "github.com/gogo/protobuf/types" tmstrings "github.com/tendermint/tendermint/libs/strings" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -55,6 +56,7 @@ func handleMsgCreateValidator(ctx sdk.Context, msg *types.MsgCreateValidator, k return nil, types.ErrValidatorOwnerExists } + codectypes.PackAny(msg.Pubkey) pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, msg.Pubkey) if err != nil { return nil, err diff --git a/x/staking/teststaking/handler.go b/x/staking/teststaking/handler.go deleted file mode 100644 index 0a5b8ced3b0d..000000000000 --- a/x/staking/teststaking/handler.go +++ /dev/null @@ -1,64 +0,0 @@ -package teststaking - -import ( - "testing" - - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/crypto" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking" - "github.com/cosmos/cosmos-sdk/x/staking/keeper" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -// HandlerT is a structure which wraps the staking handler -// and provides methods useful in tests -type HandlerT struct { - ctx sdk.Context - h sdk.Handler -} - -// NewHandlerT creates staking Handler wrapper for tests -func NewHandlerT(ctx sdk.Context, k keeper.Keeper) *HandlerT { - return &HandlerT{ctx, staking.NewHandler(k)} -} - -// CreateValidator calls handler to create a new staking validator -func (sk *HandlerT) CreateValidator(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, stakeAmount int64, cr stakingtypes.CommissionRates) { - coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(stakeAmount)) - sk.createValidator(t, addr, pk, coin, cr) - -} - -// CreateValidatorWithValPower calls handler to create a new staking validator -func (sk *HandlerT) CreateValidatorWithValPower(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, valPower int64, cr stakingtypes.CommissionRates) sdk.Int { - amount := sdk.TokensFromConsensusPower(valPower) - coin := sdk.NewCoin(sdk.DefaultBondDenom, amount) - sk.createValidator(t, addr, pk, coin, cr) - return amount -} - -func (sk *HandlerT) createValidator(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, coin sdk.Coin, cr stakingtypes.CommissionRates) { - msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, cr, sdk.OneInt()) - require.NoError(t, err) - sk.handle(t, msg) -} - -func (sk *HandlerT) delegate(t *testing.T, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount int64) { - coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(amount)) - msg := stakingtypes.NewMsgDelegate(delAddr, valAddr, coin) - sk.handle(t, msg) -} - -func (sk *HandlerT) delegateWithPower(t *testing.T, delAddr sdk.AccAddress, valAddr sdk.ValAddress, power int64) { - coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(power)) - msg := stakingtypes.NewMsgDelegate(delAddr, valAddr, coin) - sk.handle(t, msg) -} - -func (sk *HandlerT) handle(t *testing.T, msg sdk.Msg) { - res, err := sk.h(sk.ctx, msg) - require.NoError(t, err) - require.NotNil(t, res) -} diff --git a/x/staking/teststaking/service.go b/x/staking/teststaking/service.go new file mode 100644 index 000000000000..0262ba25c0f7 --- /dev/null +++ b/x/staking/teststaking/service.go @@ -0,0 +1,67 @@ +package teststaking + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/staking" + "github.com/cosmos/cosmos-sdk/x/staking/keeper" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +// Service is a structure which wraps the staking handler +// and provides methods useful in tests +type Service struct { + ctx sdk.Context + h sdk.Handler +} + +// NewService creates staking Handler wrapper for tests +func NewService(ctx sdk.Context, k keeper.Keeper) *Service { + return &Service{ctx, staking.NewHandler(k)} +} + +// CreateValidator calls handler to create a new staking validator +func (sh *Service) CreateValidator(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, stakeAmount int64, cr stakingtypes.CommissionRates) { + coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(stakeAmount)) + sh.createValidator(t, addr, pk, coin, cr) + +} + +// CreateValidatorWithValPower calls handler to create a new staking validator +func (sh *Service) CreateValidatorWithValPower(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, valPower int64, cr stakingtypes.CommissionRates) sdk.Int { + amount := sdk.TokensFromConsensusPower(valPower) + coin := sdk.NewCoin(sdk.DefaultBondDenom, amount) + sh.createValidator(t, addr, pk, coin, cr) + return amount +} + +func (sh *Service) createValidator(t *testing.T, addr sdk.ValAddress, pk crypto.PubKey, coin sdk.Coin, cr stakingtypes.CommissionRates) { + msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, cr, sdk.OneInt()) + require.NoError(t, err) + sh.Handle(t, msg) +} + +// Delegate calls handler to delegate stake for a validator +func (sh *Service) Delegate(t *testing.T, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amount int64) { + coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(amount)) + msg := stakingtypes.NewMsgDelegate(delAddr, valAddr, coin) + sh.Handle(t, msg) +} + +// DelegateWithPower calls handler to delegate stake for a validator +func (sh *Service) DelegateWithPower(t *testing.T, delAddr sdk.AccAddress, valAddr sdk.ValAddress, power int64) { + coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(power)) + msg := stakingtypes.NewMsgDelegate(delAddr, valAddr, coin) + sh.Handle(t, msg) +} + +// Handle calls staking handler on a given message +func (sh *Service) Handle(t *testing.T, msg sdk.Msg) { + res, err := sh.h(sh.ctx, msg) + require.NoError(t, err) + require.NotNil(t, res) +} diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index 93eaf0f06c32..0703dbcb4ae4 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -3,7 +3,6 @@ package types import ( "bytes" - proto "github.com/gogo/protobuf/proto" "github.com/tendermint/tendermint/crypto" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -34,20 +33,15 @@ func NewMsgCreateValidator( valAddr sdk.ValAddress, pubKey crypto.PubKey, selfDelegation sdk.Coin, description Description, commission CommissionRates, minSelfDelegation sdk.Int, ) (*MsgCreateValidator, error) { - protoMsg, ok := pubKey.(proto.Message) - if !ok { - return nil, sdkerrors.ErrInvalidPubKey - } - pubKeyAny, err := codectypes.NewAnyWithValue(protoMsg) + pkAny, err := codectypes.PackAny(pubKey) if err != nil { return nil, err } - return &MsgCreateValidator{ Description: description, DelegatorAddress: sdk.AccAddress(valAddr).String(), ValidatorAddress: valAddr.String(), - Pubkey: pubKeyAny, + Pubkey: pkAny, Value: selfDelegation, Commission: commission, MinSelfDelegation: minSelfDelegation,