Skip to content

Commit

Permalink
Apply @colin-axner suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilkumarpilli committed Mar 18, 2021
1 parent fb273d9 commit 5e5a15c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 66 deletions.
71 changes: 71 additions & 0 deletions relayer/misbehaviour.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package relayer

import (
"encoding/hex"
"fmt"

clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
tmclient "github.com/cosmos/cosmos-sdk/x/ibc/light-clients/07-tendermint/types"
)

var (
// strings for parsing events
updateCliTag = "update_client"
headerTag = "header"
clientIDTag = "client_id"
)

// checkAndSubmitMisbehaviour check headers from update_client tx events
// and submit misbehaviour if found any
func checkAndSubmitMisbehaviour(src *Chain, events map[string][]string) error {
if hdrs, ok := events[fmt.Sprintf("%s.%s", updateCliTag, headerTag)]; ok {
for i, hdr := range hdrs {
clientIDs := events[fmt.Sprintf("%s.%s", updateCliTag, clientIDTag)]
emittedClientID := clientIDs[i]

if src.PathEnd.ClientID == emittedClientID {
hdrBytes, err := hex.DecodeString(hdr)
if err != nil {
return err
}

exportedHeader, err := clienttypes.UnmarshalHeader(src.Encoding.Marshaler, hdrBytes)
if err != nil {
return err
}

emittedHeader, ok := exportedHeader.(*tmclient.Header)
if !ok {
return fmt.Errorf("emitted header is not tendermint type")
}

trustedHeader, err := src.GetLightSignedHeaderAtHeight(emittedHeader.Header.Height)
if err != nil {
return err
}

trustedHeader.TrustedValidators = emittedHeader.TrustedValidators
trustedHeader.TrustedHeight = emittedHeader.TrustedHeight

if !IsMatchingConsensusState(emittedHeader.ConsensusState(), trustedHeader.ConsensusState()) {
misbehaviour := tmclient.NewMisbehaviour(emittedClientID, emittedHeader, trustedHeader)
msg, err := clienttypes.NewMsgSubmitMisbehaviour(emittedClientID, misbehaviour, src.MustGetAddress())
if err != nil {
return err
}
res, success, err := src.SendMsg(msg)
if err != nil {
return err
}
if !success {
return fmt.Errorf("submit misbehaviour tx failed: %s", res.RawLog)
}
src.Log(fmt.Sprintf("Submitted misbehaviour for emitted header with height: %d",
emittedHeader.Header.Height))
}
}
}
}

return nil
}
77 changes: 11 additions & 66 deletions relayer/naive-strategy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package relayer

import (
"encoding/hex"
"fmt"
"strconv"
"strings"
Expand All @@ -10,7 +9,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client/types"
chantypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types"
tmclient "github.com/cosmos/cosmos-sdk/x/ibc/light-clients/07-tendermint/types"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"golang.org/x/sync/errgroup"
)
Expand All @@ -20,20 +18,17 @@ var (
_ Strategy = &NaiveStrategy{}

// Strings for parsing events
spTag = "send_packet"
waTag = "write_acknowledgement"
srcChanTag = "packet_src_channel"
dstChanTag = "packet_dst_channel"
srcPortTag = "packet_src_port"
dstPortTag = "packet_dst_port"
dataTag = "packet_data"
ackTag = "packet_ack"
toHeightTag = "packet_timeout_height"
toTSTag = "packet_timeout_timestamp"
seqTag = "packet_sequence"
updateCliTag = "update_client"
headerTag = "header"
clientIDTag = "client_id"
spTag = "send_packet"
waTag = "write_acknowledgement"
srcChanTag = "packet_src_channel"
dstChanTag = "packet_dst_channel"
srcPortTag = "packet_src_port"
dstPortTag = "packet_dst_port"
dataTag = "packet_data"
ackTag = "packet_ack"
toHeightTag = "packet_timeout_height"
toTSTag = "packet_timeout_timestamp"
seqTag = "packet_sequence"
)

// NewNaiveStrategy returns the proper config for the NaiveStrategy
Expand Down Expand Up @@ -249,56 +244,6 @@ func (nrs *NaiveStrategy) HandleEvents(src, dst *Chain, events map[string][]stri
}
}

func checkAndSubmitMisbehaviour(src *Chain, events map[string][]string) error {
if hdrs, ok := events[fmt.Sprintf("%s.%s", updateCliTag, headerTag)]; ok {
for i, hdr := range hdrs {
clientIDs := events[fmt.Sprintf("%s.%s", updateCliTag, clientIDTag)]
emittedClientID := clientIDs[i]

if src.PathEnd.ClientID == emittedClientID {
hdrBytes, err := hex.DecodeString(hdr)
if err != nil {
return err
}

exportedHeader, err := clienttypes.UnmarshalHeader(src.Encoding.Marshaler, hdrBytes)
if err != nil {
return err
}

emittedHeader, ok := exportedHeader.(*tmclient.Header)
if !ok {
return fmt.Errorf("emitted header is not tendermint type")
}

trustedHeader, err := src.GetLightSignedHeaderAtHeight(emittedHeader.Header.Height)
if err != nil {
return err
}

if !IsMatchingConsensusState(emittedHeader.ConsensusState(), trustedHeader.ConsensusState()) {
misbehaviour := tmclient.NewMisbehaviour(emittedClientID, emittedHeader, trustedHeader)
msg, err := clienttypes.NewMsgSubmitMisbehaviour(emittedClientID, misbehaviour, src.MustGetAddress())
if err != nil {
return err
}
res, success, err := src.SendMsg(msg)
if err != nil {
return err
}
if !success {
return fmt.Errorf("submit misbehaviour tx failed: %s", res.RawLog)
}
src.Log(fmt.Sprintf("Submitted misbehaviour for emitted header with height: %d",
emittedHeader.Header.Height))
}
}
}
}

return nil
}

func relayPacketsFromEventListener(src, dst *PathEnd, events map[string][]string) (rlyPkts []relayPacket, err error) {
// check for send packets
if pdval, ok := events[fmt.Sprintf("%s.%s", spTag, dataTag)]; ok {
Expand Down

0 comments on commit 5e5a15c

Please sign in to comment.