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