/
orderbook.go
394 lines (345 loc) · 12.9 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
package ingest
import (
"context"
"database/sql"
"math/rand"
"sort"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/TosinShada/monorepo/exp/orderbook"
"github.com/TosinShada/monorepo/services/horizon/internal/db2/history"
"github.com/TosinShada/monorepo/services/horizon/internal/ingest/processors"
"github.com/TosinShada/monorepo/support/errors"
"github.com/TosinShada/monorepo/xdr"
)
const (
verificationFrequency = time.Hour
updateFrequency = 2 * time.Second
)
// OrderBookStream updates an in memory graph to be consistent with
// offers in the Horizon DB. Any offers which are created, modified, or removed
// from the Horizon DB during ingestion will be applied to the in memory order book
// graph. OrderBookStream assumes that no other component will update the
// in memory graph. However, it is safe for other go routines to use the
// in memory graph for read operations.
type OrderBookStream struct {
graph orderbook.OBGraph
historyQ history.IngestionQ
// LatestLedgerGauge exposes the local (order book graph)
// latest processed ledger
LatestLedgerGauge prometheus.Gauge
lastLedger uint32
lastVerification time.Time
encodingBuffer *xdr.EncodingBuffer
}
// NewOrderBookStream constructs and initializes an OrderBookStream instance
func NewOrderBookStream(historyQ history.IngestionQ, graph orderbook.OBGraph) *OrderBookStream {
return &OrderBookStream{
graph: graph,
historyQ: historyQ,
LatestLedgerGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "horizon", Subsystem: "order_book_stream", Name: "latest_ledger",
}),
lastVerification: time.Now(),
encodingBuffer: xdr.NewEncodingBuffer(),
}
}
type ingestionStatus struct {
HistoryConsistentWithState bool
StateInvalid bool
LastIngestedLedger uint32
LastOfferCompactionLedger uint32
LastLiquidityPoolCompactionLedger uint32
}
func (o *OrderBookStream) getIngestionStatus(ctx context.Context) (ingestionStatus, error) {
var status ingestionStatus
var err error
status.StateInvalid, err = o.historyQ.GetExpStateInvalid(ctx)
if err != nil {
return status, errors.Wrap(err, "Error from GetExpStateInvalid")
}
var lastHistoryLedger uint32
lastHistoryLedger, err = o.historyQ.GetLatestHistoryLedger(ctx)
if err != nil {
return status, errors.Wrap(err, "Error from GetLatestHistoryLedger")
}
status.LastIngestedLedger, err = o.historyQ.GetLastLedgerIngestNonBlocking(ctx)
if err != nil {
return status, errors.Wrap(err, "Error from GetLastLedgerIngestNonBlocking")
}
status.LastOfferCompactionLedger, err = o.historyQ.GetOfferCompactionSequence(ctx)
if err != nil {
return status, errors.Wrap(err, "Error from GetOfferCompactionSequence")
}
status.LastLiquidityPoolCompactionLedger, err = o.historyQ.GetLiquidityPoolCompactionSequence(ctx)
if err != nil {
return status, errors.Wrap(err, "Error from GetLiquidityPoolCompactionSequence")
}
status.HistoryConsistentWithState = (status.LastIngestedLedger == lastHistoryLedger) ||
// Running ingestion on an empty DB is a special case because we first ingest from the history archive.
// Then, on the next iteration, we ingest TX Meta from Stellar Core. So there is a brief
// period where there will not be any rows in the history_ledgers table but that is ok.
(lastHistoryLedger == 0)
return status, nil
}
// update returns true if the order book graph was reset
func (o *OrderBookStream) update(ctx context.Context, status ingestionStatus) (bool, error) {
reset := o.lastLedger == 0
if status.StateInvalid {
log.WithField("status", status).Warn("ingestion state is invalid")
reset = true
} else if !status.HistoryConsistentWithState {
log.WithField("status", status).
Info("waiting for ingestion system catchup")
reset = true
} else if status.LastIngestedLedger < o.lastLedger {
log.WithField("status", status).
WithField("last_ledger", o.lastLedger).
Warn("ingestion is behind order book last ledger")
reset = true
} else if o.lastLedger > 0 && o.lastLedger < status.LastOfferCompactionLedger {
log.WithField("status", status).
WithField("last_ledger", o.lastLedger).
Warn("order book is behind the last offer compaction ledger")
reset = true
} else if status.LastOfferCompactionLedger != status.LastLiquidityPoolCompactionLedger {
log.WithField("status", status).
WithField("last_ledger", o.lastLedger).
Warn("offer compaction is not consistentwith liquidity pool compaction")
reset = true
}
if reset {
o.graph.Clear()
o.lastLedger = 0
// wait until offers in horizon db is valid before populating order book graph
if status.StateInvalid || !status.HistoryConsistentWithState {
return true, nil
}
defer o.graph.Discard()
err := o.historyQ.StreamAllOffers(ctx, func(offer history.Offer) error {
o.graph.AddOffers(offerToXDR(offer))
return nil
})
if err != nil {
return true, errors.Wrap(err, "Error loading offers into orderbook")
}
err = o.historyQ.StreamAllLiquidityPools(ctx, func(liquidityPool history.LiquidityPool) error {
if liquidityPoolXDR, liquidityPoolErr := liquidityPoolToXDR(liquidityPool); liquidityPoolErr != nil {
return errors.Wrapf(liquidityPoolErr, "Invalid liquidity pool row %v, unable to marshal to xdr", liquidityPool)
} else {
o.graph.AddLiquidityPools(liquidityPoolXDR)
return nil
}
})
if err != nil {
return true, errors.Wrap(err, "Error loading liquidity pools into orderbook")
}
if err := o.graph.Apply(status.LastIngestedLedger); err != nil {
return true, errors.Wrap(err, "Error applying changes to order book")
}
o.lastLedger = status.LastIngestedLedger
o.LatestLedgerGauge.Set(float64(status.LastIngestedLedger))
return true, nil
}
if status.LastIngestedLedger == o.lastLedger {
return false, nil
}
defer o.graph.Discard()
offers, err := o.historyQ.GetUpdatedOffers(ctx, o.lastLedger)
if err != nil {
return false, errors.Wrap(err, "Error from GetUpdatedOffers")
}
liquidityPools, err := o.historyQ.GetUpdatedLiquidityPools(ctx, o.lastLedger)
if err != nil {
return false, errors.Wrap(err, "Error from GetUpdatedLiquidityPools")
}
for _, offer := range offers {
if offer.Deleted {
o.graph.RemoveOffer(xdr.Int64(offer.OfferID))
} else {
o.graph.AddOffers(offerToXDR(offer))
}
}
for _, liquidityPool := range liquidityPools {
var poolXDR xdr.LiquidityPoolEntry
poolXDR, err = liquidityPoolToXDR(liquidityPool)
if err != nil {
return false, errors.Wrap(err, "Error converting liquidity pool row to xdr")
}
if liquidityPool.Deleted {
o.graph.RemoveLiquidityPool(poolXDR)
} else {
o.graph.AddLiquidityPools(poolXDR)
}
}
if err = o.graph.Apply(status.LastIngestedLedger); err != nil {
return false, errors.Wrap(err, "Error applying changes to order book")
}
o.lastLedger = status.LastIngestedLedger
o.LatestLedgerGauge.Set(float64(status.LastIngestedLedger))
return false, nil
}
func (o *OrderBookStream) verifyAllOffers(ctx context.Context, offers []xdr.OfferEntry) (bool, error) {
var ingestionOffers []history.Offer
err := o.historyQ.StreamAllOffers(ctx, func(offer history.Offer) error {
ingestionOffers = append(ingestionOffers, offer)
return nil
})
if err != nil {
return false, errors.Wrap(err, "Error loading all offers for orderbook verification")
}
mismatch := len(offers) != len(ingestionOffers)
if !mismatch {
sort.Slice(offers, func(i, j int) bool {
return offers[i].OfferId < offers[j].OfferId
})
sort.Slice(ingestionOffers, func(i, j int) bool {
return ingestionOffers[i].OfferID < ingestionOffers[j].OfferID
})
for i, offerRow := range ingestionOffers {
offerEntry := offers[i]
offerRowXDR := offerToXDR(offerRow)
offerEntryBase64, err := o.encodingBuffer.MarshalBase64(&offerEntry)
if err != nil {
return false, errors.Wrap(err, "Error from marshalling offerEntry")
}
offerRowBase64, err := o.encodingBuffer.MarshalBase64(&offerRowXDR)
if err != nil {
return false, errors.Wrap(err, "Error from marshalling offerRowXDR")
}
if offerEntryBase64 != offerRowBase64 {
mismatch = true
break
}
}
}
if mismatch {
log.WithField("stream_offers", offers).
WithField("ingestion_offers", ingestionOffers).
Error("offers derived from order book stream does not match offers from ingestion")
return false, nil
}
log.Info("offer stream verification succeeded")
return true, nil
}
func (o *OrderBookStream) verifyAllLiquidityPools(ctx context.Context, liquidityPools []xdr.LiquidityPoolEntry) (bool, error) {
var ingestionLiquidityPools []history.LiquidityPool
err := o.historyQ.StreamAllLiquidityPools(ctx, func(liquidityPool history.LiquidityPool) error {
ingestionLiquidityPools = append(ingestionLiquidityPools, liquidityPool)
return nil
})
if err != nil {
return false, errors.Wrap(err, "Error loading all liquidity pools for orderbook verification")
}
mismatch := len(liquidityPools) != len(ingestionLiquidityPools)
var liquidityPoolEntryBase64, liquidityPoolRowBase64 string
if !mismatch {
sort.Slice(liquidityPools, func(i, j int) bool {
return processors.PoolIDToString(liquidityPools[i].LiquidityPoolId) <
processors.PoolIDToString(liquidityPools[j].LiquidityPoolId)
})
sort.Slice(ingestionLiquidityPools, func(i, j int) bool {
return ingestionLiquidityPools[i].PoolID < ingestionLiquidityPools[j].PoolID
})
for i, liquidityPoolRow := range ingestionLiquidityPools {
var liquidityPoolRowXDR xdr.LiquidityPoolEntry
var err error
liquidityPoolEntry := liquidityPools[i]
liquidityPoolRowXDR, err = liquidityPoolToXDR(liquidityPoolRow)
if err != nil {
return false, errors.Wrap(err, "Error from converting liquidity pool row to xdr")
}
liquidityPoolEntryBase64, err = o.encodingBuffer.MarshalBase64(&liquidityPoolEntry)
if err != nil {
return false, errors.Wrap(err, "Error from marshalling liquidityPoolEntry")
}
liquidityPoolRowBase64, err = o.encodingBuffer.MarshalBase64(&liquidityPoolRowXDR)
if err != nil {
return false, errors.Wrap(err, "Error from marshalling liquidityPoolRowXDR")
}
if liquidityPoolEntryBase64 != liquidityPoolRowBase64 {
mismatch = true
break
}
}
}
if mismatch {
log.WithField("stream_liquidity_pool", liquidityPoolEntryBase64).
WithField("ingestion_liquidity_pool", liquidityPoolRowBase64).
Error("one or more liquidity pools derived from order book stream does not match liquidity pool from ingestion")
return false, nil
}
log.Info("liquidity pool stream verification succeeded")
return true, nil
}
// Update will query the Horizon DB for offers which have been created, removed, or updated since the
// last time Update() was called. Those changes will then be applied to the in memory order book graph.
// After calling this function, the the in memory order book graph should be consistent with the
// Horizon DB (assuming no error is returned).
func (o *OrderBookStream) Update(ctx context.Context) error {
if err := o.historyQ.BeginTx(&sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead}); err != nil {
return errors.Wrap(err, "Error starting repeatable read transaction")
}
defer o.historyQ.Rollback()
status, err := o.getIngestionStatus(ctx)
if err != nil {
return errors.Wrap(err, "Error obtaining ingestion status")
}
if reset, err := o.update(ctx, status); err != nil {
return errors.Wrap(err, "Error updating")
} else if reset {
return nil
}
// add 15 minute jitter so that not all horizon nodes are calling
// historyQ.StreamAllOffers at the same time
jitter := time.Duration(rand.Int63n(int64(15 * time.Minute)))
requiresVerification := o.lastLedger > 0 &&
time.Since(o.lastVerification) >= verificationFrequency+jitter
if requiresVerification {
offers, pools, err := o.graph.Verify()
if err != nil {
log.WithError(err).
Error("Orderbook graph is not internally consistent")
o.lastVerification = time.Now()
// set last ledger to 0 so that we reset on next update
o.lastLedger = 0
return nil
}
offersOk, err := o.verifyAllOffers(ctx, offers)
if err != nil {
if !isCancelledError(err) {
log.WithError(err).Info("Could not verify offers")
return nil
}
}
liquidityPoolsOK, err := o.verifyAllLiquidityPools(ctx, pools)
if err != nil {
if !isCancelledError(err) {
log.WithError(err).Info("Could not verify liquidity pools")
return nil
}
}
o.lastVerification = time.Now()
if !offersOk || !liquidityPoolsOK {
// set last ledger to 0 so that we reset on next update
o.lastLedger = 0
}
}
return nil
}
// Run will call Update() every 30 seconds until the given context is terminated.
func (o *OrderBookStream) Run(ctx context.Context) {
ticker := time.NewTicker(updateFrequency)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := o.Update(ctx); err != nil && !isCancelledError(err) {
log.WithError(err).Error("could not apply updates from order book stream")
}
case <-ctx.Done():
log.Info("shutting down OrderBookStream")
return
}
}
}