• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

payjoin / rust-payjoin / 17624224891

10 Sep 2025 07:08PM UTC coverage: 84.831% (-1.2%) from 86.019%
17624224891

Pull #1039

github

web-flow
Merge 042d23188 into 52cfeef1a
Pull Request #1039: WIP - display session history

0 of 134 new or added lines in 6 files covered. (0.0%)

51 existing lines in 3 files now uncovered.

8232 of 9704 relevant lines covered (84.83%)

481.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/payjoin-cli/src/db/v2.rs
1
use std::sync::Arc;
2

3
use payjoin::persist::SessionPersister;
4
use payjoin::receive::v2::SessionEvent as ReceiverSessionEvent;
5
use payjoin::send::v2::SessionEvent as SenderSessionEvent;
6
use payjoin::HpkePublicKey;
7
use rusqlite::params;
8

9
use super::*;
10

11
#[derive(Debug, Clone)]
12
pub(crate) struct SessionId(i64);
13

14
impl core::ops::Deref for SessionId {
15
    type Target = i64;
16
    fn deref(&self) -> &Self::Target { &self.0 }
18✔
17
}
18

19
impl std::fmt::Display for SessionId {
NEW
20
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) }
×
21
}
22

23
#[derive(Clone)]
24
pub(crate) struct SenderPersister {
25
    db: Arc<Database>,
26
    session_id: SessionId,
27
}
28

29
impl SenderPersister {
30
    pub fn new(db: Arc<Database>, receiver_pubkey: HpkePublicKey) -> crate::db::Result<Self> {
1✔
31
        let conn = db.get_connection()?;
1✔
32

33
        // Create a new session in send_sessions and get its ID
34
        let session_id: i64 = conn.query_row(
1✔
35
            "INSERT INTO send_sessions (session_id, receiver_pubkey) VALUES (NULL, ?1) RETURNING session_id",
1✔
36
            params![receiver_pubkey.to_compressed_bytes()],
1✔
37
            |row| row.get(0),
1✔
38
        )?;
×
39

40
        Ok(Self { db, session_id: SessionId(session_id) })
1✔
41
    }
1✔
42

43
    pub fn from_id(db: Arc<Database>, id: SessionId) -> Self { Self { db, session_id: id } }
1✔
44
}
45

46
impl SessionPersister for SenderPersister {
47
    type SessionEvent = SenderSessionEvent;
48
    type InternalStorageError = crate::db::error::Error;
49

50
    fn save_event(
3✔
51
        &self,
3✔
52
        event: SenderSessionEvent,
3✔
53
    ) -> std::result::Result<(), Self::InternalStorageError> {
3✔
54
        let conn = self.db.get_connection()?;
3✔
55
        let event_data = serde_json::to_string(&event).map_err(Error::Serialize)?;
3✔
56

57
        conn.execute(
3✔
58
            "INSERT INTO send_session_events (session_id, event_data, created_at) VALUES (?1, ?2, ?3)",
3✔
59
            params![*self.session_id, event_data, now()],
3✔
60
        )?;
3✔
61

62
        Ok(())
3✔
63
    }
3✔
64

65
    fn load(
1✔
66
        &self,
1✔
67
    ) -> std::result::Result<Box<dyn Iterator<Item = SenderSessionEvent>>, Self::InternalStorageError>
1✔
68
    {
69
        let conn = self.db.get_connection()?;
1✔
70
        let mut stmt = conn.prepare(
1✔
71
            "SELECT event_data FROM send_session_events WHERE session_id = ?1 ORDER BY created_at ASC",
1✔
72
        )?;
1✔
73

74
        let event_rows = stmt.query_map(params![*self.session_id], |row| {
2✔
75
            let event_data: String = row.get(0)?;
2✔
76
            Ok(event_data)
2✔
77
        })?;
2✔
78

79
        let events: Vec<SenderSessionEvent> = event_rows
1✔
80
            .map(|row| {
2✔
81
                let event_data = row.expect("Failed to read event data from database");
2✔
82
                serde_json::from_str::<SenderSessionEvent>(&event_data)
2✔
83
                    .expect("Database corruption: failed to deserialize session event")
2✔
84
            })
2✔
85
            .collect();
1✔
86

87
        Ok(Box::new(events.into_iter()))
1✔
88
    }
1✔
89

90
    fn close(&self) -> std::result::Result<(), Self::InternalStorageError> {
1✔
91
        let conn = self.db.get_connection()?;
1✔
92

93
        conn.execute(
1✔
94
            "UPDATE send_sessions SET completed_at = ?1 WHERE session_id = ?2",
1✔
95
            params![now(), *self.session_id],
1✔
96
        )?;
1✔
97

98
        Ok(())
1✔
99
    }
1✔
100
}
101

102
#[derive(Clone)]
103
pub(crate) struct ReceiverPersister {
104
    db: Arc<Database>,
105
    session_id: SessionId,
106
}
107

108
impl ReceiverPersister {
109
    pub fn new(db: Arc<Database>) -> crate::db::Result<Self> {
1✔
110
        let conn = db.get_connection()?;
1✔
111

112
        // Create a new session in receive_sessions and get its ID
113
        let session_id: i64 = conn.query_row(
1✔
114
            "INSERT INTO receive_sessions (session_id) VALUES (NULL) RETURNING session_id",
1✔
115
            [],
1✔
116
            |row| row.get(0),
1✔
117
        )?;
×
118

119
        Ok(Self { db, session_id: SessionId(session_id) })
1✔
120
    }
1✔
121

122
    pub fn from_id(db: Arc<Database>, id: SessionId) -> Self { Self { db, session_id: id } }
1✔
123
}
124

125
impl SessionPersister for ReceiverPersister {
126
    type SessionEvent = ReceiverSessionEvent;
127
    type InternalStorageError = crate::db::error::Error;
128

129
    fn save_event(
10✔
130
        &self,
10✔
131
        event: ReceiverSessionEvent,
10✔
132
    ) -> std::result::Result<(), Self::InternalStorageError> {
10✔
133
        let conn = self.db.get_connection()?;
10✔
134
        let event_data = serde_json::to_string(&event).map_err(Error::Serialize)?;
10✔
135

136
        conn.execute(
10✔
137
            "INSERT INTO receive_session_events (session_id, event_data, created_at) VALUES (?1, ?2, ?3)",
10✔
138
            params![*self.session_id, event_data, now()],
10✔
139
        )?;
10✔
140

141
        Ok(())
10✔
142
    }
10✔
143

144
    fn load(
2✔
145
        &self,
2✔
146
    ) -> std::result::Result<
2✔
147
        Box<dyn Iterator<Item = ReceiverSessionEvent>>,
2✔
148
        Self::InternalStorageError,
2✔
149
    > {
2✔
150
        let conn = self.db.get_connection()?;
2✔
151
        let mut stmt = conn.prepare(
2✔
152
            "SELECT event_data FROM receive_session_events WHERE session_id = ?1 ORDER BY created_at ASC",
2✔
153
        )?;
2✔
154

155
        let event_rows = stmt.query_map(params![*self.session_id], |row| {
2✔
156
            let event_data: String = row.get(0)?;
2✔
157
            Ok(event_data)
2✔
158
        })?;
2✔
159

160
        let events: Vec<ReceiverSessionEvent> = event_rows
2✔
161
            .map(|row| {
2✔
162
                let event_data = row.expect("Failed to read event data from database");
2✔
163
                serde_json::from_str::<ReceiverSessionEvent>(&event_data)
2✔
164
                    .expect("Database corruption: failed to deserialize session event")
2✔
165
            })
2✔
166
            .collect();
2✔
167

168
        Ok(Box::new(events.into_iter()))
2✔
169
    }
2✔
170

171
    fn close(&self) -> std::result::Result<(), Self::InternalStorageError> {
1✔
172
        let conn = self.db.get_connection()?;
1✔
173

174
        conn.execute(
1✔
175
            "UPDATE receive_sessions SET completed_at = ?1 WHERE session_id = ?2",
1✔
176
            params![now(), *self.session_id],
1✔
177
        )?;
1✔
178

179
        Ok(())
1✔
180
    }
1✔
181
}
182

183
impl Database {
184
    pub(crate) fn get_recv_session_ids(&self) -> Result<Vec<SessionId>> {
3✔
185
        let conn = self.get_connection()?;
3✔
186
        let mut stmt =
3✔
187
            conn.prepare("SELECT session_id FROM receive_sessions WHERE completed_at IS NULL")?;
3✔
188

189
        let session_rows = stmt.query_map([], |row| {
3✔
190
            let session_id: i64 = row.get(0)?;
1✔
191
            Ok(SessionId(session_id))
1✔
192
        })?;
1✔
193

194
        let mut session_ids = Vec::new();
3✔
195
        for session_row in session_rows {
4✔
196
            let session_id = session_row?;
1✔
197
            session_ids.push(session_id);
1✔
198
        }
199

200
        Ok(session_ids)
3✔
201
    }
3✔
202

203
    pub(crate) fn get_send_session_ids(&self) -> Result<Vec<SessionId>> {
5✔
204
        let conn = self.get_connection()?;
5✔
205
        let mut stmt =
5✔
206
            conn.prepare("SELECT session_id FROM send_sessions WHERE completed_at IS NULL")?;
5✔
207

208
        let session_rows = stmt.query_map([], |row| {
5✔
209
            let session_id: i64 = row.get(0)?;
1✔
210
            Ok(SessionId(session_id))
1✔
211
        })?;
1✔
212

213
        let mut session_ids = Vec::new();
5✔
214
        for session_row in session_rows {
6✔
215
            let session_id = session_row?;
1✔
216
            session_ids.push(session_id);
1✔
217
        }
218

219
        Ok(session_ids)
5✔
220
    }
5✔
221

222
    pub(crate) fn get_send_session_receiver_pk(
1✔
223
        &self,
1✔
224
        session_id: &SessionId,
1✔
225
    ) -> Result<HpkePublicKey> {
1✔
226
        let conn = self.get_connection()?;
1✔
227
        let mut stmt =
1✔
228
            conn.prepare("SELECT receiver_pubkey FROM send_sessions WHERE session_id = ?1")?;
1✔
229
        let receiver_pubkey: Vec<u8> = stmt.query_row(params![session_id.0], |row| row.get(0))?;
1✔
230
        Ok(HpkePublicKey::from_compressed_bytes(&receiver_pubkey).expect("Valid receiver pubkey"))
1✔
231
    }
1✔
232

NEW
233
    pub(crate) fn get_inactive_send_session_ids(&self) -> Result<Vec<(SessionId, u64)>> {
×
NEW
234
        let conn = self.get_connection()?;
×
NEW
235
        let mut stmt = conn.prepare(
×
NEW
236
            "SELECT session_id, completed_at FROM send_sessions WHERE completed_at IS NOT NULL",
×
NEW
237
        )?;
×
NEW
238
        let session_rows = stmt.query_map([], |row| {
×
NEW
239
            let session_id: i64 = row.get(0)?;
×
NEW
240
            let completed_at: u64 = row.get(1)?;
×
NEW
241
            Ok((SessionId(session_id), completed_at))
×
NEW
242
        })?;
×
243

NEW
244
        let mut session_ids = Vec::new();
×
NEW
245
        for session_row in session_rows {
×
NEW
246
            let (session_id, completed_at) = session_row?;
×
NEW
247
            session_ids.push((session_id, completed_at));
×
248
        }
NEW
249
        Ok(session_ids)
×
NEW
250
    }
×
251

NEW
252
    pub(crate) fn get_inactive_recv_session_ids(&self) -> Result<Vec<(SessionId, u64)>> {
×
NEW
253
        let conn = self.get_connection()?;
×
NEW
254
        let mut stmt = conn.prepare(
×
NEW
255
            "SELECT session_id, completed_at FROM receive_sessions WHERE completed_at IS NOT NULL",
×
NEW
256
        )?;
×
NEW
257
        let session_rows = stmt.query_map([], |row| {
×
NEW
258
            let session_id: i64 = row.get(0)?;
×
NEW
259
            let completed_at: u64 = row.get(1)?;
×
NEW
260
            Ok((SessionId(session_id), completed_at))
×
NEW
261
        })?;
×
262

NEW
263
        let mut session_ids = Vec::new();
×
NEW
264
        for session_row in session_rows {
×
NEW
265
            let (session_id, completed_at) = session_row?;
×
NEW
266
            session_ids.push((session_id, completed_at));
×
267
        }
NEW
268
        Ok(session_ids)
×
NEW
269
    }
×
270
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc