From 6857cd27dfc7e4740afd8df72035df49ead77d25 Mon Sep 17 00:00:00 2001 From: --global Date: Thu, 18 Mar 2021 17:17:36 +0530 Subject: [PATCH] Apply @colin suggestions --- relayer/misbehaviour.go | 71 ++++++++++++++++++++++++++++++++++++ relayer/naive-strategy.go | 77 ++++++--------------------------------- 2 files changed, 82 insertions(+), 66 deletions(-) create mode 100644 relayer/misbehaviour.go diff --git a/relayer/misbehaviour.go b/relayer/misbehaviour.go new file mode 100644 index 0000000000..7e075dde7a --- /dev/null +++ b/relayer/misbehaviour.go @@ -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 +} diff --git a/relayer/naive-strategy.go b/relayer/naive-strategy.go index 0137ddc76f..53f7d02924 100644 --- a/relayer/naive-strategy.go +++ b/relayer/naive-strategy.go @@ -1,7 +1,6 @@ package relayer import ( - "encoding/hex" "fmt" "strconv" "strings" @@ -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" ) @@ -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 @@ -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 {