From 173973adaf04fd5c075b13b65c42973222e7a434 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 1 Sep 2022 01:58:48 +0200 Subject: [PATCH] fix: msg interface implementation (#1318) ## Description closes: #1314 --- ### Author Checklist _All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues._ I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] added appropriate labels to the PR - [ ] targeted the correct branch (see [PR Targeting](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist _All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items._ I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- x/leverage/types/tx.go | 14 ++--- x/oracle/simulations/operations.go | 97 +++++++++++------------------- x/oracle/types/keys.go | 3 - x/oracle/types/msgs.go | 28 ++------- x/oracle/types/msgs_test.go | 6 -- 5 files changed, 48 insertions(+), 100 deletions(-) diff --git a/x/leverage/types/tx.go b/x/leverage/types/tx.go index 86a44311c1..30fdb7a14a 100644 --- a/x/leverage/types/tx.go +++ b/x/leverage/types/tx.go @@ -14,7 +14,7 @@ func NewMsgSupply(supplier sdk.AccAddress, asset sdk.Coin) *MsgSupply { } } -func (msg MsgSupply) Route() string { return ModuleName } +func (msg MsgSupply) Route() string { return sdk.MsgTypeURL(&msg) } func (msg MsgSupply) Type() string { return sdk.MsgTypeURL(&msg) } func (msg *MsgSupply) ValidateBasic() error { @@ -38,7 +38,7 @@ func NewMsgWithdraw(supplier sdk.AccAddress, asset sdk.Coin) *MsgWithdraw { } } -func (msg MsgWithdraw) Route() string { return ModuleName } +func (msg MsgWithdraw) Route() string { return sdk.MsgTypeURL(&msg) } func (msg MsgWithdraw) Type() string { return sdk.MsgTypeURL(&msg) } func (msg *MsgWithdraw) ValidateBasic() error { @@ -62,7 +62,7 @@ func NewMsgCollateralize(borrower sdk.AccAddress, asset sdk.Coin) *MsgCollateral } } -func (msg MsgCollateralize) Route() string { return ModuleName } +func (msg MsgCollateralize) Route() string { return sdk.MsgTypeURL(&msg) } func (msg MsgCollateralize) Type() string { return sdk.MsgTypeURL(&msg) } func (msg *MsgCollateralize) ValidateBasic() error { @@ -86,7 +86,7 @@ func NewMsgDecollateralize(borrower sdk.AccAddress, asset sdk.Coin) *MsgDecollat } } -func (msg MsgDecollateralize) Route() string { return ModuleName } +func (msg MsgDecollateralize) Route() string { return sdk.MsgTypeURL(&msg) } func (msg MsgDecollateralize) Type() string { return sdk.MsgTypeURL(&msg) } func (msg *MsgDecollateralize) ValidateBasic() error { @@ -110,7 +110,7 @@ func NewMsgBorrow(borrower sdk.AccAddress, asset sdk.Coin) *MsgBorrow { } } -func (msg MsgBorrow) Route() string { return ModuleName } +func (msg MsgBorrow) Route() string { return sdk.MsgTypeURL(&msg) } func (msg MsgBorrow) Type() string { return sdk.MsgTypeURL(&msg) } func (msg *MsgBorrow) ValidateBasic() error { @@ -134,7 +134,7 @@ func NewMsgRepay(borrower sdk.AccAddress, asset sdk.Coin) *MsgRepay { } } -func (msg MsgRepay) Route() string { return ModuleName } +func (msg MsgRepay) Route() string { return sdk.MsgTypeURL(&msg) } func (msg MsgRepay) Type() string { return sdk.MsgTypeURL(&msg) } func (msg *MsgRepay) ValidateBasic() error { @@ -160,7 +160,7 @@ func NewMsgLiquidate(liquidator, borrower sdk.AccAddress, repayment sdk.Coin, re } } -func (msg MsgLiquidate) Route() string { return ModuleName } +func (msg MsgLiquidate) Route() string { return sdk.MsgTypeURL(&msg) } func (msg MsgLiquidate) Type() string { return sdk.MsgTypeURL(&msg) } func (msg *MsgLiquidate) ValidateBasic() error { diff --git a/x/oracle/simulations/operations.go b/x/oracle/simulations/operations.go index e2804bb304..224ae9b7b5 100644 --- a/x/oracle/simulations/operations.go +++ b/x/oracle/simulations/operations.go @@ -110,25 +110,20 @@ func SimulateMsgAggregateExchangeRatePrevote( ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { simAccount, _ := simtypes.RandomAcc(r, accs) address := sdk.ValAddress(simAccount.Address) + noop := func(comment string) simtypes.OperationMsg { + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgAggregateExchangeRatePrevote)), comment) + } // ensure the validator exists val := k.StakingKeeper.Validator(ctx, address) if val == nil || !val.IsBonded() { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRatePrevote, - "unable to find validator", - ), nil, nil + return noop("unable to find validator"), nil, nil } // check for an existing prevote _, err := k.GetAggregateExchangeRatePrevote(ctx, address) if err == nil { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRatePrevote, - "prevote already exists for this validator", - ), nil, nil + return noop("prevote already exists for this validator"), nil, nil } prices := make(map[string]sdk.Dec, len(acceptList)) @@ -147,16 +142,12 @@ func SimulateMsgAggregateExchangeRatePrevote( fees, err := simtypes.RandomFees(r, ctx, spendable) if err != nil { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRatePrevote, - "unable to generate fees", - ), nil, err + return noop("unable to generate fees"), nil, err } msg := types.NewMsgAggregateExchangeRatePrevote(voteHash, feederAddr, address) - - txGen := simappparams.MakeTestEncodingConfig().TxConfig + cfg := simappparams.MakeTestEncodingConfig() + txGen := cfg.TxConfig tx, err := helpers.GenSignedMockTx( r, txGen, @@ -169,17 +160,17 @@ func SimulateMsgAggregateExchangeRatePrevote( feederSimAccount.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err + return noop("unable to generate mock tx"), nil, err } _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err + return noop("unable to deliver tx"), nil, err } voteHashMap[address.String()] = exchangeRatesStr - return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil + return simtypes.NewOperationMsg(msg, true, "", cfg.Codec.(*codec.ProtoCodec)), nil, nil } } @@ -195,40 +186,31 @@ func SimulateMsgAggregateExchangeRateVote( ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { simAccount, _ := simtypes.RandomAcc(r, accs) address := sdk.ValAddress(simAccount.Address) + noop := func(comment string) simtypes.OperationMsg { + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgAggregateExchangeRateVote)), comment) + } // ensure the validator exists val := k.StakingKeeper.Validator(ctx, address) if val == nil || !val.IsBonded() { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRateVote, - "unable to find validator", - ), nil, nil + return noop("unable to find validator"), nil, nil } // ensure vote hash exists exchangeRatesStr, ok := voteHashMap[address.String()] if !ok { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRateVote, - "vote hash does not exist", - ), nil, nil + return noop("vote hash does not exist"), nil, nil } // get prevote prevote, err := k.GetAggregateExchangeRatePrevote(ctx, address) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgAggregateExchangeRateVote, "prevote not found"), nil, nil + return noop("prevote not found"), nil, nil } params := k.GetParams(ctx) if (uint64(ctx.BlockHeight())/params.VotePeriod)-(prevote.SubmitBlock/params.VotePeriod) != 1 { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRateVote, - "reveal period of submitted vote does not match with registered prevote", - ), nil, nil + return noop("reveal period of submitted vote does not match with registered prevote"), nil, nil } feederAddr, _ := k.GetFeederDelegation(ctx, address) @@ -238,16 +220,12 @@ func SimulateMsgAggregateExchangeRateVote( fees, err := simtypes.RandomFees(r, ctx, spendableCoins) if err != nil { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRateVote, - "unable to generate fees", - ), nil, err + return noop("unable to generate fees"), nil, err } msg := types.NewMsgAggregateExchangeRateVote(salt, exchangeRatesStr, feederAddr, address) - - txGen := simappparams.MakeTestEncodingConfig().TxConfig + cfg := simappparams.MakeTestEncodingConfig() + txGen := cfg.TxConfig tx, err := helpers.GenSignedMockTx( r, txGen, @@ -260,15 +238,15 @@ func SimulateMsgAggregateExchangeRateVote( feederSimAccount.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err + return noop("unable to generate mock tx"), nil, err } _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err + return noop("unable to deliver tx"), nil, err } - return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil + return simtypes.NewOperationMsg(msg, true, "", cfg.Codec.(*codec.ProtoCodec)), nil, nil } } @@ -282,36 +260,31 @@ func SimulateMsgDelegateFeedConsent(ak types.AccountKeeper, bk types.BankKeeper, valAddress := sdk.ValAddress(simAccount.Address) delegateValAddress := sdk.ValAddress(delegateAccount.Address) account := ak.GetAccount(ctx, simAccount.Address) + noop := func(comment string) simtypes.OperationMsg { + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(new(types.MsgDelegateFeedConsent)), comment) + } // ensure the validator exists val := k.StakingKeeper.Validator(ctx, valAddress) if val == nil { - return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgDelegateFeedConsent, "unable to find validator"), nil, nil + return noop("unable to find validator"), nil, nil } // ensure the target address is not a validator val2 := k.StakingKeeper.Validator(ctx, delegateValAddress) if val2 != nil { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgDelegateFeedConsent, - "unable to delegate to validator", - ), nil, nil + return noop("unable to delegate to validator"), nil, nil } spendableCoins := bk.SpendableCoins(ctx, account.GetAddress()) fees, err := simtypes.RandomFees(r, ctx, spendableCoins) if err != nil { - return simtypes.NoOpMsg( - types.ModuleName, - types.TypeMsgAggregateExchangeRateVote, - "unable to generate fees", - ), nil, err + return noop("unable to generate fees"), nil, err } msg := types.NewMsgDelegateFeedConsent(valAddress, delegateAccount.Address) - - txGen := simappparams.MakeTestEncodingConfig().TxConfig + cfg := simappparams.MakeTestEncodingConfig() + txGen := cfg.TxConfig tx, err := helpers.GenSignedMockTx( r, txGen, @@ -324,14 +297,14 @@ func SimulateMsgDelegateFeedConsent(ak types.AccountKeeper, bk types.BankKeeper, simAccount.PrivKey, ) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err + return noop("unable to generate mock tx"), nil, err } _, _, err = app.SimDeliver(txGen.TxEncoder(), tx) if err != nil { - return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err + return noop("unable to deliver tx"), nil, err } - return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil + return simtypes.NewOperationMsg(msg, true, "", cfg.Codec.(*codec.ProtoCodec)), nil, nil } } diff --git a/x/oracle/types/keys.go b/x/oracle/types/keys.go index 7f58ea0fbe..19282ff8a0 100644 --- a/x/oracle/types/keys.go +++ b/x/oracle/types/keys.go @@ -12,9 +12,6 @@ const ( // StoreKey is the string store representation StoreKey = ModuleName - // RouterKey is the msg router key for the oracle module - RouterKey = ModuleName - // QuerierRoute is the query router key for the oracle module QuerierRoute = ModuleName ) diff --git a/x/oracle/types/msgs.go b/x/oracle/types/msgs.go index 5de7c7246d..b2ab5f0e5d 100644 --- a/x/oracle/types/msgs.go +++ b/x/oracle/types/msgs.go @@ -14,13 +14,6 @@ var ( _ sdk.Msg = &MsgAggregateExchangeRateVote{} ) -// Messages types constants -const ( - TypeMsgDelegateFeedConsent = "delegate_feeder" - TypeMsgAggregateExchangeRatePrevote = "aggregate_exchange_rate_prevote" - TypeMsgAggregateExchangeRateVote = "aggregate_exchange_rate_vote" -) - func NewMsgAggregateExchangeRatePrevote( hash AggregateVoteHash, feeder sdk.AccAddress, @@ -33,11 +26,8 @@ func NewMsgAggregateExchangeRatePrevote( } } -// Route implements sdk.Msg -func (msg MsgAggregateExchangeRatePrevote) Route() string { return RouterKey } - -// Type implements sdk.Msg -func (msg MsgAggregateExchangeRatePrevote) Type() string { return TypeMsgAggregateExchangeRatePrevote } +// Type implements LegacyMsg interface +func (msg MsgAggregateExchangeRatePrevote) Type() string { return sdk.MsgTypeURL(&msg) } // GetSignBytes implements sdk.Msg func (msg MsgAggregateExchangeRatePrevote) GetSignBytes() []byte { @@ -88,11 +78,8 @@ func NewMsgAggregateExchangeRateVote( } } -// Route implements sdk.Msg -func (msg MsgAggregateExchangeRateVote) Route() string { return RouterKey } - -// Type implements sdk.Msg -func (msg MsgAggregateExchangeRateVote) Type() string { return TypeMsgAggregateExchangeRateVote } +// Type implements LegacyMsg interface +func (msg MsgAggregateExchangeRateVote) Type() string { return sdk.MsgTypeURL(&msg) } // GetSignBytes implements sdk.Msg func (msg MsgAggregateExchangeRateVote) GetSignBytes() []byte { @@ -153,11 +140,8 @@ func NewMsgDelegateFeedConsent(operatorAddress sdk.ValAddress, feederAddress sdk } } -// Route implements sdk.Msg -func (msg MsgDelegateFeedConsent) Route() string { return RouterKey } - -// Type implements sdk.Msg -func (msg MsgDelegateFeedConsent) Type() string { return TypeMsgDelegateFeedConsent } +// Type implements LegacyMsg interface +func (msg MsgDelegateFeedConsent) Type() string { return sdk.MsgTypeURL(&msg) } // GetSignBytes implements sdk.Msg func (msg MsgDelegateFeedConsent) GetSignBytes() []byte { diff --git a/x/oracle/types/msgs_test.go b/x/oracle/types/msgs_test.go index b7499ec6ae..e071a2a211 100644 --- a/x/oracle/types/msgs_test.go +++ b/x/oracle/types/msgs_test.go @@ -142,8 +142,6 @@ func TestNewMsgAggregateExchangeRatePrevote(t *testing.T) { vals[0], ) - require.Equal(t, aggregateExchangeRatePreVote.Route(), RouterKey) - require.Equal(t, aggregateExchangeRatePreVote.Type(), TypeMsgAggregateExchangeRatePrevote) require.NotNil(t, aggregateExchangeRatePreVote.GetSignBytes()) require.Equal(t, aggregateExchangeRatePreVote.GetSigners(), []sdk.AccAddress{feederAddr}) } @@ -159,8 +157,6 @@ func TestNewMsgAggregateExchangeRateVote(t *testing.T) { vals[0], ) - require.Equal(t, aggregateExchangeRateVote.Route(), RouterKey) - require.Equal(t, aggregateExchangeRateVote.Type(), TypeMsgAggregateExchangeRateVote) require.NotNil(t, aggregateExchangeRateVote.GetSignBytes()) require.Equal(t, aggregateExchangeRateVote.GetSigners(), []sdk.AccAddress{feederAddr}) } @@ -169,8 +165,6 @@ func TestMsgDelegateFeedConsent(t *testing.T) { vals := GenerateRandomValAddr(2) msgFeedConsent := NewMsgDelegateFeedConsent(vals[0], sdk.AccAddress(vals[1])) - require.Equal(t, msgFeedConsent.Route(), RouterKey) - require.Equal(t, msgFeedConsent.Type(), TypeMsgDelegateFeedConsent) require.NotNil(t, msgFeedConsent.GetSignBytes()) require.Equal(t, msgFeedConsent.GetSigners(), []sdk.AccAddress{sdk.AccAddress(vals[0])}) }