Skip to content

Commit

Permalink
Remove Evidence handler from IBC (cosmos#7196)
Browse files Browse the repository at this point in the history
* fix builds

* fix tests

* lint

* Update x/ibc/02-client/handler.go

* Update x/ibc/handler.go

Co-authored-by: Federico Kunze <[email protected]>
Co-authored-by: Christopher Goes <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2020
1 parent 018915b commit 5ee4fad
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 257 deletions.
2 changes: 1 addition & 1 deletion proto/ibc/lightclients/solomachine/v1/solomachine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ message MsgUpdateClient {
// arbitrary Misbehaviour.
message MsgSubmitClientMisbehaviour {
option (gogoproto.goproto_getters) = false;
bytes submitter = 1
bytes signer = 1
[(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
Misbehaviour misbehaviour = 2;
}
Expand Down
2 changes: 1 addition & 1 deletion proto/ibc/tendermint/tendermint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@ message MsgSubmitClientMisbehaviour {
option (gogoproto.goproto_getters) = false;

Misbehaviour misbehaviour = 1;
bytes submitter = 2
bytes signer = 2
[(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
}
7 changes: 1 addition & 6 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ import (
transfer "github.com/cosmos/cosmos-sdk/x/ibc-transfer"
ibctransferkeeper "github.com/cosmos/cosmos-sdk/x/ibc-transfer/keeper"
ibctransfertypes "github.com/cosmos/cosmos-sdk/x/ibc-transfer/types"
ibcclient "github.com/cosmos/cosmos-sdk/x/ibc/02-client"
ibcclienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
porttypes "github.com/cosmos/cosmos-sdk/x/ibc/05-port/types"
ibchost "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
ibckeeper "github.com/cosmos/cosmos-sdk/x/ibc/keeper"
Expand Down Expand Up @@ -280,10 +278,7 @@ func NewSimApp(
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
)
evidenceRouter := evidencetypes.NewRouter().
AddRoute(ibcclienttypes.RouterKey, ibcclient.HandlerClientMisbehaviour(app.IBCKeeper.ClientKeeper))

evidenceKeeper.SetRouter(evidenceRouter)
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

// NOTE: Any module instantiated in the module manager that is later modified
Expand Down
14 changes: 12 additions & 2 deletions x/ibc/02-client/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/cosmos/cosmos-sdk/codec"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
connectionexported "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/exported"
channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
commitmentexported "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported"
Expand Down Expand Up @@ -135,9 +134,13 @@ type ConsensusState interface {

// Misbehaviour defines counterparty misbehaviour for a specific consensus type
type Misbehaviour interface {
evidenceexported.Evidence
ClientType() ClientType
GetClientID() string
String() string
ValidateBasic() error

// Height at which the infraction occurred
GetHeight() uint64
}

// Header is the consensus state update information
Expand Down Expand Up @@ -172,6 +175,13 @@ type MsgUpdateClient interface {
GetHeader() Header
}

// MsgSubmitMisbehaviour defines the msg interface that the
// SubmitMisbehaviour Handler expects
type MsgSubmitMisbehaviour interface {
sdk.Msg
GetMisbehaviour() Misbehaviour
}

// ClientType defines the type of the consensus algorithm
type ClientType byte

Expand Down
47 changes: 24 additions & 23 deletions x/ibc/02-client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/keeper"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
Expand Down Expand Up @@ -78,29 +76,32 @@ func HandleMsgUpdateClient(ctx sdk.Context, k keeper.Keeper, msg exported.MsgUpd
}, nil
}

// HandlerClientMisbehaviour defines the Evidence module handler for submitting a
// HandleMsgSubmitMisbehaviour defines the Evidence module handler for submitting a
// light client misbehaviour.
func HandlerClientMisbehaviour(k keeper.Keeper) evidencetypes.Handler {
return func(ctx sdk.Context, evidence evidenceexported.Evidence) error {
misbehaviour, ok := evidence.(exported.Misbehaviour)
if !ok {
return sdkerrors.Wrapf(types.ErrInvalidMisbehaviour,
"expected evidence to implement client Misbehaviour interface, got %T", evidence,
)
}
func HandleMsgSubmitMisbehaviour(ctx sdk.Context, k keeper.Keeper, msg exported.MsgSubmitMisbehaviour) (*sdk.Result, error) {
misbehaviour := msg.GetMisbehaviour()
if misbehaviour == nil {
return nil, sdkerrors.Wrapf(types.ErrInvalidMisbehaviour, "misbehaviour is nil")
}

if err := k.CheckMisbehaviourAndUpdateState(ctx, misbehaviour); err != nil {
return sdkerrors.Wrap(err, "failed to process misbehaviour for IBC client")
}
if err := misbehaviour.ValidateBasic(); err != nil {
return nil, err
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitMisbehaviour,
sdk.NewAttribute(types.AttributeKeyClientID, misbehaviour.GetClientID()),
sdk.NewAttribute(types.AttributeKeyClientType, misbehaviour.ClientType().String()),
sdk.NewAttribute(types.AttributeKeyConsensusHeight, fmt.Sprintf("%d", uint64(misbehaviour.GetHeight()))),
),
)
return nil
if err := k.CheckMisbehaviourAndUpdateState(ctx, misbehaviour); err != nil {
return nil, sdkerrors.Wrap(err, "failed to process misbehaviour for IBC client")
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSubmitMisbehaviour,
sdk.NewAttribute(types.AttributeKeyClientID, misbehaviour.GetClientID()),
sdk.NewAttribute(types.AttributeKeyClientType, misbehaviour.ClientType().String()),
sdk.NewAttribute(types.AttributeKeyConsensusHeight, fmt.Sprintf("%d", misbehaviour.GetHeight())),
),
)

return &sdk.Result{
Events: ctx.EventManager().Events().ToABCIEvents(),
}, nil
}
3 changes: 1 addition & 2 deletions x/ibc/07-tendermint/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
)

Expand All @@ -19,7 +18,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
&ConsensusState{},
)
registry.RegisterImplementations(
(*evidenceexported.Evidence)(nil),
(*clientexported.Misbehaviour)(nil),
&Misbehaviour{},
)
}
Expand Down
24 changes: 2 additions & 22 deletions x/ibc/07-tendermint/types/misbehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ import (

yaml "gopkg.in/yaml.v2"

"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
)

var (
_ evidenceexported.Evidence = Misbehaviour{}
_ clientexported.Misbehaviour = Misbehaviour{}
)

Expand All @@ -44,16 +40,6 @@ func (misbehaviour Misbehaviour) GetClientID() string {
return misbehaviour.ClientId
}

// Route implements Misbehaviour interface
func (misbehaviour Misbehaviour) Route() string {
return clienttypes.SubModuleName
}

// Type implements Misbehaviour interface
func (misbehaviour Misbehaviour) Type() string {
return clientexported.TypeEvidenceClientMisbehaviour
}

// String implements Misbehaviour interface
func (misbehaviour Misbehaviour) String() string {
// FIXME: implement custom marshaller
Expand All @@ -64,17 +50,11 @@ func (misbehaviour Misbehaviour) String() string {
return string(bz)
}

// Hash implements Misbehaviour interface
func (misbehaviour Misbehaviour) Hash() tmbytes.HexBytes {
bz := SubModuleCdc.MustMarshalBinaryBare(&misbehaviour)
return tmhash.Sum(bz)
}

// GetHeight returns the height at which misbehaviour occurred
//
// NOTE: assumes that misbehaviour headers have the same height
func (misbehaviour Misbehaviour) GetHeight() int64 {
return int64(math.Min(float64(misbehaviour.Header1.GetHeight()), float64(misbehaviour.Header2.GetHeight())))
func (misbehaviour Misbehaviour) GetHeight() uint64 {
return uint64(math.Min(float64(misbehaviour.Header1.GetHeight()), float64(misbehaviour.Header2.GetHeight())))
}

// GetTime returns the timestamp at which misbehaviour occurred. It uses the
Expand Down
6 changes: 3 additions & 3 deletions x/ibc/07-tendermint/types/misbehaviour_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (cs ClientState) CheckMisbehaviourAndUpdateState(
) (clientexported.ClientState, error) {

// If client is already frozen at earlier height than misbehaviour, return with error
if cs.IsFrozen() && cs.FrozenHeight <= uint64(misbehaviour.GetHeight()) {
if cs.IsFrozen() && cs.FrozenHeight <= misbehaviour.GetHeight() {
return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidMisbehaviour,
"client is already frozen at earlier height %d than misbehaviour height %d", cs.FrozenHeight, misbehaviour.GetHeight())
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func (cs ClientState) CheckMisbehaviourAndUpdateState(
infractionHeight := tmEvidence.GetHeight()
infractionTime := tmEvidence.GetTime()
ageDuration := ctx.BlockTime().Sub(infractionTime)
ageBlocks := int64(cs.LatestHeight) - infractionHeight
ageBlocks := int64(cs.LatestHeight - infractionHeight)

// TODO: Retrieve consensusparams from client state and not context
// Issue #6516: https://github.com/cosmos/cosmos-sdk/issues/6516
Expand Down Expand Up @@ -96,7 +96,7 @@ func (cs ClientState) CheckMisbehaviourAndUpdateState(
return nil, sdkerrors.Wrap(err, "verifying Header2 in Misbehaviour failed")
}

cs.FrozenHeight = uint64(tmEvidence.GetHeight())
cs.FrozenHeight = tmEvidence.GetHeight()
return &cs, nil
}

Expand Down
14 changes: 5 additions & 9 deletions x/ibc/07-tendermint/types/misbehaviour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"time"

"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
)

func (suite *TendermintTestSuite) TestEvidence() {
func (suite *TendermintTestSuite) TestMisbehaviour() {
signers := []tmtypes.PrivValidator{suite.privVal}

misbehaviour := &types.Misbehaviour{
Expand All @@ -22,15 +21,12 @@ func (suite *TendermintTestSuite) TestEvidence() {
ClientId: clientID,
}

suite.Require().Equal(misbehaviour.ClientType(), clientexported.Tendermint)
suite.Require().Equal(misbehaviour.GetClientID(), clientID)
suite.Require().Equal(misbehaviour.Route(), "client")
suite.Require().Equal(misbehaviour.Type(), "client_misbehaviour")
suite.Require().Equal(misbehaviour.Hash(), tmbytes.HexBytes(tmhash.Sum(suite.cdc.MustMarshalBinaryBare(misbehaviour))))
suite.Require().Equal(misbehaviour.GetHeight(), int64(height))
suite.Require().Equal(clientexported.Tendermint, misbehaviour.ClientType())
suite.Require().Equal(clientID, misbehaviour.GetClientID())
suite.Require().Equal(uint64(height), misbehaviour.GetHeight())
}

func (suite *TendermintTestSuite) TestEvidenceValidateBasic() {
func (suite *TendermintTestSuite) TestMisbehaviourValidateBasic() {
altPrivVal := tmtypes.NewMockPV()
altPubKey, err := altPrivVal.GetPubKey()
suite.Require().NoError(err)
Expand Down
21 changes: 8 additions & 13 deletions x/ibc/07-tendermint/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
)

var (
_ clientexported.MsgCreateClient = (*MsgCreateClient)(nil)
_ clientexported.MsgUpdateClient = (*MsgUpdateClient)(nil)
_ evidenceexported.MsgSubmitEvidence = (*MsgSubmitClientMisbehaviour)(nil)
_ clientexported.MsgCreateClient = (*MsgCreateClient)(nil)
_ clientexported.MsgUpdateClient = (*MsgUpdateClient)(nil)
_ clientexported.MsgSubmitMisbehaviour = (*MsgSubmitClientMisbehaviour)(nil)
)

// NewMsgCreateClient creates a new MsgCreateClient instance
Expand Down Expand Up @@ -187,7 +186,7 @@ func (msg MsgUpdateClient) GetHeader() clientexported.Header {
// NewMsgSubmitClientMisbehaviour creates a new MsgSubmitClientMisbehaviour
// instance.
func NewMsgSubmitClientMisbehaviour(m *Misbehaviour, s sdk.AccAddress) *MsgSubmitClientMisbehaviour {
return &MsgSubmitClientMisbehaviour{Misbehaviour: m, Submitter: s}
return &MsgSubmitClientMisbehaviour{Misbehaviour: m, Signer: s}
}

// Route returns the MsgSubmitClientMisbehaviour's route.
Expand All @@ -206,8 +205,8 @@ func (msg MsgSubmitClientMisbehaviour) ValidateBasic() error {
if err := msg.Misbehaviour.ValidateBasic(); err != nil {
return err
}
if msg.Submitter.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Submitter.String())
if msg.Signer.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer.String())
}

return nil
Expand All @@ -221,13 +220,9 @@ func (msg MsgSubmitClientMisbehaviour) GetSignBytes() []byte {

// GetSigners returns the single expected signer for a MsgSubmitClientMisbehaviour.
func (msg MsgSubmitClientMisbehaviour) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Submitter}
return []sdk.AccAddress{msg.Signer}
}

func (msg MsgSubmitClientMisbehaviour) GetEvidence() evidenceexported.Evidence {
func (msg MsgSubmitClientMisbehaviour) GetMisbehaviour() clientexported.Misbehaviour {
return msg.Misbehaviour
}

func (msg MsgSubmitClientMisbehaviour) GetSubmitter() sdk.AccAddress {
return msg.Submitter
}
Loading

0 comments on commit 5ee4fad

Please sign in to comment.