Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor rly pth generate command #486

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 15 additions & 132 deletions cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func pathsGenCmd() *cobra.Command {
Short: "generate identifiers for a new path between src and dst, reusing any that exist",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should update this description, no identifiers should be generated

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 (
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
53 changes: 0 additions & 53 deletions relayer/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down