-
Notifications
You must be signed in to change notification settings - Fork 179
/
keeper.go
547 lines (463 loc) · 15.6 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package keeper
import (
"log"
"sync"
"github.com/willf/bitset"
"github.com/okex/okexchain/x/common/monitor"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okex/okexchain/x/params"
"github.com/okex/okexchain/x/common"
"github.com/okex/okexchain/x/order/types"
)
var onStartUp sync.Once
// Keeper maintains the link to data storage and exposes getter/setter methods
// for the various parts of the state machine
type Keeper struct {
// The reference to the TokenKeeper to modify balances
tokenKeeper TokenKeeper
// The reference to the Paramstore to get and set gov specific params
paramSpace params.Subspace
dexKeeper DexKeeper
supplyKeeper SupplyKeeper
feeCollectorName string
// Unexposed key to access name store from sdk.Context
orderStoreKey sdk.StoreKey
cdc *codec.Codec // The wire codec for binary encoding/decoding.
enableBackend bool // whether open backend plugin
metric *monitor.OrderMetric
// cache data in memory to avoid marshal/unmarshal too frequently
// reset cache data in BeginBlock
cache *Cache
diskCache *DiskCache
}
// NewKeeper creates new instances of the nameservice Keeper
func NewKeeper(tokenKeeper TokenKeeper, supplyKeeper SupplyKeeper, dexKeeper DexKeeper,
paramSpace params.Subspace, feeCollectorName string, ordersStoreKey sdk.StoreKey,
cdc *codec.Codec,
enableBackend bool, metrics *monitor.OrderMetric) Keeper {
return Keeper{
metric: metrics,
enableBackend: enableBackend,
feeCollectorName: feeCollectorName,
tokenKeeper: tokenKeeper,
supplyKeeper: supplyKeeper,
dexKeeper: dexKeeper,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
orderStoreKey: ordersStoreKey,
cdc: cdc,
cache: NewCache(),
diskCache: newDiskCache(),
}
}
// ResetCache is called in BeginBlock
func (k Keeper) ResetCache(ctx sdk.Context) {
onStartUp.Do(func() {
// init depth book map
depthStore := ctx.KVStore(k.orderStoreKey)
depthIter := sdk.KVStorePrefixIterator(depthStore, types.DepthBookKey)
for ; depthIter.Valid(); depthIter.Next() {
depthBook := &types.DepthBook{}
k.cdc.MustUnmarshalBinaryBare(depthIter.Value(), depthBook)
k.diskCache.addDepthBook(types.GetKey(depthIter), depthBook)
}
depthIter.Close()
// init OrderIDs map
bookStore := ctx.KVStore(k.orderStoreKey)
bookIter := sdk.KVStorePrefixIterator(bookStore, types.OrderIDsKey)
for ; bookIter.Valid(); bookIter.Next() {
var orderIDs []string
k.cdc.MustUnmarshalJSON(bookIter.Value(), &orderIDs)
k.diskCache.addOrderIDs(types.GetKey(bookIter), orderIDs)
}
bookIter.Close()
})
// Reset cache
k.cache.reset()
// VERY IMPORTANT: always reset disk cache in BeginBlock
k.diskCache.reset()
k.diskCache.setOpenNum(k.GetOpenOrderNum(ctx))
k.diskCache.setStoreOrderNum(k.GetStoreOrderNum(ctx))
}
// Cache2Disk flushes cached data into KVStore, called in EndBlock
func (k Keeper) Cache2Disk(ctx sdk.Context) {
closedOrderIDs := k.diskCache.GetClosedOrderIDs()
k.SetLastClosedOrderIDs(ctx, closedOrderIDs)
k.setOpenOrderNum(ctx, k.diskCache.openNum)
k.setStoreOrderNum(ctx, k.diskCache.storeOrderNum)
// update depth book to KVStore
updatedBookKeys := k.diskCache.GetUpdatedDepthbookKeys()
for _, key := range updatedBookKeys {
k.StoreDepthBook(ctx, key, k.diskCache.getDepthBook(key))
}
updatedItemKeys := k.diskCache.GetUpdatedOrderIDKeys()
for _, key := range updatedItemKeys {
k.StoreOrderIDsMap(ctx, key, k.diskCache.getOrderIDs(key))
}
}
// OrderOperationMetric records the order information in the depthBook
type OrderOperationMetric struct {
FullFillNum int64
OpenNum int64
CancelNum int64
ExpireNum int64
PartialFillNum int64
}
// GetOperationMetric gets OperationMetric from keeper
func (k Keeper) GetOperationMetric() OrderOperationMetric {
return OrderOperationMetric{
FullFillNum: k.cache.GetFullFillNum(),
OpenNum: k.diskCache.getOpenNum(),
CancelNum: k.cache.GetCancelNum(),
ExpireNum: k.cache.GetExpireNum(),
PartialFillNum: k.cache.GetPartialFillNum(),
}
}
// GetCache returns the memoryCache
func (k Keeper) GetCache() *Cache {
return k.cache
}
// GetDiskCache returns the diskCache
func (k Keeper) GetDiskCache() *DiskCache {
return k.diskCache
}
// nolint
func (k Keeper) GetTokenKeeper() TokenKeeper {
return k.tokenKeeper
}
// nolint
func (k Keeper) GetDexKeeper() DexKeeper {
return k.dexKeeper
}
// GetExpireBlockHeight gets a slice of ExpireBlockHeight from KVStore
func (k Keeper) GetExpireBlockHeight(ctx sdk.Context, blockHeight int64) []int64 {
store := ctx.KVStore(k.orderStoreKey)
orderInfo := store.Get(types.GetExpireBlockHeightKey(blockHeight))
if orderInfo == nil {
return []int64{}
}
var expireBlockNumbers []int64
k.cdc.MustUnmarshalBinaryBare(orderInfo, &expireBlockNumbers)
return expireBlockNumbers
}
// GetOrder gets order from KVStore
func (k Keeper) GetOrder(ctx sdk.Context, orderID string) *types.Order {
store := ctx.KVStore(k.orderStoreKey)
orderInfo := store.Get(types.GetOrderKey(orderID))
if orderInfo == nil {
return nil
}
order := &types.Order{}
k.cdc.MustUnmarshalBinaryBare(orderInfo, order)
return order
}
// nolint
func (k Keeper) GetLastPrice(ctx sdk.Context, product string) sdk.Dec {
// get last price from cache
price := k.diskCache.getLastPrice(product)
if price.IsPositive() {
return price
}
// get last price from KVStore
store := ctx.KVStore(k.orderStoreKey)
priceBytes := store.Get(types.GetPriceKey(product))
if priceBytes == nil {
// If last price does not exist, set the init price of token pair as last price
tokenPair := k.dexKeeper.GetTokenPair(ctx, product)
if tokenPair == nil {
return sdk.ZeroDec()
}
k.SetLastPrice(ctx, product, tokenPair.InitPrice)
return tokenPair.InitPrice
}
k.cdc.MustUnmarshalBinaryBare(priceBytes, &price)
return price
}
// GetDepthBookCopy gets depth book copy from cache, you are supposed to update the Depthbook if you change it
// create if not exist
func (k Keeper) GetDepthBookCopy(product string) *types.DepthBook {
book := k.diskCache.getDepthBook(product)
if nil == book {
return &types.DepthBook{}
//c.depthBookMap[product] = res // you should do it by yourself
}
return book.Copy()
}
// SetDepthBook updates depthBook in diskCache
func (k Keeper) SetDepthBook(product string, book *types.DepthBook) {
k.diskCache.setDepthBook(product, book)
}
// GetDepthBookFromDB gets depthBook from KVStore
func (k Keeper) GetDepthBookFromDB(ctx sdk.Context, product string) *types.DepthBook {
store := ctx.KVStore(k.orderStoreKey)
bookBytes := store.Get(types.GetDepthBookKey(product))
if bookBytes == nil {
// Return an empty DepthBook instead of nil
return &types.DepthBook{}
}
depthBook := &types.DepthBook{}
k.cdc.MustUnmarshalBinaryBare(bookBytes, depthBook)
return depthBook
}
// GetProductPriceOrderIDs gets OrderIDs from diskCache
func (k Keeper) GetProductPriceOrderIDs(key string) []string {
if orderIDs := k.diskCache.getOrderIDs(key); orderIDs != nil {
return orderIDs
}
return []string{}
}
// GetProductPriceOrderIDsFromDB gets OrderIDs from KVStore
func (k Keeper) GetProductPriceOrderIDsFromDB(ctx sdk.Context, key string) []string {
store := ctx.KVStore(k.orderStoreKey)
idsBytes := store.Get(types.GetOrderIDsKey(key))
if idsBytes == nil {
return nil
}
var orderIDs []string
k.cdc.MustUnmarshalJSON(idsBytes, &orderIDs)
return orderIDs
}
// GetBlockOrderNum gets the num of orders in specific block
func (k Keeper) GetBlockOrderNum(ctx sdk.Context, blockHeight int64) int64 {
store := ctx.KVStore(k.orderStoreKey)
key := types.GetOrderNumPerBlockKey(blockHeight)
numBytes := store.Get(key)
if numBytes == nil {
return 0
}
return common.BytesToInt64(numBytes)
}
// GetLastExpiredBlockHeight gets LastExpiredBlockHeight from KVStore
// LastExpiredBlockHeight means that the block height of his expired height
// list has been processed by expired recently
func (k Keeper) GetLastExpiredBlockHeight(ctx sdk.Context) int64 {
store := ctx.KVStore(k.orderStoreKey)
numBytes := store.Get(types.LastExpiredBlockHeightKey)
if numBytes == nil {
return 0
}
return common.BytesToInt64(numBytes)
}
// GetOpenOrderNum gets OpenOrderNum from KVStore
// OpenOrderNum means the number of orders currently in the open state
func (k Keeper) GetOpenOrderNum(ctx sdk.Context) int64 {
store := ctx.KVStore(k.orderStoreKey)
numBytes := store.Get(types.OpenOrderNumKey)
if numBytes == nil {
return 0
}
return common.BytesToInt64(numBytes)
}
// StoreOrderNum means the number of orders currently stored
// nolint
func (k Keeper) GetStoreOrderNum(ctx sdk.Context) int64 {
store := ctx.KVStore(k.orderStoreKey)
numBytes := store.Get(types.StoreOrderNumKey)
if numBytes == nil {
return 0
}
return common.BytesToInt64(numBytes)
}
// GetUpdatedDepthbookKeys gets UpdatedDepthbookKeys from diskCache
func (k Keeper) GetUpdatedDepthbookKeys() []string {
return k.diskCache.GetUpdatedDepthbookKeys()
}
// GetUpdatedOrderIDs gets UpdatedOrderIDs from memoryCache
func (k Keeper) GetUpdatedOrderIDs() []string {
return k.cache.getUpdatedOrderIDs()
}
// GetTxHandlerMsgResult: be careful, only call by backend module, other module should never use it!
func (k Keeper) GetTxHandlerMsgResult() []bitset.BitSet {
if !k.enableBackend {
return nil
}
return k.cache.toggleCopyTxHandlerMsgResult()
}
// nolint
func (k Keeper) addUpdatedOrderID(orderID string) {
if k.enableBackend {
k.cache.addUpdatedOrderID(orderID)
}
}
// GetLastClosedOrderIDs gets closed order ids in last block
func (k Keeper) GetLastClosedOrderIDs(ctx sdk.Context) []string {
store := ctx.KVStore(k.orderStoreKey)
bz := store.Get(types.RecentlyClosedOrderIDsKey)
orderIDs := []string{}
if bz == nil {
return orderIDs
}
k.cdc.MustUnmarshalJSON(bz, &orderIDs)
return orderIDs
}
// nolint
func (k Keeper) GetBlockMatchResult() *types.BlockMatchResult {
return k.cache.getBlockMatchResult()
}
// nolint
func (k Keeper) SetBlockMatchResult(result *types.BlockMatchResult) {
if k.enableBackend {
k.cache.setBlockMatchResult(result)
}
}
// LockCoins locks coins from the specified address,
func (k Keeper) LockCoins(ctx sdk.Context, addr sdk.AccAddress, coins sdk.SysCoins, lockCoinsType int) error {
if coins.IsZero() {
return nil
}
return k.tokenKeeper.LockCoins(ctx, addr, coins, lockCoinsType)
}
// nolint
func (k Keeper) UnlockCoins(ctx sdk.Context, addr sdk.AccAddress, coins sdk.SysCoins, lockCoinsType int) {
if coins.IsZero() {
return
}
if err := k.tokenKeeper.UnlockCoins(ctx, addr, coins, lockCoinsType); err != nil {
log.Printf("User(%s) unlock coins(%s) failed\n", addr.String(), coins.String())
}
}
// BalanceAccount burns the specified coin and obtains another coin
func (k Keeper) BalanceAccount(ctx sdk.Context, addr sdk.AccAddress,
outputCoins sdk.SysCoins, inputCoins sdk.SysCoins) {
if err := k.tokenKeeper.BalanceAccount(ctx, addr, outputCoins, inputCoins); err != nil {
log.Printf("User(%s) burn locked coins(%s) failed\n", addr.String(), outputCoins.String())
}
}
// nolint
func (k Keeper) GetCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.SysCoins {
return k.tokenKeeper.GetCoins(ctx, addr)
}
// GetProductFeeReceiver gets the fee receiver of specified product from dexKeeper
func (k Keeper) GetProductFeeReceiver(ctx sdk.Context, product string) (sdk.AccAddress, error) {
tokenPair := k.GetDexKeeper().GetTokenPair(ctx, product)
if tokenPair == nil {
return sdk.AccAddress{}, types.ErrTokenPairNotExist(product)
}
operator, exists := k.GetDexKeeper().GetOperator(ctx, tokenPair.Owner)
if exists {
return operator.HandlingFeeAddress, nil
}
return tokenPair.Owner, nil
}
// AddFeeDetail adds detail message of fee to tokenKeeper
func (k Keeper) AddFeeDetail(ctx sdk.Context, from sdk.AccAddress, coins sdk.SysCoins,
feeType string) {
if coins.IsZero() {
return
}
k.tokenKeeper.AddFeeDetail(ctx, from.String(), coins, feeType, "")
}
// SendFeesToProductOwner sends fees from the specified address to productOwner
func (k Keeper) SendFeesToProductOwner(ctx sdk.Context, coins sdk.SysCoins, from sdk.AccAddress,
feeType string, product string) (feeReceiver string, err error) {
if coins.IsZero() {
return "", nil
}
to, err := k.GetProductFeeReceiver(ctx, product)
if err != nil {
return "", err
}
k.tokenKeeper.AddFeeDetail(ctx, from.String(), coins, feeType, "")
if err := k.tokenKeeper.SendCoinsFromAccountToAccount(ctx, from, to, coins); err != nil {
log.Printf("Send fee(%s) to address(%s) failed\n", coins.String(), to.String())
return "", types.ErrSendCoinsFailed(coins.String(), to.String())
}
return to.String(), nil
}
// AddCollectedFees adds fee to the feePool
func (k Keeper) AddCollectedFees(ctx sdk.Context, coins sdk.SysCoins, from sdk.AccAddress,
feeType string, hasFeeDetail bool) error {
if coins.IsZero() {
return nil
}
if hasFeeDetail {
k.tokenKeeper.AddFeeDetail(ctx, from.String(), coins, feeType, k.feeCollectorName)
}
baseCoins := coins
return k.supplyKeeper.SendCoinsFromAccountToModule(ctx, from, k.feeCollectorName, baseCoins)
}
// GetParams gets inflation params from the global param store
func (k Keeper) GetParams(ctx sdk.Context) *types.Params {
var param types.Params
k.paramSpace.GetParamSet(ctx, ¶m)
return ¶m
}
// SetParams sets inflation params from the global param store
func (k Keeper) SetParams(ctx sdk.Context, params *types.Params) {
k.paramSpace.SetParamSet(ctx, params)
}
// nolint
func (k Keeper) GetMetric() *monitor.OrderMetric {
return k.metric
}
// nolint
func (k Keeper) SetMetric() {
k.metric.FullFilledNum.Set(float64(k.cache.fullFillNum))
k.metric.PendingNum.Set(float64(k.diskCache.openNum))
k.metric.CanceledNum.Set(float64(k.cache.cancelNum))
k.metric.ExpiredNum.Set(float64(k.cache.expireNum))
k.metric.PartialFilledNum.Set(float64(k.cache.partialFillNum))
}
// GetBestBidAndAsk gets the highest bidPrice and the lowest askPrice from depthBook
func (k Keeper) GetBestBidAndAsk(ctx sdk.Context, product string) (sdk.Dec, sdk.Dec) {
bestBid := sdk.ZeroDec()
bestAsk := sdk.ZeroDec()
depthBook := k.GetDepthBookFromDB(ctx, product)
for _, item := range depthBook.Items {
if item.BuyQuantity.IsPositive() {
if item.Price.GT(bestBid) {
bestBid = item.Price
}
}
if item.SellQuantity.IsPositive() {
if item.Price.LT(bestAsk) {
bestAsk = item.Price
}
}
}
return bestBid, bestAsk
}
// RemoveOrderFromDepthBook removes order from depthBook, and updates cancelNum, expireNum, updatedOrderIDs from cache
func (k Keeper) RemoveOrderFromDepthBook(order *types.Order, feeType string) {
k.addUpdatedOrderID(order.OrderID)
if feeType == types.FeeTypeOrderCancel {
k.cache.IncreaseCancelNum()
} else if feeType == types.FeeTypeOrderExpire {
k.cache.IncreaseExpireNum()
}
k.diskCache.removeOrder(order)
}
// nolint
func (k Keeper) UpdateOrder(order *types.Order, ctx sdk.Context) {
// update order to keeper
k.SetOrder(ctx, order.OrderID, order)
// record updated orderID
k.addUpdatedOrderID(order.OrderID)
if order.Status == types.OrderStatusFilled {
k.diskCache.closeOrder(order.OrderID)
k.cache.IncreaseFullFillNum()
} else {
k.cache.IncreasePartialFillNum()
}
}
// nolint
func (k Keeper) InsertOrderIntoDepthBook(order *types.Order) {
k.diskCache.insertOrder(order)
}
// FilterDelistedProducts deletes non-existent products from the specified products
func (k Keeper) FilterDelistedProducts(ctx sdk.Context, products []string) []string {
var cleanProducts []string
for _, product := range products {
tokenPair := k.dexKeeper.GetTokenPair(ctx, product)
if tokenPair != nil {
cleanProducts = append(cleanProducts, product)
}
}
return cleanProducts
}
// nolint
func (k Keeper) AddTxHandlerMsgResult(resultSet bitset.BitSet) {
if k.enableBackend {
k.cache.addTxHandlerMsgResult(resultSet)
}
}