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

Remove duplicate e2e test setup for democracy consumer #381

Merged
merged 8 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
179 changes: 15 additions & 164 deletions tests/e2e/democracy_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package e2e_test

import (
"bytes"
"fmt"
"strconv"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -22,176 +18,37 @@ import (
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
proposaltypes "github.com/cosmos/cosmos-sdk/x/params/types/proposal"
appConsumer "github.com/cosmos/interchain-security/app/consumer-democracy"
appProvider "github.com/cosmos/interchain-security/app/provider"
"github.com/cosmos/interchain-security/testutil/simapp"
consumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/x/ccv/types"
"github.com/cosmos/interchain-security/x/ccv/utils"

tmtypes "github.com/tendermint/tendermint/types"

"github.com/stretchr/testify/suite"
)

var consumerFraction, _ = sdk.NewDecFromStr(consumerkeeper.ConsumerRedistributeFrac)

type ConsumerDemocracyTestSuite struct {
underlyingSuite CCVTestSuite
suite.Suite

coordinator *ibctesting.Coordinator

// testing chains
providerChain *ibctesting.TestChain
consumerChain *ibctesting.TestChain

path *ibctesting.Path
transferPath *ibctesting.Path
}

func (s *ConsumerDemocracyTestSuite) SetupTest() {
s.coordinator, s.providerChain, s.consumerChain = simapp.NewProviderConsumerDemocracyCoordinator(s.T())

// valsets must match
providerValUpdates := tmtypes.TM2PB.ValidatorUpdates(s.providerChain.Vals)
consumerValUpdates := tmtypes.TM2PB.ValidatorUpdates(s.consumerChain.Vals)
s.Require().True(len(providerValUpdates) == len(consumerValUpdates), "initial valset not matching")
for i := 0; i < len(providerValUpdates); i++ {
addr1 := utils.GetChangePubKeyAddress(providerValUpdates[i])
addr2 := utils.GetChangePubKeyAddress(consumerValUpdates[i])
s.Require().True(bytes.Equal(addr1, addr2), "validator mismatch")
}

// move both chains to the next block
s.providerChain.NextBlock()
s.consumerChain.NextBlock()

// create consumer client on provider chain and set as consumer client for consumer chainID in provider keeper.
err := s.providerChain.App.(*appProvider.App).ProviderKeeper.CreateConsumerClient(
s.providerCtx(),
s.consumerChain.ChainID,
s.consumerChain.LastHeader.GetHeight().(clienttypes.Height),
false,
)
s.Require().NoError(err)

// move provider to next block to commit the state
s.providerChain.NextBlock()

// initialize the consumer chain with the genesis state stored on the provider
consumerGenesis, found := s.providerChain.App.(*appProvider.App).ProviderKeeper.GetConsumerGenesis(
s.providerCtx(),
s.consumerChain.ChainID,
)
s.Require().True(found, "consumer genesis not found")
s.consumerChain.App.(*appConsumer.App).ConsumerKeeper.InitGenesis(s.consumerChain.GetContext(), &consumerGenesis)

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

// update CCV path with correct info
// - set provider endpoint's clientID
consumerClient, found := s.providerChain.App.(*appProvider.App).ProviderKeeper.GetConsumerClientId(
s.providerCtx(),
s.consumerChain.ChainID,
)
s.Require().True(found, "consumer client not found")
s.path.EndpointB.ClientID = consumerClient
// - set consumer endpoint's clientID
providerClient, found := s.consumerChain.App.(*appConsumer.App).ConsumerKeeper.GetProviderClientID(s.consumerChain.GetContext())
s.Require().True(found, "provider client not found")
s.path.EndpointA.ClientID = providerClient
// - client config
providerUnbondingPeriod := s.providerChain.App.(*appProvider.App).GetStakingKeeper().UnbondingTime(s.providerCtx())
s.path.EndpointB.ClientConfig.(*ibctesting.TendermintConfig).UnbondingPeriod = providerUnbondingPeriod
s.path.EndpointB.ClientConfig.(*ibctesting.TendermintConfig).TrustingPeriod = providerUnbondingPeriod / utils.TrustingPeriodFraction
consumerUnbondingPeriod := utils.ComputeConsumerUnbondingPeriod(providerUnbondingPeriod)
s.path.EndpointA.ClientConfig.(*ibctesting.TendermintConfig).UnbondingPeriod = consumerUnbondingPeriod
s.path.EndpointA.ClientConfig.(*ibctesting.TendermintConfig).TrustingPeriod = consumerUnbondingPeriod / utils.TrustingPeriodFraction
// - channel config
s.path.EndpointA.ChannelConfig.PortID = types.ConsumerPortID
s.path.EndpointB.ChannelConfig.PortID = types.ProviderPortID
s.path.EndpointA.ChannelConfig.Version = types.Version
s.path.EndpointB.ChannelConfig.Version = types.Version
s.path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED
s.path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED

// set chains sender account number
// TODO: to be fixed in #151
err = s.path.EndpointB.Chain.SenderAccount.SetAccountNumber(6)
s.Require().NoError(err)
err = s.path.EndpointA.Chain.SenderAccount.SetAccountNumber(0)
s.Require().NoError(err)

// create path for the transfer channel
s.transferPath = ibctesting.NewPath(s.consumerChain, s.providerChain)
s.transferPath.EndpointA.ChannelConfig.PortID = transfertypes.PortID
s.transferPath.EndpointB.ChannelConfig.PortID = transfertypes.PortID
s.transferPath.EndpointA.ChannelConfig.Version = transfertypes.Version
s.transferPath.EndpointB.ChannelConfig.Version = transfertypes.Version
}

func (s *ConsumerDemocracyTestSuite) SetupCCVChannel() {
s.StartSetupCCVChannel()
s.CompleteSetupCCVChannel()
s.SetupTransferChannel()
}

func (s *ConsumerDemocracyTestSuite) StartSetupCCVChannel() {
s.coordinator.CreateConnections(s.path)

err := s.path.EndpointA.ChanOpenInit()
s.Require().NoError(err)

err = s.path.EndpointB.ChanOpenTry()
s.Require().NoError(err)
}

func (s *ConsumerDemocracyTestSuite) CompleteSetupCCVChannel() {
err := s.path.EndpointA.ChanOpenAck()
s.Require().NoError(err)

err = s.path.EndpointB.ChanOpenConfirm()
s.Require().NoError(err)

// ensure counterparty is up to date
err = s.path.EndpointA.UpdateClient()
s.Require().NoError(err)
}

func (s *ConsumerDemocracyTestSuite) SetupTransferChannel() {
// transfer path will use the same connection as ccv path

s.transferPath.EndpointA.ClientID = s.path.EndpointA.ClientID
s.transferPath.EndpointA.ConnectionID = s.path.EndpointA.ConnectionID
s.transferPath.EndpointB.ClientID = s.path.EndpointB.ClientID
s.transferPath.EndpointB.ConnectionID = s.path.EndpointB.ConnectionID

// CCV channel handshake will automatically initiate transfer channel handshake on ACK
// so transfer channel will be on stage INIT when CompleteSetupCCVChannel returns.
s.transferPath.EndpointA.ChannelID = s.consumerChain.App.(*appConsumer.App).
ConsumerKeeper.GetDistributionTransmissionChannel(s.consumerChain.GetContext())

// Complete TRY, ACK, CONFIRM for transfer path
err := s.transferPath.EndpointB.ChanOpenTry()
s.Require().NoError(err)

err = s.transferPath.EndpointA.ChanOpenAck()
s.Require().NoError(err)

err = s.transferPath.EndpointB.ChanOpenConfirm()
s.Require().NoError(err)

// ensure counterparty is up to date
err = s.transferPath.EndpointA.UpdateClient()
s.Require().NoError(err)
// SetupTest is a shim for the democracy suite to share setup code with the ccv suite
func (democSuite *ConsumerDemocracyTestSuite) SetupTest() {
ccvSuite := CCVTestSuite{}
ccvSuite.SetT(democSuite.T())
ccvSuite.coordinator, ccvSuite.providerChain,
ccvSuite.consumerChain = simapp.NewProviderConsumerDemocracyCoordinator(democSuite.T())
CommonSetup(&ccvSuite, true)
democSuite.underlyingSuite = ccvSuite
}

func TestConsumerDemocracyTestSuite(t *testing.T) {
suite.Run(t, new(ConsumerDemocracyTestSuite))
}

func (s *ConsumerDemocracyTestSuite) TestDemocracyRewarsDistribution() {
func (suite *ConsumerDemocracyTestSuite) TestDemocracyRewarsDistribution() {

s := suite.underlyingSuite

s.consumerChain.NextBlock()
stakingKeeper := s.consumerChain.App.(*appConsumer.App).StakingKeeper
Expand Down Expand Up @@ -263,7 +120,9 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewarsDistribution() {
}
}

func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
func (suite *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
s := suite.underlyingSuite

govKeeper := s.consumerChain.App.(*appConsumer.App).GovKeeper
stakingKeeper := s.consumerChain.App.(*appConsumer.App).StakingKeeper
bankKeeper := s.consumerChain.App.(*appConsumer.App).BankKeeper
Expand Down Expand Up @@ -365,11 +224,3 @@ func getAccountsBalances(ctx sdk.Context, bankKeeper bankkeeper.Keeper, bondDeno

return accountsBalances
}

func (s *ConsumerDemocracyTestSuite) providerCtx() sdk.Context {
return s.providerChain.GetContext()
}

func (s *ConsumerDemocracyTestSuite) consumerCtx() sdk.Context {
return s.consumerChain.GetContext()
}
30 changes: 26 additions & 4 deletions tests/e2e/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package e2e_test

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

"bytes"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"

ccv "github.com/cosmos/interchain-security/x/ccv/types"
"github.com/cosmos/interchain-security/x/ccv/utils"

Expand All @@ -15,9 +17,11 @@ import (
ibctesting "github.com/cosmos/ibc-go/v3/testing"

appConsumer "github.com/cosmos/interchain-security/app/consumer"
appconsumerdemocracy "github.com/cosmos/interchain-security/app/consumer-democracy"
appProvider "github.com/cosmos/interchain-security/app/provider"
"github.com/cosmos/interchain-security/testutil/simapp"

abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/stretchr/testify/suite"
Expand All @@ -40,6 +44,24 @@ func TestCCVTestSuite(t *testing.T) {

func (suite *CCVTestSuite) SetupTest() {
suite.coordinator, suite.providerChain, suite.consumerChain = simapp.NewProviderConsumerCoordinator(suite.T())
CommonSetup(suite, false)
}

// Interface for democracy suite to use CommonSetup
type consumerKeeper interface {
InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState) []abci.ValidatorUpdate
GetProviderClientID(ctx sdk.Context) (string, bool)
}

// CommonSetup sets up a test suite, with the option to cast the consumer keeper to a democracy consumer
func CommonSetup(suite *CCVTestSuite, isDemocracyTest bool) {

var consumerKeeper consumerKeeper
if isDemocracyTest {
consumerKeeper = suite.consumerChain.App.(*appconsumerdemocracy.App).ConsumerKeeper
} else {
consumerKeeper = suite.consumerChain.App.(*appConsumer.App).ConsumerKeeper
}

// valsets must match
providerValUpdates := tmtypes.TM2PB.ValidatorUpdates(suite.providerChain.Vals)
Expand Down Expand Up @@ -72,7 +94,7 @@ func (suite *CCVTestSuite) SetupTest() {
suite.consumerChain.ChainID,
)
suite.Require().True(found, "consumer genesis not found")
suite.consumerChain.App.(*appConsumer.App).ConsumerKeeper.InitGenesis(suite.consumerChain.GetContext(), &consumerGenesis)
consumerKeeper.InitGenesis(suite.consumerChain.GetContext(), &consumerGenesis)
suite.providerClient = consumerGenesis.ProviderClientState
suite.providerConsState = consumerGenesis.ProviderConsensusState

Expand All @@ -89,7 +111,7 @@ func (suite *CCVTestSuite) SetupTest() {
suite.Require().True(found, "consumer client not found")
suite.path.EndpointB.ClientID = consumerClient
// - set consumer endpoint's clientID
providerClient, found := suite.consumerChain.App.(*appConsumer.App).ConsumerKeeper.GetProviderClientID(suite.consumerChain.GetContext())
providerClient, found := consumerKeeper.GetProviderClientID(suite.consumerChain.GetContext())
suite.Require().True(found, "provider client not found")
suite.path.EndpointA.ClientID = providerClient
// - client config
Expand Down