Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/postgres-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CI Checks - PostgreSQL Integration Tests

on: [push, pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-and-test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest
ports:
- 5432:5432
env:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- name: Checkout code
uses: actions/checkout@v3
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think there are newer action versions available? Cf #861.

- name: Install Rust stable toolchain
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal --default-toolchain stable
- name: Run PostgreSQL store tests
env:
TEST_POSTGRES_URL: "host=localhost user=postgres password=postgres"
run: cargo test --features postgres io::postgres_store
- name: Run PostgreSQL integration tests
env:
TEST_POSTGRES_URL: "host=localhost user=postgres password=postgres"
run: |
RUSTFLAGS="--cfg no_download --cfg cycle_tests" cargo test --features postgres --test integration_tests_postgres
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ codegen-units = 1 # Reduce number of codegen units to increase optimizations.
panic = 'abort' # Abort on panic

[features]
default = []
default = ["sqlite"]
sqlite = ["dep:rusqlite"]
postgres = ["dep:tokio-postgres"]

[dependencies]
#lightning = { version = "0.2.0", features = ["std"] }
Expand Down Expand Up @@ -58,7 +60,7 @@ bdk_wallet = { version = "2.3.0", default-features = false, features = ["std", "

bitreq = { version = "0.3", default-features = false, features = ["async-https", "json-using-serde"] }
rustls = { version = "0.23", default-features = false }
rusqlite = { version = "0.31.0", features = ["bundled"] }
rusqlite = { version = "0.31.0", features = ["bundled"], optional = true }
bitcoin = "0.32.7"
bip39 = { version = "2.0.0", features = ["rand"] }
bip21 = { version = "0.5", features = ["std"], default-features = false }
Expand All @@ -76,6 +78,7 @@ serde_json = { version = "1.0.128", default-features = false, features = ["std"]
log = { version = "0.4.22", default-features = false, features = ["std"]}

async-trait = { version = "0.1", default-features = false }
tokio-postgres = { version = "0.7", default-features = false, features = ["runtime"], optional = true }
vss-client = { package = "vss-client-ng", version = "0.5" }
prost = { version = "0.11.6", default-features = false}
#bitcoin-payment-instructions = { version = "0.6" }
Expand Down
41 changes: 40 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ use crate::entropy::NodeEntropy;
use crate::event::EventQueue;
use crate::fee_estimator::OnchainFeeEstimator;
use crate::gossip::GossipSource;
#[cfg(feature = "sqlite")]
use crate::io;
#[cfg(feature = "sqlite")]
use crate::io::sqlite_store::SqliteStore;
use crate::io::utils::{
read_event_queue, read_external_pathfinding_scores_from_cache, read_network_graph,
Expand All @@ -61,7 +64,7 @@ use crate::io::utils::{
};
use crate::io::vss_store::VssStoreBuilder;
use crate::io::{
self, PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE, PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE,
PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE, PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE,
PENDING_PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE,
PENDING_PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE,
};
Expand Down Expand Up @@ -616,6 +619,7 @@ impl NodeBuilder {

/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
/// previously configured.
#[cfg(feature = "sqlite")]
pub fn build(&self, node_entropy: NodeEntropy) -> Result<Node, BuildError> {
let storage_dir_path = self.config.storage_dir_path.clone();
fs::create_dir_all(storage_dir_path.clone())
Expand All @@ -629,6 +633,24 @@ impl NodeBuilder {
self.build_with_store(node_entropy, kv_store)
}

/// Builds a [`Node`] instance with a [PostgreSQL] backend and according to the options
/// previously configured.
///
/// Connects to the PostgreSQL database at the given `connection_string`.
/// The given `kv_table_name` will be used or default to
/// [`DEFAULT_KV_TABLE_NAME`](crate::io::postgres_store::DEFAULT_KV_TABLE_NAME).
///
/// [PostgreSQL]: https://www.postgresql.org
#[cfg(feature = "postgres")]
pub fn build_with_postgres_store(
&self, node_entropy: NodeEntropy, connection_string: &str, kv_table_name: Option<String>,
) -> Result<Node, BuildError> {
let kv_store =
crate::io::postgres_store::PostgresStore::new(connection_string, kv_table_name)
.map_err(|_| BuildError::KVStoreSetupFailed)?;
self.build_with_store(node_entropy, kv_store)
}

/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
/// previously configured.
pub fn build_with_fs_store(&self, node_entropy: NodeEntropy) -> Result<Node, BuildError> {
Expand Down Expand Up @@ -1083,10 +1105,27 @@ impl ArcedNodeBuilder {

/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
/// previously configured.
#[cfg(feature = "sqlite")]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we suddenly introduce features? We so far held off and meant to do that in a dedicated PR at some point for this or next release. Is it necessary to make Postgres work?

pub fn build(&self, node_entropy: Arc<NodeEntropy>) -> Result<Arc<Node>, BuildError> {
self.inner.read().unwrap().build(*node_entropy).map(Arc::new)
}

/// Builds a [`Node`] instance with a [PostgreSQL] backend and according to the options
/// previously configured.
///
/// [PostgreSQL]: https://www.postgresql.org
#[cfg(feature = "postgres")]
pub fn build_with_postgres_store(
&self, node_entropy: Arc<NodeEntropy>, connection_string: String,
kv_table_name: Option<String>,
) -> Result<Arc<Node>, BuildError> {
self.inner
.read()
.unwrap()
.build_with_postgres_store(*node_entropy, &connection_string, kv_table_name)
.map(Arc::new)
}

/// Builds a [`Node`] instance with a [`FilesystemStore`] backend and according to the options
/// previously configured.
pub fn build_with_fs_store(
Expand Down
3 changes: 3 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

//! Objects and traits for data persistence.

#[cfg(feature = "postgres")]
pub mod postgres_store;
#[cfg(feature = "sqlite")]
pub mod sqlite_store;
#[cfg(test)]
pub(crate) mod test_utils;
Expand Down
21 changes: 21 additions & 0 deletions src/io/postgres_store/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.

use lightning::io;
use tokio_postgres::Client;

pub(super) async fn migrate_schema(
_client: &Client, _kv_table_name: &str, from_version: u16, to_version: u16,
) -> io::Result<()> {
assert!(from_version < to_version);
// Future migrations go here, e.g.:
// if from_version == 1 && to_version >= 2 {
// migrate_v1_to_v2(client, kv_table_name).await?;
// from_version = 2;
// }
Ok(())
}
Loading
Loading