-
Notifications
You must be signed in to change notification settings - Fork 637
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
feat: adding genesis validation + tests #561
Merged
seantking
merged 7 commits into
ics29-fee-middleware
from
sean/issue#264-genesis-validation
Nov 24, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e091131
feat: adding genesis validation + tests
seantking d292e68
fix: imports
seantking 422d957
Update modules/apps/29-fee/types/genesis.go
seantking e6d4584
fix: nit
seantking 9ca60b8
Update modules/apps/29-fee/types/genesis_test.go
seantking 20ef9d8
nit: imporve default gen val test
seantking baa58a7
chore: move packetId + val to channeltypes and use validate fn
seantking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -1,49 +1,186 @@ | ||
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/modules/apps/transfer/types" | ||
"github.com/cosmos/ibc-go/modules/apps/29-fee/types" | ||
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" | ||
ibctesting "github.com/cosmos/ibc-go/testing" | ||
) | ||
|
||
var ( | ||
addr1 = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String() | ||
addr2 = sdk.AccAddress("testaddr2").String() | ||
validCoins = sdk.Coins{sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}} | ||
validCoins2 = sdk.Coins{sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(200)}} | ||
validCoins3 = sdk.Coins{sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(300)}} | ||
) | ||
|
||
func TestValidateGenesis(t *testing.T) { | ||
var ( | ||
packetId *channeltypes.PacketId | ||
fee types.Fee | ||
refundAcc string | ||
sender string | ||
counterparty string | ||
portID string | ||
channelID string | ||
seq uint64 | ||
) | ||
|
||
testCases := []struct { | ||
name string | ||
genState *types.GenesisState | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
name: "default", | ||
genState: types.DefaultGenesisState(), | ||
expPass: true, | ||
"valid genesis", | ||
func() {}, | ||
true, | ||
}, | ||
{ | ||
"valid genesis", | ||
&types.GenesisState{ | ||
PortId: "portidone", | ||
"invalid packetId: invalid channel", | ||
func() { | ||
packetId = channeltypes.NewPacketId( | ||
"", | ||
portID, | ||
seq, | ||
) | ||
}, | ||
true, | ||
false, | ||
}, | ||
{ | ||
"invalid packetId: invalid port", | ||
func() { | ||
packetId = channeltypes.NewPacketId( | ||
channelID, | ||
"", | ||
seq, | ||
) | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid client", | ||
&types.GenesisState{ | ||
PortId: "(INVALIDPORT)", | ||
"invalid packetId: invalid sequence", | ||
func() { | ||
packetId = channeltypes.NewPacketId( | ||
channelID, | ||
portID, | ||
0, | ||
) | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid packetId: invalid fee", | ||
func() { | ||
fee = types.Fee{ | ||
sdk.Coins{}, | ||
sdk.Coins{}, | ||
sdk.Coins{}, | ||
} | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid packetId: invalid refundAcc", | ||
func() { | ||
refundAcc = "" | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid FeeEnabledChannel: invalid ChannelID", | ||
func() { | ||
channelID = "" | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid FeeEnabledChannel: invalid PortID", | ||
func() { | ||
portID = "" | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid RegisteredRelayers: invalid sender", | ||
func() { | ||
sender = "" | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid RegisteredRelayers: invalid counterparty", | ||
func() { | ||
counterparty = "" | ||
}, | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
err := tc.genState.Validate() | ||
portID = types.PortID | ||
channelID = ibctesting.FirstChannelID | ||
seq = uint64(1) | ||
|
||
// build PacketId & Fee | ||
packetId = channeltypes.NewPacketId( | ||
portID, | ||
channelID, | ||
seq, | ||
) | ||
fee = types.Fee{ | ||
validCoins, | ||
validCoins2, | ||
validCoins3, | ||
} | ||
|
||
refundAcc = addr1 | ||
|
||
// relayer addresses | ||
sender = addr1 | ||
counterparty = addr2 | ||
|
||
tc.malleate() | ||
|
||
genState := types.GenesisState{ | ||
IdentifiedFees: []*types.IdentifiedPacketFee{ | ||
{ | ||
PacketId: packetId, | ||
Fee: fee, | ||
RefundAddress: refundAcc, | ||
Relayers: nil, | ||
}, | ||
}, | ||
FeeEnabledChannels: []*types.FeeEnabledChannel{ | ||
{ | ||
PortId: portID, | ||
ChannelId: channelID, | ||
}, | ||
}, | ||
RegisteredRelayers: []*types.RegisteredRelayerAddress{ | ||
{ | ||
Address: sender, | ||
CounterpartyAddress: counterparty, | ||
}, | ||
}, | ||
} | ||
|
||
err := genState.Validate() | ||
if tc.expPass { | ||
require.NoError(t, err, tc.name) | ||
} else { | ||
require.Error(t, err, tc.name) | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
func TestValidateDefaultGenesis(t *testing.T) { | ||
err := types.DefaultGenesisState().Validate() | ||
require.NoError(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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,42 +111,15 @@ func NewMsgPayPacketFeeAsync(identifiedPacketFee IdentifiedPacketFee) *MsgPayPac | |
|
||
// ValidateBasic performs a basic check of the MsgPayPacketFeeAsync fields | ||
func (msg MsgPayPacketFeeAsync) ValidateBasic() error { | ||
// validate channelId | ||
err := host.ChannelIdentifierValidator(msg.IdentifiedPacketFee.PacketId.ChannelId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// validate portId | ||
err = host.PortIdentifierValidator(msg.IdentifiedPacketFee.PacketId.PortId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// signer check | ||
_, err = sdk.AccAddressFromBech32(msg.IdentifiedPacketFee.RefundAddress) | ||
_, err := sdk.AccAddressFromBech32(msg.IdentifiedPacketFee.RefundAddress) | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "failed to convert msg.Signer into sdk.AccAddress") | ||
} | ||
|
||
// enforce relayer is nil | ||
if msg.IdentifiedPacketFee.Relayers != nil { | ||
return ErrRelayersNotNil | ||
} | ||
|
||
// ensure sequence is not 0 | ||
if msg.IdentifiedPacketFee.PacketId.Sequence == 0 { | ||
return sdkerrors.ErrInvalidSequence | ||
} | ||
|
||
// if any of the fee's are invalid return an error | ||
if !msg.IdentifiedPacketFee.Fee.AckFee.IsValid() || !msg.IdentifiedPacketFee.Fee.ReceiveFee.IsValid() || !msg.IdentifiedPacketFee.Fee.TimeoutFee.IsValid() { | ||
return sdkerrors.ErrInvalidCoins | ||
} | ||
|
||
// if all three fee's are zero or empty return an error | ||
if msg.IdentifiedPacketFee.Fee.AckFee.IsZero() && msg.IdentifiedPacketFee.Fee.ReceiveFee.IsZero() && msg.IdentifiedPacketFee.Fee.TimeoutFee.IsZero() { | ||
return sdkerrors.ErrInvalidCoins | ||
err = msg.IdentifiedPacketFee.Validate() | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "Invalid IdentifiedPacketFee") | ||
} | ||
|
||
return nil | ||
|
@@ -171,8 +144,32 @@ func NewIdentifiedPacketFee(packetId *channeltypes.PacketId, fee Fee, refundAddr | |
} | ||
} | ||
|
||
// NewPacketId returns a new instance of PacketId | ||
// TODO: move to channeltypes | ||
func NewPacketId(channelId, portId string, seq uint64) *channeltypes.PacketId { | ||
return &channeltypes.PacketId{ChannelId: channelId, PortId: portId, Sequence: seq} | ||
func (fee IdentifiedPacketFee) Validate() error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much cleaner! |
||
// validate PacketId | ||
err := fee.PacketId.Validate() | ||
if err != nil { | ||
return sdkerrors.Wrap(err, "Invalid PacketId") | ||
} | ||
|
||
_, err = sdk.AccAddressFromBech32(fee.RefundAddress) | ||
if err != nil { | ||
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.ReceiveFee.IsValid() || !fee.Fee.TimeoutFee.IsValid() { | ||
return sdkerrors.ErrInvalidCoins | ||
} | ||
|
||
// if all three fee's are zero or empty return an error | ||
if fee.Fee.AckFee.IsZero() && fee.Fee.ReceiveFee.IsZero() && fee.Fee.TimeoutFee.IsZero() { | ||
return sdkerrors.ErrInvalidCoins | ||
} | ||
|
||
// enforce relayer is nil | ||
if fee.Relayers != nil { | ||
return ErrRelayersNotNil | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate test as it was clunky otherwise as I had to include the full genesis state in each test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why even create the
testCases
and for loop?Just call
types.DefaultGenesisState.Validate()
and then assert the error is nil. 1-2 line test depending on readabilityThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point.