From 1a2519c214644301ac998bd1dece2f28b69d267b Mon Sep 17 00:00:00 2001 From: --global Date: Mon, 5 Apr 2021 12:19:50 +0530 Subject: [PATCH 1/2] refactor rly pth generate command --- cmd/paths.go | 147 +++++------------------------------------------- relayer/path.go | 53 ----------------- 2 files changed, 15 insertions(+), 185 deletions(-) diff --git a/cmd/paths.go b/cmd/paths.go index 2b55490dd..f5577623e 100644 --- a/cmd/paths.go +++ b/cmd/paths.go @@ -49,7 +49,7 @@ func pathsGenCmd() *cobra.Command { Short: "generate identifiers for a new path between src and dst, reusing any that exist", Args: cobra.ExactArgs(3), Example: strings.TrimSpace(fmt.Sprintf(` -$ %s paths generate ibc-0 ibc-1 demo-path --force +$ %s paths generate ibc-0 ibc-1 demo-path $ %s pth gen ibc-0 ibc-1 demo-path --unordered false --version ics20-2`, appName, appName)), RunE: func(cmd *cobra.Command, args []string) (err error) { var ( @@ -60,7 +60,6 @@ $ %s pth gen ibc-0 ibc-1 demo-path --unordered false --version ics20-2`, appName srcConns, dstConns *conntypes.QueryConnectionsResponse srcCon, dstCon *conntypes.IdentifiedConnection srcChans, dstChans *chantypes.QueryChannelsResponse - srcChan, dstChan *chantypes.IdentifiedChannel ) if c, err = config.Chains.Gets(src, dst); err != nil { return fmt.Errorf("chains need to be configured before paths to them can be added: %w", err) @@ -83,23 +82,6 @@ $ %s pth gen ibc-0 ibc-1 demo-path --unordered false --version ics20-2`, appName path.Dst.Order = ORDERED } - // if -f is passed, generate a random path between the two chains - if force, _ := cmd.Flags().GetBool(flagForce); force { - path.GenSrcClientID() - path.GenDstClientID() - path.GenSrcConnID() - path.GenDstConnID() - path.GenSrcChanID() - path.GenDstChanID() - // validate it... - if err = config.Paths.AddForce(pth, path); err != nil { - return err - } - logPathGen(pth) - // ...then add it to the config file - return overWriteConfig(config) - } - // see if there are existing clients that can be reused eg.Go(func() error { srcClients, err = c[src].QueryClients(0, 1000) @@ -149,51 +131,8 @@ $ %s pth gen ibc-0 ibc-1 demo-path --unordered false --version ics20-2`, appName } } - switch { - // If there aren't any matching clients between chains, generate - case path.Src.ClientID == "" && path.Dst.ClientID == "": - path.GenSrcClientID() - path.GenDstClientID() - path.GenSrcConnID() - path.GenSrcConnID() - path.GenSrcChanID() - path.GenDstChanID() - if err = config.ValidatePath(path); err != nil { - return err - } - if err = config.Paths.Add(pth, path); err != nil { - return err - } - logPathGen(pth) - return overWriteConfig(config) - case path.Src.ClientID == "" && path.Dst.ClientID != "": - path.GenSrcClientID() - path.GenSrcConnID() - path.GenSrcConnID() - path.GenSrcChanID() - path.GenDstChanID() - if err = config.ValidatePath(path); err != nil { - return err - } - if err = config.Paths.Add(pth, path); err != nil { - return err - } - logPathGen(pth) - return overWriteConfig(config) - case path.Dst.ClientID == "" && path.Src.ClientID != "": - path.GenDstClientID() - path.GenSrcConnID() - path.GenSrcConnID() - path.GenSrcChanID() - path.GenDstChanID() - if err = config.ValidatePath(path); err != nil { - return err - } - if err = config.Paths.Add(pth, path); err != nil { - return err - } - logPathGen(pth) - return overWriteConfig(config) + if path.Src.ClientID == "" || path.Dst.ClientID == "" { + return valPathAndUpdateConfig(pth, path) } // see if there are existing connections that can be reused @@ -233,32 +172,10 @@ $ %s pth gen ibc-0 ibc-1 demo-path --unordered false --version ics20-2`, appName srcOpen := srcCon.State == conntypes.OPEN dstOpen := dstCon.State == conntypes.OPEN if !(dstCpForSrc && srcCpForDst && srcOpen && dstOpen) { - path.GenSrcConnID() - path.GenDstConnID() - path.GenSrcChanID() - path.GenDstChanID() - if err = config.ValidatePath(path); err != nil { - return err - } - if err = config.Paths.Add(pth, path); err != nil { - return err - } - logPathGen(pth) - return overWriteConfig(config) + return valPathAndUpdateConfig(pth, path) } default: - path.GenSrcConnID() - path.GenDstConnID() - path.GenSrcChanID() - path.GenDstChanID() - if err = config.ValidatePath(path); err != nil { - return err - } - if err = config.Paths.Add(pth, path); err != nil { - return err - } - logPathGen(pth) - return overWriteConfig(config) + return valPathAndUpdateConfig(pth, path) } eg.Go(func() error { @@ -275,65 +192,31 @@ $ %s pth gen ibc-0 ibc-1 demo-path --unordered false --version ics20-2`, appName for _, c := range srcChans.Channels { if c.ConnectionHops[0] == path.Src.ConnectionID { - srcChan = c path.Src.ChannelID = c.ChannelId } } for _, c := range dstChans.Channels { if c.ConnectionHops[0] == path.Dst.ConnectionID { - dstChan = c path.Dst.ChannelID = c.ChannelId } } - switch { - case path.Src.ChannelID != "" && path.Dst.ChannelID != "": - // If we have identified a channel, make sure that each end is the - // other's counterparty and that the channel is open. In the failure case - // we should generate a new channel identifier - dstCpForSrc := srcChan.Counterparty.ChannelId == dstChan.ChannelId - srcCpForDst := dstChan.Counterparty.ChannelId == srcChan.ChannelId - srcOpen := srcChan.State == chantypes.OPEN - dstOpen := dstChan.State == chantypes.OPEN - srcPort := srcChan.PortId == path.Src.PortID - dstPort := dstChan.PortId == path.Dst.PortID - srcOrder := srcChan.Ordering == path.Src.GetOrder() - dstOrder := dstChan.Ordering == path.Dst.GetOrder() - srcVersion := srcChan.Version == path.Src.Version - dstVersion := dstChan.Version == path.Dst.Version - if !(dstCpForSrc && srcCpForDst && srcOpen && dstOpen && srcPort && dstPort && - srcOrder && dstOrder && srcVersion && dstVersion) { - path.GenSrcChanID() - path.GenDstChanID() - } - if err = config.ValidatePath(path); err != nil { - return err - } - if err = config.Paths.Add(pth, path); err != nil { - return err - } - logPathGen(pth) - return overWriteConfig(config) - default: - path.GenSrcChanID() - path.GenDstChanID() - if err = config.ValidatePath(path); err != nil { - return err - } - if err = config.Paths.Add(pth, path); err != nil { - return err - } - logPathGen(pth) - return overWriteConfig(config) - } + return valPathAndUpdateConfig(pth, path) }, } - return forceFlag(orderFlag(versionFlag(pathStrategy(portFlag(cmd))))) + return orderFlag(versionFlag(pathStrategy(portFlag(cmd)))) } -func logPathGen(pth string) { +func valPathAndUpdateConfig(pth string, path *relayer.Path) (err error) { + if err = config.ValidatePath(path); err != nil { + return err + } + if err = config.Paths.Add(pth, path); err != nil { + return err + } fmt.Printf("Generated path(%s), run 'rly paths show %s --yaml' to see details\n", pth, pth) + return overWriteConfig(config) } func pathsDeleteCmd() *cobra.Command { diff --git a/relayer/path.go b/relayer/path.go index 1c51e1d02..513e6f065 100644 --- a/relayer/path.go +++ b/relayer/path.go @@ -56,18 +56,6 @@ func (p Paths) Add(name string, path *Path) error { return nil } -// AddForce ignores existing paths and overwrites an existing path with that name -func (p Paths) AddForce(name string, path *Path) error { - if err := path.Validate(); err != nil { - return err - } - if _, found := p[name]; found { - fmt.Printf("overwriting path %s with new path...\n", name) - } - p[name] = path - return nil -} - // MustYAML returns the yaml string representation of the Path func (p *Path) MustYAML() string { out, err := yaml.Marshal(p) @@ -106,52 +94,11 @@ type Path struct { Strategy *StrategyCfg `yaml:"strategy" json:"strategy"` } -// TODO: remove gen functions - -// GenSrcClientID generates the specififed identifier -func (p *Path) GenSrcClientID() { p.Src.ClientID = "" } - -// GenDstClientID generates the specififed identifier -func (p *Path) GenDstClientID() { p.Dst.ClientID = "" } - -// GenSrcConnID generates the specififed identifier -func (p *Path) GenSrcConnID() { p.Src.ConnectionID = "" } - -// GenDstConnID generates the specififed identifier -func (p *Path) GenDstConnID() { p.Dst.ConnectionID = "" } - -// GenSrcChanID generates the specififed identifier -func (p *Path) GenSrcChanID() { p.Src.ChannelID = "" } - -// GenDstChanID generates the specififed identifier -func (p *Path) GenDstChanID() { p.Dst.ChannelID = "" } - // Ordered returns true if the path is ordered and false if otherwise func (p *Path) Ordered() bool { return p.Src.GetOrder() == chantypes.ORDERED } -// Validate checks that a path is valid -func (p *Path) Validate() (err error) { - if err = p.Src.ValidateFull(); err != nil { - return err - } - if p.Src.Version == "" { - return fmt.Errorf("source must specify a version") - } - if err = p.Dst.ValidateFull(); err != nil { - return err - } - if _, err = p.GetStrategy(); err != nil { - return err - } - if p.Src.Order != p.Dst.Order { - return fmt.Errorf("both sides must have same order ('ORDERED' or 'UNORDERED'), got src(%s) and dst(%s)", - p.Src.Order, p.Dst.Order) - } - return nil -} - // End returns the proper end given a chainID func (p *Path) End(chainID string) *PathEnd { if p.Dst.ChainID == chainID { From 9f2e240eabd872aab38d5ebb92b37e83f50bdcb9 Mon Sep 17 00:00:00 2001 From: --global Date: Thu, 8 Apr 2021 16:10:53 +0530 Subject: [PATCH 2/2] Update generate cmd description --- cmd/paths.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/paths.go b/cmd/paths.go index f5577623e..c7fff6285 100644 --- a/cmd/paths.go +++ b/cmd/paths.go @@ -46,7 +46,7 @@ func pathsGenCmd() *cobra.Command { cmd := &cobra.Command{ Use: "generate [src-chain-id] [dst-chain-id] [name]", Aliases: []string{"gen"}, - Short: "generate identifiers for a new path between src and dst, reusing any that exist", + Short: "generate a new path between src and dst, reusing any identifiers that exist", Args: cobra.ExactArgs(3), Example: strings.TrimSpace(fmt.Sprintf(` $ %s paths generate ibc-0 ibc-1 demo-path