Skip to content

Commit

Permalink
chore: replace more occurrences of UnwrapSDKContext with service usage
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Dec 20, 2024
1 parent 6c3e11f commit 4224996
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
8 changes: 2 additions & 6 deletions modules/core/04-channel/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

errorsmod "cosmossdk.io/errors"

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

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
Expand Down Expand Up @@ -157,8 +155,7 @@ func (k *Keeper) RecvPacket(
}

// check if packet timed out by comparing it with the latest height of the chain
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano())
selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(k.HeaderService.HeaderInfo(ctx).Time.UnixNano())
timeout := types.NewTimeout(packet.GetTimeoutHeight().(clienttypes.Height), packet.GetTimeoutTimestamp())
if timeout.Elapsed(selfHeight, selfTimestamp) {
return "", errorsmod.Wrap(timeout.ErrTimeoutElapsed(selfHeight, selfTimestamp), "packet timeout elapsed")
Expand Down Expand Up @@ -477,9 +474,8 @@ func (k *Keeper) AcknowledgePacket(
func (k *Keeper) handleFlushState(ctx context.Context, packet types.Packet, channel types.Channel) {
if counterpartyUpgrade, found := k.GetCounterpartyUpgrade(ctx, packet.GetSourcePort(), packet.GetSourceChannel()); found {
timeout := counterpartyUpgrade.Timeout
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano())

selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(k.HeaderService.HeaderInfo(ctx).Time.UnixNano())
if timeout.Elapsed(selfHeight, selfTimestamp) {
// packet flushing timeout has expired, abort the upgrade
// committing an error receipt to state, deleting upgrade information and restoring the channel.
Expand Down
13 changes: 4 additions & 9 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
errorsmod "cosmossdk.io/errors"

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

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
Expand Down Expand Up @@ -341,9 +340,7 @@ func (k *Keeper) ChanUpgradeAck(
}

timeout := counterpartyUpgrade.Timeout
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano())

selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(k.HeaderService.HeaderInfo(ctx).Time.UnixNano())
if timeout.Elapsed(selfHeight, selfTimestamp) {
return types.NewUpgradeError(channel.UpgradeSequence, errorsmod.Wrap(timeout.ErrTimeoutElapsed(selfHeight, selfTimestamp), "counterparty upgrade timeout elapsed"))
}
Expand Down Expand Up @@ -464,9 +461,7 @@ func (k *Keeper) ChanUpgradeConfirm(
}

timeout := counterpartyUpgrade.Timeout
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
selfHeight, selfTimestamp := clienttypes.GetSelfHeight(sdkCtx), uint64(sdkCtx.BlockTime().UnixNano())

selfHeight, selfTimestamp := clienttypes.GetSelfHeight(ctx), uint64(k.HeaderService.HeaderInfo(ctx).Time.UnixNano())
if timeout.Elapsed(selfHeight, selfTimestamp) {
return types.NewUpgradeError(channel.UpgradeSequence, errorsmod.Wrap(timeout.ErrTimeoutElapsed(selfHeight, selfTimestamp), "counterparty upgrade timeout elapsed"))
}
Expand Down Expand Up @@ -885,8 +880,8 @@ func (k *Keeper) startFlushing(ctx context.Context, portID, channelID string, up
// getAbsoluteUpgradeTimeout returns the absolute timeout for the given upgrade.
func (k *Keeper) getAbsoluteUpgradeTimeout(ctx context.Context) types.Timeout {
upgradeTimeout := k.GetParams(ctx).UpgradeTimeout
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/7223
return types.NewTimeout(clienttypes.ZeroHeight(), uint64(sdkCtx.BlockTime().UnixNano())+upgradeTimeout.Timestamp)
blockTime := k.HeaderService.HeaderInfo(ctx).Time.UnixNano()
return types.NewTimeout(clienttypes.ZeroHeight(), uint64(blockTime)+upgradeTimeout.Timestamp)
}

// checkForUpgradeCompatibility checks performs stateful validation of self upgrade fields relative to counterparty upgrade.
Expand Down
39 changes: 21 additions & 18 deletions modules/core/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() {
)

suite.chainA.GetSimApp().IBCMockModule.IBCApp.OnChanUpgradeInit = func(ctx context.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, version string) (string, error) {
storeKey := suite.chainA.GetSimApp().GetKey(exported.ModuleName)
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
store := sdkCtx.KVStore(storeKey)
store.Set(ibcmock.TestKey, ibcmock.TestValue)
store := suite.chainA.GetSimApp().GetIBCKeeper().KVStoreService.OpenKVStore(ctx)
err := store.Set(ibcmock.TestKey, ibcmock.TestValue)
suite.Require().NoError(err)

sdkCtx.EventManager().EmitEvent(sdk.NewEvent(ibcmock.MockEventType))
eventService := suite.chainA.GetSimApp().GetIBCKeeper().EventService
err = eventService.EventManager(ctx).EmitKV(ibcmock.MockEventType)
suite.Require().NoError(err)
return ibcmock.UpgradeVersion, nil
}
},
Expand Down Expand Up @@ -1153,12 +1154,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() {
"ibc application does not commit state changes in callback",
func() {
suite.chainA.GetSimApp().IBCMockModule.IBCApp.OnChanUpgradeTry = func(ctx context.Context, portID, channelID string, order channeltypes.Order, connectionHops []string, counterpartyVersion string) (string, error) {
storeKey := suite.chainA.GetSimApp().GetKey(exported.ModuleName)
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
store := sdkCtx.KVStore(storeKey)
store.Set(ibcmock.TestKey, ibcmock.TestValue)
store := suite.chainA.GetSimApp().GetIBCKeeper().KVStoreService.OpenKVStore(ctx)
err := store.Set(ibcmock.TestKey, ibcmock.TestValue)
suite.Require().NoError(err)

sdkCtx.EventManager().EmitEvent(sdk.NewEvent(ibcmock.MockEventType))
eventService := suite.chainA.GetSimApp().GetIBCKeeper().EventService
err = eventService.EventManager(ctx).EmitKV(ibcmock.MockEventType)
suite.Require().NoError(err)
return ibcmock.UpgradeVersion, nil
}
},
Expand Down Expand Up @@ -1361,9 +1363,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() {
ctx context.Context, portID, channelID, counterpartyVersion string,
) error {
// set arbitrary value in store to mock application state changes
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
store := sdkCtx.KVStore(suite.chainA.GetSimApp().GetKey(exported.ModuleName))
store.Set([]byte("foo"), []byte("bar"))
store := suite.chainA.GetSimApp().GetIBCKeeper().KVStoreService.OpenKVStore(ctx)
err := store.Set(ibcmock.TestKey, ibcmock.TestValue)
suite.Require().NoError(err)
return fmt.Errorf("mock app callback failed")
}
},
Expand Down Expand Up @@ -1435,12 +1437,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() {
"ibc application does not commit state changes in callback",
func() {
suite.chainA.GetSimApp().IBCMockModule.IBCApp.OnChanUpgradeAck = func(ctx context.Context, portID, channelID, counterpartyVersion string) error {
storeKey := suite.chainA.GetSimApp().GetKey(exported.ModuleName)
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
store := sdkCtx.KVStore(storeKey)
store.Set(ibcmock.TestKey, ibcmock.TestValue)
store := suite.chainA.GetSimApp().GetIBCKeeper().KVStoreService.OpenKVStore(ctx)
err := store.Set(ibcmock.TestKey, ibcmock.TestValue)
suite.Require().NoError(err)

sdkCtx.EventManager().EmitEvent(sdk.NewEvent(ibcmock.MockEventType))
eventService := suite.chainA.GetSimApp().GetIBCKeeper().EventService
err = eventService.EventManager(ctx).EmitKV(ibcmock.MockEventType)
suite.Require().NoError(err)
return nil
}
},
Expand Down

0 comments on commit 4224996

Please sign in to comment.