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

chore (04-channel): use IBC channel sentinel errors in genesis validate #7675

Merged
merged 10 commits into from
Dec 18, 2024
2 changes: 2 additions & 0 deletions modules/core/04-channel/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ var (
ErrTimeoutElapsed = errorsmod.Register(SubModuleName, 40, "timeout elapsed")
ErrPruningSequenceStartNotFound = errorsmod.Register(SubModuleName, 41, "pruning sequence start not found")
ErrRecvStartSequenceNotFound = errorsmod.Register(SubModuleName, 42, "recv start sequence not found")
ErrInvalidCommitment = errorsmod.Register(SubModuleName, 43, "invalid commitment")
ErrInvalidSend = errorsmod.Register(SubModuleName, 44, "invalid send")
DongLieu marked this conversation as resolved.
Show resolved Hide resolved
)
18 changes: 10 additions & 8 deletions modules/core/04-channel/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"

errorsmod "cosmossdk.io/errors"

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

Expand Down Expand Up @@ -98,36 +100,36 @@ func (gs GenesisState) Validate() error {
}

if maxSequence != 0 && maxSequence >= gs.NextChannelSequence {
return fmt.Errorf("next channel sequence %d must be greater than maximum sequence used in channel identifier %d", gs.NextChannelSequence, maxSequence)
return errorsmod.Wrapf(host.ErrInvalidID, "next channel sequence %d must be greater than maximum sequence used in channel identifier %d", gs.NextChannelSequence, maxSequence)
DongLieu marked this conversation as resolved.
Show resolved Hide resolved
}

for i, ack := range gs.Acknowledgements {
if err := ack.Validate(); err != nil {
return fmt.Errorf("invalid acknowledgement %v ack index %d: %w", ack, i, err)
return errorsmod.Wrapf(ErrInvalidAcknowledgement, "%v ack index %d: %s", ack, i, err.Error())
}
if len(ack.Data) == 0 {
return fmt.Errorf("invalid acknowledgement %v ack index %d: data bytes cannot be empty", ack, i)
return errorsmod.Wrapf(ErrInvalidAcknowledgement, "%v ack index %d: data bytes cannot be empty", ack, i)
}
}

for i, receipt := range gs.Receipts {
if err := receipt.Validate(); err != nil {
return fmt.Errorf("invalid acknowledgement %v ack index %d: %w", receipt, i, err)
return errorsmod.Wrapf(ErrInvalidAcknowledgement, "%v ack index %d: %s", receipt, i, err.Error())
}
}

for i, commitment := range gs.Commitments {
if err := commitment.Validate(); err != nil {
return fmt.Errorf("invalid commitment %v index %d: %w", commitment, i, err)
return errorsmod.Wrapf(ErrInvalidCommitment, "%v index %d: %s", commitment, i, err.Error())
}
if len(commitment.Data) == 0 {
return fmt.Errorf("invalid acknowledgement %v ack index %d: data bytes cannot be empty", commitment, i)
return errorsmod.Wrapf(ErrInvalidAcknowledgement, "%v ack index %d: data bytes cannot be empty", commitment, i)
}
}

for i, ss := range gs.SendSequences {
if err := ss.Validate(); err != nil {
return fmt.Errorf("invalid send sequence %v index %d: %w", ss, i, err)
return errorsmod.Wrapf(ErrInvalidSend, "invalid send sequence %v index %d: %s", ss, i, err.Error())
}
}

Expand All @@ -139,7 +141,7 @@ func (gs GenesisState) Validate() error {

for i, as := range gs.AckSequences {
if err := as.Validate(); err != nil {
return fmt.Errorf("invalid acknowledgement sequence %v index %d: %w", as, i, err)
return errorsmod.Wrapf(ErrInvalidAcknowledgement, "sequence %v index %d: %s", as, i, err.Error())
}
}

Expand Down
28 changes: 15 additions & 13 deletions modules/core/04-channel/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

const (
Expand All @@ -26,12 +27,12 @@ func TestValidateGenesis(t *testing.T) {
testCases := []struct {
name string
genState types.GenesisState
expPass bool
expErr error
}{
{
name: "default",
genState: types.DefaultGenesisState(),
expPass: true,
expErr: nil,
},
{
name: "valid genesis",
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestValidateGenesis(t *testing.T) {
2,
types.Params{UpgradeTimeout: types.DefaultTimeout},
),
expPass: true,
expErr: nil,
},
{
name: "invalid channel",
Expand All @@ -82,7 +83,7 @@ func TestValidateGenesis(t *testing.T) {
),
},
},
expPass: false,
expErr: host.ErrInvalidID,
},
{
name: "invalid ack",
Expand All @@ -91,7 +92,7 @@ func TestValidateGenesis(t *testing.T) {
types.NewPacketState(testPort2, testChannel2, 1, nil),
},
},
expPass: false,
expErr: types.ErrInvalidAcknowledgement,
},
{
name: "invalid commitment",
Expand All @@ -100,7 +101,7 @@ func TestValidateGenesis(t *testing.T) {
types.NewPacketState(testPort1, testChannel1, 1, nil),
},
},
expPass: false,
expErr: types.ErrInvalidCommitment,
},
{
name: "invalid send seq",
Expand All @@ -109,7 +110,7 @@ func TestValidateGenesis(t *testing.T) {
types.NewPacketSequence(testPort1, testChannel1, 0),
},
},
expPass: false,
expErr: types.ErrInvalidSend,
},
{
name: "invalid recv seq",
Expand All @@ -118,7 +119,7 @@ func TestValidateGenesis(t *testing.T) {
types.NewPacketSequence(testPort1, "(testChannel1)", 1),
},
},
expPass: false,
expErr: host.ErrInvalidID,
},
{
name: "invalid recv seq 2",
Expand All @@ -127,7 +128,7 @@ func TestValidateGenesis(t *testing.T) {
types.NewPacketSequence("(testPort1)", testChannel1, 1),
},
},
expPass: false,
expErr: host.ErrInvalidID,
},
{
name: "invalid ack seq",
Expand All @@ -136,7 +137,7 @@ func TestValidateGenesis(t *testing.T) {
types.NewPacketSequence(testPort1, "(testChannel1)", 1),
},
},
expPass: false,
expErr: types.ErrInvalidAcknowledgement,
},
{
name: "invalid channel identifier",
Expand Down Expand Up @@ -174,7 +175,7 @@ func TestValidateGenesis(t *testing.T) {
0,
types.Params{UpgradeTimeout: types.DefaultTimeout},
),
expPass: false,
expErr: host.ErrInvalidID,
},
{
name: "next channel sequence is less than maximum channel identifier sequence used",
Expand Down Expand Up @@ -212,17 +213,18 @@ func TestValidateGenesis(t *testing.T) {
0,
types.Params{UpgradeTimeout: types.DefaultTimeout},
),
expPass: false,
expErr: host.ErrInvalidID,
},
}

for _, tc := range testCases {
tc := tc
err := tc.genState.Validate()
if tc.expPass {
if tc.expErr == nil {
require.NoError(t, err, tc.name)
} else {
require.Error(t, err, tc.name)
require.ErrorIs(t, err, tc.expErr)
}
}
}
Loading