upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/wallet_sqlite.cpp
blob: f4b4b378729148c196e68136fa99f59efbd9654f (plain)
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
// Copyright (c) 2026 Bitflash developers
// Distributed under the MIT/X11 software license.

#include "headers.h"
#include "wallet_sqlite.h"

static void SetSQLiteError(sqlite3* pdb, const string& strContext,
                           string& strError)
{
    if (pdb)
        strError = strContext + ": " + sqlite3_errmsg(pdb);
    else
        strError = strContext;
}

CWalletDBSQLite::CWalletDBSQLite() : pdb(NULL)
{
}

CWalletDBSQLite::~CWalletDBSQLite()
{
    Close();
}

bool CWalletDBSQLite::Open(const string& strPath, string& strError)
{
    Close();

    int ret = sqlite3_open_v2(strPath.c_str(), &pdb,
                              SQLITE_OPEN_READWRITE |
                              SQLITE_OPEN_CREATE |
                              SQLITE_OPEN_FULLMUTEX,
                              NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot open SQLite wallet", strError);
        Close();
        return false;
    }

    sqlite3_busy_timeout(pdb, 5000);

    if (!Exec("PRAGMA journal_mode=WAL;", strError))
    {
        Close();
        return false;
    }
    if (!Exec("PRAGMA synchronous=FULL;", strError))
    {
        Close();
        return false;
    }
    if (!InitSchema(strError))
    {
        Close();
        return false;
    }
    return true;
}

bool CWalletDBSQLite::OpenReadOnly(const string& strPath, string& strError)
{
    Close();

    int ret = sqlite3_open_v2(strPath.c_str(), &pdb,
                              SQLITE_OPEN_READONLY |
                              SQLITE_OPEN_FULLMUTEX,
                              NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot open SQLite wallet read-only", strError);
        Close();
        return false;
    }

    sqlite3_busy_timeout(pdb, 5000);
    return true;
}

void CWalletDBSQLite::Close()
{
    if (pdb)
    {
        sqlite3_close(pdb);
        pdb = NULL;
    }
}

bool CWalletDBSQLite::BeginTransaction(string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    return Exec("BEGIN IMMEDIATE;", strError);
}

bool CWalletDBSQLite::CommitTransaction(string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    return Exec("COMMIT;", strError);
}

bool CWalletDBSQLite::RollbackTransaction(string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    return Exec("ROLLBACK;", strError);
}

bool CWalletDBSQLite::Checkpoint(string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    return Exec("PRAGMA wal_checkpoint(TRUNCATE);", strError);
}

bool CWalletDBSQLite::Exec(const char* pszSql, string& strError)
{
    char* pszErr = NULL;
    int ret = sqlite3_exec(pdb, pszSql, NULL, NULL, &pszErr);
    if (ret != SQLITE_OK)
    {
        if (pszErr)
        {
            strError = pszErr;
            sqlite3_free(pszErr);
        }
        else
            SetSQLiteError(pdb, "SQLite exec failed", strError);
        return false;
    }
    return true;
}

bool CWalletDBSQLite::InitSchema(string& strError)
{
    return Exec("CREATE TABLE IF NOT EXISTS wallet_records ("
                "key BLOB PRIMARY KEY NOT NULL,"
                "value BLOB NOT NULL"
                ");",
                strError) &&
           Exec("PRAGMA user_version=1;", strError);
}

bool CWalletDBSQLite::WriteRecord(const vector<unsigned char>& vchKey,
                                  const vector<unsigned char>& vchValue,
                                  string& strError,
                                  bool fOverwrite)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    if (vchKey.empty())
    {
        strError = "SQLite wallet record key is empty";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    const char* pszSql = fOverwrite
        ? "INSERT OR REPLACE INTO wallet_records(key, value) VALUES(?, ?);"
        : "INSERT INTO wallet_records(key, value) VALUES(?, ?);";
    int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet write", strError);
        return false;
    }

    ret = sqlite3_bind_blob(stmt, 1, &vchKey[0], (int)vchKey.size(),
                            SQLITE_TRANSIENT);
    if (ret == SQLITE_OK)
        ret = sqlite3_bind_blob(stmt, 2,
                                vchValue.empty() ? NULL : &vchValue[0],
                                (int)vchValue.size(),
                                SQLITE_TRANSIENT);
    if (ret == SQLITE_OK)
        ret = sqlite3_step(stmt);

    bool fOk = (ret == SQLITE_DONE);
    if (!fOk)
        SetSQLiteError(pdb, "cannot write SQLite wallet record", strError);
    sqlite3_finalize(stmt);
    return fOk;
}

bool CWalletDBSQLite::ReadRecord(const vector<unsigned char>& vchKey,
                                 vector<unsigned char>& vchValueRet,
                                 string& strError)
{
    vchValueRet.clear();
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    if (vchKey.empty())
    {
        strError = "SQLite wallet record key is empty";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    const char* pszSql = "SELECT value FROM wallet_records WHERE key = ?;";
    int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet read", strError);
        return false;
    }

    ret = sqlite3_bind_blob(stmt, 1, &vchKey[0], (int)vchKey.size(),
                            SQLITE_TRANSIENT);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot bind SQLite wallet key", strError);
        sqlite3_finalize(stmt);
        return false;
    }

    ret = sqlite3_step(stmt);
    if (ret == SQLITE_ROW)
    {
        const unsigned char* pch =
            (const unsigned char*)sqlite3_column_blob(stmt, 0);
        int nBytes = sqlite3_column_bytes(stmt, 0);
        if (nBytes > 0 && pch)
            vchValueRet.assign(pch, pch + nBytes);
        sqlite3_finalize(stmt);
        return true;
    }

    if (ret == SQLITE_DONE)
        strError = "SQLite wallet record not found";
    else
        SetSQLiteError(pdb, "cannot read SQLite wallet record", strError);
    sqlite3_finalize(stmt);
    return false;
}

bool CWalletDBSQLite::EraseRecord(const vector<unsigned char>& vchKey,
                                  string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }
    if (vchKey.empty())
    {
        strError = "SQLite wallet record key is empty";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    const char* pszSql = "DELETE FROM wallet_records WHERE key = ?;";
    int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet erase", strError);
        return false;
    }

    ret = sqlite3_bind_blob(stmt, 1, &vchKey[0], (int)vchKey.size(),
                            SQLITE_TRANSIENT);
    if (ret == SQLITE_OK)
        ret = sqlite3_step(stmt);

    bool fOk = (ret == SQLITE_DONE);
    if (!fOk)
        SetSQLiteError(pdb, "cannot erase SQLite wallet record", strError);
    sqlite3_finalize(stmt);
    return fOk;
}

bool CWalletDBSQLite::ScanRecords(CWalletRecordVisitor& visitor,
                                  string& strError)
{
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    const char* pszSql = "SELECT key, value FROM wallet_records ORDER BY rowid;";
    int ret = sqlite3_prepare_v2(pdb, pszSql, -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet scan", strError);
        return false;
    }

    while ((ret = sqlite3_step(stmt)) == SQLITE_ROW)
    {
        const unsigned char* pchKey =
            (const unsigned char*)sqlite3_column_blob(stmt, 0);
        int nKeyBytes = sqlite3_column_bytes(stmt, 0);
        const unsigned char* pchValue =
            (const unsigned char*)sqlite3_column_blob(stmt, 1);
        int nValueBytes = sqlite3_column_bytes(stmt, 1);

        vector<unsigned char> vchKey;
        vector<unsigned char> vchValue;
        if (nKeyBytes > 0 && pchKey)
            vchKey.assign(pchKey, pchKey + nKeyBytes);
        if (nValueBytes > 0 && pchValue)
            vchValue.assign(pchValue, pchValue + nValueBytes);

        CDataStream ssKey(vchKey, SER_DISK);
        CDataStream ssValue(vchValue, SER_DISK);
        if (!visitor.VisitWalletRecord(ssKey, ssValue, strError))
        {
            sqlite3_finalize(stmt);
            return false;
        }
    }

    if (ret != SQLITE_DONE)
    {
        SetSQLiteError(pdb, "cannot scan SQLite wallet records", strError);
        sqlite3_finalize(stmt);
        return false;
    }

    sqlite3_finalize(stmt);
    return true;
}

bool CWalletDBSQLite::CountRecords(int& nRecordsRet, string& strError)
{
    nRecordsRet = 0;
    if (!pdb)
    {
        strError = "SQLite wallet is not open";
        return false;
    }

    sqlite3_stmt* stmt = NULL;
    int ret = sqlite3_prepare_v2(pdb,
                                 "SELECT COUNT(*) FROM wallet_records;",
                                 -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        SetSQLiteError(pdb, "cannot prepare SQLite wallet count", strError);
        return false;
    }

    ret = sqlite3_step(stmt);
    if (ret != SQLITE_ROW)
    {
        SetSQLiteError(pdb, "cannot count SQLite wallet records", strError);
        sqlite3_finalize(stmt);
        return false;
    }

    nRecordsRet = sqlite3_column_int(stmt, 0);
    sqlite3_finalize(stmt);
    return true;
}