From 4c0481878611c4dc11804e72683284fe5638b947 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 7 Oct 2024 09:16:24 +0800 Subject: [PATCH 1/3] fix: avoid invalid identifier error when validate genesis that contains localhost client --- CHANGELOG.md | 1 + modules/core/03-connection/types/keys.go | 4 ++++ modules/core/03-connection/types/keys_test.go | 1 + 3 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b9b450e733..a40c468838d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (apps/27-interchain-accounts) [\#7277](https://github.com/cosmos/ibc-go/pull/7277) Use `GogoResolver` when populating module query safe allow list to avoid panics from unresolvable protobuf dependencies. * (core/04-channel) [\#7342](https://github.com/cosmos/ibc-go/pull/7342) Read Tx cmd flags including from address to avoid Address cannot be empty error when upgrade-channels via cli. +* (core/03-connection) [\#7397](https://github.com/cosmos/ibc-go/pull/7397) Skip the validation connectionID for localhost client. ## [v9.0.0](https://github.com/cosmos/ibc-go/releases/tag/v9.0.0) - 2024-10-01 diff --git a/modules/core/03-connection/types/keys.go b/modules/core/03-connection/types/keys.go index ebea9087eb7..20af96912d6 100644 --- a/modules/core/03-connection/types/keys.go +++ b/modules/core/03-connection/types/keys.go @@ -7,6 +7,7 @@ import ( errorsmod "cosmossdk.io/errors" host "github.com/cosmos/ibc-go/v9/modules/core/24-host" + "github.com/cosmos/ibc-go/v9/modules/core/exported" ) const ( @@ -52,6 +53,9 @@ func IsValidConnectionID(connectionID string) bool { // ParseConnectionSequence parses the connection sequence from the connection identifier. func ParseConnectionSequence(connectionID string) (uint64, error) { + if connectionID == exported.LocalhostConnectionID { + return 0, nil + } if !IsConnectionIDFormat(connectionID) { return 0, errorsmod.Wrap(host.ErrInvalidID, "connection identifier is not in the format: `connection-{N}`") } diff --git a/modules/core/03-connection/types/keys_test.go b/modules/core/03-connection/types/keys_test.go index bd929d63a42..c618e6b6040 100644 --- a/modules/core/03-connection/types/keys_test.go +++ b/modules/core/03-connection/types/keys_test.go @@ -30,6 +30,7 @@ func TestParseConnectionSequence(t *testing.T) { {"blank id", " ", 0, false}, {"empty id", "", 0, false}, {"negative sequence", "connection--1", 0, false}, + {"localhost", "connection-localhost", 0, true}, } for _, tc := range testCases { From 0f4bc900202aff3dde79e26985c45fab4750a78a Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 7 Oct 2024 17:45:58 +0800 Subject: [PATCH 2/3] Apply suggestions from code review --- CHANGELOG.md | 2 +- modules/core/03-connection/types/genesis.go | 5 +++++ modules/core/03-connection/types/genesis_test.go | 15 +++++++++++++++ modules/core/03-connection/types/keys.go | 4 ---- modules/core/03-connection/types/keys_test.go | 1 - 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a40c468838d..e2225e03952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,7 +64,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (apps/27-interchain-accounts) [\#7277](https://github.com/cosmos/ibc-go/pull/7277) Use `GogoResolver` when populating module query safe allow list to avoid panics from unresolvable protobuf dependencies. * (core/04-channel) [\#7342](https://github.com/cosmos/ibc-go/pull/7342) Read Tx cmd flags including from address to avoid Address cannot be empty error when upgrade-channels via cli. -* (core/03-connection) [\#7397](https://github.com/cosmos/ibc-go/pull/7397) Skip the validation connectionID for localhost client. +* (core/03-connection) [\#7397](https://github.com/cosmos/ibc-go/pull/7397) Skip the genesis validation connectionID for localhost client. ## [v9.0.0](https://github.com/cosmos/ibc-go/releases/tag/v9.0.0) - 2024-10-01 diff --git a/modules/core/03-connection/types/genesis.go b/modules/core/03-connection/types/genesis.go index 941b441f7e1..ca7750b2374 100644 --- a/modules/core/03-connection/types/genesis.go +++ b/modules/core/03-connection/types/genesis.go @@ -2,8 +2,10 @@ package types import ( "fmt" + "strings" host "github.com/cosmos/ibc-go/v9/modules/core/24-host" + "github.com/cosmos/ibc-go/v9/modules/core/exported" ) // NewConnectionPaths creates a ConnectionPaths instance. @@ -47,6 +49,9 @@ func (gs GenesisState) Validate() error { for i, conn := range gs.Connections { sequence, err := ParseConnectionSequence(conn.Id) if err != nil { + if conn.Id == exported.LocalhostConnectionID && strings.Contains(err.Error(), host.ErrInvalidID.Error()) { + continue + } return err } diff --git a/modules/core/03-connection/types/genesis_test.go b/modules/core/03-connection/types/genesis_test.go index c009d6410fb..39a930c1433 100644 --- a/modules/core/03-connection/types/genesis_test.go +++ b/modules/core/03-connection/types/genesis_test.go @@ -7,6 +7,7 @@ import ( "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types" commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types" + "github.com/cosmos/ibc-go/v9/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/v9/testing" ) @@ -91,6 +92,20 @@ func TestValidateGenesis(t *testing.T) { ), expPass: false, }, + { + name: "localhost connection identifier", + genState: types.NewGenesisState( + []types.IdentifiedConnection{ + types.NewIdentifiedConnection(exported.LocalhostConnectionID, types.NewConnectionEnd(types.INIT, clientID, types.Counterparty{clientID2, connectionID2, commitmenttypes.NewMerklePrefix([]byte("prefix"))}, []*types.Version{ibctesting.ConnectionVersion}, 500)), + }, + []types.ConnectionPaths{ + {clientID, []string{connectionID}}, + }, + 0, + types.DefaultParams(), + ), + expPass: true, + }, { name: "next connection sequence is not greater than maximum connection identifier sequence provided", genState: types.NewGenesisState( diff --git a/modules/core/03-connection/types/keys.go b/modules/core/03-connection/types/keys.go index 20af96912d6..ebea9087eb7 100644 --- a/modules/core/03-connection/types/keys.go +++ b/modules/core/03-connection/types/keys.go @@ -7,7 +7,6 @@ import ( errorsmod "cosmossdk.io/errors" host "github.com/cosmos/ibc-go/v9/modules/core/24-host" - "github.com/cosmos/ibc-go/v9/modules/core/exported" ) const ( @@ -53,9 +52,6 @@ func IsValidConnectionID(connectionID string) bool { // ParseConnectionSequence parses the connection sequence from the connection identifier. func ParseConnectionSequence(connectionID string) (uint64, error) { - if connectionID == exported.LocalhostConnectionID { - return 0, nil - } if !IsConnectionIDFormat(connectionID) { return 0, errorsmod.Wrap(host.ErrInvalidID, "connection identifier is not in the format: `connection-{N}`") } diff --git a/modules/core/03-connection/types/keys_test.go b/modules/core/03-connection/types/keys_test.go index c618e6b6040..bd929d63a42 100644 --- a/modules/core/03-connection/types/keys_test.go +++ b/modules/core/03-connection/types/keys_test.go @@ -30,7 +30,6 @@ func TestParseConnectionSequence(t *testing.T) { {"blank id", " ", 0, false}, {"empty id", "", 0, false}, {"negative sequence", "connection--1", 0, false}, - {"localhost", "connection-localhost", 0, true}, } for _, tc := range testCases { From 8b357fe0510ee608f843a84294f6285bae3b4438 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 7 Oct 2024 17:49:48 +0800 Subject: [PATCH 3/3] skip --- modules/core/03-connection/types/genesis.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/modules/core/03-connection/types/genesis.go b/modules/core/03-connection/types/genesis.go index ca7750b2374..fc6689a0704 100644 --- a/modules/core/03-connection/types/genesis.go +++ b/modules/core/03-connection/types/genesis.go @@ -2,7 +2,6 @@ package types import ( "fmt" - "strings" host "github.com/cosmos/ibc-go/v9/modules/core/24-host" "github.com/cosmos/ibc-go/v9/modules/core/exported" @@ -47,16 +46,14 @@ func (gs GenesisState) Validate() error { var maxSequence uint64 for i, conn := range gs.Connections { - sequence, err := ParseConnectionSequence(conn.Id) - if err != nil { - if conn.Id == exported.LocalhostConnectionID && strings.Contains(err.Error(), host.ErrInvalidID.Error()) { - continue + if conn.Id != exported.LocalhostConnectionID { + sequence, err := ParseConnectionSequence(conn.Id) + if err != nil { + return err + } + if sequence > maxSequence { + maxSequence = sequence } - return err - } - - if sequence > maxSequence { - maxSequence = sequence } if err := conn.ValidateBasic(); err != nil {