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

feat: adding genesis validation + tests #561

Merged
merged 7 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions modules/apps/29-fee/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

host "github.com/cosmos/ibc-go/modules/core/24-host"
)

// NewGenesisState creates a 29-fee GenesisState instance.
func NewGenesisState(identifiedFees []*IdentifiedPacketFee, feeEnabledChannels []*FeeEnabledChannel, registeredRelayers []*RegisteredRelayerAddress) *GenesisState {
return &GenesisState{
Expand All @@ -21,5 +28,35 @@ func DefaultGenesisState() *GenesisState {
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
// Validate IdentifiedPacketFees
for _, fee := range gs.IdentifiedFees {
err := fee.Validate()
if err != nil {
return err
}
}

// Validate FeeEnabledChannels
for _, feeCh := range gs.FeeEnabledChannels {
if err := host.PortIdentifierValidator(feeCh.PortId); err != nil {
return sdkerrors.Wrap(err, "invalid source port ID")
}
if err := host.ChannelIdentifierValidator(feeCh.ChannelId); err != nil {
return sdkerrors.Wrap(err, "invalid source channel ID")
}
}

// Validate RegisteredRelayers
for _, rel := range gs.RegisteredRelayers {
_, err := sdk.AccAddressFromBech32(rel.Address)
if err != nil {
return sdkerrors.Wrap(err, "failed to convert source relayer address into sdk.AccAddress")
}

if rel.CounterpartyAddress == "" {
return ErrCounterpartyAddressEmpty
}
}

return nil
}
185 changes: 171 additions & 14 deletions modules/apps/29-fee/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,197 @@
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 = types.NewPacketId(
"",
portID,
seq,
)

seantking marked this conversation as resolved.
Show resolved Hide resolved
},
true,
false,
},
{
"invalid packetId: invalid port",
func() {
packetId = types.NewPacketId(
channelID,
"",
seq,
)
},
false,
},
{
"invalid packetId: invalid sequence",
func() {
packetId = types.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 client",
&types.GenesisState{
PortId: "(INVALIDPORT)",
"invalid RegisteredRelayers: invalid sender",
func() {
sender = ""
},
false,
},
{
"invalid RegisteredRelayers: invalid counterparty",
func() {
counterparty = ""
},
false,
},
}

for _, tc := range testCases {
portID = types.PortID
channelID = ibctesting.FirstChannelID
seq = uint64(1)

// build PacketId & Fee
packetId = types.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) {
Copy link
Contributor Author

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.

Copy link
Member

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 readability

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 point.

testCases := []struct {
name string
genState *types.GenesisState
expPass bool
}{
{
name: "default",
genState: types.DefaultGenesisState(),
expPass: true,
},
}

for _, tc := range testCases {
Expand All @@ -46,4 +204,3 @@ func TestValidateGenesis(t *testing.T) {
}
}
}
*/
87 changes: 56 additions & 31 deletions modules/apps/29-fee/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -171,8 +144,60 @@ func NewIdentifiedPacketFee(packetId *channeltypes.PacketId, fee Fee, refundAddr
}
}

func (fee IdentifiedPacketFee) Validate() error {
Copy link
Member

Choose a reason for hiding this comment

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

Much cleaner!

if err := host.PortIdentifierValidator(fee.PacketId.PortId); err != nil {
return sdkerrors.Wrap(err, "invalid source port ID")
}

if err := host.ChannelIdentifierValidator(fee.PacketId.ChannelId); err != nil {
return sdkerrors.Wrap(err, "invalid source channel ID")
}

if fee.PacketId.Sequence == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidSequence, "packet sequence cannot be 0")
}

_, 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
}

// 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}
}

// Validates a PacketId
// TODO: move to channeltypes
/*
func (p channeltypes.PacketId) Validate() error {
Copy link
Contributor Author

@seantking seantking Nov 24, 2021

Choose a reason for hiding this comment

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

leaving in as we can copy pasta to channeltypes PR later.

Copy link
Member

Choose a reason for hiding this comment

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

ahh i moved it in #551, you can move it as well. Or just make it a non-object method for now (i.e. ValidatePacketId(pid PacketId) and move it later .

Or just do the same change and resolve merge conflict later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok moved it over and I'm using the packetId validation now. Sorry about the confusion there lol.

if err := host.PortIdentifierValidator(p.PortId); err != nil {
return sdkerrors.Wrap(err, "invalid source port ID")
}
if err := host.ChannelIdentifierValidator(p.ChannelId); err != nil {
return sdkerrors.Wrap(err, "invalid source channel ID")
}
if p.Sequence == 0 {
return sdkerrors.Wrap(ErrInvalidPacket, "packet sequence cannot be 0")
}
}
*/