orderbook: Check assignment of time values and reject if not set (#1318)

* orderbook: Check assignment of time values and reject if not set.

* linter: fix

* buffer: additional linter winter fixter

* Implement through pending exchanges

* finished push

* linty: minty

* gomod: tidy

* thrasher: nits

* glorious: nits

* orderbook: purge type now in favour of external call allocation

* orderbook: push last param

* orderbook: only 1 unlock call is needed

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2023-09-07 11:00:16 +10:00
committed by GitHub
parent 20143886ca
commit ad9de19d47
31 changed files with 726 additions and 327 deletions

View File

@@ -41,6 +41,7 @@ func createSnapshot() (holder *Orderbook, asks, bids orderbook.Items, err error)
Asset: asset.Spot,
Pair: cp,
PriceDuplication: true,
LastUpdated: time.Now(),
}
newBook := make(map[Key]*orderbookHolder)
@@ -93,7 +94,10 @@ func BenchmarkUpdateBidsByPrice(b *testing.B) {
Asset: asset.Spot,
}
holder := ob.ob[Key{Base: cp.Base.Item, Quote: cp.Quote.Item, Asset: asset.Spot}]
holder.updateByPrice(update)
err = holder.updateByPrice(update)
if err != nil {
b.Fatal(err)
}
}
}
@@ -113,7 +117,10 @@ func BenchmarkUpdateAsksByPrice(b *testing.B) {
Asset: asset.Spot,
}
holder := ob.ob[Key{Base: cp.Base.Item, Quote: cp.Quote.Item, Asset: asset.Spot}]
holder.updateByPrice(update)
err = holder.updateByPrice(update)
if err != nil {
b.Fatal(err)
}
}
}
@@ -240,7 +247,7 @@ func TestUpdates(t *testing.T) {
}
book := holder.ob[Key{Base: cp.Base.Item, Quote: cp.Quote.Item, Asset: asset.Spot}]
book.updateByPrice(&orderbook.Update{
err = book.updateByPrice(&orderbook.Update{
Bids: itemArray[5],
Asks: itemArray[5],
Pair: cp,
@@ -251,7 +258,7 @@ func TestUpdates(t *testing.T) {
t.Error(err)
}
book.updateByPrice(&orderbook.Update{
err = book.updateByPrice(&orderbook.Update{
Bids: itemArray[0],
Asks: itemArray[0],
Pair: cp,
@@ -375,11 +382,12 @@ func TestSortIDs(t *testing.T) {
asks := itemArray[i]
bids := itemArray[i]
err = holder.Update(&orderbook.Update{
Bids: bids,
Asks: asks,
Pair: cp,
UpdateID: int64(i),
Asset: asset.Spot,
Bids: bids,
Asks: asks,
Pair: cp,
UpdateID: int64(i),
Asset: asset.Spot,
UpdateTime: time.Now(),
})
if err != nil {
t.Fatal(err)
@@ -420,10 +428,11 @@ func TestOutOfOrderIDs(t *testing.T) {
for i := range itemArray {
asks := itemArray[i]
err = holder.Update(&orderbook.Update{
Asks: asks,
Pair: cp,
UpdateID: outOFOrderIDs[i],
Asset: asset.Spot,
Asks: asks,
Pair: cp,
UpdateID: outOFOrderIDs[i],
Asset: asset.Spot,
UpdateTime: time.Now(),
})
if err != nil {
t.Fatal(err)
@@ -454,10 +463,11 @@ func TestOrderbookLastUpdateID(t *testing.T) {
// this update invalidates the book
err = holder.Update(&orderbook.Update{
Asks: []orderbook.Item{{Price: 999999}},
Pair: cp,
UpdateID: -1,
Asset: asset.Spot,
Asks: []orderbook.Item{{Price: 999999}},
Pair: cp,
UpdateID: -1,
Asset: asset.Spot,
UpdateTime: time.Now(),
})
if !errors.Is(err, orderbook.ErrOrderbookInvalid) {
t.Fatalf("received: %v but expected: %v", err, orderbook.ErrOrderbookInvalid)
@@ -474,10 +484,11 @@ func TestOrderbookLastUpdateID(t *testing.T) {
for i := range itemArray {
asks := itemArray[i]
err = holder.Update(&orderbook.Update{
Asks: asks,
Pair: cp,
UpdateID: int64(i) + 1,
Asset: asset.Spot,
Asks: asks,
Pair: cp,
UpdateID: int64(i) + 1,
Asset: asset.Spot,
UpdateTime: time.Now(),
})
if err != nil {
t.Fatal(err)
@@ -566,6 +577,7 @@ func TestRunSnapshotWithNoData(t *testing.T) {
snapShot1.Pair = cp
snapShot1.Exchange = "test"
obl.exchangeName = "test"
snapShot1.LastUpdated = time.Now()
err := obl.LoadSnapshot(&snapShot1)
if err != nil {
t.Fatal(err)
@@ -590,6 +602,7 @@ func TestLoadSnapshot(t *testing.T) {
snapShot1.Bids = bids
snapShot1.Asset = asset.Spot
snapShot1.Pair = cp
snapShot1.LastUpdated = time.Now()
err := obl.LoadSnapshot(&snapShot1)
if err != nil {
t.Error(err)
@@ -651,6 +664,7 @@ func TestInsertingSnapShots(t *testing.T) {
snapShot1.Bids = bids
snapShot1.Asset = asset.Spot
snapShot1.Pair = cp
snapShot1.LastUpdated = time.Now()
err := holder.LoadSnapshot(&snapShot1)
if err != nil {
t.Fatal(err)
@@ -694,6 +708,7 @@ func TestInsertingSnapShots(t *testing.T) {
if err != nil {
t.Fatal(err)
}
snapShot2.LastUpdated = time.Now()
err = holder.LoadSnapshot(&snapShot2)
if err != nil {
t.Fatal(err)
@@ -737,6 +752,7 @@ func TestInsertingSnapShots(t *testing.T) {
if err != nil {
t.Fatal(err)
}
snapShot3.LastUpdated = time.Now()
err = holder.LoadSnapshot(&snapShot3)
if err != nil {
t.Fatal(err)
@@ -873,7 +889,7 @@ func TestEnsureMultipleUpdatesViaPrice(t *testing.T) {
asks := bidAskGenerator()
book := holder.ob[Key{Base: cp.Base.Item, Quote: cp.Quote.Item, Asset: asset.Spot}]
book.updateByPrice(&orderbook.Update{
err = book.updateByPrice(&orderbook.Update{
Bids: asks,
Asks: asks,
Pair: cp,
@@ -916,7 +932,10 @@ func TestUpdateByIDAndAction(t *testing.T) {
t.Fatal(err)
}
book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Time{}, true)
err = book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Now(), true)
if err != nil {
t.Fatal(err)
}
ob, err := book.Retrieve()
if !errors.Is(err, nil) {
@@ -948,7 +967,10 @@ func TestUpdateByIDAndAction(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", err, errAmendFailure)
}
book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Time{}, true)
err = book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Now(), true)
if err != nil {
t.Fatal(err)
}
// append to slice
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.UpdateInsert,
@@ -966,6 +988,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
Amount: 1,
},
},
UpdateTime: time.Now(),
})
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
@@ -1000,6 +1023,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
Amount: 100,
},
},
UpdateTime: time.Now(),
})
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
@@ -1035,6 +1059,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
Amount: 99,
},
},
UpdateTime: time.Now(),
})
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
@@ -1053,7 +1078,10 @@ func TestUpdateByIDAndAction(t *testing.T) {
t.Fatal("did not adjust ask item placement and details")
}
book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Time{}, true) //nolint:gocritic
err = book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Now(), true) //nolint:gocritic
if err != nil {
t.Fatal(err)
}
// Delete - not found
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.Delete,
@@ -1069,13 +1097,17 @@ func TestUpdateByIDAndAction(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", err, errDeleteFailure)
}
book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Time{}, true) //nolint:gocritic
err = book.LoadSnapshot(append(bids[:0:0], bids...), append(asks[:0:0], asks...), 0, time.Now(), true) //nolint:gocritic
if err != nil {
t.Fatal(err)
}
// Delete - found
err = holder.updateByIDAndAction(&orderbook.Update{
Action: orderbook.Delete,
Asks: []orderbook.Item{
asks[0],
},
UpdateTime: time.Now(),
})
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
@@ -1101,7 +1133,10 @@ func TestUpdateByIDAndAction(t *testing.T) {
t.Fatalf("received: '%v' but expected: '%v'", err, errAmendFailure)
}
book.LoadSnapshot(bids, bids, 0, time.Time{}, true)
err = book.LoadSnapshot(bids, bids, 0, time.Now(), true)
if err != nil {
t.Fatal(err)
}
ob, err = book.Retrieve()
if !errors.Is(err, nil) {
@@ -1119,6 +1154,7 @@ func TestUpdateByIDAndAction(t *testing.T) {
Asks: []orderbook.Item{
update,
},
UpdateTime: time.Now(),
})
if err != nil {
t.Fatal(err)
@@ -1154,6 +1190,7 @@ func TestFlushOrderbook(t *testing.T) {
snapShot1.Bids = bids
snapShot1.Asset = asset.Spot
snapShot1.Pair = cp
snapShot1.LastUpdated = time.Now()
err = w.FlushOrderbook(cp, asset.Spot)
if err == nil {