Skip to content

Commit

Permalink
chore: make all keepers use address codecs (#7896)
Browse files Browse the repository at this point in the history
* all keepers now use addr codec

* linter
  • Loading branch information
technicallyty authored Jan 29, 2025
1 parent c0365a3 commit 6a1986c
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 27 deletions.
4 changes: 2 additions & 2 deletions modules/apps/29-fee/ibc_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (im IBCMiddleware) OnAcknowledgementPacket(
payee = relayer.String()
}

payeeAddr, err := sdk.AccAddressFromBech32(payee)
payeeAddr, err := im.keeper.AddrCodec.StringToBytes(payee)
if err != nil {
return errorsmod.Wrapf(err, "failed to create sdk.Address from payee: %s", payee)
}
Expand Down Expand Up @@ -329,7 +329,7 @@ func (im IBCMiddleware) OnTimeoutPacket(
payee = relayer.String()
}

payeeAddr, err := sdk.AccAddressFromBech32(payee)
payeeAddr, err := im.keeper.AddrCodec.StringToBytes(payee)
if err != nil {
return errorsmod.Wrapf(err, "failed to create sdk.Address from payee: %s", payee)
}
Expand Down
10 changes: 5 additions & 5 deletions modules/apps/29-fee/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// escrowPacketFee sends the packet fee to the 29-fee module account to hold in escrow
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)
refundAddr, err := k.AddrCodec.StringToBytes(packetFee.RefundAddress)
if err != nil {
return err
}
Expand Down Expand Up @@ -52,7 +52,7 @@ func (k Keeper) DistributePacketFeesOnAcknowledgement(ctx context.Context, forwa
// if the escrow account has insufficient balance then we want to avoid partially distributing fees
if err := k.BranchService.Execute(ctx, func(ctx context.Context) error {
// forward relayer address will be empty if conversion fails
forwardAddr, _ := sdk.AccAddressFromBech32(forwardRelayer)
forwardAddr, _ := k.AddrCodec.StringToBytes(forwardRelayer)

for _, packetFee := range packetFees {
if !k.EscrowAccountHasBalance(ctx, packetFee.Fee.Total()) {
Expand All @@ -61,7 +61,7 @@ func (k Keeper) DistributePacketFeesOnAcknowledgement(ctx context.Context, forwa
}

// check if refundAcc address works
refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress)
refundAddr, err := k.AddrCodec.StringToBytes(packetFee.RefundAddress)
if err != nil {
panic(fmt.Errorf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress))
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func (k Keeper) DistributePacketFeesOnTimeout(ctx context.Context, timeoutRelaye
}

// check if refundAcc address works
refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress)
refundAddr, err := k.AddrCodec.StringToBytes(packetFee.RefundAddress)
if err != nil {
panic(fmt.Errorf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress))
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (k Keeper) RefundFeesOnChannelClosure(ctx context.Context, portID, channelI
return ibcerrors.ErrInsufficientFunds
}

refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress)
refundAddr, err := k.AddrCodec.StringToBytes(packetFee.RefundAddress)
if err != nil {
unRefundedFees = append(unRefundedFees, packetFee)
continue
Expand Down
19 changes: 11 additions & 8 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
storetypes "cosmossdk.io/store/types"

Expand All @@ -24,7 +25,8 @@ var _ types.ChannelKeeper = (*Keeper)(nil)
type Keeper struct {
appmodule.Environment

cdc codec.BinaryCodec
cdc codec.BinaryCodec
AddrCodec address.Codec

authKeeper types.AuthKeeper
ics4Wrapper porttypes.ICS4Wrapper
Expand All @@ -34,12 +36,13 @@ type Keeper struct {

// NewKeeper creates a new 29-fee Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, env appmodule.Environment,
cdc codec.BinaryCodec, addrCdc address.Codec, env appmodule.Environment,
ics4Wrapper porttypes.ICS4Wrapper, channelKeeper types.ChannelKeeper,
authKeeper types.AuthKeeper, bankKeeper types.BankKeeper,
) Keeper {
return Keeper{
cdc: cdc,
AddrCodec: addrCdc,
Environment: env,
ics4Wrapper: ics4Wrapper,
channelKeeper: channelKeeper,
Expand Down Expand Up @@ -220,17 +223,17 @@ func (k Keeper) GetAllPayees(ctx context.Context) []types.RegisteredPayee {

// SetCounterpartyPayeeAddress maps the destination chain counterparty payee address to the source relayer address
// The receiving chain must store the mapping from: address -> counterpartyPayeeAddress for the given channel
func (k Keeper) SetCounterpartyPayeeAddress(ctx context.Context, address, counterpartyAddress, channelID string) {
func (k Keeper) SetCounterpartyPayeeAddress(ctx context.Context, addr, counterpartyAddress, channelID string) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(types.KeyCounterpartyPayee(address, channelID), []byte(counterpartyAddress)); err != nil {
if err := store.Set(types.KeyCounterpartyPayee(addr, channelID), []byte(counterpartyAddress)); err != nil {
panic(err)
}
}

// GetCounterpartyPayeeAddress gets the counterparty payee address given a destination relayer address
func (k Keeper) GetCounterpartyPayeeAddress(ctx context.Context, address, channelID string) (string, bool) {
func (k Keeper) GetCounterpartyPayeeAddress(ctx context.Context, relayerAddr, channelID string) (string, bool) {
store := k.KVStoreService.OpenKVStore(ctx)
key := types.KeyCounterpartyPayee(address, channelID)
key := types.KeyCounterpartyPayee(relayerAddr, channelID)

addr, err := store.Get(key)
if err != nil {
Expand Down Expand Up @@ -269,9 +272,9 @@ func (k Keeper) GetAllCounterpartyPayees(ctx context.Context) []types.Registered
}

// SetRelayerAddressForAsyncAck sets the forward relayer address during OnRecvPacket in case of async acknowledgement
func (k Keeper) SetRelayerAddressForAsyncAck(ctx context.Context, packetID channeltypes.PacketId, address string) {
func (k Keeper) SetRelayerAddressForAsyncAck(ctx context.Context, packetID channeltypes.PacketId, addr string) {
store := k.KVStoreService.OpenKVStore(ctx)
if err := store.Set(types.KeyRelayerAddressForAsyncAck(packetID), []byte(address)); err != nil {
if err := store.Set(types.KeyRelayerAddressForAsyncAck(packetID), []byte(addr)); err != nil {
panic(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/apps/29-fee/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m Migrator) Migrate1to2(ctx context.Context) error {
for _, packetFee := range feesInEscrow.PacketFees {
refundCoins := legacyTotal(packetFee.Fee).Sub(packetFee.Fee.Total()...)

refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress)
refundAddr, err := m.keeper.AddrCodec.StringToBytes(packetFee.RefundAddress)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions modules/apps/29-fee/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

errorsmod "cosmossdk.io/errors"

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

"github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
Expand All @@ -20,7 +18,7 @@ var _ types.MsgServer = (*Keeper)(nil)
// the source chain from which packets originate as this is where fee distribution takes place. This function may be
// called more than once by a relayer, in which case, the latest payee is always used.
func (k Keeper) RegisterPayee(ctx context.Context, msg *types.MsgRegisterPayee) (*types.MsgRegisterPayeeResponse, error) {
payee, err := sdk.AccAddressFromBech32(msg.Payee)
payee, err := k.AddrCodec.StringToBytes(msg.Payee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,7 +85,7 @@ func (k Keeper) PayPacketFee(ctx context.Context, msg *types.MsgPayPacketFee) (*
return nil, types.ErrFeeModuleLocked
}

refundAcc, err := sdk.AccAddressFromBech32(msg.Signer)
refundAcc, err := k.AddrCodec.StringToBytes(msg.Signer)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +128,7 @@ func (k Keeper) PayPacketFeeAsync(ctx context.Context, msg *types.MsgPayPacketFe
return nil, types.ErrFeeModuleLocked
}

refundAcc, err := sdk.AccAddressFromBech32(msg.PacketFee.RefundAddress)
refundAcc, err := k.AddrCodec.StringToBytes(msg.PacketFee.RefundAddress)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions modules/apps/callbacks/testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ func NewSimApp(

app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")),
app.GetSubspace(ibcexported.ModuleName),
app.UpgradeKeeper,
Expand All @@ -431,6 +432,7 @@ func NewSimApp(
// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcfeetypes.StoreKey]), logger.With(log.ModuleKey, fmt.Sprintf("x/%s-%s", ibcexported.ModuleName, ibcfeetypes.ModuleName))),
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
Expand Down
7 changes: 5 additions & 2 deletions modules/core/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strings"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -27,14 +28,15 @@ type Keeper struct {
ChannelKeeper *channelkeeper.Keeper
PortKeeper *portkeeper.Keeper

cdc codec.BinaryCodec
cdc codec.BinaryCodec
AddrCdc address.Codec

authority string
}

// NewKeeper creates a new ibc Keeper
func NewKeeper(
cdc codec.BinaryCodec, env appmodule.Environment, paramSpace types.ParamSubspace,
cdc codec.BinaryCodec, addrCdc address.Codec, env appmodule.Environment, paramSpace types.ParamSubspace,
upgradeKeeper clienttypes.UpgradeKeeper, authority string,
) *Keeper {
// panic if any of the keepers passed in is empty
Expand All @@ -54,6 +56,7 @@ func NewKeeper(
return &Keeper{
Environment: env,
cdc: cdc,
AddrCdc: addrCdc,
ClientKeeper: clientKeeper,
ConnectionKeeper: connectionKeeper,
ChannelKeeper: channelKeeper,
Expand Down
3 changes: 3 additions & 0 deletions modules/core/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"cosmossdk.io/log"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"

"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/runtime"

clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
Expand Down Expand Up @@ -76,6 +77,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
newIBCKeeperFn = func() {
ibckeeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
address.NewBech32Codec(suite.chainA.Bech32Prefix),
runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), log.NewNopLogger()),
suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName),
upgradeKeeper,
Expand All @@ -96,6 +98,7 @@ func (suite *KeeperTestSuite) TestNewKeeper() {
newIBCKeeperFn = func() {
ibckeeper.NewKeeper(
suite.chainA.GetSimApp().AppCodec(),
address.NewBech32Codec(suite.chainA.Bech32Prefix),
runtime.NewEnvironment(runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), log.NewNopLogger()),
suite.chainA.GetSimApp().GetSubspace(ibcexported.ModuleName),
upgradeKeeper,
Expand Down
8 changes: 4 additions & 4 deletions modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (k *Keeper) ChannelCloseConfirm(ctx context.Context, msg *channeltypes.MsgC
func (k *Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPacket) (*channeltypes.MsgRecvPacketResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

relayer, err := sdk.AccAddressFromBech32(msg.Signer)
relayer, err := k.AddrCdc.StringToBytes(msg.Signer)
if err != nil {
k.Logger.Error("receive packet failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer"))
return nil, errorsmod.Wrap(err, "Invalid address for msg Signer")
Expand Down Expand Up @@ -424,7 +424,7 @@ func (k *Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPack

// Timeout defines a rpc handler method for MsgTimeout.
func (k *Keeper) Timeout(ctx context.Context, msg *channeltypes.MsgTimeout) (*channeltypes.MsgTimeoutResponse, error) {
relayer, err := sdk.AccAddressFromBech32(msg.Signer)
relayer, err := k.AddrCdc.StringToBytes(msg.Signer)
if err != nil {
k.Logger.Error("timeout failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer"))
return nil, errorsmod.Wrap(err, "Invalid address for msg Signer")
Expand Down Expand Up @@ -482,7 +482,7 @@ func (k *Keeper) Timeout(ctx context.Context, msg *channeltypes.MsgTimeout) (*ch

// TimeoutOnClose defines a rpc handler method for MsgTimeoutOnClose.
func (k *Keeper) TimeoutOnClose(ctx context.Context, msg *channeltypes.MsgTimeoutOnClose) (*channeltypes.MsgTimeoutOnCloseResponse, error) {
relayer, err := sdk.AccAddressFromBech32(msg.Signer)
relayer, err := k.AddrCdc.StringToBytes(msg.Signer)
if err != nil {
k.Logger.Error("timeout on close failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer"))
return nil, errorsmod.Wrap(err, "Invalid address for msg Signer")
Expand Down Expand Up @@ -541,7 +541,7 @@ func (k *Keeper) TimeoutOnClose(ctx context.Context, msg *channeltypes.MsgTimeou

// Acknowledgement defines a rpc handler method for MsgAcknowledgement.
func (k *Keeper) Acknowledgement(ctx context.Context, msg *channeltypes.MsgAcknowledgement) (*channeltypes.MsgAcknowledgementResponse, error) {
relayer, err := sdk.AccAddressFromBech32(msg.Signer)
relayer, err := k.AddrCdc.StringToBytes(msg.Signer)
if err != nil {
k.Logger.Error("acknowledgement failed", "error", errorsmod.Wrap(err, "Invalid address for msg Signer"))
return nil, errorsmod.Wrap(err, "Invalid address for msg Signer")
Expand Down
2 changes: 2 additions & 0 deletions modules/light-clients/08-wasm/testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func NewSimApp(

app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")),
app.GetSubspace(ibcexported.ModuleName),
app.UpgradeKeeper,
Expand Down Expand Up @@ -559,6 +560,7 @@ func NewSimApp(
// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcfeetypes.StoreKey]), logger.With(log.ModuleKey, fmt.Sprintf("x/%s-%s", ibcexported.ModuleName, ibcfeetypes.ModuleName))),
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
Expand Down
2 changes: 2 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ func NewSimApp(

app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")),
app.GetSubspace(ibcexported.ModuleName),
app.UpgradeKeeper,
Expand Down Expand Up @@ -541,6 +542,7 @@ func NewSimApp(
// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcfeetypes.StoreKey]), logger.With(log.ModuleKey, fmt.Sprintf("x/%s-%s", ibcexported.ModuleName, ibcfeetypes.ModuleName))),
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
Expand Down
2 changes: 2 additions & 0 deletions testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ func NewSimApp(

app.IBCKeeper = ibckeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcexported.StoreKey]), logger.With(log.ModuleKey, "x/ibc")),
app.GetSubspace(ibcexported.ModuleName),
app.UpgradeKeeper,
Expand Down Expand Up @@ -511,6 +512,7 @@ func NewSimApp(
// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec,
signingCtx.AddressCodec(),
runtime.NewEnvironment(runtime.NewKVStoreService(keys[ibcfeetypes.StoreKey]), logger.With(log.ModuleKey, fmt.Sprintf("x/%s-%s", ibcexported.ModuleName, ibcfeetypes.ModuleName))),
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
Expand Down

0 comments on commit 6a1986c

Please sign in to comment.