Use sqlx inplace of rusqlite#242
Conversation
This is temporary to avoid a dependency conflict that arises in `libsqlite3-sys`
The two DBMs are now in use, this is an intermediary commit and `dbm.rs` should be deleted and replaced with `dbm_new.rs`, and the tower components should be adjusted accordingly (drop the mutex around the dbm since the pool handles it by itself & await on dbm methods since they are now async)
Some mutexed needed their lifetimes be shortened due to the fact that an std mutex guard can't live across await point. one specific mutex on GateKeeper::registered_users was more fitting to be a tokio mutex instead due to the nature of how it is uses beside the database usage (std mutex is generally better/has less overhead than a tokio mutex). tests still need to be patched for async
Postgres tests are still not working because with each test we need to spawn a brand new database (best done inside a docker container) from within the tests
| // This spawns a separate async actor in the background that will be fed new blocks from a sync block listener. | ||
| // In this way we can have our components listen to blocks in an async manner from the async actor. | ||
| let listener = AsyncBlockListener::wrap_listener(listeners, dbm); | ||
|
|
||
| let cache = &mut UnboundedCache::new(); | ||
| let spv_client = SpvClient::new(tip, poller, cache, listener); | ||
| let spv_client = SpvClient::new(tip, poller, cache, &listener); | ||
| let mut chain_monitor = ChainMonitor::new( |
There was a problem hiding this comment.
I think it's gonna be cleaner to merge all of these into the chain monitor.
And also merge async_listener.rs into chain_monitor.rs and change the chain monitor tests to accommodate the AsyncListen trait.
this still needs an external test db running on the background. one can easily be spawned with docker
remove rusqlite dependency from teos-common
c955cda to
782ed39
Compare
|
To test the tower with postgresql DB, this docker compose script might be handy: version: '3.1'
services:
db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: pass
POSTGRES_USER: user
POSTGRES_DB: teos |
orbitalturtle
left a comment
There was a problem hiding this comment.
Awesome PR :confetti: I'm looking forward to being able to use a postgres backend.
A couple thoughts from my first look:
- Would be nice to have a high-level explanation in the README (or another doc if the main README is getting to long?) explaining that both sqlite and postgres are now supported & how sqlite is the default, but users can use the
--database-urlconfig option to use Postgres, etc. - Commits could maybe be cleaned up a bit to be more self-contained, looks like some of the later ones can be squashed with the earlier ones.
Will give this a more thorough test run soon as well
teos/src/dbm.rs
Outdated
| )", | ||
| ]; | ||
|
|
||
| /// Packs the errors than can raise when interacting with the underlying database. |
| pub btc_rpc_port: u16, | ||
|
|
||
| // Database | ||
| pub database_url: String, |
There was a problem hiding this comment.
looks like a repeat of line 91
| #[structopt(long, default_value = "~/.teos")] | ||
| pub data_dir: String, | ||
|
|
||
| /// Database connection string [default: managed (managed SQLite database)] |
There was a problem hiding this comment.
Since this is an option and the default seems to be setting up a sqlite db, we could perhaps just let the default be "None" instead of the string "managed"?
teos/src/dbm_new.rs
Outdated
| #[cfg(feature = "postgres")] | ||
| return_db_if_matching!(connection_string, postgres, "migrations/postgres"); | ||
|
|
||
| let supported_dbs = vec![ |
teos/src/dbm_new.rs
Outdated
| .bind(user_info.subscription_expiry as i64) | ||
| .execute(&self.pool) | ||
| .await | ||
| .map(|_| ()) |
There was a problem hiding this comment.
? instead of .map to bubble up the error? (and same thing for some of the calls below)
Also the debug and error messages on successful/failed db insertions/etc are missing here and below
This PR replaces the
rusqliteSQLite engine withsqlx.sqlxis an asynchronous versatile sql engine that supports both SQLite & PostgreSQL.Due to the asynchronous-only nature of
sqlx, every method that interacted with the DB had to be asynchronous as well. This ripple effect propagated to nearly all the methods of the tower components. Also to thelightning::chain::Listentrait, thus a newAsyncListentrait was introduced to replace it.The sqlite driver is enabled with the
sqlitefeature, and the postgresql driver with thepostgresfeature.By default, both of them are enabled, meaning that the tower program can run on top of either a sqlite or postgresql db.
To enable postgres-only driver:
cargo build --no-default-features --features postgres(replacepostgreswithsqlitefor sqlite-only build)Fixes #32