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

Replace hardcoded constants with params #385

Closed
wants to merge 16 commits into from
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
)

require (
github.com/confio/ics23/go v0.7.0
github.com/golang/mock v1.6.0
github.com/oxyno-zeta/gomock-extra-matcher v1.1.0
github.com/regen-network/cosmos-proto v0.3.1
Expand All @@ -48,7 +49,6 @@ require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/coinbase/rosetta-sdk-go v0.7.0 // indirect
github.com/confio/ics23/go v0.7.0 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/iavl v0.17.3 // indirect
Expand Down
16 changes: 16 additions & 0 deletions proto/interchain_security/ccv/consumer/v1/consumer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "google/protobuf/any.proto";
import "cosmos/staking/v1beta1/staking.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/duration.proto";

// Params defines the parameters for CCV consumer module
message Params {
Expand All @@ -29,6 +30,21 @@ message Params {
// provider handshake procedure.
string distribution_transmission_channel = 3;
string provider_fee_pool_addr_str = 4;
// Sent CCV related IBC packets will timeout after this duration
google.protobuf.Duration ccv_timeout_period = 5
[(gogoproto.nullable) = false, (gogoproto.stdduration) = true];

// Sent transfer related IBC packets will timeout after this duration
google.protobuf.Duration transfer_timeout_period = 6
[(gogoproto.nullable) = false, (gogoproto.stdduration) = true];

// The fraction of tokens allocated to the consumer redistribution address
// during distribution events. The fraction is a string representing a
// decimal number. For example "0.75" would represent 75%.
string consumer_redistribution_fraction = 7;

// The number of historical info entries to persist in store
int64 num_historical_entries = 8;
}

// LastTransmissionBlockHeight is the last time validator holding
Expand Down
6 changes: 6 additions & 0 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option go_package = "github.com/cosmos/interchain-security/x/ccv/provider/types"

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
import "ibc/core/client/v1/client.proto";
import "ibc/lightclients/tendermint/v1/tendermint.proto";

Expand Down Expand Up @@ -58,6 +59,11 @@ message ConsumerAdditionProposal {
// Params defines the parameters for CCV Provider module
message Params {
ibc.lightclients.tendermint.v1.ClientState template_client = 1;
// Sent IBC packets will timeout after this duration
google.protobuf.Duration ccv_timeout_period = 2
[(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
// TrustingPeriodFraction is used to compute the provider IBC client's TrustingPeriod
int64 trusting_period_fraction = 3;
}

message HandshakeMetadata {
Expand Down
6 changes: 5 additions & 1 deletion tests/difference/core/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ func (b *Builder) createConsumerGenesis(tmConfig *ibctesting.TendermintConfig) *
1000, // ignore distribution
"", // ignore distribution
"", // ignore distribution
ccv.DefaultCCVTimeoutPeriod,
consumertypes.DefaultTransferTimeoutPeriod,
consumertypes.DefaultConsumerRedistributeFrac,
consumertypes.DefaultNumHistoricalEntries,
)
return consumertypes.NewInitialGenesisState(providerClient, providerConsState, valUpdates, consumertypes.SlashRequests{}, params)
}
Expand Down Expand Up @@ -534,7 +538,7 @@ func (b *Builder) doIBCHandshake() {
func (b *Builder) sendEmptyVSCPacketToFinishHandshake() {
vscID := b.providerKeeper().GetValidatorSetUpdateId(b.providerChain().GetContext())

timeout := uint64(types.GetTimeoutTimestamp(b.chain(P).CurrentHeader.Time).UnixNano())
timeout := uint64(b.chain(P).CurrentHeader.Time.Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())

pd := types.NewValidatorSetChangePacketData(
[]abci.ValidatorUpdate{},
Expand Down
9 changes: 5 additions & 4 deletions tests/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

providertypes "github.com/cosmos/interchain-security/x/ccv/provider/types"
ccv "github.com/cosmos/interchain-security/x/ccv/types"
"github.com/cosmos/interchain-security/x/ccv/utils"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -293,7 +294,7 @@ func (suite *CCVTestSuite) SendEmptyVSCPacket() {
providerKeeper := suite.providerChain.App.(*appProvider.App).ProviderKeeper

oldBlockTime := suite.providerChain.GetContext().BlockTime()
timeout := uint64(ccv.GetTimeoutTimestamp(oldBlockTime).UnixNano())
timeout := uint64(oldBlockTime.Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())

valUpdateID := providerKeeper.GetValidatorSetUpdateId(suite.providerChain.GetContext())

Expand All @@ -320,7 +321,7 @@ func (suite *CCVTestSuite) SendEmptyVSCPacket() {
// Note that it must be called before sending the embedding IBC packet.
func (suite *CCVTestSuite) commitSlashPacket(ctx sdk.Context, packetData ccv.SlashPacketData) []byte {
oldBlockTime := ctx.BlockTime()
timeout := uint64(ccv.GetTimeoutTimestamp(oldBlockTime).UnixNano())
timeout := uint64(oldBlockTime.Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())

packet := channeltypes.NewPacket(packetData.GetBytes(), 1, ccv.ConsumerPortID, suite.path.EndpointA.ChannelID,
ccv.ProviderPortID, suite.path.EndpointB.ChannelID, clienttypes.Height{}, timeout)
Expand All @@ -334,7 +335,7 @@ func incrementTimeBy(s *CCVTestSuite, jumpPeriod time.Duration) {
consumerUnbondingPeriod, found := s.consumerChain.App.(*appConsumer.App).ConsumerKeeper.GetUnbondingTime(s.consumerChain.GetContext())
s.Require().True(found)
split := 1
if jumpPeriod > consumerUnbondingPeriod/utils.TrustingPeriodFraction {
if jumpPeriod > consumerUnbondingPeriod/providertypes.DefaultTrustingPeriodFraction {
// Make sure the clients do not expire
split = 4
jumpPeriod = jumpPeriod / 4
Expand Down Expand Up @@ -365,7 +366,7 @@ func (suite *CCVTestSuite) CreateCustomClient(endpoint *ibctesting.Endpoint, unb
tmConfig, ok := endpoint.ClientConfig.(*ibctesting.TendermintConfig)
require.True(endpoint.Chain.T, ok)
tmConfig.UnbondingPeriod = unbondingPeriod
tmConfig.TrustingPeriod = unbondingPeriod / utils.TrustingPeriodFraction
tmConfig.TrustingPeriod = unbondingPeriod / providertypes.DefaultTrustingPeriodFraction

height := endpoint.Counterparty.Chain.LastHeader.GetHeight().(clienttypes.Height)
UpgradePath := []string{"upgrade", "upgradedIBCState"}
Expand Down
167 changes: 8 additions & 159 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,33 @@ 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)
var consumerFraction, _ = sdk.NewDecFromStr(consumertypes.DefaultConsumerRedistributeFrac)

type ConsumerDemocracyTestSuite struct {
suite.Suite

coordinator *ibctesting.Coordinator

// testing chains
coordinator *ibctesting.Coordinator
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 sets up in-mem state for the group of tests relevant to ccv with a democracy consumer
// TODO: Make this method more generalizable to be called by any provider/consumer implementation
func (democSuite *ConsumerDemocracyTestSuite) SetupTest() {
democSuite.coordinator, democSuite.providerChain,
democSuite.consumerChain = simapp.NewProviderConsumerDemocracyCoordinator(democSuite.T())
}

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

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

s.consumerChain.NextBlock()
stakingKeeper := s.consumerChain.App.(*appConsumer.App).StakingKeeper
Expand Down Expand Up @@ -366,10 +219,6 @@ 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()
}
3 changes: 1 addition & 2 deletions tests/e2e/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
transfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
app "github.com/cosmos/interchain-security/app/consumer"
providerApp "github.com/cosmos/interchain-security/app/provider"
consumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"
ccv "github.com/cosmos/interchain-security/x/ccv/types"
)
Expand Down Expand Up @@ -45,7 +44,7 @@ func (s *CCVTestSuite) TestRewardsDistribution() {
s.Require().Equal(sdk.NewInt(100).Add(feePoolTokensOld.AmountOf(sdk.DefaultBondDenom)), feePoolTokens.AmountOf(sdk.DefaultBondDenom))

//calculate the reward for consumer and provider chain. Consumer will receive ConsumerRedistributeFrac, the rest is going to provider
frac, err := sdk.NewDecFromStr(consumerkeeper.ConsumerRedistributeFrac)
frac, err := sdk.NewDecFromStr(consumertypes.DefaultConsumerRedistributeFrac)
s.Require().NoError(err)
consumerExpectedRewards, _ := sdk.NewDecCoinsFromCoins(feePoolTokens...).MulDec(frac).TruncateDecimal()
providerExpectedRewards := feePoolTokens.Sub(consumerExpectedRewards)
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/normal_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package e2e_test
import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
appConsumer "github.com/cosmos/interchain-security/app/consumer"
"github.com/cosmos/interchain-security/x/ccv/consumer/types"
consumertypes "github.com/cosmos/interchain-security/x/ccv/consumer/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

Expand All @@ -22,7 +22,7 @@ func (k CCVTestSuite) TestTrackHistoricalInfo() {
createVal := func(k CCVTestSuite) {
// add new validator to consumer states
pk := ed25519.GenPrivKey().PubKey()
cVal, err := types.NewCCValidator(pk.Address(), int64(1), pk)
cVal, err := consumertypes.NewCCValidator(pk.Address(), int64(1), pk)
k.Require().NoError(err)

consumerKeeper.SetCCValidator(k.consumerChain.GetContext(), cVal)
Expand All @@ -39,7 +39,7 @@ func (k CCVTestSuite) TestTrackHistoricalInfo() {
createVal,
createVal,
func(k CCVTestSuite) {
newHeight := k.consumerChain.GetContext().BlockHeight() + int64(types.HistoricalEntries)
newHeight := k.consumerChain.GetContext().BlockHeight() + int64(consumertypes.DefaultNumHistoricalEntries)
header := tmproto.Header{
ChainID: "HelloChain",
Height: newHeight,
Expand Down Expand Up @@ -71,7 +71,7 @@ func (k CCVTestSuite) TestTrackHistoricalInfo() {
expLen: 0,
},
{
height: initHeight + int64(types.HistoricalEntries) + 2,
height: initHeight + int64(consumertypes.DefaultNumHistoricalEntries) + 2,
found: true,
expLen: initValsetLen + 2,
},
Expand Down
Loading