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

chore: Address linter findings on core/02-client #6119

Merged
merged 9 commits into from
Apr 9, 2024
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
37 changes: 25 additions & 12 deletions modules/core/02-client/keeper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,15 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(suite.chainA.GetContext(), clientID, updateHeader.GetHeight(), conflictConsState)
}, true, true},
{"misbehaviour detection: monotonic time violation", func() {
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
clientID := path.EndpointA.ClientID
trustedHeight := clientState.LatestHeight

// store intermediate consensus state at a time greater than updateHeader time
// this will break time monotonicity
incrementedClientHeight := clientState.LatestHeight.Increment().(clienttypes.Height)
incrementedClientHeight, ok := clientState.LatestHeight.Increment().(clienttypes.Height)
suite.Require().True(ok)
DimitrisJim marked this conversation as resolved.
Show resolved Hide resolved
intermediateConsState := &ibctm.ConsensusState{
Timestamp: suite.coordinator.CurrentTime.Add(2 * time.Hour),
NextValidatorsHash: suite.chainB.Vals.Hash(),
Expand All @@ -238,7 +240,8 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {
updateHeader = createFutureUpdateFn(trustedHeight)
}, true, true},
{"client state not found", func() {
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
updateHeader = createFutureUpdateFn(clientState.LatestHeight)

path.EndpointA.ClientID = ibctesting.InvalidID
Expand All @@ -247,21 +250,25 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {
clientState := path.EndpointA.GetClientState()
tmClient, ok := clientState.(*ibctm.ClientState)
suite.Require().True(ok)
tmClient.LatestHeight = tmClient.LatestHeight.Increment().(clienttypes.Height)
tmClient.LatestHeight, ok = tmClient.LatestHeight.Increment().(clienttypes.Height)
suite.Require().True(ok)

suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, clientState)
updateHeader = createFutureUpdateFn(tmClient.LatestHeight)
}, false, false},
{"client is not active", func() {
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
clientState.FrozenHeight = clienttypes.NewHeight(1, 1)
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, clientState)
updateHeader = createFutureUpdateFn(clientState.LatestHeight)
}, false, false},
{"invalid header", func() {
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
updateHeader = createFutureUpdateFn(clientState.LatestHeight)
updateHeader.TrustedHeight = updateHeader.TrustedHeight.Increment().(clienttypes.Height)
updateHeader.TrustedHeight, ok = updateHeader.TrustedHeight.Increment().(clienttypes.Height)
suite.Require().True(ok)
}, false, false},
}

Expand All @@ -275,16 +282,19 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {
tc.malleate()

var clientState *ibctm.ClientState
var ok bool
if tc.expPass {
clientState = path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok = path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
}

err := suite.chainA.App.GetIBCKeeper().ClientKeeper.UpdateClient(suite.chainA.GetContext(), path.EndpointA.ClientID, updateHeader)

if tc.expPass {
suite.Require().NoError(err, err)

newClientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
newClientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)

if tc.expFreeze {
suite.Require().True(!newClientState.FrozenHeight.IsZero(), "client did not freeze after conflicting header was submitted to UpdateClient")
Expand Down Expand Up @@ -463,7 +473,8 @@ func (suite *KeeperTestSuite) TestUpgradeClient() {
path = ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()

clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
DimitrisJim marked this conversation as resolved.
Show resolved Hide resolved
revisionNumber := clienttypes.ParseChainID(clientState.ChainId)

newChainID, err := clienttypes.SetRevisionNumber(clientState.ChainId, revisionNumber+1)
Expand Down Expand Up @@ -589,7 +600,8 @@ func (suite *KeeperTestSuite) TestRecoverClient() {
func() {
tmClientState, ok := subjectClientState.(*ibctm.ClientState)
suite.Require().True(ok)
tmClientState.LatestHeight = substituteClientState.(*ibctm.ClientState).LatestHeight.Increment().(clienttypes.Height)
tmClientState.LatestHeight, ok = substituteClientState.(*ibctm.ClientState).LatestHeight.Increment().(clienttypes.Height)
suite.Require().True(ok)
DimitrisJim marked this conversation as resolved.
Show resolved Hide resolved
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState)
},
clienttypes.ErrInvalidHeight,
Expand Down Expand Up @@ -665,7 +677,8 @@ func (suite *KeeperTestSuite) TestRecoverClient() {

// Assert that client status is now Active
clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), subjectPath.EndpointA.ClientID)
tmClientState := subjectPath.EndpointA.GetClientState().(*ibctm.ClientState)
tmClientState, ok := subjectPath.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
suite.Require().Equal(tmClientState.Status(suite.chainA.GetContext(), clientStore, suite.chainA.App.AppCodec()), exported.Active)

} else {
Expand Down
6 changes: 4 additions & 2 deletions modules/core/02-client/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func (suite *KeeperTestSuite) TestMsgCreateClientEvents() {
tmConfig, ok := path.EndpointA.ClientConfig.(*ibctesting.TendermintConfig)
suite.Require().True(ok)

height := path.EndpointA.Counterparty.Chain.LatestCommittedHeader.GetHeight().(clienttypes.Height)
height, ok := path.EndpointA.Counterparty.Chain.LatestCommittedHeader.GetHeight().(clienttypes.Height)
suite.Require().True(ok)
clientState := ibctm.NewClientState(
path.EndpointA.Counterparty.Chain.ChainID, tmConfig.TrustLevel, tmConfig.TrustingPeriod, tmConfig.UnbondingPeriod, tmConfig.MaxClockDrift,
height, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath)
Expand Down Expand Up @@ -56,7 +57,8 @@ func (suite *KeeperTestSuite) TestMsgUpdateClientEvents() {

suite.chainB.Coordinator.CommitBlock(suite.chainB)

clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)
trustedHeight := clientState.LatestHeight
header, err := suite.chainB.IBCClientHeader(suite.chainB.LatestCommittedHeader, trustedHeight)
suite.Require().NoError(err)
Expand Down
20 changes: 13 additions & 7 deletions modules/core/02-client/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ func (suite *KeeperTestSuite) TestQueryConsensusStates() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()

height1 := path.EndpointA.GetClientLatestHeight().(types.Height)
height1, ok := path.EndpointA.GetClientLatestHeight().(types.Height)
suite.Require().True(ok)
expConsensusStates = append(
expConsensusStates,
types.NewConsensusStateWithHeight(
Expand All @@ -354,8 +355,8 @@ func (suite *KeeperTestSuite) TestQueryConsensusStates() {

err := path.EndpointA.UpdateClient()
suite.Require().NoError(err)

height2 := path.EndpointA.GetClientLatestHeight().(types.Height)
height2, ok := path.EndpointA.GetClientLatestHeight().(types.Height)
suite.Require().True(ok)
expConsensusStates = append(
expConsensusStates,
types.NewConsensusStateWithHeight(
Expand Down Expand Up @@ -548,10 +549,12 @@ func (suite *KeeperTestSuite) TestQueryClientStatus() {
func() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)

// increment latest height so no consensus state is stored
clientState.LatestHeight = clientState.LatestHeight.Increment().(types.Height)
clientState.LatestHeight, ok = clientState.LatestHeight.Increment().(types.Height)
suite.Require().True(ok)
path.EndpointA.SetClientState(clientState)

req = &types.QueryClientStatusRequest{
Expand All @@ -565,7 +568,8 @@ func (suite *KeeperTestSuite) TestQueryClientStatus() {
func() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()
clientState := path.EndpointA.GetClientState().(*ibctm.ClientState)
clientState, ok := path.EndpointA.GetClientState().(*ibctm.ClientState)
suite.Require().True(ok)

clientState.FrozenHeight = types.NewHeight(0, 1)
path.EndpointA.SetClientState(clientState)
Expand Down Expand Up @@ -636,7 +640,9 @@ func (suite *KeeperTestSuite) TestQueryUpgradedClientState() {
suite.Require().NoError(err)
suite.Require().NotNil(resp)

expClientState = clientState.(*ibctm.ClientState)
var ok bool
expClientState, ok = clientState.(*ibctm.ClientState)
suite.Require().True(ok)
},
nil,
},
Expand Down
6 changes: 5 additions & 1 deletion modules/core/02-client/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,11 @@ func (k Keeper) GetClientLatestHeight(ctx sdk.Context, clientID string) types.He
return types.ZeroHeight()
}

return clientModule.LatestHeight(ctx, clientID).(types.Height)
clientModuleHeight, ok := clientModule.LatestHeight(ctx, clientID).(types.Height)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clientModuleHeight, ok := clientModule.LatestHeight(ctx, clientID).(types.Height)
latestHeight, ok := clientModule.LatestHeight(ctx, clientID).(types.Height)

if !ok {
panic("can't convert clientModule.LatestHeight to types.Height")
bznein marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider returning an error instead of panicking for type assertion failure.
Panicking can lead to abrupt termination of applications using this library. It's generally more robust to return an error and allow the caller to decide how to handle it.

- panic("can't convert clientModule.LatestHeight to types.Height")
+ return types.ZeroHeight(), errors.New("can't convert clientModule.LatestHeight to types.Height")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
clientModuleHeight, ok := clientModule.LatestHeight(ctx, clientID).(types.Height)
if !ok {
panic("can't convert clientModule.LatestHeight to types.Height")
}
clientModuleHeight, ok := clientModule.LatestHeight(ctx, clientID).(types.Height)
if !ok {
return types.ZeroHeight(), errors.New("can't convert clientModule.LatestHeight to types.Height")
}

return clientModuleHeight
}

// GetClientTimestampAtHeight returns the timestamp in nanoseconds of the consensus state at the given height.
Expand Down
2 changes: 1 addition & 1 deletion modules/core/02-client/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"

v7 "github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
)

Expand Down
5 changes: 3 additions & 2 deletions modules/core/02-client/migrations/v7/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"

ibcclient "github.com/cosmos/ibc-go/v8/modules/core/02-client"
v7 "github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
Expand Down Expand Up @@ -67,7 +67,8 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() {
// set in store for ease of determining expected genesis
clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), sm.ClientID)

cdc := suite.chainA.App.AppCodec().(*codec.ProtoCodec)
cdc, ok := suite.chainA.App.AppCodec().(*codec.ProtoCodec)
suite.Require().True(ok)
v7.RegisterInterfaces(cdc.InterfaceRegistry())

bz, err := cdc.MarshalInterface(legacyClientState)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/02-client/migrations/v7/localhost_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v7_test

import (
v7 "github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
)
Expand Down
5 changes: 3 additions & 2 deletions modules/core/02-client/migrations/v7/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"

v7 "github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v8/modules/core/02-client/migrations/v7"
"github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
Expand Down Expand Up @@ -104,7 +104,8 @@ func (suite *MigrationsV7TestSuite) createSolomachineClients(solomachines []*ibc
AllowUpdateAfterProposal: true,
}

cdc := suite.chainA.App.AppCodec().(*codec.ProtoCodec)
cdc, ok := suite.chainA.App.AppCodec().(*codec.ProtoCodec)
suite.Require().True(ok)
v7.RegisterInterfaces(cdc.InterfaceRegistry())

bz, err := cdc.MarshalInterface(legacyClientState)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/02-client/types/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"
"strings"

proto "github.com/cosmos/gogoproto/proto"
"github.com/cosmos/gogoproto/proto"

errorsmod "cosmossdk.io/errors"

Expand Down
3 changes: 2 additions & 1 deletion modules/core/02-client/types/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func (suite *TypesTestSuite) TestMarshalConsensusStateWithHeight() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupClients()

latestHeight := path.EndpointA.GetClientLatestHeight().(types.Height)
latestHeight, ok := path.EndpointA.GetClientLatestHeight().(types.Height)
suite.Require().True(ok)
consensusState, ok := suite.chainA.GetConsensusState(path.EndpointA.ClientID, latestHeight)
suite.Require().True(ok)

Expand Down
2 changes: 1 addition & 1 deletion modules/core/02-client/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
proto "github.com/cosmos/gogoproto/proto"
"github.com/cosmos/gogoproto/proto"

errorsmod "cosmossdk.io/errors"

Expand Down
3 changes: 2 additions & 1 deletion modules/core/02-client/types/legacy_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (suite *TypesTestSuite) TestMarshalClientUpdateProposalProposal() {
cdc := codec.NewProtoCodec(ir)

// marshal message
content := proposal.(*types.ClientUpdateProposal)
content, ok := proposal.(*types.ClientUpdateProposal)
suite.Require().True(ok)
bz, err := cdc.MarshalJSON(content)
suite.Require().NoError(err)

Expand Down
Loading