forked from stellar/go
/
orderbook.go
279 lines (242 loc) · 8.49 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
package ingest
import (
"context"
"database/sql"
"math/rand"
"sort"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stellar/go/exp/orderbook"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/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
}
// 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(),
}
}
type ingestionStatus struct {
HistoryConsistentWithState bool
StateInvalid bool
LastIngestedLedger uint32
LastOfferCompactionLedger uint32
}
func (o *OrderBookStream) getIngestionStatus() (ingestionStatus, error) {
var status ingestionStatus
var err error
status.StateInvalid, err = o.historyQ.GetExpStateInvalid()
if err != nil {
return status, errors.Wrap(err, "Error from GetExpStateInvalid")
}
var lastHistoryLedger uint32
lastHistoryLedger, err = o.historyQ.GetLatestLedger()
if err != nil {
return status, errors.Wrap(err, "Error from GetLatestLedger")
}
status.LastIngestedLedger, err = o.historyQ.GetLastLedgerIngestNonBlocking()
if err != nil {
return status, errors.Wrap(err, "Error from GetLastLedgerIngestNonBlocking")
}
status.LastOfferCompactionLedger, err = o.historyQ.GetOfferCompactionSequence()
if err != nil {
return status, errors.Wrap(err, "Error from GetOfferCompactionSequence")
}
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
}
func addOfferToGraph(graph orderbook.OBGraph, offer history.Offer) {
sellerID := xdr.MustAddress(offer.SellerID)
graph.AddOffer(xdr.OfferEntry{
SellerId: sellerID,
OfferId: xdr.Int64(offer.OfferID),
Selling: offer.SellingAsset,
Buying: offer.BuyingAsset,
Amount: xdr.Int64(offer.Amount),
Price: xdr.Price{
N: xdr.Int32(offer.Pricen),
D: xdr.Int32(offer.Priced),
},
Flags: xdr.Uint32(offer.Flags),
})
}
// update returns true if the order book graph was reset
func (o *OrderBookStream) update(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
}
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()
offers, err := o.historyQ.GetAllOffers()
if err != nil {
return true, errors.Wrap(err, "Error from GetAllOffers")
}
for _, offer := range offers {
addOfferToGraph(o.graph, offer)
}
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(o.lastLedger)
if err != nil {
return false, errors.Wrap(err, "Error from GetUpdatedOffers")
}
for _, offer := range offers {
if offer.Deleted {
o.graph.RemoveOffer(xdr.Int64(offer.OfferID))
} else {
addOfferToGraph(o.graph, offer)
}
}
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() {
offers := o.graph.Offers()
ingestionOffers, err := o.historyQ.GetAllOffers()
if err != nil {
if !isCancelledError(err) {
log.WithError(err).Info("Could not verify offers because of error from GetAllOffers")
}
return
}
o.lastVerification = time.Now()
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]
if xdr.Int64(offerRow.OfferID) != offerEntry.OfferId ||
xdr.Int64(offerRow.Amount) != offerEntry.Amount ||
offerRow.Priced != int32(offerEntry.Price.D) ||
offerRow.Pricen != int32(offerEntry.Price.N) ||
!offerRow.BuyingAsset.Equals(offerEntry.Buying) ||
!offerRow.SellingAsset.Equals(offerEntry.Selling) ||
offerRow.SellerID != offerEntry.SellerId.Address() {
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")
// set last ledger to 0 so that we reset on next update
o.lastLedger = 0
} else {
log.Info("order book stream verification succeeded")
}
}
// 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() 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()
if err != nil {
return errors.Wrap(err, "Error obtaining ingestion status")
}
if reset, err := o.update(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.GetAllOffers 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 {
o.verifyAllOffers()
}
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(); 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
}
}
}