Skip to content

Commit

Permalink
Merge branch 'damian/query-total-ack-fees' into damian/query-total-ti…
Browse files Browse the repository at this point in the history
…meout-fees
  • Loading branch information
damiannolan committed Mar 1, 2022
2 parents eb8e6c7 + 6d8a5bc commit 6d3d67a
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 227 deletions.
2 changes: 1 addition & 1 deletion docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ QueryTotalAckFeesResponse defines the response type for the TotalAckFees rpc

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `ack_fees` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | the total packet receive fees |
| `ack_fees` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | repeated | the total packet acknowledgement fees |



Expand Down
84 changes: 50 additions & 34 deletions modules/apps/29-fee/types/fee_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package types
package types_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
)

var (
// defaultRecvFee is the default packet receive fee used for testing purposes
defaultRecvFee = sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)})

// defaultAckFee is the default packet acknowledgement fee used for testing purposes
defaultAckFee = sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(200)})

// defaultTimeoutFee is the default packet timeout fee used for testing purposes
defaultTimeoutFee = sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(300)})

// invalidFee is an invalid coin set used to trigger error cases for testing purposes
invalidFee = sdk.Coins{sdk.Coin{Denom: "invalid-denom", Amount: sdk.NewInt(-2)}}

// defaultAccAddress is the default account used for testing purposes
defaultAccAddress = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String()
)

func TestFeeTotal(t *testing.T) {
fee := Fee{
AckFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}),
RecvFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}),
TimeoutFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}),
}
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)

total := fee.Total()
require.Equal(t, sdk.NewInt(300), total.AmountOf(sdk.DefaultBondDenom))
require.Equal(t, sdk.NewInt(600), total.AmountOf(sdk.DefaultBondDenom))
}

// TestFeeValidation tests Validate
func TestFeeValidation(t *testing.T) {
func TestPacketFeeValidation(t *testing.T) {
var (
fee Fee
ackFee sdk.Coins
receiveFee sdk.Coins
timeoutFee sdk.Coins
packetFee types.PacketFee
)

testCases := []struct {
Expand All @@ -37,66 +49,70 @@ func TestFeeValidation(t *testing.T) {
func() {},
true,
},
{
"should fail when refund address is invalid",
func() {
packetFee.RefundAddress = "invalid-address"
},
false,
},
{
"should fail when all fees are invalid",
func() {
ackFee = invalidCoins
receiveFee = invalidCoins
timeoutFee = invalidCoins
packetFee.Fee.AckFee = invalidFee
packetFee.Fee.RecvFee = invalidFee
packetFee.Fee.TimeoutFee = invalidFee
},
false,
},
{
"should fail with single invalid fee",
func() {
ackFee = invalidCoins
packetFee.Fee.AckFee = invalidFee
},
false,
},
{
"should fail with two invalid fees",
func() {
timeoutFee = invalidCoins
ackFee = invalidCoins
packetFee.Fee.TimeoutFee = invalidFee
packetFee.Fee.AckFee = invalidFee
},
false,
},
{
"should pass with two empty fees",
func() {
timeoutFee = sdk.Coins{}
ackFee = sdk.Coins{}
packetFee.Fee.TimeoutFee = sdk.Coins{}
packetFee.Fee.AckFee = sdk.Coins{}
},
true,
},
{
"should pass with one empty fee",
func() {
timeoutFee = sdk.Coins{}
packetFee.Fee.TimeoutFee = sdk.Coins{}
},
true,
},
{
"should fail if all fees are empty",
func() {
ackFee = sdk.Coins{}
receiveFee = sdk.Coins{}
timeoutFee = sdk.Coins{}
packetFee.Fee.AckFee = sdk.Coins{}
packetFee.Fee.RecvFee = sdk.Coins{}
packetFee.Fee.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()
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
packetFee = types.NewPacketFee(fee, defaultAccAddress, nil)

tc.malleate() // malleate mutates test data

err := packetFee.Validate()

if tc.expPass {
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 6d3d67a

Please sign in to comment.