Skip to content

Commit

Permalink
Add UpgradeTimeout as a params field in the channel submodule (#4411)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Aug 23, 2023
1 parent f1e8ae8 commit f230633
Show file tree
Hide file tree
Showing 18 changed files with 1,014 additions and 209 deletions.
7 changes: 7 additions & 0 deletions modules/core/04-channel/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package channel

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v7/modules/core/04-channel/keeper"
Expand All @@ -10,6 +12,10 @@ import (
// InitGenesis initializes the ibc channel submodule's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, gs types.GenesisState) {
if err := gs.Params.Validate(); err != nil {
panic(fmt.Sprintf("invalid ibc channel genesis state parameters: %v", err))
}
k.SetParams(ctx, gs.Params)
for _, channel := range gs.Channels {
ch := types.NewChannel(channel.State, channel.Ordering, channel.Counterparty, channel.ConnectionHops, channel.Version)
k.SetChannel(ctx, channel.PortId, channel.ChannelId, ch)
Expand Down Expand Up @@ -46,5 +52,6 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
RecvSequences: k.GetAllPacketRecvSeqs(ctx),
AckSequences: k.GetAllPacketAckSeqs(ctx),
NextChannelSequence: k.GetNextChannelSequence(ctx),
Params: k.GetParams(ctx),
}
}
20 changes: 20 additions & 0 deletions modules/core/04-channel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,26 @@ func (k Keeper) deleteUpgradeInfo(ctx sdk.Context, portID, channelID string) {
k.deleteCounterpartyUpgrade(ctx, portID, channelID)
}

// SetParams sets the channel parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&params)
store.Set([]byte(types.ParamsKey), bz)
}

// GetParams returns the total set of the channel parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.ParamsKey))
if bz == nil { // only panic on unset params and not on empty params
panic("channel params are not set in store")
}

var params types.Params
k.cdc.MustUnmarshal(bz, &params)
return params
}

// common functionality for IteratePacketCommitment and IteratePacketAcknowledgement
func (Keeper) iterateHashes(ctx sdk.Context, iterator db.Iterator, cb func(portID, channelID string, sequence uint64, hash []byte) bool) {
defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() })
Expand Down
58 changes: 58 additions & 0 deletions modules/core/04-channel/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
testifysuite "github.com/stretchr/testify/suite"

transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
ibcmock "github.com/cosmos/ibc-go/v7/testing/mock"
)
Expand Down Expand Up @@ -483,3 +485,59 @@ func (suite *KeeperTestSuite) TestSetUpgradeErrorReceipt() {
suite.Require().True(found)
suite.Require().Equal(expErrorReceipt, errorReceipt)
}

// TestDefaultSetParams tests the default params set are what is expected
func (suite *KeeperTestSuite) TestDefaultSetParams() {
expParams := types.DefaultParams()

channelKeeper := suite.chainA.App.GetIBCKeeper().ChannelKeeper
params := channelKeeper.GetParams(suite.chainA.GetContext())

suite.Require().Equal(expParams, params)
suite.Require().Equal(expParams.UpgradeTimeout, channelKeeper.GetParams(suite.chainA.GetContext()).UpgradeTimeout)
}

// TestParams tests that Param setting and retrieval works properly
func (suite *KeeperTestSuite) TestParams() {
testCases := []struct {
name string
input types.Params
expPass bool
}{
{"success: set default params", types.DefaultParams(), true},
{"success: zero timeout height", types.NewParams(types.NewTimeout(clienttypes.ZeroHeight(), 10000)), true},
{"success: zero timestamp timestamp", types.NewParams(types.NewTimeout(types.DefaultTimeout.Height, 0)), true},
{"fail: zero timeout", types.NewParams(types.NewTimeout(clienttypes.ZeroHeight(), 0)), false},
}

for _, tc := range testCases {
tc := tc

suite.Run(tc.name, func() {
suite.SetupTest() // reset
ctx := suite.chainA.GetContext()
err := tc.input.Validate()
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetParams(ctx, tc.input)
if tc.expPass {
suite.Require().NoError(err)
expected := tc.input
p := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetParams(ctx)
suite.Require().Equal(expected, p)
} else {
suite.Require().Error(err)
}
})
}
}

// TestUnsetParams tests that trying to get params that are not set panics.
func (suite *KeeperTestSuite) TestUnsetParams() {
suite.SetupTest()
ctx := suite.chainA.GetContext()
store := ctx.KVStore(suite.chainA.GetSimApp().GetKey(exported.StoreKey))
store.Delete([]byte(types.ParamsKey))

suite.Require().Panics(func() {
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetParams(ctx)
})
}
12 changes: 8 additions & 4 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,15 +785,19 @@ func (k Keeper) startFlushing(ctx sdk.Context, portID, channelID string, upgrade
}

upgrade.LatestSequenceSend = nextSequenceSend - 1
upgrade.Timeout = getUpgradeTimeout()
upgrade.Timeout = k.getUpgradeTimeout(ctx)
k.SetUpgrade(ctx, portID, channelID, *upgrade)

return nil
}

// TODO: use a hard coded value for now. Will be resolved in https://github.com/cosmos/ibc-go/issues/4313
func getUpgradeTimeout() types.Timeout {
return types.NewTimeout(clienttypes.NewHeight(1, 1000), 0)
// getUpgradeTimeout returns the absolute timeout for the given upgrade.
func (k Keeper) getUpgradeTimeout(ctx sdk.Context) types.Timeout {
// relativeTimeout := k.GetParams(ctx).UpgradeTimeout
// absoluteTimeoutHeight := clienttypes.NewHeight(clienttypes.ParseChainID(ctx.ChainID()), uint64(ctx.BlockHeight())+relativeTimeout.Height.RevisionHeight)
// absoluteTimeoutTimestamp := uint64(ctx.BlockTime().UnixNano()) + relativeTimeout.Timestamp
// return types.NewTimeout(absoluteTimeoutHeight, absoluteTimeoutTimestamp)
return k.GetParams(ctx).UpgradeTimeout
}

// syncUpgradeSequence ensures current upgrade handshake only continues if both channels are using the same upgrade sequence,
Expand Down
Loading

0 comments on commit f230633

Please sign in to comment.