Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* use cleaner helper function

* fix error strings

* lint

* typos

Co-authored-by: colin axnér <[email protected]>
Co-authored-by: Federico Kunze <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Aug 19, 2020
1 parent 81ec566 commit 8f51c15
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
29 changes: 6 additions & 23 deletions x/ibc/07-tendermint/types/misbehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
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"
)

// CheckMisbehaviourAndUpdateState determines whether or not two conflicting
Expand Down Expand Up @@ -40,31 +39,15 @@ func (cs ClientState) CheckMisbehaviourAndUpdateState(
// and unmarshal from clientStore

// Get consensus bytes from clientStore
consBytes1 := clientStore.Get(host.KeyConsensusState(tmEvidence.Header1.TrustedHeight))
if consBytes1 == nil {
return nil, sdkerrors.Wrapf(clienttypes.ErrConsensusStateNotFound,
"could not find trusted consensus state at height %d", tmEvidence.Header1.TrustedHeight)
}
// Unmarshal consensus bytes into clientexported.ConensusState
consensusState1 := clienttypes.MustUnmarshalConsensusState(cdc, consBytes1)
// Cast to tendermint-specific type
tmConsensusState1, ok := consensusState1.(*ConsensusState)
if !ok {
return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "invalid consensus state type for first header: expected type %T, got %T", &ConsensusState{}, consensusState1)
tmConsensusState1, err := GetConsensusState(clientStore, cdc, tmEvidence.Header1.TrustedHeight)
if err != nil {
return nil, sdkerrors.Wrapf(err, "could not get trusted consensus state from clientStore for Header1 at TrustedHeight: %d", tmEvidence.Header1.TrustedHeight)
}

// Get consensus bytes from clientStore
consBytes2 := clientStore.Get(host.KeyConsensusState(tmEvidence.Header2.TrustedHeight))
if consBytes2 == nil {
return nil, sdkerrors.Wrapf(clienttypes.ErrConsensusStateNotFound,
"could not find trusted consensus state at height %d", tmEvidence.Header2.TrustedHeight)
}
// Unmarshal consensus bytes into clientexported.ConensusState
consensusState2 := clienttypes.MustUnmarshalConsensusState(cdc, consBytes2)
// Cast to tendermint-specific type
tmConsensusState2, ok := consensusState2.(*ConsensusState)
if !ok {
return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "invalid consensus state for second header: expected type %T, got %T", &ConsensusState{}, consensusState2)
tmConsensusState2, err := GetConsensusState(clientStore, cdc, tmEvidence.Header2.TrustedHeight)
if err != nil {
return nil, sdkerrors.Wrapf(err, "could not get trusted consensus state from clientStore for Header2 at TrustedHeight: %d", tmEvidence.Header2.TrustedHeight)
}

// calculate the age of the misbehaviour evidence
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/07-tendermint/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func GetConsensusState(store sdk.KVStore, cdc codec.BinaryMarshaler, height uint

var consensusStateI clientexported.ConsensusState
if err := codec.UnmarshalAny(cdc, &consensusStateI, bz); err != nil {
return nil, err
return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "unmarshal error: %v", err)
}

consensusState, ok := consensusStateI.(*ConsensusState)
Expand Down
16 changes: 3 additions & 13 deletions x/ibc/07-tendermint/types/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
)

// CheckHeaderAndUpdateState checks if the provided header is valid, and if valid it will:
Expand Down Expand Up @@ -47,19 +46,10 @@ func (cs ClientState) CheckHeaderAndUpdateState(
}

// Get consensus bytes from clientStore
consBytes := clientStore.Get(host.KeyConsensusState(tmHeader.TrustedHeight))
if consBytes == nil {
return nil, nil, sdkerrors.Wrapf(
clienttypes.ErrConsensusStateNotFound, "consensus state not found for trusted height %d", tmHeader.TrustedHeight,
)
}
// Unmarshal consensus bytes into clientexported.ConensusState
consState := clienttypes.MustUnmarshalConsensusState(cdc, consBytes)
// Cast to tendermint-specific type
tmConsState, ok := consState.(*ConsensusState)
if !ok {
tmConsState, err := GetConsensusState(clientStore, cdc, tmHeader.TrustedHeight)
if err != nil {
return nil, nil, sdkerrors.Wrapf(
clienttypes.ErrInvalidConsensus, "expected type %T, got %T", ConsensusState{}, consState,
err, "could not get consensus state from clientstore at TrustedHeight: %d", tmHeader.TrustedHeight,
)
}

Expand Down

0 comments on commit 8f51c15

Please sign in to comment.