From 01e268762e86c4f896d9e9b81c1b25cc50b78195 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Tue, 28 Nov 2023 10:51:19 +0900 Subject: [PATCH 1/9] Update path config Signed-off-by: Dongri Jin --- cmd/chains.go | 36 +----------- cmd/config.go | 4 +- cmd/paths.go | 4 +- config/config.go | 25 +++++++- config/core.go | 59 +++++++++++++++++++ core/channel.go | 8 ++- core/client.go | 6 +- core/config.go | 65 +++++++++++++++++++++ core/connection.go | 8 ++- core/ics24.go | 9 +++ core/query.go | 32 ++++++++++ core/relayMsgs.go | 29 ++++++--- core/send.go | 9 +++ tests/cases/tm2tm/configs/path.json | 12 ++-- tests/cases/tmmock2tmmock/configs/path.json | 12 ++-- 15 files changed, 254 insertions(+), 64 deletions(-) create mode 100644 config/core.go diff --git a/cmd/chains.go b/cmd/chains.go index 3e75d1f2..cd94a01a 100644 --- a/cmd/chains.go +++ b/cmd/chains.go @@ -6,11 +6,9 @@ import ( "os" "path" - "github.com/cosmos/cosmos-sdk/client/flags" "github.com/hyperledger-labs/yui-relayer/config" "github.com/hyperledger-labs/yui-relayer/core" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func chainsCmd(ctx *config.Context) *cobra.Command { @@ -37,7 +35,7 @@ func chainsAddDirCmd(ctx *config.Context) *cobra.Command { if err := filesAdd(ctx, args[0]); err != nil { return err } - return overWriteConfig(ctx, cmd) + return ctx.Config.OverWriteConfig() }, } @@ -78,35 +76,3 @@ func filesAdd(ctx *config.Context, dir string) error { } return nil } - -func overWriteConfig(ctx *config.Context, cmd *cobra.Command) error { - home, err := cmd.Flags().GetString(flags.FlagHome) - if err != nil { - return err - } - - cfgPath := path.Join(home, "config", "config.yaml") - if _, err = os.Stat(cfgPath); err == nil { - viper.SetConfigFile(cfgPath) - if err = viper.ReadInConfig(); err == nil { - // ensure validateConfig runs properly - err = config.InitChains(ctx, homePath, debug) - if err != nil { - return err - } - - // marshal the new config - out, err := config.MarshalJSON(*ctx.Config) - if err != nil { - return err - } - - // overwrite the config file - err = os.WriteFile(viper.ConfigFileUsed(), out, 0600) - if err != nil { - return err - } - } - } - return err -} diff --git a/cmd/config.go b/cmd/config.go index 63e15220..a638a68f 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -117,7 +117,7 @@ func configShowCmd(ctx *config.Context) *cobra.Command { } func defaultConfig() []byte { - bz, err := json.Marshal(config.DefaultConfig()) + bz, err := json.Marshal(config.DefaultConfig(homePath)) if err != nil { panic(err) } @@ -157,7 +157,7 @@ func initConfig(ctx *config.Context, cmd *cobra.Command) error { } } } else { - defConfig := config.DefaultConfig() + defConfig := config.DefaultConfig(homePath) ctx.Config = &defConfig } return nil diff --git a/cmd/paths.go b/cmd/paths.go index 664e9031..240b4caf 100644 --- a/cmd/paths.go +++ b/cmd/paths.go @@ -103,7 +103,7 @@ func pathsAddCmd(ctx *config.Context) *cobra.Command { } } - return overWriteConfig(ctx, cmd) + return ctx.Config.OverWriteConfig() }, } return fileFlag(cmd) @@ -144,7 +144,7 @@ func pathsEditCmd(ctx *config.Context) *cobra.Command { default: return fmt.Errorf("invalid key: %s. Valid keys are: client-id, channel-id, connection-id, port-id", key) } - return overWriteConfig(ctx, cmd) + return ctx.Config.OverWriteConfig() }, } return cmd diff --git a/config/config.go b/config/config.go index f0023423..3d4a3155 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,10 @@ package config import ( + "encoding/json" "fmt" + "os" + "path" "time" "github.com/cosmos/cosmos-sdk/codec" @@ -17,9 +20,9 @@ type Config struct { chains Chains `yaml:"-" json:"-"` } -func DefaultConfig() Config { +func DefaultConfig(homePath string) Config { return Config{ - Global: newDefaultGlobalConfig(), + Global: newDefaultGlobalConfig(homePath), Chains: []core.ChainProverConfig{}, Paths: core.Paths{}, } @@ -30,6 +33,7 @@ type GlobalConfig struct { Timeout string `yaml:"timeout" json:"timeout"` LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"` LoggerConfig LoggerConfig `yaml:"logger" json:"logger"` + HomePath string `yaml:"home-path" json:"home-path"` } type LoggerConfig struct { @@ -39,7 +43,7 @@ type LoggerConfig struct { } // newDefaultGlobalConfig returns a global config with defaults set -func newDefaultGlobalConfig() GlobalConfig { +func newDefaultGlobalConfig(homePath string) GlobalConfig { return GlobalConfig{ Timeout: "10s", LightCacheSize: 20, @@ -48,6 +52,7 @@ func newDefaultGlobalConfig() GlobalConfig { Format: "json", Output: "stderr", }, + HomePath: homePath, } } @@ -95,6 +100,8 @@ func (c *Config) DeleteChain(chain string) *Config { // ChainsFromPath takes the path name and returns the properly configured chains func (c *Config) ChainsFromPath(path string) (map[string]*core.ProvableChain, string, string, error) { + initCoreConfig(*c, path) + pth, err := c.Paths.Get(path) if err != nil { return nil, "", "", err @@ -131,3 +138,15 @@ func InitChains(ctx *Context, homePath string, debug bool) error { return nil } + +func (c *Config) OverWriteConfig() error { + configData, err := json.Marshal(c) + if err != nil { + return err + } + cfgPath := path.Join(c.Global.HomePath, "config", "config.yaml") + if err := os.WriteFile(cfgPath, configData, 0600); err != nil { + return err + } + return nil +} diff --git a/config/core.go b/config/core.go new file mode 100644 index 00000000..3bfebba4 --- /dev/null +++ b/config/core.go @@ -0,0 +1,59 @@ +package config + +import ( + "encoding/json" + "fmt" + "os" + "path" + + "github.com/hyperledger-labs/yui-relayer/core" +) + +type CoreConfig struct { + config Config + pathName string +} + +var _ core.ConfigI = (*CoreConfig)(nil) + +func initCoreConfig(c Config, pathName string) { + config := &CoreConfig{ + config: c, + pathName: pathName, + } + core.CoreConfig(config) +} + +func (c CoreConfig) UpdateConfigID(chainID string, key core.PathEndName, ID string) error { + configPath, err := c.config.Paths.Get(c.pathName) + if err != nil { + return err + } + var pathEnd *core.PathEnd + if chainID == configPath.Src.ChainID { + pathEnd = configPath.Src + } + if chainID == configPath.Dst.ChainID { + pathEnd = configPath.Dst + } + if pathEnd == nil { + return fmt.Errorf("pathEnd is nil") + } + switch key { + case core.PathEndNameClient: + pathEnd.ClientID = ID + case core.PathEndNameConnection: + pathEnd.ConnectionID = ID + case core.PathEndNameChannel: + pathEnd.ChannelID = ID + } + configData, err := json.Marshal(c.config) + if err != nil { + return err + } + cfgPath := path.Join(c.config.Global.HomePath, "config", "config.yaml") + if err := os.WriteFile(cfgPath, configData, 0600); err != nil { + return err + } + return nil +} diff --git a/core/channel.go b/core/channel.go index 4885ca2a..869e22c3 100644 --- a/core/channel.go +++ b/core/channel.go @@ -34,7 +34,10 @@ func CreateChannel(src, dst *ProvableChain, to time.Duration) error { continue } - chanSteps.Send(src, dst) + srcMsgIDs, dstMsgIDs := chanSteps.Send(src, dst) + if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, PathEndNameChannel); err != nil { + return err + } switch { // In the case of success and this being the last transaction @@ -193,6 +196,9 @@ func logChannelStates(src, dst *ProvableChain, srcChan, dstChan *chantypes.Query func checkChannelFinality(src, dst *ProvableChain, srcChannel, dstChannel *chantypes.Channel) (bool, error) { logger := GetChannelPairLogger(src, dst) + if src.Chain.Path().ChannelID == "" && dst.Chain.Path().ChannelID == "" { + return true, nil + } sh, err := src.LatestHeight() if err != nil { return false, err diff --git a/core/client.go b/core/client.go index 9d9d4e81..7f058871 100644 --- a/core/client.go +++ b/core/client.go @@ -64,11 +64,15 @@ func CreateClients(src, dst *ProvableChain, srcHeight, dstHeight exported.Height // Send msgs to both chains if clients.Ready() { // TODO: Add retry here for out of gas or other errors - if clients.Send(src, dst); clients.Success() { + srcMsgIDs, dstMsgIDs := clients.Send(src, dst) + if clients.Success() { logger.Info( "★ Clients created", ) } + if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, PathEndNameClient); err != nil { + return err + } } return nil } diff --git a/core/config.go b/core/config.go index 0e2825c8..39a47544 100644 --- a/core/config.go +++ b/core/config.go @@ -5,12 +5,31 @@ import ( "errors" "fmt" + retry "github.com/avast/retry-go" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/gogoproto/proto" "github.com/hyperledger-labs/yui-relayer/log" "github.com/hyperledger-labs/yui-relayer/utils" ) +var config ConfigI + +type PathEndName string + +const ( + PathEndNameClient PathEndName = "client" + PathEndNameConnection PathEndName = "connection" + PathEndNameChannel PathEndName = "channel" +) + +type ConfigI interface { + UpdateConfigID(chainID string, key PathEndName, ID string) error +} + +func CoreConfig(c ConfigI) { + config = c +} + // ChainProverConfig defines the top level configuration for a chain instance type ChainProverConfig struct { Chain json.RawMessage `json:"chain" yaml:"chain"` // NOTE: it's any type as json format @@ -111,3 +130,49 @@ func (cc ChainProverConfig) Build() (*ProvableChain, error) { } return NewProvableChain(chain, prover), nil } + +func SyncChainConfigFromEvents(msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain, key PathEndName) error { + if err := ProcessPathMsgIDs(msgIDsSrc, src, key); err != nil { + return err + } + if err := ProcessPathMsgIDs(msgIDsDst, dst, key); err != nil { + return err + } + return nil +} + +func ProcessPathMsgIDs(msgIDs []MsgID, chain *ProvableChain, key PathEndName) error { + for _, msgID := range msgIDs { + msgRes, err := chain.Chain.GetMsgResult(msgID) + if err != nil { + return retry.Unrecoverable(fmt.Errorf("failed to get message result: %v", err)) + } else if ok, failureReason := msgRes.Status(); !ok { + return retry.Unrecoverable(fmt.Errorf("msg(id=%v) execution failed: %v", msgID, failureReason)) + } + + for _, event := range msgRes.Events() { + var id string + switch key { + case PathEndNameClient: + if clientIdentifier, ok := event.(*EventGenerateClientIdentifier); ok { + id = clientIdentifier.ID + } + case PathEndNameConnection: + if connectionIdentifier, ok := event.(*EventGenerateConnectionIdentifier); ok { + id = connectionIdentifier.ID + } + case PathEndNameChannel: + if channelIdentifier, ok := event.(*EventGenerateChannelIdentifier); ok { + id = channelIdentifier.ID + } + } + if id != "" { + if err := config.UpdateConfigID(chain.ChainID(), key, id); err != nil { + return err + } + } + } + + } + return nil +} diff --git a/core/connection.go b/core/connection.go index e2732435..d9e68fb9 100644 --- a/core/connection.go +++ b/core/connection.go @@ -43,7 +43,10 @@ func CreateConnection(src, dst *ProvableChain, to time.Duration) error { continue } - connSteps.Send(src, dst) + srcMsgIDs, dstMsgIDs := connSteps.Send(src, dst) + if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, PathEndNameConnection); err != nil { + return err + } switch { // In the case of success and this being the last transaction @@ -261,6 +264,9 @@ func mustGetAddress(chain interface { func checkConnectionFinality(src, dst *ProvableChain, srcConnection, dstConnection *conntypes.ConnectionEnd) (bool, error) { logger := GetConnectionPairLogger(src, dst) + if src.Path().ConnectionID == "" && dst.Path().ConnectionID == "" { + return true, nil + } sh, err := src.LatestHeight() if err != nil { return false, err diff --git a/core/ics24.go b/core/ics24.go index 62ecdabd..e2034ef7 100644 --- a/core/ics24.go +++ b/core/ics24.go @@ -9,16 +9,25 @@ import ( // Vclient validates the client identifier in the path func (pe *PathEnd) Vclient() error { + if pe.ClientID == "" { + return nil + } return host.ClientIdentifierValidator(pe.ClientID) } // Vconn validates the connection identifier in the path func (pe *PathEnd) Vconn() error { + if pe.ConnectionID == "" { + return nil + } return host.ConnectionIdentifierValidator(pe.ConnectionID) } // Vchan validates the channel identifier in the path func (pe *PathEnd) Vchan() error { + if pe.ChannelID == "" { + return nil + } return host.ChannelIdentifierValidator(pe.ChannelID) } diff --git a/core/query.go b/core/query.go index 942bbc38..5edd7d22 100644 --- a/core/query.go +++ b/core/query.go @@ -118,6 +118,14 @@ func QueryConnectionPair( ) (srcConn, dstConn *conntypes.QueryConnectionResponse, err error) { var eg = new(errgroup.Group) eg.Go(func() error { + if src.Path().ConnectionID == "" { + srcConn = &conntypes.QueryConnectionResponse{ + Connection: &conntypes.ConnectionEnd{ + State: conntypes.UNINITIALIZED, + }, + } + return nil + } var err error srcConn, err = src.QueryConnection(srcCtx) if err != nil { @@ -137,6 +145,14 @@ func QueryConnectionPair( return err }) eg.Go(func() error { + if dst.Path().ConnectionID == "" { + dstConn = &conntypes.QueryConnectionResponse{ + Connection: &conntypes.ConnectionEnd{ + State: conntypes.UNINITIALIZED, + }, + } + return nil + } var err error dstConn, err = dst.QueryConnection(dstCtx) if err != nil { @@ -166,6 +182,14 @@ func QueryChannelPair(srcCtx, dstCtx QueryContext, src, dst interface { }, prove bool) (srcChan, dstChan *chantypes.QueryChannelResponse, err error) { var eg = new(errgroup.Group) eg.Go(func() error { + if src.Path().ChannelID == "" { + srcChan = &chantypes.QueryChannelResponse{ + Channel: &chantypes.Channel{ + State: chantypes.UNINITIALIZED, + }, + } + return nil + } var err error srcChan, err = src.QueryChannel(srcCtx) if err != nil { @@ -185,6 +209,14 @@ func QueryChannelPair(srcCtx, dstCtx QueryContext, src, dst interface { return err }) eg.Go(func() error { + if dst.Path().ChannelID == "" { + dstChan = &chantypes.QueryChannelResponse{ + Channel: &chantypes.Channel{ + State: chantypes.UNINITIALIZED, + }, + } + return nil + } var err error dstChan, err = dst.QueryChannel(dstCtx) if err != nil { diff --git a/core/relayMsgs.go b/core/relayMsgs.go index 4b0c9600..1ec8f717 100644 --- a/core/relayMsgs.go +++ b/core/relayMsgs.go @@ -47,7 +47,7 @@ func (r *RelayMsgs) IsMaxTx(msgLen, txSize uint64) bool { // Send sends the messages with appropriate output // TODO: Parallelize? Maybe? -func (r *RelayMsgs) Send(src, dst Chain) { +func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { logger := GetChannelPairLogger(src, dst) //nolint:prealloc // can not be pre allocated var ( @@ -57,6 +57,8 @@ func (r *RelayMsgs) Send(src, dst Chain) { r.Succeeded = true + srcMsgIDs := []MsgID{} + dstMsgIDs := []MsgID{} // submit batches of relay transactions for _, msg := range r.Src { bz, err := proto.Marshal(msg) @@ -70,7 +72,9 @@ func (r *RelayMsgs) Send(src, dst Chain) { if r.IsMaxTx(msgLen, txSize) { // Submit the transactions to src chain and update its status - r.Succeeded = r.Succeeded && SendCheckMsgs(src, msgs) + msgIDs, err := SendMsgs(src, msgs) + r.Succeeded = r.Succeeded && err == nil + srcMsgIDs = append(srcMsgIDs, msgIDs...) // clear the current batch and reset variables msgLen, txSize = 1, uint64(len(bz)) @@ -80,8 +84,12 @@ func (r *RelayMsgs) Send(src, dst Chain) { } // submit leftover msgs - if len(msgs) > 0 && !SendCheckMsgs(src, msgs) { - r.Succeeded = false + if len(msgs) > 0 { + msgIDs, err := SendMsgs(src, msgs) + if err != nil { + r.Succeeded = false + } + srcMsgIDs = append(srcMsgIDs, msgIDs...) } // reset variables @@ -100,7 +108,9 @@ func (r *RelayMsgs) Send(src, dst Chain) { if r.IsMaxTx(msgLen, txSize) { // Submit the transaction to dst chain and update its status - r.Succeeded = r.Succeeded && SendCheckMsgs(dst, msgs) + msgIDs, err := SendMsgs(dst, msgs) + r.Succeeded = r.Succeeded && err == nil + dstMsgIDs = append(dstMsgIDs, msgIDs...) // clear the current batch and reset variables msgLen, txSize = 1, uint64(len(bz)) @@ -110,9 +120,14 @@ func (r *RelayMsgs) Send(src, dst Chain) { } // submit leftover msgs - if len(msgs) > 0 && !SendCheckMsgs(dst, msgs) { - r.Succeeded = false + if len(msgs) > 0 { + msgIDs, err := SendMsgs(dst, msgs) + if err != nil { + r.Succeeded = false + } + dstMsgIDs = append(dstMsgIDs, msgIDs...) } + return srcMsgIDs, dstMsgIDs } // Merge merges the argument into the receiver diff --git a/core/send.go b/core/send.go index 33c7cbbd..da8f1368 100644 --- a/core/send.go +++ b/core/send.go @@ -20,6 +20,15 @@ func SendCheckMsgs(chain Chain, msgs []types.Msg) bool { return true } +func SendMsgs(chain Chain, msgs []types.Msg) ([]MsgID, error) { + msgIDs, err := chain.SendMsgs(msgs) + if err != nil { + GetChainLogger(chain).Error("failed to send msgs", err, "msgs", msgs) + return nil, err + } + return msgIDs, nil +} + // GetFinalizedMsgResult is an utility function that waits for the finalization of the message execution and then returns the result. func GetFinalizedMsgResult(chain ProvableChain, msgID MsgID) (MsgResult, error) { var msgRes MsgResult diff --git a/tests/cases/tm2tm/configs/path.json b/tests/cases/tm2tm/configs/path.json index 52b485f2..1cf5cddc 100644 --- a/tests/cases/tm2tm/configs/path.json +++ b/tests/cases/tm2tm/configs/path.json @@ -1,18 +1,18 @@ { "src": { "chain-id": "ibc0", - "client-id": "07-tendermint-0", - "connection-id": "connection-0", - "channel-id": "channel-0", + "client-id": "", + "connection-id": "", + "channel-id": "", "port-id": "transfer", "order": "unordered", "version": "ics20-1" }, "dst": { "chain-id": "ibc1", - "client-id": "07-tendermint-0", - "connection-id": "connection-0", - "channel-id": "channel-0", + "client-id": "", + "connection-id": "", + "channel-id": "", "port-id": "transfer", "order": "unordered", "version": "ics20-1" diff --git a/tests/cases/tmmock2tmmock/configs/path.json b/tests/cases/tmmock2tmmock/configs/path.json index d104bff1..1cf5cddc 100644 --- a/tests/cases/tmmock2tmmock/configs/path.json +++ b/tests/cases/tmmock2tmmock/configs/path.json @@ -1,18 +1,18 @@ { "src": { "chain-id": "ibc0", - "client-id": "mock-client-0", - "connection-id": "connection-0", - "channel-id": "channel-0", + "client-id": "", + "connection-id": "", + "channel-id": "", "port-id": "transfer", "order": "unordered", "version": "ics20-1" }, "dst": { "chain-id": "ibc1", - "client-id": "mock-client-0", - "connection-id": "connection-0", - "channel-id": "channel-0", + "client-id": "", + "connection-id": "", + "channel-id": "", "port-id": "transfer", "order": "unordered", "version": "ics20-1" From 88198637e0741852ff8c464f1e47e3f6d735c46d Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Tue, 5 Dec 2023 21:40:30 +0900 Subject: [PATCH 2/9] Refactoring core config Signed-off-by: Dongri Jin --- cmd/config.go | 1 + cmd/root.go | 2 +- config/config.go | 22 ++++++++++++++-------- config/core.go | 38 ++++++++++++++------------------------ core/channel.go | 2 +- core/client.go | 2 +- core/config.go | 35 +++++++++++++++++------------------ core/connection.go | 2 +- core/relayMsgs.go | 20 ++++++++------------ core/send.go | 16 ++++------------ 10 files changed, 62 insertions(+), 78 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index a638a68f..c945bacc 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -160,5 +160,6 @@ func initConfig(ctx *config.Context, cmd *cobra.Command) error { defConfig := config.DefaultConfig(homePath) ctx.Config = &defConfig } + ctx.Config.InitCoreConfig() return nil } diff --git a/cmd/root.go b/cmd/root.go index d4012fec..5a2cf7e9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,7 +53,7 @@ func Execute(modules ...config.ModuleI) error { for _, module := range modules { module.RegisterInterfaces(codec.InterfaceRegistry()) } - ctx := &config.Context{Modules: modules, Config: &config.Config{}, Codec: codec} + ctx := &config.Context{Modules: modules, Config: &config.Config{HomePath: homePath}, Codec: codec} // Register subcommands diff --git a/config/config.go b/config/config.go index 3d4a3155..da33614d 100644 --- a/config/config.go +++ b/config/config.go @@ -18,13 +18,17 @@ type Config struct { // cache chains Chains `yaml:"-" json:"-"` + + HomePath string `yaml:"-" json:"-"` + Path string `yaml:"-" json:"-"` } func DefaultConfig(homePath string) Config { return Config{ - Global: newDefaultGlobalConfig(homePath), - Chains: []core.ChainProverConfig{}, - Paths: core.Paths{}, + Global: newDefaultGlobalConfig(), + Chains: []core.ChainProverConfig{}, + Paths: core.Paths{}, + HomePath: homePath, } } @@ -33,7 +37,6 @@ type GlobalConfig struct { Timeout string `yaml:"timeout" json:"timeout"` LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"` LoggerConfig LoggerConfig `yaml:"logger" json:"logger"` - HomePath string `yaml:"home-path" json:"home-path"` } type LoggerConfig struct { @@ -43,7 +46,7 @@ type LoggerConfig struct { } // newDefaultGlobalConfig returns a global config with defaults set -func newDefaultGlobalConfig(homePath string) GlobalConfig { +func newDefaultGlobalConfig() GlobalConfig { return GlobalConfig{ Timeout: "10s", LightCacheSize: 20, @@ -52,10 +55,13 @@ func newDefaultGlobalConfig(homePath string) GlobalConfig { Format: "json", Output: "stderr", }, - HomePath: homePath, } } +func (c *Config) InitCoreConfig() { + initCoreConfig(c) +} + func (c *Config) GetChain(chainID string) (*core.ProvableChain, error) { return c.chains.Get(chainID) } @@ -100,7 +106,7 @@ func (c *Config) DeleteChain(chain string) *Config { // ChainsFromPath takes the path name and returns the properly configured chains func (c *Config) ChainsFromPath(path string) (map[string]*core.ProvableChain, string, string, error) { - initCoreConfig(*c, path) + c.Path = path pth, err := c.Paths.Get(path) if err != nil { @@ -144,7 +150,7 @@ func (c *Config) OverWriteConfig() error { if err != nil { return err } - cfgPath := path.Join(c.Global.HomePath, "config", "config.yaml") + cfgPath := path.Join(c.HomePath, "config", "config.yaml") if err := os.WriteFile(cfgPath, configData, 0600); err != nil { return err } diff --git a/config/core.go b/config/core.go index 3bfebba4..73c8ebc4 100644 --- a/config/core.go +++ b/config/core.go @@ -1,31 +1,26 @@ package config import ( - "encoding/json" "fmt" - "os" - "path" "github.com/hyperledger-labs/yui-relayer/core" ) type CoreConfig struct { - config Config - pathName string + config *Config } var _ core.ConfigI = (*CoreConfig)(nil) -func initCoreConfig(c Config, pathName string) { +func initCoreConfig(c *Config) { config := &CoreConfig{ - config: c, - pathName: pathName, + config: c, } - core.CoreConfig(config) + core.SetCoreConfig(config) } -func (c CoreConfig) UpdateConfigID(chainID string, key core.PathEndName, ID string) error { - configPath, err := c.config.Paths.Get(c.pathName) +func (c CoreConfig) UpdateConfigID(chainID string, configID core.ConfigIDType, id string) error { + configPath, err := c.config.Paths.Get(c.config.Path) if err != nil { return err } @@ -39,20 +34,15 @@ func (c CoreConfig) UpdateConfigID(chainID string, key core.PathEndName, ID stri if pathEnd == nil { return fmt.Errorf("pathEnd is nil") } - switch key { - case core.PathEndNameClient: - pathEnd.ClientID = ID - case core.PathEndNameConnection: - pathEnd.ConnectionID = ID - case core.PathEndNameChannel: - pathEnd.ChannelID = ID + switch configID { + case core.ConfigIDClient: + pathEnd.ClientID = id + case core.ConfigIDConnection: + pathEnd.ConnectionID = id + case core.ConfigIDChannel: + pathEnd.ChannelID = id } - configData, err := json.Marshal(c.config) - if err != nil { - return err - } - cfgPath := path.Join(c.config.Global.HomePath, "config", "config.yaml") - if err := os.WriteFile(cfgPath, configData, 0600); err != nil { + if err := c.config.OverWriteConfig(); err != nil { return err } return nil diff --git a/core/channel.go b/core/channel.go index 869e22c3..e51f1180 100644 --- a/core/channel.go +++ b/core/channel.go @@ -35,7 +35,7 @@ func CreateChannel(src, dst *ProvableChain, to time.Duration) error { } srcMsgIDs, dstMsgIDs := chanSteps.Send(src, dst) - if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, PathEndNameChannel); err != nil { + if err := SyncChainConfigsFromEvents(srcMsgIDs, dstMsgIDs, src, dst, ConfigIDChannel); err != nil { return err } diff --git a/core/client.go b/core/client.go index 7f058871..e83975ed 100644 --- a/core/client.go +++ b/core/client.go @@ -70,7 +70,7 @@ func CreateClients(src, dst *ProvableChain, srcHeight, dstHeight exported.Height "★ Clients created", ) } - if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, PathEndNameClient); err != nil { + if err := SyncChainConfigsFromEvents(srcMsgIDs, dstMsgIDs, src, dst, ConfigIDClient); err != nil { return err } } diff --git a/core/config.go b/core/config.go index 39a47544..65ca09e8 100644 --- a/core/config.go +++ b/core/config.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" - retry "github.com/avast/retry-go" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/gogoproto/proto" "github.com/hyperledger-labs/yui-relayer/log" @@ -14,19 +13,19 @@ import ( var config ConfigI -type PathEndName string +type ConfigIDType string const ( - PathEndNameClient PathEndName = "client" - PathEndNameConnection PathEndName = "connection" - PathEndNameChannel PathEndName = "channel" + ConfigIDClient ConfigIDType = "client" + ConfigIDConnection ConfigIDType = "connection" + ConfigIDChannel ConfigIDType = "channel" ) type ConfigI interface { - UpdateConfigID(chainID string, key PathEndName, ID string) error + UpdateConfigID(chainID string, configID ConfigIDType, id string) error } -func CoreConfig(c ConfigI) { +func SetCoreConfig(c ConfigI) { config = c } @@ -131,43 +130,43 @@ func (cc ChainProverConfig) Build() (*ProvableChain, error) { return NewProvableChain(chain, prover), nil } -func SyncChainConfigFromEvents(msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain, key PathEndName) error { - if err := ProcessPathMsgIDs(msgIDsSrc, src, key); err != nil { +func SyncChainConfigsFromEvents(msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain, configID ConfigIDType) error { + if err := SyncChainConfigFromEvents(msgIDsSrc, src, configID); err != nil { return err } - if err := ProcessPathMsgIDs(msgIDsDst, dst, key); err != nil { + if err := SyncChainConfigFromEvents(msgIDsDst, dst, configID); err != nil { return err } return nil } -func ProcessPathMsgIDs(msgIDs []MsgID, chain *ProvableChain, key PathEndName) error { +func SyncChainConfigFromEvents(msgIDs []MsgID, chain *ProvableChain, configID ConfigIDType) error { for _, msgID := range msgIDs { msgRes, err := chain.Chain.GetMsgResult(msgID) if err != nil { - return retry.Unrecoverable(fmt.Errorf("failed to get message result: %v", err)) + return fmt.Errorf("failed to get message result: %v", err) } else if ok, failureReason := msgRes.Status(); !ok { - return retry.Unrecoverable(fmt.Errorf("msg(id=%v) execution failed: %v", msgID, failureReason)) + return fmt.Errorf("msg(id=%v) execution failed: %v", msgID, failureReason) } for _, event := range msgRes.Events() { var id string - switch key { - case PathEndNameClient: + switch configID { + case ConfigIDClient: if clientIdentifier, ok := event.(*EventGenerateClientIdentifier); ok { id = clientIdentifier.ID } - case PathEndNameConnection: + case ConfigIDConnection: if connectionIdentifier, ok := event.(*EventGenerateConnectionIdentifier); ok { id = connectionIdentifier.ID } - case PathEndNameChannel: + case ConfigIDChannel: if channelIdentifier, ok := event.(*EventGenerateChannelIdentifier); ok { id = channelIdentifier.ID } } if id != "" { - if err := config.UpdateConfigID(chain.ChainID(), key, id); err != nil { + if err := config.UpdateConfigID(chain.ChainID(), configID, id); err != nil { return err } } diff --git a/core/connection.go b/core/connection.go index d9e68fb9..e9c7d1e9 100644 --- a/core/connection.go +++ b/core/connection.go @@ -44,7 +44,7 @@ func CreateConnection(src, dst *ProvableChain, to time.Duration) error { } srcMsgIDs, dstMsgIDs := connSteps.Send(src, dst) - if err := SyncChainConfigFromEvents(srcMsgIDs, dstMsgIDs, src, dst, PathEndNameConnection); err != nil { + if err := SyncChainConfigsFromEvents(srcMsgIDs, dstMsgIDs, src, dst, ConfigIDConnection); err != nil { return err } diff --git a/core/relayMsgs.go b/core/relayMsgs.go index 1ec8f717..f322eb3f 100644 --- a/core/relayMsgs.go +++ b/core/relayMsgs.go @@ -72,8 +72,8 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { if r.IsMaxTx(msgLen, txSize) { // Submit the transactions to src chain and update its status - msgIDs, err := SendMsgs(src, msgs) - r.Succeeded = r.Succeeded && err == nil + msgIDs, ok := SendCheckMsgs(src, msgs) + r.Succeeded = r.Succeeded && ok srcMsgIDs = append(srcMsgIDs, msgIDs...) // clear the current batch and reset variables @@ -85,10 +85,8 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { // submit leftover msgs if len(msgs) > 0 { - msgIDs, err := SendMsgs(src, msgs) - if err != nil { - r.Succeeded = false - } + msgIDs, ok := SendCheckMsgs(src, msgs) + r.Succeeded = ok srcMsgIDs = append(srcMsgIDs, msgIDs...) } @@ -108,8 +106,8 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { if r.IsMaxTx(msgLen, txSize) { // Submit the transaction to dst chain and update its status - msgIDs, err := SendMsgs(dst, msgs) - r.Succeeded = r.Succeeded && err == nil + msgIDs, ok := SendCheckMsgs(dst, msgs) + r.Succeeded = r.Succeeded && ok dstMsgIDs = append(dstMsgIDs, msgIDs...) // clear the current batch and reset variables @@ -121,10 +119,8 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { // submit leftover msgs if len(msgs) > 0 { - msgIDs, err := SendMsgs(dst, msgs) - if err != nil { - r.Succeeded = false - } + msgIDs, ok := SendCheckMsgs(dst, msgs) + r.Succeeded = ok dstMsgIDs = append(dstMsgIDs, msgIDs...) } return srcMsgIDs, dstMsgIDs diff --git a/core/send.go b/core/send.go index da8f1368..d17bb37c 100644 --- a/core/send.go +++ b/core/send.go @@ -9,24 +9,16 @@ import ( ) // SendCheckMsgs is an utility function that executes `Chain::SendMsgs` and checks the execution results of all the messages. -func SendCheckMsgs(chain Chain, msgs []types.Msg) bool { +func SendCheckMsgs(chain Chain, msgs []types.Msg) ([]MsgID, bool) { logger := GetChainLogger(chain) now := time.Now() - if _, err := chain.SendMsgs(msgs); err != nil { - GetChainLogger(chain).Error("failed to send msgs", err, "msgs", msgs) - return false - } - logger.TimeTrack(now, "SendMsgs", "num_msgs", len(msgs)) - return true -} - -func SendMsgs(chain Chain, msgs []types.Msg) ([]MsgID, error) { msgIDs, err := chain.SendMsgs(msgs) if err != nil { GetChainLogger(chain).Error("failed to send msgs", err, "msgs", msgs) - return nil, err + return nil, false } - return msgIDs, nil + logger.TimeTrack(now, "SendMsgs", "num_msgs", len(msgs)) + return msgIDs, true } // GetFinalizedMsgResult is an utility function that waits for the finalization of the message execution and then returns the result. From 6cd8ddfac9f81b28a1bcd55c42ea7b4bfaa1b3b0 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Wed, 6 Dec 2023 17:52:13 +0900 Subject: [PATCH 3/9] Add pathName to CreateClient, CreateConnection, CreateChannel Signed-off-by: Dongri Jin --- cmd/tx.go | 16 ++++++++++------ config/config.go | 2 -- config/core.go | 4 ++-- core/channel.go | 4 ++-- core/client.go | 4 ++-- core/config.go | 15 +++++++++------ core/connection.go | 4 ++-- 7 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cmd/tx.go b/cmd/tx.go index b1b61f99..160fa7da 100644 --- a/cmd/tx.go +++ b/cmd/tx.go @@ -56,7 +56,8 @@ func createClientsCmd(ctx *config.Context) *cobra.Command { " path by querying headers from each chain and then sending the corresponding create-client messages", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - c, src, dst, err := ctx.Config.ChainsFromPath(args[0]) + pathName := args[0] + c, src, dst, err := ctx.Config.ChainsFromPath(pathName) if err != nil { return err } @@ -93,7 +94,7 @@ func createClientsCmd(ctx *config.Context) *cobra.Command { dstHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height) } - return core.CreateClients(c[src], c[dst], srcHeight, dstHeight) + return core.CreateClients(pathName, c[src], c[dst], srcHeight, dstHeight) }, } cmd.Flags().Uint64(flagSrcHeight, defaultSrcHeight, "src header at this height is submitted to dst chain") @@ -136,7 +137,8 @@ func createConnectionCmd(ctx *config.Context) *cobra.Command { a connection between two chains with a configured path in the config file`), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - c, src, dst, err := ctx.Config.ChainsFromPath(args[0]) + pathName := args[0] + c, src, dst, err := ctx.Config.ChainsFromPath(pathName) if err != nil { return err } @@ -154,7 +156,7 @@ func createConnectionCmd(ctx *config.Context) *cobra.Command { return err } - return core.CreateConnection(c[src], c[dst], to) + return core.CreateConnection(pathName, c[src], c[dst], to) }, } @@ -169,7 +171,8 @@ func createChannelCmd(ctx *config.Context) *cobra.Command { create a channel between two chains with a configured path in the config file`), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - c, src, dst, err := ctx.Config.ChainsFromPath(args[0]) + pathName := args[0] + c, src, dst, err := ctx.Config.ChainsFromPath(pathName) if err != nil { return err } @@ -186,7 +189,8 @@ func createChannelCmd(ctx *config.Context) *cobra.Command { if _, err = c[dst].GetAddress(); err != nil { return err } - return core.CreateChannel(c[src], c[dst], to) + + return core.CreateChannel(pathName, c[src], c[dst], to) }, } diff --git a/config/config.go b/config/config.go index da33614d..9b12a7b4 100644 --- a/config/config.go +++ b/config/config.go @@ -20,7 +20,6 @@ type Config struct { chains Chains `yaml:"-" json:"-"` HomePath string `yaml:"-" json:"-"` - Path string `yaml:"-" json:"-"` } func DefaultConfig(homePath string) Config { @@ -106,7 +105,6 @@ func (c *Config) DeleteChain(chain string) *Config { // ChainsFromPath takes the path name and returns the properly configured chains func (c *Config) ChainsFromPath(path string) (map[string]*core.ProvableChain, string, string, error) { - c.Path = path pth, err := c.Paths.Get(path) if err != nil { diff --git a/config/core.go b/config/core.go index 73c8ebc4..08055b99 100644 --- a/config/core.go +++ b/config/core.go @@ -19,8 +19,8 @@ func initCoreConfig(c *Config) { core.SetCoreConfig(config) } -func (c CoreConfig) UpdateConfigID(chainID string, configID core.ConfigIDType, id string) error { - configPath, err := c.config.Paths.Get(c.config.Path) +func (c CoreConfig) UpdateConfigID(pathName string, chainID string, configID core.ConfigIDType, id string) error { + configPath, err := c.config.Paths.Get(pathName) if err != nil { return err } diff --git a/core/channel.go b/core/channel.go index e51f1180..07d41bba 100644 --- a/core/channel.go +++ b/core/channel.go @@ -13,7 +13,7 @@ import ( // CreateChannel runs the channel creation messages on timeout until they pass // TODO: add max retries or something to this function -func CreateChannel(src, dst *ProvableChain, to time.Duration) error { +func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) error { logger := GetChannelPairLogger(src, dst) defer logger.TimeTrack(time.Now(), "CreateChannel") @@ -35,7 +35,7 @@ func CreateChannel(src, dst *ProvableChain, to time.Duration) error { } srcMsgIDs, dstMsgIDs := chanSteps.Send(src, dst) - if err := SyncChainConfigsFromEvents(srcMsgIDs, dstMsgIDs, src, dst, ConfigIDChannel); err != nil { + if err := SyncChainConfigsFromEvents(pathName, srcMsgIDs, dstMsgIDs, src, dst, ConfigIDChannel); err != nil { return err } diff --git a/core/client.go b/core/client.go index e83975ed..4b142b12 100644 --- a/core/client.go +++ b/core/client.go @@ -10,7 +10,7 @@ import ( "github.com/hyperledger-labs/yui-relayer/log" ) -func CreateClients(src, dst *ProvableChain, srcHeight, dstHeight exported.Height) error { +func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeight exported.Height) error { logger := GetChainPairLogger(src, dst) defer logger.TimeTrack(time.Now(), "CreateClients") var ( @@ -70,7 +70,7 @@ func CreateClients(src, dst *ProvableChain, srcHeight, dstHeight exported.Height "★ Clients created", ) } - if err := SyncChainConfigsFromEvents(srcMsgIDs, dstMsgIDs, src, dst, ConfigIDClient); err != nil { + if err := SyncChainConfigsFromEvents(pathName, srcMsgIDs, dstMsgIDs, src, dst, ConfigIDClient); err != nil { return err } } diff --git a/core/config.go b/core/config.go index 65ca09e8..70701631 100644 --- a/core/config.go +++ b/core/config.go @@ -22,10 +22,13 @@ const ( ) type ConfigI interface { - UpdateConfigID(chainID string, configID ConfigIDType, id string) error + UpdateConfigID(pathName string, chainID string, configID ConfigIDType, id string) error } func SetCoreConfig(c ConfigI) { + if config != nil { + panic("core config already set") + } config = c } @@ -130,17 +133,17 @@ func (cc ChainProverConfig) Build() (*ProvableChain, error) { return NewProvableChain(chain, prover), nil } -func SyncChainConfigsFromEvents(msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain, configID ConfigIDType) error { - if err := SyncChainConfigFromEvents(msgIDsSrc, src, configID); err != nil { +func SyncChainConfigsFromEvents(pathName string, msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain, configID ConfigIDType) error { + if err := SyncChainConfigFromEvents(pathName, msgIDsSrc, src, configID); err != nil { return err } - if err := SyncChainConfigFromEvents(msgIDsDst, dst, configID); err != nil { + if err := SyncChainConfigFromEvents(pathName, msgIDsDst, dst, configID); err != nil { return err } return nil } -func SyncChainConfigFromEvents(msgIDs []MsgID, chain *ProvableChain, configID ConfigIDType) error { +func SyncChainConfigFromEvents(pathName string, msgIDs []MsgID, chain *ProvableChain, configID ConfigIDType) error { for _, msgID := range msgIDs { msgRes, err := chain.Chain.GetMsgResult(msgID) if err != nil { @@ -166,7 +169,7 @@ func SyncChainConfigFromEvents(msgIDs []MsgID, chain *ProvableChain, configID Co } } if id != "" { - if err := config.UpdateConfigID(chain.ChainID(), configID, id); err != nil { + if err := config.UpdateConfigID(pathName, chain.ChainID(), configID, id); err != nil { return err } } diff --git a/core/connection.go b/core/connection.go index e9c7d1e9..0099a24a 100644 --- a/core/connection.go +++ b/core/connection.go @@ -22,7 +22,7 @@ var ( rtyErr = retry.LastErrorOnly(true) ) -func CreateConnection(src, dst *ProvableChain, to time.Duration) error { +func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration) error { logger := GetConnectionPairLogger(src, dst) defer logger.TimeTrack(time.Now(), "CreateConnection") ticker := time.NewTicker(to) @@ -44,7 +44,7 @@ func CreateConnection(src, dst *ProvableChain, to time.Duration) error { } srcMsgIDs, dstMsgIDs := connSteps.Send(src, dst) - if err := SyncChainConfigsFromEvents(srcMsgIDs, dstMsgIDs, src, dst, ConfigIDConnection); err != nil { + if err := SyncChainConfigsFromEvents(pathName, srcMsgIDs, dstMsgIDs, src, dst, ConfigIDConnection); err != nil { return err } From 4a6607c455edc6775abd4fecd2c324950d4eb567 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Tue, 12 Dec 2023 16:10:55 +0900 Subject: [PATCH 4/9] Fix config.yaml to config.json Refactoring config path Signed-off-by: Dongri Jin --- cmd/config.go | 89 ++++++++++++++---------------------------------- cmd/root.go | 5 +-- config/config.go | 16 ++++----- 3 files changed, 35 insertions(+), 75 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index c945bacc..65e2e229 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -4,12 +4,10 @@ import ( "encoding/json" "fmt" "os" - "path" + "path/filepath" - "github.com/cosmos/cosmos-sdk/client/flags" "github.com/hyperledger-labs/yui-relayer/config" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func configCmd(ctx *config.Context) *cobra.Command { @@ -22,44 +20,26 @@ func configCmd(ctx *config.Context) *cobra.Command { cmd.AddCommand( configShowCmd(ctx), - configInitCmd(), + configInitCmd(ctx), ) return cmd } // Command for inititalizing an empty config at the --home location -func configInitCmd() *cobra.Command { +func configInitCmd(ctx *config.Context) *cobra.Command { cmd := &cobra.Command{ Use: "init", Aliases: []string{"i"}, Short: "Creates a default home directory at path defined by --home", RunE: func(cmd *cobra.Command, args []string) error { - home, err := cmd.Flags().GetString(flags.FlagHome) - if err != nil { - return err - } - - cfgDir := path.Join(home, "config") - cfgPath := path.Join(cfgDir, "config.yaml") - + cfgPath := ctx.Config.ConfigPath // If the config doesn't exist... if _, err := os.Stat(cfgPath); os.IsNotExist(err) { - // And the config folder doesn't exist... - if _, err := os.Stat(cfgDir); os.IsNotExist(err) { - // And the home folder doesn't exist - if _, err := os.Stat(home); os.IsNotExist(err) { - // Create the home folder - if err = os.Mkdir(home, os.ModePerm); err != nil { - return err - } - } - // Create the home config folder - if err = os.Mkdir(cfgDir, os.ModePerm); err != nil { - return err - } + dirPath := filepath.Dir(cfgPath) + if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { + return err } - // Then create the file... f, err := os.Create(cfgPath) if err != nil { @@ -90,16 +70,8 @@ func configShowCmd(ctx *config.Context) *cobra.Command { Aliases: []string{"s", "list", "l"}, Short: "Prints current configuration", RunE: func(cmd *cobra.Command, args []string) error { - home, err := cmd.Flags().GetString(flags.FlagHome) - if err != nil { - return err - } - - cfgPath := path.Join(home, "config", "config.yaml") + cfgPath := ctx.Config.ConfigPath if _, err := os.Stat(cfgPath); os.IsNotExist(err) { - if _, err := os.Stat(home); os.IsNotExist(err) { - return fmt.Errorf("home path does not exist: %s", home) - } return fmt.Errorf("config does not exist: %s", cfgPath) } @@ -117,7 +89,7 @@ func configShowCmd(ctx *config.Context) *cobra.Command { } func defaultConfig() []byte { - bz, err := json.Marshal(config.DefaultConfig(homePath)) + bz, err := json.Marshal(config.DefaultConfig(fmt.Sprintf("%s/%s", homePath, configPath))) if err != nil { panic(err) } @@ -126,38 +98,27 @@ func defaultConfig() []byte { // initConfig reads in config file and ENV variables if set. func initConfig(ctx *config.Context, cmd *cobra.Command) error { - home, err := cmd.PersistentFlags().GetString(flags.FlagHome) - if err != nil { - return err - } - - cfgPath := path.Join(home, "config", "config.yaml") + cfgPath := ctx.Config.ConfigPath if _, err := os.Stat(cfgPath); err == nil { - viper.SetConfigFile(cfgPath) - if err := viper.ReadInConfig(); err == nil { - // read the config file bytes - file, err := os.ReadFile(viper.ConfigFileUsed()) - if err != nil { - fmt.Println("Error reading file:", err) - os.Exit(1) - } + file, err := os.ReadFile(cfgPath) + if err != nil { + fmt.Println("Error reading file:", err) + os.Exit(1) + } - // unmarshall them into the struct - err = config.UnmarshalJSON(ctx.Codec, file, ctx.Config) - if err != nil { - fmt.Println("Error unmarshalling config:", err) - os.Exit(1) - } + // unmarshall them into the struct + if err = config.UnmarshalJSON(ctx.Codec, file, ctx.Config); err != nil { + fmt.Println("Error unmarshalling config:", err) + os.Exit(1) + } - // ensure config has []*relayer.Chain used for all chain operations - err = config.InitChains(ctx, homePath, debug) - if err != nil { - fmt.Println("Error parsing chain config:", err) - os.Exit(1) - } + // ensure config has []*relayer.Chain used for all chain operations + if err = config.InitChains(ctx, homePath, debug); err != nil { + fmt.Println("Error parsing chain config:", err) + os.Exit(1) } } else { - defConfig := config.DefaultConfig(homePath) + defConfig := config.DefaultConfig(fmt.Sprintf("%s/%s", homePath, configPath)) ctx.Config = &defConfig } ctx.Config.InitCoreConfig() diff --git a/cmd/root.go b/cmd/root.go index 5a2cf7e9..ab3221ec 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -21,6 +21,7 @@ var ( homePath string debug bool defaultHome = os.ExpandEnv("$HOME/.yui-relayer") + configPath = "config/config.json" ) // Execute adds all child commands to the root command and sets flags appropriately. @@ -53,7 +54,7 @@ func Execute(modules ...config.ModuleI) error { for _, module := range modules { module.RegisterInterfaces(codec.InterfaceRegistry()) } - ctx := &config.Context{Modules: modules, Config: &config.Config{HomePath: homePath}, Codec: codec} + ctx := &config.Context{Modules: modules, Config: &config.Config{ConfigPath: fmt.Sprintf("%s/%s", homePath, configPath)}, Codec: codec} // Register subcommands @@ -74,7 +75,7 @@ func Execute(modules ...config.ModuleI) error { } rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { - // reads `homeDir/config/config.yaml` into `var config *Config` before each command + // reads `homeDir/config/config.json` into `var config *Config` before each command if err := viper.BindPFlags(cmd.Flags()); err != nil { return fmt.Errorf("failed to bind the flag set to the configuration: %v", err) } diff --git a/config/config.go b/config/config.go index 9b12a7b4..076e2070 100644 --- a/config/config.go +++ b/config/config.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "os" - "path" "time" "github.com/cosmos/cosmos-sdk/codec" @@ -19,15 +18,15 @@ type Config struct { // cache chains Chains `yaml:"-" json:"-"` - HomePath string `yaml:"-" json:"-"` + ConfigPath string `yaml:"-" json:"-"` } -func DefaultConfig(homePath string) Config { +func DefaultConfig(configPath string) Config { return Config{ - Global: newDefaultGlobalConfig(), - Chains: []core.ChainProverConfig{}, - Paths: core.Paths{}, - HomePath: homePath, + Global: newDefaultGlobalConfig(), + Chains: []core.ChainProverConfig{}, + Paths: core.Paths{}, + ConfigPath: configPath, } } @@ -148,8 +147,7 @@ func (c *Config) OverWriteConfig() error { if err != nil { return err } - cfgPath := path.Join(c.HomePath, "config", "config.yaml") - if err := os.WriteFile(cfgPath, configData, 0600); err != nil { + if err := os.WriteFile(c.ConfigPath, configData, 0600); err != nil { return err } return nil From 345878aeda24baf2b27b6ab3d3c845af73f16771 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Tue, 19 Dec 2023 17:32:42 +0900 Subject: [PATCH 5/9] * Remove SendCheckMsgs * Fix succeeded * Add SentSrcMsgIDs, SentDstMsgIDs * Check send success Signed-off-by: Dongri Jin --- core/channel.go | 8 +++++--- core/client.go | 8 ++++---- core/connection.go | 8 +++++--- core/relayMsgs.go | 40 ++++++++++++++++++++++++++++------------ core/send.go | 14 -------------- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/core/channel.go b/core/channel.go index 07d41bba..b55b3275 100644 --- a/core/channel.go +++ b/core/channel.go @@ -34,9 +34,11 @@ func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) e continue } - srcMsgIDs, dstMsgIDs := chanSteps.Send(src, dst) - if err := SyncChainConfigsFromEvents(pathName, srcMsgIDs, dstMsgIDs, src, dst, ConfigIDChannel); err != nil { - return err + chanSteps.Send(src, dst) + if chanSteps.Success() { + if err := SyncChainConfigsFromEvents(pathName, chanSteps.SentSrcMsgIDs, chanSteps.SentDstMsgIDs, src, dst, ConfigIDChannel); err != nil { + return err + } } switch { diff --git a/core/client.go b/core/client.go index 4b142b12..8c9ba988 100644 --- a/core/client.go +++ b/core/client.go @@ -64,14 +64,14 @@ func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeigh // Send msgs to both chains if clients.Ready() { // TODO: Add retry here for out of gas or other errors - srcMsgIDs, dstMsgIDs := clients.Send(src, dst) + clients.Send(src, dst) if clients.Success() { logger.Info( "★ Clients created", ) - } - if err := SyncChainConfigsFromEvents(pathName, srcMsgIDs, dstMsgIDs, src, dst, ConfigIDClient); err != nil { - return err + if err := SyncChainConfigsFromEvents(pathName, clients.SentSrcMsgIDs, clients.SentDstMsgIDs, src, dst, ConfigIDClient); err != nil { + return err + } } } return nil diff --git a/core/connection.go b/core/connection.go index 0099a24a..116820f6 100644 --- a/core/connection.go +++ b/core/connection.go @@ -43,9 +43,11 @@ func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration continue } - srcMsgIDs, dstMsgIDs := connSteps.Send(src, dst) - if err := SyncChainConfigsFromEvents(pathName, srcMsgIDs, dstMsgIDs, src, dst, ConfigIDConnection); err != nil { - return err + connSteps.Send(src, dst) + if connSteps.Success() { + if err := SyncChainConfigsFromEvents(pathName, connSteps.SentSrcMsgIDs, connSteps.SentDstMsgIDs, src, dst, ConfigIDConnection); err != nil { + return err + } } switch { diff --git a/core/relayMsgs.go b/core/relayMsgs.go index f322eb3f..54043c2c 100644 --- a/core/relayMsgs.go +++ b/core/relayMsgs.go @@ -16,6 +16,9 @@ type RelayMsgs struct { Last bool `json:"last"` Succeeded bool `json:"success"` + + SentSrcMsgIDs []MsgID `json:"sent_src_msg_ids"` + SentDstMsgIDs []MsgID `json:"sent_dst_msg_ids"` } // NewRelayMsgs returns an initialized version of relay messages @@ -47,7 +50,7 @@ func (r *RelayMsgs) IsMaxTx(msgLen, txSize uint64) bool { // Send sends the messages with appropriate output // TODO: Parallelize? Maybe? -func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { +func (r *RelayMsgs) Send(src, dst Chain) { logger := GetChannelPairLogger(src, dst) //nolint:prealloc // can not be pre allocated var ( @@ -57,8 +60,8 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { r.Succeeded = true - srcMsgIDs := []MsgID{} - dstMsgIDs := []MsgID{} + srcMsgIDs := make([]MsgID, len(r.Src)) + dstMsgIDs := make([]MsgID, len(r.Dst)) // submit batches of relay transactions for _, msg := range r.Src { bz, err := proto.Marshal(msg) @@ -72,8 +75,11 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { if r.IsMaxTx(msgLen, txSize) { // Submit the transactions to src chain and update its status - msgIDs, ok := SendCheckMsgs(src, msgs) - r.Succeeded = r.Succeeded && ok + msgIDs, err := src.SendMsgs(msgs) + if err != nil { + logger.Error("failed to send msgs", err, "msgs", msgs) + } + r.Succeeded = r.Succeeded && (err == nil) srcMsgIDs = append(srcMsgIDs, msgIDs...) // clear the current batch and reset variables @@ -85,8 +91,11 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { // submit leftover msgs if len(msgs) > 0 { - msgIDs, ok := SendCheckMsgs(src, msgs) - r.Succeeded = ok + msgIDs, err := src.SendMsgs(msgs) + if err != nil { + logger.Error("failed to send msgs", err, "msgs", msgs) + } + r.Succeeded = r.Succeeded && (err == nil) srcMsgIDs = append(srcMsgIDs, msgIDs...) } @@ -106,8 +115,11 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { if r.IsMaxTx(msgLen, txSize) { // Submit the transaction to dst chain and update its status - msgIDs, ok := SendCheckMsgs(dst, msgs) - r.Succeeded = r.Succeeded && ok + msgIDs, err := dst.SendMsgs(msgs) + if err != nil { + logger.Error("failed to send msgs", err, "msgs", msgs) + } + r.Succeeded = r.Succeeded && (err == nil) dstMsgIDs = append(dstMsgIDs, msgIDs...) // clear the current batch and reset variables @@ -119,11 +131,15 @@ func (r *RelayMsgs) Send(src, dst Chain) ([]MsgID, []MsgID) { // submit leftover msgs if len(msgs) > 0 { - msgIDs, ok := SendCheckMsgs(dst, msgs) - r.Succeeded = ok + msgIDs, err := dst.SendMsgs(msgs) + if err != nil { + logger.Error("failed to send msgs", err, "msgs", msgs) + } + r.Succeeded = r.Succeeded && (err == nil) dstMsgIDs = append(dstMsgIDs, msgIDs...) } - return srcMsgIDs, dstMsgIDs + r.SentSrcMsgIDs = srcMsgIDs + r.SentDstMsgIDs = dstMsgIDs } // Merge merges the argument into the receiver diff --git a/core/send.go b/core/send.go index d17bb37c..dd9bc94f 100644 --- a/core/send.go +++ b/core/send.go @@ -5,22 +5,8 @@ import ( "time" retry "github.com/avast/retry-go" - "github.com/cosmos/cosmos-sdk/types" ) -// SendCheckMsgs is an utility function that executes `Chain::SendMsgs` and checks the execution results of all the messages. -func SendCheckMsgs(chain Chain, msgs []types.Msg) ([]MsgID, bool) { - logger := GetChainLogger(chain) - now := time.Now() - msgIDs, err := chain.SendMsgs(msgs) - if err != nil { - GetChainLogger(chain).Error("failed to send msgs", err, "msgs", msgs) - return nil, false - } - logger.TimeTrack(now, "SendMsgs", "num_msgs", len(msgs)) - return msgIDs, true -} - // GetFinalizedMsgResult is an utility function that waits for the finalization of the message execution and then returns the result. func GetFinalizedMsgResult(chain ProvableChain, msgID MsgID) (MsgResult, error) { var msgRes MsgResult From 488997f353ef922a1f319828cebc35e8e4000bf4 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Wed, 20 Dec 2023 17:59:44 +0900 Subject: [PATCH 6/9] * check config show * move initconfig, create config to config package Signed-off-by: Dongri Jin --- cmd/config.go | 68 +-------------------- cmd/root.go | 4 +- config/config.go | 54 ++++++++++++++++ core/config.go | 5 +- core/relayMsgs.go | 22 +++++-- tests/cases/tm2tm/scripts/handshake | 32 ++++++++++ tests/cases/tmmock2tmmock/scripts/handshake | 32 ++++++++++ 7 files changed, 143 insertions(+), 74 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 65e2e229..a6baf019 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,10 +1,8 @@ package cmd import ( - "encoding/json" "fmt" "os" - "path/filepath" "github.com/hyperledger-labs/yui-relayer/config" "github.com/spf13/cobra" @@ -33,31 +31,10 @@ func configInitCmd(ctx *config.Context) *cobra.Command { Aliases: []string{"i"}, Short: "Creates a default home directory at path defined by --home", RunE: func(cmd *cobra.Command, args []string) error { - cfgPath := ctx.Config.ConfigPath - // If the config doesn't exist... - if _, err := os.Stat(cfgPath); os.IsNotExist(err) { - dirPath := filepath.Dir(cfgPath) - if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { - return err - } - // Then create the file... - f, err := os.Create(cfgPath) - if err != nil { - return err - } - defer f.Close() - - // And write the default config to that location... - if _, err = f.Write(defaultConfig()); err != nil { - return err - } - - // And return no error... - return nil + if err := ctx.Config.CreateConfig(); err != nil { + return err } - - // Otherwise, the config file exists, and an error is returned... - return fmt.Errorf("config already exists: %s", cfgPath) + return nil }, } return cmd @@ -74,12 +51,10 @@ func configShowCmd(ctx *config.Context) *cobra.Command { if _, err := os.Stat(cfgPath); os.IsNotExist(err) { return fmt.Errorf("config does not exist: %s", cfgPath) } - out, err := config.MarshalJSON(*ctx.Config) if err != nil { return err } - fmt.Println(string(out)) return nil }, @@ -87,40 +62,3 @@ func configShowCmd(ctx *config.Context) *cobra.Command { return cmd } - -func defaultConfig() []byte { - bz, err := json.Marshal(config.DefaultConfig(fmt.Sprintf("%s/%s", homePath, configPath))) - if err != nil { - panic(err) - } - return bz -} - -// initConfig reads in config file and ENV variables if set. -func initConfig(ctx *config.Context, cmd *cobra.Command) error { - cfgPath := ctx.Config.ConfigPath - if _, err := os.Stat(cfgPath); err == nil { - file, err := os.ReadFile(cfgPath) - if err != nil { - fmt.Println("Error reading file:", err) - os.Exit(1) - } - - // unmarshall them into the struct - if err = config.UnmarshalJSON(ctx.Codec, file, ctx.Config); err != nil { - fmt.Println("Error unmarshalling config:", err) - os.Exit(1) - } - - // ensure config has []*relayer.Chain used for all chain operations - if err = config.InitChains(ctx, homePath, debug); err != nil { - fmt.Println("Error parsing chain config:", err) - os.Exit(1) - } - } else { - defConfig := config.DefaultConfig(fmt.Sprintf("%s/%s", homePath, configPath)) - ctx.Config = &defConfig - } - ctx.Config.InitCoreConfig() - return nil -} diff --git a/cmd/root.go b/cmd/root.go index ab3221ec..d92fd823 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -54,7 +54,7 @@ func Execute(modules ...config.ModuleI) error { for _, module := range modules { module.RegisterInterfaces(codec.InterfaceRegistry()) } - ctx := &config.Context{Modules: modules, Config: &config.Config{ConfigPath: fmt.Sprintf("%s/%s", homePath, configPath)}, Codec: codec} + ctx := &config.Context{Modules: modules, Config: &config.Config{}, Codec: codec} // Register subcommands @@ -79,7 +79,7 @@ func Execute(modules ...config.ModuleI) error { if err := viper.BindPFlags(cmd.Flags()); err != nil { return fmt.Errorf("failed to bind the flag set to the configuration: %v", err) } - if err := initConfig(ctx, rootCmd); err != nil { + if err := ctx.Config.InitConfig(ctx, homePath, configPath, debug); err != nil { return fmt.Errorf("failed to initialize the configuration: %v", err) } if err := initLogger(ctx); err != nil { diff --git a/config/config.go b/config/config.go index 076e2070..435d8abd 100644 --- a/config/config.go +++ b/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" "time" "github.com/cosmos/cosmos-sdk/codec" @@ -142,6 +143,51 @@ func InitChains(ctx *Context, homePath string, debug bool) error { return nil } +func (c *Config) InitConfig(ctx *Context, homePath, configPath string, debug bool) error { + cfgPath := fmt.Sprintf("%s/%s", homePath, configPath) + c.ConfigPath = cfgPath + if _, err := os.Stat(cfgPath); err == nil { + file, err := os.ReadFile(cfgPath) + if err != nil { + return err + } + // unmarshall them into the struct + if err = UnmarshalJSON(ctx.Codec, file, c); err != nil { + return err + } + // ensure config has []*relayer.Chain used for all chain operations + if err = InitChains(ctx, homePath, debug); err != nil { + return err + } + } else { + defConfig := DefaultConfig(cfgPath) + c = &defConfig + } + c.InitCoreConfig() + ctx.Config = c + return nil +} + +func (c *Config) CreateConfig() error { + cfgPath := c.ConfigPath + if _, err := os.Stat(cfgPath); os.IsNotExist(err) { + dirPath := filepath.Dir(cfgPath) + if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { + return err + } + f, err := os.Create(cfgPath) + if err != nil { + return err + } + defer f.Close() + if _, err = f.Write(defaultConfig(c.ConfigPath)); err != nil { + return err + } + return nil + } + return nil +} + func (c *Config) OverWriteConfig() error { configData, err := json.Marshal(c) if err != nil { @@ -152,3 +198,11 @@ func (c *Config) OverWriteConfig() error { } return nil } + +func defaultConfig(configPath string) []byte { + bz, err := json.Marshal(DefaultConfig(configPath)) + if err != nil { + panic(err) + } + return bz +} diff --git a/core/config.go b/core/config.go index 70701631..1969fcc7 100644 --- a/core/config.go +++ b/core/config.go @@ -145,6 +145,9 @@ func SyncChainConfigsFromEvents(pathName string, msgIDsSrc, msgIDsDst []MsgID, s func SyncChainConfigFromEvents(pathName string, msgIDs []MsgID, chain *ProvableChain, configID ConfigIDType) error { for _, msgID := range msgIDs { + if msgID == nil { + continue + } msgRes, err := chain.Chain.GetMsgResult(msgID) if err != nil { return fmt.Errorf("failed to get message result: %v", err) @@ -174,7 +177,7 @@ func SyncChainConfigFromEvents(pathName string, msgIDs []MsgID, chain *ProvableC } } } - } + return nil } diff --git a/core/relayMsgs.go b/core/relayMsgs.go index 54043c2c..01fe514f 100644 --- a/core/relayMsgs.go +++ b/core/relayMsgs.go @@ -63,6 +63,7 @@ func (r *RelayMsgs) Send(src, dst Chain) { srcMsgIDs := make([]MsgID, len(r.Src)) dstMsgIDs := make([]MsgID, len(r.Dst)) // submit batches of relay transactions + srcMaxTxCount := 0 for _, msg := range r.Src { bz, err := proto.Marshal(msg) if err != nil { @@ -80,9 +81,11 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - srcMsgIDs = append(srcMsgIDs, msgIDs...) - + for i := range msgs { + srcMsgIDs[i+srcMaxTxCount] = msgIDs[i] + } // clear the current batch and reset variables + srcMaxTxCount += len(msgs) msgLen, txSize = 1, uint64(len(bz)) msgs = []sdk.Msg{} } @@ -96,13 +99,16 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - srcMsgIDs = append(srcMsgIDs, msgIDs...) + for i := range msgs { + srcMsgIDs[i+srcMaxTxCount] = msgIDs[i] + } } // reset variables msgLen, txSize = 0, 0 msgs = []sdk.Msg{} + dstMaxTxCount := 0 for _, msg := range r.Dst { bz, err := proto.Marshal(msg) if err != nil { @@ -120,9 +126,11 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - dstMsgIDs = append(dstMsgIDs, msgIDs...) - + for i := range msgs { + dstMsgIDs[i+dstMaxTxCount] = msgIDs[i] + } // clear the current batch and reset variables + dstMaxTxCount += len(msgs) msgLen, txSize = 1, uint64(len(bz)) msgs = []sdk.Msg{} } @@ -136,7 +144,9 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - dstMsgIDs = append(dstMsgIDs, msgIDs...) + for i := range msgs { + dstMsgIDs[i+dstMaxTxCount] = msgIDs[i] + } } r.SentSrcMsgIDs = srcMsgIDs r.SentDstMsgIDs = dstMsgIDs diff --git a/tests/cases/tm2tm/scripts/handshake b/tests/cases/tm2tm/scripts/handshake index 6e5dd1bb..894a76eb 100755 --- a/tests/cases/tm2tm/scripts/handshake +++ b/tests/cases/tm2tm/scripts/handshake @@ -30,3 +30,35 @@ $RLY paths add $CHAINID_ONE $CHAINID_TWO $PATH_NAME --file=./configs/path.json retry 5 $RLY tx clients --src-height 2 $PATH_NAME retry 5 $RLY tx connection $PATH_NAME retry 5 $RLY tx channel $PATH_NAME + +# check created config +config=$(${RLY} config show) + +srcClientId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].src."client-id"') +srcConnectionId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].src."connection-id"') +srcChannelId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].src."channel-id"') + +dstClientId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."client-id"') +dstConnectionId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."connection-id"') +dstChannelId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."channel-id"') + +if [[ "$srcClientId" == "$dstClientId" && "$srcClientId" == "07-tendermint-0" ]]; then + echo "Client IDs match and are '07-tendermint-0'" +else + echo "Client IDs do not match or are not '07-tendermint-0'" + exit 1 +fi + +if [[ "$srcConnectionId" == "$dstConnectionId" && "$srcConnectionId" == "connection-0" ]]; then + echo "Connection IDs match and are 'connection-0'" +else + echo "Connection IDs do not match or are not 'connection-0'" + exit 1 +fi + +if [[ "$srcChannelId" == "$dstChannelId" && "$srcChannelId" == "channel-0" ]]; then + echo "Channel IDs match and are 'channel-0'" +else + echo "Channel IDs do not match or are not 'channel-0'" + exit 1 +fi diff --git a/tests/cases/tmmock2tmmock/scripts/handshake b/tests/cases/tmmock2tmmock/scripts/handshake index 1b77689f..4a9f932e 100755 --- a/tests/cases/tmmock2tmmock/scripts/handshake +++ b/tests/cases/tmmock2tmmock/scripts/handshake @@ -22,3 +22,35 @@ $RLY paths add $CHAINID_ONE $CHAINID_TWO $PATH_NAME --file=./configs/path.json retry 5 $RLY tx clients --dst-height 2 $PATH_NAME retry 5 $RLY tx connection $PATH_NAME retry 5 $RLY tx channel $PATH_NAME + +# check created config +config=$(${RLY} config show) + +srcClientId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].src."client-id"') +srcConnectionId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].src."connection-id"') +srcChannelId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].src."channel-id"') + +dstClientId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."client-id"') +dstConnectionId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."connection-id"') +dstChannelId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."channel-id"') + +if [[ "$srcClientId" == "$dstClientId" && "$srcClientId" == "mock-client-0" ]]; then + echo "Client IDs match and are 'mock-client-0'" +else + echo "Client IDs do not match or are not 'mock-client-0'" + exit 1 +fi + +if [[ "$srcConnectionId" == "$dstConnectionId" && "$srcConnectionId" == "connection-0" ]]; then + echo "Connection IDs match and are 'connection-0'" +else + echo "Connection IDs do not match or are not 'connection-0'" + exit 1 +fi + +if [[ "$srcChannelId" == "$dstChannelId" && "$srcChannelId" == "channel-0" ]]; then + echo "Channel IDs match and are 'channel-0'" +else + echo "Channel IDs do not match or are not 'channel-0'" + exit 1 +fi From eadd642dbda3ae47998160e42457a40ed8a67c48 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Wed, 20 Dec 2023 20:10:29 +0900 Subject: [PATCH 7/9] Remove unused code Signed-off-by: Dongri Jin --- core/channel.go | 3 --- core/connection.go | 3 --- 2 files changed, 6 deletions(-) diff --git a/core/channel.go b/core/channel.go index b55b3275..0c49bd56 100644 --- a/core/channel.go +++ b/core/channel.go @@ -198,9 +198,6 @@ func logChannelStates(src, dst *ProvableChain, srcChan, dstChan *chantypes.Query func checkChannelFinality(src, dst *ProvableChain, srcChannel, dstChannel *chantypes.Channel) (bool, error) { logger := GetChannelPairLogger(src, dst) - if src.Chain.Path().ChannelID == "" && dst.Chain.Path().ChannelID == "" { - return true, nil - } sh, err := src.LatestHeight() if err != nil { return false, err diff --git a/core/connection.go b/core/connection.go index 116820f6..e2b0aee3 100644 --- a/core/connection.go +++ b/core/connection.go @@ -266,9 +266,6 @@ func mustGetAddress(chain interface { func checkConnectionFinality(src, dst *ProvableChain, srcConnection, dstConnection *conntypes.ConnectionEnd) (bool, error) { logger := GetConnectionPairLogger(src, dst) - if src.Path().ConnectionID == "" && dst.Path().ConnectionID == "" { - return true, nil - } sh, err := src.LatestHeight() if err != nil { return false, err From 286bfe5359804373830a090058994c3dbbb0447b Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Wed, 24 Jan 2024 09:30:10 +0900 Subject: [PATCH 8/9] * Fix defaultConfig * Remove configID from sync function * Fix parameters Signed-off-by: Dongri Jin --- config/config.go | 10 +++---- core/channel.go | 2 +- core/client.go | 2 +- core/config.go | 32 ++++++++++---------- core/connection.go | 2 +- core/relayMsgs.go | 25 ++++++++-------- tests/cases/tm2tm/scripts/handshake | 33 +++++++++++++-------- tests/cases/tmmock2tmmock/scripts/handshake | 33 +++++++++++++-------- 8 files changed, 78 insertions(+), 61 deletions(-) diff --git a/config/config.go b/config/config.go index 435d8abd..30aa67b6 100644 --- a/config/config.go +++ b/config/config.go @@ -22,7 +22,7 @@ type Config struct { ConfigPath string `yaml:"-" json:"-"` } -func DefaultConfig(configPath string) Config { +func defaultConfig(configPath string) Config { return Config{ Global: newDefaultGlobalConfig(), Chains: []core.ChainProverConfig{}, @@ -160,7 +160,7 @@ func (c *Config) InitConfig(ctx *Context, homePath, configPath string, debug boo return err } } else { - defConfig := DefaultConfig(cfgPath) + defConfig := defaultConfig(cfgPath) c = &defConfig } c.InitCoreConfig() @@ -180,7 +180,7 @@ func (c *Config) CreateConfig() error { return err } defer f.Close() - if _, err = f.Write(defaultConfig(c.ConfigPath)); err != nil { + if _, err = f.Write(defaultConfigBytes(c.ConfigPath)); err != nil { return err } return nil @@ -199,8 +199,8 @@ func (c *Config) OverWriteConfig() error { return nil } -func defaultConfig(configPath string) []byte { - bz, err := json.Marshal(DefaultConfig(configPath)) +func defaultConfigBytes(configPath string) []byte { + bz, err := json.Marshal(defaultConfig(configPath)) if err != nil { panic(err) } diff --git a/core/channel.go b/core/channel.go index 0c49bd56..e4b12ea4 100644 --- a/core/channel.go +++ b/core/channel.go @@ -36,7 +36,7 @@ func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) e chanSteps.Send(src, dst) if chanSteps.Success() { - if err := SyncChainConfigsFromEvents(pathName, chanSteps.SentSrcMsgIDs, chanSteps.SentDstMsgIDs, src, dst, ConfigIDChannel); err != nil { + if err := SyncChainConfigsFromEvents(pathName, chanSteps.SrcMsgIDs, chanSteps.DstMsgIDs, src, dst); err != nil { return err } } diff --git a/core/client.go b/core/client.go index 8c9ba988..5acf4c5d 100644 --- a/core/client.go +++ b/core/client.go @@ -69,7 +69,7 @@ func CreateClients(pathName string, src, dst *ProvableChain, srcHeight, dstHeigh logger.Info( "★ Clients created", ) - if err := SyncChainConfigsFromEvents(pathName, clients.SentSrcMsgIDs, clients.SentDstMsgIDs, src, dst, ConfigIDClient); err != nil { + if err := SyncChainConfigsFromEvents(pathName, clients.SrcMsgIDs, clients.DstMsgIDs, src, dst); err != nil { return err } } diff --git a/core/config.go b/core/config.go index 1969fcc7..ce1b8c9c 100644 --- a/core/config.go +++ b/core/config.go @@ -133,17 +133,17 @@ func (cc ChainProverConfig) Build() (*ProvableChain, error) { return NewProvableChain(chain, prover), nil } -func SyncChainConfigsFromEvents(pathName string, msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain, configID ConfigIDType) error { - if err := SyncChainConfigFromEvents(pathName, msgIDsSrc, src, configID); err != nil { +func SyncChainConfigsFromEvents(pathName string, msgIDsSrc, msgIDsDst []MsgID, src, dst *ProvableChain) error { + if err := SyncChainConfigFromEvents(pathName, msgIDsSrc, src); err != nil { return err } - if err := SyncChainConfigFromEvents(pathName, msgIDsDst, dst, configID); err != nil { + if err := SyncChainConfigFromEvents(pathName, msgIDsDst, dst); err != nil { return err } return nil } -func SyncChainConfigFromEvents(pathName string, msgIDs []MsgID, chain *ProvableChain, configID ConfigIDType) error { +func SyncChainConfigFromEvents(pathName string, msgIDs []MsgID, chain *ProvableChain) error { for _, msgID := range msgIDs { if msgID == nil { continue @@ -157,19 +157,17 @@ func SyncChainConfigFromEvents(pathName string, msgIDs []MsgID, chain *ProvableC for _, event := range msgRes.Events() { var id string - switch configID { - case ConfigIDClient: - if clientIdentifier, ok := event.(*EventGenerateClientIdentifier); ok { - id = clientIdentifier.ID - } - case ConfigIDConnection: - if connectionIdentifier, ok := event.(*EventGenerateConnectionIdentifier); ok { - id = connectionIdentifier.ID - } - case ConfigIDChannel: - if channelIdentifier, ok := event.(*EventGenerateChannelIdentifier); ok { - id = channelIdentifier.ID - } + var configID ConfigIDType + switch event := event.(type) { + case *EventGenerateClientIdentifier: + configID = ConfigIDClient + id = event.ID + case *EventGenerateConnectionIdentifier: + configID = ConfigIDConnection + id = event.ID + case *EventGenerateChannelIdentifier: + configID = ConfigIDChannel + id = event.ID } if id != "" { if err := config.UpdateConfigID(pathName, chain.ChainID(), configID, id); err != nil { diff --git a/core/connection.go b/core/connection.go index e2b0aee3..29f89490 100644 --- a/core/connection.go +++ b/core/connection.go @@ -45,7 +45,7 @@ func CreateConnection(pathName string, src, dst *ProvableChain, to time.Duration connSteps.Send(src, dst) if connSteps.Success() { - if err := SyncChainConfigsFromEvents(pathName, connSteps.SentSrcMsgIDs, connSteps.SentDstMsgIDs, src, dst, ConfigIDConnection); err != nil { + if err := SyncChainConfigsFromEvents(pathName, connSteps.SrcMsgIDs, connSteps.DstMsgIDs, src, dst); err != nil { return err } } diff --git a/core/relayMsgs.go b/core/relayMsgs.go index 01fe514f..8557d0ea 100644 --- a/core/relayMsgs.go +++ b/core/relayMsgs.go @@ -17,8 +17,8 @@ type RelayMsgs struct { Last bool `json:"last"` Succeeded bool `json:"success"` - SentSrcMsgIDs []MsgID `json:"sent_src_msg_ids"` - SentDstMsgIDs []MsgID `json:"sent_dst_msg_ids"` + SrcMsgIDs []MsgID `json:"src_msg_ids"` + DstMsgIDs []MsgID `json:"dst_msg_ids"` } // NewRelayMsgs returns an initialized version of relay messages @@ -63,7 +63,8 @@ func (r *RelayMsgs) Send(src, dst Chain) { srcMsgIDs := make([]MsgID, len(r.Src)) dstMsgIDs := make([]MsgID, len(r.Dst)) // submit batches of relay transactions - srcMaxTxCount := 0 + maxTxCount := 0 + for _, msg := range r.Src { bz, err := proto.Marshal(msg) if err != nil { @@ -82,10 +83,10 @@ func (r *RelayMsgs) Send(src, dst Chain) { } r.Succeeded = r.Succeeded && (err == nil) for i := range msgs { - srcMsgIDs[i+srcMaxTxCount] = msgIDs[i] + srcMsgIDs[i+maxTxCount] = msgIDs[i] } // clear the current batch and reset variables - srcMaxTxCount += len(msgs) + maxTxCount += len(msgs) msgLen, txSize = 1, uint64(len(bz)) msgs = []sdk.Msg{} } @@ -100,15 +101,15 @@ func (r *RelayMsgs) Send(src, dst Chain) { } r.Succeeded = r.Succeeded && (err == nil) for i := range msgs { - srcMsgIDs[i+srcMaxTxCount] = msgIDs[i] + srcMsgIDs[i+maxTxCount] = msgIDs[i] } } // reset variables msgLen, txSize = 0, 0 msgs = []sdk.Msg{} + maxTxCount = 0 - dstMaxTxCount := 0 for _, msg := range r.Dst { bz, err := proto.Marshal(msg) if err != nil { @@ -127,10 +128,10 @@ func (r *RelayMsgs) Send(src, dst Chain) { } r.Succeeded = r.Succeeded && (err == nil) for i := range msgs { - dstMsgIDs[i+dstMaxTxCount] = msgIDs[i] + dstMsgIDs[i+maxTxCount] = msgIDs[i] } // clear the current batch and reset variables - dstMaxTxCount += len(msgs) + maxTxCount += len(msgs) msgLen, txSize = 1, uint64(len(bz)) msgs = []sdk.Msg{} } @@ -145,11 +146,11 @@ func (r *RelayMsgs) Send(src, dst Chain) { } r.Succeeded = r.Succeeded && (err == nil) for i := range msgs { - dstMsgIDs[i+dstMaxTxCount] = msgIDs[i] + dstMsgIDs[i+maxTxCount] = msgIDs[i] } } - r.SentSrcMsgIDs = srcMsgIDs - r.SentDstMsgIDs = dstMsgIDs + r.SrcMsgIDs = srcMsgIDs + r.DstMsgIDs = dstMsgIDs } // Merge merges the argument into the receiver diff --git a/tests/cases/tm2tm/scripts/handshake b/tests/cases/tm2tm/scripts/handshake index 894a76eb..57dac8d4 100755 --- a/tests/cases/tm2tm/scripts/handshake +++ b/tests/cases/tm2tm/scripts/handshake @@ -42,23 +42,32 @@ dstClientId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.path dstConnectionId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."connection-id"') dstChannelId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."channel-id"') -if [[ "$srcClientId" == "$dstClientId" && "$srcClientId" == "07-tendermint-0" ]]; then - echo "Client IDs match and are '07-tendermint-0'" -else - echo "Client IDs do not match or are not '07-tendermint-0'" +if [[ "$srcClientId" != "07-tendermint-0" ]]; then + echo "Source client ID is not '07-tendermint-0'" exit 1 +elif [[ "$dstClientId" != "07-tendermint-0" ]]; then + echo "Destination client ID is not '07-tendermint-0'" + exit 1 +else + echo "Client IDs match and are '07-tendermint-0'" fi -if [[ "$srcConnectionId" == "$dstConnectionId" && "$srcConnectionId" == "connection-0" ]]; then - echo "Connection IDs match and are 'connection-0'" -else - echo "Connection IDs do not match or are not 'connection-0'" +if [[ "$srcConnectionId" != "connection-0" ]]; then + echo "Source connection ID is not 'connection-0'" + exit 1 +elif [[ "$dstConnectionId" != "connection-0" ]]; then + echo "Destination connection ID is not 'connection-0'" exit 1 +else + echo "Connection IDs match and are 'connection-0'" fi -if [[ "$srcChannelId" == "$dstChannelId" && "$srcChannelId" == "channel-0" ]]; then - echo "Channel IDs match and are 'channel-0'" -else - echo "Channel IDs do not match or are not 'channel-0'" +if [[ "$srcChannelId" != "channel-0" ]]; then + echo "Source channel ID is not 'channel-0'" exit 1 +elif [[ "$dstChannelId" != "channel-0" ]]; then + echo "Destination channel ID is not 'channel-0'" + exit 1 +else + echo "Channel IDs match and are 'channel-0'" fi diff --git a/tests/cases/tmmock2tmmock/scripts/handshake b/tests/cases/tmmock2tmmock/scripts/handshake index 4a9f932e..d72830da 100755 --- a/tests/cases/tmmock2tmmock/scripts/handshake +++ b/tests/cases/tmmock2tmmock/scripts/handshake @@ -34,23 +34,32 @@ dstClientId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.path dstConnectionId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."connection-id"') dstChannelId=$(echo $config | jq --raw-output --arg path_name "$PATH_NAME" '.paths[$path_name].dst."channel-id"') -if [[ "$srcClientId" == "$dstClientId" && "$srcClientId" == "mock-client-0" ]]; then - echo "Client IDs match and are 'mock-client-0'" -else - echo "Client IDs do not match or are not 'mock-client-0'" +if [[ "$srcClientId" != "mock-client-0" ]]; then + echo "Source client ID is not 'mock-client-0'" exit 1 +elif [[ "$dstClientId" != "mock-client-0" ]]; then + echo "Destination client ID is not 'mock-client-0'" + exit 1 +else + echo "Client IDs match and are 'mock-client-0'" fi -if [[ "$srcConnectionId" == "$dstConnectionId" && "$srcConnectionId" == "connection-0" ]]; then - echo "Connection IDs match and are 'connection-0'" -else - echo "Connection IDs do not match or are not 'connection-0'" +if [[ "$srcConnectionId" != "connection-0" ]]; then + echo "Source connection ID is not 'connection-0'" + exit 1 +elif [[ "$dstConnectionId" != "connection-0" ]]; then + echo "Destination connection ID is not 'connection-0'" exit 1 +else + echo "Connection IDs match and are 'connection-0'" fi -if [[ "$srcChannelId" == "$dstChannelId" && "$srcChannelId" == "channel-0" ]]; then - echo "Channel IDs match and are 'channel-0'" -else - echo "Channel IDs do not match or are not 'channel-0'" +if [[ "$srcChannelId" != "channel-0" ]]; then + echo "Source channel ID is not 'channel-0'" exit 1 +elif [[ "$dstChannelId" != "channel-0" ]]; then + echo "Destination channel ID is not 'channel-0'" + exit 1 +else + echo "Channel IDs match and are 'channel-0'" fi From be89032c1a6919881fe205aabf46fa613b85a108 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Tue, 30 Jan 2024 10:28:46 +0900 Subject: [PATCH 9/9] Add check `err` Signed-off-by: Dongri Jin --- core/relayMsgs.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/core/relayMsgs.go b/core/relayMsgs.go index 8557d0ea..cff5edee 100644 --- a/core/relayMsgs.go +++ b/core/relayMsgs.go @@ -82,8 +82,10 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - for i := range msgs { - srcMsgIDs[i+maxTxCount] = msgIDs[i] + if err == nil { + for i := range msgs { + srcMsgIDs[i+maxTxCount] = msgIDs[i] + } } // clear the current batch and reset variables maxTxCount += len(msgs) @@ -100,8 +102,10 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - for i := range msgs { - srcMsgIDs[i+maxTxCount] = msgIDs[i] + if err == nil { + for i := range msgs { + srcMsgIDs[i+maxTxCount] = msgIDs[i] + } } } @@ -127,8 +131,10 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - for i := range msgs { - dstMsgIDs[i+maxTxCount] = msgIDs[i] + if err == nil { + for i := range msgs { + dstMsgIDs[i+maxTxCount] = msgIDs[i] + } } // clear the current batch and reset variables maxTxCount += len(msgs) @@ -145,8 +151,10 @@ func (r *RelayMsgs) Send(src, dst Chain) { logger.Error("failed to send msgs", err, "msgs", msgs) } r.Succeeded = r.Succeeded && (err == nil) - for i := range msgs { - dstMsgIDs[i+maxTxCount] = msgIDs[i] + if err == nil { + for i := range msgs { + dstMsgIDs[i+maxTxCount] = msgIDs[i] + } } } r.SrcMsgIDs = srcMsgIDs