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

Misbehaviour Monitoring #457

Merged
merged 5 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
COMMIT := $(shell git log -1 --format='%H')
SDKCOMMIT := $(shell go list -m -u -f '{{.Version}}' github.com/cosmos/cosmos-sdk)
GAIA_VERSION := v4.0.0
GAIA_VERSION := v4.1.0
AKASH_VERSION := jack/update-sdk
WASMD_VERSION := v0.14.1

Expand Down
73 changes: 73 additions & 0 deletions relayer/misbehaviour.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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
// against the associated light client. If the headers do not match, the emitted
// header and a reconstructed header are used in misbehaviour submission to
// the IBC client on the source chain.
func checkAndSubmitMisbehaviour(src *Chain, events map[string][]string) error {
if hdrs, ok := events[fmt.Sprintf("%s.%s", updateCliTag, headerTag)]; ok {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
for i, hdr := range hdrs {
clientIDs := events[fmt.Sprintf("%s.%s", updateCliTag, clientIDTag)]
emittedClientID := clientIDs[i]
colin-axner marked this conversation as resolved.
Show resolved Hide resolved

if src.PathEnd.ClientID == emittedClientID {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
hdrBytes, err := hex.DecodeString(hdr)
if err != nil {
return err
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
}

exportedHeader, err := clienttypes.UnmarshalHeader(src.Encoding.Marshaler, hdrBytes)
if err != nil {
return err
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
}

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()) {
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
trustedHeader.TrustedValidators = emittedHeader.TrustedValidators
trustedHeader.TrustedHeight = emittedHeader.TrustedHeight

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)
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
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
}
6 changes: 6 additions & 0 deletions relayer/naive-strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ func (nrs *NaiveStrategy) UnrelayedAcknowledgements(src, dst *Chain) (*RelaySequ

// HandleEvents defines how the relayer will handle block and transaction events as they are emitted
func (nrs *NaiveStrategy) HandleEvents(src, dst *Chain, events map[string][]string) {
// check for misbehaviour and submit if found
err := checkAndSubmitMisbehaviour(src, events)
if err != nil {
src.Error(err)
}

colin-axner marked this conversation as resolved.
Show resolved Hide resolved
rlyPackets, err := relayPacketsFromEventListener(src.PathEnd, dst.PathEnd, events)
if len(rlyPackets) > 0 && err == nil {
nrs.sendTxFromEventPackets(src, dst, rlyPackets)
Expand Down