/
orderbook.go
691 lines (585 loc) · 18.6 KB
/
orderbook.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package orderbook
import (
"bytes"
"fmt"
"sync"
"sync/atomic"
"decred.org/dcrdex/dex"
"decred.org/dcrdex/dex/msgjson"
"decred.org/dcrdex/dex/order"
)
// ErrEmptyOrderbook is returned from MidGap when the order book is empty.
const ErrEmptyOrderbook = dex.ErrorKind("cannot calculate mid-gap from empty order book")
// Order represents an ask or bid.
type Order struct {
OrderID order.OrderID
Side uint8
Quantity uint64
Rate uint64
Time uint64
// Epoch is only used in the epoch queue, otherwise it is ignored.
Epoch uint64
}
func (o *Order) sell() bool {
return o.Side == msgjson.SellOrderNum
}
// RemoteOrderBook defines the functions a client tracked order book
// must implement.
type RemoteOrderBook interface {
// Sync instantiates a client tracked order book with the
// current order book snapshot.
Sync(*msgjson.OrderBook)
// Book adds a new order to the order book.
Book(*msgjson.BookOrderNote)
// Unbook removes an order from the order book.
Unbook(*msgjson.UnbookOrderNote) error
}
// CachedOrderNote represents a cached order not entry.
type cachedOrderNote struct {
Route string
OrderNote interface{}
}
// rateSell provides the rate and book side information about an order that is
// required for efficiently referencing it in a bookSide.
type rateSell struct {
rate uint64
sell bool
}
// MatchSummary summarizes one or more consecutive matches at a given rate and
// buy/sell direction. Consecutive matches of the same rate and direction are
// binned by the server.
type MatchSummary struct {
Rate uint64 `json:"rate"`
Qty uint64 `json:"qty"`
Stamp uint64 `json:"stamp"`
Sell bool `json:"sell"`
}
// OrderBook represents a client tracked order book.
type OrderBook struct {
// feeRates is at the top to account for atomic field alignment in
// 32-bit systems. See also https://golang.org/pkg/sync/atomic/#pkg-note-BUG
feeRates struct {
base uint64
quote uint64
}
log dex.Logger
seqMtx sync.Mutex
seq uint64
marketID string
noteQueueMtx sync.Mutex
noteQueue []*cachedOrderNote
// Track the orders stored in each bookSide.
ordersMtx sync.Mutex
orders map[order.OrderID]rateSell
buys *bookSide
sells *bookSide
syncedMtx sync.Mutex
synced bool
epochMtx sync.Mutex
currentEpoch uint64
proofedEpoch uint64
epochQueues map[uint64]*EpochQueue
matchSummaryMtx sync.Mutex
matchesSummary []*MatchSummary
}
// NewOrderBook creates a new order book.
func NewOrderBook(logger dex.Logger) *OrderBook {
ob := &OrderBook{
log: logger,
noteQueue: make([]*cachedOrderNote, 0, 16),
orders: make(map[order.OrderID]rateSell),
buys: newBookSide(descending),
sells: newBookSide(ascending),
epochQueues: make(map[uint64]*EpochQueue),
}
return ob
}
// BaseFeeRate is the last reported base asset fee rate.
func (ob *OrderBook) BaseFeeRate() uint64 {
return atomic.LoadUint64(&ob.feeRates.base)
}
// QuoteFeeRate is the last reported quote asset fee rate.
func (ob *OrderBook) QuoteFeeRate() uint64 {
return atomic.LoadUint64(&ob.feeRates.quote)
}
// setSynced sets the synced state of the order book.
func (ob *OrderBook) setSynced(value bool) {
ob.syncedMtx.Lock()
ob.synced = value
ob.syncedMtx.Unlock()
}
// isSynced returns the synced state of the order book.
func (ob *OrderBook) isSynced() bool {
ob.syncedMtx.Lock()
defer ob.syncedMtx.Unlock()
return ob.synced
}
// setSeq should be called whenever a sequenced message is received. If seq is
// out of sequence, an error is logged.
func (ob *OrderBook) setSeq(seq uint64) {
ob.seqMtx.Lock()
defer ob.seqMtx.Unlock()
if seq != ob.seq+1 {
ob.log.Errorf("notification received out of sync. %d != %d - 1", ob.seq, seq)
}
if seq > ob.seq {
ob.seq = seq
}
}
// cacheOrderNote caches an order note.
func (ob *OrderBook) cacheOrderNote(route string, entry interface{}) error {
note := new(cachedOrderNote)
switch route {
case msgjson.BookOrderRoute, msgjson.UnbookOrderRoute, msgjson.UpdateRemainingRoute:
note.Route = route
note.OrderNote = entry
ob.noteQueueMtx.Lock()
ob.noteQueue = append(ob.noteQueue, note)
ob.noteQueueMtx.Unlock()
return nil
default:
return fmt.Errorf("unknown route provided %s", route)
}
}
// processCachedNotes processes all cached notes, each processed note is
// removed from the cache.
func (ob *OrderBook) processCachedNotes() error {
ob.noteQueueMtx.Lock()
defer ob.noteQueueMtx.Unlock()
ob.log.Debugf("Processing %d cached order notes", len(ob.noteQueue))
for len(ob.noteQueue) > 0 {
var entry *cachedOrderNote
entry, ob.noteQueue = ob.noteQueue[0], ob.noteQueue[1:] // so much for preallocating
switch entry.Route {
case msgjson.BookOrderRoute:
note, ok := entry.OrderNote.(*msgjson.BookOrderNote)
if !ok {
panic("failed to cast cached book order note as a BookOrderNote")
}
err := ob.book(note, true)
if err != nil {
return err
}
case msgjson.UnbookOrderRoute:
note, ok := entry.OrderNote.(*msgjson.UnbookOrderNote)
if !ok {
panic("failed to cast cached unbook order note as an UnbookOrderNote")
}
err := ob.unbook(note, true)
if err != nil {
return err
}
case msgjson.UpdateRemainingRoute:
note, ok := entry.OrderNote.(*msgjson.UpdateRemainingNote)
if !ok {
panic("failed to cast cached update_remaining note as an UnbookOrderNote")
}
err := ob.updateRemaining(note, true)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown cached note route provided: %s", entry.Route)
}
}
return nil
}
// Sync updates a client tracked order book with an order book snapshot. It is
// an error if the the OrderBook is already synced.
func (ob *OrderBook) Sync(snapshot *msgjson.OrderBook) error {
if ob.isSynced() {
return fmt.Errorf("order book is already synced")
}
return ob.Reset(snapshot)
}
// Reset forcibly updates a client tracked order book with an order book
// snapshot. This resets the sequence.
// TODO: eliminate this and half of the mutexes!
func (ob *OrderBook) Reset(snapshot *msgjson.OrderBook) error {
// Don't use setSeq here, since this message is the seed and is not expected
// to be 1 more than the current seq value.
ob.seqMtx.Lock()
ob.seq = snapshot.Seq
ob.seqMtx.Unlock()
atomic.StoreUint64(&ob.feeRates.base, snapshot.BaseFeeRate)
atomic.StoreUint64(&ob.feeRates.quote, snapshot.QuoteFeeRate)
ob.marketID = snapshot.MarketID
func() { // Using a function for mutex management with defer.
ob.matchSummaryMtx.Lock()
defer ob.matchSummaryMtx.Unlock()
ob.matchesSummary = make([]*MatchSummary, len(snapshot.RecentMatches))
for i, match := range snapshot.RecentMatches {
rate, qty, ts := match[0], match[1], match[2]
sell := true
if match[1] < 0 {
qty *= -1
sell = false
}
ob.matchesSummary[i] = &MatchSummary{
Rate: uint64(rate),
Qty: uint64(qty),
Sell: sell,
Stamp: uint64(ts),
}
}
}()
err := func() error { // Using a function for mutex management with defer.
ob.ordersMtx.Lock()
defer ob.ordersMtx.Unlock()
ob.orders = make(map[order.OrderID]rateSell, len(snapshot.Orders))
ob.buys.reset()
ob.sells.reset()
for _, o := range snapshot.Orders {
if len(o.OrderID) != order.OrderIDSize {
return fmt.Errorf("expected order id length of %d, got %d", order.OrderIDSize, len(o.OrderID))
}
var oid order.OrderID
copy(oid[:], o.OrderID)
order := &Order{
OrderID: oid,
Side: o.Side,
Quantity: o.Quantity,
Rate: o.Rate,
Time: o.Time,
}
ob.orders[oid] = rateSell{order.Rate, order.sell()}
// Append the order to the order book.
switch o.Side {
case msgjson.BuyOrderNum:
ob.buys.Add(order)
case msgjson.SellOrderNum:
ob.sells.Add(order)
default:
ob.log.Errorf("unknown order side provided: %d", o.Side)
}
}
return nil
}()
if err != nil {
return err
}
// Process cached order notes.
err = ob.processCachedNotes()
if err != nil {
return err
}
ob.setSynced(true)
return nil
}
// book is the workhorse of the exported Book function. It allows booking
// cached and uncached order notes.
func (ob *OrderBook) book(note *msgjson.BookOrderNote, cached bool) error {
if ob.marketID != note.MarketID {
return fmt.Errorf("invalid note market id %s", note.MarketID)
}
if !cached {
// Cache the note if the order book is not synced.
if !ob.isSynced() {
return ob.cacheOrderNote(msgjson.BookOrderRoute, note)
}
}
ob.setSeq(note.Seq)
if len(note.OrderID) != order.OrderIDSize {
return fmt.Errorf("expected order id length of %d, got %d",
order.OrderIDSize, len(note.OrderID))
}
var oid order.OrderID
copy(oid[:], note.OrderID)
order := &Order{
OrderID: oid,
Side: note.Side,
Quantity: note.Quantity,
Rate: note.Rate,
Time: note.Time,
}
ob.ordersMtx.Lock()
ob.orders[order.OrderID] = rateSell{order.Rate, order.sell()}
ob.ordersMtx.Unlock()
// Add the order to its associated books side.
switch order.Side {
case msgjson.BuyOrderNum:
ob.buys.Add(order)
case msgjson.SellOrderNum:
ob.sells.Add(order)
default:
return fmt.Errorf("unknown order side provided: %d", order.Side)
}
return nil
}
// Book adds a new order to the order book.
func (ob *OrderBook) Book(note *msgjson.BookOrderNote) error {
return ob.book(note, false)
}
// updateRemaining is the workhorse of the exported UpdateRemaining function. It
// allows updating cached and uncached orders.
func (ob *OrderBook) updateRemaining(note *msgjson.UpdateRemainingNote, cached bool) error {
if ob.marketID != note.MarketID {
return fmt.Errorf("invalid update_remaining note market id %s", note.MarketID)
}
if !cached {
// Cache the note if the order book is not synced.
if !ob.isSynced() {
return ob.cacheOrderNote(msgjson.UpdateRemainingRoute, note)
}
}
ob.setSeq(note.Seq)
if len(note.OrderID) != order.OrderIDSize {
return fmt.Errorf("expected order id length of %d, got %d",
order.OrderIDSize, len(note.OrderID))
}
var oid order.OrderID
copy(oid[:], note.OrderID)
ob.ordersMtx.Lock()
ordInfo, found := ob.orders[oid]
ob.ordersMtx.Unlock()
if !found {
return fmt.Errorf("update_remaining order %s not found", oid)
}
if ordInfo.sell {
ob.sells.UpdateRemaining(oid, ordInfo.rate, note.Remaining)
} else {
ob.buys.UpdateRemaining(oid, ordInfo.rate, note.Remaining)
}
return nil
}
// UpdateRemaining updates the remaining quantity of a booked order.
func (ob *OrderBook) UpdateRemaining(note *msgjson.UpdateRemainingNote) error {
return ob.updateRemaining(note, false)
}
// LogEpochReport is currently a no-op, and will update market history charts in
// the future.
func (ob *OrderBook) LogEpochReport(note *msgjson.EpochReportNote) error {
// TODO: update future candlestick charts.
atomic.StoreUint64(&ob.feeRates.base, note.BaseFeeRate)
atomic.StoreUint64(&ob.feeRates.quote, note.QuoteFeeRate)
return nil
}
// unbook is the workhorse of the exported Unbook function. It allows unbooking
// cached and uncached order notes.
func (ob *OrderBook) unbook(note *msgjson.UnbookOrderNote, cached bool) error {
if ob.marketID != note.MarketID {
return fmt.Errorf("invalid note market id %s", note.MarketID)
}
if !cached {
// Cache the note if the order book is not synced.
if !ob.isSynced() {
return ob.cacheOrderNote(msgjson.UnbookOrderRoute, note)
}
}
ob.setSeq(note.Seq)
if len(note.OrderID) != order.OrderIDSize {
return fmt.Errorf("expected order id length of %d, got %d",
order.OrderIDSize, len(note.OrderID))
}
var oid order.OrderID
copy(oid[:], note.OrderID)
ob.ordersMtx.Lock()
defer ob.ordersMtx.Unlock() // slightly longer than necessary
ordInfo, ok := ob.orders[oid]
if !ok {
return fmt.Errorf("no order found with id %v", oid)
}
delete(ob.orders, oid)
// Remove the order from its associated book side and rate bin.
if ordInfo.sell {
return ob.sells.Remove(oid, ordInfo.rate)
}
return ob.buys.Remove(oid, ordInfo.rate)
}
// Unbook removes an order from the order book.
func (ob *OrderBook) Unbook(note *msgjson.UnbookOrderNote) error {
return ob.unbook(note, false)
}
// BestNOrders returns the best n orders from the provided side.
// NOTE: This is UNUSED, and test coverage is a near dup of bookside_test.go.
func (ob *OrderBook) BestNOrders(n int, side uint8) ([]*Order, bool, error) {
if !ob.isSynced() {
return nil, false, fmt.Errorf("order book is unsynced")
}
var orders []*Order
var filled bool
switch side {
case msgjson.BuyOrderNum:
orders, filled = ob.buys.BestNOrders(n)
case msgjson.SellOrderNum:
orders, filled = ob.sells.BestNOrders(n)
default:
return nil, false, fmt.Errorf("unknown side provided: %d", side)
}
return orders, filled, nil
}
// Orders is the full order book, as slices of sorted buys and sells, and
// unsorted epoch orders in the current epoch.
func (ob *OrderBook) Orders() ([]*Order, []*Order, []*Order) {
ob.epochMtx.Lock()
eq := ob.epochQueues[ob.currentEpoch]
ob.epochMtx.Unlock()
var epochOrders []*Order
if eq != nil {
// NOTE: This epoch is either (1) open or (2) closed but awaiting a
// match_proof and with no orders for a subsequent epoch yet.
epochOrders = eq.Orders()
}
return ob.buys.Orders(), ob.sells.Orders(), epochOrders
}
// Enqueue appends the provided order note to the corresponding epoch's queue.
func (ob *OrderBook) Enqueue(note *msgjson.EpochOrderNote) error {
ob.setSeq(note.Seq)
idx := note.Epoch
ob.epochMtx.Lock()
defer ob.epochMtx.Unlock()
eq, have := ob.epochQueues[idx]
if !have {
eq = NewEpochQueue()
ob.epochQueues[idx] = eq // NOTE: trusting server here a bit not to flood us with fake epochs
if idx > ob.currentEpoch {
ob.currentEpoch = idx
} else {
ob.log.Errorf("epoch order note received for epoch %d but current epoch is %d", idx, ob.currentEpoch)
}
}
return eq.Enqueue(note)
}
// ValidateMatchProof ensures the match proof data provided is correct by
// comparing it to a locally generated proof from the same epoch queue.
func (ob *OrderBook) ValidateMatchProof(note msgjson.MatchProofNote) error {
idx := note.Epoch
noteSize := len(note.Preimages) + len(note.Misses)
// Extract the EpochQueue in a closure for clean epochMtx handling.
var firstProof bool
extractEpochQueue := func() (*EpochQueue, error) {
ob.epochMtx.Lock()
defer ob.epochMtx.Unlock()
firstProof = ob.proofedEpoch == 0
ob.proofedEpoch = idx
if eq := ob.epochQueues[idx]; eq != nil {
delete(ob.epochQueues, idx) // there will be no more additions to this epoch
return eq, nil
}
// This is expected for an empty match proof or if we started mid-epoch.
if noteSize == 0 || firstProof {
return nil, nil
}
return nil, fmt.Errorf("epoch %d match proof note references %d orders, but local epoch queue is empty",
idx, noteSize)
}
eq, err := extractEpochQueue()
if eq == nil /* includes err != nil */ {
return err
}
if noteSize > 0 {
ob.log.Tracef("Validating match proof note for epoch %d (%s) with %d preimages and %d misses.",
idx, note.MarketID, len(note.Preimages), len(note.Misses))
}
if localSize := eq.Size(); noteSize != localSize {
if firstProof && localSize < noteSize {
return nil // we only saw part of the epoch
}
// Since match_proof lags epoch close by up to preimage request timeout,
// this can still happen for multiple proofs after (re)connect.
return fmt.Errorf("epoch %d match proof note references %d orders, but local epoch queue has %d",
idx, noteSize, localSize)
}
if len(note.Preimages) == 0 {
return nil
}
pimgs := make([]order.Preimage, len(note.Preimages))
for i, entry := range note.Preimages {
copy(pimgs[i][:], entry)
}
misses := make([]order.OrderID, len(note.Misses))
for i, entry := range note.Misses {
copy(misses[i][:], entry)
}
seed, csum, err := eq.GenerateMatchProof(pimgs, misses)
if err != nil {
return fmt.Errorf("unable to generate match proof for epoch %d: %w",
idx, err)
}
if !bytes.Equal(seed, note.Seed) {
return fmt.Errorf("match proof seed mismatch for epoch %d: "+
"expected %s, got %s", idx, note.Seed, seed)
}
if !bytes.Equal(csum, note.CSum) {
return fmt.Errorf("match proof csum mismatch for epoch %d: "+
"expected %s, got %s", idx, note.CSum, csum)
}
return nil
}
// MidGap returns the mid-gap price for the market. If one market side is empty
// the bets rate from the other side will be used. If both sides are empty, an
// error will be returned.
func (ob *OrderBook) MidGap() (uint64, error) {
s, senough := ob.sells.BestNOrders(1)
b, benough := ob.buys.BestNOrders(1)
if !senough {
if !benough {
return 0, ErrEmptyOrderbook
}
return b[0].Rate, nil
}
if !benough {
return s[0].Rate, nil
}
return (s[0].Rate + b[0].Rate) / 2, nil
}
// BestFill is the best (rate, quantity) fill for an order of the type and
// quantity specified. BestFill should be used when the exact quantity of base asset
// is known, i.e. limit orders and market sell orders. For market buy orders,
// use BestFillMarketBuy.
func (ob *OrderBook) BestFill(sell bool, qty uint64) ([]*Fill, bool) {
if sell {
return ob.buys.BestFill(qty)
}
return ob.sells.BestFill(qty)
}
// BestFillMarketBuy is the best (rate, quantity) fill for a market buy order.
// The qty given will be in units of quote asset.
func (ob *OrderBook) BestFillMarketBuy(qty, lotSize uint64) ([]*Fill, bool) {
return ob.sells.bestFill(qty, true, lotSize)
}
// AddRecentMatches adds the recent matches. If the recent matches cache length
// grows bigger than 100, it will slice out the ones first added.
func (ob *OrderBook) AddRecentMatches(matches [][2]int64, ts uint64) []*MatchSummary {
if matches == nil {
return nil
}
newMatches := make([]*MatchSummary, len(matches))
for i, m := range matches {
rate, qty := m[0], m[1]
// negative qty means maker is a sell
sell := true
if qty < 0 {
qty *= -1
sell = false
}
newMatches[i] = &MatchSummary{
Rate: uint64(rate),
Qty: uint64(qty),
Stamp: ts,
Sell: sell,
}
}
// Put the newest first.
for i, j := 0, len(newMatches)-1; i < j; i, j = i+1, j-1 {
newMatches[i], newMatches[j] = newMatches[j], newMatches[i]
}
ob.matchSummaryMtx.Lock()
defer ob.matchSummaryMtx.Unlock()
ob.matchesSummary = append(newMatches, ob.matchesSummary...)
const maxLength = 100
// if ob.matchesSummary length is greater than max length, we slice the array
// to maxLength, removing values first added.
if len(ob.matchesSummary) > maxLength {
ob.matchesSummary = ob.matchesSummary[:maxLength]
}
return newMatches
}
// RecentMatches returns up to 100 recent matches, newest first.
func (ob *OrderBook) RecentMatches() []*MatchSummary {
ob.matchSummaryMtx.Lock()
defer ob.matchSummaryMtx.Unlock()
return ob.matchesSummary
}