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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! LookupService trait — indexes admitted outputs and answers lookup queries.
//!
//! Each lookup service in the overlay is notified when outputs are admitted or
//! spent, and responds to queries from clients. The Engine calls lifecycle hooks
//! during submit() and delegates query handling to lookup().
//!
//! Ported from `~/bsv/overlay-services/src/LookupService.ts`.
use async_trait::async_trait;
use crate::types::{
AdmissionMode, LookupQuestion, LookupResult, OutputAdmittedByTopic, OutputSpent,
ServiceMetadata, SpendNotificationMode,
};
#[cfg(test)]
use crate::types::UTXOReference;
/// Indexes overlay outputs and answers lookup queries.
///
/// Implementations handle specific types of data — e.g., SHIPLookupService
/// indexes SHIP advertisements and answers "which nodes host topic X?".
///
/// The Engine calls lifecycle hooks (`output_admitted_by_topic`, `output_spent`,
/// etc.) during transaction processing, and `lookup()` when clients query.
#[async_trait(?Send)]
pub trait LookupService {
/// How the Engine should deliver data when an output is admitted.
/// - `LockingScript`: provides txid, outputIndex, topic, satoshis, locking script
/// - `WholeTx`: provides the entire Atomic BEEF
fn admission_mode(&self) -> AdmissionMode;
/// How the Engine should notify when an output is spent.
/// - `None`: no notification
/// - `Txid`: just the spending txid
/// - `Script`: spending txid + unlocking script + input details
/// - `WholeTx`: entire spending transaction as Atomic BEEF
fn spend_notification_mode(&self) -> SpendNotificationMode;
/// Whether this service READS the off-chain values of what it is told
/// of in `topic` (`output_admitted_by_topic`, `output_spent`; lane E1D's
/// delta fold, L1). The door lands a carried predecessor first with NO
/// off-chain values (the submit carries only the successor's), and the
/// predecessor's own submit is a dupe after that, so the values are
/// never told: the engine lands nothing first into a topic for which
/// any lookup service answers `true` (the successor waits for the
/// predecessor's own submit). Default `true`, so a service that does
/// not say never loses a value; one that never reads them answers
/// `false`.
fn reads_off_chain_values(&self, _topic: &str) -> bool {
true
}
// ========================================================================
// Lifecycle hooks (called by Engine during submit)
// ========================================================================
/// Called when a Topic Manager admits a new UTXO.
///
/// The lookup service should index this output so it can be found via `lookup()`.
/// Payload shape depends on `admission_mode()`.
async fn output_admitted_by_topic(
&self,
payload: &OutputAdmittedByTopic,
) -> Result<(), LookupServiceError>;
/// Called when a previously-admitted UTXO is spent.
///
/// Payload shape depends on `spend_notification_mode()`.
/// Default: no-op (for services with SpendNotificationMode::None).
async fn output_spent(&self, _payload: &OutputSpent) -> Result<(), LookupServiceError> {
Ok(())
}
/// Called when an output is permanently evicted from the overlay.
///
/// The lookup service MUST remove this output from all indices.
/// After eviction, the output MUST NOT appear in any future lookup answers.
async fn output_evicted(&self, txid: &str, output_index: u32)
-> Result<(), LookupServiceError>;
/// Called when historical retention of an output is no longer needed.
///
/// Default: no-op.
async fn output_no_longer_retained_in_history(
&self,
_txid: &str,
_output_index: u32,
_topic: &str,
) -> Result<(), LookupServiceError> {
Ok(())
}
// ========================================================================
// Query API
// ========================================================================
/// Answer a lookup query.
///
/// Returns a [`LookupResult`] in one of two shapes:
///
/// - `LookupResult::OutputList(refs)` — common case. The Engine
/// hydrates each `UTXOReference` with the BEEF from storage and
/// assembles a `LookupAnswer::OutputList`. This is the cheap path:
/// services don't need direct access to BEEF storage.
///
/// - `LookupResult::Answer(answer)` — escape hatch. Services that
/// return aggregate stats (`Freeform`) or formula chains
/// (`Formula`) emit the full answer themselves; the Engine passes
/// it through verbatim.
///
/// Existing impls that returned `Vec<UTXOReference>` migrate
/// mechanically by wrapping in `LookupResult::OutputList(refs)` (or
/// using the `From<Vec<UTXOReference>>` impl).
async fn lookup(&self, question: &LookupQuestion) -> Result<LookupResult, LookupServiceError>;
// ========================================================================
// Documentation
// ========================================================================
/// Return Markdown documentation for this lookup service.
async fn get_documentation(&self) -> String;
/// Return metadata identifying this lookup service.
async fn get_metadata(&self) -> ServiceMetadata;
}
/// Errors from LookupService operations.
#[derive(Debug, thiserror::Error)]
pub enum LookupServiceError {
/// The query is malformed or missing required fields.
#[error("invalid query: {0}")]
InvalidQuery(String),
/// Storage operation failed.
#[error("storage error: {0}")]
StorageError(String),
/// The service does not support this query type.
#[error("unsupported query: {0}")]
Unsupported(String),
/// Generic error.
#[error("{0}")]
Other(String),
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
/// Mock lookup service that stores references in memory.
struct MockLookupService {
records: std::sync::Mutex<Vec<UTXOReference>>,
}
impl MockLookupService {
fn new() -> Self {
Self {
records: std::sync::Mutex::new(Vec::new()),
}
}
fn record_count(&self) -> usize {
self.records.lock().unwrap().len()
}
}
#[async_trait(?Send)]
impl LookupService for MockLookupService {
fn admission_mode(&self) -> AdmissionMode {
AdmissionMode::LockingScript
}
fn spend_notification_mode(&self) -> SpendNotificationMode {
SpendNotificationMode::None
}
async fn output_admitted_by_topic(
&self,
payload: &OutputAdmittedByTopic,
) -> Result<(), LookupServiceError> {
let (txid, output_index) = match payload {
OutputAdmittedByTopic::LockingScript {
txid, output_index, ..
} => (txid.clone(), *output_index),
OutputAdmittedByTopic::WholeTx { output_index, .. } => {
("whole-tx".to_string(), *output_index)
}
};
self.records
.lock()
.unwrap()
.push(UTXOReference { txid, output_index });
Ok(())
}
async fn output_evicted(
&self,
txid: &str,
output_index: u32,
) -> Result<(), LookupServiceError> {
self.records
.lock()
.unwrap()
.retain(|r| !(r.txid == txid && r.output_index == output_index));
Ok(())
}
async fn lookup(
&self,
_question: &LookupQuestion,
) -> Result<LookupResult, LookupServiceError> {
Ok(LookupResult::OutputList(
self.records.lock().unwrap().clone(),
))
}
async fn get_documentation(&self) -> String {
"Mock lookup service for testing.".to_string()
}
async fn get_metadata(&self) -> ServiceMetadata {
ServiceMetadata {
name: "mock-lookup".to_string(),
description: Some("Test lookup service".to_string()),
..Default::default()
}
}
}
#[tokio::test]
async fn test_output_admitted_and_lookup() {
let svc = MockLookupService::new();
let payload = OutputAdmittedByTopic::LockingScript {
txid: "abc".to_string(),
output_index: 0,
topic: "tm_test".to_string(),
satoshis: 1000,
locking_script: vec![0x76],
off_chain_values: None,
};
svc.output_admitted_by_topic(&payload).await.unwrap();
assert_eq!(svc.record_count(), 1);
let question = LookupQuestion::new("ls_test", serde_json::json!({}));
let result = svc.lookup(&question).await.unwrap();
let LookupResult::OutputList(refs) = result else {
panic!("expected OutputList");
};
assert_eq!(refs.len(), 1);
assert_eq!(refs[0].txid, "abc");
assert_eq!(refs[0].output_index, 0);
}
#[tokio::test]
async fn test_output_evicted() {
let svc = MockLookupService::new();
let payload = OutputAdmittedByTopic::LockingScript {
txid: "abc".to_string(),
output_index: 0,
topic: "tm_test".to_string(),
satoshis: 1000,
locking_script: vec![],
off_chain_values: None,
};
svc.output_admitted_by_topic(&payload).await.unwrap();
assert_eq!(svc.record_count(), 1);
svc.output_evicted("abc", 0).await.unwrap();
assert_eq!(svc.record_count(), 0);
// Lookup should return empty
let question = LookupQuestion::new("ls_test", serde_json::json!({}));
let result = svc.lookup(&question).await.unwrap();
let LookupResult::OutputList(refs) = result else {
panic!("expected OutputList");
};
assert!(refs.is_empty());
}
#[tokio::test]
async fn test_default_output_spent_is_noop() {
let svc = MockLookupService::new();
let payload = OutputSpent::None {
txid: "abc".to_string(),
output_index: 0,
topic: "tm_test".to_string(),
};
// Default impl should not error
svc.output_spent(&payload).await.unwrap();
}
#[tokio::test]
async fn test_default_output_no_longer_retained() {
let svc = MockLookupService::new();
svc.output_no_longer_retained_in_history("abc", 0, "tm_test")
.await
.unwrap();
}
#[tokio::test]
async fn test_lookup_service_is_object_safe() {
let svc: Box<dyn LookupService> = Box::new(MockLookupService::new());
assert_eq!(svc.admission_mode(), AdmissionMode::LockingScript);
assert_eq!(svc.spend_notification_mode(), SpendNotificationMode::None);
let meta = svc.get_metadata().await;
assert_eq!(meta.name, "mock-lookup");
}
#[tokio::test]
async fn test_multiple_admissions_and_selective_eviction() {
let svc = MockLookupService::new();
for i in 0..5 {
let payload = OutputAdmittedByTopic::LockingScript {
txid: format!("tx{i}"),
output_index: 0,
topic: "tm_test".to_string(),
satoshis: 1000,
locking_script: vec![],
off_chain_values: None,
};
svc.output_admitted_by_topic(&payload).await.unwrap();
}
assert_eq!(svc.record_count(), 5);
// Evict only tx2
svc.output_evicted("tx2", 0).await.unwrap();
assert_eq!(svc.record_count(), 4);
let result = svc
.lookup(&LookupQuestion::new("ls_test", serde_json::json!({})))
.await
.unwrap();
let LookupResult::OutputList(refs) = result else {
panic!("expected OutputList");
};
assert!(!refs.iter().any(|r| r.txid == "tx2"));
}
}