Skip to content

Commit 67362bf

Browse files
committed
Configurable test store
To enable more realistic testing with sqlite as a backend.
1 parent dd51908 commit 67362bf

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

tests/common/mod.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ use electrsd::corepc_node::{Client as BitcoindClient, Node as BitcoinD};
2929
use electrsd::{corepc_node, ElectrsD};
3030
use electrum_client::ElectrumApi;
3131
use ldk_node::config::{AsyncPaymentsRole, Config, ElectrumSyncConfig, EsploraSyncConfig};
32-
use ldk_node::io::sqlite_store::SqliteStore;
32+
use ldk_node::io::sqlite_store::{SqliteStore, KV_TABLE_NAME, SQLITE_DB_FILE_NAME};
3333
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
3434
use ldk_node::{
35-
Builder, CustomTlvRecord, Event, LightningBalance, Node, NodeError, PendingSweepBalance,
35+
Builder, CustomTlvRecord, DynStore, Event, LightningBalance, Node, NodeError,
36+
PendingSweepBalance,
3637
};
3738
use lightning::io;
3839
use lightning::ln::msgs::SocketAddress;
@@ -262,10 +263,23 @@ pub(crate) enum TestChainSource<'a> {
262263
BitcoindRestSync(&'a BitcoinD),
263264
}
264265

266+
#[derive(Clone, Copy)]
267+
pub(crate) enum TestStoreType {
268+
InMemory,
269+
Sqlite,
270+
}
271+
272+
impl Default for TestStoreType {
273+
fn default() -> Self {
274+
TestStoreType::InMemory
275+
}
276+
}
277+
265278
#[derive(Clone, Default)]
266279
pub(crate) struct TestConfig {
267280
pub node_config: Config,
268281
pub log_writer: TestLogWriter,
282+
pub store_type: TestStoreType,
269283
}
270284

271285
macro_rules! setup_builder {
@@ -282,13 +296,28 @@ pub(crate) use setup_builder;
282296
pub(crate) fn setup_two_nodes(
283297
chain_source: &TestChainSource, allow_0conf: bool, anchor_channels: bool,
284298
anchors_trusted_no_reserve: bool,
299+
) -> (TestNode, TestNode) {
300+
setup_two_nodes_with_store(
301+
chain_source,
302+
allow_0conf,
303+
anchor_channels,
304+
anchors_trusted_no_reserve,
305+
TestStoreType::InMemory,
306+
)
307+
}
308+
309+
pub(crate) fn setup_two_nodes_with_store(
310+
chain_source: &TestChainSource, allow_0conf: bool, anchor_channels: bool,
311+
anchors_trusted_no_reserve: bool, store_type: TestStoreType,
285312
) -> (TestNode, TestNode) {
286313
println!("== Node A ==");
287-
let config_a = random_config(anchor_channels);
314+
let mut config_a = random_config(anchor_channels);
315+
config_a.store_type = store_type;
288316
let node_a = setup_node(chain_source, config_a, None);
289317

290318
println!("\n== Node B ==");
291319
let mut config_b = random_config(anchor_channels);
320+
config_b.store_type = store_type;
292321
if allow_0conf {
293322
config_b.node_config.trusted_peers_0conf.push(node_a.node_id());
294323
}
@@ -381,8 +410,21 @@ pub(crate) fn setup_node_for_async_payments(
381410

382411
builder.set_async_payments_role(async_payments_role).unwrap();
383412

384-
let test_sync_store = Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.into()));
385-
let node = builder.build_with_store(test_sync_store).unwrap();
413+
let kv_store: Arc<DynStore> = match config.store_type {
414+
TestStoreType::InMemory => {
415+
Arc::new(TestSyncStore::new(config.node_config.storage_dir_path.into()))
416+
},
417+
TestStoreType::Sqlite => Arc::new(
418+
SqliteStore::new(
419+
config.node_config.storage_dir_path.into(),
420+
Some(SQLITE_DB_FILE_NAME.to_string()),
421+
Some(KV_TABLE_NAME.to_string()),
422+
)
423+
.unwrap(),
424+
),
425+
};
426+
427+
let node = builder.build_with_store(kv_store).unwrap();
386428
node.start().unwrap();
387429
assert!(node.status().is_running);
388430
assert!(node.status().latest_fee_rate_cache_update_timestamp.is_some());

0 commit comments

Comments
 (0)