-
Notifications
You must be signed in to change notification settings - Fork 579
/
keeper.go
129 lines (106 loc) · 3.82 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package keeper
import (
"errors"
"fmt"
"reflect"
"strings"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
clientkeeper "github.com/cosmos/ibc-go/v8/modules/core/02-client/keeper"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
connectionkeeper "github.com/cosmos/ibc-go/v8/modules/core/03-connection/keeper"
channelkeeper "github.com/cosmos/ibc-go/v8/modules/core/04-channel/keeper"
portkeeper "github.com/cosmos/ibc-go/v8/modules/core/05-port/keeper"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
"github.com/cosmos/ibc-go/v8/modules/core/types"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
)
var _ types.QueryServer = (*Keeper)(nil)
// Keeper defines each ICS keeper for IBC
type Keeper struct {
// implements gRPC QueryServer interface
types.QueryServer
cdc codec.BinaryCodec
ClientKeeper clientkeeper.Keeper
ConnectionKeeper connectionkeeper.Keeper
ChannelKeeper channelkeeper.Keeper
PortKeeper *portkeeper.Keeper
Router *porttypes.Router
authority string
}
// NewKeeper creates a new ibc Keeper
func NewKeeper(
cdc codec.BinaryCodec, key storetypes.StoreKey, paramSpace types.ParamSubspace,
stakingKeeper clienttypes.StakingKeeper, upgradeKeeper clienttypes.UpgradeKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper, authority string,
) *Keeper {
// panic if any of the keepers passed in is empty
if isEmpty(stakingKeeper) {
panic(errors.New("cannot initialize IBC keeper: empty staking keeper"))
}
if isEmpty(upgradeKeeper) {
panic(errors.New("cannot initialize IBC keeper: empty upgrade keeper"))
}
if reflect.DeepEqual(capabilitykeeper.ScopedKeeper{}, scopedKeeper) {
panic(errors.New("cannot initialize IBC keeper: empty scoped keeper"))
}
if strings.TrimSpace(authority) == "" {
panic(errors.New("authority must be non-empty"))
}
clientKeeper := clientkeeper.NewKeeper(cdc, key, paramSpace, stakingKeeper, upgradeKeeper)
connectionKeeper := connectionkeeper.NewKeeper(cdc, key, paramSpace, clientKeeper)
portKeeper := portkeeper.NewKeeper(scopedKeeper)
channelKeeper := channelkeeper.NewKeeper(cdc, key, clientKeeper, connectionKeeper, &portKeeper, scopedKeeper)
keeper := &Keeper{
cdc: cdc,
ClientKeeper: clientKeeper,
ConnectionKeeper: connectionKeeper,
ChannelKeeper: channelKeeper,
PortKeeper: &portKeeper,
authority: authority,
}
keeper.SetConsensusHost(ibctm.NewConsensusHost(stakingKeeper))
return keeper
}
// Codec returns the IBC module codec.
func (k Keeper) Codec() codec.BinaryCodec {
return k.cdc
}
// SetConsensusHost sets a custom ConsensusHost for self client state and consensus state validation.
func (k *Keeper) SetConsensusHost(consensusHost clienttypes.ConsensusHost) {
if consensusHost == nil {
panic(fmt.Errorf("cannot set a nil self consensus host"))
}
k.ClientKeeper.SetConsensusHost(consensusHost)
k.ConnectionKeeper.SetConsensusHost(consensusHost)
}
// SetRouter sets the Router in IBC Keeper and seals it. The method panics if
// there is an existing router that's already sealed.
func (k *Keeper) SetRouter(rtr *porttypes.Router) {
if k.Router != nil && k.Router.Sealed() {
panic(errors.New("cannot reset a sealed router"))
}
k.PortKeeper.Router = rtr
k.Router = rtr
k.Router.Seal()
}
// GetAuthority returns the ibc module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}
// isEmpty checks if the interface is an empty struct or a pointer pointing
// to an empty struct
func isEmpty(keeper interface{}) bool {
switch reflect.TypeOf(keeper).Kind() {
case reflect.Ptr:
if reflect.ValueOf(keeper).Elem().IsZero() {
return true
}
default:
if reflect.ValueOf(keeper).IsZero() {
return true
}
}
return false
}