Skip to content

Commit

Permalink
Address more findings.
Browse files Browse the repository at this point in the history
  • Loading branch information
bznein committed Apr 16, 2024
1 parent e405110 commit 49d002c
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
govcli "github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

types "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
)

Expand Down
6 changes: 4 additions & 2 deletions modules/light-clients/08-wasm/keeper/contract_keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (suite *KeeperTestSuite) TestWasmInstantiate() {
err := json.Unmarshal(initMsg, &payload)
suite.Require().NoError(err)

wrappedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
wrappedClientState, ok := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
suite.Require().True(ok)

clientState := types.NewClientState(payload.ClientState, payload.Checksum, wrappedClientState.LatestHeight)
clientStateBz := clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), clientState)
Expand Down Expand Up @@ -147,7 +148,8 @@ func (suite *KeeperTestSuite) TestWasmInstantiate() {
suite.Require().NoError(err)

// Change the checksum to something else.
wrappedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
wrappedClientState, ok := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
suite.Require().True(ok)
clientState := types.NewClientState(payload.ClientState, []byte("new checksum"), wrappedClientState.LatestHeight)
store.Set(host.ClientStateKey(), clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), clientState))

Expand Down
3 changes: 2 additions & 1 deletion modules/light-clients/08-wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func (suite *KeeperTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[
err := json.Unmarshal(initMsg, &payload)
suite.Require().NoError(err)

wrappedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
wrappedClientState, ok := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
suite.Require().True(ok)

clientState := types.NewClientState(payload.ClientState, payload.Checksum, wrappedClientState.LatestHeight)
clientStateBz := clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), clientState)
Expand Down
4 changes: 3 additions & 1 deletion modules/light-clients/08-wasm/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ func (suite *KeeperTestSuite) TestMsgMigrateContract() {
suite.mockVM.MigrateFn = func(_ wasmvm.Checksum, _ wasmvmtypes.Env, _ []byte, store wasmvm.KVStore, _ wasmvm.GoAPI, _ wasmvm.Querier, _ wasmvm.GasMeter, _ uint64, _ wasmvmtypes.UFraction) (*wasmvmtypes.ContractResult, uint64, error) {
// the checksum written in the client state will later be overwritten by the message server.
expClientStateBz := wasmtesting.CreateMockClientStateBz(suite.chainA.App.AppCodec(), []byte("invalid checksum"))
expClientState = clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), expClientStateBz).(*types.ClientState)
var ok bool
expClientState, ok = clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), expClientStateBz).(*types.ClientState)
suite.Require().True(ok)
store.Set(host.ClientStateKey(), expClientStateBz)

data, err := json.Marshal(types.EmptyResult{})
Expand Down
6 changes: 4 additions & 2 deletions modules/light-clients/08-wasm/light_client_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ func (suite *WasmTestSuite) TestInitialize() {

suite.Require().Equal(env.Contract.Address, wasmClientID)

wrappedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
wrappedClientState, ok := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
suite.Require().True(ok)

clientState := types.NewClientState(payload.ClientState, payload.Checksum, wrappedClientState.LatestHeight)
clientStateBz := clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), clientState)
Expand Down Expand Up @@ -824,7 +825,8 @@ func (suite *WasmTestSuite) TestVerifyUpgradeAndUpdateState() {
suite.Require().NoError(err)

// set new client state and consensus state
wrappedUpgradedClient := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), expectedUpgradedClient.Data).(*ibctm.ClientState)
wrappedUpgradedClient, ok := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), expectedUpgradedClient.Data).(*ibctm.ClientState)
suite.Require().True(ok)
store.Set(host.ClientStateKey(), clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), upgradedClient))
store.Set(host.ConsensusStateKey(wrappedUpgradedClient.LatestHeight), clienttypes.MustMarshalConsensusState(suite.chainA.App.AppCodec(), upgradedConsState))

Expand Down
8 changes: 6 additions & 2 deletions modules/light-clients/08-wasm/testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import (
ibcfee "github.com/cosmos/ibc-go/v8/modules/apps/29-fee"
ibcfeekeeper "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/keeper"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
transfer "github.com/cosmos/ibc-go/v8/modules/apps/transfer"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
Expand Down Expand Up @@ -565,8 +565,12 @@ func NewSimApp(

// initialize ICA module with mock module as the authentication module on the controller side
var icaControllerStack porttypes.IBCModule
var ok bool
icaControllerStack = ibcmock.NewIBCModule(&mockModule, ibcmock.NewIBCApp("", scopedICAMockKeeper))
app.ICAAuthModule = icaControllerStack.(ibcmock.IBCModule)
app.ICAAuthModule, ok = icaControllerStack.(ibcmock.IBCModule)
if !ok {
panic(fmt.Errorf("cannot convert %T into %T", icaControllerStack, app.ICAAuthModule))
}
icaControllerStack = icacontroller.NewIBCMiddleware(icaControllerStack, app.ICAControllerKeeper)
icaControllerStack = ibcfee.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper)

Expand Down
10 changes: 5 additions & 5 deletions modules/light-clients/08-wasm/testing/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v8/modules/core/23-commitment/types"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
testing "github.com/cosmos/ibc-go/v8/testing"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
)

var (
Expand All @@ -37,12 +37,12 @@ func CreateMockTendermintClientState(height clienttypes.Height) *ibctm.ClientSta
return ibctm.NewClientState(
"chain-id",
ibctm.DefaultTrustLevel,
testing.TrustingPeriod,
testing.UnbondingPeriod,
testing.MaxClockDrift,
ibctesting.TrustingPeriod,
ibctesting.UnbondingPeriod,
ibctesting.MaxClockDrift,
height,
commitmenttypes.GetSDKSpecs(),
testing.UpgradePath,
ibctesting.UpgradePath,
)
}

Expand Down
8 changes: 7 additions & 1 deletion modules/light-clients/08-wasm/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"bytes"
"errors"
"fmt"
"io"

wasmvm "github.com/CosmWasm/wasmvm/v2"
Expand Down Expand Up @@ -35,7 +36,12 @@ func GetClientState(store storetypes.KVStore, cdc codec.BinaryCodec) (*ClientSta
}

clientStateI := clienttypes.MustUnmarshalClientState(cdc, bz)
return clientStateI.(*ClientState), true
var clientState *ClientState
clientState, ok := clientStateI.(*ClientState)
if !ok {
panic(fmt.Errorf("cannot convert %T into %T", clientStateI, clientState))

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}
return clientState, ok
}

// Checksum is a type alias used for wasm byte code checksums.
Expand Down
5 changes: 3 additions & 2 deletions modules/light-clients/08-wasm/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

wasmtesting "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing"
simapp "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing/simapp"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing/simapp"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
Expand Down Expand Up @@ -90,7 +90,8 @@ func (suite *TypesTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[s
err := json.Unmarshal(initMsg, &payload)
suite.Require().NoError(err)

wrappedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
wrappedClientState, ok := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
suite.Require().True(ok)

clientState := types.NewClientState(payload.ClientState, payload.Checksum, wrappedClientState.LatestHeight)
clientStateBz := clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), clientState)
Expand Down
5 changes: 3 additions & 2 deletions modules/light-clients/08-wasm/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

wasmtesting "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing"
simapp "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing/simapp"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/testing/simapp"
"github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
Expand Down Expand Up @@ -85,7 +85,8 @@ func (suite *WasmTestSuite) setupWasmWithMockVM() (ibctesting.TestingApp, map[st
err := json.Unmarshal(initMsg, &payload)
suite.Require().NoError(err)

wrappedClientState := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
wrappedClientState, ok := clienttypes.MustUnmarshalClientState(suite.chainA.App.AppCodec(), payload.ClientState).(*ibctm.ClientState)
suite.Require().True(ok)

clientState := types.NewClientState(payload.ClientState, payload.Checksum, wrappedClientState.LatestHeight)
clientStateBz := clienttypes.MustMarshalClientState(suite.chainA.App.AppCodec(), clientState)
Expand Down

0 comments on commit 49d002c

Please sign in to comment.