Cancel outstanding orders when their trading key misses its heartbeat

This commit is contained in:
2026-12-03 05:57:49 +08:00
parent d01ab8f15c
commit 2306c6fd96
14 changed files with 296 additions and 9 deletions
+94
View File
@@ -1907,3 +1907,97 @@ fn fees_follow_taker_cash_budget_and_net_sale_proceeds() {
.iter()
.all(|r| r["fee_micros"] == 0));
}
#[test]
fn heartbeat_rotates_and_recovers_only_orders_owned_by_the_expired_key() {
let dir = tempdir().unwrap();
let path = dir.path().join("heartbeats.db");
let mut e = Exchange::open(&path).unwrap();
setup(&mut e);
e.execute(json!({"op":"list","id":"second","market":"other","rules":"r"}))
.unwrap();
let mut a = order("ask", "alice", "sell", 60, 4);
a["key_id"] = json!("bot");
e.execute(a).unwrap();
let mut b = order("bid", "alice", "buy", 30, 2);
b["market"] = json!("other");
b["key_id"] = json!("bot");
e.execute(b).unwrap();
let base = order("default", "alice", "buy", 10, 1);
e.execute(base).unwrap();
let mut manual = order("manual", "alice", "buy", 11, 1);
manual["key_id"] = json!("manual");
e.execute(manual).unwrap();
let mut bob = order("bob-bid", "bob", "buy", 12, 1);
bob["key_id"] = json!("bot");
e.execute(bob).unwrap();
let beat = |account: &str, id: &str| json!({"op":"heartbeat","account":account,"key_id":"bot","heartbeat_id":id});
let first = e.execute(beat("alice", "")).unwrap();
let first_id = first["heartbeat_id"].as_str().unwrap();
let second = e.execute(beat("alice", first_id)).unwrap();
let second_id = second["heartbeat_id"].as_str().unwrap();
assert_ne!(first_id, second_id);
assert_eq!(
e.execute(beat("alice", first_id)).unwrap(),
json!({"error_msg":"Invalid Heartbeat ID","heartbeat_id":second_id})
);
let observer = rusqlite::Connection::open(&path).unwrap();
let deadline: i64 = observer
.query_row(
"SELECT deadline_ms FROM order_heartbeats WHERE account='alice'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(second["deadline_ms"], deadline);
e.execute(beat("bob", "")).unwrap();
observer
.execute(
"UPDATE order_heartbeats SET deadline_ms=0 WHERE account='alice'",
[],
)
.unwrap();
drop(e);
let mut e = Exchange::open(&path).unwrap();
let s = state(&mut e);
let statuses: Vec<_> = s["orders"]
.as_array()
.unwrap()
.iter()
.map(|o| o["status"].as_str().unwrap())
.collect();
assert_eq!(statuses, ["cancelled", "cancelled", "live", "live", "live"]);
assert_eq!(s["accounts"][0]["cash"], 979);
assert_eq!(
s["positions"]
.as_array()
.unwrap()
.iter()
.find(|p| p["outcome"] == "YES")
.unwrap()["quantity"],
10
);
assert_eq!(
e.execute(beat("alice", second_id)).unwrap()["heartbeat_id"],
""
);
let mut later = order("later", "alice", "buy", 10, 1);
later["key_id"] = json!("bot");
e.execute(later).unwrap();
e.maintain().unwrap();
assert_eq!(state(&mut e)["orders"][5]["status"], "live");
e.execute(json!({"op":"heartbeat","account":"alice","heartbeat_id":""}))
.unwrap();
observer
.execute(
"UPDATE order_heartbeats SET deadline_ms=0 WHERE account='alice' AND key_id=''",
[],
)
.unwrap();
e.maintain().unwrap();
let s = state(&mut e);
assert_eq!(s["orders"][2]["status"], "cancelled");
assert_eq!(s["orders"][3]["status"], "live");
assert_eq!(s["orders"][5]["status"], "live");
}