refactor: remove .Type() and .Route() from msgs (#14751)

This commit is contained in:
Julien Robert
2023-01-24 17:03:06 +00:00
committed by GitHub
parent bc0386e996
commit 8dbdfea9ef
48 changed files with 409 additions and 769 deletions
+10 -12
View File
@@ -91,7 +91,7 @@ func SimulateMsgSetWithdrawAddress(txConfig client.TxConfig, ak types.AccountKee
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
if !k.GetWithdrawAddrEnabled(ctx) {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgSetWithdrawAddress, "withdrawal is not enabled"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgSetWithdrawAddress{}), "withdrawal is not enabled"), nil, nil
}
simAccount, _ := simtypes.RandomAcc(r, accs)
@@ -108,7 +108,6 @@ func SimulateMsgSetWithdrawAddress(txConfig client.TxConfig, ak types.AccountKee
TxGen: txConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
@@ -129,14 +128,14 @@ func SimulateMsgWithdrawDelegatorReward(txConfig client.TxConfig, ak types.Accou
simAccount, _ := simtypes.RandomAcc(r, accs)
delegations := sk.GetAllDelegatorDelegations(ctx, simAccount.Address)
if len(delegations) == 0 {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawDelegatorReward, "number of delegators equal 0"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgWithdrawDelegatorReward{}), "number of delegators equal 0"), nil, nil
}
delegation := delegations[r.Intn(len(delegations))]
validator := sk.Validator(ctx, delegation.GetValidatorAddr())
if validator == nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawDelegatorReward, "validator is nil"), nil, fmt.Errorf("validator %s not found", delegation.GetValidatorAddr())
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgWithdrawDelegatorReward{}), "validator is nil"), nil, fmt.Errorf("validator %s not found", delegation.GetValidatorAddr())
}
account := ak.GetAccount(ctx, simAccount.Address)
@@ -150,7 +149,6 @@ func SimulateMsgWithdrawDelegatorReward(txConfig client.TxConfig, ak types.Accou
TxGen: txConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
@@ -168,19 +166,21 @@ func SimulateMsgWithdrawValidatorCommission(txConfig client.TxConfig, ak types.A
return func(
r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&types.MsgWithdrawValidatorCommission{})
validator, ok := testutil.RandSliceElem(r, sk.GetAllValidators(ctx))
if !ok {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawValidatorCommission, "random validator is not ok"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, msgType, "random validator is not ok"), nil, nil
}
commission := k.GetValidatorAccumulatedCommission(ctx, validator.GetOperator())
if commission.Commission.IsZero() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawValidatorCommission, "validator commission is zero"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, msgType, "validator commission is zero"), nil, nil
}
simAccount, found := simtypes.FindAccount(accs, sdk.AccAddress(validator.GetOperator()))
if !found {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgWithdrawValidatorCommission, "could not find account"), nil, fmt.Errorf("validator %s not found", validator.GetOperator())
return simtypes.NoOpMsg(types.ModuleName, msgType, "could not find account"), nil, fmt.Errorf("validator %s not found", validator.GetOperator())
}
account := ak.GetAccount(ctx, simAccount.Address)
@@ -194,7 +194,6 @@ func SimulateMsgWithdrawValidatorCommission(txConfig client.TxConfig, ak types.A
TxGen: txConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
@@ -220,7 +219,7 @@ func SimulateMsgFundCommunityPool(txConfig client.TxConfig, ak types.AccountKeep
fundAmount := simtypes.RandSubsetCoins(r, spendable)
if fundAmount.Empty() {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgFundCommunityPool, "fund amount is empty"), nil, nil
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgFundCommunityPool{}), "fund amount is empty"), nil, nil
}
var (
@@ -232,7 +231,7 @@ func SimulateMsgFundCommunityPool(txConfig client.TxConfig, ak types.AccountKeep
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgFundCommunityPool, "unable to generate fees"), nil, err
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgFundCommunityPool{}), "unable to generate fees"), nil, err
}
}
@@ -244,7 +243,6 @@ func SimulateMsgFundCommunityPool(txConfig client.TxConfig, ak types.AccountKeep
TxGen: txConfig,
Cdc: nil,
Msg: msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: funder,
AccountKeeper: ak,
+8 -12
View File
@@ -43,10 +43,10 @@ func (suite *SimTestSuite) TestWeightedOperations() {
opMsgRoute string
opMsgName string
}{
{simulation.DefaultWeightMsgSetWithdrawAddress, types.ModuleName, types.TypeMsgSetWithdrawAddress},
{simulation.DefaultWeightMsgWithdrawDelegationReward, types.ModuleName, types.TypeMsgWithdrawDelegatorReward},
{simulation.DefaultWeightMsgWithdrawValidatorCommission, types.ModuleName, types.TypeMsgWithdrawValidatorCommission},
{simulation.DefaultWeightMsgFundCommunityPool, types.ModuleName, types.TypeMsgFundCommunityPool},
{simulation.DefaultWeightMsgSetWithdrawAddress, types.ModuleName, sdk.MsgTypeURL(&types.MsgSetWithdrawAddress{})},
{simulation.DefaultWeightMsgWithdrawDelegationReward, types.ModuleName, sdk.MsgTypeURL(&types.MsgWithdrawDelegatorReward{})},
{simulation.DefaultWeightMsgWithdrawValidatorCommission, types.ModuleName, sdk.MsgTypeURL(&types.MsgWithdrawValidatorCommission{})},
{simulation.DefaultWeightMsgFundCommunityPool, types.ModuleName, sdk.MsgTypeURL(&types.MsgFundCommunityPool{})},
}
for i, w := range weightesOps {
@@ -84,8 +84,7 @@ func (suite *SimTestSuite) TestSimulateMsgSetWithdrawAddress() {
suite.Require().True(operationMsg.OK)
suite.Require().Equal("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.DelegatorAddress)
suite.Require().Equal("cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.WithdrawAddress)
suite.Require().Equal(types.TypeMsgSetWithdrawAddress, msg.Type())
suite.Require().Equal(types.ModuleName, msg.Route())
suite.Require().Equal(sdk.MsgTypeURL(&types.MsgSetWithdrawAddress{}), sdk.MsgTypeURL(&msg))
suite.Require().Len(futureOperations, 0)
}
@@ -125,8 +124,7 @@ func (suite *SimTestSuite) TestSimulateMsgWithdrawDelegatorReward() {
suite.Require().True(operationMsg.OK)
suite.Require().Equal("cosmosvaloper1l4s054098kk9hmr5753c6k3m2kw65h686d3mhr", msg.ValidatorAddress)
suite.Require().Equal("cosmos1d6u7zhjwmsucs678d7qn95uqajd4ucl9jcjt26", msg.DelegatorAddress)
suite.Require().Equal(types.TypeMsgWithdrawDelegatorReward, msg.Type())
suite.Require().Equal(types.ModuleName, msg.Route())
suite.Require().Equal(sdk.MsgTypeURL(&types.MsgWithdrawDelegatorReward{}), sdk.MsgTypeURL(&msg))
suite.Require().Len(futureOperations, 0)
}
@@ -185,8 +183,7 @@ func (suite *SimTestSuite) testSimulateMsgWithdrawValidatorCommission(tokenName
suite.Require().True(operationMsg.OK)
suite.Require().Equal("cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", msg.ValidatorAddress)
suite.Require().Equal(types.TypeMsgWithdrawValidatorCommission, msg.Type())
suite.Require().Equal(types.ModuleName, msg.Route())
suite.Require().Equal(sdk.MsgTypeURL(&types.MsgWithdrawValidatorCommission{}), sdk.MsgTypeURL(&msg))
suite.Require().Len(futureOperations, 0)
}
}
@@ -213,8 +210,7 @@ func (suite *SimTestSuite) TestSimulateMsgFundCommunityPool() {
suite.Require().True(operationMsg.OK)
suite.Require().Equal("4896096stake", msg.Amount.String())
suite.Require().Equal("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.Depositor)
suite.Require().Equal(types.TypeMsgFundCommunityPool, msg.Type())
suite.Require().Equal(types.ModuleName, msg.Route())
suite.Require().Equal(sdk.MsgTypeURL(&types.MsgFundCommunityPool{}), sdk.MsgTypeURL(&msg))
suite.Require().Len(futureOperations, 0)
}
+8 -44
View File
@@ -5,17 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// distribution message types
const (
TypeMsgSetWithdrawAddress = "set_withdraw_address"
TypeMsgWithdrawDelegatorReward = "withdraw_delegator_reward"
TypeMsgWithdrawValidatorCommission = "withdraw_validator_commission"
TypeMsgFundCommunityPool = "fund_community_pool"
TypeMsgUpdateParams = "update_params"
TypeMsgCommunityPoolSpend = "community_pool_spend"
TypeMsgDepositValidatorRewardsPool = "deposit_validator_rewards_pool"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)
// Verify interface at compile time
@@ -26,6 +16,13 @@ var (
_ sdk.Msg = (*MsgUpdateParams)(nil)
_ sdk.Msg = (*MsgCommunityPoolSpend)(nil)
_ sdk.Msg = (*MsgDepositValidatorRewardsPool)(nil)
_ legacytx.LegacyMsg = (*MsgSetWithdrawAddress)(nil)
_ legacytx.LegacyMsg = (*MsgWithdrawDelegatorReward)(nil)
_ legacytx.LegacyMsg = (*MsgWithdrawValidatorCommission)(nil)
_ legacytx.LegacyMsg = (*MsgUpdateParams)(nil)
_ legacytx.LegacyMsg = (*MsgCommunityPoolSpend)(nil)
_ legacytx.LegacyMsg = (*MsgDepositValidatorRewardsPool)(nil)
)
func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) *MsgSetWithdrawAddress {
@@ -35,9 +32,6 @@ func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) *MsgSetWithd
}
}
func (msg MsgSetWithdrawAddress) Route() string { return ModuleName }
func (msg MsgSetWithdrawAddress) Type() string { return TypeMsgSetWithdrawAddress }
// Return address that must sign over msg.GetSignBytes()
func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress {
delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress)
@@ -69,9 +63,6 @@ func NewMsgWithdrawDelegatorReward(delAddr sdk.AccAddress, valAddr sdk.ValAddres
}
}
func (msg MsgWithdrawDelegatorReward) Route() string { return ModuleName }
func (msg MsgWithdrawDelegatorReward) Type() string { return TypeMsgWithdrawDelegatorReward }
// Return address that must sign over msg.GetSignBytes()
func (msg MsgWithdrawDelegatorReward) GetSigners() []sdk.AccAddress {
delegator, _ := sdk.AccAddressFromBech32(msg.DelegatorAddress)
@@ -101,9 +92,6 @@ func NewMsgWithdrawValidatorCommission(valAddr sdk.ValAddress) *MsgWithdrawValid
}
}
func (msg MsgWithdrawValidatorCommission) Route() string { return ModuleName }
func (msg MsgWithdrawValidatorCommission) Type() string { return TypeMsgWithdrawValidatorCommission }
// Return address that must sign over msg.GetSignBytes()
func (msg MsgWithdrawValidatorCommission) GetSigners() []sdk.AccAddress {
valAddr, _ := sdk.ValAddressFromBech32(msg.ValidatorAddress)
@@ -133,12 +121,6 @@ func NewMsgFundCommunityPool(amount sdk.Coins, depositor sdk.AccAddress) *MsgFun
}
}
// Route returns the MsgFundCommunityPool message route.
func (msg MsgFundCommunityPool) Route() string { return ModuleName }
// Type returns the MsgFundCommunityPool message type.
func (msg MsgFundCommunityPool) Type() string { return TypeMsgFundCommunityPool }
// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
func (msg MsgFundCommunityPool) GetSigners() []sdk.AccAddress {
@@ -164,12 +146,6 @@ func (msg MsgFundCommunityPool) ValidateBasic() error {
return nil
}
// Route returns the MsgUpdateParams message route.
func (msg MsgUpdateParams) Route() string { return ModuleName }
// Type returns the MsgUpdateParams message type.
func (msg MsgUpdateParams) Type() string { return TypeMsgUpdateParams }
// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
func (msg MsgUpdateParams) GetSigners() []sdk.AccAddress {
@@ -198,12 +174,6 @@ func (msg MsgUpdateParams) ValidateBasic() error {
return msg.Params.ValidateBasic()
}
// Route returns the MsgCommunityPoolSpend message route.
func (msg MsgCommunityPoolSpend) Route() string { return ModuleName }
// Type returns the MsgCommunityPoolSpend message type.
func (msg MsgCommunityPoolSpend) Type() string { return TypeMsgCommunityPoolSpend }
// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes, which is the authority.
func (msg MsgCommunityPoolSpend) GetSigners() []sdk.AccAddress {
@@ -237,12 +207,6 @@ func NewMsgDepositValidatorRewardsPool(depositor sdk.AccAddress, valAddr sdk.Val
}
}
// Route returns the MsgDepositValidatorRewardsPool message route.
func (msg MsgDepositValidatorRewardsPool) Route() string { return ModuleName }
// Type returns the MsgDepositValidatorRewardsPool message type.
func (msg MsgDepositValidatorRewardsPool) Type() string { return TypeMsgDepositValidatorRewardsPool }
// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes, which is the authority.
func (msg MsgDepositValidatorRewardsPool) GetSigners() []sdk.AccAddress {