Skip to content

Commit

Permalink
fix: msg interface implementation (#1318)
Browse files Browse the repository at this point in the history
## 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)
  • Loading branch information
robert-zaremba authored Aug 31, 2022
1 parent 2180ff1 commit 173973a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 100 deletions.
14 changes: 7 additions & 7 deletions x/leverage/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
97 changes: 35 additions & 62 deletions x/oracle/simulations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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,
Expand All @@ -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
}
}

Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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
}
}

Expand All @@ -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,
Expand All @@ -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
}
}
3 changes: 0 additions & 3 deletions x/oracle/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
28 changes: 6 additions & 22 deletions x/oracle/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions x/oracle/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand All @@ -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})
}
Expand All @@ -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])})
}

0 comments on commit 173973a

Please sign in to comment.