-
Notifications
You must be signed in to change notification settings - Fork 41
/
keeper.go
438 lines (368 loc) · 15 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package keeper
import (
"errors"
"fmt"
"strings"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
ibctypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/provenance-io/provenance/x/marker/types"
)
// Handler is a handler function for use with IterateRecords.
type Handler func(record types.MarkerAccountI) error
// MarkerKeeperI provides a read/write iterate interface to marker acccounts in the auth account keeper store
type MarkerKeeperI interface {
// Returns a new marker instance with the address and baseaccount assigned. Does not save to auth store
NewMarker(sdk.Context, types.MarkerAccountI) types.MarkerAccountI
// GetMarker looks up a marker by a given address
GetMarker(sdk.Context, sdk.AccAddress) (types.MarkerAccountI, error)
// Set a marker in the auth account store
SetMarker(sdk.Context, types.MarkerAccountI)
// Remove a marker from the auth account store
RemoveMarker(sdk.Context, types.MarkerAccountI)
GetEscrow(sdk.Context, types.MarkerAccountI) sdk.Coins
// IterateMarker processes all markers with the given handler function.
IterateMarkers(sdk.Context, func(types.MarkerAccountI) bool)
// GetAuthority returns the signing authority
GetAuthority() string
}
// Keeper defines the name module Keeper
type Keeper struct {
// To check whether accounts exist for addresses.
authKeeper types.AccountKeeper
// To check whether accounts exist for addresses.
authzKeeper types.AuthzKeeper
// To handle movement of coin between accounts and check total supply
bankKeeper types.BankKeeper
// To pass through grant creation for callers with admin access on a marker.
feegrantKeeper types.FeeGrantKeeper
// To access attributes for addresses
attrKeeper types.AttrKeeper
// To access names and normalize required attributes
nameKeeper types.NameKeeper
// Key to access the key-value store from sdk.Context.
storeKey storetypes.StoreKey
// The codec for binary encoding/decoding.
cdc codec.BinaryCodec
// the signing authority for the gov proposals
authority string
markerModuleAddr sdk.AccAddress
ibcTransferModuleAddr sdk.AccAddress
feeCollectorAddr sdk.AccAddress
// Used to transfer the ibc marker
ibcTransferServer types.IbcTransferMsgServer
// reqAttrBypassAddrs is a set of addresses that are allowed to bypass the required attribute check.
// When sending to one of these, if there are required attributes, it behaves as if the addr has them;
// if there aren't required attributes, the sender still needs transfer permission.
// When sending from one of these, if there are required attributes, the destination must have them;
// if there aren't required attributes, it behaves as if the sender has transfer permission.
reqAttrBypassAddrs types.ImmutableAccAddresses
// groupChecker provides a way to check if an account is in a group.
groupChecker types.GroupChecker
}
// NewKeeper returns a marker keeper. It handles:
// - managing MarkerAccounts
// - enforcing permissions for marker creation/deletion/management
//
// CONTRACT: the parameter Subspace must have the param key table already initialized
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
authKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
authzKeeper types.AuthzKeeper,
feegrantKeeper types.FeeGrantKeeper,
attrKeeper types.AttrKeeper,
nameKeeper types.NameKeeper,
ibcTransferServer types.IbcTransferMsgServer,
reqAttrBypassAddrs []sdk.AccAddress,
checker types.GroupChecker,
) Keeper {
rv := Keeper{
authKeeper: authKeeper,
authzKeeper: authzKeeper,
bankKeeper: bankKeeper,
feegrantKeeper: feegrantKeeper,
attrKeeper: attrKeeper,
nameKeeper: nameKeeper,
storeKey: key,
cdc: cdc,
authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
markerModuleAddr: authtypes.NewModuleAddress(types.CoinPoolName),
ibcTransferModuleAddr: authtypes.NewModuleAddress(ibctypes.ModuleName),
feeCollectorAddr: authtypes.NewModuleAddress(authtypes.FeeCollectorName),
ibcTransferServer: ibcTransferServer,
reqAttrBypassAddrs: types.NewImmutableAccAddresses(reqAttrBypassAddrs),
groupChecker: checker,
}
bankKeeper.AppendSendRestriction(rv.SendRestrictionFn)
return rv
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
var _ MarkerKeeperI = &Keeper{}
// NewMarker returns a new marker instance with the address and baseaccount assigned. Does not save to auth store
func (k Keeper) NewMarker(ctx sdk.Context, marker types.MarkerAccountI) types.MarkerAccountI {
return k.authKeeper.NewAccount(ctx, marker).(types.MarkerAccountI)
}
// GetMarker looks up a marker by a given address
func (k Keeper) GetMarker(ctx sdk.Context, address sdk.AccAddress) (types.MarkerAccountI, error) {
mac := k.authKeeper.GetAccount(ctx, address)
if mac != nil {
macc, ok := mac.(types.MarkerAccountI)
if !ok {
return nil, fmt.Errorf("account at %s is not a marker account", address.String())
}
return macc, nil
}
return nil, nil
}
// SetMarker sets a marker in the auth account store will panic if the marker account is not valid or
// if the auth module account keeper fails to marshall the account.
func (k Keeper) SetMarker(ctx sdk.Context, marker types.MarkerAccountI) {
store := ctx.KVStore(k.storeKey)
if err := marker.Validate(); err != nil {
panic(err)
}
k.authKeeper.SetAccount(ctx, marker)
store.Set(types.MarkerStoreKey(marker.GetAddress()), marker.GetAddress())
}
// RemoveMarker removes a marker from the auth account store. Note: if the account holds coins this will
// likely cause an invariant constraint violation for the coin supply
func (k Keeper) RemoveMarker(ctx sdk.Context, marker types.MarkerAccountI) {
store := ctx.KVStore(k.storeKey)
k.authKeeper.RemoveAccount(ctx, marker)
k.RemoveNetAssetValues(ctx, marker.GetAddress())
k.ClearSendDeny(ctx, marker.GetAddress())
store.Delete(types.MarkerStoreKey(marker.GetAddress()))
}
// IterateMarkers iterates all markers with the given handler function.
func (k Keeper) IterateMarkers(ctx sdk.Context, cb func(marker types.MarkerAccountI) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.MarkerStoreKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
account := k.authKeeper.GetAccount(ctx, iterator.Value())
ma, ok := account.(types.MarkerAccountI)
if !ok {
panic(fmt.Errorf("invalid account type in marker account registry"))
}
if cb(ma) {
break
}
}
}
// GetEscrow returns the balances of all coins held in escrow in the marker
func (k Keeper) GetEscrow(ctx sdk.Context, marker types.MarkerAccountI) sdk.Coins {
return k.bankKeeper.GetAllBalances(ctx, marker.GetAddress())
}
// GetAuthority is signer of the proposal
func (k Keeper) GetAuthority() string {
return k.authority
}
// IsAuthority returns true if the provided address bech32 string is the authority address.
func (k Keeper) IsAuthority(addr string) bool {
return strings.EqualFold(k.authority, addr)
}
// ValidateAuthority returns an error if the provided address is not the authority.
func (k Keeper) ValidateAuthority(addr string) error {
if !k.IsAuthority(addr) {
return govtypes.ErrInvalidSigner.Wrapf("expected %q got %q", k.GetAuthority(), addr)
}
return nil
}
// IsSendDeny returns true if sender address is denied for marker
func (k Keeper) IsSendDeny(ctx sdk.Context, markerAddr, senderAddr sdk.AccAddress) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.DenySendKey(markerAddr, senderAddr))
}
// AddSendDeny set sender address to denied for marker
func (k Keeper) AddSendDeny(ctx sdk.Context, markerAddr, senderAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Set(types.DenySendKey(markerAddr, senderAddr), []byte{})
}
// RemoveSendDeny removes sender address from marker deny list
func (k Keeper) RemoveSendDeny(ctx sdk.Context, markerAddr, senderAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.DenySendKey(markerAddr, senderAddr))
}
// ClearSendDeny removes all entries of a marker from a send deny list
func (k Keeper) ClearSendDeny(ctx sdk.Context, markerAddr sdk.AccAddress) {
list := k.GetSendDenyList(ctx, markerAddr)
for _, sender := range list {
k.RemoveSendDeny(ctx, markerAddr, sender)
}
}
// IterateMarkers iterates all markers with the given handler function.
func (k Keeper) IterateSendDeny(ctx sdk.Context, handler func(key []byte) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.DenySendKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
if handler(iterator.Key()) {
break
}
}
}
// GetSendDenyList gets the list of sender addresses from the marker's deny list
func (k Keeper) GetSendDenyList(ctx sdk.Context, markerAddr sdk.AccAddress) []sdk.AccAddress {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.DenySendMarkerPrefix(markerAddr))
list := []sdk.AccAddress{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
_, denied := types.GetDenySendAddresses(iterator.Key())
list = append(list, denied)
}
return list
}
// AddSetNetAssetValues adds a set of net asset values to a marker
func (k Keeper) AddSetNetAssetValues(ctx sdk.Context, marker types.MarkerAccountI, netAssetValues []types.NetAssetValue, source string) error {
var errs []error
for _, nav := range netAssetValues {
if nav.Price.Denom == marker.GetDenom() {
errs = append(errs, fmt.Errorf("net asset value denom cannot match marker denom %q", marker.GetDenom()))
continue
}
if nav.Price.Denom != types.UsdDenom {
_, err := k.GetMarkerByDenom(ctx, nav.Price.Denom)
if err != nil {
if err2 := nav.Validate(); err2 == nil {
navEvent := types.NewEventSetNetAssetValue(marker.GetDenom(), nav.Price, nav.Volume, source)
_ = ctx.EventManager().EmitTypedEvent(navEvent)
}
errs = append(errs, fmt.Errorf("net asset value denom does not exist: %w", err))
continue
}
}
if err := k.SetNetAssetValue(ctx, marker, nav, source); err != nil {
errs = append(errs, fmt.Errorf("cannot set net asset value: %w", err))
}
}
return errors.Join(errs...)
}
// SetNetAssetValue adds/updates a net asset value to marker
func (k Keeper) SetNetAssetValue(ctx sdk.Context, marker types.MarkerAccountI, netAssetValue types.NetAssetValue, source string) error {
netAssetValue.UpdatedBlockHeight = uint64(ctx.BlockHeight())
if err := netAssetValue.Validate(); err != nil {
return err
}
setNetAssetValueEvent := types.NewEventSetNetAssetValue(marker.GetDenom(), netAssetValue.Price, netAssetValue.Volume, source)
if err := ctx.EventManager().EmitTypedEvent(setNetAssetValueEvent); err != nil {
return err
}
key := types.NetAssetValueKey(marker.GetAddress(), netAssetValue.Price.Denom)
bz, err := k.cdc.Marshal(&netAssetValue)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
store.Set(key, bz)
return nil
}
// SetNetAssetValueWithBlockHeight adds/updates a net asset value to marker with a specific block height
func (k Keeper) SetNetAssetValueWithBlockHeight(ctx sdk.Context, marker types.MarkerAccountI, netAssetValue types.NetAssetValue, source string, blockHeight uint64) error {
netAssetValue.UpdatedBlockHeight = blockHeight
if err := netAssetValue.Validate(); err != nil {
return err
}
setNetAssetValueEvent := types.NewEventSetNetAssetValue(marker.GetDenom(), netAssetValue.Price, netAssetValue.Volume, source)
if err := ctx.EventManager().EmitTypedEvent(setNetAssetValueEvent); err != nil {
return err
}
key := types.NetAssetValueKey(marker.GetAddress(), netAssetValue.Price.Denom)
bz, err := k.cdc.Marshal(&netAssetValue)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
store.Set(key, bz)
return nil
}
// GetNetAssetValue gets the NetAssetValue for a marker denom with a specific price denom.
func (k Keeper) GetNetAssetValue(ctx sdk.Context, markerDenom, priceDenom string) (*types.NetAssetValue, error) {
store := ctx.KVStore(k.storeKey)
markerAddr, err := types.MarkerAddress(markerDenom)
if err != nil {
return nil, fmt.Errorf("could not get marker %q address: %w", markerDenom, err)
}
key := types.NetAssetValueKey(markerAddr, priceDenom)
value := store.Get(key)
if len(value) == 0 {
return nil, nil
}
var markerNav types.NetAssetValue
err = k.cdc.Unmarshal(value, &markerNav)
if err != nil {
return nil, fmt.Errorf("could not read nav for marker %q with price denom %q: %w", markerDenom, priceDenom, err)
}
return &markerNav, nil
}
// IterateNetAssetValues iterates net asset values for marker
func (k Keeper) IterateNetAssetValues(ctx sdk.Context, markerAddr sdk.AccAddress, handler func(state types.NetAssetValue) (stop bool)) error {
store := ctx.KVStore(k.storeKey)
it := storetypes.KVStorePrefixIterator(store, types.NetAssetValueKeyPrefix(markerAddr))
defer it.Close()
for ; it.Valid(); it.Next() {
var markerNav types.NetAssetValue
err := k.cdc.Unmarshal(it.Value(), &markerNav)
if err != nil {
return err
} else if handler(markerNav) {
break
}
}
return nil
}
// IterateAllNetAssetValues iterates all net asset values
func (k Keeper) IterateAllNetAssetValues(ctx sdk.Context, handler func(sdk.AccAddress, types.NetAssetValue) (stop bool)) error {
store := ctx.KVStore(k.storeKey)
it := storetypes.KVStorePrefixIterator(store, types.NetAssetValuePrefix)
defer it.Close()
for ; it.Valid(); it.Next() {
markerAddr := types.GetMarkerFromNetAssetValueKey(it.Key())
var markerNav types.NetAssetValue
err := k.cdc.Unmarshal(it.Value(), &markerNav)
if err != nil {
return err
} else if handler(markerAddr, markerNav) {
break
}
}
return nil
}
// RemoveNetAssetValues removes all net asset values for a marker
func (k Keeper) RemoveNetAssetValues(ctx sdk.Context, markerAddr sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
it := storetypes.KVStorePrefixIterator(store, types.NetAssetValueKeyPrefix(markerAddr))
var keys [][]byte
for ; it.Valid(); it.Next() {
keys = append(keys, it.Key())
}
it.Close()
for _, key := range keys {
store.Delete(key)
}
}
// GetReqAttrBypassAddrs returns a deep copy of the addresses that bypass the required attributes checking.
func (k Keeper) GetReqAttrBypassAddrs() []sdk.AccAddress {
return k.reqAttrBypassAddrs.GetSlice()
}
// IsReqAttrBypassAddr returns true if the provided addr can bypass the required attributes checking.
func (k Keeper) IsReqAttrBypassAddr(addr sdk.AccAddress) bool {
return k.reqAttrBypassAddrs.Has(addr)
}
// IsMarkerAccount returns true if the provided address is one for a marker account.
func (k Keeper) IsMarkerAccount(ctx sdk.Context, addr sdk.AccAddress) bool {
if len(addr) == 0 {
return false
}
store := ctx.KVStore(k.storeKey)
return store.Has(types.MarkerStoreKey(addr))
}