From 03f9219d118404ec81944126636d1e530c95ff9b Mon Sep 17 00:00:00 2001 From: Daisuke Kanda Date: Tue, 9 Apr 2024 09:26:59 +0000 Subject: [PATCH 1/9] Change behaviors of create client, connection, and channel commands. - just exit with status 1 if config.json has id but that client/connection/channel is not exists. - just exit with status 0 if config.json has id and that client/connection/channel is already opened. Signed-off-by: Daisuke Kanda --- core/channel.go | 56 ++++++++++++++++++ core/client.go | 49 +++++++++++++++ core/connection.go | 55 +++++++++++++++++ tests/cases/tm2tm/tests/Makefile | 36 +++++++++++ tests/cases/tm2tm/tests/setup.source.sh | 33 +++++++++++ .../test-channel-fail-already-created.sh | 59 +++++++++++++++++++ .../tm2tm/tests/test-channel-fail-unexist.sh | 54 +++++++++++++++++ .../cases/tm2tm/tests/test-channel-success.sh | 33 +++++++++++ .../tests/test-client-fail-already-created.sh | 38 ++++++++++++ .../tm2tm/tests/test-client-fail-unexist.sh | 36 +++++++++++ .../cases/tm2tm/tests/test-client-success.sh | 15 +++++ .../tests/test-conn-fail-already-created.sh | 44 ++++++++++++++ .../tm2tm/tests/test-conn-fail-unexist.sh | 43 ++++++++++++++ tests/cases/tm2tm/tests/test-conn-success.sh | 21 +++++++ 14 files changed, 572 insertions(+) create mode 100644 tests/cases/tm2tm/tests/Makefile create mode 100644 tests/cases/tm2tm/tests/setup.source.sh create mode 100755 tests/cases/tm2tm/tests/test-channel-fail-already-created.sh create mode 100755 tests/cases/tm2tm/tests/test-channel-fail-unexist.sh create mode 100755 tests/cases/tm2tm/tests/test-channel-success.sh create mode 100755 tests/cases/tm2tm/tests/test-client-fail-already-created.sh create mode 100755 tests/cases/tm2tm/tests/test-client-fail-unexist.sh create mode 100755 tests/cases/tm2tm/tests/test-client-success.sh create mode 100755 tests/cases/tm2tm/tests/test-conn-fail-already-created.sh create mode 100755 tests/cases/tm2tm/tests/test-conn-fail-unexist.sh create mode 100755 tests/cases/tm2tm/tests/test-conn-success.sh diff --git a/core/channel.go b/core/channel.go index bb37a033..e1a5135e 100644 --- a/core/channel.go +++ b/core/channel.go @@ -7,6 +7,7 @@ import ( "time" retry "github.com/avast/retry-go" + clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" "github.com/hyperledger-labs/yui-relayer/log" ) @@ -14,6 +15,12 @@ import ( // CreateChannel runs the channel creation messages on timeout until they pass // TODO: add max retries or something to this function func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) error { + if cont, err := checkChannelCreateReady(src, dst); err != nil { + return err + } else if !cont { + return nil + } + logger := GetChannelPairLogger(src, dst) defer logger.TimeTrack(time.Now(), "CreateChannel") @@ -74,6 +81,55 @@ func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) e return nil } +func checkChannelCreateReady(src, dst *ProvableChain) (bool, error) { + srcID := src.Chain.Path().ChannelID + dstID := dst.Chain.Path().ChannelID + + if srcID == "" && dstID == "" { + return true, nil + } + + getState := func(pc *ProvableChain) (chantypes.State, error) { + if pc.Chain.Path().ChannelID == "" { + return chantypes.UNINITIALIZED, nil + } + + latestHeight, err := pc.LatestHeight() + if err != nil { + return chantypes.UNINITIALIZED, err + } + queryHeight := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), 0) + res, err2 := pc.QueryChannel(NewQueryContext(context.TODO(), queryHeight)) + if err2 != nil { + return chantypes.UNINITIALIZED, err2 + } + return res.Channel.State, nil + } + + srcState, srcErr := getState(src) + if srcErr != nil { + return false, srcErr + } + + dstState, dstErr := getState(src) + if dstErr != nil { + return false, dstErr + } + + if srcID != "" && srcState == chantypes.UNINITIALIZED { + return false, fmt.Errorf("src channel id is given but that channel is not exists: %s", srcID); + } + if dstID != "" && dstState == chantypes.UNINITIALIZED { + return false, fmt.Errorf("dst channel id is given but that channel is not exists: %s", dstID); + } + + if srcState == chantypes.OPEN && dstState == chantypes.OPEN { + fmt.Printf("channels are already created: src=%s, dst=%s\n", srcID, dstID) + return false, nil + } + return true, nil +} + func createChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) { out := NewRelayMsgs() if err := validatePaths(src, dst); err != nil { diff --git a/core/client.go b/core/client.go index 385a83ed..8d2db4a6 100644 --- a/core/client.go +++ b/core/client.go @@ -1,6 +1,7 @@ package core import ( + "context" "fmt" "time" @@ -10,7 +11,55 @@ import ( "github.com/hyperledger-labs/yui-relayer/log" ) +func checkCreateClientsReady(src, dst *ProvableChain) (bool, error) { + srcID := src.Chain.Path().ClientID; + dstID := dst.Chain.Path().ClientID; + + if srcID == "" && dstID == "" { + return true, nil + } else if srcID == "" && dstID != "" { + return false, fmt.Errorf("dst client id is given but src's is not: %s", dstID) + } else if srcID != "" && dstID == "" { + return false, fmt.Errorf("src client id is given but dst's is not: %s", srcID) + } + + getState := func(pc *ProvableChain) (*clienttypes.QueryClientStateResponse, error) { + latestHeight, err := pc.LatestHeight() + if err != nil { + return nil, err + } + height := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), 0); + + ctx := NewQueryContext(context.TODO(), height) + return pc.QueryClientState(ctx) + } + srcState, srcErr := getState(src) + if srcErr != nil { + return false, srcErr + } + if srcState == nil { + return false, fmt.Errorf("src client id is given but that client is not exists: %s", srcID) + } + + dstState, dstErr := getState(dst) + if dstErr != nil { + return false, dstErr + } + if dstState == nil { + return false, fmt.Errorf("dst client id is given but that client is not exists: %s", dstID) + } + + fmt.Printf("clients are already created: src=%s, dst=%s\n", srcID, dstID) + return false, nil +} + func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeight exported.Height) error { + if cont, err := checkCreateClientsReady(src, dst); err != nil { + return err + } else if !cont { + return nil + } + logger := GetChainPairLogger(src, dst) defer logger.TimeTrack(time.Now(), "CreateClients") var ( diff --git a/core/connection.go b/core/connection.go index 5557f80d..13624038 100644 --- a/core/connection.go +++ b/core/connection.go @@ -23,6 +23,12 @@ var ( ) func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration) error { + if cont, err := checkConnectionCreateReady(src, dst); err != nil { + return err + } else if !cont { + return nil + } + logger := GetConnectionPairLogger(src, dst) defer logger.TimeTrack(time.Now(), "CreateConnection") ticker := time.NewTicker(to) @@ -84,6 +90,55 @@ func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration return nil } +func checkConnectionCreateReady(src, dst *ProvableChain) (bool, error) { + srcID := src.Chain.Path().ConnectionID + dstID := dst.Chain.Path().ConnectionID + + if srcID == "" && dstID == "" { + return true, nil + } + + getState := func(pc *ProvableChain) (conntypes.State, error) { + if pc.Chain.Path().ConnectionID == "" { + return conntypes.UNINITIALIZED, nil + } + + latestHeight, err := pc.LatestHeight() + if err != nil { + return conntypes.UNINITIALIZED, err + } + queryHeight := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), 0) + res, err2 := pc.QueryConnection(NewQueryContext(context.TODO(), queryHeight)) + if err2 != nil { + return conntypes.UNINITIALIZED, err2 + } + return res.Connection.State, nil + } + + srcState, srcErr := getState(src) + if srcErr != nil { + return false, srcErr + } + + dstState, dstErr := getState(src) + if dstErr != nil { + return false, dstErr + } + + if srcID != "" && srcState == conntypes.UNINITIALIZED { + return false, fmt.Errorf("src connection id is given but that connection is not exists: %s", srcID); + } + if dstID != "" && dstState == conntypes.UNINITIALIZED { + return false, fmt.Errorf("dst connection id is given but that connection is not exists: %s", dstID); + } + + if srcState == conntypes.OPEN && dstState == conntypes.OPEN { + fmt.Printf("connections are already created: src=%s, dst=%s\n", srcID, dstID) + return false, nil + } + return true, nil +} + func createConnectionStep(src, dst *ProvableChain) (*RelayMsgs, error) { out := NewRelayMsgs() if err := validatePaths(src, dst); err != nil { diff --git a/tests/cases/tm2tm/tests/Makefile b/tests/cases/tm2tm/tests/Makefile new file mode 100644 index 00000000..4c8ec3e6 --- /dev/null +++ b/tests/cases/tm2tm/tests/Makefile @@ -0,0 +1,36 @@ +TESTS := \ + test-client-success \ + test-client-fail-unexist \ + test-client-fail-already-created \ + test-conn-success \ + test-conn-fail-unexist \ + test-conn-fail-already-created \ + test-channel-success \ + test-channel-fail-unexist \ + test-channel-fail-already-created \ + +.PHONY: help +help: + @echo "$(MAKE) " + @echo "targets are:" + @for t in $(TESTS); do echo " $${t}"; done + +$(TESTS): + ./$@.sh + +.PHONY: all +all: + @for t in $(TESTS); do\ + echo -n $$t...; \ + ./$$t.sh >log.$$t 2>&1; \ + if [ $$? -eq 0 ]; then \ + echo "ok"; \ + else \ + echo "fail"; \ + exit 1; \ + fi \ + done + +.PHONY: clean +clean: + rm -rf log.* *~ diff --git a/tests/cases/tm2tm/tests/setup.source.sh b/tests/cases/tm2tm/tests/setup.source.sh new file mode 100644 index 00000000..1478cce3 --- /dev/null +++ b/tests/cases/tm2tm/tests/setup.source.sh @@ -0,0 +1,33 @@ +rm -rf $HOME/.yui-relayer +DIR=$(dirname $BASH_SOURCE) +CONFIG=$HOME/.yui-relayer/config/config.json + +cd $DIR/.. +./scripts/fixture +./scripts/init-rly + +source $DIR/../../scripts/util + +SCRIPT_DIR=$DIR/scripts +RLY_BINARY=${SCRIPT_DIR}/../../../../build/yrly +RLY="${RLY_BINARY} --debug" + +CHAINID_ONE=ibc0 +RLYKEY=testkey +CHAINID_TWO=ibc1 +PATH_NAME=ibc01 + +$RLY tendermint keys show $CHAINID_ONE $RLYKEY +$RLY tendermint keys show $CHAINID_TWO $RLYKEY + +# initialize the light client for {{chain_id}} +retry 5 $RLY tendermint light init $CHAINID_ONE -f +retry 5 $RLY tendermint light init $CHAINID_TWO -f + +# you should see a balance for the rly key now +# $RLY q bal $CHAINID_ONE +# $RLY q bal $CHAINID_TWO + +# add a path between chain0 and chain1 +$RLY paths add $CHAINID_ONE $CHAINID_TWO $PATH_NAME --file=${SCRIPT_DIR}/../configs/path.json + diff --git a/tests/cases/tm2tm/tests/test-channel-fail-already-created.sh b/tests/cases/tm2tm/tests/test-channel-fail-already-created.sh new file mode 100755 index 00000000..ea0a45f8 --- /dev/null +++ b/tests/cases/tm2tm/tests/test-channel-fail-already-created.sh @@ -0,0 +1,59 @@ +#!/bin/bash +source ./setup.source.sh + +$RLY tx clients --src-height 2 ibc01 +r=$? +if [ $r -ne 0 ]; then + echo "fail to create client" + exit 1 +fi + +$RLY tx connection ibc01 +r=$? + +if [ $r -eq 0 ]; then + echo "success" + exit 0 +else + echo "fail" + exit 1 +fi + +$RLY tx channel ibc01 +r=$? + +if [ $r -eq 0 ]; then + echo "success" + exit 0 +else + echo "fail" + exit 1 +fi + +expect < $CONFIG.tmp +mv $CONFIG.tmp $CONFIG + +expect < $CONFIG.tmp +mv $CONFIG.tmp $CONFIG + +expect < $CONFIG.tmp +mv $CONFIG.tmp $CONFIG + +expect < Date: Wed, 17 Apr 2024 07:07:11 +0000 Subject: [PATCH 2/9] fix reviewed Signed-off-by: Daisuke Kanda --- core/channel.go | 20 ++-- core/client.go | 94 ++++++++++--------- core/connection.go | 21 ++--- tests/cases/tm2tm/Makefile | 7 ++ .../test-create-channel-fail-already-created | 35 +++++++ .../scripts/test-create-channel-fail-unexist | 44 +++++++++ .../test-create-client-fail-already-created} | 17 ++-- .../test-create-client-fail-unexist} | 16 +++- .../scripts/test-create-client-success-single | 22 +++++ ...st-create-connection-fail-already-created} | 23 ++--- .../test-create-connection-fail-unexist} | 22 ++--- tests/cases/tm2tm/tests/Makefile | 36 ------- tests/cases/tm2tm/tests/setup.source.sh | 33 ------- .../test-channel-fail-already-created.sh | 59 ------------ .../tm2tm/tests/test-channel-fail-unexist.sh | 54 ----------- .../cases/tm2tm/tests/test-channel-success.sh | 33 ------- .../cases/tm2tm/tests/test-client-success.sh | 15 --- tests/cases/tm2tm/tests/test-conn-success.sh | 21 ----- 18 files changed, 216 insertions(+), 356 deletions(-) create mode 100755 tests/cases/tm2tm/scripts/test-create-channel-fail-already-created create mode 100755 tests/cases/tm2tm/scripts/test-create-channel-fail-unexist rename tests/cases/tm2tm/{tests/test-client-fail-already-created.sh => scripts/test-create-client-fail-already-created} (66%) rename tests/cases/tm2tm/{tests/test-client-fail-unexist.sh => scripts/test-create-client-fail-unexist} (69%) create mode 100755 tests/cases/tm2tm/scripts/test-create-client-success-single rename tests/cases/tm2tm/{tests/test-conn-fail-already-created.sh => scripts/test-create-connection-fail-already-created} (58%) rename tests/cases/tm2tm/{tests/test-conn-fail-unexist.sh => scripts/test-create-connection-fail-unexist} (63%) delete mode 100644 tests/cases/tm2tm/tests/Makefile delete mode 100644 tests/cases/tm2tm/tests/setup.source.sh delete mode 100755 tests/cases/tm2tm/tests/test-channel-fail-already-created.sh delete mode 100755 tests/cases/tm2tm/tests/test-channel-fail-unexist.sh delete mode 100755 tests/cases/tm2tm/tests/test-channel-success.sh delete mode 100755 tests/cases/tm2tm/tests/test-client-success.sh delete mode 100755 tests/cases/tm2tm/tests/test-conn-success.sh diff --git a/core/channel.go b/core/channel.go index e1a5135e..273dbf41 100644 --- a/core/channel.go +++ b/core/channel.go @@ -7,7 +7,6 @@ import ( "time" retry "github.com/avast/retry-go" - clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" "github.com/hyperledger-labs/yui-relayer/log" ) @@ -15,15 +14,15 @@ import ( // CreateChannel runs the channel creation messages on timeout until they pass // TODO: add max retries or something to this function func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) error { - if cont, err := checkChannelCreateReady(src, dst); err != nil { + logger := GetChannelPairLogger(src, dst) + defer logger.TimeTrack(time.Now(), "CreateChannel") + + if cont, err := checkChannelCreateReady(src, dst, logger); err != nil { return err } else if !cont { return nil } - logger := GetChannelPairLogger(src, dst) - defer logger.TimeTrack(time.Now(), "CreateChannel") - ticker := time.NewTicker(to) failures := 0 for ; true; <-ticker.C { @@ -81,7 +80,7 @@ func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) e return nil } -func checkChannelCreateReady(src, dst *ProvableChain) (bool, error) { +func checkChannelCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) (bool, error) { srcID := src.Chain.Path().ChannelID dstID := dst.Chain.Path().ChannelID @@ -98,8 +97,7 @@ func checkChannelCreateReady(src, dst *ProvableChain) (bool, error) { if err != nil { return chantypes.UNINITIALIZED, err } - queryHeight := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), 0) - res, err2 := pc.QueryChannel(NewQueryContext(context.TODO(), queryHeight)) + res, err2 := pc.QueryChannel(NewQueryContext(context.TODO(), latestHeight)) if err2 != nil { return chantypes.UNINITIALIZED, err2 } @@ -117,14 +115,14 @@ func checkChannelCreateReady(src, dst *ProvableChain) (bool, error) { } if srcID != "" && srcState == chantypes.UNINITIALIZED { - return false, fmt.Errorf("src channel id is given but that channel is not exists: %s", srcID); + return false, fmt.Errorf("src channel id is given but that channel does not exist: %s", srcID); } if dstID != "" && dstState == chantypes.UNINITIALIZED { - return false, fmt.Errorf("dst channel id is given but that channel is not exists: %s", dstID); + return false, fmt.Errorf("dst channel id is given but that channel does not exist: %s", dstID); } if srcState == chantypes.OPEN && dstState == chantypes.OPEN { - fmt.Printf("channels are already created: src=%s, dst=%s\n", srcID, dstID) + logger.Warn(fmt.Sprintf("channels are already created: src=%s, dst=%s", srcID, dstID)) return false, nil } return true, nil diff --git a/core/client.go b/core/client.go index 8d2db4a6..3ce5af8c 100644 --- a/core/client.go +++ b/core/client.go @@ -11,16 +11,12 @@ import ( "github.com/hyperledger-labs/yui-relayer/log" ) -func checkCreateClientsReady(src, dst *ProvableChain) (bool, error) { +func checkCreateClientsReady(src, dst *ProvableChain, logger *log.RelayLogger) (bool, error) { srcID := src.Chain.Path().ClientID; dstID := dst.Chain.Path().ClientID; if srcID == "" && dstID == "" { return true, nil - } else if srcID == "" && dstID != "" { - return false, fmt.Errorf("dst client id is given but src's is not: %s", dstID) - } else if srcID != "" && dstID == "" { - return false, fmt.Errorf("src client id is given but dst's is not: %s", srcID) } getState := func(pc *ProvableChain) (*clienttypes.QueryClientStateResponse, error) { @@ -28,62 +24,67 @@ func checkCreateClientsReady(src, dst *ProvableChain) (bool, error) { if err != nil { return nil, err } - height := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), 0); - ctx := NewQueryContext(context.TODO(), height) + ctx := NewQueryContext(context.TODO(), latestHeight) return pc.QueryClientState(ctx) } - srcState, srcErr := getState(src) - if srcErr != nil { - return false, srcErr - } - if srcState == nil { - return false, fmt.Errorf("src client id is given but that client is not exists: %s", srcID) - } - dstState, dstErr := getState(dst) - if dstErr != nil { - return false, dstErr + srcState := (*clienttypes.QueryClientStateResponse)(nil) + if srcID != "" { + s, err := getState(src) + if err != nil { + return false, err + } + if s == nil { + return false, fmt.Errorf("src client id is given but that client does not exist: %s", srcID) + } + srcState = s } - if dstState == nil { - return false, fmt.Errorf("dst client id is given but that client is not exists: %s", dstID) + + dstState := (*clienttypes.QueryClientStateResponse)(nil) + if dstID != "" { + s, err := getState(dst) + if err != nil { + return false, err + } + if s == nil { + return false, fmt.Errorf("dst client id is given but that client does not exist: %s", dstID) + } + dstState = s } - fmt.Printf("clients are already created: src=%s, dst=%s\n", srcID, dstID) - return false, nil + if srcState != nil && dstState != nil { + logger.Warn(fmt.Sprintf("clients are already created: src=%s, dst=%s", srcID, dstID)) + return false, nil + } else { + return true, nil + } } func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeight exported.Height) error { - if cont, err := checkCreateClientsReady(src, dst); err != nil { + logger := GetChainPairLogger(src, dst) + defer logger.TimeTrack(time.Now(), "CreateClients") + + if cont, err := checkCreateClientsReady(src, dst, logger); err != nil { return err } else if !cont { return nil } - logger := GetChainPairLogger(src, dst) - defer logger.TimeTrack(time.Now(), "CreateClients") var ( clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}} ) - srcAddr, err := src.GetAddress() - if err != nil { - logger.Error( - "failed to get address for create client", - err, - ) - return err - } - dstAddr, err := dst.GetAddress() - if err != nil { - logger.Error( - "failed to get address for create client", - err, - ) - return err - } + if src.Chain.Path().ClientID == "" { + srcAddr, err := src.GetAddress() + if err != nil { + logger.Error( + "failed to get address for create client", + err, + ) + return err + } - { cs, cons, err := dst.CreateInitialLightClientState(dstHeight) if err != nil { logger.Error("failed to create initial light client state", err) @@ -96,7 +97,16 @@ func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeigh clients.Src = append(clients.Src, msg) } - { + if dst.Chain.Path().ClientID == "" { + dstAddr, err := dst.GetAddress() + if err != nil { + logger.Error( + "failed to get address for create client", + err, + ) + return err + } + cs, cons, err := src.CreateInitialLightClientState(srcHeight) if err != nil { logger.Error("failed to create initial light client state", err) diff --git a/core/connection.go b/core/connection.go index 13624038..c9096aa9 100644 --- a/core/connection.go +++ b/core/connection.go @@ -23,16 +23,16 @@ var ( ) func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration) error { - if cont, err := checkConnectionCreateReady(src, dst); err != nil { + logger := GetConnectionPairLogger(src, dst) + defer logger.TimeTrack(time.Now(), "CreateConnection") + ticker := time.NewTicker(to) + + if cont, err := checkConnectionCreateReady(src, dst, logger); err != nil { return err } else if !cont { return nil } - logger := GetConnectionPairLogger(src, dst) - defer logger.TimeTrack(time.Now(), "CreateConnection") - ticker := time.NewTicker(to) - failed := 0 for ; true; <-ticker.C { connSteps, err := createConnectionStep(src, dst) @@ -90,7 +90,7 @@ func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration return nil } -func checkConnectionCreateReady(src, dst *ProvableChain) (bool, error) { +func checkConnectionCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) (bool, error) { srcID := src.Chain.Path().ConnectionID dstID := dst.Chain.Path().ConnectionID @@ -107,8 +107,7 @@ func checkConnectionCreateReady(src, dst *ProvableChain) (bool, error) { if err != nil { return conntypes.UNINITIALIZED, err } - queryHeight := clienttypes.NewHeight(latestHeight.GetRevisionNumber(), 0) - res, err2 := pc.QueryConnection(NewQueryContext(context.TODO(), queryHeight)) + res, err2 := pc.QueryConnection(NewQueryContext(context.TODO(), latestHeight)) if err2 != nil { return conntypes.UNINITIALIZED, err2 } @@ -126,14 +125,14 @@ func checkConnectionCreateReady(src, dst *ProvableChain) (bool, error) { } if srcID != "" && srcState == conntypes.UNINITIALIZED { - return false, fmt.Errorf("src connection id is given but that connection is not exists: %s", srcID); + return false, fmt.Errorf("src connection id is given but that connection does not exist: %s", srcID); } if dstID != "" && dstState == conntypes.UNINITIALIZED { - return false, fmt.Errorf("dst connection id is given but that connection is not exists: %s", dstID); + return false, fmt.Errorf("dst connection id is given but that connection does not exist: %s", dstID); } if srcState == conntypes.OPEN && dstState == conntypes.OPEN { - fmt.Printf("connections are already created: src=%s, dst=%s\n", srcID, dstID) + logger.Warn(fmt.Sprintf("connections are already created: src=%s, dst=%s", srcID, dstID)) return false, nil } return true, nil diff --git a/tests/cases/tm2tm/Makefile b/tests/cases/tm2tm/Makefile index 0f3f713d..fd5f539d 100644 --- a/tests/cases/tm2tm/Makefile +++ b/tests/cases/tm2tm/Makefile @@ -12,6 +12,13 @@ test: ./scripts/fixture ./scripts/init-rly ./scripts/handshake + ./scripts/test-create-client-success-single + ./scripts/test-create-client-fail-already-created + ./scripts/test-create-client-fail-unexist + ./scripts/test-create-connection-fail-already-created + ./scripts/test-create-connection-fail-unexist + ./scripts/test-create-channel-fail-already-created + ./scripts/test-create-channel-fail-unexist ./scripts/test-tx ./scripts/test-service diff --git a/tests/cases/tm2tm/scripts/test-create-channel-fail-already-created b/tests/cases/tm2tm/scripts/test-create-channel-fail-already-created new file mode 100755 index 00000000..cbf2e251 --- /dev/null +++ b/tests/cases/tm2tm/scripts/test-create-channel-fail-already-created @@ -0,0 +1,35 @@ +#!/bin/bash + +set -eux + +SCRIPT_DIR=$(cd $(dirname $0); pwd) +RLY_BINARY=${SCRIPT_DIR}/../../../../build/yrly +RLY="${RLY_BINARY} --debug" + +set +e +expect < $CONFIG + +set +e +expect < $CONFIG.tmp -mv $CONFIG.tmp $CONFIG + > $CONFIG +set +e expect < $CONFIG +$RLY tx clients --src-height 2 ibc01 + +cat $CONFIG.tmp \ + | jq '.paths.ibc01.src["client-id"] |= ""' \ + > $CONFIG +$RLY tx clients --src-height 2 ibc01 + +cp $CONFIG.tmp $CONFIG diff --git a/tests/cases/tm2tm/tests/test-conn-fail-already-created.sh b/tests/cases/tm2tm/scripts/test-create-connection-fail-already-created similarity index 58% rename from tests/cases/tm2tm/tests/test-conn-fail-already-created.sh rename to tests/cases/tm2tm/scripts/test-create-connection-fail-already-created index 1ae016cd..e6750d50 100755 --- a/tests/cases/tm2tm/tests/test-conn-fail-already-created.sh +++ b/tests/cases/tm2tm/scripts/test-create-connection-fail-already-created @@ -1,20 +1,12 @@ #!/bin/bash -source ./setup.source.sh -$RLY tx clients --src-height 2 ibc01 -r=$? -if [ $r -ne 0 ]; then - echo "fail to create client" - exit 1 -fi +set -eux -$RLY tx connection ibc01 -r=$? -if [ $r -ne 0 ]; then - echo "fail to create connection" - exit 1 -fi +SCRIPT_DIR=$(cd $(dirname $0); pwd) +RLY_BINARY=${SCRIPT_DIR}/../../../../build/yrly +RLY="${RLY_BINARY} --debug" +set +e expect < $CONFIG.tmp -mv $CONFIG.tmp $CONFIG + > $CONFIG +set +e expect <" - @echo "targets are:" - @for t in $(TESTS); do echo " $${t}"; done - -$(TESTS): - ./$@.sh - -.PHONY: all -all: - @for t in $(TESTS); do\ - echo -n $$t...; \ - ./$$t.sh >log.$$t 2>&1; \ - if [ $$? -eq 0 ]; then \ - echo "ok"; \ - else \ - echo "fail"; \ - exit 1; \ - fi \ - done - -.PHONY: clean -clean: - rm -rf log.* *~ diff --git a/tests/cases/tm2tm/tests/setup.source.sh b/tests/cases/tm2tm/tests/setup.source.sh deleted file mode 100644 index 1478cce3..00000000 --- a/tests/cases/tm2tm/tests/setup.source.sh +++ /dev/null @@ -1,33 +0,0 @@ -rm -rf $HOME/.yui-relayer -DIR=$(dirname $BASH_SOURCE) -CONFIG=$HOME/.yui-relayer/config/config.json - -cd $DIR/.. -./scripts/fixture -./scripts/init-rly - -source $DIR/../../scripts/util - -SCRIPT_DIR=$DIR/scripts -RLY_BINARY=${SCRIPT_DIR}/../../../../build/yrly -RLY="${RLY_BINARY} --debug" - -CHAINID_ONE=ibc0 -RLYKEY=testkey -CHAINID_TWO=ibc1 -PATH_NAME=ibc01 - -$RLY tendermint keys show $CHAINID_ONE $RLYKEY -$RLY tendermint keys show $CHAINID_TWO $RLYKEY - -# initialize the light client for {{chain_id}} -retry 5 $RLY tendermint light init $CHAINID_ONE -f -retry 5 $RLY tendermint light init $CHAINID_TWO -f - -# you should see a balance for the rly key now -# $RLY q bal $CHAINID_ONE -# $RLY q bal $CHAINID_TWO - -# add a path between chain0 and chain1 -$RLY paths add $CHAINID_ONE $CHAINID_TWO $PATH_NAME --file=${SCRIPT_DIR}/../configs/path.json - diff --git a/tests/cases/tm2tm/tests/test-channel-fail-already-created.sh b/tests/cases/tm2tm/tests/test-channel-fail-already-created.sh deleted file mode 100755 index ea0a45f8..00000000 --- a/tests/cases/tm2tm/tests/test-channel-fail-already-created.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash -source ./setup.source.sh - -$RLY tx clients --src-height 2 ibc01 -r=$? -if [ $r -ne 0 ]; then - echo "fail to create client" - exit 1 -fi - -$RLY tx connection ibc01 -r=$? - -if [ $r -eq 0 ]; then - echo "success" - exit 0 -else - echo "fail" - exit 1 -fi - -$RLY tx channel ibc01 -r=$? - -if [ $r -eq 0 ]; then - echo "success" - exit 0 -else - echo "fail" - exit 1 -fi - -expect < $CONFIG.tmp -mv $CONFIG.tmp $CONFIG - -expect < Date: Fri, 19 Apr 2024 02:24:59 +0000 Subject: [PATCH 3/9] install expect and jq on github actions Signed-off-by: Daisuke Kanda --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ff51a39a..3cfe9888 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,6 +80,8 @@ jobs: working-directory: ./tests/scripts run: | ./load_docker_images $CACHE_DOCKER_TENDERMINT_DIR tendermint-chain0:latest tendermint-chain1:latest + - name: Insatall softwares used by test + run: sudo apt-get install -y expect jq - name: Run Test working-directory: ./tests/cases/tm2tm run: | From dbb9b1ed13bbc9e1c2c034ca0cc4d9a52b4ad5e2 Mon Sep 17 00:00:00 2001 From: Daisuke Kanda Date: Fri, 19 Apr 2024 08:38:10 +0000 Subject: [PATCH 4/9] fix typo Signed-off-by: Daisuke Kanda --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3cfe9888..a7e56fd1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,7 +80,7 @@ jobs: working-directory: ./tests/scripts run: | ./load_docker_images $CACHE_DOCKER_TENDERMINT_DIR tendermint-chain0:latest tendermint-chain1:latest - - name: Insatall softwares used by test + - name: Install softwares used by test run: sudo apt-get install -y expect jq - name: Run Test working-directory: ./tests/cases/tm2tm From 35f9e9c535a4b7866b260eb782ea0b099281e15e Mon Sep 17 00:00:00 2001 From: Daisuke Kanda Date: Fri, 19 Apr 2024 09:06:08 +0000 Subject: [PATCH 5/9] fix a bug getState(src) to getState(dst) Signed-off-by: Daisuke Kanda --- core/channel.go | 2 +- core/connection.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/channel.go b/core/channel.go index 273dbf41..d6af9c60 100644 --- a/core/channel.go +++ b/core/channel.go @@ -109,7 +109,7 @@ func checkChannelCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) ( return false, srcErr } - dstState, dstErr := getState(src) + dstState, dstErr := getState(dst) if dstErr != nil { return false, dstErr } diff --git a/core/connection.go b/core/connection.go index c9096aa9..9411bfba 100644 --- a/core/connection.go +++ b/core/connection.go @@ -119,7 +119,7 @@ func checkConnectionCreateReady(src, dst *ProvableChain, logger *log.RelayLogger return false, srcErr } - dstState, dstErr := getState(src) + dstState, dstErr := getState(dst) if dstErr != nil { return false, dstErr } From b21521c39309103723fcf6504b0bb35f24f41be8 Mon Sep 17 00:00:00 2001 From: Daisuke Kanda Date: Fri, 19 Apr 2024 09:09:30 +0000 Subject: [PATCH 6/9] use var for default assignment Signed-off-by: Daisuke Kanda --- core/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/client.go b/core/client.go index 3ce5af8c..7f4c5c6c 100644 --- a/core/client.go +++ b/core/client.go @@ -29,7 +29,7 @@ func checkCreateClientsReady(src, dst *ProvableChain, logger *log.RelayLogger) ( return pc.QueryClientState(ctx) } - srcState := (*clienttypes.QueryClientStateResponse)(nil) + var srcState *clienttypes.QueryClientStateResponse if srcID != "" { s, err := getState(src) if err != nil { @@ -41,7 +41,7 @@ func checkCreateClientsReady(src, dst *ProvableChain, logger *log.RelayLogger) ( srcState = s } - dstState := (*clienttypes.QueryClientStateResponse)(nil) + var dstState *clienttypes.QueryClientStateResponse if dstID != "" { s, err := getState(dst) if err != nil { From 9ba77cf0091e03d5290003ab5a6aacf19c580723 Mon Sep 17 00:00:00 2001 From: Daisuke Kanda Date: Fri, 19 Apr 2024 09:43:46 +0000 Subject: [PATCH 7/9] check client-id is changed or unchanged in creating one side of clients in e2e test Signed-off-by: Daisuke Kanda --- .../scripts/test-create-client-success-single | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/cases/tm2tm/scripts/test-create-client-success-single b/tests/cases/tm2tm/scripts/test-create-client-success-single index bd779daa..64652548 100755 --- a/tests/cases/tm2tm/scripts/test-create-client-success-single +++ b/tests/cases/tm2tm/scripts/test-create-client-success-single @@ -7,16 +7,37 @@ RLY_BINARY=${SCRIPT_DIR}/../../../../build/yrly RLY="${RLY_BINARY} --debug" CONFIG=$HOME/.yui-relayer/config/config.json +cat $CONFIG|jq +OLD_SRC_CLIENT_ID=$(cat $CONFIG | jq -r '.paths.ibc01.src["client-id"]') +OLD_DST_CLIENT_ID=$(cat $CONFIG | jq -r '.paths.ibc01.dst["client-id"]') + cp $CONFIG $CONFIG.tmp cat $CONFIG.tmp \ | jq '.paths.ibc01.dst["client-id"] |= ""' \ > $CONFIG $RLY tx clients --src-height 2 ibc01 +cat $CONFIG | jq +if [[ $(cat $CONFIG | jq -r '.paths.ibc01.src["client-id"]') != $OLD_SRC_CLIENT_ID ]]; then + echo "src client id is changed." + exit 1 +fi +if [[ $(cat $CONFIG | jq -r '.paths.ibc01.dst["client-id"]') == $OLD_DST_CLIENT_ID ]]; then + echo "dst client id is not renewed." + exit 1 +fi cat $CONFIG.tmp \ | jq '.paths.ibc01.src["client-id"] |= ""' \ > $CONFIG $RLY tx clients --src-height 2 ibc01 +if [[ $(cat $CONFIG | jq -r '.paths.ibc01.src["client-id"]') == $OLD_SRC_CLIENT_ID ]]; then + echo "src client id is not renewed." + exit 1 +fi +if [[ $(cat $CONFIG | jq -r '.paths.ibc01.dst["client-id"]') != $OLD_DST_CLIENT_ID ]]; then + echo "dst client id is changed." + exit 1 +fi cp $CONFIG.tmp $CONFIG From 09f13c4b695ea9f9a092ba361725de1930105afc Mon Sep 17 00:00:00 2001 From: Daisuke Kanda Date: Fri, 19 Apr 2024 09:44:55 +0000 Subject: [PATCH 8/9] use structured log Signed-off-by: Daisuke Kanda --- core/channel.go | 2 +- core/client.go | 2 +- core/connection.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/channel.go b/core/channel.go index d6af9c60..cfbec6d3 100644 --- a/core/channel.go +++ b/core/channel.go @@ -122,7 +122,7 @@ func checkChannelCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) ( } if srcState == chantypes.OPEN && dstState == chantypes.OPEN { - logger.Warn(fmt.Sprintf("channels are already created: src=%s, dst=%s", srcID, dstID)) + logger.Warn("channels are already created", "src_channel_id", srcID, "dst_channel_id", dstID) return false, nil } return true, nil diff --git a/core/client.go b/core/client.go index 7f4c5c6c..055d813d 100644 --- a/core/client.go +++ b/core/client.go @@ -54,7 +54,7 @@ func checkCreateClientsReady(src, dst *ProvableChain, logger *log.RelayLogger) ( } if srcState != nil && dstState != nil { - logger.Warn(fmt.Sprintf("clients are already created: src=%s, dst=%s", srcID, dstID)) + logger.Warn("clients are already created", "src_client_id", srcID, "dst_client_id", dstID) return false, nil } else { return true, nil diff --git a/core/connection.go b/core/connection.go index 9411bfba..f347be56 100644 --- a/core/connection.go +++ b/core/connection.go @@ -132,7 +132,7 @@ func checkConnectionCreateReady(src, dst *ProvableChain, logger *log.RelayLogger } if srcState == conntypes.OPEN && dstState == conntypes.OPEN { - logger.Warn(fmt.Sprintf("connections are already created: src=%s, dst=%s", srcID, dstID)) + logger.Warn("connections are already created", "src_connection_id", srcID, "dst_connection_id", dstID) return false, nil } return true, nil From b66bd2d924bb0b0f48a0aaf7993f8ba176633253 Mon Sep 17 00:00:00 2001 From: Daisuke Kanda Date: Thu, 25 Apr 2024 03:00:11 +0000 Subject: [PATCH 9/9] check new client id is not empty Signed-off-by: Daisuke Kanda --- .../scripts/test-create-client-success-single | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/cases/tm2tm/scripts/test-create-client-success-single b/tests/cases/tm2tm/scripts/test-create-client-success-single index 64652548..87fa368e 100755 --- a/tests/cases/tm2tm/scripts/test-create-client-success-single +++ b/tests/cases/tm2tm/scripts/test-create-client-success-single @@ -17,25 +17,38 @@ cat $CONFIG.tmp \ | jq '.paths.ibc01.dst["client-id"] |= ""' \ > $CONFIG $RLY tx clients --src-height 2 ibc01 -cat $CONFIG | jq -if [[ $(cat $CONFIG | jq -r '.paths.ibc01.src["client-id"]') != $OLD_SRC_CLIENT_ID ]]; then +NEW_SRC_CLIENT_ID=$(cat $CONFIG | jq -r '.paths.ibc01.src["client-id"]') +NEW_DST_CLIENT_ID=$(cat $CONFIG | jq -r '.paths.ibc01.dst["client-id"]') + +if [ "$NEW_SRC_CLIENT_ID" != "$OLD_SRC_CLIENT_ID" ]; then echo "src client id is changed." exit 1 fi -if [[ $(cat $CONFIG | jq -r '.paths.ibc01.dst["client-id"]') == $OLD_DST_CLIENT_ID ]]; then +if [ "$NEW_DST_CLIENT_ID" = "$OLD_DST_CLIENT_ID" ]; then echo "dst client id is not renewed." exit 1 fi +if [ "$NEW_DST_CLIENT_ID" = "" ]; then + echo "new dst client id is empty." + exit 1 +fi cat $CONFIG.tmp \ | jq '.paths.ibc01.src["client-id"] |= ""' \ > $CONFIG $RLY tx clients --src-height 2 ibc01 -if [[ $(cat $CONFIG | jq -r '.paths.ibc01.src["client-id"]') == $OLD_SRC_CLIENT_ID ]]; then +NEW_SRC_CLIENT_ID=$(cat $CONFIG | jq -r '.paths.ibc01.src["client-id"]') +NEW_DST_CLIENT_ID=$(cat $CONFIG | jq -r '.paths.ibc01.dst["client-id"]') + +if [ "$NEW_SRC_CLIENT_ID" = "$OLD_SRC_CLIENT_ID" ]; then echo "src client id is not renewed." exit 1 fi -if [[ $(cat $CONFIG | jq -r '.paths.ibc01.dst["client-id"]') != $OLD_DST_CLIENT_ID ]]; then +if [ "$NEW_SRC_CLIENT_ID" = "" ]; then + echo "new src client id is empty." + exit 1 +fi +if [ "$NEW_DST_CLIENT_ID" != "$OLD_DST_CLIENT_ID" ]; then echo "dst client id is changed." exit 1 fi