Skip to content

Commit

Permalink
Fix unnecessary retention of irrelevant messages (cosmos#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin authored and TxCorpi0x committed Oct 7, 2022
1 parent 26715dd commit 811889c
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 86 deletions.
8 changes: 4 additions & 4 deletions relayer/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func (c *Chain) CreateOpenChannels(

srcPathChain := pathChain{
provider: c.ChainProvider,
pathEnd: processor.NewPathEnd(pathName, c.PathEnd.ChainID, c.PathEnd.ClientID, "", []processor.ChannelKey{}),
pathEnd: processor.NewPathEnd(pathName, c.PathEnd.ChainID, c.PathEnd.ClientID, "", []processor.ChainChannelKey{}),
}
dstPathChain := pathChain{
provider: dst.ChainProvider,
pathEnd: processor.NewPathEnd(pathName, dst.PathEnd.ChainID, dst.PathEnd.ClientID, "", []processor.ChannelKey{}),
pathEnd: processor.NewPathEnd(pathName, dst.PathEnd.ChainID, dst.PathEnd.ClientID, "", []processor.ChainChannelKey{}),
}

// Timeout is per message. Four channel handshake messages, allowing maxRetries for each.
Expand Down Expand Up @@ -120,11 +120,11 @@ func (c *Chain) CloseChannel(
) error {
srcPathChain := pathChain{
provider: c.ChainProvider,
pathEnd: processor.NewPathEnd(pathName, c.PathEnd.ChainID, c.PathEnd.ClientID, "", []processor.ChannelKey{}),
pathEnd: processor.NewPathEnd(pathName, c.PathEnd.ChainID, c.PathEnd.ClientID, "", []processor.ChainChannelKey{}),
}
dstPathChain := pathChain{
provider: dst.ChainProvider,
pathEnd: processor.NewPathEnd(pathName, dst.PathEnd.ChainID, dst.PathEnd.ClientID, "", []processor.ChannelKey{}),
pathEnd: processor.NewPathEnd(pathName, dst.PathEnd.ChainID, dst.PathEnd.ClientID, "", []processor.ChainChannelKey{}),
}

// Timeout is per message. Two close channel handshake messages, allowing maxRetries for each.
Expand Down
4 changes: 2 additions & 2 deletions relayer/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func (c *Chain) CreateOpenConnections(

srcpathChain := pathChain{
provider: c.ChainProvider,
pathEnd: processor.NewPathEnd(pathName, c.PathEnd.ChainID, c.PathEnd.ClientID, "", []processor.ChannelKey{}),
pathEnd: processor.NewPathEnd(pathName, c.PathEnd.ChainID, c.PathEnd.ClientID, "", []processor.ChainChannelKey{}),
}
dstpathChain := pathChain{
provider: dst.ChainProvider,
pathEnd: processor.NewPathEnd(pathName, dst.PathEnd.ChainID, dst.PathEnd.ClientID, "", []processor.ChannelKey{}),
pathEnd: processor.NewPathEnd(pathName, dst.PathEnd.ChainID, dst.PathEnd.ClientID, "", []processor.ChainChannelKey{}),
}

// Timeout is per message. Four connection handshake messages, allowing maxRetries for each.
Expand Down
28 changes: 17 additions & 11 deletions relayer/processor/path_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ type PathEnd struct {

// Can be either "allowlist" or "denylist"
Rule string
FilterList []ChannelKey // which channels to allow or deny
FilterList []ChainChannelKey // which channels to allow or deny
}

type ChainChannelKey struct {
ChainID string
CounterpartyChainID string
ChannelKey ChannelKey
}

// NewPathEnd constructs a PathEnd, validating initial parameters.
func NewPathEnd(pathName string, chainID string, clientID string, rule string, filterList []ChannelKey) PathEnd {
func NewPathEnd(pathName string, chainID string, clientID string, rule string, filterList []ChainChannelKey) PathEnd {
return PathEnd{
PathName: pathName,
ChainID: chainID,
Expand All @@ -29,34 +35,34 @@ const (
RuleDenyList = "denylist"
)

func (pe PathEnd) checkChannelMatch(listChannelID, listPortID string, channelKey ChannelKey) bool {
func (pe PathEnd) checkChannelMatch(listChainID, listChannelID, listPortID string, channelKey ChainChannelKey) bool {
if listChannelID == "" {
return false
}
if listChannelID == channelKey.ChannelID {
if listChannelID == channelKey.ChannelKey.ChannelID && listChainID == channelKey.ChainID {
if listPortID == "" {
return true
}
if listPortID == channelKey.PortID {
if listPortID == channelKey.ChannelKey.PortID {
return true
}
}
if listChannelID == channelKey.CounterpartyChannelID {
if listChannelID == channelKey.ChannelKey.CounterpartyChannelID && listChainID == channelKey.CounterpartyChainID {
if listPortID == "" {
return true
}
if listPortID == channelKey.CounterpartyPortID {
if listPortID == channelKey.ChannelKey.CounterpartyPortID {
return true
}
}
return false
}

func (pe PathEnd) shouldRelayChannelSingle(channelKey ChannelKey, listChannel ChannelKey, allowList bool) bool {
if pe.checkChannelMatch(listChannel.ChannelID, listChannel.PortID, channelKey) {
func (pe PathEnd) shouldRelayChannelSingle(channelKey ChainChannelKey, listChannel ChainChannelKey, allowList bool) bool {
if pe.checkChannelMatch(listChannel.ChainID, listChannel.ChannelKey.ChannelID, listChannel.ChannelKey.PortID, channelKey) {
return allowList
}
if pe.checkChannelMatch(listChannel.CounterpartyChannelID, listChannel.CounterpartyPortID, channelKey) {
if pe.checkChannelMatch(listChannel.CounterpartyChainID, listChannel.ChannelKey.CounterpartyChannelID, listChannel.ChannelKey.CounterpartyPortID, channelKey) {
return allowList
}
return !allowList
Expand All @@ -66,7 +72,7 @@ func (pe PathEnd) shouldRelayChannelSingle(channelKey ChannelKey, listChannel Ch
// if port ID is non-empty on allowlist channel, allow only that specific port
// if port ID is empty on blocklist channel, block all ports
// if port ID is non-empty on blocklist channel, block only that specific port
func (pe PathEnd) ShouldRelayChannel(channelKey ChannelKey) bool {
func (pe PathEnd) ShouldRelayChannel(channelKey ChainChannelKey) bool {
if pe.Rule == RuleAllowList {
for _, allowedChannel := range pe.FilterList {
if pe.shouldRelayChannelSingle(channelKey, allowedChannel, true) {
Expand Down
2 changes: 1 addition & 1 deletion relayer/processor/path_end_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (pathEnd *pathEndRuntime) mergeMessageCache(messageCache IBCMessagesCache,
channelHandshakeMessages := make(ChannelMessagesCache)

for ch, pmc := range messageCache.PacketFlow {
if pathEnd.info.ShouldRelayChannel(ch) {
if pathEnd.info.ShouldRelayChannel(ChainChannelKey{ChainID: pathEnd.info.ChainID, ChannelKey: ch}) {
if inSync && pathEnd.metrics != nil {
for eventType, pCache := range pmc {
pathEnd.metrics.AddPacketsObserved(pathEnd.info.PathName, pathEnd.info.ChainID, ch.ChannelID, ch.PortID, eventType, len(pCache))
Expand Down
Loading

0 comments on commit 811889c

Please sign in to comment.