Skip to content

Commit

Permalink
do cap logic and fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaSripal committed Aug 5, 2021
1 parent 7b51ebd commit 150211d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 50 deletions.
17 changes: 1 addition & 16 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package keeper

/*
import (
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/ibc-go/modules/apps/transfer/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
Expand All @@ -25,7 +23,6 @@ type Keeper struct {
scopedKeeper capabilitykeeper.ScopedKeeper
}

// NewKeeper creates a new 29-fee Keeper instance
func NewKeeper(
cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
Expand Down Expand Up @@ -59,7 +56,7 @@ func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
return k.scopedKeeper.ClaimCapability(ctx, cap, host.PortPath(portID))
}

// GetPort returns the portID for the transfer module. Used in ExportGenesis
Expand All @@ -73,15 +70,3 @@ func (k Keeper) SetPort(ctx sdk.Context, portID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.PortKey, []byte(portID))
}
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.scopedKeeper.AuthenticateCapability(ctx, cap, name)
}
// ClaimCapability allows the transfer module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
*/
111 changes: 77 additions & 34 deletions modules/apps/29-fee/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fee
import (
"context"
"encoding/json"
"fmt"
"math/rand"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -19,12 +18,17 @@ import (
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/ibc-go/modules/apps/transfer/client/cli"
"github.com/cosmos/ibc-go/modules/apps/transfer/keeper"
"github.com/cosmos/ibc-go/modules/apps/transfer/simulation"
"github.com/cosmos/ibc-go/modules/apps/transfer/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
"github.com/cosmos/ibc-go/modules/apps/29-fee/client/cli"
"github.com/cosmos/ibc-go/modules/apps/29-fee/keeper"
"github.com/cosmos/ibc-go/modules/apps/29-fee/types"

// "github.com/cosmos/ibc-go/modules/apps/29-fee/client/cli"
// "github.com/cosmos/ibc-go/modules/apps/29-fee/keeper"
// "github.com/cosmos/ibc-go/modules/apps/29-fee/simulation"
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/modules/core/exported"
)

Expand All @@ -47,30 +51,32 @@ func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}

// RegisterInterfaces registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
// types.RegisterInterfaces(registry)
}

// DefaultGenesis returns default genesis state as raw bytes for the ibc
// transfer module.
// 29-fee module.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesisState())
// return cdc.MustMarshalJSON(types.DefaultGenesisState())
return nil
}

// ValidateGenesis performs genesis state validation for the 29-fee module.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
var gs types.GenesisState
if err := cdc.UnmarshalJSON(bz, &gs); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
// var gs types.GenesisState
// if err := cdc.UnmarshalJSON(bz, &gs); err != nil {
// return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
// }

return gs.Validate()
// return gs.Validate()
return nil
}

// RegisterRESTRoutes implements AppModuleBasic interface
func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) {
}

// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the ibc-transfer module.
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the ibc-29-fee module.
func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
}
Expand All @@ -88,8 +94,9 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
// AppModule represents the AppModule for this module
type AppModule struct {
AppModuleBasic
keeper keeper.Keeper
app porttypes.IBCModule
keeper keeper.Keeper
scopedKeeper capabilitykeeper.ScopedKeeper
app porttypes.IBCModule
}

// NewAppModule creates a new 29-fee module
Expand Down Expand Up @@ -122,24 +129,25 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
// types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
// types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the ibc-transfer module. It returns
// InitGenesis performs genesis initialization for the ibc-29-fee module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
am.keeper.InitGenesis(ctx, genesisState)
// var genesisState types.GenesisState
// cdc.MustUnmarshalJSON(data, &genesisState)
// am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the ibc-transfer
// ExportGenesis returns the exported genesis state as raw bytes for the ibc-29-fee
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(gs)
// gs := am.keeper.ExportGenesis(ctx)
// return cdc.MustMarshalJSON(gs)
return nil
}

// ConsensusVersion implements AppModule/ConsensusVersion.
Expand All @@ -156,27 +164,28 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V

// AppModuleSimulation functions

// GenerateGenesisState creates a randomized GenState of the transfer module.
// GenerateGenesisState creates a randomized GenState of the 29-fee module.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
// simulation.RandomizedGenState(simState)
}

// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}

// RandomizedParams creates randomized ibc-transfer param changes for the simulator.
// RandomizedParams creates randomized ibc-29-fee param changes for the simulator.
func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
return simulation.ParamChanges(r)
// return simulation.ParamChanges(r)
return nil
}

// RegisterStoreDecoder registers a decoder for transfer module's types
// RegisterStoreDecoder registers a decoder for 29-fee module's types
func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper)
// sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper)
}

// WeightedOperations returns the all the transfer module operations with their respective weights.
// WeightedOperations returns the all the 29-fee module operations with their respective weights.
func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation {
return nil
}
Expand All @@ -196,9 +205,19 @@ func (am AppModule) OnChanOpenInit(
if feeVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected: %s, got: %s", types.Version, feeVersion)
}
// Claim channel capability passed back by IBC module
if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return err
}

appCap, err := am.scopedKeeper.NewCapability(ctx, types.AppCapabilityName(channelID, portID))
if err != nil {
return sdkerrors.Wrap(err, "could not create capability for underlying application")
}

// call underlying app's OnChanOpenInit callback with the appVersion
return am.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID,
chanCap, counterparty, appVersion)
appCap, counterparty, appVersion)
}

// OnChanOpenTry implements the IBCModule interface
Expand All @@ -222,9 +241,33 @@ func (am AppModule) OnChanOpenTry(
if cpFeeVersion != feeVersion {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "expected counterparty version: %s, got: %s", types.Version, cpFeeVersion)
}
var (
appCap *capabilitytypes.Capability
err error
ok bool
)
// Module may have already claimed capability in OnChanOpenInit in the case of crossing hellos
// (ie chainA and chainB both call ChanOpenInit before one of them calls ChanOpenTry)
// If module can already authenticate the capability then module already owns it so we don't need to claim
// Otherwise, module does not have channel capability and we must claim it from IBC
if !am.scopedKeeper.AuthenticateCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)) {
// Only claim channel capability passed back by IBC module if we do not already own it
if err := am.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return err
}
appCap, err = am.scopedKeeper.NewCapability(ctx, types.AppCapabilityName(channelID, portID))
if err != nil {
return sdkerrors.Wrap(err, "could not create capability for underlying app")
}
}
appCap, ok = am.scopedKeeper.GetCapability(ctx, types.AppCapabilityName(channelID, portID))
if !ok {
return sdkerrors.Wrap(capabilitytypes.ErrCapabilityNotFound,
"could not find app capability on OnChanOpenTry even after OnChanOpenInit called on this chain first (crossing hellos)")
}
// call underlying app's OnChanOpenTry callback with the app versions
return am.app.OnChanOpenTry(ctx, order, connectionHops, portID, channelID,
chanCap, counterparty, appVersion, cpAppVersion)
appCap, counterparty, appVersion, cpAppVersion)
}

// OnChanOpenAck implements the IBCModule interface
Expand Down
1 change: 1 addition & 0 deletions modules/apps/29-fee/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package fee_test
8 changes: 8 additions & 0 deletions modules/apps/29-fee/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "fmt"

const (
// ModuleName defines the 29-fee name
ModuleName = "ibcfee"
Expand All @@ -14,4 +16,10 @@ const (
QuerierRoute = ModuleName

Version = "fee29-1"

KeyAppCapability = "app_capabilities"
)

func AppCapabilityName(channelID, portID string) string {
return fmt.Sprintf("%s/%s/%s", KeyAppCapability, channelID, portID)
}

0 comments on commit 150211d

Please sign in to comment.