Skip to content

Commit

Permalink
chore(ante): improve SpamPreventionDecorator devux (#1216)
Browse files Browse the repository at this point in the history
## Description

Improves DevUX of the oracle SpamPreventionDecorator

---

### 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 4, 2022
1 parent e41fa2c commit e1a6f1f
Showing 1 changed file with 27 additions and 50 deletions.
77 changes: 27 additions & 50 deletions ante/spam_prevention.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,63 +56,40 @@ func (spd *SpamPreventionDecorator) CheckOracleSpam(ctx sdk.Context, msgs []sdk.

curHeight := ctx.BlockHeight()
for _, msg := range msgs {
var err error
switch msg := msg.(type) {
case *oracletypes.MsgAggregateExchangeRatePrevote:
feederAddr, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return err
}

valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return err
}

err = spd.oracleKeeper.ValidateFeeder(ctx, feederAddr, valAddr)
if err != nil {
return err
}

if lastSubmittedHeight, ok := spd.oraclePrevoteMap[msg.Validator]; ok && lastSubmittedHeight == curHeight {
return sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
"validator has already submitted a pre-vote message at the current height",
)
}

spd.oraclePrevoteMap[msg.Validator] = curHeight
continue

err = spd.validate(ctx, msg.Feeder, msg.Validator, spd.oraclePrevoteMap, curHeight, "pre-vote")
case *oracletypes.MsgAggregateExchangeRateVote:
feederAddr, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return err
}

valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return err
}

err = spd.oracleKeeper.ValidateFeeder(ctx, feederAddr, valAddr)
if err != nil {
return err
}

if lastSubmittedHeight, ok := spd.oracleVoteMap[msg.Validator]; ok && lastSubmittedHeight == curHeight {
return sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
"validator has already submitted a vote message at the current height",
)
}

spd.oracleVoteMap[msg.Validator] = curHeight
continue

err = spd.validate(ctx, msg.Feeder, msg.Validator, spd.oracleVoteMap, curHeight, "vote")
default:
// non oracle msg: stop validation!
return nil
}
if err != nil {
return err
}
}

return nil
}

func (spd *SpamPreventionDecorator) validate(ctx sdk.Context, feeder, validator string, cache map[string]int64, curHeight int64, txType string) error {
feederAddr, err := sdk.AccAddressFromBech32(feeder)
if err != nil {
return err
}
valAddr, err := sdk.ValAddressFromBech32(validator)
if err != nil {
return err
}
if err = spd.oracleKeeper.ValidateFeeder(ctx, feederAddr, valAddr); err != nil {
return err
}
if lastSubmitted, ok := cache[validator]; ok && lastSubmitted == curHeight {
return sdkerrors.ErrInvalidRequest.Wrapf(
"validator has already submitted a %s message at the current height", txType)
}
cache[validator] = curHeight
return nil
}

0 comments on commit e1a6f1f

Please sign in to comment.