forked from evmos/ethermint
-
Notifications
You must be signed in to change notification settings - Fork 5
/
keeper.go
348 lines (292 loc) · 9.06 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package keeper
import (
"fmt"
"strconv"
"github.com/Entangle-Protocol/entangle-blockchain/x/distributorsauth/types"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// var (
// pollPrefix = "poll"
// votesPrefix = "votes"
// countKey = "count"
// pollQueueName = "pending_poll_queue"
// )
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
AuthKeeper types.AccountKeeper
testingMode bool
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
at types.AccountKeeper,
testingMode bool,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
AuthKeeper: at,
testingMode: testingMode,
}
}
func (k Keeper) IsTestingMode() bool {
return k.testingMode
}
func (k *Keeper) SetTestingMode(testingMode bool) {
k.testingMode = testingMode
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k *Keeper) AddDistributor(ctx sdk.Context, info types.DistributorInfo) error {
bech32Address, err := types.EnsureBech32Address(info.Address)
if err != nil {
return err
}
if types.CheckDistrubutorEndDateExpire(ctx, info.GetEndDate()) {
return sdkerrors.Wrap(types.ErrDistributorEndDateLessThenBlockTime, info.Address)
}
info.Address = bech32Address
k.addDistributor(ctx, info)
return nil
}
func (k *Keeper) addDistributor(ctx sdk.Context, info types.DistributorInfo) {
store := ctx.KVStore(k.storeKey)
stringKey := types.DistributorPrefix + info.Address
key := []byte(stringKey)
value := k.cdc.MustMarshalLengthPrefixed(&info)
store.Set(key, value)
ctx.EventManager().EmitEvent(
types.AddDistributorEvent(info.Address, strconv.FormatUint(info.EndDate, 10)),
)
}
func (k *Keeper) GetDistributor(ctx sdk.Context, address string) (types.DistributorInfo, error) {
bech32Address, err := types.EnsureBech32Address(address)
if err != nil {
return types.DistributorInfo{}, sdkerrors.Wrap(types.ErrWrongDistributorAddress, address)
}
return k.getDistributor(ctx, bech32Address)
}
func (k *Keeper) getDistributor(ctx sdk.Context, address string) (types.DistributorInfo, error) {
store := ctx.KVStore(k.storeKey)
stringKey := types.DistributorPrefix + address
key := []byte(stringKey)
value := store.Get(key)
if value == nil {
return types.DistributorInfo{
Address: "",
EndDate: 0,
}, sdkerrors.Wrap(types.ErrNoDistributorInStoreForAddress, address)
}
var info types.DistributorInfo
k.cdc.MustUnmarshalLengthPrefixed(value, &info)
return info, nil
}
func (k *Keeper) GetDistributors(ctx sdk.Context) ([]types.DistributorInfo, error) {
var distrList []types.DistributorInfo
store := ctx.KVStore(k.storeKey)
bytes_prefix := []byte(types.DistributorPrefix)
iterator := sdk.KVStorePrefixIterator(store, bytes_prefix)
for ; iterator.Valid(); iterator.Next() {
var info types.DistributorInfo
k.cdc.MustUnmarshalLengthPrefixed(store.Get(iterator.Key()), &info)
distrList = append(distrList, info)
}
// k.cdc.Unmarshal(iter.Value(), &authorization)
// res := codec.MustMarshalJSONIndent(&k.cdc, distrList)
return distrList, nil
}
func (k *Keeper) RemoveDistributor(ctx sdk.Context, address string) error {
bech32Address, err := types.EnsureBech32Address(address)
if err != nil {
return err
}
k.removeDistributor(ctx, bech32Address)
return nil
}
func (k *Keeper) removeDistributor(ctx sdk.Context, address string) {
key := []byte(types.DistributorPrefix + address)
store := ctx.KVStore(k.storeKey)
store.Delete(key)
ctx.EventManager().EmitEvent(
types.RemoveDistributorEvent(address),
)
}
func (k Keeper) IterateDistributors(ctx sdk.Context,
handler func(address string, end_date uint64) bool,
) {
store := ctx.KVStore(k.storeKey)
bytesPrefix := []byte(types.DistributorPrefix)
iter := sdk.KVStorePrefixIterator(store, bytesPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var distributor types.DistributorInfo
k.cdc.MustUnmarshalLengthPrefixed(store.Get(iter.Key()), &distributor)
if handler(distributor.Address, distributor.EndDate) {
break
}
}
}
func (k *Keeper) AddAdmin(ctx sdk.Context, admin types.Admin) error {
bech32Address, err := types.EnsureBech32Address(admin.Address)
if err != nil {
return err
}
admin.Address = bech32Address
return k.addAdmin(ctx, admin)
}
func (k *Keeper) addAdmin(ctx sdk.Context, admin types.Admin) error {
store := ctx.KVStore(k.storeKey)
key := []byte(types.DistributorAdminPrefix + admin.Address)
value := k.cdc.MustMarshalLengthPrefixed(&admin)
store.Set(key, value)
ctx.EventManager().EmitEvent(
types.AddAdminEvent(admin.Address, strconv.FormatBool(admin.EditOption)),
)
return nil
}
func (k *Keeper) GetAdmin(ctx sdk.Context, address string) (types.Admin, error) {
bech32Address, err := types.EnsureBech32Address(address)
if err != nil {
return types.Admin{}, sdkerrors.Wrap(types.ErrWrongAdminAddress, address)
}
return k.getAdmin(ctx, bech32Address)
}
func (k *Keeper) getAdmin(ctx sdk.Context, address string) (types.Admin, error) {
store := ctx.KVStore(k.storeKey)
key := []byte(types.DistributorAdminPrefix + address)
value := store.Get(key)
if value == nil {
return types.Admin{}, sdkerrors.Wrap(types.ErrNoAdminInStoreForAddress, address)
}
var admin types.Admin
k.cdc.MustUnmarshalLengthPrefixed(value, &admin)
return admin, nil
}
func (k Keeper) GetAdmins(ctx sdk.Context) ([]types.Admin, error) {
var AdminList []types.Admin
store := ctx.KVStore(k.storeKey)
bytes_prefix := []byte(types.DistributorAdminPrefix)
iterator := sdk.KVStorePrefixIterator(store, bytes_prefix)
for ; iterator.Valid(); iterator.Next() {
var admin types.Admin
k.cdc.MustUnmarshalLengthPrefixed(store.Get(iterator.Key()), &admin)
AdminList = append(AdminList, admin)
}
// k.cdc.Unmarshal(iter.Value(), &authorization)
// res := codec.MustMarshalJSONIndent(&k.cdc, distrList)
return AdminList, nil
}
func (k *Keeper) RemoveAdmin(ctx sdk.Context, address string) error {
bech32Address, err := types.EnsureBech32Address(address)
if err != nil {
return err
}
k.removeAdmin(ctx, bech32Address)
return nil
}
func (k *Keeper) removeAdmin(ctx sdk.Context, address string) {
key := []byte(types.DistributorAdminPrefix + address)
store := ctx.KVStore(k.storeKey)
store.Delete(key)
ctx.EventManager().EmitEvent(
types.RemoveAdminEvent(address),
)
}
func (k Keeper) IterateAdmins(ctx sdk.Context,
handler func(address string, editOption bool) bool,
) {
store := ctx.KVStore(k.storeKey)
bytes_prefix := []byte(types.DistributorAdminPrefix)
iter := sdk.KVStorePrefixIterator(store, bytes_prefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var admin types.Admin
k.cdc.MustUnmarshalLengthPrefixed(store.Get(iter.Key()), &admin)
if handler(admin.Address, admin.EditOption) {
break
}
}
}
// ValidateDistributor returns error if the given adress is not allowed distributor.
func (k Keeper) ValidateDistributor(ctx sdk.Context, address string) error {
distributor, err := k.GetDistributor(ctx, address)
if err != nil {
return err
}
if distributor.Address == "" {
return sdkerrors.Wrap(types.ErrNoDistributorInStoreForAddress, address)
}
return nil
}
// ValidateAdmin returns error if the given adress is not allowed admin.
func (k Keeper) ValidateAdmin(ctx sdk.Context, address string) error {
if address == "" {
return sdkerrors.Wrap(types.ErrWrongAdminAddress, address)
}
admin, err := k.GetAdmin(ctx, address)
if err != nil {
return err
}
if admin.Address == "" {
return sdkerrors.Wrap(types.ErrNoAdminInStoreForAddress, address)
}
return nil
}
func (k Keeper) ValidateTransaction(ctx sdk.Context, address string) error {
if address == "" {
return sdkerrors.Wrap(types.ErrWrongDistributorAddress, address)
}
// // Remove 0x prefix
// address = strings.TrimPrefix(address, "0x")
// // Convert Ethereum hex address to bytes
// decodedBytes, err := hex.DecodeString(address)
// if err != nil {
// panic(err)
// }
// // Convert bytes to bech32
// bech32Address := sdk.AccAddress(decodedBytes).String()
// bech32Address, err := types.EnsureBech32Address(address)
// if err != nil {
// return err
// }
err := k.ValidateAdmin(ctx, address)
if err != nil {
err := k.ValidateDistributor(ctx, address)
if err != nil {
return err
}
}
return nil
}
func (k *Keeper) HandleAddDistributorProposal(ctx sdk.Context, p *types.AddDistributorProposal) error {
// check address is valid
_, err := types.EnsureBech32Address(p.Address)
if err != nil {
return sdkerrors.Wrap(types.ErrWrongDistributorAddress, p.Address)
}
// Create DistributorInfo
distributorInfo := types.DistributorInfo{
Address: p.Address,
EndDate: p.EndDate,
}
// Add distributor to store
err = k.AddDistributor(ctx, distributorInfo)
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
types.AddDistributoProposalEvent(p.Address, strconv.FormatUint(p.EndDate, 10)),
)
return nil
}