cdk-spilman 0.17.6

Standalone Spilman payment channels library for Cashu
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use std::sync::RwLock;

use super::*;

// ============================================================================
// MemoryStorage
// ============================================================================

/// Thread-safe in-memory storage using `RwLock<HashMap>`.
#[derive(Default)]
pub struct MemoryStorage {
    funding: RwLock<HashMap<ChannelId, ChannelFunding>>,
    balance: RwLock<HashMap<ChannelId, PaymentProof>>,
    usage: RwLock<HashMap<ChannelId, UsageMap>>,
    closing: RwLock<HashMap<ChannelId, ClosingData>>,
    closed: RwLock<HashMap<ChannelId, ClosedDataView>>,
    keysets: RwLock<HashMap<(String, Id), KeysetCacheEntry>>,
}

impl std::fmt::Debug for MemoryStorage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MemoryStorage").finish_non_exhaustive()
    }
}

impl MemoryStorage {
    /// Create a new, empty in-memory storage.
    pub fn new() -> Self {
        Self::default()
    }
}

impl SpilmanStorage for MemoryStorage {
    fn get_funding(&self, channel_id: &str) -> Option<ChannelFunding> {
        self.funding
            .read()
            .expect("funding lock")
            .get(channel_id)
            .cloned()
    }

    fn save_funding(&self, channel_id: &str, funding: ChannelFunding) -> Result<(), String> {
        let mut store = self.funding.write().expect("funding lock");
        if !store.contains_key(channel_id) {
            store.insert(channel_id.to_string(), funding);
        }
        Ok(())
    }

    fn get_balance(&self, channel_id: &str) -> Option<PaymentProof> {
        self.balance
            .read()
            .expect("balance lock")
            .get(channel_id)
            .cloned()
    }

    fn update_balance(&self, channel_id: &str, payment: PaymentProof) -> Result<(), String> {
        let mut store = self.balance.write().expect("balance lock");
        let should_update = store
            .get(channel_id)
            .map(|b| payment.balance > b.balance)
            .unwrap_or(true);
        if should_update {
            store.insert(channel_id.to_string(), payment);
        }
        Ok(())
    }

    fn get_usage(&self, channel_id: &str) -> Option<UsageMap> {
        let store = self.usage.read().expect("usage lock");
        let map = store.get(channel_id)?;
        if map.is_empty() {
            None
        } else {
            Some(map.clone())
        }
    }

    fn increment_usage(&self, channel_id: &str, increments: &UsageMap) -> Result<(), String> {
        let mut store = self.usage.write().expect("usage lock");
        let usage = store.entry(channel_id.to_string()).or_default();
        for (var, delta) in increments {
            *usage.entry(var.clone()).or_insert(0) += delta;
        }
        Ok(())
    }

    fn get_state(&self, channel_id: &str) -> ChannelState {
        if self
            .closed
            .read()
            .expect("closed lock")
            .contains_key(channel_id)
        {
            ChannelState::Closed
        } else if self
            .closing
            .read()
            .expect("closing lock")
            .contains_key(channel_id)
        {
            ChannelState::Closing
        } else {
            ChannelState::Open
        }
    }

    fn mark_closing(&self, channel_id: &str, closing: ClosingData) -> Result<(), String> {
        if self
            .closed
            .read()
            .expect("closed lock")
            .contains_key(channel_id)
        {
            return Err("channel already closed".to_string());
        }
        self.closing
            .write()
            .expect("closing lock")
            .insert(channel_id.to_string(), closing);
        Ok(())
    }

    fn get_closing_data(&self, channel_id: &str) -> Option<ClosingData> {
        self.closing
            .read()
            .expect("closing lock")
            .get(channel_id)
            .cloned()
    }

    fn mark_closed(&self, channel_id: &str, data: ClosedDataView) -> Result<(), String> {
        if self
            .closed
            .read()
            .expect("closed lock")
            .contains_key(channel_id)
        {
            return Err("channel already closed".to_string());
        }
        // Insert into closed before removing from closing, so that
        // get_state (which checks closed first) never sees the channel
        // in neither store and briefly reports it as Open.
        self.closed
            .write()
            .expect("closed lock")
            .insert(channel_id.to_string(), data);
        self.closing
            .write()
            .expect("closing lock")
            .remove(channel_id);
        Ok(())
    }

    fn get_closed_data(&self, channel_id: &str) -> Option<ClosedDataView> {
        self.closed
            .read()
            .expect("closed lock")
            .get(channel_id)
            .cloned()
    }

    fn get_keyset(&self, mint: &str, keyset_id: &Id) -> Option<KeysetCacheEntry> {
        self.keysets
            .read()
            .expect("keysets lock")
            .get(&(mint.to_string(), *keyset_id))
            .cloned()
    }

    fn set_keyset(&self, mint: &str, keyset_id: Id, entry: KeysetCacheEntry) -> Result<(), String> {
        self.keysets
            .write()
            .expect("keysets lock")
            .insert((mint.to_string(), keyset_id), entry);
        Ok(())
    }

    fn get_active_keyset_ids(&self, mint: &str, unit: &CurrencyUnit) -> Vec<Id> {
        // There is no requirement that this be 'up-to-date'. So this is
        // the set of keysets were active the last time the server updated
        // its records of the keysets
        self.keysets
            .read()
            .expect("keysets lock")
            .iter()
            .filter(|((m, _), entry)| m == mint && entry.unit == *unit && entry.active)
            .map(|((_, kid), _)| *kid)
            .collect()
    }

    /// Returns `{ mint_url: { unit: [keyset_id, …] } }` for all active keysets.
    fn get_mints_units_keysets(&self) -> HashMap<String, HashMap<String, Vec<String>>> {
        let mut result: HashMap<String, HashMap<String, Vec<String>>> = HashMap::new();
        let store = self.keysets.read().expect("keysets lock");
        for ((mint, keyset_id), entry) in store.iter() {
            if !entry.active {
                continue;
            }
            result
                .entry(mint.clone())
                .or_default()
                .entry(entry.unit.to_string())
                .or_default()
                .push(keyset_id.to_string());
        }
        result
    }

    /// Returns the set of units that have at least one active keyset.
    fn get_active_units(&self) -> std::collections::HashSet<String> {
        self.keysets
            .read()
            .expect("keysets lock")
            .values()
            .filter(|e| e.active)
            .map(|e| e.unit.to_string())
            .collect()
    }
}

// ============================================================================
// SqliteStorage
// ============================================================================

/// SQLite-backed persistent storage.
///
/// Schema:
/// - `spilman_channels` — funding, balance, state, closing/closed JSON
/// - `spilman_usage` — normalized: one row per (channel, variable) with atomic
///   `INSERT ... ON CONFLICT DO UPDATE SET count = count + excluded.count`
/// - `spilman_keysets` — cached mint keyset metadata (JSON)
pub struct SqliteStorage {
    conn: std::sync::Mutex<rusqlite::Connection>,
    /// Write-once cache: funding data is never updated or deleted, so cache
    /// entries are populated lazily on first access and never invalidated.
    funding_cache: std::sync::Mutex<HashMap<String, ChannelFunding>>,
}

impl std::fmt::Debug for SqliteStorage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SqliteStorage").finish_non_exhaustive()
    }
}

impl SqliteStorage {
    /// Open (or create) a SQLite database at the given path.
    pub fn open(path: &str) -> Result<Self, String> {
        let conn = rusqlite::Connection::open(path)
            .map_err(|e| format!("failed to open SQLite at {path}: {e}"))?;
        let storage = Self {
            conn: std::sync::Mutex::new(conn),
            funding_cache: std::sync::Mutex::new(HashMap::new()),
        };
        storage.init_schema()?;
        Ok(storage)
    }

    /// Create an in-memory SQLite database (useful for testing).
    #[cfg(test)]
    pub fn open_in_memory() -> Result<Self, String> {
        let conn = rusqlite::Connection::open_in_memory()
            .map_err(|e| format!("failed to open in-memory SQLite: {e}"))?;
        let storage = Self {
            conn: std::sync::Mutex::new(conn),
            funding_cache: std::sync::Mutex::new(HashMap::new()),
        };
        storage.init_schema()?;
        Ok(storage)
    }

    fn init_schema(&self) -> Result<(), String> {
        let conn = self.conn.lock().expect("sqlite lock");
        conn.execute_batch(
            "
            CREATE TABLE IF NOT EXISTS spilman_channels (
                channel_id    TEXT NOT NULL PRIMARY KEY,
                funding_json  TEXT NOT NULL,
                balance       INTEGER NOT NULL DEFAULT 0,
                signature     TEXT NOT NULL DEFAULT '',
                state         TEXT NOT NULL DEFAULT 'Open',
                closing_json  TEXT,
                closed_json   TEXT
            );

            CREATE TABLE IF NOT EXISTS spilman_usage (
                channel_id TEXT NOT NULL,
                var_name   TEXT NOT NULL,
                count      INTEGER NOT NULL DEFAULT 0,
                PRIMARY KEY (channel_id, var_name)
            );

            CREATE TABLE IF NOT EXISTS spilman_keysets (
                mint_url   TEXT NOT NULL,
                keyset_id  TEXT NOT NULL,
                entry_json TEXT NOT NULL,
                PRIMARY KEY (mint_url, keyset_id)
            );
            ",
        )
        .map_err(|e| format!("failed to initialize SQLite schema: {e}"))
    }
}

impl SpilmanStorage for SqliteStorage {
    fn get_funding(&self, channel_id: &str) -> Option<ChannelFunding> {
        // Check the in-memory cache first.
        {
            let cache = self.funding_cache.lock().expect("funding_cache lock");
            if let Some(f) = cache.get(channel_id) {
                return Some(f.clone());
            }
        }
        // Cache miss — query SQLite and populate on hit.
        let conn = self.conn.lock().expect("sqlite lock");
        let funding: Option<ChannelFunding> = conn
            .query_row(
                "SELECT funding_json FROM spilman_channels WHERE channel_id = ?1",
                [channel_id],
                |row| {
                    let json: String = row.get(0)?;
                    Ok(json)
                },
            )
            .ok()
            .and_then(|json| serde_json::from_str(&json).ok());
        if let Some(ref f) = funding {
            drop(conn);
            self.funding_cache
                .lock()
                .expect("funding_cache lock")
                .insert(channel_id.to_string(), f.clone());
        }
        funding
    }

    fn save_funding(&self, channel_id: &str, funding: ChannelFunding) -> Result<(), String> {
        let conn = self.conn.lock().expect("sqlite lock");
        let json = serde_json::to_string(&funding).expect("ChannelFunding serialization failed");
        conn.execute(
            "INSERT INTO spilman_channels (channel_id, funding_json)
             VALUES (?1, ?2)
             ON CONFLICT(channel_id) DO NOTHING",
            rusqlite::params![channel_id, json],
        )
        .map_err(|e| format!("save_funding: {e}"))?;
        // Populate the cache (write-once, so first insert wins — matches the SQL).
        self.funding_cache
            .lock()
            .expect("funding_cache lock")
            .entry(channel_id.to_string())
            .or_insert(funding);
        Ok(())
    }

    fn get_balance(&self, channel_id: &str) -> Option<PaymentProof> {
        let conn = self.conn.lock().expect("sqlite lock");
        conn.query_row(
            "SELECT balance, signature FROM spilman_channels
             WHERE channel_id = ?1 AND signature != ''",
            [channel_id],
            |row| {
                let balance: i64 = row.get(0)?;
                let signature: String = row.get(1)?;
                Ok(PaymentProof {
                    balance: balance as u64,
                    signature,
                })
            },
        )
        .ok()
    }

    fn update_balance(&self, channel_id: &str, payment: PaymentProof) -> Result<(), String> {
        let conn = self.conn.lock().expect("sqlite lock");
        // Monotonic: only update if strictly greater, OR if this is the
        // first real balance (signature is still the empty-string default).
        // Returns Ok(()) even if 0 rows affected (monotonic no-op).
        conn.execute(
            "UPDATE spilman_channels
             SET balance = ?2, signature = ?3
             WHERE channel_id = ?1
               AND (balance < ?2 OR signature = '')",
            rusqlite::params![channel_id, payment.balance as i64, payment.signature],
        )
        .map_err(|e| format!("update_balance: {e}"))?;
        Ok(())
    }

    fn get_usage(&self, channel_id: &str) -> Option<UsageMap> {
        let conn = self.conn.lock().expect("sqlite lock");
        let mut stmt =
            match conn.prepare("SELECT var_name, count FROM spilman_usage WHERE channel_id = ?1") {
                Ok(s) => s,
                Err(_) => return None,
            };
        let map: UsageMap = stmt
            .query_map([channel_id], |row| {
                let var: String = row.get(0)?;
                let count: i64 = row.get(1)?;
                Ok((var, count as u64))
            })
            .ok()?
            .filter_map(|r| r.ok())
            .collect();

        if map.is_empty() {
            None
        } else {
            Some(map)
        }
    }

    /// Atomically increment usage counters for a channel.
    ///
    /// `increments` maps variable names to their deltas for this request,
    /// e.g. `{"chars": 42, "requests": 1}`.  Each entry produces one
    /// SQL upsert against the `spilman_usage` table (keyed by
    /// `(channel_id, var_name)`): the row is created if it doesn't exist,
    /// or its `count` is bumped by the delta if it does.  All upserts
    /// for the channel run in a single transaction.
    fn increment_usage(&self, channel_id: &str, increments: &UsageMap) -> Result<(), String> {
        let mut conn = self.conn.lock().expect("sqlite lock");
        let tx = conn
            .transaction()
            .map_err(|e| format!("increment_usage: begin transaction: {e}"))?;
        for (var, delta) in increments {
            tx.execute(
                "INSERT INTO spilman_usage (channel_id, var_name, count)
                 VALUES (?1, ?2, ?3)
                 ON CONFLICT(channel_id, var_name)
                 DO UPDATE SET count = count + excluded.count",
                rusqlite::params![channel_id, var, *delta as i64],
            )
            .map_err(|e| format!("increment_usage({var}): {e}"))?;
        }
        tx.commit()
            .map_err(|e| format!("increment_usage: commit: {e}"))?;
        Ok(())
    }

    fn get_state(&self, channel_id: &str) -> ChannelState {
        let conn = self.conn.lock().expect("sqlite lock");
        conn.query_row(
            "SELECT state FROM spilman_channels WHERE channel_id = ?1",
            [channel_id],
            |row| {
                let state: String = row.get(0)?;
                Ok(state)
            },
        )
        .ok()
        .map(|s| match s.as_str() {
            "Closing" => ChannelState::Closing,
            "Closed" => ChannelState::Closed,
            _ => ChannelState::Open,
        })
        .unwrap_or(ChannelState::Open)
    }

    fn mark_closing(&self, channel_id: &str, closing: ClosingData) -> Result<(), String> {
        let conn = self.conn.lock().expect("sqlite lock");
        let json = serde_json::to_string(&closing).expect("ClosingData serialization failed");
        let rows = conn
            .execute(
                "UPDATE spilman_channels
                 SET state = 'Closing', closing_json = ?2
                 WHERE channel_id = ?1 AND state != 'Closed'",
                rusqlite::params![channel_id, json],
            )
            .map_err(|e| format!("mark_closing: {e}"))?;
        if rows == 0 {
            return Err("channel not found or already closed".to_string());
        }
        Ok(())
    }

    fn get_closing_data(&self, channel_id: &str) -> Option<ClosingData> {
        let conn = self.conn.lock().expect("sqlite lock");
        conn.query_row(
            "SELECT closing_json FROM spilman_channels
             WHERE channel_id = ?1 AND state = 'Closing'",
            [channel_id],
            |row| {
                let json: String = row.get(0)?;
                Ok(json)
            },
        )
        .ok()
        .and_then(|json| serde_json::from_str(&json).ok())
    }

    fn mark_closed(&self, channel_id: &str, data: ClosedDataView) -> Result<(), String> {
        let conn = self.conn.lock().expect("sqlite lock");
        let json = serde_json::to_string(&data).expect("ClosedDataView serialization failed");
        // Single UPDATE with WHERE guard: only transitions non-Closed channels.
        let rows = conn
            .execute(
                "UPDATE spilman_channels
                 SET state = 'Closed', closed_json = ?2, closing_json = NULL
                 WHERE channel_id = ?1 AND state != 'Closed'",
                rusqlite::params![channel_id, json],
            )
            .map_err(|e| format!("mark_closed: {e}"))?;
        if rows == 0 {
            return Err("channel already closed or not found".to_string());
        }
        Ok(())
    }

    fn get_closed_data(&self, channel_id: &str) -> Option<ClosedDataView> {
        let conn = self.conn.lock().expect("sqlite lock");
        conn.query_row(
            "SELECT closed_json FROM spilman_channels
             WHERE channel_id = ?1 AND state = 'Closed'",
            [channel_id],
            |row| {
                let json: String = row.get(0)?;
                Ok(json)
            },
        )
        .ok()
        .and_then(|json| serde_json::from_str(&json).ok())
    }

    fn get_keyset(&self, mint: &str, keyset_id: &Id) -> Option<KeysetCacheEntry> {
        let conn = self.conn.lock().expect("sqlite lock");
        conn.query_row(
            "SELECT entry_json FROM spilman_keysets WHERE mint_url = ?1 AND keyset_id = ?2",
            rusqlite::params![mint, keyset_id.to_string()],
            |row| {
                let json: String = row.get(0)?;
                Ok(json)
            },
        )
        .ok()
        .and_then(|json| serde_json::from_str(&json).ok())
    }

    fn set_keyset(&self, mint: &str, keyset_id: Id, entry: KeysetCacheEntry) -> Result<(), String> {
        let conn = self.conn.lock().expect("sqlite lock");
        let json = serde_json::to_string(&entry).expect("KeysetCacheEntry serialization failed");
        conn.execute(
            "INSERT INTO spilman_keysets (mint_url, keyset_id, entry_json)
             VALUES (?1, ?2, ?3)
             ON CONFLICT(mint_url, keyset_id) DO UPDATE SET entry_json = ?3",
            rusqlite::params![mint, keyset_id.to_string(), json],
        )
        .map_err(|e| format!("set_keyset: {e}"))?;
        Ok(())
    }

    fn get_active_keyset_ids(&self, mint: &str, unit: &CurrencyUnit) -> Vec<Id> {
        let conn = self.conn.lock().expect("sqlite lock");
        let mut stmt = match conn
            .prepare("SELECT keyset_id, entry_json FROM spilman_keysets WHERE mint_url = ?1")
        {
            Ok(s) => s,
            Err(_) => return vec![],
        };

        let unit_str = unit.to_string();
        stmt.query_map([mint], |row| {
            let kid_str: String = row.get(0)?;
            let json: String = row.get(1)?;
            Ok((kid_str, json))
        })
        .ok()
        .map(|rows| {
            rows.filter_map(|r| r.ok())
                .filter_map(|(kid_str, json)| {
                    let entry: KeysetCacheEntry = serde_json::from_str(&json).ok()?;
                    if entry.active && entry.unit.to_string() == unit_str {
                        kid_str.parse::<Id>().ok()
                    } else {
                        None
                    }
                })
                .collect()
        })
        .unwrap_or_default()
    }

    fn get_mints_units_keysets(&self) -> HashMap<String, HashMap<String, Vec<String>>> {
        let conn = self.conn.lock().expect("sqlite lock");
        let mut stmt =
            match conn.prepare("SELECT mint_url, keyset_id, entry_json FROM spilman_keysets") {
                Ok(s) => s,
                Err(_) => return HashMap::new(),
            };

        let mut result: HashMap<String, HashMap<String, Vec<String>>> = HashMap::new();
        if let Ok(rows) = stmt.query_map([], |row| {
            let mint: String = row.get(0)?;
            let kid: String = row.get(1)?;
            let json: String = row.get(2)?;
            Ok((mint, kid, json))
        }) {
            for row in rows.flatten() {
                let (mint, kid, json) = row;
                if let Ok(entry) = serde_json::from_str::<KeysetCacheEntry>(&json) {
                    // Only active keysets: inactive ones still work for
                    // existing channels, but we don't advertise them to
                    // new clients.
                    if entry.active {
                        result
                            .entry(mint)
                            .or_default()
                            .entry(entry.unit.to_string())
                            .or_default()
                            .push(kid);
                    }
                }
            }
        }
        result
    }

    fn get_active_units(&self) -> std::collections::HashSet<String> {
        let conn = self.conn.lock().expect("sqlite lock");
        let mut stmt = match conn.prepare("SELECT entry_json FROM spilman_keysets") {
            Ok(s) => s,
            Err(_) => return std::collections::HashSet::new(),
        };

        stmt.query_map([], |row| {
            let json: String = row.get(0)?;
            Ok(json)
        })
        .ok()
        .map(|rows| {
            rows.filter_map(|r| r.ok())
                .filter_map(|json| {
                    let entry: KeysetCacheEntry = serde_json::from_str(&json).ok()?;
                    if entry.active {
                        Some(entry.unit.to_string())
                    } else {
                        None
                    }
                })
                .collect()
        })
        .unwrap_or_default()
    }
}