-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
254 lines (219 loc) · 7.78 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
245
246
247
248
249
250
251
252
253
254
package keeper
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/axc/rlp"
"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/ibc"
"github.com/cosmos/cosmos-sdk/x/paramHub/types"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/sidechain"
sTypes "github.com/cosmos/cosmos-sdk/x/sidechain/types"
"github.com/tendermint/tendermint/libs/log"
)
var (
ParamStoreKeyLastFeeChangeProposalID = []byte("lastFeeChangeProposalID")
ParamStoreKeyFees = []byte("fees")
// for side chain
ParamStoreKeySCLastParamsChangeProposalID = []byte("SCLastParamsChangeProposalID")
// for beacon chain
ParamStoreKeyBCLastParamsChangeProposalID = []byte("BCLastParamsChangeProposalID")
)
const (
ParamSpace = "paramhub"
SafeToleratePeriod = 2 * 7 * 24 * 60 * 60 * time.Second // 2 weeks
)
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable(
ParamStoreKeyLastFeeChangeProposalID, types.LastProposalID{},
ParamStoreKeyFees, []types.FeeParam{},
ParamStoreKeySCLastParamsChangeProposalID, types.LastProposalID{},
ParamStoreKeyBCLastParamsChangeProposalID, types.LastProposalID{},
)
}
type Keeper struct {
params.Keeper
cdc *codec.Codec
paramSpace params.Subspace
// just for query
subscriberParamSpace []*types.ParamSpaceProto
govKeeper *gov.Keeper
ibcKeeper *ibc.Keeper
ScKeeper *sidechain.Keeper
updateCallbacks []func(sdk.Context, interface{})
genesisCallbacks []func(sdk.Context, interface{})
loadCallBacks []func(sdk.Context, interface{})
// for beacon chain
subscriberBCParamSpace []*types.BCParamSpaceProto
updateBCCallbacks []func(sdk.Context, interface{})
}
func NewKeeper(cdc *codec.Codec, key *sdk.KVStoreKey, tkey *sdk.TransientStoreKey) *Keeper {
keeper := Keeper{
Keeper: params.NewKeeper(cdc, key, tkey),
cdc: cdc,
updateCallbacks: make([]func(sdk.Context, interface{}), 0),
genesisCallbacks: make([]func(sdk.Context, interface{}), 0),
subscriberParamSpace: make([]*types.ParamSpaceProto, 0),
}
keeper.paramSpace = keeper.Subspace(ParamSpace).WithTypeTable(ParamTypeTable())
// Add global callback(belongs to no other plugin) here
keeper.registerFeeParamCallBack()
keeper.registerCSCParamsCallBack()
return &keeper
}
func (keeper *Keeper) GetSubscriberParamSpace() []*types.ParamSpaceProto {
return keeper.subscriberParamSpace
}
func (keeper *Keeper) GetSubscriberBCParamSpace() []*types.BCParamSpaceProto {
return keeper.subscriberBCParamSpace
}
func (keeper *Keeper) SetGovKeeper(govKeeper *gov.Keeper) {
keeper.govKeeper = govKeeper
}
func (keeper *Keeper) SetupForSideChain(scKeeper *sidechain.Keeper, ibcKeeper *ibc.Keeper) {
keeper.ScKeeper = scKeeper
keeper.ibcKeeper = ibcKeeper
keeper.initIbc()
}
func (keeper *Keeper) GetCodeC() *codec.Codec {
return keeper.cdc
}
func (keeper Keeper) initIbc() {
if keeper.ibcKeeper == nil {
return
}
err := keeper.ScKeeper.RegisterChannel(ChannelName, ChannelId, &keeper)
if err != nil {
panic(fmt.Sprintf("register ibc channel failed, channel=%s, err=%s", ChannelName, err.Error()))
}
}
func (keeper *Keeper) EndBreatheBlock(ctx sdk.Context) {
log := keeper.Logger(ctx)
log.Info("Sync breath block params proposals.")
feeChange := keeper.getLastFeeChangeParam(ctx)
if feeChange != nil {
keeper.notifyOnUpdate(ctx, feeChange)
}
if sdk.IsUpgrade(sdk.LaunchAxcUpgrade) {
_, storePrefixes := keeper.ScKeeper.GetAllSideChainPrefixes(ctx)
for i := range storePrefixes {
sideChainCtx := ctx.WithSideChainKeyPrefix(storePrefixes[i])
scParamChanges := keeper.getLastSCParamChanges(sideChainCtx)
if scParamChanges != nil {
for _, change := range scParamChanges.SCParams {
keeper.notifyOnUpdate(sideChainCtx, change)
}
}
}
}
return
}
func (keeper *Keeper) EndBlock(ctx sdk.Context) {
log := keeper.Logger(ctx)
log.Info("Sync params proposals.")
if sdk.IsUpgrade(sdk.LaunchAxcUpgrade) && keeper.ScKeeper != nil {
sideChainIds, storePrefixes := keeper.ScKeeper.GetAllSideChainPrefixes(ctx)
for idx := range storePrefixes {
sideChainCtx := ctx.WithSideChainKeyPrefix(storePrefixes[idx])
cscChanges := keeper.getLastCSCParamChanges(sideChainCtx)
if len(cscChanges) > 0 {
keeper.notifyOnUpdate(sideChainCtx, types.CSCParamChanges{Changes: cscChanges, ChainID: sideChainIds[idx]})
}
}
}
if sdk.IsUpgrade(sdk.BEP159) {
bcParamChanges := keeper.getLastBCParamChanges(ctx)
if bcParamChanges != nil {
for _, change := range bcParamChanges.BCParams {
keeper.notifyOnBCUpdate(ctx, change)
}
}
}
return
}
func (keeper *Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "paramHub")
}
func (keeper *Keeper) notifyOnUpdate(context sdk.Context, change interface{}) {
for _, c := range keeper.updateCallbacks {
c(context, change)
}
}
func (keeper *Keeper) notifyOnBCUpdate(context sdk.Context, change interface{}) {
for _, c := range keeper.updateBCCallbacks {
c(context, change)
}
}
func (keeper *Keeper) notifyOnLoad(ctx sdk.Context, load interface{}) {
for _, c := range keeper.loadCallBacks {
c(ctx, load)
}
}
func (keeper *Keeper) notifyOnGenesis(ctx sdk.Context, state interface{}) {
for _, c := range keeper.genesisCallbacks {
c(ctx, state)
}
}
func (keeper *Keeper) InitGenesis(ctx sdk.Context, params types.GenesisState) {
keeper.initFeeGenesis(ctx, params)
keeper.notifyOnGenesis(ctx, params)
keeper.setLastFeeChangeProposalId(ctx, types.LastProposalID{0})
}
func (keeper *Keeper) Load(ctx sdk.Context) {
keeper.loadFeeParam(ctx)
}
func (keeper *Keeper) SubscribeParamChange(updateCb func(sdk.Context, interface{}), spaceProto *types.ParamSpaceProto, genesisCb func(sdk.Context, interface{}), loadCb func(sdk.Context, interface{})) {
if updateCb != nil {
keeper.SubscribeUpdateEvent(updateCb)
}
if genesisCb != nil {
keeper.SubscribeGenesisEvent(genesisCb)
}
if loadCb != nil {
keeper.SubscribeLoadEvent(loadCb)
}
if spaceProto != nil {
keeper.subscriberParamSpace = append(keeper.subscriberParamSpace, spaceProto)
}
}
func (keeper *Keeper) SubscribeBCParamChange(updateCb func(sdk.Context, interface{}), spaceProto *types.BCParamSpaceProto) {
if updateCb != nil {
keeper.updateBCCallbacks = append(keeper.updateBCCallbacks, updateCb)
}
if spaceProto != nil {
keeper.subscriberBCParamSpace = append(keeper.subscriberBCParamSpace, spaceProto)
}
}
func (keeper *Keeper) SubscribeUpdateEvent(c func(sdk.Context, interface{})) {
keeper.updateCallbacks = append(keeper.updateCallbacks, c)
}
func (keeper *Keeper) SubscribeGenesisEvent(c func(sdk.Context, interface{})) {
keeper.genesisCallbacks = append(keeper.genesisCallbacks, c)
}
func (keeper *Keeper) SubscribeLoadEvent(c func(sdk.Context, interface{})) {
keeper.loadCallBacks = append(keeper.loadCallBacks, c)
}
// implement cross chain app
func (keeper *Keeper) ExecuteSynPackage(ctx sdk.Context, payload []byte, _ int64) sdk.ExecuteResult {
panic("receive unexpected package")
}
func (keeper *Keeper) ExecuteAckPackage(ctx sdk.Context, payload []byte) sdk.ExecuteResult {
var ackPackage sTypes.CommonAckPackage
err := rlp.DecodeBytes(payload, &ackPackage)
if err != nil {
keeper.Logger(ctx).Error("fail to decode ack package", "payload", payload)
return sdk.ExecuteResult{Err: types.ErrInvalidCrossChainPackage(types.DefaultCodespace)}
}
if !ackPackage.IsOk() {
keeper.Logger(ctx).Error("side chain failed to process param package", "code", ackPackage.Code)
}
return sdk.ExecuteResult{}
}
// When the ack application crash, payload is the payload of the origin package.
func (keeper *Keeper) ExecuteFailAckPackage(ctx sdk.Context, payload []byte) sdk.ExecuteResult {
//do no thing
keeper.Logger(ctx).Error("side chain process params package crashed", "payload", payload)
return sdk.ExecuteResult{}
}