Skip to content

Commit

Permalink
Merge pull request #138 from dai1975/pr/id-in-config
Browse files Browse the repository at this point in the history
Change behaviors of create client, connection, and channel commands.
  • Loading branch information
siburu authored Apr 26, 2024
2 parents 4f3349d + b66bd2d commit c00fc08
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ jobs:
working-directory: ./tests/scripts
run: |
./load_docker_images $CACHE_DOCKER_TENDERMINT_DIR tendermint-chain0:latest tendermint-chain1:latest
- name: Install softwares used by test
run: sudo apt-get install -y expect jq
- name: Run Test
working-directory: ./tests/cases/tm2tm
run: |
Expand Down
54 changes: 54 additions & 0 deletions core/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) e
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
}

ticker := time.NewTicker(to)
failures := 0
for ; true; <-ticker.C {
Expand Down Expand Up @@ -74,6 +80,54 @@ func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) e
return nil
}

func checkChannelCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) (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
}
res, err2 := pc.QueryChannel(NewQueryContext(context.TODO(), latestHeight))
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(dst)
if dstErr != nil {
return false, dstErr
}

if srcID != "" && srcState == chantypes.UNINITIALIZED {
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 does not exist: %s", dstID);
}

if srcState == chantypes.OPEN && dstState == chantypes.OPEN {
logger.Warn("channels are already created", "src_channel_id", srcID, "dst_channel_id", dstID)
return false, nil
}
return true, nil
}

func createChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
out := NewRelayMsgs()
if err := validatePaths(src, dst); err != nil {
Expand Down
95 changes: 77 additions & 18 deletions core/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"context"
"fmt"
"time"

Expand All @@ -10,31 +11,80 @@ import (
"github.com/hyperledger-labs/yui-relayer/log"
)

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
}

getState := func(pc *ProvableChain) (*clienttypes.QueryClientStateResponse, error) {
latestHeight, err := pc.LatestHeight()
if err != nil {
return nil, err
}

ctx := NewQueryContext(context.TODO(), latestHeight)
return pc.QueryClientState(ctx)
}

var srcState *clienttypes.QueryClientStateResponse
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
}

var dstState *clienttypes.QueryClientStateResponse
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
}

if srcState != nil && dstState != nil {
logger.Warn("clients are already created", "src_client_id", srcID, "dst_client_id", dstID)
return false, nil
} else {
return true, nil
}
}

func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeight exported.Height) error {
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
}

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)
Expand All @@ -47,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)
Expand Down
54 changes: 54 additions & 0 deletions core/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration
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
}

failed := 0
for ; true; <-ticker.C {
connSteps, err := createConnectionStep(src, dst)
Expand Down Expand Up @@ -84,6 +90,54 @@ func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration
return nil
}

func checkConnectionCreateReady(src, dst *ProvableChain, logger *log.RelayLogger) (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
}
res, err2 := pc.QueryConnection(NewQueryContext(context.TODO(), latestHeight))
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(dst)
if dstErr != nil {
return false, dstErr
}

if srcID != "" && srcState == conntypes.UNINITIALIZED {
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 does not exist: %s", dstID);
}

if srcState == conntypes.OPEN && dstState == conntypes.OPEN {
logger.Warn("connections are already created", "src_connection_id", srcID, "dst_connection_id", dstID)
return false, nil
}
return true, nil
}

func createConnectionStep(src, dst *ProvableChain) (*RelayMsgs, error) {
out := NewRelayMsgs()
if err := validatePaths(src, dst); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions tests/cases/tm2tm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions tests/cases/tm2tm/scripts/test-create-channel-fail-already-created
Original file line number Diff line number Diff line change
@@ -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 <<EOF
set timeout 5
set expect_result 0
spawn $RLY tx channel ibc01
expect {
"channels are already created" {
set expect_result 1
exp_continue
}
eof
}
catch wait spawn_result
set ryly_result [lindex \$spawn_result 3]
set r [expr \$ryly_result * 100 + \$expect_result ]
exit \$r
EOF

r=$?

if [ $r -eq 1 ]; then
echo "$(basename $0): success"
exit 0
else
echo "$(basename $0): fail: $r"
exit 1
fi
44 changes: 44 additions & 0 deletions tests/cases/tm2tm/scripts/test-create-channel-fail-unexist
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

set -eux

SCRIPT_DIR=$(cd $(dirname $0); pwd)
RLY_BINARY=${SCRIPT_DIR}/../../../../build/yrly
RLY="${RLY_BINARY} --debug"
CONFIG=$HOME/.yui-relayer/config/config.json

cp $CONFIG $CONFIG.tmp
cat $CONFIG.tmp \
| jq '.paths.ibc01.src["channel-id"] |= "channel-999"' \
| jq '.paths.ibc01.dst["channel-id"] |= "channel-999"' \
> $CONFIG

set +e
expect <<EOF
set timeout 5
set expect_result 0
spawn $RLY tx channel ibc01
expect {
"src channel id is given but that channel does not exist" {
set expect_result 1
exp_continue
}
eof
}
catch wait spawn_result
set ryly_result [lindex \$spawn_result 3]
set r [expr \$ryly_result * 100 + \$expect_result ]
exit \$r
EOF

r=$?

if [ $r -eq 101 ]; then
echo "$(basename $0): success"
cp $CONFIG.tmp $CONFIG
exit 0
else
echo "$(basename $0): fail: $r"
exit 1
fi

35 changes: 35 additions & 0 deletions tests/cases/tm2tm/scripts/test-create-client-fail-already-created
Original file line number Diff line number Diff line change
@@ -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 <<EOF
set timeout 5
set expect_result 0
spawn $RLY tx clients --src-height 2 ibc01
expect {
"clients are already created" {
set expect_result 1
exp_continue
}
eof
}
catch wait spawn_result
set ryly_result [lindex \$spawn_result 3]
set r [expr \$ryly_result * 100 + \$expect_result ]
exit \$r
EOF

r=$?

if [ $r -eq 1 ]; then
echo "$(basename $0): success"
exit 0
else
echo "$(basename $0): fail: $r"
exit 1
fi
Loading

0 comments on commit c00fc08

Please sign in to comment.