/
disk_cache.go
236 lines (201 loc) · 6.11 KB
/
disk_cache.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
package keeper
import (
"sort"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
"github.com/FiboChain/fbc/x/order/types"
)
// OrderIDsMap stores orderIDSlice with map.
// <product:price:side> -> <orderIDs>
type OrderIDsMap struct {
Data map[string][]string
updatedItems map[string]struct{}
}
// DepthBookMap stores depthBook with map.
// <product> -> <depthBook>
type DepthBookMap struct {
data map[string]*types.DepthBook
updatedItems map[string]struct{}
newItems map[string]struct{}
}
// DiskCache stores cache that will persist to disk at endBlock.
type DiskCache struct {
depthBookMap *DepthBookMap
orderIDsMap *OrderIDsMap
priceMap map[string]sdk.Dec
storeOrderNum int64 // current stored order num
openNum int64 // current open orders num
closedOrderIDs []string
}
func newDiskCache() *DiskCache {
return &DiskCache{
closedOrderIDs: []string{},
orderIDsMap: &OrderIDsMap{make(map[string][]string), make(map[string]struct{})},
priceMap: make(map[string]sdk.Dec),
depthBookMap: &DepthBookMap{make(map[string]*types.DepthBook), make(map[string]struct{}),
make(map[string]struct{})},
}
}
// reset is invoked in begin block
func (c *DiskCache) reset() {
c.closedOrderIDs = []string{}
c.orderIDsMap.updatedItems = make(map[string]struct{})
c.depthBookMap.updatedItems = make(map[string]struct{})
c.depthBookMap.newItems = make(map[string]struct{})
}
// nolint
func (c *DiskCache) GetClosedOrderIDs() []string {
return c.closedOrderIDs
}
func (c *DiskCache) setLastPrice(product string, price sdk.Dec) {
c.priceMap[product] = price
}
func (c *DiskCache) getLastPrice(product string) sdk.Dec {
if price, ok := c.priceMap[product]; ok {
return price
}
return sdk.ZeroDec()
}
// GetOrderIDsMapCopy returns a new copy of OrderIDsMap
func (c *DiskCache) GetOrderIDsMapCopy() *OrderIDsMap {
if c.orderIDsMap == nil {
return nil
}
ret := make(map[string][]string)
for k, v := range c.orderIDsMap.Data {
if len(v) == 0 {
ret[k] = []string{}
}
ret[k] = append(ret[k], v...)
}
return &OrderIDsMap{Data: ret}
}
func (c *DiskCache) getOrderIDs(key string) []string {
return c.orderIDsMap.Data[key]
}
func (c *DiskCache) setStoreOrderNum(num int64) {
c.storeOrderNum = num
}
// nolint
func (c *DiskCache) DecreaseStoreOrderNum(num int64) int64 {
c.storeOrderNum -= num
return c.storeOrderNum
}
func (c *DiskCache) setOpenNum(num int64) {
c.openNum = num
}
func (c *DiskCache) getOpenNum() int64 {
return c.openNum
}
func (c *DiskCache) addOrderIDs(key string, orderIDs []string) {
c.orderIDsMap.Data[key] = orderIDs
}
func (c *DiskCache) addDepthBook(product string, book *types.DepthBook) {
c.depthBookMap.data[product] = book
}
// setOrderIDs updates or removes unfilled order ids
func (c *DiskCache) setOrderIDs(key string, orderIDs []string) {
if len(orderIDs) == 0 {
// remove empty element immediately, not do it by the end of endblock
delete(c.orderIDsMap.Data, key)
} else {
c.orderIDsMap.Data[key] = orderIDs
}
c.orderIDsMap.updatedItems[key] = struct{}{}
}
// setDepthBook updates or removes a depth book
func (c *DiskCache) setDepthBook(product string, book *types.DepthBook) {
if book != nil && len(book.Items) > 0 {
c.depthBookMap.data[product] = book
} else {
delete(c.depthBookMap.data, product)
}
c.depthBookMap.updatedItems[product] = struct{}{}
}
// UpdatedOrderIDKeys
// nolint
func (c *DiskCache) GetUpdatedOrderIDKeys() []string {
updatedKeys := make([]string, 0, len(c.orderIDsMap.updatedItems))
for key := range c.orderIDsMap.updatedItems {
updatedKeys = append(updatedKeys, key)
}
sort.Strings(updatedKeys)
return updatedKeys
}
func (c *DiskCache) getDepthBook(product string) *types.DepthBook {
res := c.depthBookMap.data[product]
return res
}
func (c *DiskCache) getProductsFromDepthBookMap() []string {
products := make([]string, 0, len(c.depthBookMap.data))
for product := range c.depthBookMap.data {
products = append(products, product)
}
return products
}
// GetUpdatedDepthbookKeys returns a new copy of UpdatedDepthbookKeys
func (c *DiskCache) GetUpdatedDepthbookKeys() []string {
updatedKeys := make([]string, 0, len(c.depthBookMap.updatedItems))
for key := range c.depthBookMap.updatedItems {
updatedKeys = append(updatedKeys, key)
}
sort.Strings(updatedKeys)
return updatedKeys
}
// GetNewDepthbookKeys returns a new copy of NewDepthbookKeys
func (c *DiskCache) GetNewDepthbookKeys() []string {
newAddKeys := make([]string, 0, len(c.depthBookMap.newItems))
for key := range c.depthBookMap.newItems {
newAddKeys = append(newAddKeys, key)
}
return newAddKeys
}
// insertOrder inserts a new order into orderIDsMap
func (c *DiskCache) insertOrder(order *types.Order) {
// 1. update depthBookMap
depthBook, ok := c.depthBookMap.data[order.Product]
if !ok {
depthBook = &types.DepthBook{}
c.depthBookMap.data[order.Product] = depthBook
}
depthBook.InsertOrder(order)
c.depthBookMap.updatedItems[order.Product] = struct{}{}
c.depthBookMap.newItems[order.Product] = struct{}{}
// 2. update orderIDsMap
orderIDsMap := c.orderIDsMap
key := types.FormatOrderIDsKey(order.Product, order.Price, order.Side)
orderIDs, ok := orderIDsMap.Data[key]
if !ok {
orderIDs = []string{}
}
orderIDs = append(orderIDs, order.OrderID)
orderIDsMap.Data[key] = orderIDs
c.orderIDsMap.updatedItems[key] = struct{}{}
c.openNum++
c.storeOrderNum++
}
func (c *DiskCache) closeOrder(orderID string) {
c.closedOrderIDs = append(c.closedOrderIDs, orderID)
c.openNum--
}
// remove an order from orderIDsMap when order cancelled/expired
func (c *DiskCache) removeOrder(order *types.Order) {
// update depth book map
depthBook := c.getDepthBook(order.Product)
if depthBook != nil {
depthBook.RemoveOrder(order)
c.setDepthBook(order.Product, depthBook)
}
// update order id map
orderIDsMap := c.orderIDsMap
key := types.FormatOrderIDsKey(order.Product, order.Price, order.Side)
orderIDs := orderIDsMap.Data[key]
orderIDsLen := len(orderIDs)
for i := 0; i < orderIDsLen; i++ {
if orderIDs[i] == order.OrderID {
orderIDs = append(orderIDs[:i], orderIDs[i+1:]...)
c.setOrderIDs(key, orderIDs)
break
}
}
c.closeOrder(order.OrderID)
}