-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
244 lines (212 loc) · 7.83 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package sidechain
import (
"encoding/binary"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/params"
)
var (
separator = "::"
)
type Keeper struct {
storeKey sdk.StoreKey
paramspace params.Subspace
cfg *crossChainConfig
cdc *codec.Codec
govKeeper *gov.Keeper
ibcKeeper IbcKeeper
}
type IbcKeeper interface {
CreateRawIBCPackageById(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID,
packageType sdk.CrossChainPackageType, packageLoad []byte) (uint64, sdk.Error)
}
func NewKeeper(storeKey sdk.StoreKey, paramspace params.Subspace, cdc *codec.Codec) Keeper {
return Keeper{
storeKey: storeKey,
paramspace: paramspace.WithTypeTable(ParamTypeTable()),
cfg: newCrossChainCfg(),
cdc: cdc,
}
}
func (k *Keeper) SetGovKeeper(govKeeper *gov.Keeper) {
k.govKeeper = govKeeper
}
func (k *Keeper) SetIbcKeeper(ibcKeeper IbcKeeper) {
k.ibcKeeper = ibcKeeper
}
func (k *Keeper) PrepareCtxForSideChain(ctx sdk.Context, sideChainId string) (sdk.Context, error) {
storePrefix := k.GetSideChainStorePrefix(ctx, sideChainId)
if storePrefix == nil {
return sdk.Context{}, fmt.Errorf("invalid sideChainId: %s", sideChainId)
}
// add store prefix to ctx for side chain use
return ctx.WithSideChainKeyPrefix(storePrefix).WithSideChainId(sideChainId), nil
}
// TODO: to support multi side chains in the future. We will enable a registration mechanism and add these chain ids to db.
// then we need to check if the sideChainId already exists
func (k Keeper) SetSideChainIdAndStorePrefix(ctx sdk.Context, sideChainId string, storePrefix []byte) {
store := ctx.KVStore(k.storeKey)
key := GetSideChainStorePrefixKey(sideChainId)
store.Set(key, storePrefix)
}
// get side chain store key prefix
func (k Keeper) GetSideChainStorePrefix(ctx sdk.Context, sideChainId string) []byte {
store := ctx.KVStore(k.storeKey)
return store.Get(GetSideChainStorePrefixKey(sideChainId))
}
func (k *Keeper) GetAllSideChainPrefixes(ctx sdk.Context) ([]string, [][]byte) {
store := ctx.KVStore(k.storeKey)
sideChainIds := make([]string, 0, 1)
prefixes := make([][]byte, 0, 1)
iterator := sdk.KVStorePrefixIterator(store, SideChainStorePrefixByIdKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
sideChainId := iterator.Key()[len(SideChainStorePrefixByIdKey):]
sideChainIds = append(sideChainIds, string(sideChainId))
prefixes = append(prefixes, iterator.Value())
}
return sideChainIds, prefixes
}
func (k *Keeper) RegisterChannel(name string, id sdk.ChannelID, app sdk.CrossChainApplication) error {
_, ok := k.cfg.nameToChannelID[name]
if ok {
return fmt.Errorf("duplicated channel name")
}
_, ok = k.cfg.channelIDToName[id]
if ok {
return fmt.Errorf("duplicated channel id")
}
k.cfg.nameToChannelID[name] = id
k.cfg.channelIDToName[id] = name
k.cfg.channelIDToApp[id] = app
return nil
}
// internally, we use name as the id of the chain, must be unique
func (k *Keeper) RegisterDestChain(name string, chainID sdk.ChainID) error {
if strings.Contains(name, separator) {
return fmt.Errorf("destination chain name should not contains %s", separator)
}
_, ok := k.cfg.destChainNameToID[name]
if ok {
return fmt.Errorf("duplicated destination chain name")
}
_, ok = k.cfg.destChainIDToName[chainID]
if ok {
return fmt.Errorf("duplicated destination chain chainID")
}
k.cfg.destChainNameToID[name] = chainID
k.cfg.destChainIDToName[chainID] = name
return nil
}
func (k *Keeper) SetChannelSendPermission(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID, permission sdk.ChannelPermission) {
kvStore := ctx.KVStore(k.storeKey)
kvStore.Set(buildChannelPermissionKey(destChainID, channelID), []byte{byte(permission)})
}
func (k *Keeper) GetChannelSendPermission(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID) sdk.ChannelPermission {
kvStore := ctx.KVStore(k.storeKey)
bz := kvStore.Get(buildChannelPermissionKey(destChainID, channelID))
if bz == nil {
return sdk.ChannelForbidden
}
return sdk.ChannelPermission(bz[0])
}
func (k *Keeper) GetChannelSendPermissions(ctx sdk.Context, destChainID sdk.ChainID) map[sdk.ChannelID]sdk.ChannelPermission {
kvStore := ctx.KVStore(k.storeKey).Prefix(buildChannelPermissionsPrefixKey(destChainID))
ite := kvStore.Iterator(nil, nil)
defer ite.Close()
permissions := make(map[sdk.ChannelID]sdk.ChannelPermission, 0)
for ; ite.Valid(); ite.Next() {
key := ite.Key()
if len(key) < 1 {
continue
}
channelId := sdk.ChannelID(key[0])
value := ite.Value()
permissions[channelId] = sdk.ChannelPermission(value[0])
}
return permissions
}
func (k *Keeper) GetChannelID(channelName string) (sdk.ChannelID, error) {
id, ok := k.cfg.nameToChannelID[channelName]
if !ok {
return sdk.ChannelID(0), fmt.Errorf("non-existing channel")
}
return id, nil
}
func (k *Keeper) SetSrcChainID(srcChainID sdk.ChainID) {
k.cfg.srcChainID = srcChainID
}
func (k *Keeper) GetSrcChainID() sdk.ChainID {
return k.cfg.srcChainID
}
func (k *Keeper) GetDestChainID(name string) (sdk.ChainID, error) {
destChainID, exist := k.cfg.destChainNameToID[name]
if !exist {
return sdk.ChainID(0), fmt.Errorf("non-existing destination chainName ")
}
return destChainID, nil
}
func (k *Keeper) GetDestChainName(id sdk.ChainID) (string, error) {
destChainName, exist := k.cfg.destChainIDToName[id]
if !exist {
return "", fmt.Errorf("non-existing destination chainID")
}
return destChainName, nil
}
func (k *Keeper) GetSendSequence(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID) uint64 {
return k.getSequence(ctx, destChainID, channelID, PrefixForSendSequenceKey)
}
func (k *Keeper) IncrSendSequence(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID) {
k.incrSequence(ctx, destChainID, channelID, PrefixForSendSequenceKey)
}
func (k *Keeper) GetReceiveSequence(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID) uint64 {
return k.getSequence(ctx, destChainID, channelID, PrefixForReceiveSequenceKey)
}
func (k *Keeper) IncrReceiveSequence(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID) {
k.incrSequence(ctx, destChainID, channelID, PrefixForReceiveSequenceKey)
}
func (k *Keeper) GetCrossChainApp(ctx sdk.Context, channelID sdk.ChannelID) sdk.CrossChainApplication {
return k.cfg.channelIDToApp[channelID]
}
func (k *Keeper) getSequence(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID, prefix []byte) uint64 {
kvStore := ctx.KVStore(k.storeKey)
bz := kvStore.Get(buildChannelSequenceKey(destChainID, channelID, prefix))
if bz == nil {
return 0
}
return binary.BigEndian.Uint64(bz)
}
func (k *Keeper) incrSequence(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID, prefix []byte) {
var sequence uint64
kvStore := ctx.KVStore(k.storeKey)
bz := kvStore.Get(buildChannelSequenceKey(destChainID, channelID, prefix))
if bz == nil {
sequence = 0
} else {
sequence = binary.BigEndian.Uint64(bz)
}
sequenceBytes := make([]byte, sequenceLength)
binary.BigEndian.PutUint64(sequenceBytes, sequence+1)
kvStore.Set(buildChannelSequenceKey(destChainID, channelID, prefix), sequenceBytes)
}
func EndBlock(ctx sdk.Context, k Keeper) {
if sdk.IsUpgrade(sdk.LaunchAxcUpgrade) && k.govKeeper != nil {
chanPermissions := k.getLastChanPermissionChanges(ctx)
// should in reverse order
for j := len(chanPermissions) - 1; j >= 0; j-- {
change := chanPermissions[j]
// must exist
id, _ := k.cfg.destChainNameToID[change.SideChainId]
k.SetChannelSendPermission(ctx, id, change.ChannelId, change.Permission)
_, err := k.SaveChannelSettingChangeToIbc(ctx, id, change.ChannelId, change.Permission)
if err != nil {
ctx.Logger().With("module", "side_chain").Error("failed to write cross chain channel permission change message ",
"err", err)
}
}
}
return
}