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

connection and channel identifiers reuse #386

Merged
merged 8 commits into from
Feb 1, 2021
47 changes: 42 additions & 5 deletions relayer/channel-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ func ExecuteChannelStep(src, dst *Chain) (success, last, modified bool, err erro
// if either identifier is missing, an existing channel that matches the required fields
// is chosen or a new channel is created.
if src.PathEnd.ChannelID == "" || dst.PathEnd.ChannelID == "" {
// TODO: Query for existing identifier and fill config, if possible
success, modified, err := InitializeChannel(src, dst, srcUpdateHeader, dstUpdateHeader, sh)
if err != nil {
return false, false, false, err
channelID, found := FindMatchingChannel(src, dst)
if !found {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
success, modified, err := InitializeChannel(src, dst, srcUpdateHeader, dstUpdateHeader, sh)
if err != nil {
return false, false, false, err
}
return success, false, modified, nil
}

return success, false, modified, nil
src.PathEnd.ChannelID = channelID
return true, false, true, nil
}

// Query Channel data from src and dst
Expand Down Expand Up @@ -453,3 +457,36 @@ func (c *Chain) CloseChannelStep(dst *Chain) (*RelayMsgs, error) {
}
return out, nil
}

// FindMatchingChannel will determine if there already exists a unintialized channel between source and counterparty.
func FindMatchingChannel(source, counterparty *Chain) (string, bool) {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
// TODO: add appropriate offset and limits, along with retries
channelsResp, err := source.QueryChannels(0, 1000)
if err != nil {
if source.debug {
source.Log(fmt.Sprintf("Error: querying channels on %s failed: %v", source.PathEnd.ChainID, err))
}
return "", false
}

for _, channel := range channelsResp.Channels {
if channel.State == chantypes.UNINITIALIZED && channel.Ordering == source.PathEnd.GetOrder() &&
IsConnectionFound(channel.ConnectionHops, source.PathEnd.ConnectionID) &&
channel.PortId == source.PathEnd.PortID && channel.Counterparty.PortId == counterparty.PathEnd.PortID {
// unused channel found
return channel.ChannelId, true
}
}

return "", false
}

// IsConnectionFound determines if given connectionId is present in channel connectionHops list
func IsConnectionFound(connectionHops []string, connectionID string) bool {
for _, id := range connectionHops {
if id == connectionID {
return true
}
}
return false
}
37 changes: 32 additions & 5 deletions relayer/connection-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ func ExecuteConnectionStep(src, dst *Chain) (success, last, modified bool, err e
// is chosen or a new connection is created.
// This will perform either an OpenInit or OpenTry step and return
if src.PathEnd.ConnectionID == "" || dst.PathEnd.ConnectionID == "" {
// TODO: Query for existing identifier and fill config, if possible
success, modified, err := InitializeConnection(src, dst, srcUpdateHeader, dstUpdateHeader, sh)
if err != nil {
return false, false, false, err
connectionID, found := FindMatchingConnection(src, dst)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
if !found {
success, modified, err := InitializeConnection(src, dst, srcUpdateHeader, dstUpdateHeader, sh)
if err != nil {
return false, false, false, err
}
return success, false, modified, nil
}

return success, false, modified, nil
src.PathEnd.ConnectionID = connectionID
return true, false, true, nil
}

// Query Connection data from src and dst
Expand Down Expand Up @@ -324,3 +328,26 @@ func InitializeConnection(src, dst *Chain, srcUpdateHeader, dstUpdateHeader *tmc
return false, true, fmt.Errorf("connection ends already created")
}
}

// FindMatchingConnection will determine if there already exists a unintialized connection between source and counterparty.
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
func FindMatchingConnection(source, counterparty *Chain) (string, bool) {
// TODO: add appropriate offset and limits, along with retries
connectionsResp, err := source.QueryConnections(0, 1000)
if err != nil {
if source.debug {
source.Log(fmt.Sprintf("Error: querying connections on %s failed: %v", source.PathEnd.ChainID, err))
}
return "", false
}

for _, connection := range connectionsResp.Connections {
if connection.ClientId == source.PathEnd.ClientID &&
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
connection.Counterparty.ClientId == counterparty.PathEnd.ClientID &&
connection.State == conntypes.UNINITIALIZED {
// unused connection found
return connection.Id, true
}
}

return "", false
}