Skip to content

Commit

Permalink
Handle list of ibc data with same connections and clients the same wa…
Browse files Browse the repository at this point in the history
…y, allow channels to be a list (hyperweb-io#142)
  • Loading branch information
Anmol1696 authored Jul 5, 2023
1 parent 912482f commit 8892590
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
8 changes: 4 additions & 4 deletions registry/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type ChainClient struct {
client *lens.ChainClient

mu sync.Mutex
chainIBCInfo []*ChainIBCInfo
chainIBCInfo ChainIBCInfos
}

func NewChainClient(logger *zap.Logger, chainID, rpcAddr, exposerAddr, home string) (*ChainClient, error) {
Expand Down Expand Up @@ -251,13 +251,13 @@ func (c *ChainClient) getChainIdFromClient(clientId string) (string, error) {
}

// GetChainInfo will fetch all the IBC channels for the chain
func (c *ChainClient) GetChainInfo() ([]*ChainIBCInfo, error) {
func (c *ChainClient) GetChainInfo() (ChainIBCInfos, error) {
channelsInfo, err := c.getChannelsPorts()
if err != nil {
return nil, err
}

var chainIBCInfos []*ChainIBCInfo
var chainIBCInfos ChainIBCInfos
for _, channelInfo := range channelsInfo {
connectionInfo, err := c.getConnectionClient(channelInfo.ConnectionId)
if err != nil {
Expand Down Expand Up @@ -296,7 +296,7 @@ func (c *ChainClient) GetChainInfo() ([]*ChainIBCInfo, error) {
}

// GetCachedChainInfo will return cached chain info, if no cache, then will cache the info
func (c *ChainClient) GetCachedChainInfo() ([]*ChainIBCInfo, error) {
func (c *ChainClient) GetCachedChainInfo() (ChainIBCInfos, error) {
c.mu.Lock()
defer c.mu.Unlock()

Expand Down
14 changes: 5 additions & 9 deletions registry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,7 @@ func (a *AppServer) ListChainIBC(ctx context.Context, requestChain *pb.RequestCh
return nil, err
}

var resData []*pb.IBCData
for _, info := range infos {
resData = append(resData, info.ToProto())
}

return &pb.ResponseListIBC{Data: resData}, nil
return &pb.ResponseListIBC{Data: infos.ToProto()}, nil
}

func (a *AppServer) GetIBCInfo(ctx context.Context, requestIBCInfo *pb.RequestIBCInfo) (*pb.IBCData, error) {
Expand All @@ -296,9 +291,10 @@ func (a *AppServer) GetIBCInfo(ctx context.Context, requestIBCInfo *pb.RequestIB
return nil, err
}

for _, info := range infos {
if info.Counterparty.ChainId == requestIBCInfo.Chain_2 {
return info.ToProto(), nil
infoProtos := infos.ToProto()
for _, info := range infoProtos {
if info.Chain_2.ChainName == requestIBCInfo.Chain_2 {
return info, nil
}
}

Expand Down
60 changes: 58 additions & 2 deletions registry/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
pb "github.com/cosmology-tech/starship/registry/registry"
"sort"
"strings"
)

type ChannelPort struct {
Expand Down Expand Up @@ -38,6 +40,62 @@ type IBCInfo struct {
ClientId string `json:"client_id"`
}

type ChainIBCInfos []*ChainIBCInfo

func (infos ChainIBCInfos) ToProto() []*pb.IBCData {
chainsIBCData := map[string]*pb.IBCData{}
for _, ibcInfo := range infos {
chain1 := &pb.IBCChain{
ChainName: ibcInfo.ChainId,
ClientId: ibcInfo.ClientId,
ConnectionId: ibcInfo.ConnectionId,
}
chain2 := &pb.IBCChain{
ChainName: ibcInfo.Counterparty.ChainId,
ClientId: ibcInfo.Counterparty.ClientId,
ConnectionId: ibcInfo.Counterparty.ConnectionId,
}
keys := []string{chain1.String(), chain2.String()}
sort.Strings(keys)
keyMap := strings.Join(keys, "--")
_, ok := chainsIBCData[keyMap]

if !ok {
chainsIBCData[keyMap] = ibcInfo.ToProto()
continue
}

// append channel to IBCData
channel := &pb.ChannelData{
Chain_1: &pb.ChannelData_ChannelPort{
ChannelId: ibcInfo.ChannelId,
PortId: ibcInfo.PortId,
},
Chain_2: &pb.ChannelData_ChannelPort{
ChannelId: ibcInfo.Counterparty.ChannelId,
PortId: ibcInfo.Counterparty.PortId,
},
Ordering: ibcInfo.Ordering,
Version: ibcInfo.Version,
Tags: &pb.ChannelData_Tags{
// todo: fetch status from client status instead of hardcoding
Status: "live",
Perferred: true,
},
}

chainsIBCData[keyMap].Channels = append(chainsIBCData[keyMap].Channels, channel)
}

values := []*pb.IBCData{}

for _, value := range chainsIBCData {
values = append(values, value)
}

return values
}

type ChainIBCInfo struct {
IBCInfo

Expand Down Expand Up @@ -83,8 +141,6 @@ func (info *ChainIBCInfo) ToProto() *pb.IBCData {
}
}

type ChainIBCInfos []*ChainIBCInfo

// GetCounterpartyChainInfo returns ChainIBCInfo struct for given counterparty
// chain id.
func (c ChainIBCInfos) GetCounterpartyChainInfo(chainId string) *ChainIBCInfo {
Expand Down

0 comments on commit 8892590

Please sign in to comment.