Skip to content

Commit

Permalink
E2e setup cleanup (#442)
Browse files Browse the repository at this point in the history
* wip

* clean setup

* Update setup.go

* remove unused fields

* Update setup.go

* Update setup.go
  • Loading branch information
shaspitz authored Nov 8, 2022
1 parent 256c26b commit dc19c57
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 47 deletions.
15 changes: 4 additions & 11 deletions tests/e2e/channel_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func (suite *CCVTestSuite) TestConsumerGenesis() {

genesis := consumerKeeper.ExportGenesis(suite.consumerChain.GetContext())

suite.Require().Equal(suite.providerClient, genesis.ProviderClientState)
suite.Require().Equal(suite.providerConsState, genesis.ProviderConsensusState)
// Confirm that client and cons state are exported from consumer keeper properly
consumerEndpointClientState, consumerEndpointConsState := suite.GetConsumerEndpointClientAndConsState()
suite.Require().Equal(consumerEndpointClientState, genesis.ProviderClientState)
suite.Require().Equal(consumerEndpointConsState, genesis.ProviderConsensusState)

suite.Require().NotPanics(func() {
consumerKeeper.InitGenesis(suite.consumerChain.GetContext(), genesis)
Expand Down Expand Up @@ -85,15 +87,6 @@ func (suite *CCVTestSuite) TestConsumerGenesis() {
})
}

// TestProviderClientMatches tests that the provider client managed by the consumer keeper matches the client keeper's client state
func (suite *CCVTestSuite) TestProviderClientMatches() {
providerClientID, ok := suite.consumerApp.GetConsumerKeeper().GetProviderClientID(suite.consumerCtx())
suite.Require().True(ok)

clientState, _ := suite.consumerApp.GetIBCKeeper().ClientKeeper.GetClientState(suite.consumerCtx(), providerClientID)
suite.Require().Equal(suite.providerClient, clientState, "stored client state does not match genesis provider client")
}

// TestInitTimeout tests the init timeout
func (suite *CCVTestSuite) TestInitTimeout() {
testCases := []struct {
Expand Down
16 changes: 16 additions & 0 deletions tests/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,19 @@ func (suite *CCVTestSuite) CreateCustomClient(endpoint *ibctesting.Endpoint, unb
endpoint.ClientID, err = ibctesting.ParseClientIDFromEvents(res.GetEvents())
require.NoError(endpoint.Chain.T, err)
}

func (suite *CCVTestSuite) GetConsumerEndpointClientAndConsState() (exported.ClientState, exported.ConsensusState) {

clientID, found := suite.consumerApp.GetConsumerKeeper().GetProviderClientID(suite.consumerCtx())
suite.Require().True(found)

clientState, found := suite.consumerApp.GetIBCKeeper().ClientKeeper.GetClientState(
suite.consumerCtx(), clientID)
suite.Require().True(found)

consState, found := suite.consumerApp.GetIBCKeeper().ClientKeeper.GetClientConsensusState(
suite.consumerCtx(), clientID, clientState.GetLatestHeight())
suite.Require().True(found)

return clientState, consState
}
87 changes: 53 additions & 34 deletions tests/e2e/setup.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package e2e

import (
"time"

ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types"

"bytes"
Expand All @@ -28,16 +26,14 @@ import (
// Any method implemented for this struct will be ran when suite.Run() is called.
type CCVTestSuite struct {
suite.Suite
coordinator *ibctesting.Coordinator
providerChain *ibctesting.TestChain
consumerChain *ibctesting.TestChain
providerApp e2e.ProviderApp
consumerApp e2e.ConsumerApp
providerClient *ibctmtypes.ClientState
providerConsState *ibctmtypes.ConsensusState
path *ibctesting.Path
transferPath *ibctesting.Path
setupCallback SetupCallback
coordinator *ibctesting.Coordinator
providerChain *ibctesting.TestChain
consumerChain *ibctesting.TestChain
providerApp e2e.ProviderApp
consumerApp e2e.ConsumerApp
path *ibctesting.Path
transferPath *ibctesting.Path
setupCallback SetupCallback
}

// NewCCVTestSuite returns a new instance of CCVTestSuite, ready to be tested against using suite.Run().
Expand Down Expand Up @@ -94,40 +90,40 @@ func (suite *CCVTestSuite) SetupTest() {
suite.providerChain.NextBlock()

// initialize the consumer chain with the genesis state stored on the provider
consumerGenesis, found := providerKeeper.GetConsumerGenesis(
consumerGenesisState, found := providerKeeper.GetConsumerGenesis(
suite.providerCtx(),
suite.consumerChain.ChainID,
)
suite.Require().True(found, "consumer genesis not found")
consumerKeeper.InitGenesis(suite.consumerCtx(), &consumerGenesis)
suite.providerClient = consumerGenesis.ProviderClientState
suite.providerConsState = consumerGenesis.ProviderConsensusState
consumerKeeper.InitGenesis(suite.consumerCtx(), &consumerGenesisState)

// Confirm client and cons state for consumer were set correctly in InitGenesis
consumerEndpointClientState, consumerEndpointConsState := suite.GetConsumerEndpointClientAndConsState()
suite.Require().Equal(consumerGenesisState.ProviderClientState, consumerEndpointClientState)
suite.Require().Equal(consumerGenesisState.ProviderConsensusState, consumerEndpointConsState)

// create path for the CCV channel
suite.path = ibctesting.NewPath(suite.consumerChain, suite.providerChain)

// update CCV path with correct info
// - set provider endpoint's clientID
consumerClient, found := providerKeeper.GetConsumerClientId(
// Set provider endpoint's clientID
providerEndpointClientID, found := providerKeeper.GetConsumerClientId(
suite.providerCtx(),
suite.consumerChain.ChainID,
)
suite.Require().True(found, "provider endpoint clientID not found")
suite.path.EndpointB.ClientID = providerEndpointClientID

// Set consumer endpoint's clientID
consumerEndpointClientID, found := consumerKeeper.GetProviderClientID(suite.consumerChain.GetContext())
suite.Require().True(found, "consumer endpoint clientID not found")
suite.path.EndpointA.ClientID = consumerEndpointClientID

// Note: suite.path.EndpointA.ClientConfig and suite.path.EndpointB.ClientConfig are not populated,
// since these IBC testing package fields are unused in our tests.

// Confirm client config is now correct
suite.ValidateEndpointsClientConfig()

suite.Require().True(found, "consumer client not found")
suite.path.EndpointB.ClientID = consumerClient
// - set consumer endpoint's clientID
providerClient, found := consumerKeeper.GetProviderClientID(suite.consumerChain.GetContext())
suite.Require().True(found, "provider client not found")
suite.path.EndpointA.ClientID = providerClient
// - client config

trustingPeriodFraction := suite.providerApp.GetProviderKeeper().GetTrustingPeriodFraction(suite.providerCtx())
providerUnbondingPeriod := suite.providerApp.GetStakingKeeper().UnbondingTime(suite.providerCtx())
suite.path.EndpointB.ClientConfig.(*ibctesting.TendermintConfig).UnbondingPeriod = providerUnbondingPeriod
suite.path.EndpointB.ClientConfig.(*ibctesting.TendermintConfig).TrustingPeriod = providerUnbondingPeriod / time.Duration(trustingPeriodFraction)
consumerUnbondingPeriod := consumerKeeper.GetUnbondingPeriod(suite.consumerCtx())
suite.path.EndpointA.ClientConfig.(*ibctesting.TendermintConfig).UnbondingPeriod = consumerUnbondingPeriod
suite.path.EndpointA.ClientConfig.(*ibctesting.TendermintConfig).TrustingPeriod = consumerUnbondingPeriod / time.Duration(trustingPeriodFraction)
// - channel config
suite.path.EndpointA.ChannelConfig.PortID = ccv.ConsumerPortID
suite.path.EndpointB.ChannelConfig.PortID = ccv.ProviderPortID
Expand Down Expand Up @@ -206,3 +202,26 @@ func (suite *CCVTestSuite) SetupTransferChannel() {
err = suite.transferPath.EndpointA.UpdateClient()
suite.Require().NoError(err)
}

func (s CCVTestSuite) ValidateEndpointsClientConfig() {
consumerKeeper := s.consumerApp.GetConsumerKeeper()
providerStakingKeeper := s.providerApp.GetStakingKeeper()

consumerUnbondingPeriod := consumerKeeper.GetUnbondingPeriod(s.consumerCtx())
cs, ok := s.providerApp.GetIBCKeeper().ClientKeeper.GetClientState(s.providerCtx(), s.path.EndpointB.ClientID)
s.Require().True(ok)
s.Require().Equal(
consumerUnbondingPeriod,
cs.(*ibctmtypes.ClientState).UnbondingPeriod,
"unexpected unbonding period in consumer client state",
)

providerUnbondingPeriod := providerStakingKeeper.UnbondingTime(s.providerCtx())
cs, ok = s.consumerApp.GetIBCKeeper().ClientKeeper.GetClientState(s.consumerCtx(), s.path.EndpointA.ClientID)
s.Require().True(ok)
s.Require().Equal(
providerUnbondingPeriod,
cs.(*ibctmtypes.ClientState).UnbondingPeriod,
"unexpected unbonding period in provider client state",
)
}
5 changes: 3 additions & 2 deletions x/ccv/provider/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func (k Keeper) MakeConsumerGenesis(ctx sdk.Context) (gen consumertypes.GenesisS
height := clienttypes.GetSelfHeight(ctx)

clientState := k.GetTemplateClient(ctx)
clientState.ChainId = ctx.ChainID()
clientState.LatestHeight = height //(+-1???)
clientState.ChainId = ctx.ChainID() // This will be the counter party chain ID for the consumer
clientState.LatestHeight = height //(+-1???)
clientState.TrustingPeriod = providerUnbondingPeriod / time.Duration(k.GetTrustingPeriodFraction(ctx))
clientState.UnbondingPeriod = providerUnbondingPeriod

Expand All @@ -224,6 +224,7 @@ func (k Keeper) MakeConsumerGenesis(ctx sdk.Context) (gen consumertypes.GenesisS

gen.Params.Enabled = true
gen.NewChain = true
// This will become the ccv client state for the consumer
gen.ProviderClientState = clientState
gen.ProviderConsensusState = consState.(*ibctmtypes.ConsensusState)

Expand Down

0 comments on commit dc19c57

Please sign in to comment.