Skip to content

Commit

Permalink
ics29:fix: update genesis type (#913)
Browse files Browse the repository at this point in the history
* fix: adding ForwardRelayerAddresses to genesis

* fix: trimspace on string check

* nit: err + trimspace error case
  • Loading branch information
seantking authored Feb 15, 2022
1 parent 53ecc94 commit c43ff9d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
22 changes: 18 additions & 4 deletions modules/apps/29-fee/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package types

import (
"strings"

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

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

// NewGenesisState creates a 29-fee GenesisState instance.
func NewGenesisState(identifiedFees []IdentifiedPacketFee, feeEnabledChannels []*FeeEnabledChannel, registeredRelayers []*RegisteredRelayerAddress) *GenesisState {
func NewGenesisState(identifiedFees []IdentifiedPacketFee, feeEnabledChannels []*FeeEnabledChannel, registeredRelayers []*RegisteredRelayerAddress, forwardRelayers []*ForwardRelayerAddress) *GenesisState {
return &GenesisState{
IdentifiedFees: identifiedFees,
FeeEnabledChannels: feeEnabledChannels,
RegisteredRelayers: registeredRelayers,
ForwardRelayers: forwardRelayers,
}
}

Expand All @@ -22,6 +25,7 @@ func DefaultGenesisState() *GenesisState {
IdentifiedFees: []IdentifiedPacketFee{},
FeeEnabledChannels: []*FeeEnabledChannel{},
RegisteredRelayers: []*RegisteredRelayerAddress{},
ForwardRelayers: []*ForwardRelayerAddress{},
}
}

Expand All @@ -48,15 +52,25 @@ func (gs GenesisState) Validate() error {

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

if rel.CounterpartyAddress == "" {
if strings.TrimSpace(rel.CounterpartyAddress) == "" {
return ErrCounterpartyAddressEmpty
}
}

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

if err := rel.PacketId.Validate(); err != nil {
return err
}
}

return nil
}
42 changes: 33 additions & 9 deletions modules/apps/29-fee/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ var (

func TestValidateGenesis(t *testing.T) {
var (
packetId channeltypes.PacketId
fee types.Fee
refundAcc string
sender string
counterparty string
portID string
channelID string
seq uint64
packetId channeltypes.PacketId
fee types.Fee
refundAcc string
sender string
forwardAddr string
counterparty string
portID string
channelID string
packetChannelID string
seq uint64
)

testCases := []struct {
Expand Down Expand Up @@ -118,7 +120,21 @@ func TestValidateGenesis(t *testing.T) {
{
"invalid RegisteredRelayers: invalid counterparty",
func() {
counterparty = ""
counterparty = " "
},
false,
},
{
"invalid ForwardRelayerAddress: invalid forwardAddr",
func() {
forwardAddr = ""
},
false,
},
{
"invalid ForwardRelayerAddress: invalid packet",
func() {
packetChannelID = "1"
},
false,
},
Expand All @@ -127,6 +143,7 @@ func TestValidateGenesis(t *testing.T) {
for _, tc := range testCases {
portID = transfertypes.PortID
channelID = ibctesting.FirstChannelID
packetChannelID = ibctesting.FirstChannelID
seq = uint64(1)

// build PacketId & Fee
Expand All @@ -146,6 +163,7 @@ func TestValidateGenesis(t *testing.T) {
// relayer addresses
sender = addr1
counterparty = addr2
forwardAddr = addr2

tc.malleate()

Expand All @@ -170,6 +188,12 @@ func TestValidateGenesis(t *testing.T) {
CounterpartyAddress: counterparty,
},
},
ForwardRelayers: []*types.ForwardRelayerAddress{
{
Address: forwardAddr,
PacketId: channeltypes.NewPacketId(packetChannelID, portID, 1),
},
},
}

err := genState.Validate()
Expand Down

0 comments on commit c43ff9d

Please sign in to comment.