Keep per-instrument order books in memory and recover committed orders
This commit is contained in:
@@ -132,3 +132,91 @@ fn market_outcome_and_limit_are_isolated() {
|
||||
}
|
||||
assert_eq!(state(&mut e)["fills"], json!([]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reboot_restores_partial_fifo_and_excludes_cancelled_orders() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("market.db");
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
setup(&mut e);
|
||||
e.execute(order("ask-first", "alice", "sell", 40, 4))
|
||||
.unwrap();
|
||||
e.execute(order("ask-second", "alice", "sell", 40, 3))
|
||||
.unwrap();
|
||||
e.execute(order("cancel-me", "alice", "sell", 30, 1))
|
||||
.unwrap();
|
||||
e.execute(json!({"id":"cancel","op":"cancel","account":"alice","order":3}))
|
||||
.unwrap();
|
||||
e.execute(order("bid", "bob", "buy", 40, 2)).unwrap();
|
||||
drop(e);
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
e.execute(json!({"id":"open","op":"open","market":"e"}))
|
||||
.unwrap();
|
||||
drop(e);
|
||||
// A database from the original version has live orders but no revision index.
|
||||
let legacy = rusqlite::Connection::open(&path).unwrap();
|
||||
legacy
|
||||
.execute_batch(
|
||||
"DROP TRIGGER book_insert; DROP TRIGGER book_update; DROP TABLE book_changes;",
|
||||
)
|
||||
.unwrap();
|
||||
drop(legacy);
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
e.execute(order("sweep", "bob", "buy", 40, 4)).unwrap();
|
||||
let s = state(&mut e);
|
||||
assert_eq!(
|
||||
s["fills"],
|
||||
json!([
|
||||
{"seq":1,"ask":1,"bid":4,"price":40,"quantity":2},
|
||||
{"seq":2,"ask":1,"bid":5,"price":40,"quantity":2},
|
||||
{"seq":3,"ask":2,"bid":5,"price":40,"quantity":2}
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_resident_books_follow_partial_fill_and_cancel() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("market.db");
|
||||
let mut a = Exchange::open(&path).unwrap();
|
||||
setup(&mut a);
|
||||
let mut b = Exchange::open(&path).unwrap();
|
||||
a.execute(json!({"id":"open","op":"open","market":"e"}))
|
||||
.unwrap();
|
||||
a.execute(order("maker", "alice", "sell", 55, 6)).unwrap();
|
||||
assert_eq!(
|
||||
b.execute(order("taker", "bob", "buy", 55, 4)).unwrap()["filled"],
|
||||
4
|
||||
);
|
||||
assert_eq!(
|
||||
a.execute(json!({"id":"cancel","op":"cancel","order":1,"account":"alice"}))
|
||||
.unwrap()["cancelled"],
|
||||
2
|
||||
);
|
||||
assert_eq!(
|
||||
b.execute(order("next", "bob", "buy", 55, 1)).unwrap()["filled"],
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_receipt_discards_planned_fill() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("market.db");
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
setup(&mut e);
|
||||
e.execute(json!({"id":"open","op":"open","market":"e"}))
|
||||
.unwrap();
|
||||
e.execute(order("ask", "alice", "sell", 55, 3)).unwrap();
|
||||
let db = rusqlite::Connection::open(&path).unwrap();
|
||||
db.execute_batch("CREATE TRIGGER reject_receipt BEFORE INSERT ON requests WHEN NEW.id='retry' BEGIN SELECT RAISE(ABORT,'injected'); END;").unwrap();
|
||||
let bid = order("retry", "bob", "buy", 55, 3);
|
||||
let before = state(&mut e);
|
||||
assert!(e.execute(bid.clone()).is_err());
|
||||
assert_eq!(state(&mut e), before);
|
||||
db.execute_batch("DROP TRIGGER reject_receipt").unwrap();
|
||||
let reply = e.execute(bid.clone()).unwrap();
|
||||
assert_eq!(reply["filled"], 3);
|
||||
assert_eq!(e.execute(bid).unwrap(), reply);
|
||||
assert_eq!(state(&mut e)["fills"].as_array().unwrap().len(), 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user