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

payjoin / rust-payjoin / 17082577327

19 Aug 2025 09:45PM UTC coverage: 86.412% (-0.1%) from 86.514%
17082577327

Pull #873

github

web-flow
Merge fc3351d88 into 10f1a31d5
Pull Request #873: Migrate payjoin-cli from sled to rusqlite

154 of 179 new or added lines in 4 files covered. (86.03%)

1 existing line in 1 file now uncovered.

7911 of 9155 relevant lines covered (86.41%)

509.34 hits per line

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

92.54
/payjoin-cli/src/db/mod.rs
1
use std::path::Path;
2

3
use payjoin::bitcoin::consensus::encode::serialize;
4
use payjoin::bitcoin::OutPoint;
5
use r2d2::Pool;
6
use r2d2_sqlite::SqliteConnectionManager;
7
use rusqlite::{params, Connection};
8

9
pub(crate) mod error;
10
use error::*;
11

12
#[inline]
13
pub(crate) fn now() -> i64 {
18✔
14
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64
18✔
15
}
18✔
16

17
pub(crate) const DB_PATH: &str = "payjoin.sqlite";
18

19
pub(crate) struct Database {
20
    pool: Pool<SqliteConnectionManager>,
21
}
22

23
impl Database {
24
    pub(crate) fn create(path: impl AsRef<Path>) -> Result<Self> {
10✔
25
        let manager = SqliteConnectionManager::file(path.as_ref());
10✔
26
        let pool = Pool::new(manager)?;
10✔
27

28
        // Initialize database schema
29
        let conn = pool.get()?;
10✔
30
        Self::init_schema(&conn)?;
10✔
31

32
        Ok(Self { pool })
10✔
33
    }
10✔
34

35
    fn init_schema(conn: &Connection) -> Result<()> {
10✔
36
        // Enable foreign keys
37
        conn.execute("PRAGMA foreign_keys = ON", [])?;
10✔
38

39
        conn.execute(
10✔
40
            "CREATE TABLE IF NOT EXISTS send_sessions (
10✔
41
                session_id INTEGER PRIMARY KEY AUTOINCREMENT,
10✔
42
                completed_at INTEGER
10✔
43
            )",
10✔
44
            [],
10✔
NEW
45
        )?;
×
46

47
        conn.execute(
10✔
48
            "CREATE TABLE IF NOT EXISTS receive_sessions (
10✔
49
                session_id INTEGER PRIMARY KEY AUTOINCREMENT,
10✔
50
                completed_at INTEGER
10✔
51
            )",
10✔
52
            [],
10✔
NEW
53
        )?;
×
54

55
        conn.execute(
10✔
56
            "CREATE TABLE IF NOT EXISTS send_session_events (
10✔
57
                id INTEGER PRIMARY KEY AUTOINCREMENT,
10✔
58
                session_id INTEGER NOT NULL,
10✔
59
                event_data TEXT NOT NULL,
10✔
60
                created_at INTEGER NOT NULL,
10✔
61
                FOREIGN KEY(session_id) REFERENCES send_sessions(session_id)
10✔
62
            )",
10✔
63
            [],
10✔
NEW
64
        )?;
×
65

66
        conn.execute(
10✔
67
            "CREATE TABLE IF NOT EXISTS receive_session_events (
10✔
68
                id INTEGER PRIMARY KEY AUTOINCREMENT,
10✔
69
                session_id INTEGER NOT NULL,
10✔
70
                event_data TEXT NOT NULL,
10✔
71
                created_at INTEGER NOT NULL,
10✔
72
                FOREIGN KEY(session_id) REFERENCES receive_sessions(session_id)
10✔
73
            )",
10✔
74
            [],
10✔
NEW
75
        )?;
×
76

77
        conn.execute(
10✔
78
            "CREATE TABLE IF NOT EXISTS inputs_seen (
10✔
79
                outpoint BLOB PRIMARY KEY,
10✔
80
                created_at INTEGER NOT NULL
10✔
81
            )",
10✔
82
            [],
10✔
NEW
83
        )?;
×
84

85
        Ok(())
10✔
86
    }
10✔
87

88
    pub(crate) fn get_connection(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
31✔
89
        Ok(self.pool.get()?)
31✔
90
    }
31✔
91
    /// Inserts the input and returns true if the input was seen before, false otherwise.
92
    pub(crate) fn insert_input_seen_before(&self, input: OutPoint) -> Result<bool> {
3✔
93
        let conn = self.get_connection()?;
3✔
94
        let key = serialize(&input);
3✔
95

96
        let was_seen_before = conn.execute(
3✔
97
            "INSERT OR IGNORE INTO inputs_seen (outpoint, created_at) VALUES (?1, ?2)",
3✔
98
            params![key, now()],
3✔
99
        )? == 0;
3✔
100

101
        Ok(was_seen_before)
3✔
102
    }
3✔
103
}
104

105
#[cfg(feature = "v2")]
106
pub(crate) mod v2;
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