Skip to content

Replace rusqlite with sqlite-thin - #1258

Open
tnull wants to merge 2 commits into
lightningdevkit:mainfrom
tnull:2026-11-explore-sqlite-thin
Open

tnull wants to merge 2 commits into
lightningdevkit:mainfrom
tnull:2026-11-explore-sqlite-thin

Conversation

@tnull

@tnull tnull commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

libsqlite3-sys may only be linked once per build and every rusqlite
release pins exactly one version of it, so LDK Node and any application using
SQLite have to agree on a rusqlite release. Our MSRV additionally keeps us
from admitting recent releases, which keeps downstream users from picking up
newer bundled SQLite versions.

This switches SqliteStore to sqlite-thin, a small wrapper that declares the
SQLite C API itself and accepts any libsqlite3-sys release from 0.28 on,
leaving the version choice to the application. It mirrors the subset of
rusqlite's API we use, so the change is an import rename and the on-disk
format is unchanged.

For downstream users this drops 6 crates from the tree (rusqlite, hashlink,
hashbrown, foldhash, fallible-iterator, fallible-streaming-iterator) and
adds 1 (sqlite-thin).

The first commit adds tests for the paths most likely to be affected (empty and
large values, upgrading a database written by LDK Node v0.7, a failed migration
rolling back, and lock contention). They pass both before and after the switch.

Still open: sqlite-thin needs a crates.io release before this can be merged,
and since it doesn't cap libsqlite3-sys, a downstream build without its own
rusqlite resolves to 0.40.0, which requires Rust 1.88 (our MSRV is 1.85).

The SQLite store's tests only ever read databases they wrote
themselves, and neither covered zero-length or multi-megabyte values,
a schema migration failing halfway through, nor contention with a
second connection. These are the paths most likely to change behavior
when the underlying SQLite wrapper is touched.

Add tests asserting that values of any length are stored as BLOBs,
that a database written by LDK Node v0.7 is migrated and remains
usable, that a failed migration leaves the database untouched, and
that writes wait for a lock held by another connection.

Co-Authored-By: HAL 9000
@ldk-reviews-bot

ldk-reviews-bot commented Nov 24, 2026 •

Copy link
Copy Markdown

🎉 This PR is now ready for review!
Please choose at least one reviewer by assigning them on the right bar.
If no reviewers are assigned within 10 minutes, I'll automatically assign one.
Once the first reviewer has submitted a review, a second will be assigned if required.

@tnull
tnull marked this pull request as draft November 24, 2026 09:25

@vincenzopalazzo vincenzopalazzo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

I added a small comment on how to include the library!

Comment thread Cargo.toml Resolved
As libsqlite3-sys may only be linked once per build and every rusqlite
release pins a single version of it, LDK Node and any application
using SQLite themselves had to agree on a rusqlite release. Our MSRV
furthermore kept us from admitting recent releases, leaving downstream
users unable to pick up newer bundled SQLite versions.

Access SQLite through sqlite-thin instead, which declares the C API
itself and hence accepts any libsqlite3-sys release from 0.28 on,
leaving the choice to the application. It mirrors the subset of
rusqlite's API we use, so the store's logic remains unchanged.

As we no longer need to bundle SQLite on rusqlite's behalf, we move
building it from source to a separate storage-sqlite-bundled feature,
allowing users to link the system's SQLite via storage-sqlite alone.
The default and uniffi-default feature sets keep bundling it, so
their behavior remains unchanged.

Co-Authored-By: HAL 9000
@tnull
tnull requested a review from tankyleo December 4, 2026 16:03
@tnull tnull added this to the 0.8 milestone Dec 4, 2026
@tnull
tnull marked this pull request as ready for review December 4, 2026 16:04
@tnull
tnull force-pushed the 2026-11-explore-sqlite-thin branch from 01b9284 to 59847b1 Compare December 4, 2026 16:04
@tnull

tnull commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

sqlite-thin v0.1 was released earlier today, so I now added this to the v0.8 milestone.

Comment thread Cargo.toml
Comment on lines -55 to +56
storage-sqlite = ["dep:rusqlite"]
storage-sqlite = ["dep:sqlite-thin"]
storage-sqlite-bundled = ["storage-sqlite", "sqlite-thin/bundled"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here i'd prefer to have something like this:

storage-sqlite = ["storage-sqlite-system", "sqlite-thin/bundled"]
storage-sqlite-system = ["dep:sqlite-thin"]

This keeps something like

cargo build --no-default-features --features storage-sqlite

the same across this PR. Otherwise a user building with this command gets a dynamic link of SQLite, which they never asked for.

Comment thread README.md
Comment on lines +94 to +96
`storage-sqlite` links the SQLite library installed on the system, which hence needs to be
available at build time and at runtime. Enable `storage-sqlite-bundled` instead to build SQLite from
source and link it statically, which requires a C compiler.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to document here that with dynamic linking, database migrations requires at least SQLite 3.35.0. With 3.34.1, the v1 migration fails.

Comment thread README.md
LDK Node itself is written in [Rust][rust] and may therefore be natively added as a library dependency to any `std` Rust program. However, beyond its Rust API it also offers language bindings for [Swift][swift], [Kotlin][kotlin], and [Python][python] based on the [UniFFI](https://github.com/mozilla/uniffi-rs/).

## MSRV
The Minimum Supported Rust Version (MSRV) is currently 1.85.0.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that downstream applications can fail to build on 1.85.0 if they depend on LDK-node because the version 2 resolver would select libsqlite3-sys 0.40.0, which requires 1.88.

Users could work around this with

[resolver]
incompatible-rust-versions = "fallback"

or

CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=fallback

# Keep these endpoints in sync with the supported range in Cargo.toml.
# The oldest release sqlite-thin can share a libsqlite3-sys with, and the newest
# release line entirely compiling on our MSRV.
rusqlite: ["0.31.0", "0.39.0"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we could test the fallback mentioned above against an application that only depends on SQLite ldk-node, and does not specify a rustqlite dependency itself. Without the fallback cargo selects libsqlite3-sys 0.40.0 which does not build on MSRV 1.85.

# Keep these endpoints in sync with the supported range in Cargo.toml.
# The oldest release sqlite-thin can share a libsqlite3-sys with, and the newest
# release line entirely compiling on our MSRV.
rusqlite: ["0.31.0", "0.39.0"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here we could also replace v0.39.0 with v0.40.2 to test the full range available on MSRV

Comment on lines +1470 to +1473
("key_0", vec![42u8; 32]),
("key_1", Vec::new()),
("key_2", (0..=u8::MAX).collect()),
("key_3", (0..1024 * 1024).map(|i| (i % 251) as u8).collect()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Here can we move the keys around such that the expected newest-first order asserted below is different from reverse alphabetical order ? For example insert key_2 before key_1.

use crate::io::test_utils::{do_read_write_remove_list_persist, random_storage_path};

#[tokio::test]
async fn rwrl_post_schema_1_migration() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also cover this test in CI with system SQLite, for example such a test would fail with Debian 11’s SQLite 3.34.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants