Skip to content

Commit

Permalink
Allow localhost to be created (#7148)
Browse files Browse the repository at this point in the history
* revert localhost not being created

* Update x/ibc/02-client/keeper/grpc_query_test.go

Co-authored-by: Federico Kunze <[email protected]>
  • Loading branch information
colin-axner and fedekunze authored Aug 24, 2020
1 parent 3d969a1 commit 49e4d05
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 39 deletions.
16 changes: 7 additions & 9 deletions x/ibc/02-client/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/stretchr/testify/suite"

client "github.com/cosmos/cosmos-sdk/x/ibc/02-client"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing"
)

Expand All @@ -28,26 +30,22 @@ func TestClientTestSuite(t *testing.T) {
suite.Run(t, new(ClientTestSuite))
}

/* TODO: uncomment once simapp is switched to proto
func (suite *ClientTestSuite) TestBeginBlocker() {
prevHeight := suite.ctx.BlockHeight()
prevHeight := suite.chainA.GetContext().BlockHeight()

localHostClient, found := suite.app.IBCKeeper.ClientKeeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost)
suite.Require().True(found)
localHostClient := suite.chainA.GetClientState(exported.ClientTypeLocalHost)
suite.Require().Equal(prevHeight, int64(localHostClient.GetLatestHeight()))

for i := 0; i < 10; i++ {
// increment height
suite.ctx = suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1)
suite.coordinator.CommitBlock(suite.chainA, suite.chainB)

suite.Require().NotPanics(func() {
client.BeginBlocker(suite.ctx, suite.app.IBCKeeper.ClientKeeper)
client.BeginBlocker(suite.chainA.GetContext(), suite.chainA.App.IBCKeeper.ClientKeeper)
}, "BeginBlocker shouldn't panic")

localHostClient, found = suite.app.IBCKeeper.ClientKeeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost)
suite.Require().True(found)
localHostClient = suite.chainA.GetClientState(exported.ClientTypeLocalHost)
suite.Require().Equal(prevHeight+1, int64(localHostClient.GetLatestHeight()))
prevHeight = int64(localHostClient.GetLatestHeight())
}
}
*/
2 changes: 2 additions & 0 deletions x/ibc/02-client/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
}

// ExportGenesis returns the ibc client submodule's exported genesis.
// NOTE: CreateLocalhost should always be false on export since a
// created localhost will be included in the exported clients.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
return types.GenesisState{
Clients: k.GetAllGenesisClients(ctx),
Expand Down
6 changes: 0 additions & 6 deletions x/ibc/02-client/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ func (suite *ClientTestSuite) TestHandleCreateClientLocalHost() {
msg exported.MsgCreateClient
expPass bool
}{
{
"localhost",
exported.ClientTypeLocalHost,
&localhosttypes.MsgCreateClient{suite.chainA.SenderAccount.GetAddress()},
true,
},
{
"tendermint client",
"gaiamainnet",
Expand Down
6 changes: 3 additions & 3 deletions x/ibc/02-client/keeper/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
localhosttypes "github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
)

Expand Down Expand Up @@ -229,16 +230,15 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {
}
}

/*
func (suite *KeeperTestSuite) TestUpdateClientLocalhost() {
var localhostClient exported.ClientState = localhosttypes.NewClientState(suite.header.ChainID, suite.ctx.BlockHeight())
var localhostClient exported.ClientState = localhosttypes.NewClientState(suite.header.Header.GetChainID(), suite.ctx.BlockHeight())

suite.ctx = suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1)

updatedClientState, err := suite.keeper.UpdateClient(suite.ctx, exported.ClientTypeLocalHost, nil)
suite.Require().NoError(err, err)
suite.Require().Equal(localhostClient.GetLatestHeight()+1, updatedClientState.GetLatestHeight())
}*/
}

func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
altPrivVal := tmtypes.NewMockPV()
Expand Down
32 changes: 20 additions & 12 deletions x/ibc/02-client/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
Expand Down Expand Up @@ -110,19 +111,20 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
{
"success",
func() {
clientState := ibctmtypes.NewClientState(testChainID, ibctmtypes.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, 0, commitmenttypes.GetSDKSpecs())
clientState2 := ibctmtypes.NewClientState(testChainID, ibctmtypes.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, 0, commitmenttypes.GetSDKSpecs())
suite.keeper.SetClientState(suite.ctx, testClientID, clientState)
suite.keeper.SetClientState(suite.ctx, testClientID2, clientState2)
clientA1, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint)
clientA2, _ := suite.coordinator.CreateClient(suite.chainA, suite.chainB, exported.Tendermint)

idcs := types.NewIdentifiedClientState(testClientID, clientState)
idcs2 := types.NewIdentifiedClientState(testClientID2, clientState2)
clientStateA1 := suite.chainA.GetClientState(clientA1)
clientStateA2 := suite.chainA.GetClientState(clientA2)

// order is swapped because the res is sorted by client id
expClientStates = []*types.IdentifiedClientState{&idcs2, &idcs}
idcs := types.NewIdentifiedClientState(clientA1, clientStateA1)
idcs2 := types.NewIdentifiedClientState(clientA2, clientStateA2)

// order is sorted by client id, localhost is last
expClientStates = []*types.IdentifiedClientState{&idcs, &idcs2}
req = &types.QueryClientStatesRequest{
Pagination: &query.PageRequest{
Limit: 3,
Limit: 7,
CountTotal: true,
},
}
Expand All @@ -134,11 +136,18 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset
expClientStates = nil

tc.malleate()
ctx := sdk.WrapSDKContext(suite.ctx)

res, err := suite.queryClient.ClientStates(ctx, req)
// always add localhost which is created by default in init genesis
localhostClientState := suite.chainA.GetClientState(exported.Localhost.String())
identifiedLocalhost := types.NewIdentifiedClientState(exported.Localhost.String(), localhostClientState)
expClientStates = append(expClientStates, &identifiedLocalhost)

ctx := sdk.WrapSDKContext(suite.chainA.GetContext())

res, err := suite.chainA.QueryServer.ClientStates(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
Expand All @@ -147,7 +156,6 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
for i := range expClientStates {
suite.Require().Equal(expClientStates[i].ClientId, res.ClientStates[i].ClientId)
suite.Require().NotNil(res.ClientStates[i].ClientState)
expClientStates[i].ClientState.ClearCachedValue()
suite.Require().Equal(expClientStates[i].ClientState, res.ClientStates[i].ClientState)
}
} else {
Expand Down
25 changes: 17 additions & 8 deletions x/ibc/02-client/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
localhosttypes "github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand All @@ -41,6 +42,11 @@ const (
type KeeperTestSuite struct {
suite.Suite

coordinator *ibctesting.Coordinator

chainA *ibctesting.TestChain
chainB *ibctesting.TestChain

cdc codec.Marshaler
ctx sdk.Context
keeper *keeper.Keeper
Expand All @@ -56,6 +62,11 @@ type KeeperTestSuite struct {
}

func (suite *KeeperTestSuite) SetupTest() {
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2)

suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0))
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1))

isCheckTx := false
suite.now = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
suite.past = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
Expand Down Expand Up @@ -207,10 +218,9 @@ func (suite KeeperTestSuite) TestGetAllClients() {
}

// add localhost client
// TODO: uncomment after simapp is migrated to proto
// localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost)
// suite.Require().True(found)
// expClients = append(expClients, localHostClient)
localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost)
suite.Require().True(found)
expClients = append(expClients, localHostClient)

clients := suite.keeper.GetAllClients(suite.ctx)
suite.Require().Len(clients, len(expClients))
Expand All @@ -235,10 +245,9 @@ func (suite KeeperTestSuite) TestGetAllGenesisClients() {
}

// add localhost client
// TODO: uncomment after simapp is migrated to proto
// localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost)
// suite.Require().True(found)
// expGenClients = append(expGenClients, types.NewIdentifiedClientState(exported.ClientTypeLocalHost, localHostClient))
localHostClient, found := suite.keeper.GetClientState(suite.ctx, exported.ClientTypeLocalHost)
suite.Require().True(found)
expGenClients = append(expGenClients, types.NewIdentifiedClientState(exported.ClientTypeLocalHost, localHostClient))

genClients := suite.keeper.GetAllGenesisClients(suite.ctx)

Expand Down
2 changes: 1 addition & 1 deletion x/ibc/02-client/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func DefaultGenesisState() GenesisState {
return GenesisState{
Clients: []IdentifiedClientState{},
ClientsConsensus: ClientsConsensusStates{},
CreateLocalhost: false,
CreateLocalhost: true,
}
}

Expand Down

0 comments on commit 49e4d05

Please sign in to comment.