From e34078346999045d7967e47d3037eca874316176 Mon Sep 17 00:00:00 2001 From: PoisonPhang <17688291+PoisonPhang@users.noreply.github.com> Date: Wed, 1 May 2024 11:23:49 -0500 Subject: [PATCH] chore(uniond): add differedack module --- uniond/x/differedack/keeper/genesis.go | 16 +++ uniond/x/differedack/keeper/keeper.go | 44 ++++++ uniond/x/differedack/keeper/msg_server.go | 35 +++++ uniond/x/differedack/keeper/params.go | 18 +++ uniond/x/differedack/module.go | 164 ++++++++++++++++++++++ uniond/x/differedack/types/codec.go | 52 +++++++ uniond/x/differedack/types/genesis.go | 9 ++ uniond/x/differedack/types/keys.go | 18 +++ uniond/x/differedack/types/msgs.go | 23 +++ uniond/x/differedack/types/params.go | 5 + 10 files changed, 384 insertions(+) create mode 100644 uniond/x/differedack/keeper/genesis.go create mode 100644 uniond/x/differedack/keeper/keeper.go create mode 100644 uniond/x/differedack/keeper/msg_server.go create mode 100644 uniond/x/differedack/keeper/params.go create mode 100644 uniond/x/differedack/module.go create mode 100644 uniond/x/differedack/types/codec.go create mode 100644 uniond/x/differedack/types/genesis.go create mode 100644 uniond/x/differedack/types/keys.go create mode 100644 uniond/x/differedack/types/msgs.go create mode 100644 uniond/x/differedack/types/params.go diff --git a/uniond/x/differedack/keeper/genesis.go b/uniond/x/differedack/keeper/genesis.go new file mode 100644 index 0000000000..1325d932c7 --- /dev/null +++ b/uniond/x/differedack/keeper/genesis.go @@ -0,0 +1,16 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "union/x/differedack/types" +) + +func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { +} + +func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { + return &types.GenesisState{ + Params: k.GetParams(ctx), + } +} diff --git a/uniond/x/differedack/keeper/keeper.go b/uniond/x/differedack/keeper/keeper.go new file mode 100644 index 0000000000..a825d9256a --- /dev/null +++ b/uniond/x/differedack/keeper/keeper.go @@ -0,0 +1,44 @@ +package keeper + +import ( + "fmt" + + "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" + channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" + porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types" + + "union/x/differedack/types" +) + +type ( + Keeper struct { + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + ics4Wrapper porttypes.ICS4Wrapper + } +) + +func NewKeeper( + cdc codec.BinaryCodec, + storeKey storetypes.StoreKey, + ics4Wrapper porttypes.ICS4Wrapper, +) Keeper { + return Keeper{ + cdc: cdc, + storeKey: storeKey, + ics4Wrapper: ics4Wrapper, + } +} + +func (k *Keeper) WriteDifferedAck(ctx sdk.Context, packet channeltypes.Packet, data transfertypes.FungibleTokenPacketData, differedPacketInfo *types.DifferedPacketInfo, ack channeltypes.Acknowledgement) error { + return nil +} + +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +} diff --git a/uniond/x/differedack/keeper/msg_server.go b/uniond/x/differedack/keeper/msg_server.go new file mode 100644 index 0000000000..ec237fe2cd --- /dev/null +++ b/uniond/x/differedack/keeper/msg_server.go @@ -0,0 +1,35 @@ +package keeper + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" + + "union/x/differedack/types" +) + +type msgServer struct { + Keeper +} + +func NewMsgServerImpl(keeper Keeper) types.MsgServer { + return msgServer{Keeper: keeper} +} + +var _ types.MsgServer = msgServer{} + +func (server msgServer) WriteDifferedAck(goCtx context.Context, req *types.MsgWriteDifferedAck) (*types.MsgWriteDifferedAckResponse, error) { + ctx := sdk.UnwrapSDKContext(goCtx) + + err := server.Keeper.WriteDifferedAck(ctx, *req.Packet, *req.Data, req.DifferedPacketInfo, *req.Ack) + + if err != nil { + return nil, err + } + + ctx.EventManager().EmitEvent(sdk.Events{ + sdk.NewEvent() + }) + + return &types.MsgWriteDifferedAckResponse{}, nil +} diff --git a/uniond/x/differedack/keeper/params.go b/uniond/x/differedack/keeper/params.go new file mode 100644 index 0000000000..45ff753231 --- /dev/null +++ b/uniond/x/differedack/keeper/params.go @@ -0,0 +1,18 @@ +package keeper + +import ( + "union/x/differedack/types" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// GetParams returns the total set params. +func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { + k.paramSpace.GetParamSet(ctx, ¶ms) + return params +} + +// SetParams sets the total set of params. +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { + k.paramSpace.SetParamSet(ctx, ¶ms) +} diff --git a/uniond/x/differedack/module.go b/uniond/x/differedack/module.go new file mode 100644 index 0000000000..af60953344 --- /dev/null +++ b/uniond/x/differedack/module.go @@ -0,0 +1,164 @@ +/* +The differedack module allows for an IBC ack to be sent after previously being differed/ignored. +*/ +package differedack + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/gorilla/mux" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + + abci "github.com/cometbft/cometbft/abci/types" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + + "union/x/differedack/keeper" + "union/x/differedack/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// ConsensusVersion defines the current x/differedack module consensus version. +const ConsensusVersion = 1 + +// AppModuleBasic implements the AppModuleBasic interface for the capability module. +type AppModuleBasic struct{} + +func NewAppModuleBasic() AppModuleBasic { + return AppModuleBasic{} +} + +// Name returns the x/differedack module's name. +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + types.RegisterLegacyAminoCodec(cdc) +} + +// RegisterInterfaces registers the module's interface types +func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) { + types.RegisterInterfaces(reg) +} + +// DefaultGenesis returns the x/differedack module's default genesis state. +func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { + return cdc.MustMarshalJSON(types.DefaultGenesis()) +} + +// ValidateGenesis performs genesis state validation for the x/differedack module. +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error { + var genState types.GenesisState + if err := cdc.UnmarshalJSON(bz, &genState); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) + } + + return genState.Validate() +} + +// RegisterRESTRoutes registers the capability module's REST service handlers. +func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) { +} + +// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. +func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { + types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) //nolint:errcheck +} + +// AppModule implements the AppModule interface for the capability module. +type AppModule struct { + AppModuleBasic + + keeper keeper.Keeper +} + +func NewAppModule( + keeper keeper.Keeper, +) AppModule { + return AppModule{ + AppModuleBasic: NewAppModuleBasic(), + keeper: keeper, + } +} + +// IsOnePerModuleType implements the depinject.OnePerModuleType interface. +func (am AppModule) IsOnePerModuleType() { // marker +} + +// IsAppModule implements the appmodule.AppModule interface. +func (am AppModule) IsAppModule() { // marker +} + +// Name returns the x/differedack module's name. +func (am AppModule) Name() string { + return am.AppModuleBasic.Name() +} + +// QuerierRoute returns the x/differedack module's query routing key. +func (AppModule) QuerierRoute() string { return types.QuerierRoute } + +// RegisterServices registers a GRPC query service to respond to the +// module-specific GRPC queries. +func (am AppModule) RegisterServices(cfg module.Configurator) { + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) + // types.RegisterQueryServer(cfg.QueryServer(), am.keeper) + + // If migration required + // m := keeper.NewMigrator(am.keeper, am.legacySubspace) +} + +// RegisterInvariants registers the x/differedack module's invariants. +func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} + +// InitGenesis performs the x/differedack module's genesis initialization. It +// returns no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate { + var genState types.GenesisState + cdc.MustUnmarshalJSON(gs, &genState) + + am.keeper.InitGenesis(ctx, genState) + + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the x/differedack module's exported genesis state as raw +// JSON bytes. +func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { + genState := am.keeper.ExportGenesis(ctx) + return cdc.MustMarshalJSON(genState) +} + +// ConsensusVersion implements ConsensusVersion. +func (AppModule) ConsensusVersion() uint64 { + return ConsensusVersion +} + +func (AppModule) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// GenerateGenesisState creates a randomized GenState of the bank module. +func (am AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalMsg { + return nil +} + +// RegisterStoreDecoder registers a decoder for supply module's types +func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) { +} + +// WeightedOperations returns the all the gov module operations with their respective weights. +func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { + return simulation.WeightedOperations(&simState, am.keeper, am.accountKeeper, am.bankKeeper) +} diff --git a/uniond/x/differedack/types/codec.go b/uniond/x/differedack/types/codec.go new file mode 100644 index 0000000000..35c369b1e8 --- /dev/null +++ b/uniond/x/differedack/types/codec.go @@ -0,0 +1,52 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/msgservice" +) + +var ( + amino = codec.NewLegacyAmino() + + // ModuleCdc references the global erc20 module codec. Note, the codec should + // ONLY be used in certain instances of tests and for JSON encoding. + // + // The actual codec used for serialization should be provided to modules/erc20 and + // defined at the application level. + ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) + + // AminoCdc is a amino codec created to support amino JSON compatible msgs. + AminoCdc = codec.NewLegacyAmino() +) + +const ( + differedAckAck = "differedack/ack" +) + +func init() { + RegisterLegacyAminoCodec(amino) + + sdk.RegisterLegacyAminoCodec(amino) + + // Register all Amino interfaces and concrete types on the authz Amino codec + // so that this can later be used to properly serialize MsgGrant and MsgExec + // instances. + legacyAmino := codec.NewLegacyAmino() + RegisterLegacyAminoCodec(legacyAmino) + + amino.Seal() +} + +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + registry.RegisterImplementations( + (*sdk.Msg)(nil), + &MsgAck{}, + ) + msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) +} + +func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterConcrete(&MsgAck{}, differedAckAck, nil) +} diff --git a/uniond/x/differedack/types/genesis.go b/uniond/x/differedack/types/genesis.go new file mode 100644 index 0000000000..1f9f752b0a --- /dev/null +++ b/uniond/x/differedack/types/genesis.go @@ -0,0 +1,9 @@ +package types + +func DefaultGenesis() *GenesisState { + return &GenesisState{} +} + +func (gs GenesisState) Validate() error { + return nil +} diff --git a/uniond/x/differedack/types/keys.go b/uniond/x/differedack/types/keys.go new file mode 100644 index 0000000000..14b7ee5cab --- /dev/null +++ b/uniond/x/differedack/types/keys.go @@ -0,0 +1,18 @@ +package types + +const ( + // ModuleName defines the module name + ModuleName = "differedack" + + // StoreKey defines the primary module store key + StoreKey = ModuleName + + // RouterKey is the message route for slashing + RouterKey = ModuleName + + // QuerierRoute defines the module's query routing key + QuerierRoute = ModuleName + + // MemStoreKey defines the in-memory store key + MemStoreKey = "mem_differedack" +) diff --git a/uniond/x/differedack/types/msgs.go b/uniond/x/differedack/types/msgs.go new file mode 100644 index 0000000000..a1c3d3e876 --- /dev/null +++ b/uniond/x/differedack/types/msgs.go @@ -0,0 +1,23 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" + channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" +) + +const ( + TypeMsgWriteDifferedAck = "write_differed_ack" +) + +var _ sdk.Msg = &MsgWriteDifferedAck{} + +func NewMsgWriteDifferedAck(packet channeltypes.Packet, data transfertypes.FungibleTokenPacketData, info DifferedPacketInfo, ack channeltypes.Acknowledgement) *MsgWriteDifferedAck { + return &MsgWriteDifferedAck{ + Packet: &packet, + Data: &data, + DifferedPacketInfo: &info, + Ack: &ack, + } +} diff --git a/uniond/x/differedack/types/params.go b/uniond/x/differedack/types/params.go new file mode 100644 index 0000000000..8e3ba47570 --- /dev/null +++ b/uniond/x/differedack/types/params.go @@ -0,0 +1,5 @@ +package types + +func NewParams() Params { + return Params{} +}