-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hygiene: add validate fn for Fee (#748)
* hygiene: add validate fn for Fee * Update modules/apps/29-fee/types/msgs.go Co-authored-by: Damian Nolan <damiannolan@gmail.com> * fix: error message * test: move Validate to fee.go & abstract out test * chore: remove test cases Co-authored-by: Damian Nolan <damiannolan@gmail.com>
- Loading branch information
1 parent
b16353e
commit b618f02
Showing
4 changed files
with
134 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package types | ||
|
||
import ( | ||
"strings" | ||
|
||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
// Validate asserts that each Fee is valid and all three Fees are not empty or zero | ||
func (fee Fee) Validate() error { | ||
var errFees []string | ||
if !fee.AckFee.IsValid() { | ||
errFees = append(errFees, "ack fee invalid") | ||
} | ||
if !fee.RecvFee.IsValid() { | ||
errFees = append(errFees, "recv fee invalid") | ||
} | ||
if !fee.TimeoutFee.IsValid() { | ||
errFees = append(errFees, "timeout fee invalid") | ||
} | ||
|
||
if len(errFees) > 0 { | ||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "contains invalid fees: %s", strings.Join(errFees, " , ")) | ||
} | ||
|
||
// 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, "all fees are zero") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestFeeValidation tests Validate | ||
func TestFeeValidation(t *testing.T) { | ||
var ( | ||
fee Fee | ||
ackFee sdk.Coins | ||
receiveFee sdk.Coins | ||
timeoutFee sdk.Coins | ||
) | ||
|
||
testCases := []struct { | ||
name string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"success", | ||
func() {}, | ||
true, | ||
}, | ||
{ | ||
"should fail when all fees are invalid", | ||
func() { | ||
ackFee = invalidCoins | ||
receiveFee = invalidCoins | ||
timeoutFee = invalidCoins | ||
}, | ||
false, | ||
}, | ||
{ | ||
"should fail with single invalid fee", | ||
func() { | ||
ackFee = invalidCoins | ||
}, | ||
false, | ||
}, | ||
{ | ||
"should fail with two invalid fees", | ||
func() { | ||
timeoutFee = invalidCoins | ||
ackFee = invalidCoins | ||
}, | ||
false, | ||
}, | ||
{ | ||
"should pass with two empty fees", | ||
func() { | ||
timeoutFee = sdk.Coins{} | ||
ackFee = sdk.Coins{} | ||
}, | ||
true, | ||
}, | ||
{ | ||
"should pass with one empty fee", | ||
func() { | ||
timeoutFee = sdk.Coins{} | ||
}, | ||
true, | ||
}, | ||
{ | ||
"should fail if all fees are empty", | ||
func() { | ||
ackFee = sdk.Coins{} | ||
receiveFee = sdk.Coins{} | ||
timeoutFee = sdk.Coins{} | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
// build message | ||
ackFee = validCoins | ||
receiveFee = validCoins | ||
timeoutFee = validCoins | ||
|
||
// malleate | ||
tc.malleate() | ||
fee = Fee{receiveFee, ackFee, timeoutFee} | ||
err := fee.Validate() | ||
|
||
if tc.expPass { | ||
require.NoError(t, err) | ||
} else { | ||
require.Error(t, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters