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

chore: rewrite capability to not use sdk.Context #7089

Merged
merged 22 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"context"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -29,7 +31,7 @@ import (
// Prior to v6.x.x of ibc-go, the controller module was only functional as middleware, with authentication performed
// by the underlying application. For a full summary of the changes in v6.x.x, please see ADR009.
// This API will be removed in later releases.
func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, connectionID, owner, version string, ordering channeltypes.Order) error {
func (k Keeper) RegisterInterchainAccount(ctx context.Context, connectionID, owner, version string, ordering channeltypes.Order) error {
portID, err := icatypes.NewControllerPortID(owner)
if err != nil {
return err
Expand All @@ -56,7 +58,7 @@ func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, connectionID, owner,

// registerInterchainAccount registers an interchain account, returning the channel id of the MsgChannelOpenInitResponse
// and an error if one occurred.
func (k Keeper) registerInterchainAccount(ctx sdk.Context, connectionID, portID, version string, ordering channeltypes.Order) (string, error) {
func (k Keeper) registerInterchainAccount(ctx context.Context, connectionID, portID, version string, ordering channeltypes.Order) (string, error) {
// if there is an active channel for this portID / connectionID return an error
activeChannelID, found := k.GetOpenActiveChannel(ctx, connectionID, portID)
if found {
Expand All @@ -74,9 +76,10 @@ func (k Keeper) registerInterchainAccount(ctx sdk.Context, connectionID, portID,
}
}

sdkCtx := sdk.UnwrapSDKContext(ctx) //TODO: remove when Upgrading to 52
msg := channeltypes.NewMsgChannelOpenInit(portID, version, ordering, []string{connectionID}, icatypes.HostPortID, authtypes.NewModuleAddress(icatypes.ModuleName).String())
handler := k.msgRouter.Handler(msg)
res, err := handler(ctx, msg)
res, err := handler(sdkCtx, msg)
if err != nil {
return "", err
}
Expand All @@ -85,7 +88,7 @@ func (k Keeper) registerInterchainAccount(ctx sdk.Context, connectionID, portID,
k.Logger(ctx).Debug("emitting interchain account registration events", logging.SdkEventsToLogArguments(events))

// NOTE: The sdk msg handler creates a new EventManager, so events must be correctly propagated back to the current context
ctx.EventManager().EmitEvents(events)
sdkCtx.EventManager().EmitEvents(events)

firstMsgResponse := res.MsgResponses[0]
channelOpenInitResponse, ok := firstMsgResponse.GetCachedValue().(*channeltypes.MsgChannelOpenInitResponse)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package keeper

import (
"context"
"fmt"

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

genesistypes "github.com/cosmos/ibc-go/v9/modules/apps/27-interchain-accounts/genesis/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

// InitGenesis initializes the interchain accounts controller application state from a provided genesis state
func InitGenesis(ctx sdk.Context, keeper Keeper, state genesistypes.ControllerGenesisState) {
func InitGenesis(ctx context.Context, keeper Keeper, state genesistypes.ControllerGenesisState) {
for _, portID := range state.Ports {
keeper.setPort(ctx, portID)

Expand Down Expand Up @@ -44,7 +43,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, state genesistypes.ControllerGe
}

// ExportGenesis returns the interchain accounts controller exported genesis
func ExportGenesis(ctx sdk.Context, keeper Keeper) genesistypes.ControllerGenesisState {
func ExportGenesis(ctx context.Context, keeper Keeper) genesistypes.ControllerGenesisState {
return genesistypes.NewControllerGenesisState(
keeper.GetAllActiveChannels(ctx),
keeper.GetAllInterchainAccounts(ctx),
Expand Down
11 changes: 6 additions & 5 deletions modules/apps/29-fee/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// escrowPacketFee sends the packet fee to the 29-fee module account to hold in escrow
func (k Keeper) escrowPacketFee(ctx sdk.Context, packetID channeltypes.PacketId, packetFee types.PacketFee) error {
func (k Keeper) escrowPacketFee(ctx context.Context, packetID channeltypes.PacketId, packetFee types.PacketFee) error {
// check if the refund address is valid
refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress)
if err != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func (k Keeper) DistributePacketFeesOnAcknowledgement(ctx context.Context, forwa

// distributePacketFeeOnAcknowledgement pays the receive fee for a given packetID while refunding the timeout fee to the refund account associated with the Fee.
// If there was no forward relayer or the associated forward relayer address is blocked, the receive fee is refunded.
func (k Keeper) distributePacketFeeOnAcknowledgement(ctx sdk.Context, refundAddr, forwardRelayer, reverseRelayer sdk.AccAddress, packetFee types.PacketFee) {
func (k Keeper) distributePacketFeeOnAcknowledgement(ctx context.Context, refundAddr, forwardRelayer, reverseRelayer sdk.AccAddress, packetFee types.PacketFee) {
// distribute fee to valid forward relayer address otherwise refund the fee
if !forwardRelayer.Empty() && !k.bankKeeper.BlockedAddr(forwardRelayer) {
// distribute fee for forward relaying
Expand Down Expand Up @@ -140,7 +140,7 @@ func (k Keeper) DistributePacketFeesOnTimeout(ctx context.Context, timeoutRelaye
}

// distributePacketFeeOnTimeout pays the timeout fee to the timeout relayer and refunds the acknowledgement & receive fee.
func (k Keeper) distributePacketFeeOnTimeout(ctx sdk.Context, refundAddr, timeoutRelayer sdk.AccAddress, packetFee types.PacketFee) {
func (k Keeper) distributePacketFeeOnTimeout(ctx context.Context, refundAddr, timeoutRelayer sdk.AccAddress, packetFee types.PacketFee) {
// distribute fee for timeout relaying
k.distributeFee(ctx, timeoutRelayer, refundAddr, packetFee.Fee.TimeoutFee)

Expand All @@ -152,9 +152,10 @@ func (k Keeper) distributePacketFeeOnTimeout(ctx sdk.Context, refundAddr, timeou
// distributeFee will attempt to distribute the escrowed fee to the receiver address.
// If the distribution fails for any reason (such as the receiving address being blocked),
// the state changes will be discarded.
func (k Keeper) distributeFee(ctx sdk.Context, receiver, refundAccAddress sdk.AccAddress, fee sdk.Coins) {
func (k Keeper) distributeFee(ctx context.Context, receiver, refundAccAddress sdk.AccAddress, fee sdk.Coins) {
// cache context before trying to distribute fees
cacheCtx, writeFn := ctx.CacheContext()
sdkCtx := sdk.UnwrapSDKContext(ctx) //TODO: remove when Upgrading to 52
cacheCtx, writeFn := sdkCtx.CacheContext()

err := k.bankKeeper.SendCoinsFromModuleToAccount(cacheCtx, types.ModuleName, receiver, fee)
if err != nil {
Expand Down
22 changes: 13 additions & 9 deletions modules/apps/29-fee/keeper/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -11,7 +12,7 @@ import (

// emitIncentivizedPacketEvent emits an event containing information on the total amount of fees incentivizing
// a specific packet. It should be emitted on every fee escrowed for the given packetID.
func emitIncentivizedPacketEvent(ctx sdk.Context, packetID channeltypes.PacketId, packetFees types.PacketFees) {
func emitIncentivizedPacketEvent(ctx context.Context, packetID channeltypes.PacketId, packetFees types.PacketFees) {
var (
totalRecvFees sdk.Coins
totalAckFees sdk.Coins
Expand All @@ -26,8 +27,8 @@ func emitIncentivizedPacketEvent(ctx sdk.Context, packetID channeltypes.PacketId
totalTimeoutFees = totalTimeoutFees.Add(fee.Fee.TimeoutFee...)
}
}

ctx.EventManager().EmitEvents(sdk.Events{
sdkCtx := sdk.UnwrapSDKContext(ctx) //TODO: remove when Upgrading to 52
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeIncentivizedPacket,
sdk.NewAttribute(channeltypes.AttributeKeyPortID, packetID.PortId),
Expand All @@ -45,8 +46,9 @@ func emitIncentivizedPacketEvent(ctx sdk.Context, packetID channeltypes.PacketId
}

// emitRegisterPayeeEvent emits an event containing information of a registered payee for a relayer on a particular channel
func emitRegisterPayeeEvent(ctx sdk.Context, relayer, payee, channelID string) {
ctx.EventManager().EmitEvents(sdk.Events{
func emitRegisterPayeeEvent(ctx context.Context, relayer, payee, channelID string) {
sdkCtx := sdk.UnwrapSDKContext(ctx) //TODO: remove when Upgrading to 52
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterPayee,
sdk.NewAttribute(types.AttributeKeyRelayer, relayer),
Expand All @@ -61,8 +63,9 @@ func emitRegisterPayeeEvent(ctx sdk.Context, relayer, payee, channelID string) {
}

// emitRegisterCounterpartyPayeeEvent emits an event containing information of a registered counterparty payee for a relayer on a particular channel
func emitRegisterCounterpartyPayeeEvent(ctx sdk.Context, relayer, counterpartyPayee, channelID string) {
ctx.EventManager().EmitEvents(sdk.Events{
func emitRegisterCounterpartyPayeeEvent(ctx context.Context, relayer, counterpartyPayee, channelID string) {
sdkCtx := sdk.UnwrapSDKContext(ctx) //TODO: remove when Upgrading to 52
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRegisterCounterpartyPayee,
sdk.NewAttribute(types.AttributeKeyRelayer, relayer),
Expand All @@ -77,8 +80,9 @@ func emitRegisterCounterpartyPayeeEvent(ctx sdk.Context, relayer, counterpartyPa
}

// emitDistributeFeeEvent emits an event containing a distribution fee and receiver address
func emitDistributeFeeEvent(ctx sdk.Context, receiver string, fee sdk.Coins) {
ctx.EventManager().EmitEvents(sdk.Events{
func emitDistributeFeeEvent(ctx context.Context, receiver string, fee sdk.Coins) {
sdkCtx := sdk.UnwrapSDKContext(ctx) //TODO: remove when Upgrading to 52
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDistributeFee,
sdk.NewAttribute(types.AttributeKeyReceiver, receiver),
Expand Down
16 changes: 10 additions & 6 deletions modules/capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

Expand All @@ -23,7 +23,11 @@ import (
"github.com/cosmos/ibc-go/modules/capability/types"
)

const mockMemStoreKey = "memory:mock"
const (
mockMemStoreKey = "memory:mock"
bankModuleName = "bank"
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
stakingModuleName = "staking"
)

type CapabilityTestSuite struct {
testifysuite.Suite
Expand All @@ -47,7 +51,7 @@ func (suite *CapabilityTestSuite) SetupTest() {
suite.mockMemStoreKey = storetypes.NewMemoryStoreKey(mockMemStoreKey)

suite.ctx = suite.NewTestContext()
suite.keeper = keeper.NewKeeper(suite.cdc, suite.storeKey, suite.memStoreKey)
suite.keeper = keeper.NewKeeper(suite.cdc, runtime.NewKVStoreService(suite.storeKey), runtime.NewMemStoreService(suite.memStoreKey))
}

func (suite *CapabilityTestSuite) NewTestContext() sdk.Context {
Expand All @@ -70,19 +74,19 @@ func (suite *CapabilityTestSuite) NewTestContext() sdk.Context {
// BeginBlock is then called to populate the new in-memory store using the persisted state.
func (suite *CapabilityTestSuite) TestInitializeMemStore() {
// create a scoped keeper and instantiate a new capability to populate state
scopedKeeper := suite.keeper.ScopeToModule(banktypes.ModuleName)
scopedKeeper := suite.keeper.ScopeToModule(bankModuleName)

cap1, err := scopedKeeper.NewCapability(suite.ctx, "transfer")
suite.Require().NoError(err)
suite.Require().NotNil(cap1)

// mock statesync by creating a new keeper and module that shares persisted state
// but discards in-memory map by using a mock memstore key
newKeeper := keeper.NewKeeper(suite.cdc, suite.storeKey, suite.mockMemStoreKey)
newKeeper := keeper.NewKeeper(suite.cdc, runtime.NewKVStoreService(suite.storeKey), runtime.NewMemStoreService(suite.mockMemStoreKey))
newModule := capability.NewAppModule(suite.cdc, *newKeeper, true)

// reassign the scoped keeper, this will inherit the mock memstore key used above
scopedKeeper = newKeeper.ScopeToModule(banktypes.ModuleName)
scopedKeeper = newKeeper.ScopeToModule(bankModuleName)

// seal the new keeper and ensure the in-memory store is not initialized
newKeeper.Seal()
Expand Down
13 changes: 6 additions & 7 deletions modules/capability/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package capability_test

import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/cosmos-sdk/runtime"

"github.com/cosmos/ibc-go/modules/capability"
"github.com/cosmos/ibc-go/modules/capability/keeper"
Expand All @@ -13,8 +12,8 @@ func (suite *CapabilityTestSuite) TestGenesis() {
// InitGenesis must be called in order to set the initial index to 1.
capability.InitGenesis(suite.ctx, *suite.keeper, *types.DefaultGenesis())

sk1 := suite.keeper.ScopeToModule(banktypes.ModuleName)
sk2 := suite.keeper.ScopeToModule(stakingtypes.ModuleName)
sk1 := suite.keeper.ScopeToModule(bankModuleName)
sk2 := suite.keeper.ScopeToModule(stakingModuleName)

cap1, err := sk1.NewCapability(suite.ctx, "transfer")
suite.Require().NoError(err)
Expand All @@ -29,9 +28,9 @@ func (suite *CapabilityTestSuite) TestGenesis() {

genState := capability.ExportGenesis(suite.ctx, *suite.keeper)

newKeeper := keeper.NewKeeper(suite.cdc, suite.storeKey, suite.memStoreKey)
newSk1 := newKeeper.ScopeToModule(banktypes.ModuleName)
newSk2 := newKeeper.ScopeToModule(stakingtypes.ModuleName)
newKeeper := keeper.NewKeeper(suite.cdc, runtime.NewKVStoreService(suite.storeKey), runtime.NewMemStoreService(suite.memStoreKey))
newSk1 := newKeeper.ScopeToModule(bankModuleName)
newSk2 := newKeeper.ScopeToModule(stakingModuleName)
deliverCtx := suite.NewTestContext()

capability.InitGenesis(deliverCtx, *newKeeper, *genState)
Expand Down
8 changes: 6 additions & 2 deletions modules/capability/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ require (
cosmossdk.io/store v1.1.0
github.com/cometbft/cometbft v0.38.11
github.com/cosmos/cosmos-db v1.0.2
github.com/cosmos/cosmos-sdk v0.50.9
github.com/cosmos/gogoproto v1.6.0
github.com/cosmos/cosmos-sdk v0.50.10-0.20240808075341-156231be8aef
github.com/cosmos/gogoproto v1.7.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/stretchr/testify v1.9.0
sigs.k8s.io/yaml v1.4.0
Expand Down Expand Up @@ -78,6 +78,7 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand All @@ -89,6 +90,7 @@ require (
github.com/hashicorp/go-plugin v1.5.2 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
Expand All @@ -100,10 +102,12 @@ require (
github.com/klauspost/compress v1.17.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/linxGnu/grocksdb v1.8.14 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
Expand Down
Loading
Loading