-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbond_resolution.rs
More file actions
263 lines (231 loc) · 8.96 KB
/
Copy pathbond_resolution.rs
File metadata and controls
263 lines (231 loc) · 8.96 KB
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
//! Bond slash choices for admin dispute finalization (`admin-settle` / `admin-cancel`).
//!
//! See [admin settle](https://mostro.network/protocol/admin_settle_order.html) and
//! [admin cancel](https://mostro.network/protocol/admin_cancel_order.html).
use mostro_core::prelude::*;
/// How to resolve anti-abuse bonds when an admin finalizes a dispute.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BondSlashChoice {
/// Release bonds; do not slash either party.
None,
/// Slash the buyer's bond (if posted).
SlashBuyer,
/// Slash the seller's bond (if posted).
SlashSeller,
/// Slash both parties' bonds (if posted).
SlashBoth,
}
// Default for bond slash choice is None
impl Default for BondSlashChoice {
fn default() -> Self {
Self::None
}
}
// BondSlashChoice implementation
impl BondSlashChoice {
/// All choices in UI display order.
pub const ALL: [Self; 4] = [
Self::None,
Self::SlashBuyer,
Self::SlashSeller,
Self::SlashBoth,
];
/// Index in [`Self::ALL`] for TUI list selection.
pub fn choice_index(self) -> usize {
Self::ALL.iter().position(|c| *c == self).unwrap_or(0)
}
/// Choice at `index` in [`Self::ALL`], or [`Self::None`] if out of range.
pub fn from_choice_index(index: usize) -> Self {
Self::ALL.get(index).copied().unwrap_or(Self::None)
}
/// Human-readable label for TUI display (includes choice emoji).
pub fn label(self) -> &'static str {
match self {
Self::None => "🔓 No bond slash",
Self::SlashBuyer => "⚔️ Slash buyer bond",
Self::SlashSeller => "⚔️ Slash seller bond",
Self::SlashBoth => "⚔️ Slash both bonds",
}
}
/// Returns `true` if the seller's bond should be slashed.
pub fn slash_seller(self) -> bool {
matches!(self, Self::SlashSeller | Self::SlashBoth)
}
/// Returns `true` if the buyer's bond should be slashed.
pub fn slash_buyer(self) -> bool {
matches!(self, Self::SlashBuyer | Self::SlashBoth)
}
pub(crate) fn to_bond_resolution(self) -> BondResolution {
BondResolution {
slash_seller: self.slash_seller(),
slash_buyer: self.slash_buyer(),
}
}
/// Explicit [`Payload::BondResolution`] (including `{ slash_seller: false, slash_buyer: false }`
/// for [`Self::None`]). Prefer [`Self::to_optional_payload`] for wire messages.
pub fn to_payload(self) -> Payload {
Payload::BondResolution(self.to_bond_resolution())
}
/// Wire payload for `admin-settle` / `admin-cancel`.
///
/// [`Self::None`] → `None` (`payload: null`, legacy no-slash). Slash variants →
/// `Some(Payload::BondResolution(..))`.
pub fn to_optional_payload(self) -> Option<Payload> {
match self {
Self::None => None,
_ => Some(self.to_payload()),
}
}
/// Short phrase for admin finalization log lines.
pub fn log_context(self) -> &'static str {
match self {
Self::None => "no bond slash",
Self::SlashBuyer => "slash buyer bond",
Self::SlashSeller => "slash seller bond",
Self::SlashBoth => "slash both bonds",
}
}
/// Multi-line text for the post-finalization success popup (`OperationResult::Info`).
pub fn finalize_success_message(self, dispute_id: uuid::Uuid, is_settle: bool) -> String {
let outcome = if is_settle {
"Admin settle — buyer paid"
} else {
"Admin cancel — seller refunded"
};
format!(
"Dispute finalized\n\nOutcome:\n{outcome}\n\nBond:\n{}\n\nDispute ID:\n{dispute_id}",
self.label(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use uuid::uuid;
fn dispute_message(action: Action, bond: BondSlashChoice) -> Message {
Message::new_dispute(
Some(uuid!("308e1272-d5f4-47e6-bd97-3504baea9c23")),
None,
None,
action,
bond.to_optional_payload(),
)
}
fn bond_resolution_from_message(msg: &Message) -> BondResolution {
match msg {
Message::Dispute(kind) => match &kind.payload {
Some(Payload::BondResolution(b)) => b.clone(),
other => panic!("expected BondResolution payload, got {other:?}"),
},
other => panic!("expected Dispute message, got {other:?}"),
}
}
#[test]
fn finalize_success_message_is_multiline() {
let dispute_id = uuid!("8397bc78-7c98-4b4f-bb49-40c7101391b0");
let msg = BondSlashChoice::SlashBuyer.finalize_success_message(dispute_id, false);
assert!(msg.contains("Dispute finalized"));
assert!(msg.contains("Admin cancel — seller refunded"));
assert!(msg.contains("⚔️ Slash buyer bond"));
assert!(msg.contains(&dispute_id.to_string()));
assert!(msg.contains('\n'));
}
#[test]
fn choice_index_roundtrip() {
for (i, choice) in BondSlashChoice::ALL.iter().enumerate() {
assert_eq!(choice.choice_index(), i);
assert_eq!(BondSlashChoice::from_choice_index(i), *choice);
}
}
#[test]
fn slash_flags_match_choice() {
assert!(!BondSlashChoice::None.slash_seller());
assert!(!BondSlashChoice::None.slash_buyer());
assert!(!BondSlashChoice::SlashBuyer.slash_seller());
assert!(BondSlashChoice::SlashBuyer.slash_buyer());
assert!(BondSlashChoice::SlashSeller.slash_seller());
assert!(!BondSlashChoice::SlashSeller.slash_buyer());
assert!(BondSlashChoice::SlashBoth.slash_seller());
assert!(BondSlashChoice::SlashBoth.slash_buyer());
}
#[test]
fn admin_settle_and_cancel_verify_with_bond_resolution() {
for action in [Action::AdminSettle, Action::AdminCancel] {
for bond in BondSlashChoice::ALL {
let msg = dispute_message(action.clone(), bond);
assert!(msg.verify(), "{action:?} + {bond:?} should verify");
}
}
}
#[test]
fn bond_resolution_wire_format_admin_settle() {
let msg = dispute_message(Action::AdminSettle, BondSlashChoice::SlashBuyer);
let json = msg.as_json().expect("serialize");
assert!(
json.contains("\"bond_resolution\""),
"expected snake_case discriminator, got: {json}"
);
assert!(json.contains("\"slash_seller\":false"));
assert!(json.contains("\"slash_buyer\":true"));
assert!(json.contains("\"action\":\"admin-settle\""));
let decoded = Message::from_json(&json).expect("deserialize");
assert!(decoded.verify());
let b = bond_resolution_from_message(&decoded);
assert!(!b.slash_seller);
assert!(b.slash_buyer);
}
#[test]
fn bond_resolution_wire_format_admin_cancel_slash_seller() {
let msg = dispute_message(Action::AdminCancel, BondSlashChoice::SlashSeller);
let json = msg.as_json().expect("serialize");
assert!(json.contains("\"action\":\"admin-cancel\""));
assert!(json.contains("\"slash_seller\":true"));
assert!(json.contains("\"slash_buyer\":false"));
let decoded = Message::from_json(&json).expect("deserialize");
assert!(decoded.verify());
}
#[test]
fn bond_resolution_none_omits_payload() {
let msg = dispute_message(Action::AdminSettle, BondSlashChoice::None);
assert!(msg.verify());
let json = msg.as_json().expect("serialize");
assert!(
json.contains("\"payload\":null"),
"None should serialize as legacy null payload, got: {json}"
);
assert!(
!json.contains("bond_resolution"),
"None must not emit bond_resolution, got: {json}"
);
}
#[test]
fn to_payload_none_is_explicit_false_false() {
let payload = BondSlashChoice::None.to_payload();
let BondResolution {
slash_seller,
slash_buyer,
} = match payload {
Payload::BondResolution(b) => b,
other => panic!("expected BondResolution, got {other:?}"),
};
assert!(!slash_seller);
assert!(!slash_buyer);
}
#[test]
fn bond_resolution_wire_format_slash_both() {
let msg = dispute_message(Action::AdminSettle, BondSlashChoice::SlashBoth);
let json = msg.as_json().expect("serialize");
assert!(
json.contains("\"bond_resolution\""),
"expected snake_case discriminator, got: {json}"
);
assert!(json.contains("\"slash_seller\":true"));
assert!(json.contains("\"slash_buyer\":true"));
assert!(json.contains("\"action\":\"admin-settle\""));
let decoded = Message::from_json(&json).expect("deserialize");
assert!(decoded.verify());
let b = bond_resolution_from_message(&decoded);
assert!(b.slash_seller);
assert!(b.slash_buyer);
}
}