Publish settlement clarifications and atomically clear affected orders

This commit is contained in:
2026-12-03 10:42:35 +08:00
parent fe69e31929
commit 3495e8de3c
12 changed files with 319 additions and 3 deletions
+74
View File
@@ -896,6 +896,8 @@ fn contested_resolution_blocks_both_payout_paths_until_a_verdict() {
assert_eq!(e.execute(decision).unwrap(), finalized);
let resolved = e.execute(status).unwrap();
assert_eq!(resolved["winner"], "NO");
assert_eq!(resolved["payouts"], json!([0, 1]));
assert!(resolved["resolved_at_ms"].is_i64());
assert_eq!(resolved["proposals"][1]["verdict"], "NO");
assert_eq!(resolved["proposals"][0]["challenger"], "bob");
assert_eq!(e.execute(redeem).unwrap()["paid"], 200);
@@ -3163,3 +3165,75 @@ fn midpoint_keeps_half_a_tick_after_reopening_the_book() {
assert_eq!(prices[&tokens[0]].as_deref(), Some("0.08505"));
assert_eq!(prices[&tokens[1]].as_deref(), Some("0.91495"));
}
#[test]
fn published_clarification_clears_only_its_market_and_replays_after_restart() {
let dir = tempdir().unwrap();
let path = dir.path().join("clarifications.db");
let mut e = Exchange::open(&path).unwrap();
setup(&mut e);
for cmd in [
json!({"op":"list","id":"other-list","market":"other","rules":"other result"}),
json!({"op":"open","id":"start","market":"e"}),
order("rest-ask", "alice", "sell", 60, 2),
order("rest-bid", "bob", "buy", 40, 1),
json!({"op":"order","id":"other-bid","market":"other","outcome":"NO","account":"bob","side":"buy","price":10,"qty":1}),
] {
e.execute(cmd).unwrap();
}
let book = json!({"op":"clob_book","token_id":"eb:YES:e"});
let original = e.execute(book.clone()).unwrap();
let first = json!({"op":"publish_clarification","id":"n1","market":"e","content":"Use the official result sheet.","clear_book":false});
assert_eq!(e.execute(first).unwrap()["canceled_count"], 0);
assert_eq!(e.execute(book.clone()).unwrap()["asks"], original["asks"]);
let next = json!({"op":"publish_clarification","id":"n2","market":"e","content":"The result includes overtime.","clear_book":true});
let state = json!({"op":"resolution","market":"e"});
let connection = rusqlite::Connection::open(&path).unwrap();
connection.execute_batch("CREATE TRIGGER reject_clear BEFORE UPDATE OF status ON orders WHEN OLD.market='e' BEGIN SELECT RAISE(ABORT,'simulated release failure'); END;").unwrap();
assert!(e.execute(next.clone()).is_err());
assert_eq!(
e.execute(state.clone()).unwrap()["clarifications"]
.as_array()
.unwrap()
.len(),
1
);
assert_eq!(e.execute(book.clone()).unwrap()["bids"], original["bids"]);
connection
.execute_batch("DROP TRIGGER reject_clear")
.unwrap();
let receipt = e.execute(next.clone()).unwrap();
assert_eq!(receipt["canceled_count"], 2);
assert_eq!(e.execute(book.clone()).unwrap()["asks"], json!([]));
assert_eq!(e.execute(book.clone()).unwrap()["bids"], json!([]));
assert_eq!(
e.execute(json!({"op":"balance","account":"bob"})).unwrap()["available_micros"],
19_900_000
);
let other = e
.execute(json!({"op":"clob_book","token_id":"eb:NO:other"}))
.unwrap();
assert_eq!(other["bids"].as_array().unwrap().len(), 1);
e.execute(order("new-bid", "bob", "buy", 39, 1)).unwrap();
drop(e);
let mut e = Exchange::open(&path).unwrap();
assert_eq!(e.execute(next).unwrap(), receipt);
assert_eq!(
e.execute(book).unwrap()["bids"].as_array().unwrap().len(),
1
);
let resolution = e.execute(state).unwrap();
assert_eq!(resolution["rules"], "official result");
assert!(resolution["payouts"].is_null());
assert!(resolution["resolved_at_ms"].is_null());
assert_eq!(resolution["clarifications"][1]["clearBook"], true);
assert!(resolution["clarifications"][1]["txTimestamp"].is_null());
e.execute(json!({"op":"close","id":"end","market":"e"}))
.unwrap();
e.execute(json!({"op":"finalize","id":"void","market":"e","winner":"VOID"}))
.unwrap();
let settled = e.execute(json!({"op":"resolution","market":"e"})).unwrap();
assert_eq!(settled["payouts"], json!([1, 1]));
assert_eq!(settled["clarifications"], resolution["clarifications"]);
assert!(e.execute(json!({"op":"publish_clarification","id":"late","market":"e","content":"late note","clear_book":false})).is_err());
}