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

Allow localhost to be created #7148

Merged
merged 3 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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",
Copy link
Member

Choose a reason for hiding this comment

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

Why is this test-case removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

localhost is already created in genesis. This test would fail, changing it to false would just make it a duplication of the later test case "client already exists",

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()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I had to do this bc the Equal statement would fail if the expected response had the value cached. Using Proto equal would work too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It passes now when using the testing package. Going to leave as is but we can add back if necessary

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