-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathmod.rs
More file actions
110 lines (91 loc) · 3.37 KB
/
mod.rs
File metadata and controls
110 lines (91 loc) · 3.37 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
use std::path::Path;
use payjoin::bitcoin::consensus::encode::serialize;
use payjoin::bitcoin::OutPoint;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::{params, Connection};
pub(crate) mod error;
use error::*;
pub(crate) fn now() -> i64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64
}
pub(crate) const DB_PATH: &str = "payjoin.sqlite";
#[derive(Debug)]
pub(crate) struct Database(Pool<SqliteConnectionManager>);
impl Database {
pub(crate) fn create(path: impl AsRef<Path>) -> Result<Self> {
// locking_mode is a per-connection PRAGMA, so it must be set via
// with_init to apply to every connection the pool creates, not only
// the first one used during init_schema.
let manager = SqliteConnectionManager::file(path.as_ref())
.with_init(|conn| conn.execute_batch("PRAGMA locking_mode = EXCLUSIVE;"));
let pool = Pool::new(manager)?;
// Initialize database schema
let conn = pool.get()?;
Self::init_schema(&conn)?;
Ok(Self(pool))
}
fn init_schema(conn: &Connection) -> Result<()> {
// Enable foreign keys
conn.execute("PRAGMA foreign_keys = ON", [])?;
conn.execute(
"CREATE TABLE IF NOT EXISTS send_sessions (
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
pj_uri TEXT NOT NULL,
receiver_pubkey BLOB NOT NULL,
completed_at INTEGER
)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS receive_sessions (
session_id INTEGER PRIMARY KEY AUTOINCREMENT,
completed_at INTEGER
)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS send_session_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER NOT NULL,
event_data TEXT NOT NULL,
created_at INTEGER NOT NULL,
FOREIGN KEY(session_id) REFERENCES send_sessions(session_id)
)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS receive_session_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id INTEGER NOT NULL,
event_data TEXT NOT NULL,
created_at INTEGER NOT NULL,
FOREIGN KEY(session_id) REFERENCES receive_sessions(session_id)
)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS inputs_seen (
outpoint BLOB PRIMARY KEY,
created_at INTEGER NOT NULL
)",
[],
)?;
Ok(())
}
pub(crate) fn get_connection(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
Ok(self.0.get()?)
}
/// Inserts the input and returns true if the input was seen before, false otherwise.
pub(crate) fn insert_input_seen_before(&self, input: OutPoint) -> Result<bool> {
let conn = self.get_connection()?;
let key = serialize(&input);
let was_seen_before = conn.execute(
"INSERT OR IGNORE INTO inputs_seen (outpoint, created_at) VALUES (?1, ?2)",
params![key, now()],
)? == 0;
Ok(was_seen_before)
}
}
#[cfg(feature = "v2")]
pub(crate) mod v2;