Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hygiene: add validate fn for Fee #748

Merged
merged 8 commits into from
Jan 21, 2022
39 changes: 22 additions & 17 deletions modules/apps/29-fee/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,9 @@ func (msg MsgPayPacketFee) ValidateBasic() error {
return ErrRelayersNotNil
}

// if any of the fee's are invalid return an error
if !msg.Fee.AckFee.IsValid() || !msg.Fee.RecvFee.IsValid() || !msg.Fee.TimeoutFee.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "contains one or more invalid fees")
}

// if all three fee's are zero or empty return an error
if msg.Fee.AckFee.IsZero() && msg.Fee.RecvFee.IsZero() && msg.Fee.TimeoutFee.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "contains one or more invalid fees")
// validate Fee
seantking marked this conversation as resolved.
Show resolved Hide resolved
if err := msg.Fee.Validate(); err != nil {
return err
}

return nil
Expand Down Expand Up @@ -154,19 +149,29 @@ func (fee IdentifiedPacketFee) Validate() error {
return sdkerrors.Wrap(err, "failed to convert RefundAddress into sdk.AccAddress")
}

// if any of the fee's are invalid return an error
if !fee.Fee.AckFee.IsValid() || !fee.Fee.RecvFee.IsValid() || !fee.Fee.TimeoutFee.IsValid() {
return sdkerrors.ErrInvalidCoins
// enforce relayer is nil
if fee.Relayers != nil {
return ErrRelayersNotNil
}

// if all three fee's are zero or empty return an error
if fee.Fee.AckFee.IsZero() && fee.Fee.RecvFee.IsZero() && fee.Fee.TimeoutFee.IsZero() {
return sdkerrors.ErrInvalidCoins
// validate Fee
seantking marked this conversation as resolved.
Show resolved Hide resolved
if err := fee.Fee.Validate(); err != nil {
return err
}

// enforce relayer is nil
if fee.Relayers != nil {
return ErrRelayersNotNil
return nil
}

// Validate asserts that each Fee is valid and all three Fees are not empty or zero
func (fee Fee) Validate() error {
seantking marked this conversation as resolved.
Show resolved Hide resolved
// if any of the fee's are invalid return an error
if !fee.AckFee.IsValid() || !fee.RecvFee.IsValid() || !fee.TimeoutFee.IsValid() {
Copy link
Contributor

@crodriguezvega crodriguezvega Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion if we would like to specify what fees are invalid. Feel free to change the text for each error fee.

var errFees []string 
if !fee.AckFee.IsValid() {
	errFees = append(errFees, "ack fee")
}
if !fee.RecvFee.IsValid() {
	errFees = append(errFees, "recv fee")
}
if !fee.TimeoutFee.IsValid() {
	errFees = append(errFees, "timeout fee")
}

if len(errFees) > 0 {
	return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "contains invalid fees: %s", strings.Join(errFees, " , "))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good suggestion.

return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "contains one or more invalid fees")
}

// if all three fee's are zero or empty return an error
if fee.AckFee.IsZero() && fee.RecvFee.IsZero() && fee.TimeoutFee.IsZero() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "contains one or more invalid fees")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the fees are zero or empty.

Suggested change
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "contains one or more invalid fees")
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "contains all invalid fees")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change the error message to be all fees are zero

}

return nil
Expand Down