github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/05-port/keeper/keeper.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	capabilitykeeper "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/keeper"
     8  	capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types"
     9  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/05-port/types"
    10  	host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host"
    11  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    12  )
    13  
    14  // Keeper defines the IBC connection keeper
    15  type Keeper struct {
    16  	Router *types.Router
    17  
    18  	scopedKeeper *capabilitykeeper.ScopedKeeper
    19  }
    20  
    21  // NewKeeper creates a new IBC connection Keeper instance
    22  func NewKeeper(sck *capabilitykeeper.ScopedKeeper) Keeper {
    23  	return Keeper{
    24  		scopedKeeper: sck,
    25  	}
    26  }
    27  
    28  // Logger returns a module-specific logger.
    29  func (k Keeper) Logger(ctx sdk.Context) log.Logger {
    30  	return ctx.Logger().With("module", "x/"+host.ModuleName+"/"+types.SubModuleName)
    31  }
    32  
    33  // isBounded checks a given port ID is already bounded.
    34  func (k Keeper) isBound(ctx sdk.Context, portID string) bool {
    35  	_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
    36  	return ok
    37  }
    38  
    39  // BindPort binds to a port and returns the associated capability.
    40  // Ports must be bound statically when the chain starts in `app.go`.
    41  // The capability must then be passed to a module which will need to pass
    42  // it as an extra parameter when calling functions on the IBC module.
    43  func (k *Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability {
    44  	if err := host.PortIdentifierValidator(portID); err != nil {
    45  		panic(err.Error())
    46  	}
    47  
    48  	if k.isBound(ctx, portID) {
    49  		panic(fmt.Sprintf("port %s is already bound", portID))
    50  	}
    51  
    52  	key, err := k.scopedKeeper.NewCapability(ctx, host.PortPath(portID))
    53  	if err != nil {
    54  		panic(err.Error())
    55  	}
    56  
    57  	k.Logger(ctx).Info("port binded", "port", portID)
    58  	return key
    59  }
    60  
    61  // Authenticate authenticates a capability key against a port ID
    62  // by checking if the memory address of the capability was previously
    63  // generated and bound to the port (provided as a parameter) which the capability
    64  // is being authenticated against.
    65  func (k Keeper) Authenticate(ctx sdk.Context, key *capabilitytypes.Capability, portID string) bool {
    66  	if err := host.PortIdentifierValidator(portID); err != nil {
    67  		panic(err.Error())
    68  	}
    69  
    70  	return k.scopedKeeper.AuthenticateCapability(ctx, key, host.PortPath(portID))
    71  }
    72  
    73  // LookupModuleByPort will return the IBCModule along with the capability associated with a given portID
    74  func (k Keeper) LookupModuleByPort(ctx sdk.Context, portID string) (string, *capabilitytypes.Capability, error) {
    75  	modules, cap, err := k.scopedKeeper.LookupModules(ctx, host.PortPath(portID))
    76  	if err != nil {
    77  		return "", nil, err
    78  	}
    79  
    80  	return types.GetModuleOwner(modules), cap, nil
    81  }