22#![ allow( unused_imports) ]
33//! Multi-module tests that use database fixtures
44
5+ use std:: cell:: { RefCell , RefMut } ;
56use std:: sync:: Arc ;
67use std:: time:: { SystemTime , UNIX_EPOCH } ;
78use bitcoin:: { BlockHash , Network } ;
89use bitcoin:: secp256k1:: ecdsa:: Signature ;
910use bitcoin:: secp256k1:: { Secp256k1 , SecretKey } ;
1011use bitcoin:: hashes:: Hash ;
12+ use bitcoin:: hashes:: hex:: ToHex ;
1113use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
1214use lightning:: ln:: features:: ChannelFeatures ;
1315use lightning:: ln:: msgs:: { ChannelAnnouncement , ChannelUpdate , UnsignedChannelAnnouncement , UnsignedChannelUpdate } ;
@@ -21,6 +23,10 @@ use crate::types::{GossipMessage, tests::TestLogger};
2123
2224const CLIENT_BACKDATE_INTERVAL : u32 = 3600 * 24 * 7 ; // client backdates RGS by a week
2325
26+ thread_local ! {
27+ static DB_TEST_SCHEMA : RefCell <Option <String >> = RefCell :: new( None ) ;
28+ }
29+
2430fn blank_signature ( ) -> Signature {
2531 Signature :: from_compact ( & [ 0u8 ; 64 ] ) . unwrap ( )
2632}
@@ -33,6 +39,29 @@ fn current_time() -> u32 {
3339 SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) as u32
3440}
3541
42+ pub ( crate ) fn db_test_schema ( ) -> String {
43+ DB_TEST_SCHEMA . with ( |suffix_reference| {
44+ let mut suffix_option = suffix_reference. borrow_mut ( ) ;
45+ match suffix_option. as_ref ( ) {
46+ None => {
47+ let current_time = SystemTime :: now ( ) ;
48+ let unix_time = current_time. duration_since ( UNIX_EPOCH ) . expect ( "Time went backwards" ) ;
49+ let timestamp_seconds = unix_time. as_secs ( ) ;
50+ let timestamp_nanos = unix_time. as_nanos ( ) ;
51+ let preimage = format ! ( "{}" , timestamp_nanos) ;
52+ let suffix = Sha256dHash :: hash ( preimage. as_bytes ( ) ) . into_inner ( ) . to_hex ( ) ;
53+ // the schema must start with a letter
54+ let schema = format ! ( "test_{}_{}" , timestamp_seconds, suffix) ;
55+ suffix_option. replace ( schema. clone ( ) ) ;
56+ schema
57+ }
58+ Some ( suffix) => {
59+ suffix. clone ( )
60+ }
61+ }
62+ } )
63+ }
64+
3665fn generate_announcement ( short_channel_id : u64 ) -> ChannelAnnouncement {
3766 let secp_context = Secp256k1 :: new ( ) ;
3867
@@ -88,25 +117,13 @@ fn generate_update(scid: u64, direction: bool, timestamp: u32, expiry_delta: u16
88117}
89118
90119async fn clean_test_db ( ) {
91- let connection_config = config:: db_connection_config ( ) ;
92- let ( client, connection) = connection_config. connect ( NoTls ) . await . unwrap ( ) ;
93-
94- tokio:: spawn ( async move {
95- if let Err ( e) = connection. await {
96- panic ! ( "connection error: {}" , e) ;
97- }
98- } ) ;
99-
100- client. query ( "TRUNCATE TABLE channel_announcements RESTART IDENTITY CASCADE" , & [ ] ) . await . unwrap ( ) ;
101- client. query ( "TRUNCATE TABLE channel_updates RESTART IDENTITY CASCADE" , & [ ] ) . await . unwrap ( ) ;
102- client. query ( "TRUNCATE TABLE config RESTART IDENTITY CASCADE" , & [ ] ) . await . unwrap ( ) ;
120+ let client = crate :: connect_to_db ( ) . await ;
121+ let schema = db_test_schema ( ) ;
122+ client. execute ( & format ! ( "DROP SCHEMA IF EXISTS {} CASCADE" , schema) , & [ ] ) . await . unwrap ( ) ;
103123}
104124
105125#[ tokio:: test]
106126async fn test_trivial_setup ( ) {
107- // start off with a clean slate
108- clean_test_db ( ) . await ;
109-
110127 let logger = Arc :: new ( TestLogger :: new ( ) ) ;
111128 let network_graph = NetworkGraph :: new ( Network :: Bitcoin , logger. clone ( ) ) ;
112129 let network_graph_arc = Arc :: new ( network_graph) ;
@@ -149,12 +166,12 @@ async fn test_trivial_setup() {
149166 let update_result = rgs. update_network_graph ( & serialization. data ) . unwrap ( ) ;
150167 println ! ( "update result: {}" , update_result) ;
151168 // the update result must be a multiple of our snapshot granularity
152- assert_eq ! ( update_result % config:: SNAPSHOT_CALCULATION_INTERVAL , 0 ) ;
169+ assert_eq ! ( update_result % config:: snapshot_generation_interval ( ) , 0 ) ;
153170 assert ! ( update_result < timestamp) ;
154171
155172 let timestamp_delta = timestamp - update_result;
156173 println ! ( "timestamp delta: {}" , timestamp_delta) ;
157- assert ! ( timestamp_delta < config:: SNAPSHOT_CALCULATION_INTERVAL ) ;
174+ assert ! ( timestamp_delta < config:: snapshot_generation_interval ( ) ) ;
158175
159176 let readonly_graph = client_graph_arc. read_only ( ) ;
160177 let channels = readonly_graph. channels ( ) ;
@@ -200,7 +217,7 @@ async fn test_unidirectional_intermediate_update_consideration() {
200217 network_graph_arc. update_channel_unsigned ( & update_3. contents ) . unwrap ( ) ;
201218 network_graph_arc. update_channel_unsigned ( & update_4. contents ) . unwrap ( ) ;
202219
203- receiver. send ( GossipMessage :: ChannelAnnouncement ( announcement) ) . await . unwrap ( ) ;
220+ receiver. send ( GossipMessage :: ChannelAnnouncement ( announcement, Some ( current_timestamp ) ) ) . await . unwrap ( ) ;
204221 receiver. send ( GossipMessage :: ChannelUpdate ( update_1) ) . await . unwrap ( ) ;
205222 receiver. send ( GossipMessage :: ChannelUpdate ( update_2) ) . await . unwrap ( ) ;
206223 receiver. send ( GossipMessage :: ChannelUpdate ( update_3) ) . await . unwrap ( ) ;
@@ -247,7 +264,7 @@ async fn test_unidirectional_intermediate_update_consideration() {
247264 println ! ( "last sync timestamp: {}" , last_sync_timestamp) ;
248265 println ! ( "next timestamp: {}" , next_timestamp) ;
249266 // the update result must be a multiple of our snapshot granularity
250- assert_eq ! ( next_timestamp % config:: SNAPSHOT_CALCULATION_INTERVAL , 0 ) ;
267+ assert_eq ! ( next_timestamp % config:: snapshot_generation_interval ( ) , 0 ) ;
251268
252269 let readonly_graph = client_graph_arc. read_only ( ) ;
253270 let channels = readonly_graph. channels ( ) ;
0 commit comments