Skip to content

Commit

Permalink
feat(uniond): diffack module
Browse files Browse the repository at this point in the history
  • Loading branch information
PoisonPhang authored and aeryz committed Jun 5, 2024
1 parent eb8a4b8 commit 5f3ed71
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 7 deletions.
27 changes: 23 additions & 4 deletions uniond/x/differedack/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package keeper
import (
"fmt"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channelkeeper "github.com/cosmos/ibc-go/v8/modules/core/04-channel/keeper"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"

Expand All @@ -17,9 +20,10 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
ics4Wrapper porttypes.ICS4Wrapper
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
ics4Wrapper porttypes.ICS4Wrapper
channelKeeper channelkeeper.Keeper
}
)

Expand All @@ -36,7 +40,22 @@ func NewKeeper(
}

func (k *Keeper) WriteDifferedAck(ctx sdk.Context, packet channeltypes.Packet, data transfertypes.FungibleTokenPacketData, differedPacketInfo *types.DifferedPacketInfo, ack channeltypes.Acknowledgement) error {
return nil
_, chanCap, err := k.channelKeeper.LookupModuleByChannel(ctx, differedPacketInfo.RefundPortId, differedPacketInfo.RefundChannelId)

if err != nil {
return errorsmod.Wrap(err, "could not retrieve module from port-id")
}

return k.ics4Wrapper.WriteAcknowledgement(ctx, chanCap, channeltypes.Packet{
Sequence: differedPacketInfo.RefundSequence,
SourcePort: differedPacketInfo.PacketSrcPortId,
SourceChannel: differedPacketInfo.PacketSrcChannelId,
DestinationPort: differedPacketInfo.RefundPortId,
DestinationChannel: differedPacketInfo.RefundChannelId,
Data: differedPacketInfo.PacketData,
TimeoutHeight: clienttypes.MustParseHeight(differedPacketInfo.PacketTimeoutHeight),
TimeoutTimestamp: differedPacketInfo.PacketTimeoutTimestamp,
}, ack)
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
Expand Down
6 changes: 4 additions & 2 deletions uniond/x/differedack/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ func (server msgServer) WriteDifferedAck(goCtx context.Context, req *types.MsgWr
return nil, err
}

ctx.EventManager().EmitEvent(sdk.Events{
sdk.NewEvent()
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeMsgWriteDifferedAck,
),
})

return &types.MsgWriteDifferedAckResponse{}, nil
Expand Down
5 changes: 4 additions & 1 deletion uniond/x/differedack/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

"union/x/differedack/keeper"
"union/x/differedack/simulation"
"union/x/differedack/types"
)

Expand Down Expand Up @@ -81,7 +82,9 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r
type AppModule struct {
AppModuleBasic

keeper keeper.Keeper
keeper keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
}

func NewAppModule(
Expand Down
18 changes: 18 additions & 0 deletions uniond/x/differedack/simulation/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package simulation

import (
"github.com/cosmos/cosmos-sdk/types/module"

"union/x/differedack/types"
)

func RandomizedGenState(simstate *module.SimulationState) {
tfGenesis := types.DefaultGenesis()

_, err := simstate.Cdc.MarshalJSON(tfGenesis)
if err != nil {
panic(err)
}

simstate.GenState[types.ModuleName] = simstate.Cdc.MustMarshalJSON(tfGenesis)
}
115 changes: 115 additions & 0 deletions uniond/x/differedack/simulation/operations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package simulation

import (
"context"
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"

appparams "union/app/params"
"union/x/differedack/types"
)

// Simulation operation weights constants
//
//nolint:gosec
const (
OpWeightMsgWriteDifferedAck = "op_weight_write_differed_ack"
)

type AccountKeeper interface {
GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
}

type BankKeeper interface {
simulation.BankKeeper
GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins
GetBalance(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin
}

type DifferedAckKeeper interface {
GetParams(ctx sdk.Context) (params types.Params)
}

func WeightedOperations(
simstate *module.SimulationState,
daKeeper DifferedAckKeeper,
ak AccountKeeper,
bk BankKeeper,
) simulation.WeightedOperations {
var (
weightMsgWriteDifferedAck int
)

simstate.AppParams.GetOrGenerate(OpWeightMsgWriteDifferedAck, &weightMsgWriteDifferedAck, nil,
func(_ *rand.Rand) {
weightMsgWriteDifferedAck = appparams.DefaultWeightMsgCreateDenom
},
)
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgWriteDifferedAck,
SimulateMsgWriteDifferedAck(
daKeeper,
ak,
bk,
),
),
}
}

func SimulateMsgWriteDifferedAck(
keeper DifferedAckKeeper,
ak AccountKeeper,
bk BankKeeper,
) simtypes.Operation {
return func(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
accs []simtypes.Account,
chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
// TODO: Simulate ack packet
msg := types.MsgWriteDifferedAck{}

account, _ := simtypes.RandomAcc(r, accs)

txCtx := BuildOperationInput(r, app, ctx, &msg, account, ak, bk, nil)

return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}

// BuildOperationInput helper to build object
func BuildOperationInput(
r *rand.Rand,
app *baseapp.BaseApp,
ctx sdk.Context,
msg interface {
sdk.Msg
},
simAccount simtypes.Account,
ak AccountKeeper,
bk BankKeeper,
deposit sdk.Coins,
) simulation.OperationInput {
return simulation.OperationInput{
R: r,
App: app,
TxGen: appparams.MakeEncodingConfig().TxConfig,
Cdc: nil,
Msg: msg,
Context: ctx,
SimAccount: simAccount,
AccountKeeper: ak,
Bankkeeper: bk,
ModuleName: types.ModuleName,
CoinsSpentInMsg: deposit,
}
}
39 changes: 39 additions & 0 deletions uniond/x/differedack/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types

import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type BankKeeper interface {
// Methods imported from bank should be defined here
GetDenomMetaData(ctx context.Context, denom string) (banktypes.Metadata, bool)
SetDenomMetaData(ctx context.Context, denomMetaData banktypes.Metadata)

HasSupply(ctx context.Context, denom string) bool
IterateTotalSupply(ctx context.Context, cb func(sdk.Coin) bool)

SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error
BurnCoins(ctx context.Context, moduleName string, amt sdk.Coins) error

SendCoins(ctx context.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
HasBalance(ctx context.Context, addr sdk.AccAddress, amt sdk.Coin) bool
GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins
SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins
GetBalance(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin

BlockedAddr(addr sdk.AccAddress) bool
}

type AccountKeeper interface {
GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
}

// CommunityPoolKeeper defines the contract needed to be fulfilled for community pool interactions.
type CommunityPoolKeeper interface {
FundCommunityPool(ctx context.Context, amount sdk.Coins, sender sdk.AccAddress) error
}
2 changes: 2 additions & 0 deletions uniond/x/differedack/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ func NewMsgWriteDifferedAck(packet channeltypes.Packet, data transfertypes.Fungi
Ack: &ack,
}
}

func (m MsgWriteDifferedAck) Type() string { return TypeMsgWriteDifferedAck }

0 comments on commit 5f3ed71

Please sign in to comment.