Skip to content

Commit 30dc6dd

Browse files
committed
feat: add sqlite module and test, update README
1 parent df3735a commit 30dc6dd

File tree

8 files changed

+712
-11
lines changed

8 files changed

+712
-11
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/target
22
Cargo.lock
3-
3+
*.sqlite
44
.env

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ name = "async_wallet_bdk_sqlx"
1212
path = "src/main.rs"
1313

1414
[dependencies]
15-
sqlx = { version = "0.8.1", default-features = false, features = ["runtime-tokio", "tls-rustls-ring","derive", "postgres", "json", "chrono", "uuid", "sqlx-macros", "migrate"] }
15+
sqlx = { version = "0.8.1", default-features = false, features = ["runtime-tokio", "tls-rustls-ring","derive", "postgres", "sqlite", "json", "chrono", "uuid", "sqlx-macros", "migrate"] }
1616
thiserror = "1"
1717
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
1818
serde = { version = "1.0.208", features = ["derive"] }

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
# bdk-sqlx
2+
3+
4+
## Testing
5+
6+
1. Install postgresql with `psql` tool. For example (macos):
7+
```
8+
brew update
9+
brew install postgresql
10+
```
11+
2. Create empty test database:
12+
```
13+
psql postgres
14+
postgres=# create database test_bdk_wallet;
15+
```
16+
3. Set DATABASE_URL to test database:
17+
```
18+
export DATABASE_TEST_URL=postgresql://localhost/test_bdk_wallet
19+
```
20+
4. Run tests, must use a single test thread since we reuse the postgres db:
21+
```
22+
cargo test -- --test-threads=1
23+
```
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- Schema version control
2+
CREATE TABLE IF NOT EXISTS version (
3+
version INTEGER PRIMARY KEY
4+
);
5+
6+
-- Network is the valid network for all other table data
7+
CREATE TABLE IF NOT EXISTS network (
8+
wallet_name TEXT PRIMARY KEY,
9+
name TEXT NOT NULL
10+
);
11+
12+
-- Keychain is the json serialized keychain structure as JSONB,
13+
-- descriptor is the complete descriptor string,
14+
-- descriptor_id is a sha256::Hash id of the descriptor string w/o the checksum,
15+
-- last revealed index is a u32
16+
CREATE TABLE IF NOT EXISTS keychain (
17+
wallet_name TEXT NOT NULL,
18+
keychainkind TEXT NOT NULL,
19+
descriptor TEXT NOT NULL,
20+
descriptor_id BLOB NOT NULL,
21+
last_revealed INTEGER DEFAULT 0,
22+
PRIMARY KEY (wallet_name, keychainkind)
23+
24+
);
25+
26+
-- Hash is block hash hex string,
27+
-- Block height is a u32
28+
CREATE TABLE IF NOT EXISTS block (
29+
wallet_name TEXT,
30+
hash TEXT NOT NULL,
31+
height INTEGER NOT NULL,
32+
PRIMARY KEY (wallet_name, hash)
33+
);
34+
CREATE INDEX idx_block_height ON block (height);
35+
36+
-- Txid is transaction hash hex string (reversed)
37+
-- Whole_tx is a consensus encoded transaction,
38+
-- Last seen is a u64 unix epoch seconds
39+
CREATE TABLE IF NOT EXISTS tx (
40+
wallet_name TEXT,
41+
txid TEXT NOT NULL,
42+
whole_tx BLOB,
43+
last_seen INTEGER,
44+
PRIMARY KEY (wallet_name, txid)
45+
);
46+
47+
-- Outpoint txid hash hex string (reversed)
48+
-- Outpoint vout
49+
-- TxOut value as SATs
50+
-- TxOut script consensus encoded
51+
CREATE TABLE IF NOT EXISTS txout (
52+
wallet_name TEXT,
53+
txid TEXT NOT NULL,
54+
vout INTEGER NOT NULL,
55+
value INTEGER NOT NULL,
56+
script BLOB NOT NULL,
57+
PRIMARY KEY (wallet_name, txid, vout)
58+
);
59+
60+
-- Join table between anchor and tx
61+
-- Block hash hex string
62+
-- Anchor is a json serialized Anchor structure as JSONB,
63+
-- Txid is transaction hash hex string (reversed)
64+
CREATE TABLE IF NOT EXISTS anchor_tx (
65+
wallet_name TEXT,
66+
block_hash TEXT NOT NULL,
67+
anchor BLOB NOT NULL,
68+
txid TEXT NOT NULL,
69+
PRIMARY KEY (wallet_name, block_hash, txid),
70+
FOREIGN KEY (wallet_name, block_hash) REFERENCES block(wallet_name, hash),
71+
FOREIGN KEY (wallet_name, txid) REFERENCES tx(wallet_name, txid)
72+
);
73+
CREATE INDEX idx_anchor_tx_txid ON anchor_tx (txid);

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![warn(missing_docs)]
44

55
mod postgres;
6+
mod sqlite;
67

78
#[cfg(test)]
89
mod test;

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ async fn main() -> anyhow::Result<()> {
8888
let wallet_name =
8989
bdk_wallet::wallet_name_from_descriptor(VAULT_DESC, Some(CHANGE_DESC), NETWORK, &secp)?;
9090

91-
let mut store = bdk_sqlx::Store::new_with_url(url.clone(), Some(wallet_name)).await?;
91+
let mut store =
92+
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), Some(wallet_name)).await?;
9293

9394
let mut wallet = match Wallet::load().load_wallet_async(&mut store).await? {
9495
Some(wallet) => wallet,

0 commit comments

Comments
 (0)